1 19 20 21 package org.netbeans.modules.j2ee.archive.ui; 22 23 import java.util.Set ; 24 import java.util.TreeSet ; 25 import javax.swing.ComboBoxModel ; 26 import javax.swing.ListModel ; 27 import javax.swing.event.ListDataListener ; 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 37 public class JavaEePlatformUiSupport { 38 39 private JavaEePlatformUiSupport() { 40 } 41 42 public static ComboBoxModel createPlatformComboBoxModel(String serverInstanceId) { 43 return new JavaEePlatformComboBoxModel(serverInstanceId); 44 } 45 46 public static String getServerInstanceID(Object j2eePlatformModelObject) { 47 String retVal = null; 48 if (j2eePlatformModelObject != null) { 49 50 J2eePlatform j2eePlatform = ((JavaEePlatformAdapter)j2eePlatformModelObject).getJ2eePlatform(); 51 String [] 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 getServerInstanceID(String id) { 64 Object 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 implements ListModel , ComboBoxModel { 82 private transient JavaEePlatformAdapter[] j2eePlatforms; 83 private transient String initialJ2eePlatform; 84 private transient JavaEePlatformAdapter selectedJ2eePlatform; 85 86 public JavaEePlatformComboBoxModel(String serverInstanceID) { 87 initialJ2eePlatform = serverInstanceID; 88 getJavaEePlatforms(); 89 } 90 91 public Object getElementAt(int index) { 92 return getJavaEePlatforms()[index]; 93 } 94 95 public int getSize() { 96 return getJavaEePlatforms().length; 97 } 98 99 public synchronized Object getSelectedItem() { 100 return selectedJ2eePlatform; 101 } 102 103 public synchronized void setSelectedItem(Object obj) { 104 selectedJ2eePlatform = (JavaEePlatformAdapter)obj; 105 } 106 107 private synchronized JavaEePlatformAdapter[] getJavaEePlatforms() { 108 if (j2eePlatforms == null) { 109 String [] serverInstanceIDs = Deployment.getDefault().getServerInstanceIDs(); 110 Set orderedNames = new TreeSet (); 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 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 if (j2eePlatform.getSupportedSpecVersions().contains(J2eeModule.JAVA_EE_5) && 131 j2eePlatform.getSupportedModuleTypes().contains(J2eeModule.EJB)) { 132 String shortName = Deployment.getDefault().getServerID(serverInstanceIDs[i]); 133 if ("J2EE".equals(shortName)) { firstAdapter = adapter; 135 sjasFound = true; 136 } 137 else if ("JBoss4".equals(shortName)) { firstAdapter = adapter; 140 } 141 } 142 } 143 } 144 145 } 146 if (selectedJ2eePlatform == null) { 147 if (null != firstAdapter) { 148 selectedJ2eePlatform = firstAdapter; 149 } 150 } 151 152 j2eePlatforms = (JavaEePlatformAdapter[])orderedNames.toArray(new JavaEePlatformAdapter[orderedNames.size()]); 154 } 155 return j2eePlatforms; 156 } 157 158 public void addListDataListener(ListDataListener l) { 159 } 160 161 public void removeListDataListener(ListDataListener l) { 162 } 163 } 164 165 public static boolean getJ2eePlatformAndSpecVersionMatch(Object j2eePlatformModelObject, Object j2eeSpecVersionModelObject) { 166 if (!(j2eePlatformModelObject instanceof JavaEePlatformAdapter && j2eeSpecVersionModelObject instanceof String )) { 167 return false; 168 } 169 170 J2eePlatform j2eePlatform = ((JavaEePlatformAdapter)j2eePlatformModelObject).getJ2eePlatform(); 171 String specVersion = (String )j2eeSpecVersionModelObject; 172 return j2eePlatform.getSupportedSpecVersions().contains(specVersion); 173 } 174 175 private static final class JavaEePlatformAdapter implements Comparable { 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 toString() { 187 return platform.getDisplayName(); 188 } 189 190 public int compareTo(Object o) { 191 JavaEePlatformAdapter oa = (JavaEePlatformAdapter)o; 192 return toString().compareTo(oa.toString()); 193 } 194 } 195 } 196 | Popular Tags |