KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > TemplateWizard2


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.openide.loaders;
21
22 import java.awt.event.KeyEvent JavaDoc;
23 import java.beans.PropertyDescriptor JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.lang.ref.*;
26 import java.lang.reflect.Method JavaDoc;
27 import javax.swing.*;
28 import javax.swing.event.*;
29 import org.openide.explorer.propertysheet.*;
30 import org.openide.filesystems.*;
31 import org.openide.util.*;
32
33 /** Dialog that can be used in create from template.
34  *
35  * @author Jaroslav Tulach, Jiri Rechtacek
36  */

37 final class TemplateWizard2 extends javax.swing.JPanel JavaDoc implements DocumentListener {
38
39     /** listener to changes in the wizard */
40     private ChangeListener listener;
41     
42     private static final String JavaDoc PROP_LOCATION_FOLDER = "locationFolder"; // NOI18N
43
private DataFolder locationFolder;
44     private Reference<FileSystem> fileSystemRef = new WeakReference<FileSystem>(null);
45     private DefaultPropertyModel locationFolderModel;
46
47     /** File extension of the template and of the created file -
48      * it is used to test whether file already exists.
49      */

50     private String JavaDoc extension;
51
52     /** Creates new form TemplateWizard2 */
53     public TemplateWizard2() {
54         initLocationFolder ();
55         initComponents ();
56         setName (DataObject.getString("LAB_TargetLocationPanelName")); // NOI18N
57
// registers itself to listen to changes in the content of document
58
java.util.ResourceBundle JavaDoc bundle = org.openide.util.NbBundle.getBundle(TemplateWizard2.class);
59         newObjectName.getDocument().addDocumentListener(this);
60         newObjectName.unregisterKeyboardAction(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
61         newObjectName.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_NewObjectName")); // NOI18N
62
}
63     
64     /** This method is called from within the constructor to
65      * initialize the location folder and make it accessible.
66      * The getter/setter methods must be accessible for purposes introspection.
67      * Because this class is not public then these methods are made accessible explicitly.
68      */

69     private void initLocationFolder () {
70         PropertyDescriptor JavaDoc pd = null;
71         try {
72             Method JavaDoc getterMethod = this.getClass ().getDeclaredMethod("getLocationFolder", new Class JavaDoc[] {}); // NOI18N
73
getterMethod.setAccessible (true);
74             Method JavaDoc setterMethod = this.getClass ().getDeclaredMethod("setLocationFolder", new Class JavaDoc[] {DataFolder.class}); // NOI18N
75
setterMethod.setAccessible (true);
76             pd = new PropertyDescriptor JavaDoc (PROP_LOCATION_FOLDER, getterMethod, setterMethod);
77         } catch (java.beans.IntrospectionException JavaDoc ie) {
78             Exceptions.printStackTrace(ie);
79         } catch (NoSuchMethodException JavaDoc nsme) {
80             Exceptions.printStackTrace(nsme);
81         }
82         locationFolderModel = new DefaultPropertyModel (this, pd);
83     }
84
85     /** Getter for default name of a new object.
86     * @return the default name.
87     */

88     private static String JavaDoc defaultNewObjectName () {
89         return DataObject.getString ("FMT_DefaultNewObjectName"); // NOI18N
90
}
91     
92     /** This method is called from within the constructor to
93      * initialize the form.
94      * WARNING: Do NOT modify this code. The content of this method is
95      * always regenerated by the Form Editor.
96      */

97     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
98
private void initComponents() {
99
100         namePanel = new javax.swing.JPanel JavaDoc();
101         jLabel1 = new javax.swing.JLabel JavaDoc();
102         newObjectName = new javax.swing.JTextField JavaDoc();
103         dataFolderPanel = dataFolderPanel = new PropertyPanel (locationFolderModel, PropertyPanel.PREF_CUSTOM_EDITOR);
104
105         FormListener formListener = new FormListener();
106
107         setLayout(new java.awt.BorderLayout JavaDoc());
108
109         namePanel.setLayout(new java.awt.BorderLayout JavaDoc(12, 0));
110
111         jLabel1.setLabelFor(newObjectName);
112         java.util.ResourceBundle JavaDoc bundle = java.util.ResourceBundle.getBundle("org/openide/loaders/Bundle"); // NOI18N
113
org.openide.awt.Mnemonics.setLocalizedText(jLabel1, bundle.getString("CTL_NewObjectName")); // NOI18N
114
namePanel.add(jLabel1, java.awt.BorderLayout.WEST);
115
116         newObjectName.addFocusListener(formListener);
117         namePanel.add(newObjectName, java.awt.BorderLayout.CENTER);
118
119         add(namePanel, java.awt.BorderLayout.NORTH);
120         add(dataFolderPanel, java.awt.BorderLayout.CENTER);
121     }
122
123     // Code for dispatching events from components to event handlers.
124

125     private class FormListener implements java.awt.event.FocusListener JavaDoc {
126         FormListener() {}
127         public void focusGained(java.awt.event.FocusEvent JavaDoc evt) {
128             if (evt.getSource() == newObjectName) {
129                 TemplateWizard2.this.newObjectNameFocusGained(evt);
130             }
131         }
132
133         public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
134         }
135     }// </editor-fold>//GEN-END:initComponents
136

137     private void newObjectNameFocusGained(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_newObjectNameFocusGained
138
newObjectName.selectAll ();
139     }//GEN-LAST:event_newObjectNameFocusGained
140

141     /** Add a listener to changes of the panel's validity.
142      * @param l the listener to add
143      * @see #isValid
144      *
145      */

146     void addChangeListener(ChangeListener l) {
147         if (listener != null) throw new IllegalStateException JavaDoc ();
148
149         listener = l;
150     }
151     
152     public void addNotify () {
153         super.addNotify();
154         //Fix for issue 31086, initial focus on Back button
155
newObjectName.requestFocus();
156         getAccessibleContext().setAccessibleDescription(
157             NbBundle.getBundle(TemplateWizard2.class).getString ("ACSD_TemplateWizard2") // NOI18N
158
);
159     }
160     
161     /** Helper implementation of WizardDescription.Panel for TemplateWizard.Panel2.
162      * Provides the wizard panel with the current data--either
163      * the default data or already-modified settings, if the user used the previous and/or next buttons.
164      * This method can be called multiple times on one instance of <code>WizardDescriptor.Panel</code>.
165      * <p>The settings object is originally supplied to {@link WizardDescriptor#WizardDescriptor(WizardDescriptor.Iterator,Object)}.
166      * In the case of a <code>TemplateWizard.Iterator</code> panel, the object is
167      * in fact the <code>TemplateWizard</code>.
168      * @param settings the object representing wizard panel state
169      * @exception IllegalStateException if the the data provided
170      * by the wizard are not valid.
171      *
172      */

173     void implReadSettings(Object JavaDoc settings) {
174         TemplateWizard wizard = (TemplateWizard)settings;
175
176         DataObject template = wizard.getTemplate ();
177         if (template != null) {
178             extension = template.getPrimaryFile().getExt();
179         }
180         
181         setNewObjectName (wizard.getTargetName ());
182
183         try {
184             setLocationFolder (wizard.getTargetFolder ());
185         } catch (IOException JavaDoc ioe) {
186             setLocationFolder (null);
187         }
188     }
189     
190     /** Remove a listener to changes of the panel's validity.
191      * @param l the listener to remove
192      *
193      */

194     public void removeChangeListener(ChangeListener l) {
195         listener = null;
196     }
197     
198     /** Helper implementation of WizardDescription.Panel for TemplateWizard.Panel2.
199      * Provides the wizard panel with the opportunity to update the
200      * settings with its current customized state.
201      * Rather than updating its settings with every change in the GUI, it should collect them,
202      * and then only save them when requested to by this method.
203      * Also, the original settings passed to {@link #readSettings} should not be modified (mutated);
204      * rather, the object passed in here should be mutated according to the collected changes,
205      * in case it is a copy.
206      * This method can be called multiple times on one instance of <code>WizardDescriptor.Panel</code>.
207      * <p>The settings object is originally supplied to {@link WizardDescriptor#WizardDescriptor(WizardDescriptor.Iterator,Object)}.
208      * In the case of a <code>TemplateWizard.Iterator</code> panel, the object is
209      * in fact the <code>TemplateWizard</code>.
210      * @param settings the object representing wizard panel state
211      *
212      */

213     void implStoreSettings(Object JavaDoc settings) {
214         TemplateWizard wizard = (TemplateWizard)settings;
215
216         wizard.setTargetFolder (locationFolder);
217
218         String JavaDoc name = newObjectName.getText ();
219         if (name.equals (defaultNewObjectName ())) {
220             name = null;
221         }
222         wizard.setTargetName (name);
223     }
224
225     /** Helper implementation of WizardDescription.Panel for TemplateWizard.Panel2.
226     * Test whether the panel is finished and it is safe to proceed to the next one.
227     * If the panel is valid, the "Next" (or "Finish") button will be enabled.
228     * @return <code>null</code> if the user has entered satisfactory information
229     * or localized string describing the error.
230     */

231     String JavaDoc implIsValid () {
232         // test whether the selected folder on selected filesystem already exists
233
FileSystem fs = fileSystemRef.get();
234         if (locationFolder == null || fs == null)
235             return NbBundle.getMessage(TemplateWizard2.class, "MSG_fs_or_folder_does_not_exist"); // NOI18N
236

237         // target filesystem should be writable
238
if (fileSystemRef.get().isReadOnly())
239             return NbBundle.getMessage(TemplateWizard2.class, "MSG_fs_is_readonly"); // NOI18N
240

241         if (locationFolder == null) locationFolder = DataFolder.findFolder (fs.getRoot());
242         
243         // test whether the selected name already exists
244
StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
245         sb.append (locationFolder.getPrimaryFile ().getPath ());
246         sb.append ("/");
247         sb.append (newObjectName.getText ());
248         if ("" != extension) { // NOI18N
249
sb.append ('.');
250             sb.append (extension);
251         }
252         
253         FileObject f = fs.findResource (sb.toString ());
254         if (f != null) {
255             return NbBundle.getMessage(TemplateWizard2.class, "MSG_file_already_exist", sb.toString()); // NOI18N
256
}
257
258         if ((Utilities.isWindows () || (Utilities.getOperatingSystem () == Utilities.OS_OS2))) {
259             if (TemplateWizard.checkCaseInsensitiveName (locationFolder.getPrimaryFile (), newObjectName.getText (), extension)) {
260                 return NbBundle.getMessage(TemplateWizard2.class, "MSG_file_already_exist", newObjectName.getText ()); // NOI18N
261
}
262         }
263
264         // all ok
265
return null;
266     }
267     
268     /** Gives notification that an attribute or set of attributes changed.
269      *
270      * @param e the document event
271      *
272      */

273     public void changedUpdate(DocumentEvent e) {
274         if (e.getDocument () == newObjectName.getDocument ()) {
275             SwingUtilities.invokeLater (new Updater ());
276         }
277     }
278     
279     /** Gives notification that there was an insert into the document. The
280      * range given by the DocumentEvent bounds the freshly inserted region.
281      *
282      * @param e the document event
283      *
284      */

285     public void insertUpdate(DocumentEvent e) {
286         changedUpdate (e);
287     }
288     
289     /** Gives notification that a portion of the document has been
290      * removed. The range is given in terms of what the view last
291      * saw (that is, before updating sticky positions).
292      *
293      * @param e the document event
294      *
295      */

296     public void removeUpdate(DocumentEvent e) {
297         // so just check the name
298
if (e.getDocument () == newObjectName.getDocument ()) {
299             SwingUtilities.invokeLater (new Updater ());
300         }
301     }
302     
303     /** Does the update called from changedUpdate, insertUpdate and
304      * removeUpdate methods.
305      */

306     private class Updater implements Runnable JavaDoc {
307         Updater() {}
308         public void run () {
309             if (newObjectName.getText().equals ("")) { // NOI18N
310
setNewObjectName (""); // NOI18N
311
}
312
313             fireStateChanged ();
314         }
315     }
316
317     /** Request focus.
318     */

319     public void requestFocus () {
320         newObjectName.requestFocus();
321         newObjectName.selectAll ();
322     }
323
324     /** Sets the class name to some reasonable value.
325     * @param name the name to set the name to
326     */

327     private void setNewObjectName (String JavaDoc name) {
328         String JavaDoc n = name;
329         if (name == null || name.length () == 0) {
330             n = defaultNewObjectName ();
331         }
332
333         newObjectName.getDocument().removeDocumentListener(this);
334         newObjectName.setText (n);
335         newObjectName.getDocument().addDocumentListener(this);
336
337         if (name == null || name.length () == 0) {
338             newObjectName.selectAll ();
339         }
340     }
341
342     /** Fires info to listener.
343     */

344     private void fireStateChanged () {
345         if (listener != null) {
346             listener.stateChanged (new ChangeEvent (this));
347         }
348     }
349     
350     // Variables declaration - do not modify//GEN-BEGIN:variables
351
private org.openide.explorer.propertysheet.PropertyPanel dataFolderPanel;
352     private javax.swing.JLabel JavaDoc jLabel1;
353     private javax.swing.JPanel JavaDoc namePanel;
354     private javax.swing.JTextField JavaDoc newObjectName;
355     // End of variables declaration//GEN-END:variables
356

357     public void setLocationFolder (DataFolder fd) {
358         if (locationFolder == fd)
359             return ;
360         if (locationFolder != null && locationFolder.equals (fd))
361             return ;
362         DataFolder oldLocation = locationFolder;
363         locationFolder = fd;
364         firePropertyChange (PROP_LOCATION_FOLDER, oldLocation, locationFolder);
365         if (fd != null) {
366             try {
367                 fileSystemRef = new WeakReference<FileSystem>(fd.getPrimaryFile().getFileSystem());
368             } catch (org.openide.filesystems.FileStateInvalidException fsie) {
369                 fileSystemRef = new WeakReference<FileSystem>(null);
370             }
371         }
372         fireStateChanged ();
373     }
374     
375     public DataFolder getLocationFolder () {
376         return locationFolder;
377     }
378     
379 }
380
Popular Tags