KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > ui > wizard > JavaWizardIterator


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.ui.wizard;
21
22 import java.io.IOException JavaDoc;
23 import java.text.MessageFormat JavaDoc;
24 import java.util.NoSuchElementException JavaDoc;
25
26 import java.awt.Component JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import javax.swing.SwingUtilities JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30
31 import org.openide.ErrorManager;
32 import org.openide.WizardDescriptor;
33 import org.openide.filesystems.FileObject;
34 import org.openide.loaders.DataObject;
35 import org.openide.loaders.DataFolder;
36 import org.openide.loaders.TemplateWizard;
37 import org.openide.util.Utilities;
38 import org.openide.util.NbBundle;
39
40 import org.openide.src.Type;
41
42 import org.netbeans.modules.java.JavaDataLoader;
43 import org.netbeans.api.java.classpath.ClassPath;
44
45 public class JavaWizardIterator implements TemplateWizard.Iterator {
46     private static final long serialVersionUID = -1987345873459L;
47
48     /** Array of panels that form the Wizard.
49      */

50     private transient WizardDescriptor.Panel[] panels;
51
52     /**
53      * Names for panels used in the Wizard.
54      */

55     private static String JavaDoc[] panelNames;
56     
57     /** Singleton instance of JavaWizardIterator, should it be ever needed.
58      */

59     private static JavaWizardIterator instance;
60     
61     /** Index of the current panel. Panels are numbered from 0 to PANEL_COUNT - 1.
62      */

63     private transient int panelIndex = 0;
64     
65     /**
66      * Holds a reference to the instance of TemplateWizard we are communicating with.
67      */

68     private transient TemplateWizard wizardInstance;
69
70     public JavaWizardIterator() {
71     }
72     
73     /** Returns JavaWizardIterator singleton instance. This method is used
74      * for constructing the instance from filesystem.attributes.
75      */

76     public static synchronized JavaWizardIterator singleton() {
77         if (instance == null) {
78             instance = new JavaWizardIterator();
79         }
80         return instance;
81     }
82     // ========================= TemplateWizard.Iterator ============================
83

84     /** Instantiates the template using informations provided by
85      * the wizard.
86      *
87      * @param wiz the wizard
88      * @return set of data objects that has been created (should contain
89      * at least one
90      * @exception IOException if the instantiation fails
91     */

92     public java.util.Set JavaDoc instantiate(TemplateWizard wiz) throws IOException JavaDoc, IllegalArgumentException JavaDoc {
93         DataObject obj = instantiateTemplate(wiz.getTemplate(), wiz.getTargetFolder(), wiz.getTargetName());
94
95         // run default action (hopefully should be here)
96
final org.openide.nodes.Node node = obj.getNodeDelegate ();
97         final org.openide.util.actions.SystemAction sa = node.getDefaultAction ();
98         if (sa != null) {
99             SwingUtilities.invokeLater(new Runnable JavaDoc () {
100                 public void run () {
101                     sa.actionPerformed (new ActionEvent JavaDoc (node, ActionEvent.ACTION_PERFORMED, "")); // NOI18N
102
}
103             });
104         }
105         return java.util.Collections.singleton(obj);
106     }
107     
108     public WizardDescriptor.Panel current() {
109         return panels[panelIndex];
110     }
111     
112     public String JavaDoc name() {
113         return ""; // NOI18N
114
}
115     
116     public boolean hasNext() {
117         return false;
118     }
119     
120     public boolean hasPrevious() {
121         return false;
122     }
123     
124     public void nextPanel() {
125         throw new NoSuchElementException JavaDoc();
126     }
127     
128     public void previousPanel() {
129         throw new NoSuchElementException JavaDoc();
130     }
131     
132     /** Add a listener to changes of the current panel.
133      * The listener is notified when the possibility to move forward/backward changes.
134      * @param l the listener to add
135     */

136     public void addChangeListener(ChangeListener JavaDoc l) {
137     }
138     
139     /** Remove a listener to changes of the current panel.
140      * @param l the listener to remove
141     */

142     public void removeChangeListener(ChangeListener JavaDoc l) {
143     }
144
145     public void initialize(TemplateWizard wizard) {
146         this.wizardInstance = wizard;
147         
148     if (panels == null) {
149             
150             Component JavaDoc panel = wizard.targetChooser().getComponent();
151             panelNames = new String JavaDoc[]{panel.getName()};
152             if (panel instanceof javax.swing.JComponent JavaDoc) {
153                 ((javax.swing.JComponent JavaDoc)panel).putClientProperty(
154                     "WizardPanel_contentData", panelNames); // NOI18N
155
}
156             panels = new WizardDescriptor.Panel[] {
157                 wizardInstance.targetChooser()
158             };
159     }
160     }
161     
162     public void uninitialize(TemplateWizard wiz) {
163         panels = null;
164         wizardInstance = null;
165     }
166
167     // ========================= IMPLEMENTATION ============================
168

169     private Object JavaDoc readResolve() {
170         return singleton();
171     }
172     
173     /** Instantiates the template. Currently it just delegates to the template DataObject's
174      * createFromTemplate method.
175      */

176     private DataObject instantiateTemplate(DataObject tpl, DataFolder target, String JavaDoc name) throws IOException JavaDoc {
177         if (name == null) {
178             name = getDefaultName(tpl, target);
179         }
180         
181         checkValidPackageName(target);
182         checkTargetName(target, name);
183         return tpl.createFromTemplate(target, name);
184     }
185     
186     private boolean isValidPackageName(String JavaDoc s) {
187         // valid package is an empty one, or well-formed java identifier :-)
188
if ("".equals(s)) // NOI18N
189
return true;
190         try {
191             Type t = Type.parse(s);
192             return true;
193         } catch (IllegalArgumentException JavaDoc ex) {
194             return false;
195         }
196     }
197     
198     private void checkValidPackageName(DataFolder targetFolder)
199         throws IllegalStateException JavaDoc {
200         FileObject folder = targetFolder.getPrimaryFile();
201         ClassPath cp = ClassPath.getClassPath(folder,ClassPath.SOURCE);
202         String JavaDoc msg = null;
203         if (cp != null) {
204             String JavaDoc fullTarget = cp.getResourceName(folder, '.',false);
205             if (isValidPackageName (fullTarget)) {
206                 return;
207             }
208             else {
209                 msg = MessageFormat.format(getString("FMTERR_IllegalFolderName"), // NOI18N
210
new Object JavaDoc[] {
211                     folder.getPath(),
212                     fullTarget});
213             }
214         }
215         else {
216             msg = getString ("ERR_NotInSourcePath");
217         }
218         // checking for java-compatible name - both the folder name and the target name
219
// must be acceptable.
220
throw (IllegalStateException JavaDoc)ErrorManager.getDefault().annotate(
221             new IllegalStateException JavaDoc(msg),
222             ErrorManager.USER, null, msg,
223             null, null);
224     }
225     
226     /**
227      * @param folder target folder for java file
228      * @param desiredName name to check
229      * @return true if the desiredName is OK
230      */

231     private boolean checkTargetName(DataFolder folder, String JavaDoc desiredName) {
232         if (!Utilities.isJavaIdentifier(desiredName)) {
233             String JavaDoc msg = MessageFormat.format(getString("FMTERR_IllegalTargetName"), // NOI18N
234
new Object JavaDoc[] {
235                 desiredName
236             });
237             notifyError(msg);
238             return false;
239         }
240         
241         FileObject f = folder.getPrimaryFile();
242         // check whether the name already exists:
243
if (f.getFileObject(desiredName, JavaDataLoader.JAVA_EXTENSION) != null) {
244             String JavaDoc msg = MessageFormat.format(getString("FMTERR_TargetExists"), // NOI18N
245
new Object JavaDoc[] {
246                 desiredName
247             });
248             notifyError(msg);
249             return false;
250         }
251         return true;
252     }
253     
254     private void notifyError(String JavaDoc msg) {
255         this.wizardInstance.putProperty("WizardPanel_errorMessage", msg); //NOI18N
256
IllegalStateException JavaDoc ex = new IllegalStateException JavaDoc(msg);
257         ErrorManager.getDefault().annotate(ex, ErrorManager.USER, null, msg, null, null);
258         throw ex;
259     }
260     
261     private String JavaDoc getDefaultName(DataObject template, DataFolder targetFolder) {
262         String JavaDoc desiredName = org.openide.filesystems.FileUtil.findFreeFileName(targetFolder.getPrimaryFile(),
263             template.getName(), JavaDataLoader.JAVA_EXTENSION);
264         return desiredName;
265     }
266
267     static String JavaDoc getString(String JavaDoc key) {
268         return NbBundle.getMessage(JavaWizardIterator.class, key);
269     }
270     
271     static char getMnemonic(String JavaDoc key) {
272         return getString(key).charAt(0);
273     }
274 }
275
Popular Tags