KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > platform > PlatformComponentFactory


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.project.ui.platform;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.text.Collator JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Comparator JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.TreeSet JavaDoc;
33 import javax.swing.AbstractListModel JavaDoc;
34 import javax.swing.ComboBoxModel JavaDoc;
35 import javax.swing.DefaultListCellRenderer JavaDoc;
36 import javax.swing.JComboBox JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.JList JavaDoc;
39 import javax.swing.ListCellRenderer JavaDoc;
40 import javax.swing.ListSelectionModel JavaDoc;
41 import javax.swing.MutableComboBoxModel JavaDoc;
42 import javax.swing.UIManager JavaDoc;
43 import javax.swing.plaf.UIResource JavaDoc;
44 import org.netbeans.api.project.Project;
45 import org.netbeans.api.project.ui.OpenProjects;
46 import org.netbeans.modules.apisupport.project.ui.customizer.SuiteUtils;
47 import org.netbeans.modules.apisupport.project.universe.NbPlatform;
48 import org.netbeans.modules.apisupport.project.universe.ModuleEntry;
49 import org.openide.ErrorManager;
50 import org.openide.filesystems.FileUtil;
51
52 /**
53  * Factory for creating miscellaneous UI components, their models and renderers
54  * as they are needed through the code of this module.
55  *
56  * @author Martin Krauskopf
57  */

58 public final class PlatformComponentFactory {
59     
60     private static final Color JavaDoc INVALID_PLAF_COLOR = UIManager.getColor("nb.errorForeground"); // NOI18N
61

62     /** Set of suites added by the user in <em>this</em> IDE session. */
63     private static Set JavaDoc<String JavaDoc> userSuites = new TreeSet JavaDoc(Collator.getInstance());
64     
65     private PlatformComponentFactory() {
66         // don't allow instances
67
}
68     
69     /**
70      * Returns <code>JComboBox</code> initialized with {@link
71      * NbPlatformListModel} which contains all NetBeans platform.
72      */

73     public static JComboBox JavaDoc getNbPlatformsComboxBox() {
74         JComboBox JavaDoc plafComboBox = new JComboBox JavaDoc(new NbPlatformListModel());
75         plafComboBox.setRenderer(new NbPlatformListRenderer());
76         return plafComboBox;
77     }
78     
79     /**
80      * Returns <code>JList</code> initialized with {@link NbPlatformListModel}
81      * which contains all NetBeans platform.
82      */

83     public static JList JavaDoc getNbPlatformsList() {
84         JList JavaDoc plafList = new JList JavaDoc(new NbPlatformListModel());
85         plafList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
86         plafList.setCellRenderer(new NbPlatformListRenderer());
87         return plafList;
88     }
89     
90     /**
91      * Returns <code>JComboBox</code> containing all suites. Also see
92      * {@link #addUserSuite}.
93      */

94     public static JComboBox JavaDoc getSuitesComboBox() {
95         MutableComboBoxModel JavaDoc model = new SuiteListModel(userSuites);
96         Project[] projects = OpenProjects.getDefault().getOpenProjects();
97         for (int i = 0; i < projects.length; i++) {
98             String JavaDoc suiteDir = SuiteUtils.getSuiteDirectoryPath(projects[i]);
99             if (suiteDir != null) {
100                 model.addElement(suiteDir);
101             }
102         }
103         JComboBox JavaDoc suiteCombo = new JComboBox JavaDoc(model);
104         if (model.getSize() > 0) {
105             suiteCombo.setSelectedIndex(0);
106         }
107         return suiteCombo;
108     }
109     
110     /**
111      * Adds <code>suiteDir</code> to the list of suites returned by the
112      * {@link #getSuitesComboBox} method. Such a suites are remembered
113      * <b>only</b> for the current IDE session.
114      */

115     public static void addUserSuite(String JavaDoc suiteDir) {
116         userSuites.add(suiteDir);
117     }
118     
119     public static ListCellRenderer JavaDoc getURLListRenderer() {
120         return new URLListRenderer();
121     }
122     
123     /**
124      * Render {@link NbPlatform} using its computed display name. If computation
125      * fails platform ID is used as a fallback. For <code>null</code> values
126      * renders an empty string.
127      * <p>Use in conjuction with {@link NbPlatformListModel}</p>
128      */

129     private static class NbPlatformListRenderer extends JLabel JavaDoc implements ListCellRenderer JavaDoc, UIResource JavaDoc {
130         
131         public NbPlatformListRenderer () {
132             setOpaque(true);
133         }
134         
135         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value,
136                 int index, boolean isSelected, boolean cellHasFocus) {
137             // #93658: GTK needs name to render cell renderer "natively"
138
setName("ComboBox.listRenderer"); // NOI18N
139

140             NbPlatform plaf = ((NbPlatform) value);
141             // NetBeans.org modules doesn't have platform at all --> null
142
String JavaDoc text = plaf == null ? "" : plaf.getLabel(); // NOI18N
143
setText(text);
144             
145             if ( isSelected ) {
146                 setBackground(list.getSelectionBackground());
147                 setForeground(list.getSelectionForeground());
148             }
149             else {
150                 setBackground(list.getBackground());
151                 setForeground(list.getForeground());
152             }
153             
154             if (plaf != null && !plaf.isValid()) {
155                 setForeground(INVALID_PLAF_COLOR);
156             }
157             
158             return this;
159         }
160         
161         // #93658: GTK needs name to render cell renderer "natively"
162
public String JavaDoc getName() {
163             String JavaDoc name = super.getName();
164             return name == null ? "ComboBox.renderer" : name; // NOI18N
165
}
166         
167     }
168     
169     /**
170      * Returns model containing all <em>currently</em> registered NbPlatforms.
171      * See also {@link NbPlatform#getPlatforms}.
172      * <p>Use in conjuction with {@link NbPlatformListRenderer}</p>
173      */

