~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to j2ee/ejbjarproject/src/org/netbeans/modules/j2ee/ejbjarproject/ui/customizer/J2eePlatformUiSupport.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.j2ee.ejbjarproject.ui.customizer;
 
43
 
 
44
import java.util.ArrayList;
 
45
import java.util.List;
 
46
import java.util.Set;
 
47
import java.util.TreeSet;
 
48
import javax.swing.AbstractListModel;
 
49
import javax.swing.ComboBoxModel;
 
50
import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
 
51
import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
 
52
import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
 
53
import org.openide.util.NbBundle;
 
54
 
 
55
/**
 
56
 *
 
57
 * @author Andrei Badea
 
58
 */
 
59
public class J2eePlatformUiSupport {
 
60
    
 
61
    private static final String JAVA_EE_5_DISPLAY_NAME = NbBundle.getMessage(J2eePlatformUiSupport.class, "JAVA_EE_5_displayName"); // NOI18N 
 
62
    private static final String J2EE_1_4_DISPLAY_NAME = NbBundle.getMessage(J2eePlatformUiSupport.class, "J2EE_1_4_displayName"); // NOI18N 
 
63
    private static final String J2EE_1_3_DISPLAY_NAME = NbBundle.getMessage(J2eePlatformUiSupport.class, "J2EE_1_3_displayName"); // NOI18N  
 
64
 
 
65
    private J2eePlatformUiSupport() {
 
66
    }
 
67
    
 
68
    public static ComboBoxModel createPlatformComboBoxModel(String serverInstanceId) {
 
69
        return new J2eePlatformComboBoxModel(serverInstanceId);
 
70
    }
 
71
    
 
72
    public static String getServerInstanceID(Object j2eePlatformModelObject) {
 
73
        if (j2eePlatformModelObject == null)
 
74
            return null;
 
75
 
 
76
        J2eePlatform j2eePlatform = ((J2eePlatformAdapter)j2eePlatformModelObject).getJ2eePlatform();
 
77
        String[] serverInstanceIDs = Deployment.getDefault().getServerInstanceIDs();
 
78
        for (int i = 0; i < serverInstanceIDs.length; i++) {
 
79
            J2eePlatform platform = Deployment.getDefault().getJ2eePlatform(serverInstanceIDs[i]);
 
80
            if (platform != null && platform.getDisplayName().equals(j2eePlatform.getDisplayName())) {
 
81
                return serverInstanceIDs[i];
 
82
            }
 
83
        }
 
84
        
 
85
        return null;
 
86
    }
 
87
    
 
88
    public static ComboBoxModel createSpecVersionComboBoxModel(String j2eeSpecVersion) {
 
89
        return new J2eeSpecVersionComboBoxModel(j2eeSpecVersion);
 
90
    }
 
91
    
 
92
    public static String getSpecVersion(Object j2eeSpecVersionModelObject) {
 
93
        return ((J2eePlatformComboBoxItem)j2eeSpecVersionModelObject).getCode();
 
94
    }
 
95
    
 
96
    private static final class J2eePlatformComboBoxModel extends AbstractListModel implements ComboBoxModel {
 
97
        private J2eePlatformAdapter[] j2eePlatforms;
 
98
        private String initialJ2eePlatform;
 
99
        private J2eePlatformAdapter selectedJ2eePlatform;
 
100
        
 
101
        public J2eePlatformComboBoxModel(String serverInstanceID) {
 
102
            initialJ2eePlatform = serverInstanceID;
 
103
            getJ2eePlatforms();
 
104
        }
 
105
        
 
106
        public Object getElementAt(int index) {
 
107
            return getJ2eePlatforms()[index];
 
108
        }
 
109
 
 
110
        public int getSize() {
 
111
            return getJ2eePlatforms().length;
 
112
        }
 
113
        
 
114
        public Object getSelectedItem() {
 
115
            return selectedJ2eePlatform;
 
116
        }
 
117
        
 
118
        public void setSelectedItem(Object obj) {
 
119
            selectedJ2eePlatform = (J2eePlatformAdapter)obj;
 
120
        }
 
121
        
 
122
        private synchronized J2eePlatformAdapter[] getJ2eePlatforms() {
 
123
            if (j2eePlatforms == null) {
 
124
                String[] serverInstanceIDs = Deployment.getDefault().getServerInstanceIDs();
 
125
                Set orderedNames = new TreeSet();
 
126
                boolean activeFound = false;
 
127
 
 
128
                for (int i = 0; i < serverInstanceIDs.length; i++) {
 
129
                    J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(serverInstanceIDs[i]);
 
130
                    if (j2eePlatform != null) {
 
131
                        if (j2eePlatform.getSupportedModuleTypes().contains(J2eeModule.EJB)) {
 
132
                            J2eePlatformAdapter adapter = new J2eePlatformAdapter(j2eePlatform);
 
133
                            orderedNames.add(adapter);
 
134
 
 
135
                            if (selectedJ2eePlatform == null && !activeFound && initialJ2eePlatform != null) {
 
136
                                if (serverInstanceIDs[i].equals(initialJ2eePlatform)) {
 
137
                                    selectedJ2eePlatform = adapter;
 
138
                                    activeFound = true;
 
139
                                }
 
140
                            }
 
141
                        }
 
142
                    }
 
143
                }
 
144
                //j2eePlatforms = (J2eePlatform[])orderedNames.values().toArray(new J2eePlatform[orderedNames.size()]);
 
145
                j2eePlatforms = (J2eePlatformAdapter[])orderedNames.toArray(new J2eePlatformAdapter[orderedNames.size()]);
 
146
            }
 
147
            return j2eePlatforms;
 
148
        }
 
149
     }
 
150
    
 
151
    private static final class J2eeSpecVersionComboBoxModel extends AbstractListModel implements ComboBoxModel {
 
152
        private J2eePlatformComboBoxItem[] j2eeSpecVersions;
 
153
        
 
154
        private J2eePlatformComboBoxItem initialJ2eeSpecVersion;
 
155
        private J2eePlatformComboBoxItem selectedJ2eeSpecVersion;
 
156
    
 
157
        public J2eeSpecVersionComboBoxModel(String j2eeSpecVersion) {
 
158
            initialJ2eeSpecVersion = new J2eePlatformComboBoxItem(j2eeSpecVersion);
 
159
            
 
160
            List orderedListItems = new ArrayList();
 
161
            orderedListItems.add(new J2eePlatformComboBoxItem(EjbJarProjectProperties.JAVA_EE_5));
 
162
            orderedListItems.add(new J2eePlatformComboBoxItem(EjbJarProjectProperties.J2EE_1_4));
 
163
            if (!initialJ2eeSpecVersion.getCode().equals(EjbJarProjectProperties.JAVA_EE_5) &&
 
164
                    !initialJ2eeSpecVersion.getCode().equals(EjbJarProjectProperties.J2EE_1_4))
 
165
                orderedListItems.add(0, new J2eePlatformComboBoxItem(EjbJarProjectProperties.J2EE_1_3));
 
166
            
 
167
            j2eeSpecVersions = (J2eePlatformComboBoxItem[])orderedListItems.toArray(new J2eePlatformComboBoxItem[orderedListItems.size()]);
 
168
            selectedJ2eeSpecVersion = initialJ2eeSpecVersion;
 
169
        }
 
170
        
 
171
        public Object getElementAt(int index) {
 
172
            return j2eeSpecVersions[index];
 
173
        }
 
174
        
 
175
        public int getSize() {
 
176
            return j2eeSpecVersions.length;
 
177
        }
 
178
        
 
179
        public Object getSelectedItem() {
 
180
            return selectedJ2eeSpecVersion;
 
181
        }
 
182
        
 
183
        public void setSelectedItem(Object obj) {
 
184
            selectedJ2eeSpecVersion = (J2eePlatformComboBoxItem)obj;
 
185
        }
 
186
    }
 
187
    
 
188
    private static final class J2eePlatformComboBoxItem{
 
189
        private String code;
 
190
        private String displayName;
 
191
 
 
192
        public J2eePlatformComboBoxItem (String code, String displayName){
 
193
            this.code = code;
 
194
            this.displayName = displayName;        
 
195
        }
 
196
 
 
197
        public J2eePlatformComboBoxItem (String code){
 
198
             this(code, findDisplayName(code));        
 
199
        }
 
200
 
 
201
        private static String findDisplayName(String code){
 
202
            if(code.equals(EjbJarProjectProperties.JAVA_EE_5)) return JAVA_EE_5_DISPLAY_NAME;
 
203
            if(code.equals(EjbJarProjectProperties.J2EE_1_4)) return J2EE_1_4_DISPLAY_NAME;
 
204
            if(code.equals(EjbJarProjectProperties.J2EE_1_3)) return J2EE_1_3_DISPLAY_NAME;
 
205
            return code; //version display name not found, use the version code for display name        
 
206
        }
 
207
 
 
208
        public String getCode(){
 
209
            return code;        
 
210
        }
 
211
 
 
212
        public String toString(){
 
213
            return displayName;        
 
214
        }
 
215
    }
 
216
 
 
217
    public static boolean getJ2eePlatformAndSpecVersionMatch(Object j2eePlatformModelObject, Object j2eeSpecVersionModelObject) {
 
218
        if (!(j2eePlatformModelObject instanceof J2eePlatformAdapter && j2eeSpecVersionModelObject instanceof String))
 
219
            return false;
 
220
        
 
221
        J2eePlatform j2eePlatform = ((J2eePlatformAdapter)j2eePlatformModelObject).getJ2eePlatform();
 
222
        String specVersion = (String)j2eeSpecVersionModelObject;
 
223
        return j2eePlatform.getSupportedSpecVersions(J2eeModule.EJB).contains(specVersion);
 
224
    }
 
225
    
 
226
    private static final class J2eePlatformAdapter implements Comparable {
 
227
        private J2eePlatform platform;
 
228
        
 
229
        public J2eePlatformAdapter(J2eePlatform platform) {
 
230
            this.platform = platform;
 
231
        }
 
232
        
 
233
        public J2eePlatform getJ2eePlatform() {
 
234
            return platform;
 
235
        }
 
236
        
 
237
        public String toString() {
 
238
            return platform.getDisplayName();
 
239
        }
 
240
 
 
241
        public int compareTo(Object o) {
 
242
            J2eePlatformAdapter oa = (J2eePlatformAdapter)o;
 
243
            return toString().compareTo(oa.toString());
 
244
        }
 
245
    }
 
246
}