KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > TemplateChooserPanelGUI


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.ui;
21
22 import java.awt.*;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.List JavaDoc;
31 import javax.swing.DefaultComboBoxModel JavaDoc;
32 import javax.swing.JLabel JavaDoc;
33 import javax.swing.JList JavaDoc;
34 import javax.swing.ListCellRenderer JavaDoc;
35 import javax.swing.event.ChangeEvent JavaDoc;
36 import javax.swing.event.ChangeListener JavaDoc;
37 import javax.swing.plaf.UIResource JavaDoc;
38 import org.netbeans.api.project.Project;
39 import org.netbeans.api.project.ProjectUtils;
40 import org.netbeans.api.project.ProjectInformation;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.Repository;
43 import org.openide.loaders.DataFolder;
44 import org.openide.loaders.DataObject;
45 import org.openide.loaders.DataShadow;
46 import org.openide.nodes.Children;
47 import org.openide.nodes.FilterNode;
48 import org.openide.nodes.Node;
49 import org.openide.util.AsyncGUIJob;
50 import org.openide.util.NbBundle;
51 import org.openide.util.Utilities;
52
53 /** If you are looking for the non-GUI part of the panel please look
54  * into new file wizard
55  */

56
57 /**
58  * Provides the GUI for the template chooser panel.
59  * @author Jesse Glick
60  */

61 final class TemplateChooserPanelGUI extends javax.swing.JPanel JavaDoc implements PropertyChangeListener JavaDoc, AsyncGUIJob {
62     
63     /** prefered dimmension of the panels */
64     private static final java.awt.Dimension JavaDoc PREF_DIM = new java.awt.Dimension JavaDoc (500, 340);
65     
66     // private final String[] recommendedTypes = null;
67
private final List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
68
69     //Templates folder root
70
private FileObject templatesFolder;
71
72     //GUI Builder
73
private TemplatesPanelGUI.Builder builder;
74     private Project project;
75     private String JavaDoc category;
76     private String JavaDoc template;
77     private boolean isWarmUp = true;
78     private ListCellRenderer JavaDoc projectCellRenderer;
79     private boolean firstTime = true;
80     
81     public TemplateChooserPanelGUI() {
82         this.builder = new FileChooserBuilder ();
83         initComponents();
84         setPreferredSize( PREF_DIM );
85         setName (org.openide.util.NbBundle.getMessage(TemplateChooserPanelGUI.class, "LBL_TemplateChooserPanelGUI_Name")); // NOI18N
86
projectCellRenderer = new ProjectCellRenderer ();
87         projectsComboBox.setRenderer (projectCellRenderer);
88      }
89     
90     public void readValues (Project p, String JavaDoc category, String JavaDoc template) {
91         assert p != null : "Project can not be null"; //NOI18N
92
boolean wf;
93         synchronized (this) {
94             this.project = p;
95             this.category = category;
96             this.template = template;
97             wf = this.isWarmUp;
98         }
99         if (!wf) {
100             this.selectProject ( project );
101             ((TemplatesPanelGUI)this.templatesPanel).setSelectedCategoryByName (this.category);
102             ((TemplatesPanelGUI)this.templatesPanel).setSelectedTemplateByName (this.template);
103         }
104     }
105
106     /** Called from readSettings, to initialize the GUI with proper components
107      */

108     private void initValues( Project p ) {
109         // Populate the combo box with list of projects
110
Project openProjects[] = OpenProjectList.getDefault().getOpenProjects();
111         Arrays.sort( openProjects, OpenProjectList.PROJECT_BY_DISPLAYNAME );
112         DefaultComboBoxModel JavaDoc projectsModel = new DefaultComboBoxModel JavaDoc( openProjects );
113         projectsComboBox.setModel( projectsModel );
114         this.selectProject (p);
115     }
116
117     private void selectProject (Project p) {
118         if (p != null) {
119             DefaultComboBoxModel JavaDoc projectsModel = (DefaultComboBoxModel JavaDoc) projectsComboBox.getModel ();
120             if ( projectsModel.getIndexOf( p ) == -1 ) {
121                 projectsModel.insertElementAt( p, 0 );
122             }
123             projectsComboBox.setSelectedItem( p );
124         }
125     }
126
127
128     public synchronized void addChangeListener(ChangeListener JavaDoc l) {
129         listeners.add(l);
130     }
131     
132     public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
133         listeners.remove(l);
134     }
135     
136     private void fireChange() {
137         ChangeEvent JavaDoc e = new ChangeEvent JavaDoc(this);
138         List JavaDoc<ChangeListener JavaDoc> templist;
139         synchronized (this) {
140             templist = new ArrayList JavaDoc<ChangeListener JavaDoc> (listeners);
141         }
142     for (ChangeListener JavaDoc l: templist) {
143             l.stateChanged(e);
144         }
145     }
146     
147     
148     public Project getProject() {
149         boolean wf;
150         synchronized (this) {
151             wf = isWarmUp;
152         }
153         if (wf) {
154             return this.project;
155         }
156         else {
157             return (Project)projectsComboBox.getSelectedItem();
158         }
159     }
160     
161     public FileObject getTemplate() {
162         return ((TemplatesPanelGUI)this.templatesPanel).getSelectedTemplate ();
163     }
164     
165     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
166         fireChange();
167     }
168     
169     
170     public java.awt.Dimension JavaDoc getPreferredSize() {
171         return PREF_DIM;
172     }
173     
174     public String JavaDoc getCategoryName () {
175         return ((TemplatesPanelGUI)this.templatesPanel).getSelectedCategoryName ();
176     }
177
178     public String JavaDoc getTemplateName () {
179         return ((TemplatesPanelGUI)this.templatesPanel).getSelectedTemplateName ();
180     }
181
182     public void setCategory (String JavaDoc category) {
183         ((TemplatesPanelGUI)this.templatesPanel).setSelectedCategoryByName (category);
184     }
185     
186     public void addNotify () {
187         if (firstTime) {
188             //77244 prevent multiple initializations..
189
Utilities.attachInitJob (this, this);
190             firstTime = false;
191         }
192         super.addNotify ();
193     }
194     
195     /** This method is called from within the constructor to
196      * initialize the form.
197      * WARNING: Do NOT modify this code. The content of this method is
198      * always regenerated by the Form Editor.
199      */