174     public static class NbPlatformListModel extends AbstractListModel JavaDoc
175             implements ComboBoxModel JavaDoc {
176         
177         private static NbPlatform[] getSortedPlatforms() {
178             Set JavaDoc _platforms = NbPlatform.getPlatforms();
179             NbPlatform[] platforms = (NbPlatform[]) _platforms.toArray(new NbPlatform[_platforms.size()]);
180             Arrays.sort(platforms, new Comparator JavaDoc() {
181                 public int compare(Object JavaDoc o1, Object JavaDoc o2) {
182                     int res = Collator.getInstance().compare(((NbPlatform) o1).getLabel(), ((NbPlatform) o2).getLabel());
183                     if (res != 0) {
184                         return res;
185                     } else {
186                         return System.identityHashCode(o1) - System.identityHashCode(o2);
187                     }
188                 }
189             });
190             return platforms;
191         }
192         
193         private NbPlatform[] nbPlafs;
194         private Object JavaDoc selectedPlaf;
195         
196         public NbPlatformListModel() {
197             nbPlafs = getSortedPlatforms();
198             if (nbPlafs.length > 0) {
199                 selectedPlaf = nbPlafs[0];
200             }
201         }
202         
203         public int getSize() {
204             return nbPlafs.length;
205         }
206         
207         public Object JavaDoc getElementAt(int index) {
208             return index < nbPlafs.length ? nbPlafs[index] : null;
209         }
210
211         public void setSelectedItem(Object JavaDoc plaf) {
212             assert plaf == null || plaf instanceof NbPlatform;
213             if (selectedPlaf != plaf) {
214                 selectedPlaf = plaf;
215                 fireContentsChanged(this, -1, -1);
216             }
217         }
218         
219         public Object JavaDoc getSelectedItem() {
220             return selectedPlaf;
221         }
222         
223         void removePlatform(NbPlatform plaf) {
224             try {
225                 NbPlatform.removePlatform(plaf);
226                 nbPlafs = getSortedPlatforms(); // refresh
227
fireContentsChanged(this, 0, nbPlafs.length - 1);
228             } catch (IOException JavaDoc e) {
229                 // tell the user that something goes wrong
230
ErrorManager.getDefault().notify(ErrorManager.USER, e);
231             }
232         }
233         
234         NbPlatform addPlatform(String JavaDoc id, String JavaDoc destdir, String JavaDoc label) {
235             try {
236                 NbPlatform def = NbPlatform.getDefaultPlatform();
237                 NbPlatform plaf = def != null ?
238                     NbPlatform.addPlatform(id, new File JavaDoc(destdir), /* #71629 */ def.getHarnessLocation(), label) :
239                     // Installation somehow corrupted, but try to behave gracefully:
240
NbPlatform.addPlatform(id, new File JavaDoc(destdir), label);
241                 nbPlafs = getSortedPlatforms(); // refresh
242
fireContentsChanged(this, 0, nbPlafs.length - 1);
243                 return plaf;
244             } catch (IOException JavaDoc e) {
245                 // tell the user that something goes wrong
246
ErrorManager.getDefault().notify(ErrorManager.USER, e);
247             }
248             return null;
249         }
250     }
251     
252     static class ModuleEntryListModel extends AbstractListModel JavaDoc {
253         
254         private ModuleEntry[] mes;
255         
256         ModuleEntryListModel(ModuleEntry[] mes) {
257             this.mes = mes;
258         }
259         
260         public int getSize() {
261             return mes.length;
262         }
263         
264         public Object JavaDoc getElementAt(int index) {
265             return mes[index].getLocalizedName();
266         }
267     }
268     
269     private static class SuiteListModel extends AbstractListModel JavaDoc
270             implements MutableComboBoxModel JavaDoc {
271         
272         private Set JavaDoc<String JavaDoc> suites = new TreeSet JavaDoc(Collator.getInstance());
273         private String JavaDoc selectedSuite;
274         
275         SuiteListModel(Set JavaDoc<String JavaDoc> suites) {
276             this.suites.addAll(suites);
277         }
278         
279         public void setSelectedItem(Object JavaDoc suite) {
280             if (suite == null) {
281                 return;
282             }
283             if (selectedSuite != suite) {
284                 selectedSuite = (String JavaDoc) suite;
285                 fireContentsChanged(this, -1, -1);
286             }
287         }
288         
289         public Object JavaDoc getSelectedItem() {
290             return selectedSuite;
291         }
292         
293         public int getSize() {
294             return suites.size();
295         }
296         
297         public Object JavaDoc getElementAt(int index) {
298             return suites.toArray()[index];
299         }
300         
301         public void addElement(Object JavaDoc obj) {
302             suites.add((String JavaDoc) obj);
303             fireIntervalAdded(this, 0, suites.size());
304         }
305         
306         /** Shouldn't be needed in the meantime. */
307         public void insertElementAt(Object JavaDoc obj, int index) {
308             assert false : "Who needs to insertElementAt?"; // NOI18N
309
}
310         
311         /** Shouldn't be needed in the meantime. */
312         public void removeElement(Object JavaDoc obj) {
313             assert false : "Who needs to removeElement?"; // NOI18N
314
}
315         
316         /** Shouldn't be needed in the meantime. */
317         public void removeElementAt(int index) {
318             assert false : "Who needs to call removeElementAt?"; // NOI18N
319
}
320     }
321     
322     /**
323      * <code>ListModel</code> capable to manage NetBeans platform source roots.
324      * <p>Can be used in conjuction with {@link URLListRenderer}</p>
325      */

