~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to openide/loaders/src/org/openide/loaders/NewObjectWizardPanel.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 /*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.openide.loaders;
 
43
 
 
44
 
 
45
import java.io.IOException;
 
46
import javax.swing.event.ChangeListener;
 
47
import org.openide.WizardDescriptor;
 
48
import org.openide.filesystems.FileObject;
 
49
import org.openide.util.*;
 
50
 
 
51
/** Implementaion of WizardDescriptor.Panel that can be used in create from template.
 
52
 *
 
53
 * @author Jiri Rechtacek
 
54
 */
 
55
final class NewObjectWizardPanel implements WizardDescriptor.FinishablePanel<WizardDescriptor> {
 
56
    private NewObjectPanel newObjectPanelUI;
 
57
    /** listener to changes in the wizard */
 
58
    private ChangeListener listener;
 
59
    /** a folder in which will be new object created */
 
60
    DataFolder targetFolder;
 
61
    /** File extension of the template and of the created file -
 
62
     * it is used to test whether file already exists.
 
63
     */
 
64
    private String extension;
 
65
    
 
66
    private TemplateWizard wizard;
 
67
    
 
68
    private NewObjectPanel getPanelUI () {
 
69
        if (newObjectPanelUI == null) {
 
70
            newObjectPanelUI = new NewObjectPanel ();
 
71
            newObjectPanelUI.addChangeListener (listener);
 
72
        }
 
73
        return newObjectPanelUI;
 
74
    }
 
75
    
 
76
    /** Add a listener to changes of the panel's validity.
 
77
    * @param l the listener to add
 
78
    * @see #isValid
 
79
    */
 
80
    public void addChangeListener (ChangeListener l) {
 
81
        if (listener != null) throw new IllegalStateException ();
 
82
        if (newObjectPanelUI != null)
 
83
            newObjectPanelUI.addChangeListener (l);
 
84
        listener = l;
 
85
    }
 
86
 
 
87
    /** Remove a listener to changes of the panel's validity.
 
88
    * @param l the listener to remove
 
89
    */
 
90
    public void removeChangeListener (ChangeListener l) {
 
91
        listener = null;
 
92
        if (newObjectPanelUI != null)
 
93
            newObjectPanelUI.removeChangeListener (l);
 
94
    }
 
95
 
 
96
    /** Get the component displayed in this panel.
 
97
     *
 
98
     * Note; method can be called from any thread, but not concurrently
 
99
     * with other methods of this interface.
 
100
     *
 
101
     * @return the UI component of this wizard panel
 
102
     *
 
103
     */
 
104
    public java.awt.Component getComponent() {
 
105
        return getPanelUI ();
 
106
    }
 
107
    
 
108
    /** Help for this panel.
 
109
    * @return the help or <code>null</code> if no help is supplied
 
110
    */
 
111
    public org.openide.util.HelpCtx getHelp () {
 
112
        return new HelpCtx (NewObjectPanel.class);
 
113
    }
 
114
 
 
115
    /** Test whether the panel is finished and it is safe to proceed to the next one.
 
116
    * If the panel is valid, the "Next" (or "Finish") button will be enabled.
 
117
    * <p>The Attribute "isRemoteAndSlow" will be checked for on the targetFolder,
 
118
    * and if it is found and <b>true</b>, then 
 
119
    * targetFolder.getPrimaryFile().getFileObject will NOT be called.</p>
 
120
    * @return <code>true</code> if the user has entered satisfactory information
 
121
    */
 
122
    public boolean isValid () {
 
123
        String errorMsg = null;
 
124
        boolean isOK = true;
 
125
        // target filesystem should be writable
 
126
        if (!targetFolder.getPrimaryFile ().canWrite ()) {
 
127
            errorMsg = NbBundle.getMessage(TemplateWizard2.class, "MSG_fs_is_readonly");
 
128
            isOK = false;
 
129
        }
 
130
        if (isOK) {
 
131
            Object obj = targetFolder.getPrimaryFile().getAttribute( "isRemoteAndSlow" );//NOI18N
 
132
            boolean makeFileExistsChecks = true;
 
133
            if( obj instanceof Boolean )
 
134
                makeFileExistsChecks = ! ((Boolean) obj).booleanValue();
 
135
            if( makeFileExistsChecks ) {
 
136
                // test whether the selected name already exists            
 
137
                FileObject f = targetFolder.getPrimaryFile().getFileObject(getPanelUI ().getNewObjectName (), extension);
 
138
                if (f != null) {
 
139
                    errorMsg = NbBundle.getMessage(TemplateWizard2.class, "MSG_file_already_exist", f.getNameExt()); //NOI18N
 
140
                    isOK = false;
 
141
                }
 
142
                if ((Utilities.isWindows () || (Utilities.getOperatingSystem () == Utilities.OS_OS2))) {
 
143
                    if (TemplateWizard.checkCaseInsensitiveName (targetFolder.getPrimaryFile (), getPanelUI ().getNewObjectName (), extension)) {
 
144
                        errorMsg = NbBundle.getMessage(TemplateWizard2.class, "MSG_file_already_exist", getPanelUI ().getNewObjectName ()); // NOI18N
 
145
                        isOK = false;
 
146
                    }
 
147
                }
 
148
                
 
149
            }
 
150
        }
 
151
        wizard.putProperty("WizardPanel_errorMessage", errorMsg);//NOI18N
 
152
        return isOK;
 
153
    }
 
154
    
 
155
    /** Provides the wizard panel with the current data--either
 
156
     * the default data or already-modified settings, if the user used the previous and/or next buttons.
 
157
     * This method can be called multiple times on one instance of <code>WizardDescriptor.Panel</code>.
 
158
     * @param settings the object representing wizard panel state, as originally supplied to {@link WizardDescriptor#WizardDescriptor(WizardDescriptor.Iterator,Object)}
 
159
     */
 
160
    public void readSettings(WizardDescriptor settings) {
 
161
        this.wizard = (TemplateWizard)settings;
 
162
        DataObject template = wizard.getTemplate ();
 
163
        if (template != null) {
 
164
            extension = template.getPrimaryFile().getExt();
 
165
        }
 
166
        
 
167
        try {
 
168
            targetFolder = wizard.getTargetFolder();
 
169
        } catch (IOException x) {
 
170
            Exceptions.printStackTrace(x);
 
171
        }
 
172
 
 
173
    }
 
174
    
 
175
    /** Provides the wizard panel with the opportunity to update the
 
176
     * settings with its current customized state.
 
177
     * Rather than updating its settings with every change in the GUI, it should collect them,
 
178
     * and then only save them when requested to by this method.
 
179
     * Also, the original settings passed to {@link #readSettings} should not be modified (mutated);
 
180
     * rather, the (copy) passed in here should be mutated according to the collected changes.
 
181
     * This method can be called multiple times on one instance of <code>WizardDescriptor.Panel</code>.
 
182
     * @param settings the object representing a settings of the wizard
 
183
     */
 
184
    public void storeSettings(WizardDescriptor settings) {
 
185
        String name = getPanelUI ().getNewObjectName ();
 
186
        if (name.equals (NewObjectPanel.defaultNewObjectName ())) {
 
187
            name = null;
 
188
        }
 
189
        if (wizard != null) {
 
190
            wizard.setTargetName (name);
 
191
            wizard = null;
 
192
        }
 
193
        
 
194
    }
 
195
    
 
196
    public boolean isFinishPanel () {
 
197
        return true;
 
198
    }
 
199
    
 
200
}