200     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
201
private void initComponents() {
202         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
203
204         jLabel1 = new javax.swing.JLabel JavaDoc();
205         projectsComboBox = new javax.swing.JComboBox JavaDoc();
206         templatesPanel = new TemplatesPanelGUI (this.builder);
207
208         setLayout(new java.awt.GridBagLayout JavaDoc());
209
210         jLabel1.setLabelFor(projectsComboBox);
211         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(TemplateChooserPanelGUI.class, "LBL_TemplateChooserPanelGUI_jLabel1")); // NOI18N
212
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
213         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 13, 0);
214         add(jLabel1, gridBagConstraints);
215         jLabel1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(TemplateChooserPanelGUI.class, "ACSN_jLabel1")); // NOI18N
216
jLabel1.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(TemplateChooserPanelGUI.class, "ACSD_jLabel1")); // NOI18N
217

218         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
219         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
220         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
221         gridBagConstraints.weightx = 1.0;
222         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 12, 0);
223         add(projectsComboBox, gridBagConstraints);
224         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
225         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
226         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
227         gridBagConstraints.weightx = 1.0;
228         gridBagConstraints.weighty = 1.0;
229         add(templatesPanel, gridBagConstraints);
230     }// </editor-fold>//GEN-END:initComponents
231

232     
233     // Variables declaration - do not modify//GEN-BEGIN:variables
234
private javax.swing.JLabel JavaDoc jLabel1;
235     private javax.swing.JComboBox JavaDoc projectsComboBox;
236     private javax.swing.JPanel JavaDoc templatesPanel;
237     // End of variables declaration//GEN-END:variables
238

239     // private static final Comparator NATURAL_NAME_SORT = Collator.getInstance();
240

