1 19 20 package org.netbeans.modules.j2ee.ejbjarproject.ui.customizer; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import java.util.Set ; 25 import java.util.TreeSet ; 26 import javax.swing.AbstractListModel ; 27 import javax.swing.ComboBoxModel ; 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 37 public class J2eePlatformUiSupport { 38 39 private static final String JAVA_EE_5_DISPLAY_NAME = NbBundle.getMessage(J2eePlatformUiSupport.class, "JAVA_EE_5_displayName"); private static final String J2EE_1_4_DISPLAY_NAME = NbBundle.getMessage(J2eePlatformUiSupport.class, "J2EE_1_4_displayName"); private static final String J2EE_1_3_DISPLAY_NAME = NbBundle.getMessage(J2eePlatformUiSupport.class, "J2EE_1_3_displayName"); 43 private J2eePlatformUiSupport() { 44 } 45 46 public static ComboBoxModel createPlatformComboBoxModel(String serverInstanceId) { 47 return new J2eePlatformComboBoxModel(serverInstanceId); 48 } 49 50 public static String getServerInstanceID(Object j2eePlatformModelObject) { 51 if (j2eePlatformModelObject == null) 52 return null; 53 54 J2eePlatform j2eePlatform = ((J2eePlatformAdapter)j2eePlatformModelObject).getJ2eePlatform(); 55 String [] 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 createSpecVersionComboBoxModel(String j2eeSpecVersion) { 67 return new J2eeSpecVersionComboBoxModel(j2eeSpecVersion); 68 } 69 70 public static String getSpecVersion(Object j2eeSpecVersionModelObject) { 71 return ((J2eePlatformComboBoxItem)j2eeSpecVersionModelObject).getCode(); 72 } 73 74 private static final class J2eePlatformComboBoxModel extends AbstractListModel implements ComboBoxModel { 75 private J2eePlatformAdapter[] j2eePlatforms; 76 private String initialJ2eePlatform; 77 private J2eePlatformAdapter selectedJ2eePlatform; 78 79 public J2eePlatformComboBoxModel(String serverInstanceID) { 80 initialJ2eePlatform = serverInstanceID; 81 getJ2eePlatforms(); 82 } 83 84 public Object getElementAt(int index) { 85 return getJ2eePlatforms()[index]; 86 } 87 88 public int getSize() { 89 return getJ2eePlatforms().length; 90 } 91 92 public Object getSelectedItem() { 93 return selectedJ2eePlatform; 94 } 95 96 public void setSelectedItem(Object obj) { 97 selectedJ2eePlatform = (J2eePlatformAdapter)obj; 98 } 99 100 private synchronized J2eePlatformAdapter[] getJ2eePlatforms() { 101 if (j2eePlatforms == null) { 102 String [] serverInstanceIDs = Deployment.getDefault().getServerInstanceIDs(); 103 Set orderedNames = new TreeSet (); 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 = (J2eePlatformAdapter[])orderedNames.toArray(new J2eePlatformAdapter[orderedNames.size()]); 124 } 125 return j2eePlatforms; 126 } 127 } 128 129 private static final class J2eeSpecVersionComboBoxModel extends AbstractListModel implements ComboBoxModel { 130 private J2eePlatformComboBoxItem[] j2eeSpecVersions; 131 132 private J2eePlatformComboBoxItem initialJ2eeSpecVersion; 133 private J2eePlatformComboBoxItem selectedJ2eeSpecVersion; 134 135 public J2eeSpecVersionComboBoxModel(String j2eeSpecVersion) { 136 initialJ2eeSpecVersion = new J2eePlatformComboBoxItem(j2eeSpecVersion); 137 138 List orderedListItems = new ArrayList (); 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 getElementAt(int index) { 150 return j2eeSpecVersions[index]; 151 } 152 153 public int getSize() { 154 return j2eeSpecVersions.length; 155 } 156 157 public Object getSelectedItem() { 158 return selectedJ2eeSpecVersion; 159 } 160 161 public void setSelectedItem(Object obj) { 162 selectedJ2eeSpecVersion = (J2eePlatformComboBoxItem)obj; 163 } 164 } 165 166 private static final class J2eePlatformComboBoxItem{ 167 private String code; 168 private String displayName; 169 170 public J2eePlatformComboBoxItem (String code, String displayName){ 171 this.code = code; 172 this.displayName = displayName; 173 } 174 175 public J2eePlatformComboBoxItem (String code){ 176 this(code, findDisplayName(code)); 177 } 178 179 private static String findDisplayName(String 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; } 185 186 public String getCode(){ 187 return code; 188 } 189 190 public String toString(){ 191 return displayName; 192 } 193 } 194 195 public static boolean getJ2eePlatformAndSpecVersionMatch(Object j2eePlatformModelObject, Object j2eeSpecVersionModelObject) { 196 if (!(j2eePlatformModelObject instanceof J2eePlatformAdapter && j2eeSpecVersionModelObject instanceof String )) 197 return false; 198 199 J2eePlatform j2eePlatform = ((J2eePlatformAdapter)j2eePlatformModelObject).getJ2eePlatform(); 200 String specVersion = (String )j2eeSpecVersionModelObject; 201 return j2eePlatform.getSupportedSpecVersions(J2eeModule.EJB).contains(specVersion); 202 } 203 204 private static final class J2eePlatformAdapter implements Comparable { 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 toString() { 216 return platform.getDisplayName(); 217 } 218 219 public int compareTo(Object o) { 220 J2eePlatformAdapter oa = (J2eePlatformAdapter)o; 221 return toString().compareTo(oa.toString()); 222 } 223 } 224 } 225 | Popular Tags |