KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > 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.ejbjarproject.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  *
35  * @author Andrei Badea
36  */

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

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