241     private final class TemplateChildren extends Children.Keys<DataFolder> implements ActionListener JavaDoc {
242         
243         private final DataFolder folder;
244         
245         TemplateChildren(DataFolder folder) {
246             this.folder = folder;
247         }
248         
249         protected void addNotify() {
250             super.addNotify();
251             projectsComboBox.addActionListener( this );
252             updateKeys();
253         }
254         
255         protected void removeNotify() {
256             setKeys(Collections.<DataFolder>emptySet());
257             projectsComboBox.removeActionListener( this );
258             super.removeNotify();
259         }
260         
261         private void updateKeys() {
262             List JavaDoc<DataFolder> l = new ArrayList JavaDoc<DataFolder>();
263             for (DataObject d : folder.getChildren()) {
264                 FileObject prim = d.getPrimaryFile();
265                 if ( acceptTemplate( d, prim ) ) {
266                     // has children?
267
if (hasChildren ((Project)projectsComboBox.getSelectedItem (), d)) {
268                         l.add((DataFolder) d);
269                     }
270                 }
271             }
272             setKeys(l);
273         }
274         
275         protected Node[] createNodes(DataFolder d) {
276             boolean haveChildren = false;
277             for (DataObject child : d.getChildren()) {
278                 if ((child instanceof DataFolder) && !isTemplate(child)) {
279                     haveChildren = true;
280                     break;
281                 }
282             }
283             if (!haveChildren) {
284                 return new Node[] {new FilterNode(d.getNodeDelegate(), Children.LEAF )};
285             } else {
286                 return new Node[] {new FilterNode(d.getNodeDelegate(), new TemplateChildren(d))};
287             }
288         }
289         
290         public void actionPerformed (ActionEvent JavaDoc event) {
291             final String JavaDoc cat = getCategoryName ();
292             String JavaDoc template = ((TemplatesPanelGUI)TemplateChooserPanelGUI.this.templatesPanel).getSelectedTemplateName();
293             this.setKeys(Collections.<DataFolder>emptySet());
294             this.updateKeys ();
295             setCategory (cat);
296             ((TemplatesPanelGUI)TemplateChooserPanelGUI.this.templatesPanel).setSelectedTemplateByName(template);
297         }
298                 
299         
300         /** Uncoment if you want to have the templates sorted alphabeticaly
301          
302         // Comparator ----------------------------------------------------------
303          
304         public int compare(Object o1, Object o2) {
305             DataObject d1 = (DataObject)o1;
306             DataObject d2 = (DataObject)o2;
307             if ((d1 instanceof DataFolder) && !(d2 instanceof DataFolder)) {
308                 return 1;
309             } else if (!(d1 instanceof DataFolder) && (d2 instanceof DataFolder)) {
310                 return -1;
311             } else {
312                 return NATURAL_NAME_SORT.compare(d1.getNodeDelegate().getDisplayName(), d2.getNodeDelegate().getDisplayName());
313             }
314         }
315         */

316
317         // Private methods -----------------------------------------------------
318

319         private boolean acceptTemplate( DataObject d, FileObject primaryFile ) {
320             if (d instanceof DataFolder && !isTemplate((DataFolder)d)) {
321                 Object JavaDoc o = primaryFile.getAttribute ("simple"); // NOI18N
322
return o == null || Boolean.TRUE.equals (o);
323             }
324             return false;
325         }
326         
327     }
328     
329     
330     private final class FileChildren extends Children.Keys<DataObject> {
331         
332         private DataFolder root;
333                 
334         public FileChildren (DataFolder folder) {
335             this.root = folder;
336             assert this.root != null : "Root can not be null"; //NOI18N
337
}
338         
339         protected void addNotify () {
340             this.setKeys (this.root.getChildren());
341         }
342         
343         protected void removeNotify () {
344             this.setKeys (new DataObject[0]);
345         }
346         
347         protected Node[] createNodes(DataObject dobj) {
348             if (isTemplate(dobj) && OpenProjectList.isRecommended(getProject(), dobj.getPrimaryFile())) {
349                 if (dobj instanceof DataShadow) {
350                     dobj = ((DataShadow)dobj).getOriginal();
351                 }
352                 return new Node[] { new FilterNode(dobj.getNodeDelegate(), Children.LEAF) };
353             } else {
354                 return new Node[0];
355             }
356         }
357         
358     }
359     
360   
361     private final class FileChooserBuilder implements TemplatesPanelGUI.Builder {
362         
363         public Children createCategoriesChildren(DataFolder folder) {
364             return new TemplateChildren (folder);
365         }
366         
367         public Children createTemplatesChildren(DataFolder folder) {
368             return new FileChildren (folder);
369         }
370         
371         public void fireChange() {
372             TemplateChooserPanelGUI.this.fireChange();
373         }
374         
375         public String JavaDoc getCategoriesName() {
376             return NbBundle.getMessage (TemplateChooserPanelGUI.class,"CTL_Categories");
377         }
378         
379         public String JavaDoc getTemplatesName() {
380             return NbBundle.getMessage (TemplateChooserPanelGUI.class,"CTL_Files");
381         }
382         
383     }
384     
385     // #89393: GTK needs cell renderer to implement UIResource to look "natively"
386
private static class ProjectCellRenderer extends JLabel JavaDoc implements ListCellRenderer JavaDoc, UIResource JavaDoc {
387         
388         
389         public ProjectCellRenderer() {
390             setOpaque(true);
391         }
392         
393         
394         public Component getListCellRendererComponent(
395             JList JavaDoc list,
396             Object JavaDoc value,
397             int index,
398             boolean isSelected,
399             boolean cellHasFocus) {
400             
401             // #89393: GTK needs name to render cell renderer "natively"
402
setName("ComboBox.listRenderer"); // NOI18N
403

404             if ( value instanceof Project ) {
405                 ProjectInformation pi = ProjectUtils.getInformation((Project)value);
406                 setText(pi.getDisplayName());
407                 setIcon(pi.getIcon());
408             }
409             else {
410                 setText( value == null ? "" : value.toString () ); // NOI18N
411
setIcon( null );
412             }
413             if ( isSelected ) {
414                 setBackground(list.getSelectionBackground());
415                 setForeground(list.getSelectionForeground());
416             }
417             else {
418                 setBackground(list.getBackground());
419                 setForeground(list.getForeground());
420             }
421             
422             return this;
423         }
424
425         // #89393: GTK needs name to render cell renderer "natively"
426
public String JavaDoc getName() {
427             String JavaDoc name = super.getName();
428             return name == null ? "ComboBox.renderer" : name; // NOI18N
429
}
430         
431     }
432     
433     
434     private static boolean isTemplate (DataObject dobj) {
435         if (dobj.isTemplate())
436             return true;
437         if (dobj instanceof DataShadow) {
438             return ((DataShadow)dobj).getOriginal().isTemplate();
439         }
440         return false;
441     }
442     
443     private boolean hasChildren (Project p, DataObject folder) {
444         if (!(folder instanceof DataFolder)) {
445             return false;
446         }
447         
448         DataFolder f = (DataFolder) folder;
449         DataObject[] ch = f.getChildren ();
450         boolean ok = false;
451         for (int i = 0; i < ch.length; i++) {
452             if (isTemplate (ch[i]) && OpenProjectList.isRecommended(p, ch[i].getPrimaryFile ())) {
453                 // XXX: how to filter link to Package template in each java types folder?
454
if (!(ch[i] instanceof DataShadow)) {
455                     ok = true;
456                     break;
457                 }
458             } else if (ch[i] instanceof DataFolder && hasChildren (p, ch[i])) {
459                     ok = true;
460                     break;
461             }
462         }
463         return ok;
464         
465         // simplied but more counts
466
//return new FileChildren (p, (DataFolder) folder).getNodesCount () > 0;
467

468     }
469     
470     public void construct () {
471         this.templatesFolder = Repository.getDefault().getDefaultFileSystem().findResource("Templates");
472         ((TemplatesPanelGUI)this.templatesPanel).warmUp(this.templatesFolder);
473     }
474     
475     public void finished () {
476         //In the awt
477
Cursor cursor = null;
478         try {
479             Project p;
480             String JavaDoc c,t;
481             synchronized (this) {
482                 p = this.project;
483                 c = this.category;
484                 t = this.template;
485             }
486             cursor = TemplateChooserPanelGUI.this.getCursor();
487             TemplateChooserPanelGUI.this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
488             initValues( p );
489             ((TemplatesPanelGUI)this.templatesPanel).doFinished (this.templatesFolder, c, t);
490         } finally {
491             synchronized (this) {
492                 isWarmUp = false;
493             }
494             if (cursor != null) {
495                 this.setCursor (cursor);
496             }
497         }
498     }
499     
500 }
501
Popular Tags