KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > libraries > ui > LibrariesCustomizer


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.project.libraries.ui;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.GridBagConstraints JavaDoc;
25 import java.awt.GridBagLayout JavaDoc;
26 import java.awt.Image JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.ActionListener JavaDoc;
29 import java.beans.Customizer JavaDoc;
30 import java.beans.PropertyChangeEvent JavaDoc;
31 import java.beans.PropertyChangeListener JavaDoc;
32 import java.beans.PropertyVetoException JavaDoc;
33 import java.beans.VetoableChangeListener JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.MissingResourceException JavaDoc;
38 import java.util.ResourceBundle JavaDoc;
39 import javax.swing.JComponent JavaDoc;
40 import javax.swing.JPanel JavaDoc;
41 import javax.swing.event.ListDataEvent JavaDoc;
42 import javax.swing.event.ListDataListener JavaDoc;
43 import org.netbeans.modules.project.libraries.LibraryTypeRegistry;
44 import org.netbeans.spi.project.libraries.LibraryImplementation;
45 import org.netbeans.spi.project.libraries.LibraryTypeProvider;
46 import org.openide.DialogDescriptor;
47 import org.openide.DialogDisplayer;
48 import org.openide.ErrorManager;
49 import org.openide.NotifyDescriptor;
50 import org.openide.explorer.ExplorerManager;
51 import org.openide.explorer.view.BeanTreeView;
52 import org.openide.filesystems.Repository;
53 import org.openide.loaders.DataFolder;
54 import org.openide.nodes.AbstractNode;
55 import org.openide.nodes.Children;
56 import org.openide.nodes.Node;
57 import org.openide.nodes.NodeNotFoundException;
58 import org.openide.nodes.NodeOp;
59 import org.openide.util.HelpCtx;
60 import org.openide.util.NbBundle;
61 import org.openide.util.lookup.Lookups;
62
63 /**
64  *
65  * @author tom
66  */

