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 javax.swing.event.ChangeEvent;
046: import javax.swing.event.ChangeListener;
047: import org.netbeans.api.project.Project;
048: import org.netbeans.modules.project.uiapi.ProjectChooserFactory;
049: import org.netbeans.spi.project.ui.templates.support.Templates;
050: import org.openide.NotifyDescriptor;
051: import org.openide.WizardDescriptor;
052: import org.openide.ErrorManager;
053: import org.openide.filesystems.FileObject;
054: import org.openide.filesystems.Repository;
055: import org.openide.loaders.DataObject;
056: import org.openide.loaders.DataObjectNotFoundException;
057: import org.openide.loaders.TemplateWizard;
058: import org.openide.util.ChangeSupport;
059: import org.openide.util.HelpCtx;
060: import org.openide.util.NbBundle;
061:
062: /**
063: *
064: * @author Petr Hrebejk
065: */
066: final class TemplateChooserPanel implements
067: WizardDescriptor.Panel<WizardDescriptor>, ChangeListener {
068:
069: private final ChangeSupport changeSupport = new ChangeSupport(this );
070: private TemplateChooserPanelGUI gui;
071:
072: private Project project;
073:
074: // private String[] recommendedTypes;
075:
076: TemplateChooserPanel(Project p /*, String recommendedTypes[] */) {
077: this .project = p;
078: /* this.recommendedTypes = recommendedTypes; */
079: }
080:
081: public Component getComponent() {
082: if (gui == null) {
083: gui = new TemplateChooserPanelGUI();
084: gui.addChangeListener(this );
085: }
086: return gui;
087: }
088:
089: public HelpCtx getHelp() {
090: // XXX
091: return null;
092: }
093:
094: public boolean isValid() {
095: return gui != null && gui.getTemplate() != null;
096: }
097:
098: public void addChangeListener(ChangeListener l) {
099: changeSupport.addChangeListener(l);
100: }
101:
102: public void removeChangeListener(ChangeListener l) {
103: changeSupport.removeChangeListener(l);
104: }
105:
106: public void readSettings(WizardDescriptor settings) {
107: TemplateChooserPanelGUI panel = (TemplateChooserPanelGUI) this
108: .getComponent();
109: final FileObject currentTemplate = Templates
110: .getTemplate(settings);
111: FileObject templates = Repository.getDefault()
112: .getDefaultFileSystem().findResource("Templates"); //NOI18N
113: String currentCategoryName = null;
114: String currentTemplateName = null;
115: if (templates != null
116: && currentTemplate != null
117: && currentTemplate.getParent() != null
118: && templates.equals(currentTemplate.getParent()
119: .getParent())) {
120: try {
121: final DataObject dobj = DataObject
122: .find(currentTemplate);
123: final DataObject owner = DataObject
124: .find(currentTemplate.getParent());
125: currentTemplateName = dobj.getName();
126: currentCategoryName = owner.getName();
127: } catch (DataObjectNotFoundException e) {
128: //Ignore and use default
129: }
130: }
131: panel.readValues(project, currentCategoryName,
132: currentTemplateName);
133: settings.putProperty("WizardPanel_contentSelectedIndex", 0); // NOI18N
134: settings
135: .putProperty(
136: "WizardPanel_contentData",
137: new String[] { // NOI18N
138: NbBundle
139: .getBundle(
140: TemplateChooserPanel.class)
141: .getString(
142: "LBL_TemplatesPanel_Name"), // NOI18N
143: NbBundle
144: .getBundle(
145: TemplateChooserPanel.class)
146: .getString(
147: "LBL_TemplatesPanel_Dots") }); // NOI18N
148: // bugfix #44400: wizard title always changes
149: settings.putProperty("NewFileWizard_Title", null); // NOI18N
150: }
151:
152: public void storeSettings(WizardDescriptor wd) {
153: Object value = wd.getValue();
154:
155: if (NotifyDescriptor.CANCEL_OPTION != value
156: && NotifyDescriptor.CLOSED_OPTION != value) {
157: try {
158:
159: Project newProject = gui.getProject();
160: if (!project.equals(newProject)) {
161: project = newProject;
162: wd.putProperty(
163: ProjectChooserFactory.WIZARD_KEY_PROJECT,
164: newProject);
165: }
166:
167: if (gui.getTemplate() == null) {
168: return;
169: }
170:
171: if (wd instanceof TemplateWizard) {
172: ((TemplateWizard) wd).setTemplate(DataObject
173: .find(gui.getTemplate()));
174: } else {
175: wd.putProperty(
176: ProjectChooserFactory.WIZARD_KEY_TEMPLATE,
177: gui.getTemplate());
178: }
179: } catch (DataObjectNotFoundException e) {
180: ErrorManager.getDefault().notify(e);
181: }
182: }
183: }
184:
185: public void stateChanged(ChangeEvent e) {
186: /*
187: FileObject template = gui.getTemplate();
188: p = gui.getProject();
189: if (template != null) {
190: setDelegate(findTemplateWizardIterator(template, p));
191: } else {
192: setDelegate(null);
193: }
194: */
195: changeSupport.fireChange();
196:
197: }
198:
199: }
|