1 19 20 package org.netbeans.modules.j2ee.clientproject.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.JButton ; 34 import javax.swing.JList ; 35 import javax.swing.ListCellRenderer ; 36 import javax.swing.event.ListDataEvent ; 37 import javax.swing.event.ListDataListener ; 38 import org.netbeans.api.java.platform.JavaPlatform; 39 import org.netbeans.api.java.platform.JavaPlatformManager; 40 import org.netbeans.api.java.platform.Specification; 41 import org.netbeans.modules.j2ee.clientproject.AppClientProjectType; 42 import org.netbeans.modules.j2ee.clientproject.UpdateHelper; 43 import org.netbeans.spi.project.support.ant.EditableProperties; 44 import org.openide.DialogDisplayer; 45 import org.openide.ErrorManager; 46 import org.openide.NotifyDescriptor; 47 import org.openide.awt.HtmlRenderer; 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 61 private PlatformUiSupport() { 62 } 63 64 71 public static ComboBoxModel createPlatformComboBoxModel(String activePlatform) { 72 return new PlatformComboBoxModel(activePlatform); 73 } 74 75 76 81 public static ListCellRenderer createPlatformListCellRenderer() { 82 return new PlatformListCellRenderer(); 83 } 84 85 88 public static void storePlatform(EditableProperties props, UpdateHelper helper, String platformName, SpecificationVersion sourceLevel) { 89 PlatformKey platformKey; 90 if (platformName != null) { 91 platformKey = new PlatformKey(PlatformUiSupport.findPlatform(platformName)); 92 } else { 93 platformKey = new PlatformKey(JavaPlatformManager.getDefault().getDefaultPlatform()); 94 } 95 storePlatform(props, helper, platformKey, sourceLevel); 96 } 97 98 public static JavaPlatform findPlatform(String displayName) { 99 JavaPlatform[] platforms = JavaPlatformManager.getDefault().getPlatforms(displayName, new Specification("j2se", null)); return platforms.length == 0 ? null : platforms[0]; 101 } 102 103 110 public static void storePlatform(EditableProperties props, UpdateHelper helper, Object platformKey, SpecificationVersion sourceLevel) { 111 assert platformKey instanceof PlatformKey; 112 PlatformKey pk = (PlatformKey) platformKey; 113 JavaPlatform platform = getPlatform(pk); 114 if (platform != null) { 116 SpecificationVersion jdk13 = new SpecificationVersion("1.3"); String platformAntName = (String ) platform.getProperties().get("platform.ant.name"); assert platformAntName != null; 119 props.put(AppClientProjectProperties.JAVA_PLATFORM, platformAntName); 120 Element root = helper.getPrimaryConfigurationData(true); 121 boolean defaultPlatform = pk.isDefaultPlatform(); 122 boolean changed = false; 123 NodeList explicitPlatformNodes = root.getElementsByTagNameNS(AppClientProjectType.PROJECT_CONFIGURATION_NAMESPACE,"explicit-platform"); if (defaultPlatform) { 125 if (explicitPlatformNodes.getLength()==1) { 126 root.removeChild(explicitPlatformNodes.item(0)); 127 changed = true; 128 } 129 } else { 130 Element explicitPlatform; 131 switch (explicitPlatformNodes.getLength()) { 132 case 0: 133 explicitPlatform = root.getOwnerDocument().createElementNS(AppClientProjectType.PROJECT_CONFIGURATION_NAMESPACE, "explicit-platform"); NodeList sourceRootNodes = root.getElementsByTagNameNS(AppClientProjectType.PROJECT_CONFIGURATION_NAMESPACE,"source-roots"); assert sourceRootNodes.getLength() == 1 : "Broken project.xml file"; root.insertBefore(explicitPlatform, sourceRootNodes.item(0)); 137 changed = true; 138 break; 139 case 1: 140 explicitPlatform = (Element )explicitPlatformNodes.item(0); 141 break; 142 default: 143 throw new AssertionError ("Broken project.xml file"); } 145 String explicitSourceAttrValue = explicitPlatform.getAttribute("explicit-source-supported"); if (jdk13.compareTo(platform.getSpecification().getVersion())>=0 && 147 !"false".equals(explicitSourceAttrValue)) { explicitPlatform.setAttribute("explicit-source-supported","false"); changed = true; 150 } else if (jdk13.compareTo(platform.getSpecification().getVersion())<0 && 151 !"true".equals(explicitSourceAttrValue)) { explicitPlatform.setAttribute("explicit-source-supported","true"); changed = true; 154 } 155 } 156 157 if (sourceLevel == null) { 158 sourceLevel = platform.getSpecification().getVersion(); 159 } 160 String javacSource = sourceLevel.toString(); 161 if (javacSource.equals("1.6") || javacSource.equals("1.7")) 163 javacSource = "1.5"; 164 String javacTarget = jdk13.compareTo(sourceLevel)>=0 ? "1.1" : javacSource; if (!javacSource.equals(props.getProperty(AppClientProjectProperties.JAVAC_SOURCE))) { 166 props.setProperty(AppClientProjectProperties.JAVAC_SOURCE, javacSource); 167 } 168 if (!javacTarget.equals(props.getProperty(AppClientProjectProperties.JAVAC_TARGET))) { 169 props.setProperty(AppClientProjectProperties.JAVAC_TARGET, javacTarget); 170 } 171 172 if (changed) { 173 helper.putPrimaryConfigurationData(root, true); 174 } 175 } 176 } 177 178 179 187 public static JavaPlatform getPlatform(Object platformKey) { 188 if (platformKey instanceof PlatformKey) { 189 return getPlatform((PlatformKey)platformKey); 190 } else { 191 throw new IllegalArgumentException (); 192 } 193 } 194 195 203 public static ComboBoxModel createSourceLevelComboBoxModel(ComboBoxModel platformComboBoxModel, String initialValue, String j2eePlatform) { 204 return new SourceLevelComboBoxModel(platformComboBoxModel, initialValue, j2eePlatform); 205 } 206 207 public static ListCellRenderer createSourceLevelListCellRenderer() { 208 return new SourceLevelListCellRenderer(); 209 } 210 211 212 private static JavaPlatform getPlatform(PlatformKey platformKey) { 213 return platformKey.platform; 214 } 215 216 217 222 private static class PlatformKey implements Comparable { 223 224 private String name; 225 private JavaPlatform platform; 226 227 231 public PlatformKey(String name) { 232 assert name != null; 233 this.name = name; 234 } 235 236 240 public PlatformKey(JavaPlatform platform) { 241 assert platform != null; 242 this.platform = platform; 243 } 244 245 public int compareTo(Object o) { 246 return this.getDisplayName().compareTo(((PlatformKey)o).getDisplayName()); 247 } 248 249 public boolean equals(Object other) { 250 if (other instanceof PlatformKey) { 251 PlatformKey otherKey = (PlatformKey)other; 252 return (this.platform == null ? otherKey.platform == null : this.platform.equals(otherKey.platform)) && 253 otherKey.getDisplayName().equals(this.getDisplayName()); 254 } else { 255 return false; 256 } 257 } 258 259 public int hashCode() { 260 return getDisplayName().hashCode(); 261 } 262 263 public String toString() { 264 return getDisplayName(); 265 } 266 267 public synchronized String getDisplayName() { 268 if (this.name == null) { 269 this.name = this.platform.getDisplayName(); 270 } 271 return this.name; 272 } 273 274 public boolean isDefaultPlatform() { 275 if (this.platform == null) { 276 return false; 277 } 278 return this.platform.equals(JavaPlatformManager.getDefault().getDefaultPlatform()); 279 } 280 281 public boolean isBroken() { 282 return this.platform == null; 283 } 284 285 } 286 287 private static final class SourceLevelKey implements Comparable { 288 289 final SpecificationVersion sourceLevel; 290 final boolean broken; 291 292 public SourceLevelKey(final SpecificationVersion sourceLevel) { 293 this(sourceLevel, false); 294 } 295 296 public SourceLevelKey(final SpecificationVersion sourceLevel, final boolean broken) { 297 assert sourceLevel != null : "Source level cannot be null"; this.sourceLevel = sourceLevel; 299 this.broken = broken; 300 } 301 302 public SpecificationVersion getSourceLevel() { 303 return this.sourceLevel; 304 } 305 306 public boolean isBroken() { 307 return this.broken; 308 } 309 310 public int compareTo(final Object other) { 311 assert other instanceof SourceLevelKey : "Illegal argument of SourceLevelKey.compareTo()"; SourceLevelKey otherKey = (SourceLevelKey) other; 313 return this.sourceLevel.compareTo(otherKey.sourceLevel); 314 } 315 316 public boolean equals(final Object other) { 317 return (other instanceof SourceLevelKey) && 318 ((SourceLevelKey)other).sourceLevel.equals(this.sourceLevel); 319 } 320 321 public int hashCode() { 322 return this.sourceLevel.hashCode(); 323 } 324 325 public String toString() { 326 StringBuffer buffer = new StringBuffer (); 327 if (this.broken) { 328 buffer.append("Broken: "); } 330 buffer.append(this.sourceLevel.toString()); 331 return buffer.toString(); 332 } 333 334 } 335 336 private static class PlatformComboBoxModel extends AbstractListModel implements ComboBoxModel , PropertyChangeListener { 337 338 private final JavaPlatformManager pm; 339 private PlatformKey[] platformNamesCache; 340 private String initialPlatform; 341 private PlatformKey selectedPlatform; 342 343 public PlatformComboBoxModel(String initialPlatform) { 344 this.pm = JavaPlatformManager.getDefault(); 345 this.pm.addPropertyChangeListener(WeakListeners.propertyChange(this, this.pm)); 346 this.initialPlatform = initialPlatform; 347 } 348 349 public int getSize() { 350 PlatformKey[] platformNames = getPlatformNames(); 351 return platformNames.length; 352 } 353 354 public Object getElementAt(int index) { 355 PlatformKey[] platformNames = getPlatformNames(); 356 assert index >=0 && index< platformNames.length; 357 return platformNames[index]; 358 } 359 360 public Object getSelectedItem() { 361 this.getPlatformNames(); return this.selectedPlatform; 363 } 364 365 public void setSelectedItem(Object obj) { 366 this.selectedPlatform = (PlatformKey) obj; 367 this.fireContentsChanged(this, -1, -1); 368 } 369 370 public void propertyChange(PropertyChangeEvent event) { 371 if (JavaPlatformManager.PROP_INSTALLED_PLATFORMS.equals(event.getPropertyName())) { 372 synchronized (this) { 373 this.platformNamesCache = null; 374 } 375 this.fireContentsChanged(this, -1, -1); 376 } 377 } 378 379 private synchronized PlatformKey[] getPlatformNames() { 380 if (this.platformNamesCache == null) { 381 JavaPlatform[] platforms = pm.getPlatforms(null, new Specification("j2se",null)); Set <PlatformKey> orderedNames = new TreeSet <PlatformKey>(); 383 boolean activeFound = false; 384 for (int i=0; i< platforms.length; i++) { 385 if (platforms[i].getInstallFolders().size()>0) { 386 PlatformKey pk = new PlatformKey(platforms[i]); 387 orderedNames.add(pk); 388 if (!activeFound && initialPlatform != null) { 389 String antName = (String ) platforms[i].getProperties().get("platform.ant.name"); if (initialPlatform.equals(antName)) { 391 if (this.selectedPlatform == null) { 392 this.selectedPlatform = pk; 393 initialPlatform = null; 394 } 395 activeFound = true; 396 } 397 } 398 } 399 } 400 if (!activeFound) { 401 if (initialPlatform == null) { 402 if (this.selectedPlatform == null || !orderedNames.contains(this.selectedPlatform)) { 403 this.selectedPlatform = new PlatformKey(JavaPlatformManager.getDefault().getDefaultPlatform()); 404 } 405 } else { 406 PlatformKey pk = new PlatformKey(this.initialPlatform); 407 orderedNames.add(pk); 408 if (this.selectedPlatform == null) { 409 this.selectedPlatform = pk; 410 } 411 } 412 } 413 this.platformNamesCache = orderedNames.toArray(new PlatformKey[orderedNames.size()]); 414 } 415 return this.platformNamesCache; 416 } 417 418 } 419 420 private static class PlatformListCellRenderer implements ListCellRenderer { 421 422 private final ListCellRenderer delegate; 423 424 public PlatformListCellRenderer() { 425 this.delegate = HtmlRenderer.createRenderer(); 426 } 427 428 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 429 String name; 430 if (value == null) { 431 name = ""; } else { 433 assert value instanceof PlatformKey : "Wrong model"; PlatformKey key = (PlatformKey) value; 435 if (key.isBroken()) { 436 name = "<html><font color=\"#A40000\">" + NbBundle.getMessage(PlatformUiSupport.class,"TXT_BrokenPlatformFmt", key.getDisplayName()); 438 } else { 439 name = key.getDisplayName(); 440 } 441 } 442 return this.delegate.getListCellRendererComponent(list, name, index, isSelected, cellHasFocus); 443 } 444 } 445 446 private static class SourceLevelComboBoxModel extends AbstractListModel implements ComboBoxModel , ListDataListener { 447 448 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; 453 private SpecificationVersion selectedSourceLevel; 454 private SpecificationVersion[] sourceLevelCache; 455 private final ComboBoxModel platformComboBoxModel; 456 private PlatformKey activePlatform; 457 private String j2eePlatform = null; 458 459 public SourceLevelComboBoxModel(ComboBoxModel platformComboBoxModel, String initialValue) { 460 this.platformComboBoxModel = platformComboBoxModel; 461 this.activePlatform = (PlatformKey) this.platformComboBoxModel.getSelectedItem(); 462 this.platformComboBoxModel.addListDataListener(this); 463 if (initialValue != null && initialValue.length()>0) { 464 try { 465 this.selectedSourceLevel = new SpecificationVersion (initialValue); 466 } catch (NumberFormatException nfe) { 467 ErrorManager.getDefault().log("Invalid javac.source: " + initialValue); 469 } 470 } 471 } 472 473 public SourceLevelComboBoxModel(ComboBoxModel platformComboBoxModel, String initialValue, String j2eePlatform) { 474 this(platformComboBoxModel, initialValue); 475 this.j2eePlatform = j2eePlatform; 476 } 477 478 public int getSize() { 479 SpecificationVersion[] sLevels = getSourceLevels(); 481 return sLevels.length; 482 } 483 484 public Object getElementAt(int index) { 485 SpecificationVersion[] sLevels = getSourceLevels(); 486 assert index >=0 && index< sLevels.length; 487 return sLevels[index]; 488 } 489 490 public Object getSelectedItem() { 491 List sLevels = Arrays.asList(getSourceLevels()); 492 if (this.selectedSourceLevel != null) { 493 if (!sLevels.contains(this.selectedSourceLevel)) { 494 if (sLevels.size()>0) { 495 this.selectedSourceLevel = (SpecificationVersion) sLevels.get(sLevels.size()-1); 496 } else { 497 this.selectedSourceLevel = null; 498 } 499 } 500 } 501 return this.selectedSourceLevel; 502 } 503 504 public void setSelectedItem(Object obj) { 505 this.selectedSourceLevel = (SpecificationVersion) obj; 506 this.fireContentsChanged(this, -1, -1); 507 } 508 509 public void intervalAdded(ListDataEvent e) { 510 } 511 512 public void intervalRemoved(ListDataEvent e) { 513 } 514 515 public void contentsChanged(ListDataEvent e) { 516 PlatformKey selectedPlatform = (PlatformKey) this.platformComboBoxModel.getSelectedItem(); 517 JavaPlatform platform = getPlatform(selectedPlatform); 518 if (platform != null) { 519 SpecificationVersion version = platform.getSpecification().getVersion(); 520 if (this.selectedSourceLevel != null && this.selectedSourceLevel.compareTo(version)>0 && 521 !shouldChangePlatform(selectedSourceLevel, version)) { 522 this.platformComboBoxModel.setSelectedItem(this.activePlatform); 524 return; 525 } 526 } 527 this.activePlatform = selectedPlatform; 528 resetCache(); 529 } 530 531 private void resetCache() { 532 synchronized (this) { 533 this.sourceLevelCache = null; 534 } 535 this.fireContentsChanged(this, -1, -1); 536 } 537 538 private SpecificationVersion[] getSourceLevels() { 539 if (this.sourceLevelCache == null) { 540 PlatformKey selectedPlatform = (PlatformKey) this.platformComboBoxModel.getSelectedItem(); 541 JavaPlatform platform = getPlatform(selectedPlatform); 542 List <SpecificationVersion> sLevels = new ArrayList <SpecificationVersion>(); 543 if (platform != null) { 546 SpecificationVersion version = platform.getSpecification().getVersion(); 547 int index = INITIAL_VERSION_MINOR; 548 if (j2eePlatform != null && j2eePlatform.equals(AppClientProjectProperties.JAVA_EE_5)) { 550 index = INITIAL_VERSION_MINOR_JAVA_EE_5; 551 } 552 SpecificationVersion template = new SpecificationVersion(VERSION_PREFIX + Integer.toString(index++)); 553 while (template.compareTo(version)<=0) { 554 sLevels.add(template); 555 template = new SpecificationVersion(VERSION_PREFIX + Integer.toString(index++)); 556 } 557 } 558 this.sourceLevelCache = sLevels.toArray(new SpecificationVersion[sLevels.size()]); 559 } 560 return this.sourceLevelCache; 561 } 562 563 private static boolean shouldChangePlatform(SpecificationVersion selectedSourceLevel, SpecificationVersion platformSourceLevel) { 564 JButton changeOption = new JButton (NbBundle.getMessage(PlatformUiSupport.class, "CTL_ChangePlatform")); 565 changeOption.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PlatformUiSupport.class, "AD_ChangePlatform")); 566 String message = MessageFormat.format(NbBundle.getMessage(PlatformUiSupport.class,"TXT_ChangePlatform"),new Object [] { 567 selectedSourceLevel.toString(), 568 platformSourceLevel.toString(), 569 }); 570 return DialogDisplayer.getDefault().notify( 571 new NotifyDescriptor(message, 572 NbBundle.getMessage(PlatformUiSupport.class,"TXT_ChangePlatformTitle"), 573 NotifyDescriptor.DEFAULT_OPTION, 574 NotifyDescriptor.WARNING_MESSAGE, 575 new Object [] { 576 changeOption, 577 NotifyDescriptor.CANCEL_OPTION 578 }, 579 changeOption)) == changeOption; 580 } 581 } 582 583 private static class SourceLevelListCellRenderer implements ListCellRenderer { 584 585 ListCellRenderer delegate; 586 587 public SourceLevelListCellRenderer() { 588 this.delegate = HtmlRenderer.createRenderer(); 589 } 590 591 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 592 String message; 593 if (value == null) { 594 message = ""; } else { 596 assert value instanceof SpecificationVersion; 597 SpecificationVersion key = (SpecificationVersion) value; 598 message = key.toString(); 599 } 600 return this.delegate.getListCellRendererComponent(list, message, index, isSelected, cellHasFocus); 601 } 602 } 603 604 } 605 | Popular Tags |