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