1 19 20 package org.netbeans.modules.web.project.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.web.project.WebProjectType; 43 import org.netbeans.modules.web.project.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(WebProjectProperties.JAVA_PLATFORM, platformAntName); 112 Element root = helper.getPrimaryConfigurationData(true); 113 boolean defaultPlatform = pk.isDefaultPlatform(); 114 boolean changed = false; 115 NodeList explicitPlatformNodes = root.getElementsByTagNameNS (WebProjectType.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 (WebProjectProperties.JAVAC_TARGET); 131 String oldSourceValue = props.getProperty (WebProjectProperties.JAVAC_SOURCE); 132 if (!newTargetValue.equals (oldTargetValue)) { 133 props.setProperty (WebProjectProperties.JAVAC_TARGET, newTargetValue); 134 } 135 if (!newSourceValue.equals (oldSourceValue)) { 136 props.setProperty (WebProjectProperties.JAVAC_SOURCE, newSourceValue); 137 } 138 } 139 else { 140 Element explicitPlatform; 141 switch (explicitPlatformNodes.getLength()) { 142 case 0: 143 explicitPlatform = root.getOwnerDocument().createElementNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, "explicit-platform"); NodeList sourceRootNodes = root.getElementsByTagNameNS(WebProjectType.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(WebProjectProperties.JAVAC_SOURCE))) { 175 props.setProperty (WebProjectProperties.JAVAC_SOURCE, javacSource); 176 } 177 if (!javacSource.equals(props.getProperty(WebProjectProperties.JAVAC_TARGET))) { 178 props.setProperty (WebProjectProperties.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 297 private static class PlatformComboBoxModel extends AbstractListModel implements ComboBoxModel , PropertyChangeListener { 298 299 private final JavaPlatformManager pm; 300 private PlatformKey[] platformNamesCache; 301 private String initialPlatform; 302 private PlatformKey selectedPlatform; 303 304 public PlatformComboBoxModel (String initialPlatform) { 305 this.pm = JavaPlatformManager.getDefault(); 306 this.pm.addPropertyChangeListener(WeakListeners.propertyChange(this, this.pm)); 307 this.initialPlatform = initialPlatform; 308 } 309 310 public int getSize () { 311 PlatformKey[] platformNames = getPlatformNames (); 312 return platformNames.length; 313 } 314 315 public Object getElementAt (int index) { 316 PlatformKey[] platformNames = getPlatformNames (); 317 assert index >=0 && index< platformNames.length; 318 return platformNames[index]; 319 } 320 321 public Object getSelectedItem () { 322 this.getPlatformNames(); return this.selectedPlatform; 324 } 325 326 public void setSelectedItem (Object obj) { 327 this.selectedPlatform = (PlatformKey) obj; 328 this.fireContentsChanged(this, -1, -1); 329 } 330 331 public void propertyChange (PropertyChangeEvent event) { 332 if (JavaPlatformManager.PROP_INSTALLED_PLATFORMS.equals(event.getPropertyName())) { 333 synchronized (this) { 334 this.platformNamesCache = null; 335 } 336 this.fireContentsChanged(this, -1, -1); 337 } 338 } 339 340 private synchronized PlatformKey[] getPlatformNames () { 341 if (this.platformNamesCache == null) { 342 JavaPlatform[] platforms = pm.getPlatforms (null, new Specification("j2se",null)); Set orderedNames = new TreeSet (); 344 boolean activeFound = false; 345 for (int i=0; i< platforms.length; i++) { 346 if (platforms[i].getInstallFolders().size()>0) { 347 PlatformKey pk = new PlatformKey(platforms[i]); 348 orderedNames.add (pk); 349 if (!activeFound && initialPlatform != null) { 350 String antName = (String ) platforms[i].getProperties().get("platform.ant.name"); if (initialPlatform.equals(antName)) { 352 if (this.selectedPlatform == null) { 353 this.selectedPlatform = pk; 354 initialPlatform = null; 355 } 356 activeFound = true; 357 } 358 } 359 } 360 } 361 if (!activeFound) { 362 if (initialPlatform == null) { 363 if (this.selectedPlatform == null || !orderedNames.contains(this.selectedPlatform)) { 364 this.selectedPlatform = new PlatformKey (JavaPlatformManager.getDefault().getDefaultPlatform()); 365 } 366 } 367 else { 368 PlatformKey pk = new PlatformKey (this.initialPlatform); 369 orderedNames.add (pk); 370 if (this.selectedPlatform == null) { 371 this.selectedPlatform = pk; 372 } 373 } 374 } 375 this.platformNamesCache = (PlatformKey[]) orderedNames.toArray(new PlatformKey[orderedNames.size()]); 376 } 377 return this.platformNamesCache; 378 } 379 380 } 381 382 private static class PlatformListCellRenderer extends DefaultListCellRenderer { 383 384 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 385 assert value instanceof PlatformKey : "Wrong model"; PlatformKey key = (PlatformKey) value; 387 String name; 388 if (key.isBroken()) { 389 name = NbBundle.getMessage (PlatformUiSupport.class,"TXT_BrokenPlatformFmt", key.getDisplayName()); 390 } 391 else { 392 name = key.getDisplayName(); 393 } 394 return super.getListCellRendererComponent(list, name, index, isSelected, cellHasFocus); 395 } 396 } 397 398 private static class SourceLevelComboBoxModel extends AbstractListModel implements ComboBoxModel , ListDataListener { 399 400 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; 405 private SpecificationVersion selectedSourceLevel; 406 private SpecificationVersion[] sourceLevelCache; 407 private final ComboBoxModel platformComboBoxModel; 408 private PlatformKey activePlatform; 409 private String j2eePlatform = null; 410 411 public SourceLevelComboBoxModel (ComboBoxModel platformComboBoxModel, String initialValue) { 412 this.platformComboBoxModel = platformComboBoxModel; 413 this.activePlatform = (PlatformKey) this.platformComboBoxModel.getSelectedItem(); 414 this.platformComboBoxModel.addListDataListener (this); 415 if (initialValue != null && initialValue.length()>0) { 416 try { 417 this.selectedSourceLevel = new SpecificationVersion (initialValue); 418 } catch (NumberFormatException nfe) { 419 ErrorManager.getDefault().log("Invalid javac.source: " + initialValue); 421 } 422 } 423 } 424 425 public SourceLevelComboBoxModel (ComboBoxModel platformComboBoxModel, String initialValue, String j2eePlatform) { 426 this(platformComboBoxModel, initialValue); 427 this.j2eePlatform = j2eePlatform; 428 } 429 430 public int getSize () { 431 SpecificationVersion[] sLevels = getSourceLevels (); 432 return sLevels.length; 433 } 434 435 public Object getElementAt (int index) { 436 SpecificationVersion[] sLevels = getSourceLevels (); 437 assert index >=0 && index< sLevels.length; 438 return sLevels[index]; 439 } 440 441 public Object getSelectedItem () { 442 List sLevels = Arrays.asList(getSourceLevels ()); 443 if (this.selectedSourceLevel != null) { 444 if (!sLevels.contains(this.selectedSourceLevel)) { 445 if (sLevels.size()>0) { 446 this.selectedSourceLevel = (SpecificationVersion) sLevels.get(sLevels.size()-1); 447 } 448 else { 449 this.selectedSourceLevel = null; 450 } 451 } 452 } 453 return this.selectedSourceLevel; 454 } 455 456 public void setSelectedItem (Object obj) { 457 this.selectedSourceLevel = (SpecificationVersion) obj; 458 this.fireContentsChanged(this, -1, -1); 459 } 460 461 public void intervalAdded(ListDataEvent e) { 462 } 463 464 public void intervalRemoved(ListDataEvent e) { 465 } 466 467 public void contentsChanged(ListDataEvent e) { 468 PlatformKey selectedPlatform = (PlatformKey) this.platformComboBoxModel.getSelectedItem(); 469 JavaPlatform platform = getPlatform(selectedPlatform); 470 if (platform != null) { 471 SpecificationVersion version = platform.getSpecification().getVersion(); 472 if (this.selectedSourceLevel != null && this.selectedSourceLevel.compareTo(version)>0 && 473 !shouldChangePlatform (selectedSourceLevel, version)) { 474 this.platformComboBoxModel.setSelectedItem(this.activePlatform); 476 return; 477 } 478 } 479 this.activePlatform = selectedPlatform; 480 resetCache (); 481 } 482 483 private void resetCache () { 484 synchronized (this) { 485 this.sourceLevelCache = null; 486 } 487 this.fireContentsChanged(this, -1, -1); 488 } 489 490 private SpecificationVersion[] getSourceLevels () { 491 if (this.sourceLevelCache == null) { 492 PlatformKey selectedPlatform = (PlatformKey) this.platformComboBoxModel.getSelectedItem(); 493 JavaPlatform platform = getPlatform(selectedPlatform); 494 List sLevels = new ArrayList (); 495 if (platform != null) { 498 SpecificationVersion version = platform.getSpecification().getVersion(); 499 int index = INITIAL_VERSION_MINOR; 500 if (j2eePlatform != null && j2eePlatform.equals("Java EE 5")) { 502 index = INITIAL_VERSION_MINOR_JAVA_EE_5; 503 } 504 SpecificationVersion template = new SpecificationVersion (VERSION_PREFIX + Integer.toString (index++)); 505 while (template.compareTo(version)<=0) { 506 sLevels.add (template); 507 template = new SpecificationVersion (VERSION_PREFIX + Integer.toString (index++)); 508 } 509 } 510 this.sourceLevelCache = (SpecificationVersion[]) sLevels.toArray(new SpecificationVersion[sLevels.size()]); 511 } 512 return this.sourceLevelCache; 513 } 514 515 private static boolean shouldChangePlatform (SpecificationVersion selectedSourceLevel, SpecificationVersion platformSourceLevel) { 516 JButton changeOption = new JButton (NbBundle.getMessage(PlatformUiSupport.class, "CTL_ChangePlatform")); 517 changeOption.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PlatformUiSupport.class, "AD_ChangePlatform")); 518 String message = MessageFormat.format (NbBundle.getMessage(PlatformUiSupport.class,"TXT_ChangePlatform"),new Object [] { 519 selectedSourceLevel.toString(), 520 platformSourceLevel.toString(), 521 }); 522 return DialogDisplayer.getDefault().notify( 523 new NotifyDescriptor (message, 524 NbBundle.getMessage(PlatformUiSupport.class,"TXT_ChangePlatformTitle"), 525 NotifyDescriptor.DEFAULT_OPTION, 526 NotifyDescriptor.WARNING_MESSAGE, 527 new Object [] { 528 changeOption, 529 NotifyDescriptor.CANCEL_OPTION 530 }, 531 changeOption)) == changeOption; 532 } 533 } 534 535 private static String getDefaultSourceLevel(JavaPlatform platform) { 536 SpecificationVersion v = platform.getSpecification().getVersion(); 537 String javacSource = v.toString(); 538 if (javacSource.equals("1.6") || javacSource.equals("1.7")) 540 javacSource = "1.5"; 541 542 return javacSource; 543 } 544 545 } 546 | Popular Tags |