001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.project.ui;
043:
044: import java.awt.Component;
045: import java.io.IOException;
046: import javax.swing.event.ChangeEvent;
047: import javax.swing.event.ChangeListener;
048: import org.netbeans.api.project.Project;
049: import org.netbeans.api.project.SourceGroup;
050: import org.netbeans.spi.project.ui.templates.support.Templates;
051: import org.openide.ErrorManager;
052: import org.openide.WizardDescriptor;
053: import org.openide.filesystems.FileObject;
054: import org.openide.filesystems.FileUtil;
055: import org.openide.util.ChangeSupport;
056: import org.openide.util.HelpCtx;
057: import org.openide.util.NbBundle;
058:
059: /**
060: *
061: * @author Petr Hrebejk
062: */
063: final class SimpleTargetChooserPanel implements
064: WizardDescriptor.Panel<WizardDescriptor>, ChangeListener {
065:
066: private final ChangeSupport changeSupport = new ChangeSupport(this );
067: private SimpleTargetChooserPanelGUI gui;
068:
069: private Project project;
070: private SourceGroup[] folders;
071: private WizardDescriptor.Panel<WizardDescriptor> bottomPanel;
072: private WizardDescriptor wizard;
073: private boolean isFolder;
074:
075: SimpleTargetChooserPanel(Project project, SourceGroup[] folders,
076: WizardDescriptor.Panel<WizardDescriptor> bottomPanel,
077: boolean isFolder) {
078: this .folders = folders;
079: this .project = project;
080: this .bottomPanel = bottomPanel;
081: if (bottomPanel != null) {
082: bottomPanel.addChangeListener(this );
083: }
084: this .isFolder = isFolder;
085: this .gui = null;
086: }
087:
088: public Component getComponent() {
089: if (gui == null) {
090: gui = new SimpleTargetChooserPanelGUI(project, folders,
091: bottomPanel == null ? null : bottomPanel
092: .getComponent(), isFolder);
093: gui.addChangeListener(this );
094: }
095: return gui;
096: }
097:
098: public HelpCtx getHelp() {
099: if (bottomPanel != null) {
100: HelpCtx bottomHelp = bottomPanel.getHelp();
101: if (bottomHelp != null) {
102: return bottomHelp;
103: }
104: }
105:
106: //XXX
107: return null;
108:
109: }
110:
111: public boolean isValid() {
112: boolean ok = (gui != null && gui.getTargetName() != null && (bottomPanel == null || bottomPanel
113: .isValid()));
114:
115: if (!ok) {
116: return false;
117: }
118:
119: // check if the file name can be created
120: FileObject template = Templates.getTemplate(wizard);
121:
122: String errorMessage = ProjectUtilities.canUseFileName(gui
123: .getTargetGroup().getRootFolder(), gui
124: .getTargetFolder(), gui.getTargetName(), template
125: .getExt(), isFolder);
126: wizard.putProperty("WizardPanel_errorMessage", errorMessage); // NOI18N
127:
128: return errorMessage == null;
129: }
130:
131: public void addChangeListener(ChangeListener l) {
132: changeSupport.addChangeListener(l);
133: }
134:
135: public void removeChangeListener(ChangeListener l) {
136: changeSupport.removeChangeListener(l);
137: }
138:
139: public void readSettings(WizardDescriptor settings) {
140:
141: wizard = settings;
142:
143: if (gui == null) {
144: getComponent();
145: }
146:
147: // Try to preselect a folder
148: FileObject preselectedTarget = Templates
149: .getTargetFolder(wizard);
150: if (preselectedTarget == null) {
151: preselectedTarget = project.getProjectDirectory();
152: }
153: // Try to preserve the already entered target name
154: String targetName = Templates.getTargetName(wizard);
155: // Init values
156: gui.initValues(Templates.getTemplate(wizard),
157: preselectedTarget, targetName);
158:
159: // XXX hack, TemplateWizard in final setTemplateImpl() forces new wizard's title
160: // this name is used in NewFileWizard to modify the title
161: Object substitute = gui
162: .getClientProperty("NewFileWizard_Title"); // NOI18N
163: if (substitute != null) {
164: wizard.putProperty("NewFileWizard_Title", substitute); // NOI18N
165: }
166:
167: wizard
168: .putProperty(
169: "WizardPanel_contentData",
170: new String[] { // NOI18N
171: NbBundle
172: .getBundle(
173: SimpleTargetChooserPanel.class)
174: .getString(
175: "LBL_TemplatesPanel_Name"), // NOI18N
176: NbBundle
177: .getBundle(
178: SimpleTargetChooserPanel.class)
179: .getString(
180: "LBL_SimpleTargetChooserPanel_Name") }); // NOI18N
181:
182: if (bottomPanel != null) {
183: bottomPanel.readSettings(settings);
184: }
185: }
186:
187: public void storeSettings(WizardDescriptor settings) {
188: if (WizardDescriptor.PREVIOUS_OPTION
189: .equals(settings.getValue())) {
190: return;
191: }
192: if (!settings.getValue().equals(WizardDescriptor.CANCEL_OPTION)
193: && isValid()) {
194: if (bottomPanel != null) {
195: bottomPanel.storeSettings(settings);
196: }
197:
198: FileObject template = Templates.getTemplate(wizard);
199:
200: String name = gui.getTargetName();
201: if (name.indexOf('/') > 0) { // NOI18N
202: name = name.substring(name.lastIndexOf('/') + 1);
203: }
204:
205: FileObject fo = getTargetFolderFromGUI();
206: try {
207: Templates.setTargetFolder(settings, fo);
208: } catch (IllegalArgumentException iae) {
209: ErrorManager.getDefault()
210: .annotate(
211: iae,
212: ErrorManager.EXCEPTION,
213: null,
214: NbBundle.getMessage(
215: SimpleTargetChooserPanel.class,
216: "MSG_Cannot_Create_Folder", gui
217: .getTargetFolder()),
218: null, null);
219: throw iae;
220: }
221: Templates.setTargetName(settings, name);
222: }
223: settings.putProperty("NewFileWizard_Title", null); // NOI18N
224: }
225:
226: public void stateChanged(ChangeEvent e) {
227: changeSupport.fireChange();
228: }
229:
230: private FileObject getTargetFolderFromGUI() {
231: FileObject rootFolder = gui.getTargetGroup().getRootFolder();
232: String folderName = gui.getTargetFolder();
233: String newObject = gui.getTargetName();
234:
235: if (newObject.indexOf('/') > 0) { // NOI18N
236: String path = newObject.substring(0, newObject
237: .lastIndexOf('/')); // NOI18N
238: folderName = folderName == null || "".equals(folderName) ? path
239: : folderName + '/' + path; // NOI18N
240: }
241:
242: FileObject targetFolder;
243: if (folderName == null) {
244: targetFolder = rootFolder;
245: } else {
246: targetFolder = rootFolder.getFileObject(folderName);
247: }
248:
249: if (targetFolder == null) {
250: // XXX add deletion of the file in uninitalize ow the wizard
251: try {
252: targetFolder = FileUtil.createFolder(rootFolder,
253: folderName);
254: } catch (IOException ioe) {
255: // XXX
256: // Can't create the folder
257: }
258: }
259:
260: return targetFolder;
261: }
262: }
|