1 19 20 package org.netbeans.modules.j2ee.ejbjarproject.ui.customizer; 21 22 import java.awt.Component ; 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.text.MessageFormat ; 26 import java.util.ArrayList ; 27 import java.util.Arrays ; 28 import java.util.List ; 29 import java.util.Set ; 30 import java.util.TreeSet ; 31 import javax.swing.AbstractListModel ; 32 import javax.swing.ComboBoxModel ; 33 import javax.swing.DefaultListCellRenderer ; 34 import javax.swing.JButton ; 35 import javax.swing.JList ; 36 import javax.swing.ListCellRenderer ; 37 import javax.swing.event.ListDataEvent ; 38 import javax.swing.event.ListDataListener ; 39 import org.netbeans.api.java.platform.JavaPlatform; 40 import org.netbeans.api.java.platform.JavaPlatformManager; 41 import org.netbeans.api.java.platform.Specification; 42 import org.netbeans.modules.j2ee.ejbjarproject.EjbJarProjectType; 43 import org.netbeans.modules.j2ee.ejbjarproject.UpdateHelper; 44 import org.netbeans.spi.project.support.ant.EditableProperties; 45 import org.openide.DialogDisplayer; 46 import org.openide.ErrorManager; 47 import org.openide.NotifyDescriptor; 48 import org.openide.modules.SpecificationVersion; 49 import org.openide.util.NbBundle; 50 import org.openide.util.WeakListeners; 51 import org.w3c.dom.Element ; 52 import org.w3c.dom.NodeList ; 53 54 58 public class PlatformUiSupport { 59 60 private PlatformUiSupport() { 61 } 62 63 70 public static ComboBoxModel createPlatformComboBoxModel (String activePlatform) { 71 return new PlatformComboBoxModel (activePlatform); 72 } 73 74 79 public static ListCellRenderer createPlatformListCellRenderer () { 80 return new PlatformListCellRenderer (); 81 } 82 83 86 public static void storePlatform (EditableProperties props, UpdateHelper helper, String platformName, SpecificationVersion sourceLevel) { 87 PlatformKey platformKey; 88 if (platformName != null) { 89 platformKey = new PlatformKey(PlatformUiSupport.findPlatform(platformName)); 90 } else { 91 platformKey = new PlatformKey(JavaPlatformManager.getDefault().getDefaultPlatform()); 92 } 93 storePlatform(props, helper, platformKey, sourceLevel); 94 } 95 96 103 public static void storePlatform (EditableProperties props, UpdateHelper helper, Object platformKey, SpecificationVersion sourceLevel) { 104 assert platformKey instanceof PlatformKey; 105 PlatformKey pk = (PlatformKey) platformKey; 106 JavaPlatform platform = getPlatform(pk); 107 if (platform != null) { 109 String platformAntName = (String ) platform.getProperties().get("platform.ant.name"); assert platformAntName != null; 111 props.put(EjbJarProjectProperties.JAVA_PLATFORM, platformAntName); 112 Element root = helper.getPrimaryConfigurationData(true); 113 boolean defaultPlatform = pk.isDefaultPlatform(); 114 boolean changed = false; 115 NodeList explicitPlatformNodes = root.getElementsByTagNameNS (EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,"explicit-platform"); if (defaultPlatform) { 117 if (explicitPlatformNodes.getLength()==1) { 118 root.removeChild(explicitPlatformNodes.item(0)); 119 changed = true; 120 } 121 SpecificationVersion platformVersion = platform.getSpecification().getVersion(); 122 String newTargetValue; 123 String newSourceValue; 124 if (sourceLevel == null || sourceLevel.equals (platformVersion)){ 125 newTargetValue = newSourceValue = getDefaultSourceLevel(platform); 126 } 127 else { 128 newTargetValue = newSourceValue = sourceLevel.toString(); 129 } 130 String oldTargetValue = props.getProperty (EjbJarProjectProperties.JAVAC_TARGET); 131 String oldSourceValue = props.getProperty (EjbJarProjectProperties.JAVAC_SOURCE); 132 if (!newTargetValue.equals (oldTargetValue)) { 133 props.setProperty (EjbJarProjectProperties.JAVAC_TARGET, newTargetValue); 134 } 135 if (!newSourceValue.equals (oldSourceValue)) { 136 props.setProperty (EjbJarProjectProperties.JAVAC_SOURCE, newSourceValue); 137 } 138 } 139 else { 140 Element explicitPlatform; 141 switch (explicitPlatformNodes.getLength()) { 142 case 0: 143 explicitPlatform = root.getOwnerDocument().createElementNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, "explicit-platform"); NodeList sourceRootNodes = root.getElementsByTagNameNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,"minimum-ant-version"); assert sourceRootNodes.getLength() == 1 : "Broken project.xml file"; root.insertBefore(explicitPlatform, sourceRootNodes.item(0).getNextSibling()); 147 changed = true; 148 break; 149 case 1: 150 explicitPlatform = (Element )explicitPlatformNodes.item(0); 151 break; 152 default: 153 throw new AssertionError ("Broken project.xml file"); } 155 SpecificationVersion jdk13 = new SpecificationVersion ("1.3"); String explicitSourceAttrValue = explicitPlatform.getAttribute("explicit-source-supported"); if (jdk13.compareTo(platform.getSpecification().getVersion())>=0 && 158 !"false".equals(explicitSourceAttrValue)) { explicitPlatform.setAttribute("explicit-source-supported","false"); changed = true; 161 } 162 else if (jdk13.compareTo(platform.getSpecification().getVersion())<0 && 163 !"true".equals(explicitSourceAttrValue)) { explicitPlatform.setAttribute("explicit-source-supported","true"); changed = true; 166 } 167 if (sourceLevel == null) { 168 sourceLevel = platform.getSpecification().getVersion(); 169 } 170 String javacSource = sourceLevel.toString(); 171 if (javacSource.equals("1.6") || javacSource.equals("1.7")) 173 javacSource = "1.5"; 174 if (!javacSource.equals(props.getProperty(EjbJarProjectProperties.JAVAC_SOURCE))) { 175 props.setProperty (EjbJarProjectProperties.JAVAC_SOURCE, javacSource); 176 } 177 if (!javacSource.equals(props.getProperty(EjbJarProjectProperties.JAVAC_TARGET))) { 178 props.setProperty (EjbJarProjectProperties.JAVAC_TARGET, javacSource); 179 } 180 } 181 if (changed) { 182 helper.putPrimaryConfigurationData(root, true); 183 } 184 } 185 } 186 187 public static JavaPlatform findPlatform(String displayName) { 188 JavaPlatform[] platforms = JavaPlatformManager.getDefault().getPlatforms(displayName, new Specification("j2se", null)); return platforms.length == 0 ? null : platforms[0]; 190 } 191 192 193 201 public static JavaPlatform getPlatform (Object platformKey) { 202 if (platformKey instanceof PlatformKey) { 203 return getPlatform ((PlatformKey)platformKey); 204 } 205 else { 206 throw new IllegalArgumentException (); 207 } 208 } 209 210 218 public static ComboBoxModel createSourceLevelComboBoxModel (ComboBoxModel platformComboBoxModel, String initialValue, String j2eePlatform) { 219 return new SourceLevelComboBoxModel (platformComboBoxModel, initialValue, j2eePlatform); 220 } 221 222 private static JavaPlatform getPlatform (PlatformKey platformKey) { 223 return platformKey.platform; 224 } 225 226 231 private static class PlatformKey implements Comparable { 232 233 private String name; 234 private JavaPlatform platform; 235 236 240 public PlatformKey (String name) { 241 assert name != null; 242 this.name = name; 243 } 244 245 249 public PlatformKey (JavaPlatform platform) { 250 assert platform != null; 251 this.platform = platform; 252 } 253 254 public int compareTo(Object o) { 255 return this.getDisplayName().compareTo(((PlatformKey)o).getDisplayName()); 256 } 257 258 public boolean equals (Object other) { 259 if (other instanceof PlatformKey) { 260 PlatformKey otherKey = (PlatformKey)other; 261 return (this.platform == null ? otherKey.platform == null : this.platform.equals(otherKey.platform)) && 262 otherKey.getDisplayName().equals (this.getDisplayName()); 263 } 264 else { 265 return false; 266 } 267 } 268 269 public int hashCode () { 270 return getDisplayName ().hashCode (); 271 } 272 273 public String toString () { 274 return getDisplayName (); 275 } 276 277 public synchronized String getDisplayName () { 278 if (this.name == null) { 279 this.name = this.platform.getDisplayName(); 280 } 281 return this.name; 282 } 283 284 public boolean isDefaultPlatform () { 285 if (this.platform == null) { 286 return false; 287 } 288 return this.platform.equals(JavaPlatformManager.getDefault().getDefaultPlatform()); 289 } 290 291 public boolean isBroken () { 292 return this.platform == null; 293 } 294 } 295 296 private static class PlatformComboBoxModel extends AbstractListModel implements ComboBoxModel , PropertyChangeListener { 297 298 private final JavaPlatformManager pm; 299 private PlatformKey[] platformNamesCache; 300 private String initialPlatform; 301 private PlatformKey selectedPlatform; 302 303 public PlatformComboBoxModel (String initialPlatform) { 304 this.pm = JavaPlatformManager.getDefault(); 305 this.pm.addPropertyChangeListener(WeakListeners.propertyChange(this, this.pm)); 306 this.initialPlatform = initialPlatform; 307 } 308 309 public int getSize () { 310 PlatformKey[] platformNames = getPlatformNames (); 311 return platformNames.length; 312 } 313 314 public Object getElementAt (int index) { 315 PlatformKey[] platformNames = getPlatformNames (); 316 assert index >=0 && index< platformNames.length; 317 return platformNames[index]; 318 } 319 320 public Object getSelectedItem () { 321 this.getPlatformNames(); return this.selectedPlatform; 323 } 324 325 public void setSelectedItem (Object obj) { 326 this.selectedPlatform = (PlatformKey) obj; 327 this.fireContentsChanged(this, -1, -1); 328 } 329 330 public void propertyChange (PropertyChangeEvent event) { 331 if (JavaPlatformManager.PROP_INSTALLED_PLATFORMS.equals(event.getPropertyName())) { 332 synchronized (this) { 333 this.platformNamesCache = null; 334 } 335 this.fireContentsChanged(this, -1, -1); 336 } 337 } 338 339 private synchronized PlatformKey[] getPlatformNames () { 340 if (this.platformNamesCache == null) { 341 JavaPlatform[] platforms = pm.getPlatforms (null, new Specification("j2se",null)); Set orderedNames = new TreeSet (); 343 boolean activeFound = false; 344 for (int i=0; i< platforms.length; i++) { 345 if (platforms[i].getInstallFolders().size()>0) { 346 PlatformKey pk = new PlatformKey(platforms[i]); 347 orderedNames.add (pk); 348 if (!activeFound && initialPlatform != null) { 349 String antName = (String ) platforms[i].getProperties().get("platform.ant.name"); if (initialPlatform.equals(antName)) { 351 if (this.selectedPlatform == null) { 352 this.selectedPlatform = pk; 353 initialPlatform = null; 354 } 355 activeFound = true; 356 } 357 } 358 } 359 } 360 if (!activeFound) { 361 if (initialPlatform == null) { 362 if (this.selectedPlatform == null || !orderedNames.contains(this.selectedPlatform)) { 363 this.selectedPlatform = new PlatformKey (JavaPlatformManager.getDefault().getDefaultPlatform()); 364 } 365 } 366 else { 367 PlatformKey pk = new PlatformKey (this.initialPlatform); 368 orderedNames.add (pk); 369 if (this.selectedPlatform == null) { 370 this.selectedPlatform = pk; 371 } 372 } 373 } 374 this.platformNamesCache = (PlatformKey[]) orderedNames.toArray(new PlatformKey[orderedNames.size()]); 375 } 376 return this.platformNamesCache; 377 } 378 379 } 380 381 private static class PlatformListCellRenderer extends DefaultListCellRenderer { 382 383 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 384 assert value instanceof PlatformKey : "Wrong model"; PlatformKey key = (PlatformKey) value; 386 String name; 387 if (key.isBroken()) { 388 name = NbBundle.getMessage (PlatformUiSupport.class,"TXT_BrokenPlatformFmt", key.getDisplayName()); 389 } 390 else { 391 name = key.getDisplayName(); 392 } 393 return super.getListCellRendererComponent(list, name, index, isSelected, cellHasFocus); 394 } 395 } 396 397 private static class SourceLevelComboBoxModel extends AbstractListModel implements ComboBoxModel , ListDataListener { 398 399 private static final String VERSION_PREFIX = "1."; private static final int INITIAL_VERSION_MINOR = 2; private static final int INITIAL_VERSION_MINOR_JAVA_EE_5 = 5; 404 private SpecificationVersion selectedSourceLevel; 405 private SpecificationVersion[] sourceLevelCache; 406 private final ComboBoxModel platformComboBoxModel; 407 private PlatformKey activePlatform; 408 private String j2eePlatform = null; 409 410 public SourceLevelComboBoxModel (ComboBoxModel platformComboBoxModel, String initialValue) { 411 this.platformComboBoxModel = platformComboBoxModel; 412 this.activePlatform = (PlatformKey) this.platformComboBoxModel.getSelectedItem(); 413 this.platformComboBoxModel.addListDataListener (this); 414 if (initialValue != null && initialValue.length()>0) { 415 try { 416 this.selectedSourceLevel = new SpecificationVersion (initialValue); 417 } catch (NumberFormatException nfe) { 418 ErrorManager.getDefault().log("Invalid javac.source: " + initialValue); 420 } 421 } 422 } 423 424 public SourceLevelComboBoxModel (ComboBoxModel platformComboBoxModel, String initialValue, String j2eePlatform) { 425 this(platformComboBoxModel, initialValue); 426 this.j2eePlatform = j2eePlatform; 427 } 428 429 public int getSize () { 430 SpecificationVersion[] sLevels = getSourceLevels (); 431 return sLevels.length; 432 } 433 434 public Object getElementAt (int index) { 435 SpecificationVersion[] sLevels = getSourceLevels (); 436 assert index >=0 && index< sLevels.length; 437 return sLevels[index]; 438 } 439 440 public Object getSelectedItem () { 441 List sLevels = Arrays.asList(getSourceLevels ()); 442 if (this.selectedSourceLevel != null) { 443 if (!sLevels.contains(this.selectedSourceLevel)) { 444 if (sLevels.size()>0) { 445 this.selectedSourceLevel = (SpecificationVersion) sLevels.get(sLevels.size()-1); 446 } 447 else { 448 this.selectedSourceLevel = null; 449 } 450 } 451 } 452 return this.selectedSourceLevel; 453 } 454 455 public void setSelectedItem (Object obj) { 456 this.selectedSourceLevel = (SpecificationVersion) obj; 457 this.fireContentsChanged(this, -1, -1); 458 } 459 460 public void intervalAdded(ListDataEvent e) { 461 } 462 463 public void intervalRemoved(ListDataEvent e) { 464 } 465 466 public void contentsChanged(ListDataEvent e) { 467 PlatformKey selectedPlatform = (PlatformKey) this.platformComboBoxModel.getSelectedItem(); 468 JavaPlatform platform = getPlatform(selectedPlatform); 469 if (platform != null) { 470 SpecificationVersion version = platform.getSpecification().getVersion(); 471 if (this.selectedSourceLevel != null && this.selectedSourceLevel.compareTo(version)>0 && 472 !shouldChangePlatform (selectedSourceLevel, version)) { 473 this.platformComboBoxModel.setSelectedItem(this.activePlatform); 475 return; 476 } 477 } 478 this.activePlatform = selectedPlatform; 479 resetCache (); 480 } 481 482 private void resetCache () { 483 synchronized (this) { 484 this.sourceLevelCache = null; 485 } 486 this.fireContentsChanged(this, -1, -1); 487 } 488 489 private SpecificationVersion[] getSourceLevels () { 490 if (this.sourceLevelCache == null) { 491 PlatformKey selectedPlatform = (PlatformKey) this.platformComboBoxModel.getSelectedItem(); 492 JavaPlatform platform = getPlatform(selectedPlatform); 493 List sLevels = new ArrayList (); 494 if (platform != null) { 497 SpecificationVersion version = platform.getSpecification().getVersion(); 498 int index = INITIAL_VERSION_MINOR; 499 if (j2eePlatform != null && j2eePlatform.equals(EjbJarProjectProperties.JAVA_EE_5)) { 501 index = INITIAL_VERSION_MINOR_JAVA_EE_5; 502 } 503 SpecificationVersion template = new SpecificationVersion (VERSION_PREFIX + Integer.toString (index++)); 504 while (template.compareTo(version)<=0) { 505 sLevels.add (template); 506 template = new SpecificationVersion (VERSION_PREFIX + Integer.toString (index++)); 507 } 508 } 509 this.sourceLevelCache = (SpecificationVersion[]) sLevels.toArray(new SpecificationVersion[sLevels.size()]); 510 } 511 return this.sourceLevelCache; 512 } 513 514 private static boolean shouldChangePlatform (SpecificationVersion selectedSourceLevel, SpecificationVersion platformSourceLevel) { 515 JButton changeOption = new JButton (NbBundle.getMessage(PlatformUiSupport.class, "CTL_ChangePlatform")); 516 changeOption.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PlatformUiSupport.class, "AD_ChangePlatform")); 517 String message = MessageFormat.format (NbBundle.getMessage(PlatformUiSupport.class,"TXT_ChangePlatform"),new Object [] { 518 selectedSourceLevel.toString(), 519 platformSourceLevel.toString(), 520 }); 521 return DialogDisplayer.getDefault().notify( 522 new NotifyDescriptor (message, 523 NbBundle.getMessage(PlatformUiSupport.class,"TXT_ChangePlatformTitle"), 524 NotifyDescriptor.DEFAULT_OPTION, 525 NotifyDescriptor.WARNING_MESSAGE, 526 new Object [] { 527 changeOption, 528 NotifyDescriptor.CANCEL_OPTION 529 }, 530 changeOption)) == changeOption; 531 } 532 } 533 534 private static String getDefaultSourceLevel(JavaPlatform platform) { 535 SpecificationVersion v = platform.getSpecification().getVersion(); 536 String javacSource = v.toString(); 537 if (javacSource.equals("1.6") || javacSource.equals("1.7")) 539 javacSource = "1.5"; 540 541 return javacSource; 542 } 543 544 } 545 | Popular Tags |