KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > clientproject > ui > customizer > J2eePlatformUiSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.clientproject.ui.customizer;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.TreeSet JavaDoc;
26 import javax.swing.AbstractListModel JavaDoc;
27 import javax.swing.ComboBoxModel JavaDoc;
28 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
29 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
30 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
31 import org.openide.util.NbBundle;
32
33 /**
34  * @author Andrei Badea
35  */

36 public class J2eePlatformUiSupport {
37     
38     private static final String JavaDoc JAVA_EE_5_DISPLAY_NAME = NbBundle.getMessage(J2eePlatformUiSupport.class, "JAVA_EE_5_displayName"); // NOI18N
39
private static final String JavaDoc J2EE_1_4_DISPLAY_NAME = NbBundle.getMessage(J2eePlatformUiSupport.class, "J2EE_1_4_displayName"); // NOI18N
40
private static final String JavaDoc J2EE_1_3_DISPLAY_NAME = NbBundle.getMessage(J2eePlatformUiSupport.class, "J2EE_1_3_displayName"); // NOI18N
41

42     private J2eePlatformUiSupport() {
43     }
44     
45     public static ComboBoxModel JavaDoc createPlatformComboBoxModel(String JavaDoc serverInstanceId, String JavaDoc j2eeLevel) {
46         return new J2eePlatformComboBoxModel(serverInstanceId, j2eeLevel);
47     }
48     
49     public static String JavaDoc getServerInstanceID(Object JavaDoc j2eePlatformModelObject) {
50         if (j2eePlatformModelObject == null)
51             return null;
52
53         J2eePlatform j2eePlatform = ((J2eePlatformAdapter)j2eePlatformModelObject).getJ2eePlatform();
54         String JavaDoc[] serverInstanceIDs = Deployment.getDefault().getServerInstanceIDs();
55         for (int i = 0; i < serverInstanceIDs.length; i++) {
56             J2eePlatform platform = Deployment.getDefault().getJ2eePlatform(serverInstanceIDs[i]);
57             if (platform != null && platform.getDisplayName().equals(j2eePlatform.getDisplayName())) {
58                 return serverInstanceIDs[i];
59             }
60         }
61         
62         return null;
63     }
64     
65     public static ComboBoxModel JavaDoc createSpecVersionComboBoxModel(String JavaDoc j2eeSpecVersion) {
66         return new J2eeSpecVersionComboBoxModel(j2eeSpecVersion);
67     }
68     
69     public static String JavaDoc getSpecVersion(Object JavaDoc j2eeSpecVersionModelObject) {
70         return ((J2eePlatformComboBoxItem)j2eeSpecVersionModelObject).getCode();
71     }
72     
73     private static final class J2eePlatformComboBoxModel extends AbstractListModel JavaDoc implements ComboBoxModel JavaDoc {
74         
75         private J2eePlatformAdapter[] j2eePlatforms;
76         private final String JavaDoc initialJ2eePlatform;
77         private J2eePlatformAdapter selectedJ2eePlatform;
78         private final String JavaDoc j2eeLevel;
79         
80         public J2eePlatformComboBoxModel(String JavaDoc serverInstanceID, String JavaDoc j2eeLevel) {
81             initialJ2eePlatform = serverInstanceID;
82             this.j2eeLevel = j2eeLevel;
83             getJ2eePlatforms();
84         }
85         
86         public Object JavaDoc getElementAt(int index) {
87             return getJ2eePlatforms()[index];
88         }
89
90         public int getSize() {
91             return getJ2eePlatforms().length;
92         }
93         
94         public Object JavaDoc getSelectedItem() {
95             return selectedJ2eePlatform;
96         }
97         
98         public void setSelectedItem(Object JavaDoc obj) {
99             selectedJ2eePlatform = (J2eePlatformAdapter)obj;
100         }
101         
102         private synchronized J2eePlatformAdapter[] getJ2eePlatforms() {
103             if (j2eePlatforms == null) {
104                 String JavaDoc[] serverInstanceIDs = Deployment.getDefault().getServerInstanceIDs();
105                 Set JavaDoc<J2eePlatformAdapter> orderedNames = new TreeSet JavaDoc<J2eePlatformAdapter>();
106                 boolean activeFound = false;
107
108                 for (int i = 0; i < serverInstanceIDs.length; i++) {
109                     J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(serverInstanceIDs[i]);
110                     if (j2eePlatform != null) {
111                         if (j2eePlatform.getSupportedModuleTypes().contains(J2eeModule.CLIENT)
112                                 && j2eePlatform.getSupportedSpecVersions(J2eeModule.CLIENT).contains(j2eeLevel)) {
113                             J2eePlatformAdapter adapter = new J2eePlatformAdapter(j2eePlatform);
114                             orderedNames.add(adapter);
115                         
116                             if (selectedJ2eePlatform == null && !activeFound && initialJ2eePlatform != null) {
117                                 if (serverInstanceIDs[i].equals(initialJ2eePlatform)) {
118                                     selectedJ2eePlatform = adapter;
119                                     activeFound = true;
120                                 }
121                             }
122                         }
123                     }
124                 }
125                 //j2eePlatforms = (J2eePlatform[])orderedNames.values().toArray(new J2eePlatform[orderedNames.size()]);
126
j2eePlatforms = orderedNames.toArray(new J2eePlatformAdapter[orderedNames.size()]);
127             }
128             return j2eePlatforms;
129         }
130      }
131     
132     private static final class J2eeSpecVersionComboBoxModel extends AbstractListModel JavaDoc implements ComboBoxModel JavaDoc {
133         
134         private J2eePlatformComboBoxItem[] j2eeSpecVersions;
135         private final J2eePlatformComboBoxItem initialJ2eeSpecVersion;
136         private J2eePlatformComboBoxItem selectedJ2eeSpecVersion;
137     
138         public J2eeSpecVersionComboBoxModel(String JavaDoc j2eeSpecVersion) {
139             initialJ2eeSpecVersion = new J2eePlatformComboBoxItem(j2eeSpecVersion);
140             
141             List JavaDoc<J2eePlatformComboBoxItem> orderedListItems = new ArrayList JavaDoc<J2eePlatformComboBoxItem>();
142             orderedListItems.add(new J2eePlatformComboBoxItem(AppClientProjectProperties.JAVA_EE_5));
143             orderedListItems.add(new J2eePlatformComboBoxItem(AppClientProjectProperties.J2EE_1_4));
144             if (!initialJ2eeSpecVersion.getCode().equals(AppClientProjectProperties.JAVA_EE_5) &&
145                     !initialJ2eeSpecVersion.getCode().equals(AppClientProjectProperties.J2EE_1_4))
146                 orderedListItems.add(0, new J2eePlatformComboBoxItem(AppClientProjectProperties.J2EE_1_3));
147             
148             j2eeSpecVersions = orderedListItems.toArray(new J2eePlatformComboBoxItem[orderedListItems.size()]);
149             selectedJ2eeSpecVersion = initialJ2eeSpecVersion;
150         }
151         
152         public Object JavaDoc getElementAt(int index) {
153             return j2eeSpecVersions[index];
154         }
155         
156         public int getSize() {
157             return j2eeSpecVersions.length;
158         }
159         
160         public Object JavaDoc getSelectedItem() {
161             return selectedJ2eeSpecVersion;
162         }
163         
164         public void setSelectedItem(Object JavaDoc obj) {
165             selectedJ2eeSpecVersion = (J2eePlatformComboBoxItem)obj;
166         }
167     }
168     
169     private static final class J2eePlatformComboBoxItem{
170         
171         private final String JavaDoc code;
172         private final String JavaDoc displayName;
173
174         public J2eePlatformComboBoxItem (String JavaDoc code, String JavaDoc displayName){
175             this.code = code;
176             this.displayName = displayName;
177         }
178
179         public J2eePlatformComboBoxItem (String JavaDoc code){
180              this(code, findDisplayName(code));
181         }
182
183         private static String JavaDoc findDisplayName(String JavaDoc code){
184             if(code.equals(AppClientProjectProperties.JAVA_EE_5)) return JAVA_EE_5_DISPLAY_NAME;
185             if(code.equals(AppClientProjectProperties.J2EE_1_4)) return J2EE_1_4_DISPLAY_NAME;
186             if(code.equals(AppClientProjectProperties.J2EE_1_3)) return J2EE_1_3_DISPLAY_NAME;
187             return code; //version display name not found, use the version code for display name
188
}
189
190         public String JavaDoc getCode(){
191             return code;
192         }
193
194         public String JavaDoc toString(){
195             return displayName;
196         }
197     }
198
199     public static boolean getJ2eePlatformAndSpecVersionMatch(Object JavaDoc j2eePlatformModelObject, Object JavaDoc j2eeSpecVersionModelObject) {
200         if (!(j2eePlatformModelObject instanceof J2eePlatformAdapter && j2eeSpecVersionModelObject instanceof String JavaDoc))
201             return false;
202         
203         J2eePlatform j2eePlatform = ((J2eePlatformAdapter)j2eePlatformModelObject).getJ2eePlatform();
204         String JavaDoc specVersion = (String JavaDoc)j2eeSpecVersionModelObject;
205         return j2eePlatform.getSupportedSpecVersions(J2eeModule.CLIENT).contains(specVersion);
206     }
207     
208     private static final class J2eePlatformAdapter implements Comparable JavaDoc {
209         
210         private final J2eePlatform platform;
211         
212         public J2eePlatformAdapter(J2eePlatform platform) {
213             this.platform = platform;
214         }
215         
216         public J2eePlatform getJ2eePlatform() {
217             return platform;
218         }
219         
220         public String JavaDoc toString() {
221             return platform.getDisplayName();
222         }
223
224         public int compareTo(Object JavaDoc o) {
225             J2eePlatformAdapter oa = (J2eePlatformAdapter)o;
226             return toString().compareTo(oa.toString());
227         }
228     }
229 }
230
Popular Tags