326     static final class NbPlatformSourceRootsModel extends AbstractListModel JavaDoc {
327         
328         private NbPlatform plaf;
329         private URL JavaDoc[] srcRoots;
330         
331         NbPlatformSourceRootsModel(NbPlatform plaf) {
332             this.plaf = plaf;
333             this.srcRoots = plaf.getSourceRoots();
334         }
335         
336         public Object JavaDoc getElementAt(int index) {
337             return srcRoots[index];
338         }
339         
340         public int getSize() {
341             return srcRoots.length;
342         }
343         
344         void removeSourceRoot(URL JavaDoc[] srcRootToRemove) {
345             try {
346                 plaf.removeSourceRoots(srcRootToRemove);
347                 this.srcRoots = plaf.getSourceRoots(); // refresh
348
fireContentsChanged(this, 0, srcRootToRemove.length);
349             } catch (IOException JavaDoc e) {
350                 // tell the user that something goes wrong
351
ErrorManager.getDefault().notify(ErrorManager.USER, e);
352             }
353         }
354         
355         void addSourceRoot(URL JavaDoc srcRootToAdd) {
356             try {
357                 plaf.addSourceRoot(srcRootToAdd);
358                 this.srcRoots = plaf.getSourceRoots(); // refresh
359
fireContentsChanged(this, 0, srcRoots.length);
360             } catch (IOException JavaDoc e) {
361                 // tell the user that something goes wrong
362
ErrorManager.getDefault().notify(ErrorManager.USER, e);
363             }
364         }
365         
366         void moveSourceRootsDown(int[] toMoveDown) {
367             try {
368                 for (int i = 0; i < toMoveDown.length; i++) {
369                     plaf.moveSourceRootDown(toMoveDown[i]);
370                 }
371                 this.srcRoots = plaf.getSourceRoots(); // refresh
372
fireContentsChanged(this, 0, srcRoots.length);
373             } catch (IOException JavaDoc e) {
374                 // tell the user that something goes wrong
375
ErrorManager.getDefault().notify(ErrorManager.USER, e);
376             }
377         }
378         
379         void moveSourceRootsUp(int[] toMoveUp) {
380             try {
381                 for (int i = 0; i < toMoveUp.length; i++) {
382                     plaf.moveSourceRootUp(toMoveUp[i]);
383                 }
384                 this.srcRoots = plaf.getSourceRoots(); // refresh
385
fireContentsChanged(this, 0, srcRoots.length);
386             } catch (IOException JavaDoc e) {
387                 // tell the user that something goes wrong
388
ErrorManager.getDefault().notify(ErrorManager.USER, e);
389             }
390         }
391     }
392     
393     /**
394      * <code>ListModel</code> capable to manage NetBeans platform javadoc roots.
395      * <p>Can be used in conjuction with {@link URLListRenderer}</p>
396      */

