KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > 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.web.project.ui.customizer;
21
22 import java.util.Set JavaDoc;
23 import java.util.TreeSet JavaDoc;
24
25 import javax.swing.AbstractListModel JavaDoc;
26 import javax.swing.ComboBoxModel JavaDoc;
27
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, Radko Najman
35  */

36 public class J2eePlatformUiSupport {
37
38     private J2eePlatformUiSupport() {
39     }
40     
41     public static ComboBoxModel JavaDoc createPlatformComboBoxModel(String JavaDoc serverInstanceId, String JavaDoc j2eeLevel) {
42         return new J2eePlatformComboBoxModel(serverInstanceId, j2eeLevel);
43     }
44     
45     public static String JavaDoc getServerInstanceID(Object JavaDoc j2eePlatformModelObject) {
46         if (j2eePlatformModelObject == null)
47             return null;
48
49         J2eePlatform j2eePlatform = ((J2eePlatformAdapter)j2eePlatformModelObject).getJ2eePlatform();
50         String JavaDoc[] serverInstanceIDs = Deployment.getDefault().getServerInstanceIDs();
51         for (int i = 0; i < serverInstanceIDs.length; i++) {
52             J2eePlatform platform = Deployment.getDefault().getJ2eePlatform(serverInstanceIDs[i]);
53             if (platform != null && platform.getDisplayName().equals(j2eePlatform.getDisplayName())) {
54                 return serverInstanceIDs[i];
55             }
56         }
57         
58         return null;
59     }
60     
61     /**
62      * TODO: AB: Temporary fix of #54544.
63      */

64     public static void setSelectedPlatform(ComboBoxModel JavaDoc model, String JavaDoc serverInstanceID) {
65         if (!(model instanceof J2eePlatformComboBoxModel))
66             return;
67         
68         ((J2eePlatformComboBoxModel)model).setSelectedItem(serverInstanceID);
69     }
70     
71     private static final class J2eePlatformComboBoxModel extends AbstractListModel JavaDoc implements ComboBoxModel JavaDoc {
72         private J2eePlatformAdapter[] j2eePlatforms;
73         private String JavaDoc initialJ2eePlatform;
74         private J2eePlatformAdapter selectedJ2eePlatform;
75         private String JavaDoc j2eeLevel;
76         
77         public J2eePlatformComboBoxModel(String JavaDoc serverInstanceID, String JavaDoc j2eeLevel) {
78             initialJ2eePlatform = serverInstanceID;
79             this.j2eeLevel = j2eeLevel;
80             getJ2eePlatforms();
81         }
82         
83         public Object JavaDoc getElementAt(int index) {
84             return getJ2eePlatforms()[index];
85         }
86
87         public int getSize() {
88             return getJ2eePlatforms().length;
89         }
90         
91         public Object JavaDoc getSelectedItem() {
92             return selectedJ2eePlatform;
93         }
94         
95         public void setSelectedItem(Object JavaDoc obj) {
96             selectedJ2eePlatform = (J2eePlatformAdapter)obj;
97         }
98         
99         public void setSelectedItem(String JavaDoc serverInstanceID) {
100             for (int i = 0; i < j2eePlatforms.length; i++) {
101                 if (j2eePlatforms[i].getServerInstanceID().equals(serverInstanceID)) {
102                     selectedJ2eePlatform = j2eePlatforms[i];
103                     return;
104                 }
105             }
106         }
107                 
108         private synchronized J2eePlatformAdapter[] getJ2eePlatforms() {
109             if (j2eePlatforms == null) {
110                 String JavaDoc[] serverInstanceIDs = Deployment.getDefault().getServerInstanceIDs();
111                 Set JavaDoc orderedNames = new TreeSet JavaDoc();
112                 boolean activeFound = false;
113
114                 for (int i = 0; i < serverInstanceIDs.length; i++) {
115                     J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(serverInstanceIDs[i]);
116                     if (j2eePlatform != null) {
117                         if (j2eePlatform.getSupportedModuleTypes().contains(J2eeModule.WAR)
118                                 && j2eePlatform.getSupportedSpecVersions(J2eeModule.WAR).contains(j2eeLevel)) {
119                             J2eePlatformAdapter adapter = new J2eePlatformAdapter(j2eePlatform, serverInstanceIDs[i]);
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                         }
129                     }
130                 }
131                 j2eePlatforms = (J2eePlatformAdapter[])orderedNames.toArray(new J2eePlatformAdapter[orderedNames.size()]);
132             }
133             return j2eePlatforms;
134         }
135     }
136         
137     private static final class J2eePlatformAdapter implements Comparable JavaDoc {
138         private J2eePlatform platform;
139         private String JavaDoc serverInstanceID;
140         
141         public J2eePlatformAdapter(J2eePlatform platform, String JavaDoc serverInstanceID) {
142             this.platform = platform;
143             this.serverInstanceID = serverInstanceID;
144         }
145         
146         public J2eePlatform getJ2eePlatform() {
147             return platform;
148         }
149         
150         public String JavaDoc getServerInstanceID() {
151             return serverInstanceID;
152         }
153         
154         public String JavaDoc toString() {
155             return platform.getDisplayName();
156         }
157
158         public int compareTo(Object JavaDoc o) {
159             J2eePlatformAdapter oa = (J2eePlatformAdapter)o;
160             return toString().compareTo(oa.toString());
161         }
162     }
163 }
164
Popular Tags