001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.project.ui;
043:
044: import java.awt.Component;
045: import java.awt.Cursor;
046: import javax.swing.event.ChangeListener;
047: import org.openide.ErrorManager;
048: import org.openide.WizardDescriptor;
049: import org.openide.filesystems.FileObject;
050: import org.openide.loaders.DataFolder;
051: import org.openide.loaders.DataObject;
052: import org.openide.loaders.DataObjectNotFoundException;
053: import org.openide.loaders.TemplateWizard;
054: import org.openide.nodes.Children;
055: import org.openide.nodes.FilterNode;
056: import org.openide.nodes.Node;
057: import org.openide.util.AsyncGUIJob;
058: import org.openide.util.ChangeSupport;
059: import org.openide.util.HelpCtx;
060: import org.openide.util.NbBundle;
061: import org.openide.util.Utilities;
062:
063: /**
064: *
065: * @author tom
066: */
067: public class TemplatesPanel implements
068: WizardDescriptor.Panel<WizardDescriptor> {
069:
070: private final ChangeSupport changeSupport = new ChangeSupport(this );
071: private TemplatesPanelGUI panel;
072: private WarmupJob warmUp;
073: private boolean warmUpActive;
074: private boolean needsReselect = false; // WelcomeScreen hack, XXX Delete after WS is redesigned
075:
076: /** Creates a new instance of TemplatesPanel */
077: public TemplatesPanel() {
078: }
079:
080: public void readSettings(WizardDescriptor settings) {
081: TemplateWizard wd = (TemplateWizard) settings;
082: wd.putProperty("WizardPanel_contentSelectedIndex", new Integer(
083: 0)); // NOI18N
084: wd
085: .putProperty(
086: "WizardPanel_contentData",
087: new String[] { // NOI18N
088: NbBundle
089: .getBundle(TemplatesPanel.class)
090: .getString(
091: "LBL_TemplatesPanel_Name"), // NOI18N
092: NbBundle
093: .getBundle(TemplatesPanel.class)
094: .getString(
095: "LBL_TemplatesPanel_Dots") }); // NOI18N
096: FileObject templatesFolder = (FileObject) wd
097: .getProperty(TemplatesPanelGUI.TEMPLATES_FOLDER);
098:
099: // WelcomeScreen hack, XXX Delete after WS is redesigned
100: String preselectedCategory = (String) wd
101: .getProperty("PRESELECT_CATEGORY");
102: if (templatesFolder != null
103: && templatesFolder.isFolder()
104: && (wd.getTemplate() == null
105: || preselectedCategory != null || needsReselect)) {
106:
107: String preselectedTemplate = (String) wd
108: .getProperty("PRESELECT_TEMPLATE");
109: String template;
110: String selectedCategory = OpenProjectListSettings
111: .getInstance().getLastSelectedProjectCategory();
112: String selectedTemplate = OpenProjectListSettings
113: .getInstance().getLastSelectedProjectType();
114:
115: if (preselectedTemplate == null) {
116: template = preselectedCategory != null ? null
117: : selectedTemplate;
118: } else {
119: template = preselectedCategory != null ? preselectedTemplate
120: : selectedTemplate;
121: }
122:
123: TemplatesPanelGUI p = (TemplatesPanelGUI) this
124: .getComponent();
125: if (isWarmUpActive()) {
126: WarmupJob wup = getWarmUp();
127: wup.setTemplatesFolder(templatesFolder);
128: wup
129: .setSelectedCategory(preselectedCategory != null ? preselectedCategory
130: : selectedCategory);
131: wup.setSelectedTemplate(template);
132: } else {
133: p.setTemplatesFolder(templatesFolder);
134: p
135: .setSelectedCategoryByName(preselectedCategory != null ? preselectedCategory
136: : selectedCategory);
137: p.setSelectedTemplateByName(template);
138: }
139:
140: }
141: // bugfix #44792: project wizard title always changes
142: wd.putProperty("NewProjectWizard_Title", null); // NOI18N
143: }
144:
145: public void storeSettings(WizardDescriptor settings) {
146: TemplateWizard wd = (TemplateWizard) settings;
147:
148: // WelcomeScreen hack, XXX Delete after WS is redesigned
149: String preselectedCategory = (String) wd
150: .getProperty("PRESELECT_CATEGORY");
151:
152: TemplatesPanelGUI gui = (TemplatesPanelGUI) this .getComponent();
153: FileObject fo = gui.getSelectedTemplate();
154: if (fo != null) {
155: try {
156: wd.setTemplate(DataObject.find(fo));
157: } catch (DataObjectNotFoundException e) {
158: ErrorManager.getDefault().notify(e);
159: }
160: }
161: if (preselectedCategory == null) {
162:
163: String path = gui.getSelectedCategoryName();
164: if (path != null) {
165: OpenProjectListSettings.getInstance()
166: .setLastSelectedProjectCategory(path);
167: }
168: path = gui.getSelectedTemplateName();
169: if (path != null) {
170: OpenProjectListSettings.getInstance()
171: .setLastSelectedProjectType(path);
172: }
173: needsReselect = false;
174: } else {
175: needsReselect = true;
176: }
177: }
178:
179: public void addChangeListener(ChangeListener l) {
180: changeSupport.addChangeListener(l);
181: }
182:
183: public void removeChangeListener(ChangeListener l) {
184: changeSupport.removeChangeListener(l);
185: }
186:
187: public boolean isValid() {
188: return ((TemplatesPanelGUI) this .getComponent())
189: .getSelectedTemplate() != null;
190: }
191:
192: public HelpCtx getHelp() {
193: return new HelpCtx(TemplatesPanel.class);
194: }
195:
196: public synchronized Component getComponent() {
197: if (this .panel == null) {
198: TemplatesPanelGUI.Builder firer = new Builder();
199: this .panel = new TemplatesPanelGUI(firer);
200: Utilities.attachInitJob(panel, getWarmUp());
201: this .warmUpActive = true;
202: this .panel.setName(NbBundle.getBundle(TemplatesPanel.class)
203: .getString("LBL_TemplatesPanel_Name")); // NOI18N
204: }
205: return this .panel;
206: }
207:
208: private synchronized WarmupJob getWarmUp() {
209: if (this .warmUp == null) {
210: this .warmUp = new WarmupJob();
211: }
212: return this .warmUp;
213: }
214:
215: private synchronized boolean isWarmUpActive() {
216: return warmUpActive;
217: }
218:
219: private static class CategoriesChildren extends
220: Children.Keys<DataObject> {
221:
222: private DataFolder root;
223:
224: public CategoriesChildren(DataFolder folder) {
225: this .root = folder;
226: }
227:
228: protected void addNotify() {
229: setKeys(root.getChildren());
230: }
231:
232: protected void removeNotify() {
233: this .setKeys(new DataObject[0]);
234: }
235:
236: protected Node[] createNodes(DataObject dobj) {
237: if (dobj instanceof DataFolder) {
238: DataFolder folder = (DataFolder) dobj;
239: int type = 0; //Empty folder or File folder
240: for (DataObject child : folder.getChildren()) {
241: type = 1;
242: if (child.getPrimaryFile().isFolder()) {
243: type = 2; //Folder folder
244: break;
245: }
246: }
247: if (type == 1) {
248: return new Node[] { new FilterNode(dobj
249: .getNodeDelegate(), Children.LEAF) };
250: } else if (type == 2) {
251: return new Node[] { new FilterNode(dobj
252: .getNodeDelegate(), new CategoriesChildren(
253: (DataFolder) dobj)) };
254: }
255: }
256: return new Node[0];
257: }
258: }
259:
260: private static class TemplateChildren extends
261: Children.Keys<DataObject> {
262:
263: private DataFolder folder;
264:
265: public TemplateChildren(DataFolder folder) {
266: this .folder = folder;
267: }
268:
269: protected void addNotify() {
270: this .setKeys(this .folder.getChildren());
271: }
272:
273: protected void removeNotify() {
274: this .setKeys(new DataObject[0]);
275: }
276:
277: protected Node[] createNodes(DataObject dobj) {
278: if (dobj.isTemplate()) {
279: return new Node[] { new FilterNode(dobj
280: .getNodeDelegate(), Children.LEAF) };
281: } else {
282: return new Node[0];
283: }
284: }
285:
286: }
287:
288: private class WarmupJob implements AsyncGUIJob {
289:
290: private FileObject templatesFolder;
291: private String category;
292: private String template;
293:
294: public void construct() {
295: panel.warmUp(this .templatesFolder);
296: }
297:
298: public void finished() {
299: Cursor cursor = null;
300: try {
301: cursor = panel.getCursor();
302: panel.setCursor(Cursor
303: .getPredefinedCursor(Cursor.WAIT_CURSOR));
304: panel.doFinished(this .templatesFolder, this .category,
305: this .template);
306: } finally {
307: if (cursor != null) {
308: panel.setCursor(cursor);
309: }
310: synchronized (TemplatesPanel.this ) {
311: warmUpActive = false;
312: }
313: }
314: }
315:
316: void setTemplatesFolder(FileObject fo) {
317: this .templatesFolder = fo;
318: }
319:
320: void setSelectedCategory(String s) {
321: this .category = s;
322: }
323:
324: void setSelectedTemplate(String s) {
325: this .template = s;
326: }
327: }
328:
329: private class Builder implements TemplatesPanelGUI.Builder {
330:
331: public org.openide.nodes.Children createCategoriesChildren(
332: DataFolder folder) {
333: assert folder != null : "Folder cannot be null."; //NOI18N
334: return new CategoriesChildren(folder);
335: }
336:
337: public org.openide.nodes.Children createTemplatesChildren(
338: DataFolder folder) {
339: return new TemplateChildren(folder);
340: }
341:
342: public String getCategoriesName() {
343: return NbBundle.getMessage(TemplatesPanel.class,
344: "CTL_Categories");
345: }
346:
347: public String getTemplatesName() {
348: return NbBundle.getMessage(TemplatesPanel.class,
349: "CTL_Projects");
350: }
351:
352: public void fireChange() {
353: changeSupport.fireChange();
354: }
355:
356: }
357: }
|