397     static final class NbPlatformJavadocRootsModel extends AbstractListModel JavaDoc {
398         
399         private NbPlatform plaf;
400         private URL JavaDoc[] javadocRoots;
401         
402         NbPlatformJavadocRootsModel(NbPlatform plaf) {
403             this.plaf = plaf;
404             this.javadocRoots = plaf.getJavadocRoots();
405         }
406         
407         public Object JavaDoc getElementAt(int index) {
408             return javadocRoots[index];
409         }
410         
411         public int getSize() {
412             return javadocRoots.length;
413         }
414         
415         void removeJavadocRoots(URL JavaDoc[] jdRootToRemove) {
416             try {
417                 plaf.removeJavadocRoots(jdRootToRemove);
418                 this.javadocRoots = plaf.getJavadocRoots(); // refresh
419
fireContentsChanged(this, 0, javadocRoots.length);
420             } catch (IOException JavaDoc e) {
421                 // tell the user that something goes wrong
422
ErrorManager.getDefault().notify(ErrorManager.USER, e);
423             }
424         }
425         
426         void addJavadocRoot(URL JavaDoc jdRootToAdd) {
427             try {
428                 plaf.addJavadocRoot(jdRootToAdd);
429                 this.javadocRoots = plaf.getJavadocRoots(); // refresh
430
fireContentsChanged(this, 0, javadocRoots.length);
431             } catch (IOException JavaDoc e) {
432                 // tell the user that something goes wrong
433
ErrorManager.getDefault().notify(ErrorManager.USER, e);
434             }
435         }
436         
437         void moveJavadocRootsDown(int[] toMoveDown) {
438             try {
439                 for (int i = 0; i < toMoveDown.length; i++) {
440                     plaf.moveJavadocRootDown(toMoveDown[i]);
441                 }
442                 this.javadocRoots = plaf.getJavadocRoots(); // refresh
443
fireContentsChanged(this, 0, javadocRoots.length);
444             } catch (IOException JavaDoc e) {
445                 // tell the user that something goes wrong
446
ErrorManager.getDefault().notify(ErrorManager.USER, e);
447             }
448         }
449         
450         void moveJavadocRootsUp(int[] toMoveUp) {
451             try {
452                 for (int i = 0; i < toMoveUp.length; i++) {
453                     plaf.moveJavadocRootUp(toMoveUp[i]);
454                 }
455                 this.javadocRoots = plaf.getJavadocRoots(); // refresh
456
fireContentsChanged(this, 0, javadocRoots.length);
457             } catch (IOException JavaDoc e) {
458                 // tell the user that something goes wrong
459
ErrorManager.getDefault().notify(ErrorManager.USER, e);
460             }
461         }
462     }
463     
464     /**
465      * Render {@link java.net.URL} using {@link java.net.URL#getFile}.
466      * <p>Use in conjuction with {@link NbPlatformSourceRootsModel} and
467      * {@link NbPlatformJavadocRootsModel}</p>
468      */

469     static final class URLListRenderer extends DefaultListCellRenderer JavaDoc {
470         
471         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value,
472                 int index, boolean isSelected, boolean cellHasFocus) {
473             URL JavaDoc u = (URL JavaDoc) value;
474             String JavaDoc text = u.toExternalForm();
475             if (u.getProtocol().equals("file")) { // NOI18N
476
text = new File JavaDoc(URI.create(u.toExternalForm())).getAbsolutePath();
477             } else if (u.getProtocol().equals("jar")) { // NOI18N
478
URL JavaDoc baseU = FileUtil.getArchiveFile(u);
479                 if (u.equals(FileUtil.getArchiveRoot(baseU)) && baseU.getProtocol().equals("file")) { // NOI18N
480
text = new File JavaDoc(URI.create(baseU.toExternalForm())).getAbsolutePath();
481                 }
482             }
483             return super.getListCellRendererComponent(list, text, index, isSelected, cellHasFocus);
484         }
485     }
486     
487 }
488
Popular Tags