67 public final class LibrariesCustomizer extends JPanel JavaDoc implements ExplorerManager.Provider, HelpCtx.Provider {
68     
69     private ExplorerManager manager;
70     private LibrariesModel model;
71     private BeanTreeView libraries;
72
73     /** Creates new form LibrariesCustomizer */
74     public LibrariesCustomizer () {
75         this.model = new LibrariesModel ();
76         initComponents();
77         postInitComponents ();
78     }
79
80
81     public void setSelectedLibrary (LibraryImplementation library) {
82         if (library == null)
83             return;
84         ExplorerManager manager = this.getExplorerManager();
85         Node root = manager.getRootContext();
86         String JavaDoc[] path = new String JavaDoc[2];
87         path[0]=library.getType();
88         path[1]=library.getName();
89         try {
90             Node node = NodeOp.findPath(root, path);
91             if (node != null) {
92                 manager.setSelectedNodes(new Node[] {node});
93             }
94         } catch (NodeNotFoundException e) {
95             //Ignore it
96
}
97         catch (PropertyVetoException JavaDoc e) {
98             //Ignore it
99
}
100     }
101
102     public HelpCtx getHelpCtx() {
103         return new HelpCtx( LibrariesCustomizer.class );
104     }
105     
106     public boolean apply () {
107         try {
108             this.model.apply();
109             return true;
110         } catch (IOException JavaDoc ioe) {
111             ErrorManager.getDefault().notify(ioe);
112             return false;
113         }
114     }
115
116     public void cancel () {
117         this.model.cancel();
118     }
119
120     public void addNotify() {
121         super.addNotify();
122         expandAllNodes(this.libraries,this.getExplorerManager().getRootContext());
123         //Select first library if nothing selected
124
if (this.getExplorerManager().getSelectedNodes().length == 0) {
125         Node root = this.getExplorerManager().getRootContext();
126             Node[] nodes = root.getChildren().getNodes (true);
127             for (int i = 0; i< nodes.length; i++) {
128                 Node[] lnodes = nodes[i].getChildren().getNodes(true);
129                 if (lnodes.length > 0) {
130                     try {
131                         this.getExplorerManager().setSelectedNodes(new Node[] {lnodes[0]});
132                     } catch (PropertyVetoException JavaDoc e) {
133                         //Ignore it
134
}
135                     break;
136                 }
137             }
138         }
139         this.libraries.requestFocus();
140     }
141     
142     
143     public ExplorerManager getExplorerManager () {
144         if (this.manager == null) {
145             this.manager = new ExplorerManager ();
146             this.manager.addPropertyChangeListener (new PropertyChangeListener JavaDoc() {
147                 public void propertyChange (PropertyChangeEvent JavaDoc event) {
148                     if (ExplorerManager.PROP_SELECTED_NODES.equals(event.getPropertyName())) {
149                         Node[] nodes = (Node[]) event.getNewValue ();
150                         selectLibrary(nodes);
151                         libraries.requestFocus();
152                     }
153                 }
154             });
155             this.manager.addVetoableChangeListener(new VetoableChangeListener JavaDoc() {
156                 public void vetoableChange(PropertyChangeEvent JavaDoc event) throws PropertyVetoException JavaDoc {
157                     if (ExplorerManager.PROP_SELECTED_NODES.equals(event.getPropertyName())) {
158                         Node[] nodes = (Node[]) event.getNewValue();
159                         if (nodes.length <=1) {
160                             return;
161                         }
162                         else {
163                             throw new PropertyVetoException JavaDoc ("Invalid length", event); //NOI18N
164
}
165                     }
166                 }
167             });
168             this.manager.setRootContext (buildTree(this.model));
169         }
170         return this.manager;
171     }
172
173
174     private void postInitComponents () {
175         this.libraries = new LibrariesView ();
176         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc ();
177         c.gridx = GridBagConstraints.RELATIVE;
178         c.gridy = GridBagConstraints.RELATIVE;
179         c.gridwidth = GridBagConstraints.REMAINDER;
180         c.gridheight = GridBagConstraints.REMAINDER;
181         c.fill = GridBagConstraints.BOTH;
182         c.anchor = GridBagConstraints.NORTHWEST;
183         c.weightx = 1.0;
184         c.weighty = 1.0;
185         ((GridBagLayout JavaDoc)this.libsPanel.getLayout()).setConstraints(this.libraries,c);
186         this.libsPanel.add(this.libraries);
187         this.libraries.setPreferredSize(new Dimension JavaDoc (200,334));
188         this.libraryName.setColumns(25);
189         this.libraryName.setEnabled(false);
190         this.libraryName.addActionListener(
191                 new ActionListener JavaDoc () {
192                     public void actionPerformed(ActionEvent JavaDoc e) {
193                         nameChanged();
194                     }
195                 });
196     }
197
198
199     private void nameChanged () {
200         Node[] nodes = this.getExplorerManager().getSelectedNodes();
201         if (nodes.length == 1 && (nodes[0] instanceof LibraryNode)) {
202             LibraryNode node = (LibraryNode) nodes[0];
203             String JavaDoc newName = this.libraryName.getText();
204             if (newName.length () == 0) {
205                 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message (
206                         NbBundle.getMessage(LibrariesCustomizer.class, "ERR_InvalidName"),
207                         NotifyDescriptor.ERROR_MESSAGE));
208             }
209             else if (isValidName (this.model, newName)) {
210                 node.getLibrary().setName(newName);
211             }
212             else {
213                 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message (
214                         NbBundle.getMessage(LibrariesCustomizer.class, "ERR_ExistingName", newName),
215                         NotifyDescriptor.ERROR_MESSAGE));
216             }
217         }
218     }
219
220
221     private void selectLibrary (Node[] nodes) {
222         int tabCount = this.properties.getTabCount();
223         for (int i=0; i<tabCount; i++) {
224             this.properties.removeTabAt(0);
225         }
226         this.libraryName.setEnabled(false);
227         this.libraryName.setText(""); //NOI18N
228
this.jLabel1.setVisible(false);
229         this.libraryName.setVisible(false);
230         this.properties.setVisible(false);
231         this.deleteButton.setEnabled(false);
232         if (nodes.length != 1 || !(nodes[0] instanceof LibraryNode)) {
233             return;
234         }
235         this.jLabel1.setVisible(true);
236         this.libraryName.setVisible(true);
237         this.properties.setVisible(true);
238         LibraryNode lnode = (LibraryNode) nodes[0];
239         LibraryImplementation impl = lnode.getLibrary ();
240         boolean editable = model.isLibraryEditable (impl);
241         this.libraryName.setEnabled(editable);
242         this.deleteButton.setEnabled(editable);
243         this.libraryName.setText (getLocalizedString(impl.getLocalizingBundle(),impl.getName()));
244         String JavaDoc libraryType = impl.getType();
245         LibraryTypeProvider provider = lnode.getProvider ();
246         if (provider == null)
247             return;
248         String JavaDoc[] volumeTypes = provider.getSupportedVolumeTypes();
249         for (int i=0; i< volumeTypes.length; i++) {
250             Customizer JavaDoc c = provider.getCustomizer (volumeTypes[i]);
251             if (c instanceof JComponent JavaDoc) {
252                 c.setObject (impl);
253                 JComponent JavaDoc component = (JComponent JavaDoc) c;
254                 component.setEnabled (editable);
255                 String JavaDoc tabName = component.getName();
256                 if (tabName == null) {
257                     tabName = volumeTypes[i];
258                 }
259                 this.properties.addTab(tabName, component);
260             }
261         }
262     }
263
264     /** This method is called from within the constructor to
265      * initialize the form.
266      * WARNING: Do NOT modify this code. The content of this method is
267      * always regenerated by the Form Editor.
268      */

