KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import javax.swing.JComponent JavaDoc;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.api.project.ProjectUtils;
28 import org.netbeans.api.project.Sources;
29 import org.netbeans.modules.project.uiapi.ProjectChooserFactory;
30 import org.netbeans.spi.project.ui.templates.support.Templates;
31 import org.openide.WizardDescriptor;
32 import org.openide.loaders.DataObject;
33 import org.openide.loaders.DataObjectNotFoundException;
34
35 import org.openide.loaders.TemplateWizard;
36 import org.openide.util.NbBundle;
37
38 public final class NewFileWizard extends TemplateWizard {
39         
40     private Project currP;
41     private MessageFormat JavaDoc format;
42     // private String[] recommendedTypes;
43

44     private Project getCurrentProject () {
45         return currP;
46     }
47
48     private void setCurrentProject (Project p) {
49         this.currP = p;
50     }
51
52     public NewFileWizard( Project project /*, String recommendedTypes[] */ ) {
53         setCurrentProject (project);
54         putProperty( ProjectChooserFactory.WIZARD_KEY_PROJECT, getCurrentProject () );
55         format = new MessageFormat JavaDoc (NbBundle.getBundle (NewFileWizard.class).getString ("LBL_NewFileWizard_MessageFormat"));
56         // this.recommendedTypes = recommendedTypes;
57
//setTitleFormat( new MessageFormat( "{0}") );
58
addPropertyChangeListener (new PropertyChangeListener JavaDoc () {
59             public void propertyChange (PropertyChangeEvent JavaDoc evt) {
60                 // check ProjectChooserFactory.WIZARD_KEY_PROJECT property
61
if (ProjectChooserFactory.WIZARD_KEY_PROJECT.equals (evt.getPropertyName ())) {
62                     Project newProject = (Project)evt.getNewValue ();
63                     if (!getCurrentProject ().equals (newProject)) {
64                         // set the new project and force reload panels in wizard
65
setCurrentProject (newProject);
66                         try {
67                             //reload (DataObject.find (Templates.getTemplate (NewFileWizard.this)));
68
// bugfix #44481, check if the template is null
69
if (Templates.getTemplate (NewFileWizard.this) != null) {
70                                 DataObject obj = DataObject.find (Templates.getTemplate (NewFileWizard.this));
71
72                                 // read the attributes declared in module's layer
73
Object JavaDoc unknownIterator = obj.getPrimaryFile ().getAttribute ("instantiatingIterator"); //NOI18N
74
if (unknownIterator == null) {
75                                     unknownIterator = obj.getPrimaryFile ().getAttribute ("templateWizardIterator"); //NOI18N
76
}
77                                 // set default NewFileIterator if no attribute is set
78
if (unknownIterator == null) {
79                                     try {
80                                         obj.getPrimaryFile ().setAttribute ("instantiatingIterator", NewFileIterator.genericFileIterator ()); //NOI18N
81
} catch (java.io.IOException JavaDoc e) {
82                                         // can ignore it because a iterator will created though
83
}
84                                 }
85                                 Hacks.reloadPanelsInWizard (NewFileWizard.this, obj);
86                             }
87                         } catch (DataObjectNotFoundException ex) {
88                             ex.printStackTrace();
89                         }
90                     }
91                 }
92             }
93         });
94     }
95     
96     public void updateState () {
97         super.updateState ();
98         String JavaDoc substitute = (String JavaDoc)getProperty ("NewFileWizard_Title"); // NOI18N
99
String JavaDoc title;
100         if (substitute == null) {
101             title = NbBundle.getBundle (NewFileWizard.class).getString ("LBL_NewFileWizard_Title"); // NOI18N
102
} else {
103             Object JavaDoc[] args = new Object JavaDoc[] {
104                     NbBundle.getBundle (NewFileWizard.class).getString ("LBL_NewFileWizard_Subtitle"), // NOI18N
105
substitute};
106             title = format.format (args);
107         }
108         super.setTitle (title);
109     }
110     
111     public void setTitle (String JavaDoc ignore) {}
112     
113     protected WizardDescriptor.Panel createTemplateChooser () {
114         WizardDescriptor.Panel panel = new TemplateChooserPanel( getCurrentProject () /*, recommendedTypes */ );
115         JComponent JavaDoc jc = (JComponent JavaDoc)panel.getComponent ();
116         jc.getAccessibleContext ().setAccessibleName (NbBundle.getBundle (NewProjectWizard.class).getString ("ACSN_NewFileWizard")); // NOI18N
117
jc.getAccessibleContext ().setAccessibleDescription (NbBundle.getBundle (NewProjectWizard.class).getString ("ACSD_NewFileWizard")); // NOI18N
118
return panel;
119     }
120
121     protected WizardDescriptor.Panel createTargetChooser () {
122         Sources c = ProjectUtils.getSources(getCurrentProject ());
123         return Templates.createSimpleTargetChooser(getCurrentProject (), c.getSourceGroups(Sources.TYPE_GENERIC));
124     }
125     
126 }
127
128 /** Old impl might be usefull later in Wizards API
129     
130 ///** Wizard for creating new files in a project.
131 // *
132 // * @author Jesse Glick, Petr Hrebejk
133 // */

