KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 import java.io.IOException JavaDoc;
24 import javax.swing.event.ChangeListener JavaDoc;
25 import org.openide.WizardDescriptor;
26 import org.openide.filesystems.FileObject;
27 import org.openide.util.*;
28
29 /** Implementaion of WizardDescriptor.Panel that can be used in create from template.
30  *
31  * @author Jiri Rechtacek
32  */

33 final class NewObjectWizardPanel implements WizardDescriptor.FinishablePanel<WizardDescriptor> {
34     private NewObjectPanel newObjectPanelUI;
35     /** listener to changes in the wizard */
36     private ChangeListener JavaDoc listener;
37     /** a folder in which will be new object created */
38     DataFolder targetFolder;
39     /** File extension of the template and of the created file -
40      * it is used to test whether file already exists.
41      */

42     private String JavaDoc extension;
43     
44     private TemplateWizard wizard;
45     
46     private NewObjectPanel getPanelUI () {
47         if (newObjectPanelUI == null) {
48             newObjectPanelUI = new NewObjectPanel ();
49             newObjectPanelUI.addChangeListener (listener);
50         }
51         return newObjectPanelUI;
52     }
53     
54     /** Add a listener to changes of the panel's validity.
55     * @param l the listener to add
56     * @see #isValid
57     */

58     public void addChangeListener (ChangeListener JavaDoc l) {
59         if (listener != null) throw new IllegalStateException JavaDoc ();
60         if (newObjectPanelUI != null)
61             newObjectPanelUI.addChangeListener (l);
62         listener = l;
63     }
64
65     /** Remove a listener to changes of the panel's validity.
66     * @param l the listener to remove
67     */

68     public void removeChangeListener (ChangeListener JavaDoc l) {
69         listener = null;
70         if (newObjectPanelUI != null)
71             newObjectPanelUI.removeChangeListener (l);
72     }
73
74     /** Get the component displayed in this panel.
75      *
76      * Note; method can be called from any thread, but not concurrently
77      * with other methods of this interface.
78      *
79      * @return the UI component of this wizard panel
80      *
81      */

82     public java.awt.Component JavaDoc getComponent() {
83         return getPanelUI ();
84     }
85     
86     /** Help for this panel.
87     * @return the help or <code>null</code> if no help is supplied
88     */

89     public org.openide.util.HelpCtx getHelp () {
90         return new HelpCtx (NewObjectPanel.class);
91     }
92
93     /** Test whether the panel is finished and it is safe to proceed to the next one.
94     * If the panel is valid, the "Next" (or "Finish") button will be enabled.
95     * <p>The Attribute "isRemoteAndSlow" will be checked for on the targetFolder,
96     * and if it is found and <b>true</b>, then
97     * targetFolder.getPrimaryFile().getFileObject will NOT be called.</p>
98     * @return <code>true</code> if the user has entered satisfactory information
99     */

100     public boolean isValid () {
101         String JavaDoc errorMsg = null;
102         boolean isOK = true;
103         // target filesystem should be writable
104
if (!targetFolder.getPrimaryFile ().canWrite ()) {
105             errorMsg = NbBundle.getMessage(TemplateWizard2.class, "MSG_fs_is_readonly");
106             isOK = false;
107         }
108         if (isOK) {
109             Object JavaDoc obj = targetFolder.getPrimaryFile().getAttribute( "isRemoteAndSlow" );//NOI18N
110
boolean makeFileExistsChecks = true;
111             if( obj instanceof Boolean JavaDoc )
112                 makeFileExistsChecks = ! ((Boolean JavaDoc) obj).booleanValue();
113             if( makeFileExistsChecks ) {
114                 // test whether the selected name already exists
115
FileObject f = targetFolder.getPrimaryFile().getFileObject(getPanelUI ().getNewObjectName (), extension);
116                 if (f != null) {
117                     errorMsg = NbBundle.getMessage(TemplateWizard2.class, "MSG_file_already_exist", f.getNameExt()); //NOI18N
118
isOK = false;
119                 }
120                 if ((Utilities.isWindows () || (Utilities.getOperatingSystem () == Utilities.OS_OS2))) {
121                     if (TemplateWizard.checkCaseInsensitiveName (targetFolder.getPrimaryFile (), getPanelUI ().getNewObjectName (), extension)) {
122                         errorMsg = NbBundle.getMessage(TemplateWizard2.class, "MSG_file_already_exist", getPanelUI ().getNewObjectName ()); // NOI18N
123
isOK = false;
124                     }
125                 }
126                 
127             }
128         }
129         wizard.putProperty("WizardPanel_errorMessage", errorMsg);//NOI18N
130
return isOK;
131     }
132     
133     /** Provides the wizard panel with the current data--either
134      * the default data or already-modified settings, if the user used the previous and/or next buttons.
135      * This method can be called multiple times on one instance of <code>WizardDescriptor.Panel</code>.
136      * @param settings the object representing wizard panel state, as originally supplied to {@link WizardDescriptor#WizardDescriptor(WizardDescriptor.Iterator,Object)}
137      */

138     public void readSettings(WizardDescriptor settings) {
139         this.wizard = (TemplateWizard)settings;
140         DataObject template = wizard.getTemplate ();
141         if (template != null) {
142             extension = template.getPrimaryFile().getExt();
143         }
144         
145         try {
146             targetFolder = wizard.getTargetFolder();
147         } catch (IOException JavaDoc x) {
148             Exceptions.printStackTrace(x);
149         }
150
151     }
152     
153     /** Provides the wizard panel with the opportunity to update the
154      * settings with its current customized state.
155      * Rather than updating its settings with every change in the GUI, it should collect them,
156      * and then only save them when requested to by this method.
157      * Also, the original settings passed to {@link #readSettings} should not be modified (mutated);
158      * rather, the (copy) passed in here should be mutated according to the collected changes.
159      * This method can be called multiple times on one instance of <code>WizardDescriptor.Panel</code>.
160      * @param settings the object representing a settings of the wizard
161      */

162     public void storeSettings(WizardDescriptor settings) {
163         String JavaDoc name = getPanelUI ().getNewObjectName ();
164         if (name.equals (NewObjectPanel.defaultNewObjectName ())) {
165             name = null;
166         }
167         if (wizard != null) {
168             wizard.setTargetName (name);
169             wizard = null;
170         }
171         
172     }
173     
174     public boolean isFinishPanel () {
175         return true;
176     }
177     
178 }
179
Popular Tags