269     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
270
private void initComponents() {
271         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
272
273         jLabel1 = new javax.swing.JLabel JavaDoc();
274         libraryName = new javax.swing.JTextField JavaDoc();
275         jPanel1 = new javax.swing.JPanel JavaDoc();
276         properties = new javax.swing.JTabbedPane JavaDoc();
277         createButton = new javax.swing.JButton JavaDoc();
278         deleteButton = new javax.swing.JButton JavaDoc();
279         libsPanel = new javax.swing.JPanel JavaDoc();
280         jLabel2 = new javax.swing.JLabel JavaDoc();
281
282         setLayout(new java.awt.GridBagLayout JavaDoc());
283
284         getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/project/libraries/ui/Bundle").getString("AD_LibrariesCustomizer"));
285         jLabel1.setLabelFor(libraryName);
286         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, java.util.ResourceBundle.getBundle("org/netbeans/modules/project/libraries/ui/Bundle").getString("CTL_CustomizerLibraryName"));
287         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
288         gridBagConstraints.gridx = 2;
289         gridBagConstraints.gridy = 1;
290         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
291         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 12, 6);
292         add(jLabel1, gridBagConstraints);
293
294         libraryName.setEditable(false);
295         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
296         gridBagConstraints.gridx = 3;
297         gridBagConstraints.gridy = 1;
298         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
299         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
300         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
301         gridBagConstraints.weightx = 0.6;
302         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 12);
303         add(libraryName, gridBagConstraints);
304         libraryName.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/project/libraries/ui/Bundle").getString("AD_LibraryName"));
305
306         jPanel1.setLayout(new java.awt.BorderLayout JavaDoc());
307
308         properties.setPreferredSize(new java.awt.Dimension JavaDoc(400, 300));
309         jPanel1.add(properties, java.awt.BorderLayout.CENTER);
310         properties.getAccessibleContext().setAccessibleName(java.util.ResourceBundle.getBundle("org/netbeans/modules/project/libraries/ui/Bundle").getString("AN_LibrariesCustomizerProperties"));
311         properties.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/project/libraries/ui/Bundle").getString("AD_LibrariesCustomizerProperties"));
312
313         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
314         gridBagConstraints.gridx = 2;
315         gridBagConstraints.gridy = 2;
316         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
317         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
318         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
319         gridBagConstraints.weightx = 1.0;
320         gridBagConstraints.weighty = 1.0;
321         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 12, 12);
322         add(jPanel1, gridBagConstraints);
323
324         org.openide.awt.Mnemonics.setLocalizedText(createButton, java.util.ResourceBundle.getBundle("org/netbeans/modules/project/libraries/ui/Bundle").getString("CTL_NewLibrary"));
325         createButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
326             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
327                 createLibrary(evt);
328             }
329         });
330
331         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
332         gridBagConstraints.gridx = 0;
333         gridBagConstraints.gridy = 3;
334         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
335         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 0, 12);
336         add(createButton, gridBagConstraints);
337         createButton.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/project/libraries/ui/Bundle").getString("AD_NewLibrary"));
338
339         org.openide.awt.Mnemonics.setLocalizedText(deleteButton, java.util.ResourceBundle.getBundle("org/netbeans/modules/project/libraries/ui/Bundle").getString("CTL_DeleteLibrary"));
340         deleteButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
341             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
342                 deleteLibrary(evt);
343             }
344         });
345
346         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
347         gridBagConstraints.gridx = 1;
348         gridBagConstraints.gridy = 3;
349         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
350         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
351         add(deleteButton, gridBagConstraints);
352         deleteButton.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/project/libraries/ui/Bundle").getString("AD_DeleteLibrary"));
353
354         libsPanel.setLayout(new java.awt.GridBagLayout JavaDoc());
355
356         libsPanel.setBorder(new javax.swing.border.EtchedBorder JavaDoc());
357         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
358         gridBagConstraints.gridx = 0;
359         gridBagConstraints.gridy = 1;
360         gridBagConstraints.gridwidth = 2;
361         gridBagConstraints.gridheight = 2;
362         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
363         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
364         gridBagConstraints.weighty = 1.0;
365         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 12, 12, 6);
366         add(libsPanel, gridBagConstraints);
367         libsPanel.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/project/libraries/ui/Bundle").getString("AD_libsPanel"));
368
369         jLabel2.setLabelFor(libsPanel);
370         org.openide.awt.Mnemonics.setLocalizedText(jLabel2, java.util.ResourceBundle.getBundle("org/netbeans/modules/project/libraries/ui/Bundle").getString("TXT_LibrariesPanel"));
371         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
372         gridBagConstraints.gridx = 0;
373         gridBagConstraints.gridy = 0;
374         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
375         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
376         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
377         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 2, 12);
378         add(jLabel2, gridBagConstraints);
379
380     }
381     // </editor-fold>//GEN-END:initComponents
382

