KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > ui > customizer > PlatformUiSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ejbjarproject.ui.customizer;
21
22 import java.awt.Component JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.TreeSet JavaDoc;
31 import javax.swing.AbstractListModel JavaDoc;
32 import javax.swing.ComboBoxModel JavaDoc;
33 import javax.swing.DefaultListCellRenderer JavaDoc;
34 import javax.swing.JButton JavaDoc;
35 import javax.swing.JList JavaDoc;
36 import javax.swing.ListCellRenderer JavaDoc;
37 import javax.swing.event.ListDataEvent JavaDoc;
38 import javax.swing.event.ListDataListener JavaDoc;
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 JavaDoc;
52 import org.w3c.dom.NodeList JavaDoc;
53
54 /**
55  * Support class for {@link JavaPlatform} manipulation in ejbjarproject customizer.
56  * @author tzezula
57  */

58 public class PlatformUiSupport {
59     
60     private PlatformUiSupport() {
61     }
62     
63     /**
64      * Creates {@link ComboBoxModel} of J2SE platforms.
65      * The model listens on the {@link JavaPlatformManager} and update its
66      * state according to changes
67      * @param activePlatform the active project's platform
68      * @return {@link ComboBoxModel}
69      */

70     public static ComboBoxModel JavaDoc createPlatformComboBoxModel (String JavaDoc activePlatform) {
71         return new PlatformComboBoxModel (activePlatform);
72     }
73     
74     /**
75      * Creates a {@link ListCellRenderer} for rendering items of the {@link ComboBoxModel}
76      * created by the {@link PlatformUiSupport#createPlatformComboBoxModel} method.
77      * @return {@link ListCellRenderer}
78      */

79     public static ListCellRenderer JavaDoc createPlatformListCellRenderer () {
80         return new PlatformListCellRenderer ();
81     }
82
83     /**
84      * Like {@link #storePlatform}, but platformName may be null (in which case the default platform is used)
85      */

86     public static void storePlatform (EditableProperties props, UpdateHelper helper, String JavaDoc 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     /**
97      * Stores active platform, javac.source and javac.target into the project's metadata
98      * @param props project's shared properties
99      * @param helper to read/update project.xml
100      * @param platformKey the PatformKey got from the platform model
101      * @param sourceLevel source level
102      */

103     public static void storePlatform (EditableProperties props, UpdateHelper helper, Object JavaDoc platformKey, SpecificationVersion sourceLevel) {
104         assert platformKey instanceof PlatformKey;
105         PlatformKey pk = (PlatformKey) platformKey;
106         JavaPlatform platform = getPlatform(pk);
107         //null means active broken (unresolved) platform, no need to do anything
108
if (platform != null) {
109             String JavaDoc platformAntName = (String JavaDoc) platform.getProperties().get("platform.ant.name"); //NOI18N
110
assert platformAntName != null;
111             props.put(EjbJarProjectProperties.JAVA_PLATFORM, platformAntName);
112             Element JavaDoc root = helper.getPrimaryConfigurationData(true);
113             boolean defaultPlatform = pk.isDefaultPlatform();
114             boolean changed = false;
115             NodeList JavaDoc explicitPlatformNodes = root.getElementsByTagNameNS (EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,"explicit-platform"); //NOI18N
116
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 JavaDoc newTargetValue;
123                 String JavaDoc newSourceValue;
124                 if (sourceLevel == null || sourceLevel.equals (platformVersion)){
125                     newTargetValue = newSourceValue = getDefaultSourceLevel(platform);
126                 }
127                 else {
128                     newTargetValue = newSourceValue = sourceLevel.toString();
129                 }
130                 String JavaDoc oldTargetValue = props.getProperty (EjbJarProjectProperties.JAVAC_TARGET);
131                 String JavaDoc 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 JavaDoc explicitPlatform;
141                 switch (explicitPlatformNodes.getLength()) {
142                     case 0:
143                         explicitPlatform = root.getOwnerDocument().createElementNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, "explicit-platform"); //NOI18N
144
NodeList JavaDoc sourceRootNodes = root.getElementsByTagNameNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,"minimum-ant-version"); //NOI18N
145
assert sourceRootNodes.getLength() == 1 : "Broken project.xml file"; //NOI18N
146
root.insertBefore(explicitPlatform, sourceRootNodes.item(0).getNextSibling());
147                         changed = true;
148                         break;
149                     case 1:
150                         explicitPlatform = (Element JavaDoc)explicitPlatformNodes.item(0);
151                         break;
152                     default:
153                         throw new AssertionError JavaDoc("Broken project.xml file"); //NOI18N
154
}
155                 SpecificationVersion jdk13 = new SpecificationVersion ("1.3"); //NOI18N
156
String JavaDoc explicitSourceAttrValue = explicitPlatform.getAttribute("explicit-source-supported"); //NOI18N
157
if (jdk13.compareTo(platform.getSpecification().getVersion())>=0 &&
158                     !"false".equals(explicitSourceAttrValue)) { //NOI18N
159
explicitPlatform.setAttribute("explicit-source-supported","false"); //NOI18N
160
changed = true;
161                 }
162                 else if (jdk13.compareTo(platform.getSpecification().getVersion())<0 &&
163                     !"true".equals(explicitSourceAttrValue)) { //NOI18N
164
explicitPlatform.setAttribute("explicit-source-supported","true"); //NOI18N
165
changed = true;
166                 }
167                 if (sourceLevel == null) {
168                     sourceLevel = platform.getSpecification().getVersion();
169                 }
170                 String JavaDoc javacSource = sourceLevel.toString();
171                 // #89131: these levels are not actually distinct from 1.5.
172
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 JavaDoc displayName) {
188         JavaPlatform[] platforms = JavaPlatformManager.getDefault().getPlatforms(displayName, new Specification("j2se", null)); //NOI18N
189
return platforms.length == 0 ? null : platforms[0];
190     }
191     
192     
193     /**
194      * Returns a {@link JavaPlatform} for an item obtained from the ComboBoxModel created by
195      * the {@link PlatformUiSupport#createComboBoxModel} method
196      * @param platformKey an item obtained from ComboBoxModel created by {@link PlatformUiSupport#createComboBoxModel}
197      * @return JavaPlatform or null in case when platform is broken
198      * @exception {@link IllegalArgumentException} is thrown in case when parameter in not an object created by
199      * platform combobox model.
200      */

201     public static JavaPlatform getPlatform (Object JavaDoc platformKey) {
202        if (platformKey instanceof PlatformKey) {
203            return getPlatform ((PlatformKey)platformKey);
204        }
205        else {
206            throw new IllegalArgumentException JavaDoc ();
207        }
208     }
209     
210     /**
211      * Creates {@link ComboBoxModel} of source levels for active platform.
212      * The model listens on the platform's {@link ComboBoxModel} and update its
213      * state according to changes
214      * @param platformComboBoxModel the platform's model used for listenning
215      * @param initialValue initial source level value
216      * @return {@link ComboBoxModel} of {@link SpecificationVersion}
217      */

218     public static ComboBoxModel JavaDoc createSourceLevelComboBoxModel (ComboBoxModel JavaDoc platformComboBoxModel, String JavaDoc initialValue, String JavaDoc j2eePlatform) {
219         return new SourceLevelComboBoxModel (platformComboBoxModel, initialValue, j2eePlatform);
220     }
221     
222     private static JavaPlatform getPlatform (PlatformKey platformKey) {
223         return platformKey.platform;
224     }
225     
226     /**
227      * This class represents a JavaPlatform in the {@link ListModel}
228      * created by the {@link PlatformUiSupport#createPlatformComboBoxModel}
229      * method.
230      */

231     private static class PlatformKey implements Comparable JavaDoc {
232         
233         private String JavaDoc name;
234         private JavaPlatform platform;
235         
236         /**
237          * Creates a PlatformKey for a broken platform
238          * @param name the ant name of the broken platform
239          */

240         public PlatformKey (String JavaDoc name) {
241             assert name != null;
242             this.name = name;
243         }
244         
245         /**
246          * Creates a PlatformKey for a platform
247          * @param platform the {@link JavaPlatform}
248          */

249         public PlatformKey (JavaPlatform platform) {
250             assert platform != null;
251             this.platform = platform;
252         }
253
254         public int compareTo(Object JavaDoc o) {
255             return this.getDisplayName().compareTo(((PlatformKey)o).getDisplayName());
256         }
257         
258         public boolean equals (Object JavaDoc 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 JavaDoc toString () {
274             return getDisplayName ();
275         }
276         
277         public synchronized String JavaDoc 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 JavaDoc implements ComboBoxModel JavaDoc, PropertyChangeListener JavaDoc {
297         
298         private final JavaPlatformManager pm;
299         private PlatformKey[] platformNamesCache;
300         private String JavaDoc initialPlatform;
301         private PlatformKey selectedPlatform;
302         
303         public PlatformComboBoxModel (String JavaDoc 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 JavaDoc getElementAt (int index) {
315             PlatformKey[] platformNames = getPlatformNames ();
316             assert index >=0 && index< platformNames.length;
317             return platformNames[index];
318         }
319         
320         public Object JavaDoc getSelectedItem () {
321             this.getPlatformNames(); //Force setting of selectedPlatform if it is not alredy done
322
return this.selectedPlatform;
323         }
324         
325         public void setSelectedItem (Object JavaDoc obj) {
326             this.selectedPlatform = (PlatformKey) obj;
327             this.fireContentsChanged(this, -1, -1);
328         }
329         
330         public void propertyChange (PropertyChangeEvent JavaDoc 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)); //NOI18N
342
Set JavaDoc/*<PlatformKey>*/ orderedNames = new TreeSet JavaDoc ();
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 JavaDoc antName = (String JavaDoc) platforms[i].getProperties().get("platform.ant.name"); //NOI18N
350
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 JavaDoc {
382
383         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value, int index, boolean isSelected, boolean cellHasFocus) {
384             assert value instanceof PlatformKey : "Wrong model"; //NOI18N
385
PlatformKey key = (PlatformKey) value;
386             String JavaDoc 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 JavaDoc implements ComboBoxModel JavaDoc, ListDataListener JavaDoc {
398         
399         private static final String JavaDoc VERSION_PREFIX = "1."; //The version prefix
400
private static final int INITIAL_VERSION_MINOR = 2; //1.2
401
// if project is JAVA EE 5 show only 1.5 and higher
402
private static final int INITIAL_VERSION_MINOR_JAVA_EE_5 = 5; // 1.5
403

404         private SpecificationVersion selectedSourceLevel;
405         private SpecificationVersion[] sourceLevelCache;
406         private final ComboBoxModel JavaDoc platformComboBoxModel;
407         private PlatformKey activePlatform;
408         private String JavaDoc j2eePlatform = null;
409         
410         public SourceLevelComboBoxModel (ComboBoxModel JavaDoc platformComboBoxModel, String JavaDoc 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 JavaDoc nfe) {
418                     // If the javac.source has invalid value, do not preselect and log it.
419
ErrorManager.getDefault().log("Invalid javac.source: " + initialValue);
420                 }
421             }
422         }
423         
424         public SourceLevelComboBoxModel (ComboBoxModel JavaDoc platformComboBoxModel, String JavaDoc initialValue, String JavaDoc 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 JavaDoc getElementAt (int index) {
435             SpecificationVersion[] sLevels = getSourceLevels ();
436             assert index >=0 && index< sLevels.length;
437             return sLevels[index];
438         }
439         
440         public Object JavaDoc getSelectedItem () {
441             List JavaDoc 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 JavaDoc obj) {
456             this.selectedSourceLevel = (SpecificationVersion) obj;
457             this.fireContentsChanged(this, -1, -1);
458         }
459         
460         public void intervalAdded(ListDataEvent JavaDoc e) {
461         }
462
463         public void intervalRemoved(ListDataEvent JavaDoc e) {
464         }
465
466         public void contentsChanged(ListDataEvent JavaDoc 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                     //Restore original
474
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 JavaDoc/*<SpecificationVersion>*/ sLevels = new ArrayList JavaDoc ();
494                 //If platform == null broken platform, the source level range is unknown
495
//The source level combo box should be empty and disabled
496
if (platform != null) {
497                     SpecificationVersion version = platform.getSpecification().getVersion();
498                     int index = INITIAL_VERSION_MINOR;
499                     // #71619 - source level lower than 1.5 won't be shown for Java EE 5 project
500
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 JavaDoc changeOption = new JButton JavaDoc (NbBundle.getMessage(PlatformUiSupport.class, "CTL_ChangePlatform"));
516             changeOption.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PlatformUiSupport.class, "AD_ChangePlatform"));
517             String JavaDoc message = MessageFormat.format (NbBundle.getMessage(PlatformUiSupport.class,"TXT_ChangePlatform"),new Object JavaDoc[] {
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 JavaDoc[] {
527                             changeOption,
528                             NotifyDescriptor.CANCEL_OPTION
529                         },
530                         changeOption)) == changeOption;
531         }
532     }
533
534     private static String JavaDoc getDefaultSourceLevel(JavaPlatform platform) {
535         SpecificationVersion v = platform.getSpecification().getVersion();
536         String JavaDoc javacSource = v.toString();
537         // #89131: these levels are not actually distinct from 1.5.
538
if (javacSource.equals("1.6") || javacSource.equals("1.7"))
539             javacSource = "1.5";
540
541         return javacSource;
542     }
543
544 }
545
Popular Tags