2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
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]"
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.
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.
42
package org.netbeans.modules.j2ee.ejbjarproject.ui.customizer;
44
import java.util.ArrayList;
45
import java.util.List;
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;
57
* @author Andrei Badea
59
public class J2eePlatformUiSupport {
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
65
private J2eePlatformUiSupport() {
68
public static ComboBoxModel createPlatformComboBoxModel(String serverInstanceId) {
69
return new J2eePlatformComboBoxModel(serverInstanceId);
72
public static String getServerInstanceID(Object j2eePlatformModelObject) {
73
if (j2eePlatformModelObject == null)
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];
88
public static ComboBoxModel createSpecVersionComboBoxModel(String j2eeSpecVersion) {
89
return new J2eeSpecVersionComboBoxModel(j2eeSpecVersion);
92
public static String getSpecVersion(Object j2eeSpecVersionModelObject) {
93
return ((J2eePlatformComboBoxItem)j2eeSpecVersionModelObject).getCode();
96
private static final class J2eePlatformComboBoxModel extends AbstractListModel implements ComboBoxModel {
97
private J2eePlatformAdapter[] j2eePlatforms;
98
private String initialJ2eePlatform;
99
private J2eePlatformAdapter selectedJ2eePlatform;
101
public J2eePlatformComboBoxModel(String serverInstanceID) {
102
initialJ2eePlatform = serverInstanceID;
106
public Object getElementAt(int index) {
107
return getJ2eePlatforms()[index];
110
public int getSize() {
111
return getJ2eePlatforms().length;
114
public Object getSelectedItem() {
115
return selectedJ2eePlatform;
118
public void setSelectedItem(Object obj) {
119
selectedJ2eePlatform = (J2eePlatformAdapter)obj;
122
private synchronized J2eePlatformAdapter[] getJ2eePlatforms() {
123
if (j2eePlatforms == null) {
124
String[] serverInstanceIDs = Deployment.getDefault().getServerInstanceIDs();
125
Set orderedNames = new TreeSet();
126
boolean activeFound = false;
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);
135
if (selectedJ2eePlatform == null && !activeFound && initialJ2eePlatform != null) {
136
if (serverInstanceIDs[i].equals(initialJ2eePlatform)) {
137
selectedJ2eePlatform = adapter;
144
//j2eePlatforms = (J2eePlatform[])orderedNames.values().toArray(new J2eePlatform[orderedNames.size()]);
145
j2eePlatforms = (J2eePlatformAdapter[])orderedNames.toArray(new J2eePlatformAdapter[orderedNames.size()]);
147
return j2eePlatforms;
151
private static final class J2eeSpecVersionComboBoxModel extends AbstractListModel implements ComboBoxModel {
152
private J2eePlatformComboBoxItem[] j2eeSpecVersions;
154
private J2eePlatformComboBoxItem initialJ2eeSpecVersion;
155
private J2eePlatformComboBoxItem selectedJ2eeSpecVersion;
157
public J2eeSpecVersionComboBoxModel(String j2eeSpecVersion) {
158
initialJ2eeSpecVersion = new J2eePlatformComboBoxItem(j2eeSpecVersion);
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));
167
j2eeSpecVersions = (J2eePlatformComboBoxItem[])orderedListItems.toArray(new J2eePlatformComboBoxItem[orderedListItems.size()]);
168
selectedJ2eeSpecVersion = initialJ2eeSpecVersion;
171
public Object getElementAt(int index) {
172
return j2eeSpecVersions[index];
175
public int getSize() {
176
return j2eeSpecVersions.length;
179
public Object getSelectedItem() {
180
return selectedJ2eeSpecVersion;
183
public void setSelectedItem(Object obj) {
184
selectedJ2eeSpecVersion = (J2eePlatformComboBoxItem)obj;
188
private static final class J2eePlatformComboBoxItem{
190
private String displayName;
192
public J2eePlatformComboBoxItem (String code, String displayName){
194
this.displayName = displayName;
197
public J2eePlatformComboBoxItem (String code){
198
this(code, findDisplayName(code));
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
208
public String getCode(){
212
public String toString(){
217
public static boolean getJ2eePlatformAndSpecVersionMatch(Object j2eePlatformModelObject, Object j2eeSpecVersionModelObject) {
218
if (!(j2eePlatformModelObject instanceof J2eePlatformAdapter && j2eeSpecVersionModelObject instanceof String))
221
J2eePlatform j2eePlatform = ((J2eePlatformAdapter)j2eePlatformModelObject).getJ2eePlatform();
222
String specVersion = (String)j2eeSpecVersionModelObject;
223
return j2eePlatform.getSupportedSpecVersions(J2eeModule.EJB).contains(specVersion);
226
private static final class J2eePlatformAdapter implements Comparable {
227
private J2eePlatform platform;
229
public J2eePlatformAdapter(J2eePlatform platform) {
230
this.platform = platform;
233
public J2eePlatform getJ2eePlatform() {
237
public String toString() {
238
return platform.getDisplayName();
241
public int compareTo(Object o) {
242
J2eePlatformAdapter oa = (J2eePlatformAdapter)o;
243
return toString().compareTo(oa.toString());