383     private void deleteLibrary(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_deleteLibrary
384
Node[] nodes = this.getExplorerManager().getSelectedNodes();
385         if (nodes.length == 1 && (nodes[0] instanceof LibraryNode)) {
386             Node[] sib = nodes[0].getParentNode().getChildren().getNodes(true);
387             Node selNode = null;
388             for (int i=0; i < sib.length; i++) {
389                 if (nodes[0].equals(sib[i])) {
390                     if (i>0) {
391                         selNode = sib[i-1];
392                     }
393                     else if (i<sib.length-1){
394                         selNode = sib[i+1];
395                     }
396                 }
397             }
398             model.removeLibrary (((LibraryNode)nodes[0]).getLibrary());
399             try {
400                 if (selNode != null) {
401                     this.getExplorerManager().setSelectedNodes(new Node[] {selNode});
402                 }
403             } catch (PropertyVetoException JavaDoc e) {
404                 //Ignore it
405
}
406             this.libraries.requestFocus();
407         }
408     }//GEN-LAST:event_deleteLibrary
409

410     private void createLibrary(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_createLibrary
411
Dialog JavaDoc dlg = null;
412         try {
413             String JavaDoc preselectedLibraryType = null;
414             Node[] preselectedNodes = this.getExplorerManager().getSelectedNodes();
415             if (preselectedNodes.length == 1) {
416                 LibraryCategory lc = preselectedNodes[0].getLookup().lookup(LibraryCategory.class);
417                 if (lc != null) {
418                     preselectedLibraryType = lc.getCategoryType();
419                 }
420             }
421             NewLibraryPanel p = new NewLibraryPanel (this.model, preselectedLibraryType);
422             DialogDescriptor dd = new DialogDescriptor (p, NbBundle.getMessage(LibrariesCustomizer.class,"CTL_CreateLibrary"),
423                     true, DialogDescriptor.OK_CANCEL_OPTION, null, null);
424             p.setDialogDescriptor(dd);
425             dlg = DialogDisplayer.getDefault().createDialog (dd);
426             dlg.setVisible(true);
427             if (dd.getValue() == DialogDescriptor.OK_OPTION) {
428                 String JavaDoc libraryType = p.getLibraryType();
429                 String JavaDoc libraryName = p.getLibraryName();
430                 LibraryTypeProvider provider = LibraryTypeRegistry.getDefault().getLibraryTypeProvider (libraryType);
431                 if (provider == null) {
432                     return;
433                 }
434                 LibraryImplementation impl = provider.createLibrary();
435                 impl.setName (libraryName);
436                 model.addLibrary (impl);
437                 String JavaDoc[] path = new String JavaDoc[2];
438                 path[0] = impl.getType();
439                 path[1] = impl.getName();
440                 ExplorerManager mgr = this.getExplorerManager();
441                 try {
442                     Node node = NodeOp.findPath(mgr.getRootContext(),path);
443                     if (node != null) {
444                         mgr.setSelectedNodes(new Node[] {node});
445                     }
446                 } catch (PropertyVetoException JavaDoc e) {
447                     //Ignore it
448
}
449                 catch (NodeNotFoundException e) {
450                     //Ignore it
451
}
452                 this.libraryName.requestFocus();
453                 this.libraryName.selectAll();
454             }
455             else {
456                 this.libraries.requestFocus();
457             }
458         }
459         finally {
460             if (dlg != null)
461                 dlg.dispose();
462         }
463     }//GEN-LAST:event_createLibrary
464

