KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import javax.swing.event.ChangeEvent JavaDoc;
27 import javax.swing.event.ChangeListener JavaDoc;
28
29 import org.openide.ErrorManager;
30 import org.openide.WizardDescriptor;
31 import org.openide.filesystems.FileObject;
32 import org.openide.loaders.DataFolder;
33 import org.openide.loaders.DataObject;
34 import org.openide.loaders.DataObjectNotFoundException;
35 import org.openide.loaders.TemplateWizard;
36 import org.openide.nodes.Children;
37 import org.openide.nodes.FilterNode;
38 import org.openide.nodes.Node;
39 import org.openide.util.*;
40
41 /**
42  *
43  * @author tom
44  */

45 public class TemplatesPanel implements WizardDescriptor.Panel {
46     
47     private ArrayList JavaDoc<ChangeListener JavaDoc> listeners;
48     private TemplatesPanelGUI panel;
49     private WarmupJob warmUp;
50     private boolean warmUpActive;
51     private boolean needsReselect = false; // WelcomeScreen hack, XXX Delete after WS is redesigned
52

53     /** Creates a new instance of TemplatesPanel */
54     public TemplatesPanel() {
55     }
56     
57     public void readSettings (Object JavaDoc settings) {
58         TemplateWizard wd = (TemplateWizard) settings;
59         wd.putProperty ("WizardPanel_contentSelectedIndex", new Integer JavaDoc (0)); // NOI18N
60
wd.putProperty ("WizardPanel_contentData", new String JavaDoc[] { // NOI18N
61
NbBundle.getBundle (TemplatesPanel.class).getString ("LBL_TemplatesPanel_Name"), // NOI18N
62
NbBundle.getBundle (TemplatesPanel.class).getString ("LBL_TemplatesPanel_Dots")}); // NOI18N
63
FileObject templatesFolder = (FileObject) wd.getProperty (TemplatesPanelGUI.TEMPLATES_FOLDER);
64         
65         // WelcomeScreen hack, XXX Delete after WS is redesigned
66
String JavaDoc preselectedCategory = (String JavaDoc)wd.getProperty( "PRESELECT_CATEGORY" );
67         if ( templatesFolder != null && templatesFolder.isFolder() &&
68             ( wd.getTemplate() == null || preselectedCategory != null || needsReselect ) ) {
69             
70             String JavaDoc preselectedTemplate = (String JavaDoc)wd.getProperty( "PRESELECT_TEMPLATE" );
71             String JavaDoc template;
72             String JavaDoc selectedCategory = OpenProjectListSettings.getInstance().getLastSelectedProjectCategory ();
73             String JavaDoc selectedTemplate = OpenProjectListSettings.getInstance().getLastSelectedProjectType ();
74
75             if (preselectedTemplate == null) {
76                 template = preselectedCategory != null ? null : selectedTemplate;
77             } else {
78                 template = preselectedCategory != null ? preselectedTemplate : selectedTemplate;
79             }
80             
81             TemplatesPanelGUI p = (TemplatesPanelGUI) this.getComponent();
82             if (isWarmUpActive()) {
83                 WarmupJob wup = getWarmUp();
84                 wup.setTemplatesFolder (templatesFolder);
85                 wup.setSelectedCategory( preselectedCategory != null ? preselectedCategory : selectedCategory );
86                 wup.setSelectedTemplate( template );
87             }
88             else {
89                 p.setTemplatesFolder(templatesFolder);
90                 p.setSelectedCategoryByName (preselectedCategory != null ? preselectedCategory : selectedCategory);
91                 p.setSelectedTemplateByName (template);
92             }
93
94         }
95         // bugfix #44792: project wizard title always changes
96
((WizardDescriptor)settings).putProperty ("NewProjectWizard_Title", null); // NOI18N
97
}
98     
99     public void storeSettings (Object JavaDoc settings) {
100         TemplateWizard wd = (TemplateWizard) settings;
101         
102         // WelcomeScreen hack, XXX Delete after WS is redesigned
103
String JavaDoc preselectedCategory = (String JavaDoc)wd.getProperty( "PRESELECT_CATEGORY" );
104
105         TemplatesPanelGUI gui = (TemplatesPanelGUI)this.getComponent();
106         FileObject fo = gui.getSelectedTemplate();
107         if (fo != null) {
108             try {
109                 wd.setTemplate (DataObject.find(fo));
110             } catch (DataObjectNotFoundException e) {
111                 ErrorManager.getDefault().notify(e);
112             }
113         }
114         if ( preselectedCategory == null ) {
115
116             String JavaDoc path = gui.getSelectedCategoryName();
117             if (path != null) {
118                 OpenProjectListSettings.getInstance().setLastSelectedProjectCategory(path);
119             }
120             path = gui.getSelectedTemplateName();
121             if (path != null) {
122                 OpenProjectListSettings.getInstance().setLastSelectedProjectType (path);
123             }
124             needsReselect = false;
125         }
126         else {
127             needsReselect = true;
128         }
129     }
130     
131     public synchronized void addChangeListener(ChangeListener JavaDoc l) {
132         if (this.listeners == null) {
133             this.listeners = new ArrayList JavaDoc<ChangeListener JavaDoc> ();
134         }
135         this.listeners.add (l);
136     }
137     
138     public synchronized void removeChangeListener(ChangeListener JavaDoc l) {
139         if (this.listeners == null) {
140             return;
141         }
142         this.listeners.remove (l);
143     }
144     
145     public boolean isValid() {
146         return ((TemplatesPanelGUI)this.getComponent()).getSelectedTemplate() != null;
147     }
148     
149     public HelpCtx getHelp() {
150         return new HelpCtx( TemplatesPanel.class );
151     }
152     
153     public synchronized Component getComponent() {
154         if (this.panel == null) {
155             TemplatesPanelGUI.Builder firer = new Builder();
156             this.panel = new TemplatesPanelGUI (firer);
157             Utilities.attachInitJob (panel, getWarmUp());
158             this.warmUpActive = true;
159             this.panel.setName (NbBundle.getBundle (TemplatesPanel.class).getString ("LBL_TemplatesPanel_Name")); // NOI18N
160
}
161         return this.panel;
162     }
163
164
165     private synchronized WarmupJob getWarmUp () {
166         if (this.warmUp == null) {
167             this.warmUp = new WarmupJob();
168         }
169         return this.warmUp;
170     }
171
172     private synchronized boolean isWarmUpActive () {
173         return warmUpActive;
174     }
175
176     private static class CategoriesChildren extends Children.Keys<DataObject> {
177         
178         private DataFolder root;
179                 
180         public CategoriesChildren (DataFolder folder) {
181             this.root = folder;
182         }
183         
184         protected void addNotify () {
185             setKeys(root.getChildren());
186         }
187         
188         protected void removeNotify () {
189             this.setKeys(new DataObject[0]);
190         }
191         
192         protected Node[] createNodes(DataObject dobj) {
193             if (dobj instanceof DataFolder) {
194                 DataFolder folder = (DataFolder) dobj;
195                 int type = 0; //Empty folder or File folder
196
for (DataObject child : folder.getChildren()) {
197                     type = 1;
198                     if (child.getPrimaryFile().isFolder()) {
199                         type = 2; //Folder folder
200
break;
201                     }
202                 }
203                 if (type == 1) {
204                     return new Node[] {
205                         new FilterNode(dobj.getNodeDelegate(), Children.LEAF)
206                     };
207                 } else if (type == 2) {
208                     return new Node[] {
209                         new FilterNode(dobj.getNodeDelegate(), new CategoriesChildren((DataFolder)dobj))
210                     };
211                 }
212             }
213             return new Node[0];
214         }
215     }
216     
217     private static class TemplateChildren extends Children.Keys<DataObject> {
218         
219         private DataFolder folder;
220                 
221         public TemplateChildren (DataFolder folder) {
222             this.folder = folder;
223         }
224         
225         protected void addNotify () {
226             this.setKeys (this.folder.getChildren ());
227         }
228         
229         protected void removeNotify () {
230             this.setKeys(new DataObject[0]);
231         }
232         
233         protected Node[] createNodes(DataObject dobj) {
234             if (dobj.isTemplate()) {
235                 return new Node[] {
236                     new FilterNode(dobj.getNodeDelegate(), Children.LEAF)
237                 };
238             } else {
239                 return new Node[0];
240             }
241         }
242         
243     }
244     
245     private class WarmupJob implements AsyncGUIJob {
246
247         private FileObject templatesFolder;
248         private String JavaDoc category;
249         private String JavaDoc template;
250
251         public void construct () {
252             panel.warmUp (this.templatesFolder);
253         }
254         
255         public void finished () {
256             Cursor cursor = null;
257             try {
258                 cursor = panel.getCursor();
259                 panel.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
260                 panel.doFinished (this.templatesFolder, this.category, this.template);
261             } finally {
262                 if (cursor != null) {
263                     panel.setCursor (cursor);
264                 }
265                 synchronized(TemplatesPanel.this) {
266                     warmUpActive = false;
267                 }
268             }
269         }
270
271         void setTemplatesFolder (FileObject fo) {
272             this.templatesFolder = fo;
273         }
274
275         void setSelectedCategory (String JavaDoc s) {
276             this.category = s;
277         }
278
279         void setSelectedTemplate (String JavaDoc s) {
280             this.template = s;
281         }
282     }
283     
284     private class Builder implements TemplatesPanelGUI.Builder {
285
286         public org.openide.nodes.Children createCategoriesChildren (DataFolder folder) {
287             assert folder != null : "Folder cannot be null."; //NOI18N
288
return new CategoriesChildren (folder);
289         }
290
291         public org.openide.nodes.Children createTemplatesChildren(DataFolder folder) {
292             return new TemplateChildren (folder);
293         }
294
295
296         public String JavaDoc getCategoriesName() {
297             return NbBundle.getMessage(TemplatesPanel.class,"CTL_Categories");
298         }
299
300
301         public String JavaDoc getTemplatesName() {
302             return NbBundle.getMessage(TemplatesPanel.class,"CTL_Projects");
303         }
304
305         public void fireChange() {
306             Iterator JavaDoc it = null;
307             synchronized (this) {
308                 if (listeners == null) {
309                     return;
310                 }
311                 it = ((ArrayList JavaDoc)listeners.clone()).iterator();
312             }
313             ChangeEvent JavaDoc event = new ChangeEvent JavaDoc (this);
314             while (it.hasNext ()) {
315                 ((ChangeListener JavaDoc)it.next()).stateChanged(event);
316             }
317         }
318
319     }
320 }
321
Popular Tags