134 //public class NewFileWizard implements TemplateWizard.Iterator, ChangeListener {
135
//
136
// /** Currently selected project */
137
// private Project p;
138
// /** Recommended template types, or null for any. */
139
// private final String[] recommendedTypes;
140
// /** Currently selected template to delegate subsequent panels to, or null. */
141
// private InstantiatingIterator delegate = null;
142
// /** True if currently on a panel created by the delegate. */
143
// private boolean insideDelegate = false;
144
// /** The template chooser panel (initially null). */
145
// private WizardDescriptor.Panel templateChooser = null;
146
// /** Change listeners. */
147
// private final List/*<ChangeListener>*/ listeners = new ArrayList();
148
// /** Currently used wizard. */
149
// private TemplateWizard wiz = null;
150
//
151
// /** Creates a new instance of NewFileWizard */
152
// public NewFileWizard(Project p, String[] recommendedTypes) {
153
// this.p = p;
154
// this.recommendedTypes = recommendedTypes;
155
// }
156
//
157
// public void initialize(TemplateWizard wiz) {
158
// this.wiz = wiz;
159
// wiz.putProperty(ProjectChooserFactory.WIZARD_KEY_PROJECT, p);
160
// }
161
//
162
// public void uninitialize(TemplateWizard wiz) {
163
// this.wiz = null;
164
// insideDelegate = false;
165
// setDelegate(null);
166
// templateChooser = null;
167
// }
168
//
169
// public Set instantiate(TemplateWizard wiz) throws IOException {
170
// assert insideDelegate;
171
// return delegate.instantiate(wiz);
172
// }
173
//
174
// public String name() {
175
// if (insideDelegate) {
176
// return delegate.name();
177
// } else {
178
// return "Choose Template"; // XXX I18N
179
// }
180
// }
181
//
182
// /*
183
// public WizardDescriptor.Panel current() {
184
// if (insideDelegate) {
185
// return delegate.current();
186
// } else {
187
// if (templateChooser == null) {
188
// templateChooser = new TemplateChooserPanel();
189
// }
190
// return templateChooser;
191
// }
192
// }
193
// */
194
//
195
// public boolean hasNext() {
196
// if (insideDelegate) {
197
// return delegate.hasNext();
198
// } else {
199
// return delegate != null;
200
// }
201
// }
202
//
203
// public boolean hasPrevious() {
204
// return insideDelegate;
205
// }
206
//
207
// public void nextPanel() {
208
// if (insideDelegate) {
209
// delegate.nextPanel();
210
// } else {
211
// assert delegate != null;
212
// insideDelegate = true;
213
// }
214
// }
215
//
216
// public void previousPanel() {
217
// assert insideDelegate;
218
// if (delegate.hasPrevious()) {
219
// delegate.previousPanel();
220
// } else {
221
// insideDelegate = false;
222
// }
223
// }
224
//
225
// public void addChangeListener(ChangeListener l) {
226
// listeners.add(l);
227
// }
228
//
229
// public void removeChangeListener(ChangeListener l) {
230
// listeners.remove(l);
231
// }
232
//
233
// private void fireChange() {
234
// ChangeEvent e = new ChangeEvent(this);
235
// Iterator it = listeners.iterator();
236
// while (it.hasNext()) {
237
// ((ChangeListener)it.next()).stateChanged(e);
238
// }
239
// }
240
//
241
// public void setDelegate(InstantiatingIterator nue) {
242
// assert !insideDelegate;
243
// if (delegate == nue) {
244
// return;
245
// }
246
// if (delegate != null) {
247
// delegate.removeChangeListener(this);
248
// delegate.uninitialize(wiz);
249
// }
250
// if (nue != null) {
251
// nue.initialize(wiz);
252
// nue.addChangeListener(this);
253
// }
254
// delegate = nue;
255
// fireChange();
256
// }
257
//
258
// public void stateChanged(ChangeEvent e) {
259
// fireChange();
260
// }
261
//
262
// private static InstantiatingIterator findTemplateWizardIterator(FileObject template, Project p) {
263
// TemplateWizard.Iterator iter = (TemplateWizard.Iterator)template.getAttribute("templateWizardIterator"); // NOI18N
264
// if (iter != null) {
265
// return WizardIterators.templateIteratotBridge( iter );
266
// }
267
// else {
268
// Sources c = ProjectUtils.getSources(p);
269
// WizardDescriptor.Panel panels[] = new WizardDescriptor.Panel[1];
270
// panels[0] = Templates.createSimpleTargetChooser(p, template, c.getSourceGroups(Sources.TYPE_GENERIC));
271
// return new WizardIterators.InstantiatingArrayIterator( panels, template );
272
// }
273
// }
274
//
275
// }
276
Popular Tags