465
466     static boolean isValidName (LibrariesModel model, String JavaDoc name) {
467         int count = model.getSize();
468         for (int i=0; i<count; i++) {
469             LibraryImplementation lib = (LibraryImplementation) model.getElementAt (i);
470             if (lib != null && lib.getName().equals(name))
471                 return false;
472         }
473         return true;
474     }
475
476
477     static String JavaDoc getLocalizedString (String JavaDoc bundleResourceName, String JavaDoc key) {
478         if (key == null) {
479             return null;
480         }
481         if (bundleResourceName == null) {
482             return key;
483         }
484         ResourceBundle JavaDoc bundle;
485         try {
486             bundle = NbBundle.getBundle(bundleResourceName);
487         } catch (MissingResourceException JavaDoc mre) {
488             // Bundle should have existed.
489
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, mre);
490             return key;
491         }
492         try {
493             return bundle.getString (key);
494         } catch (MissingResourceException JavaDoc mre) {
495             // No problem, not specified.
496
return key;
497         }
498     }
499
500
501     // Variables declaration - do not modify//GEN-BEGIN:variables
502
private javax.swing.JButton JavaDoc createButton;
503     private javax.swing.JButton JavaDoc deleteButton;
504     private javax.swing.JLabel JavaDoc jLabel1;
505     private javax.swing.JLabel JavaDoc jLabel2;
506     private javax.swing.JPanel JavaDoc jPanel1;
507     private javax.swing.JTextField JavaDoc libraryName;
508     private javax.swing.JPanel JavaDoc libsPanel;
509     private javax.swing.JTabbedPane JavaDoc properties;
510     // End of variables declaration//GEN-END:variables
511

