KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > archive > ui > JavaEePlatformUiSupport


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
21 package org.netbeans.modules.j2ee.archive.ui;
22
23 import java.util.Set JavaDoc;
24 import java.util.TreeSet JavaDoc;
25 import javax.swing.ComboBoxModel JavaDoc;
26 import javax.swing.ListModel JavaDoc;
27 import javax.swing.event.ListDataListener 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
32 /**
33  *
34  * @author Andrei Badea
35  * @thief vince kraemer
36  */

37 public class JavaEePlatformUiSupport {
38     
39     private JavaEePlatformUiSupport() {
40     }
41     
42     public static ComboBoxModel JavaDoc createPlatformComboBoxModel(String JavaDoc serverInstanceId) {
43         return new JavaEePlatformComboBoxModel(serverInstanceId);
44     }
45     
46     public static String JavaDoc getServerInstanceID(Object JavaDoc j2eePlatformModelObject) {
47         String JavaDoc retVal = null;
48         if (j2eePlatformModelObject != null) {
49             
50             J2eePlatform j2eePlatform = ((JavaEePlatformAdapter)j2eePlatformModelObject).getJ2eePlatform();
51             String JavaDoc[] serverInstanceIDs = Deployment.getDefault().getServerInstanceIDs();
52             for (int i = 0; retVal == null && i < serverInstanceIDs.length; i++) {
53                 J2eePlatform platform = Deployment.getDefault().getJ2eePlatform(serverInstanceIDs[i]);
54                 if (platform != null && platform.getDisplayName().equals(j2eePlatform.getDisplayName())) {
55                     retVal = serverInstanceIDs[i];
56                 }
57             }
58         }
59         
60         return retVal;
61     }
62     
63     public static Object JavaDoc getServerInstanceID(String JavaDoc id) {
64         Object JavaDoc retVal = null;
65         if (null != id) {
66             
67             JavaEePlatformComboBoxModel jeepcbm = new JavaEePlatformComboBoxModel(null);
68             JavaEePlatformAdapter[] jeepas = jeepcbm.getJavaEePlatforms();
69             for (int i = 0; retVal == null && i < jeepas.length; i++) {
70                 J2eePlatform platform = Deployment.getDefault().getJ2eePlatform(id);
71                 if (platform != null && platform.getDisplayName().equals(jeepas[i].getJ2eePlatform().getDisplayName())) {
72                     retVal = jeepas[i];
73                 }
74             }
75         }
76         return retVal;
77     }
78     
79     
80     private static final class JavaEePlatformComboBoxModel //extends AbstractListModel implements ComboBoxModel {
81
implements ListModel JavaDoc, ComboBoxModel JavaDoc {
82         private transient JavaEePlatformAdapter[] j2eePlatforms;
83         private transient String JavaDoc initialJ2eePlatform;
84         private transient JavaEePlatformAdapter selectedJ2eePlatform;
85         
86         public JavaEePlatformComboBoxModel(String JavaDoc serverInstanceID) {
87             initialJ2eePlatform = serverInstanceID;
88             getJavaEePlatforms();
89         }
90         
91         public Object JavaDoc getElementAt(int index) {
92             return getJavaEePlatforms()[index];
93         }
94         
95         public int getSize() {
96             return getJavaEePlatforms().length;
97         }
98         
99         public synchronized Object JavaDoc getSelectedItem() {
100             return selectedJ2eePlatform;
101         }
102         
103         public synchronized void setSelectedItem(Object JavaDoc obj) {
104             selectedJ2eePlatform = (JavaEePlatformAdapter)obj;
105         }
106         
107         private synchronized JavaEePlatformAdapter[] getJavaEePlatforms() {
108             if (j2eePlatforms == null) {
109                 String JavaDoc[] serverInstanceIDs = Deployment.getDefault().getServerInstanceIDs();
110                 Set JavaDoc orderedNames = new TreeSet JavaDoc();
111                 boolean activeFound = false;
112                 JavaEePlatformAdapter firstAdapter = null;
113                 
114                 boolean sjasFound = false;
115                 for (int i = 0; i < serverInstanceIDs.length; i++) {
116                     J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(serverInstanceIDs[i]);
117                     if (j2eePlatform != null) {
118                         // if (j2eePlatform.getSupportedSpecVersions().contains(J2eeModule.JAVA_EE_50)) { // getSupportedModuleTypes().contains(J2eeModule.EJB)) {
119
JavaEePlatformAdapter adapter = new JavaEePlatformAdapter(j2eePlatform);
120                         orderedNames.add(adapter);
121                         
122                         if (selectedJ2eePlatform == null && !activeFound && initialJ2eePlatform != null) {
123                             if (serverInstanceIDs[i].equals(initialJ2eePlatform)) {
124                                 selectedJ2eePlatform = adapter;
125                                 activeFound = true;
126                             }
127                         }
128                         if (firstAdapter == null || !sjasFound) {
129                             // try to pick a glassfish instance
130
if (j2eePlatform.getSupportedSpecVersions().contains(J2eeModule.JAVA_EE_5) &&
131                                     j2eePlatform.getSupportedModuleTypes().contains(J2eeModule.EJB)) {
132                                 String JavaDoc shortName = Deployment.getDefault().getServerID(serverInstanceIDs[i]);
133                                 if ("J2EE".equals(shortName)) { // NOI18N
134
firstAdapter = adapter;
135                                     sjasFound = true;
136                                 }
137                                 else // prefer JBoss instance if GF not found
138
if ("JBoss4".equals(shortName)) { // NOI18N
139
firstAdapter = adapter;
140                                 }
141                             }
142                         }
143                     }
144                     
145                 }
146                 if (selectedJ2eePlatform == null) {
147                     if (null != firstAdapter) {
148                         selectedJ2eePlatform = firstAdapter;
149                     }
150                 }
151                 
152                 //j2eePlatforms = (J2eePlatform[])orderedNames.values().toArray(new J2eePlatform[orderedNames.size()]);
153
j2eePlatforms = (JavaEePlatformAdapter[])orderedNames.toArray(new JavaEePlatformAdapter[orderedNames.size()]);
154             }
155             return j2eePlatforms;
156         }
157         
158         public void addListDataListener(ListDataListener JavaDoc l) {
159         }
160         
161         public void removeListDataListener(ListDataListener JavaDoc l) {
162         }
163     }
164     
165     public static boolean getJ2eePlatformAndSpecVersionMatch(Object JavaDoc j2eePlatformModelObject, Object JavaDoc j2eeSpecVersionModelObject) {
166         if (!(j2eePlatformModelObject instanceof JavaEePlatformAdapter && j2eeSpecVersionModelObject instanceof String JavaDoc)) {
167             return false;
168         }
169         
170         J2eePlatform j2eePlatform = ((JavaEePlatformAdapter)j2eePlatformModelObject).getJ2eePlatform();
171         String JavaDoc specVersion = (String JavaDoc)j2eeSpecVersionModelObject;
172         return j2eePlatform.getSupportedSpecVersions().contains(specVersion);
173     }
174     
175     private static final class JavaEePlatformAdapter implements Comparable JavaDoc {
176         private J2eePlatform platform;
177         
178         public JavaEePlatformAdapter(J2eePlatform platform) {
179             this.platform = platform;
180         }
181         
182         public J2eePlatform getJ2eePlatform() {
183             return platform;
184         }
185         
186         public String JavaDoc toString() {
187             return platform.getDisplayName();
188         }
189         
190         public int compareTo(Object JavaDoc o) {
191             JavaEePlatformAdapter oa = (JavaEePlatformAdapter)o;
192             return toString().compareTo(oa.toString());
193         }
194     }
195 }
196
Popular Tags