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.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.text.MessageFormat;
047: import javax.swing.JComponent;
048: import org.netbeans.api.project.Project;
049: import org.netbeans.api.project.ProjectUtils;
050: import org.netbeans.api.project.Sources;
051: import org.netbeans.modules.project.uiapi.ProjectChooserFactory;
052: import org.netbeans.spi.project.ui.templates.support.Templates;
053: import org.openide.WizardDescriptor;
054: import org.openide.loaders.DataObject;
055: import org.openide.loaders.DataObjectNotFoundException;
056:
057: import org.openide.loaders.TemplateWizard;
058: import org.openide.util.NbBundle;
059:
060: public final class NewFileWizard extends TemplateWizard {
061:
062: private Project currP;
063: private MessageFormat format;
064:
065: // private String[] recommendedTypes;
066:
067: private Project getCurrentProject() {
068: return currP;
069: }
070:
071: private void setCurrentProject(Project p) {
072: this .currP = p;
073: }
074:
075: public NewFileWizard(Project project /*, String recommendedTypes[] */) {
076: setCurrentProject(project);
077: putProperty(ProjectChooserFactory.WIZARD_KEY_PROJECT,
078: getCurrentProject());
079: format = new MessageFormat(NbBundle.getBundle(
080: NewFileWizard.class).getString(
081: "LBL_NewFileWizard_MessageFormat"));
082: // this.recommendedTypes = recommendedTypes;
083: //setTitleFormat( new MessageFormat( "{0}") );
084: addPropertyChangeListener(new PropertyChangeListener() {
085: public void propertyChange(PropertyChangeEvent evt) {
086: // check ProjectChooserFactory.WIZARD_KEY_PROJECT property
087: if (ProjectChooserFactory.WIZARD_KEY_PROJECT.equals(evt
088: .getPropertyName())) {
089: Project newProject = (Project) evt.getNewValue();
090: if (!getCurrentProject().equals(newProject)) {
091: // set the new project and force reload panels in wizard
092: setCurrentProject(newProject);
093: try {
094: //reload (DataObject.find (Templates.getTemplate (NewFileWizard.this)));
095: // bugfix #44481, check if the template is null
096: if (Templates
097: .getTemplate(NewFileWizard.this ) != null) {
098: DataObject obj = DataObject
099: .find(Templates
100: .getTemplate(NewFileWizard.this ));
101:
102: // read the attributes declared in module's layer
103: Object unknownIterator = obj
104: .getPrimaryFile()
105: .getAttribute(
106: "instantiatingIterator"); //NOI18N
107: if (unknownIterator == null) {
108: unknownIterator = obj
109: .getPrimaryFile()
110: .getAttribute(
111: "templateWizardIterator"); //NOI18N
112: }
113: // set default NewFileIterator if no attribute is set
114: if (unknownIterator == null) {
115: try {
116: obj
117: .getPrimaryFile()
118: .setAttribute(
119: "instantiatingIterator",
120: NewFileIterator
121: .genericFileIterator()); //NOI18N
122: } catch (java.io.IOException e) {
123: // can ignore it because a iterator will created though
124: }
125: }
126: Hacks.reloadPanelsInWizard(
127: NewFileWizard.this , obj);
128: }
129: } catch (DataObjectNotFoundException ex) {
130: ex.printStackTrace();
131: }
132: }
133: }
134: }
135: });
136: }
137:
138: public void updateState() {
139: super .updateState();
140: String substitute = (String) getProperty("NewFileWizard_Title"); // NOI18N
141: String title;
142: if (substitute == null) {
143: title = NbBundle.getBundle(NewFileWizard.class).getString(
144: "LBL_NewFileWizard_Title"); // NOI18N
145: } else {
146: Object[] args = new Object[] {
147: NbBundle.getBundle(NewFileWizard.class).getString(
148: "LBL_NewFileWizard_Subtitle"), // NOI18N
149: substitute };
150: title = format.format(args);
151: }
152: super .setTitle(title);
153: }
154:
155: public void setTitle(String ignore) {
156: }
157:
158: protected WizardDescriptor.Panel<WizardDescriptor> createTemplateChooser() {
159: WizardDescriptor.Panel<WizardDescriptor> panel = new TemplateChooserPanel(
160: getCurrentProject() /*, recommendedTypes */);
161: JComponent jc = (JComponent) panel.getComponent();
162: jc.getAccessibleContext().setAccessibleName(
163: NbBundle.getBundle(NewProjectWizard.class).getString(
164: "ACSN_NewFileWizard")); // NOI18N
165: jc.getAccessibleContext().setAccessibleDescription(
166: NbBundle.getBundle(NewProjectWizard.class).getString(
167: "ACSD_NewFileWizard")); // NOI18N
168: return panel;
169: }
170:
171: protected WizardDescriptor.Panel<WizardDescriptor> createTargetChooser() {
172: Sources c = ProjectUtils.getSources(getCurrentProject());
173: return Templates.createSimpleTargetChooser(getCurrentProject(),
174: c.getSourceGroups(Sources.TYPE_GENERIC));
175: }
176:
177: }
178:
179: /** Old impl might be usefull later in Wizards API
180:
181: ///** Wizard for creating new files in a project.
182: // *
183: // * @author Jesse Glick, Petr Hrebejk
184: // */
185: //public class NewFileWizard implements TemplateWizard.Iterator, ChangeListener {
186: //
187: // /** Currently selected project */
188: // private Project p;
189: // /** Recommended template types, or null for any. */
190: // private final String[] recommendedTypes;
191: // /** Currently selected template to delegate subsequent panels to, or null. */
192: // private InstantiatingIterator delegate = null;
193: // /** True if currently on a panel created by the delegate. */
194: // private boolean insideDelegate = false;
195: // /** The template chooser panel (initially null). */
196: // private WizardDescriptor.Panel templateChooser = null;
197: // /** Change listeners. */
198: // private final ChangeSupport changeSupport = new ChangeSupport(this);
199: // /** Currently used wizard. */
200: // private TemplateWizard wiz = null;
201: //
202: // /** Creates a new instance of NewFileWizard */
203: // public NewFileWizard(Project p, String[] recommendedTypes) {
204: // this.p = p;
205: // this.recommendedTypes = recommendedTypes;
206: // }
207: //
208: // public void initialize(TemplateWizard wiz) {
209: // this.wiz = wiz;
210: // wiz.putProperty(ProjectChooserFactory.WIZARD_KEY_PROJECT, p);
211: // }
212: //
213: // public void uninitialize(TemplateWizard wiz) {
214: // this.wiz = null;
215: // insideDelegate = false;
216: // setDelegate(null);
217: // templateChooser = null;
218: // }
219: //
220: // public Set instantiate(TemplateWizard wiz) throws IOException {
221: // assert insideDelegate;
222: // return delegate.instantiate(wiz);
223: // }
224: //
225: // public String name() {
226: // if (insideDelegate) {
227: // return delegate.name();
228: // } else {
229: // return "Choose Template"; // XXX I18N
230: // }
231: // }
232: //
233: // /*
234: // public WizardDescriptor.Panel current() {
235: // if (insideDelegate) {
236: // return delegate.current();
237: // } else {
238: // if (templateChooser == null) {
239: // templateChooser = new TemplateChooserPanel();
240: // }
241: // return templateChooser;
242: // }
243: // }
244: // */
245: //
246: // public boolean hasNext() {
247: // if (insideDelegate) {
248: // return delegate.hasNext();
249: // } else {
250: // return delegate != null;
251: // }
252: // }
253: //
254: // public boolean hasPrevious() {
255: // return insideDelegate;
256: // }
257: //
258: // public void nextPanel() {
259: // if (insideDelegate) {
260: // delegate.nextPanel();
261: // } else {
262: // assert delegate != null;
263: // insideDelegate = true;
264: // }
265: // }
266: //
267: // public void previousPanel() {
268: // assert insideDelegate;
269: // if (delegate.hasPrevious()) {
270: // delegate.previousPanel();
271: // } else {
272: // insideDelegate = false;
273: // }
274: // }
275: //
276: // public void addChangeListener(ChangeListener l) {
277: // changeSupport.addChangeListener(l);
278: // }
279: //
280: // public void removeChangeListener(ChangeListener l) {
281: // changeSupport.removeChangeListener(l);
282: // }
283: //
284: // public void setDelegate(InstantiatingIterator nue) {
285: // assert !insideDelegate;
286: // if (delegate == nue) {
287: // return;
288: // }
289: // if (delegate != null) {
290: // delegate.removeChangeListener(this);
291: // delegate.uninitialize(wiz);
292: // }
293: // if (nue != null) {
294: // nue.initialize(wiz);
295: // nue.addChangeListener(this);
296: // }
297: // delegate = nue;
298: // changeSupport.fireChange();
299: // }
300: //
301: // public void stateChanged(ChangeEvent e) {
302: // changeSupport.fireChange();
303: // }
304: //
305: // private static InstantiatingIterator findTemplateWizardIterator(FileObject template, Project p) {
306: // TemplateWizard.Iterator iter = (TemplateWizard.Iterator)template.getAttribute("templateWizardIterator"); // NOI18N
307: // if (iter != null) {
308: // return WizardIterators.templateIteratotBridge( iter );
309: // }
310: // else {
311: // Sources c = ProjectUtils.getSources(p);
312: // WizardDescriptor.Panel panels[] = new WizardDescriptor.Panel[1];
313: // panels[0] = Templates.createSimpleTargetChooser(p, template, c.getSourceGroups(Sources.TYPE_GENERIC));
314: // return new WizardIterators.InstantiatingArrayIterator( panels, template );
315: // }
316: // }
317: //
318: // }
|