512
513     private static void expandAllNodes (BeanTreeView btv, Node node) {
514         btv.expandNode (node);
515         Children ch = node.getChildren();
516         if ( ch == Children.LEAF ) {
517             return;
518         }
519         Node nodes[] = ch.getNodes( true );
520         for ( int i = 0; i < nodes.length; i++ ) {
521             expandAllNodes( btv, nodes[i]);
522         }
523
524     }
525     
526     private static class LibrariesView extends BeanTreeView {
527         
528         public LibrariesView () {
529             super ();
530             this.setRootVisible(false);
531             this.setPopupAllowed(false);
532             this.setDefaultActionAllowed(false);
533             this.tree.setEditable (false);
534             this.tree.setShowsRootHandles (false);
535         }
536         
537     }
538     
539     
540     private static class RootChildren extends Children.Keys<LibraryTypeProvider> {
541         
542         private LibrariesModel model;
543         
544         public RootChildren (LibrariesModel model) {
545             this.model = model;
546         }
547         
548         public void addNotify () {
549             this.setKeys(LibraryTypeRegistry.getDefault().getLibraryTypeProviders());
550         }
551         
552         public void removeNotify () {
553             this.setKeys(new LibraryTypeProvider[0]);
554         }
555         
556         protected Node[] createNodes(LibraryTypeProvider provider) {
557             return new Node[] {new CategoryNode(provider, model)};
558         }
559         
560     }
561     
562     
563     private static final class LibraryCategory {
564         
565         private final String JavaDoc name;
566         
567         LibraryCategory (String JavaDoc name) {
568             this.name = name;
569         }
570         
571         public String JavaDoc getCategoryType () {
572             return this.name;
573         }
574         
575     }
576     
577     private static class CategoryNode extends AbstractNode {
578                 
579         
580         private LibraryTypeProvider provider;
581         private Node iconDelegate;
582                 
583         public CategoryNode (LibraryTypeProvider provider, LibrariesModel model) {
584             super (new CategoryChildren(provider, model), Lookups.singleton(new LibraryCategory (provider.getLibraryType())));
585             this.provider = provider;
586             this.iconDelegate = DataFolder.findFolder (Repository.getDefault().getDefaultFileSystem().getRoot()).getNodeDelegate();
587         }
588         
589         public String JavaDoc getName () {
590             return provider.getLibraryType ();
591         }
592         
593         public String JavaDoc getDisplayName() {
594             return this.provider.getDisplayName();
595         }
596         
597         public Image JavaDoc getIcon(int type) {
598             return this.iconDelegate.getIcon (type);
599         }
600         
601         public Image JavaDoc getOpenedIcon(int type) {
602             return this.iconDelegate.getOpenedIcon (type);
603         }
604                         
605     }
606     
607     private static class CategoryChildren extends Children.Keys<LibraryImplementation> implements ListDataListener JavaDoc {
608         
609         private LibraryTypeProvider provider;
610         private LibrariesModel model;
611         
612         public CategoryChildren (LibraryTypeProvider provider, LibrariesModel model) {
613             this.provider = provider;
614             this.model = model;
615             this.model.addListDataListener(this);
616         }
617         
618         public void addNotify () {
619             Collection JavaDoc<LibraryImplementation> keys = new ArrayList JavaDoc<LibraryImplementation>();
620             for (int i=0; i<model.getSize(); i++) {
621                 LibraryImplementation impl = (LibraryImplementation) model.getElementAt(i);
622                 if (this.provider.getLibraryType().equals(impl.getType())) {
623                     keys.add (impl);
624                 }
625             }
626             this.setKeys(keys);
627         }
628         
629         public void removeNotify () {
630             this.setKeys(new LibraryImplementation[0]);
631         }
632         
633         protected Node[] createNodes(LibraryImplementation impl) {
634             return new Node[] {new LibraryNode(impl, provider)};
635         }
636         
637         public void contentsChanged(ListDataEvent JavaDoc e) {
638             //Todo: Optimize it
639
this.addNotify();
640         }
641         
642         public void intervalAdded(ListDataEvent JavaDoc e) {
643             //Todo: Optimize it
644
this.addNotify();
645         }
646         
647         public void intervalRemoved(ListDataEvent JavaDoc e) {
648             //Todo: Optimize it
649
this.addNotify();
650         }
651         
652     }
653     
654     private static class LibraryNode extends AbstractNode {
655         
656         private static final String JavaDoc ICON = "org/netbeans/modules/project/libraries/resources/libraries.gif"; //NOI18N
657

658         private LibraryImplementation lib;
659         private LibraryTypeProvider provider;
660         
661         public LibraryNode (LibraryImplementation lib, LibraryTypeProvider provider) {
662             super (Children.LEAF);
663             this.lib = lib;
664             this.provider = provider;
665             this.setIconBaseWithExtension(ICON);
666         }
667         
668         public String JavaDoc getName () {
669             return this.lib.getName ();
670         }
671         
672         public String JavaDoc getDisplayName () {
673             return getLocalizedString(this.lib.getLocalizingBundle(), this.lib.getName());
674         }
675         
676         public LibraryImplementation getLibrary () {
677             return this.lib;
678         }
679         
680         public LibraryTypeProvider getProvider () {
681             return this.provider;
682         }
683         
684         public boolean equals (Object JavaDoc other) {
685             if (other instanceof LibraryNode) {
686                 LibraryNode ol = (LibraryNode) other;
687                 return (this.lib == null ? ol.lib == null : this.lib.equals(ol.lib))
688                     && (this.provider == null ? ol.provider == null : this.provider.equals(ol.provider));
689             }
690             return false;
691         }
692     }
693     
694     private static Node buildTree (LibrariesModel model) {
695         return new AbstractNode (new RootChildren (model));
696     }
697     
698     
699 }
700
Popular Tags