KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > project > NewJavaFileWizardIterator


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.java.project;
21
22 import java.awt.Component JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.NoSuchElementException JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.swing.JComponent JavaDoc;
30 import javax.swing.event.ChangeEvent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32 import org.netbeans.api.java.project.JavaProjectConstants;
33 import org.netbeans.api.project.Project;
34 import org.netbeans.api.project.ProjectUtils;
35 import org.netbeans.api.project.SourceGroup;
36 import org.netbeans.api.project.Sources;
37 import org.netbeans.spi.java.project.support.ui.templates.JavaTemplates;
38 import org.netbeans.spi.project.ui.templates.support.Templates;
39 import org.openide.WizardDescriptor;
40 import org.openide.ErrorManager;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.loaders.DataFolder;
44 import org.openide.loaders.DataObject;
45
46 /**
47  * Wizard to create a new Java file.
48  */

49 public class NewJavaFileWizardIterator implements WizardDescriptor.InstantiatingIterator {
50     
51     private static final long serialVersionUID = 1L;
52     
53     public static final int TYPE_FILE = 0;
54     public static final int TYPE_PACKAGE = 1;
55     public static final int TYPE_PKG_INFO = 2;
56     
57     private int type = TYPE_FILE;
58     
59     /** Create a new wizard iterator. */
60     public NewJavaFileWizardIterator() {}
61     
62     
63     private NewJavaFileWizardIterator( int type ) {
64         this.type = type;
65     }
66     
67     public static NewJavaFileWizardIterator packageWizard() {
68         return new NewJavaFileWizardIterator( TYPE_PACKAGE );
69     }
70     
71     public static NewJavaFileWizardIterator packageInfoWizard () {
72         return new NewJavaFileWizardIterator( TYPE_PKG_INFO );
73     }
74             
75     private WizardDescriptor.Panel[] createPanels (WizardDescriptor wizardDescriptor) {
76         
77         // Ask for Java folders
78
Project project = Templates.getProject( wizardDescriptor );
79         Sources sources = ProjectUtils.getSources(project);
80         SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
81         assert groups != null : "Cannot return null from Sources.getSourceGroups: " + sources;
82         if (groups.length == 0) {
83             groups = sources.getSourceGroups( Sources.TYPE_GENERIC );
84             return new WizardDescriptor.Panel[] {
85                 Templates.createSimpleTargetChooser( project, groups ),
86             };
87         }
88         else {
89             
90             if ( this.type == TYPE_FILE ) {
91                 return new WizardDescriptor.Panel[] {
92                     JavaTemplates.createPackageChooser( project, groups ),
93                 };
94             }
95             else {
96                 return new WizardDescriptor.Panel[] {
97                     new JavaTargetChooserPanel( project, groups, null, this.type, this.type == TYPE_PKG_INFO),
98                 };
99             }
100         }
101                
102     }
103     
104     private String JavaDoc[] createSteps(String JavaDoc[] before, WizardDescriptor.Panel[] panels) {
105         assert panels != null;
106         // hack to use the steps set before this panel processed
107
int diff = 0;
108         if (before == null) {
109             before = new String JavaDoc[0];
110         } else if (before.length > 0) {
111             diff = ("...".equals (before[before.length - 1])) ? 1 : 0; // NOI18N
112
}
113         String JavaDoc[] res = new String JavaDoc[ (before.length - diff) + panels.length];
114         for (int i = 0; i < res.length; i++) {
115             if (i < (before.length - diff)) {
116                 res[i] = before[i];
117             } else {
118                 res[i] = panels[i - before.length + diff].getComponent ().getName ();
119             }
120         }
121         return res;
122     }
123     
124     public Set JavaDoc/*<FileObject>*/ instantiate () throws IOException JavaDoc {
125         FileObject dir = Templates.getTargetFolder( wiz );
126         String JavaDoc targetName = Templates.getTargetName( wiz );
127         
128         DataFolder df = DataFolder.findFolder( dir );
129         FileObject template = Templates.getTemplate( wiz );
130         
131         FileObject createdFile = null;
132         if ( this.type == TYPE_PACKAGE ) {
133             targetName = targetName.replace( '.', '/' ); // NOI18N
134
createdFile = FileUtil.createFolder( dir, targetName );
135         }
136         else {
137             DataObject dTemplate = DataObject.find( template );
138             DataObject dobj = dTemplate.createFromTemplate( df, targetName );
139             createdFile = dobj.getPrimaryFile();
140         }
141         
142         return Collections.singleton( createdFile );
143     }
144     
145         
146     private transient int index;
147     private transient WizardDescriptor.Panel[] panels;
148     private transient WizardDescriptor wiz;
149     
150     public void initialize(WizardDescriptor wiz) {
151         this.wiz = wiz;
152         index = 0;
153         panels = createPanels( wiz );
154         // Make sure list of steps is accurate.
155
String JavaDoc[] beforeSteps = null;
156         Object JavaDoc prop = wiz.getProperty ("WizardPanel_contentData"); // NOI18N
157
if (prop != null && prop instanceof String JavaDoc[]) {
158             beforeSteps = (String JavaDoc[])prop;
159         }
160         String JavaDoc[] steps = createSteps (beforeSteps, panels);
161         for (int i = 0; i < panels.length; i++) {
162             Component JavaDoc c = panels[i].getComponent();
163             if (steps[i] == null) {
164                 // Default step name to component name of panel.
165
// Mainly useful for getting the name of the target
166
// chooser to appear in the list of steps.
167
steps[i] = c.getName();
168             }
169             if (c instanceof JComponent JavaDoc) { // assume Swing components
170
JComponent JavaDoc jc = (JComponent JavaDoc)c;
171                 // Step #.
172
jc.putClientProperty("WizardPanel_contentSelectedIndex", new Integer JavaDoc(i)); // NOI18N
173
// Step name (actually the whole list for reference).
174
jc.putClientProperty("WizardPanel_contentData", steps); // NOI18N
175
}
176         }
177     }
178     public void uninitialize (WizardDescriptor wiz) {
179         this.wiz = null;
180         panels = null;
181     }
182     
183     public String JavaDoc name() {
184         //return "" + (index + 1) + " of " + panels.length;
185
return ""; // NOI18N
186
}
187     
188     public boolean hasNext() {
189         return index < panels.length - 1;
190     }
191     public boolean hasPrevious() {
192         return index > 0;
193     }
194     public void nextPanel() {
195         if (!hasNext()) throw new NoSuchElementException JavaDoc();
196         index++;
197     }
198     public void previousPanel() {
199         if (!hasPrevious()) throw new NoSuchElementException JavaDoc();
200         index--;
201     }
202     public WizardDescriptor.Panel current() {
203         return panels[index];
204     }
205     
206     private transient Set JavaDoc<ChangeListener JavaDoc> listeners = new HashSet JavaDoc<ChangeListener JavaDoc>(1);
207     
208     public final void addChangeListener(ChangeListener JavaDoc l) {
209         synchronized(listeners) {
210             listeners.add(l);
211         }
212     }
213     public final void removeChangeListener(ChangeListener JavaDoc l) {
214         synchronized(listeners) {
215             listeners.remove(l);
216         }
217     }
218     protected final void fireChangeEvent() {
219         ChangeListener JavaDoc[] ls;
220         synchronized (listeners) {
221             ls = listeners.toArray(new ChangeListener JavaDoc[listeners.size()]);
222         }
223         ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc(this);
224         for (ChangeListener JavaDoc l : ls) {
225             l.stateChanged(ev);
226         }
227     }
228      
229     
230 }
231
Popular Tags