2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
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]"
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.
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.
42
package org.openide.loaders;
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.*;
51
/** Implementaion of WizardDescriptor.Panel that can be used in create from template.
53
* @author Jiri Rechtacek
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.
64
private String extension;
66
private TemplateWizard wizard;
68
private NewObjectPanel getPanelUI () {
69
if (newObjectPanelUI == null) {
70
newObjectPanelUI = new NewObjectPanel ();
71
newObjectPanelUI.addChangeListener (listener);
73
return newObjectPanelUI;
76
/** Add a listener to changes of the panel's validity.
77
* @param l the listener to add
80
public void addChangeListener (ChangeListener l) {
81
if (listener != null) throw new IllegalStateException ();
82
if (newObjectPanelUI != null)
83
newObjectPanelUI.addChangeListener (l);
87
/** Remove a listener to changes of the panel's validity.
88
* @param l the listener to remove
90
public void removeChangeListener (ChangeListener l) {
92
if (newObjectPanelUI != null)
93
newObjectPanelUI.removeChangeListener (l);
96
/** Get the component displayed in this panel.
98
* Note; method can be called from any thread, but not concurrently
99
* with other methods of this interface.
101
* @return the UI component of this wizard panel
104
public java.awt.Component getComponent() {
105
return getPanelUI ();
108
/** Help for this panel.
109
* @return the help or <code>null</code> if no help is supplied
111
public org.openide.util.HelpCtx getHelp () {
112
return new HelpCtx (NewObjectPanel.class);
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
122
public boolean isValid () {
123
String errorMsg = null;
125
// target filesystem should be writable
126
if (!targetFolder.getPrimaryFile ().canWrite ()) {
127
errorMsg = NbBundle.getMessage(TemplateWizard2.class, "MSG_fs_is_readonly");
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);
139
errorMsg = NbBundle.getMessage(TemplateWizard2.class, "MSG_file_already_exist", f.getNameExt()); //NOI18N
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
151
wizard.putProperty("WizardPanel_errorMessage", errorMsg);//NOI18N
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)}
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();
168
targetFolder = wizard.getTargetFolder();
169
} catch (IOException x) {
170
Exceptions.printStackTrace(x);
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
184
public void storeSettings(WizardDescriptor settings) {
185
String name = getPanelUI ().getNewObjectName ();
186
if (name.equals (NewObjectPanel.defaultNewObjectName ())) {
189
if (wizard != null) {
190
wizard.setTargetName (name);
196
public boolean isFinishPanel () {