001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.compapp.projects.wizard;
021:
022: import java.awt.Component;
023: import java.io.File;
024: import java.io.IOException;
025: import java.util.ArrayList;
026: import java.util.Iterator;
027: import java.util.List;
028: import javax.swing.event.ChangeEvent;
029: import javax.swing.event.ChangeListener;
030: import org.netbeans.api.project.Project;
031: import org.netbeans.api.project.SourceGroup;
032: import org.netbeans.spi.project.ui.templates.support.Templates;
033: import org.openide.WizardDescriptor;
034: import org.openide.filesystems.FileObject;
035: import org.openide.filesystems.FileUtil;
036: import org.openide.util.HelpCtx;
037: import org.openide.util.NbBundle;
038:
039: /**
040: *
041: * @author Tientien Li
042: */
043: final class SimpleTargetChooserPanel implements WizardDescriptor.Panel,
044: ChangeListener {
045:
046: private final List/*<ChangeListener>*/listeners = new ArrayList();
047: private SimpleTargetChooserPanelGUI gui;
048:
049: private Project project;
050: private SourceGroup[] folders;
051: private WizardDescriptor.Panel bottomPanel;
052: private WizardDescriptor wizard;
053: private boolean isFolder;
054:
055: SimpleTargetChooserPanel(Project project, SourceGroup[] folders,
056: WizardDescriptor.Panel bottomPanel, boolean isFolder) {
057: this .folders = folders;
058: this .project = project;
059: this .bottomPanel = bottomPanel;
060: if (bottomPanel != null) {
061: bottomPanel.addChangeListener(this );
062: }
063: this .isFolder = isFolder;
064: this .gui = null;
065: }
066:
067: public Component getComponent() {
068: if (gui == null) {
069: gui = new SimpleTargetChooserPanelGUI(project, folders,
070: bottomPanel == null ? null : bottomPanel
071: .getComponent(), isFolder);
072: gui.addChangeListener(this );
073: }
074: return gui;
075: }
076:
077: public HelpCtx getHelp() {
078: if (bottomPanel != null) {
079: HelpCtx bottomHelp = bottomPanel.getHelp();
080: if (bottomHelp != null) {
081: return bottomHelp;
082: }
083: }
084:
085: //XXX
086: return null;
087:
088: }
089:
090: /** Checks if the given file name can be created in the target folder.
091: *
092: * @param targetFolder target folder (e.g. source group)
093: * @param folderName name of the folder relative to target folder
094: * @param newObjectName name of created file
095: * @param extension extension of created file
096: * @return localized error message or null if all right
097: */
098: private String canUseFileName(FileObject targetFolder,
099: String folderName, String newObjectName, String extension) {
100: if (extension != null && extension.length() > 0) {
101: StringBuffer sb = new StringBuffer();
102: sb.append(newObjectName);
103: sb.append('.'); // NOI18N
104: sb.append(extension);
105: newObjectName = sb.toString();
106: }
107:
108: String relFileName = folderName == null ? newObjectName
109: : folderName + "/" + newObjectName; // NOI18N
110:
111: // test whether the selected folder on selected filesystem already exists
112: if (targetFolder == null) {
113: return NbBundle.getMessage(SimpleTargetChooserPanel.class,
114: "MSG_fs_or_folder_does_not_exist"); // NOI18N
115: }
116:
117: // target filesystem should be writable
118: if (!targetFolder.canWrite()) {
119: return NbBundle.getMessage(SimpleTargetChooserPanel.class,
120: "MSG_fs_is_readonly"); // NOI18N
121: }
122: if (existFileName(targetFolder, relFileName)) {
123: return NbBundle.getMessage(SimpleTargetChooserPanel.class,
124: "MSG_file_already_exist", newObjectName); // NOI18N
125: }
126:
127: // all ok
128: return null;
129: }
130:
131: private static boolean existFileName(FileObject targetFolder,
132: String relFileName) {
133: boolean result = false;
134: File fileForTargetFolder = FileUtil.toFile(targetFolder);
135: if (fileForTargetFolder.exists()) {
136: result = new File(fileForTargetFolder, relFileName)
137: .exists();
138: } else {
139: result = targetFolder.getFileObject(relFileName) != null;
140: }
141:
142: return result;
143: }
144:
145: public boolean isValid() {
146: boolean ok = (gui != null && gui.getTargetName() != null && (bottomPanel == null || bottomPanel
147: .isValid()));
148:
149: if (!ok) {
150: return false;
151: }
152:
153: // check if the file name can be created
154: FileObject template = Templates.getTemplate(wizard);
155:
156: // String errorMessage = ProjectUtilities.canUseFileName (gui.getTargetGroup().getRootFolder(), gui.getTargetFolder(), gui.getTargetName(), template.getExt ());
157: String errorMessage = canUseFileName(gui.getTargetGroup()
158: .getRootFolder(), gui.getTargetFolder(), gui
159: .getTargetName(), template.getExt());
160: wizard.putProperty("WizardPanel_errorMessage", errorMessage); // NOI18N
161:
162: return errorMessage == null;
163: }
164:
165: public synchronized void addChangeListener(ChangeListener l) {
166: listeners.add(l);
167: }
168:
169: public synchronized void removeChangeListener(ChangeListener l) {
170: listeners.remove(l);
171: }
172:
173: private void fireChange() {
174: ChangeEvent e = new ChangeEvent(this );
175: List templist;
176: synchronized (this ) {
177: templist = new ArrayList(listeners);
178: }
179: Iterator it = templist.iterator();
180: while (it.hasNext()) {
181: ((ChangeListener) it.next()).stateChanged(e);
182: }
183: }
184:
185: public void readSettings(Object settings) {
186:
187: wizard = (WizardDescriptor) settings;
188:
189: if (gui == null) {
190: getComponent();
191: }
192:
193: // Try to preselect a folder
194: FileObject preselectedTarget = Templates
195: .getTargetFolder(wizard);
196: // Init values
197: gui
198: .initValues(Templates.getTemplate(wizard),
199: preselectedTarget);
200:
201: // XXX hack, TemplateWizard in final setTemplateImpl() forces new generic's title
202: // this name is used in NewFileWizard to modify the title
203: Object substitute = gui
204: .getClientProperty("NewFileWizard_Title"); // NOI18N
205: if (substitute != null) {
206: wizard.putProperty("NewFileWizard_Title", substitute); // NOI18N
207: }
208:
209: wizard
210: .putProperty(
211: "WizardPanel_contentData",
212: new String[] { // NOI18N
213: NbBundle
214: .getBundle(
215: SimpleTargetChooserPanel.class)
216: .getString(
217: "LBL_TemplatesPanel_Name"), // NOI18N
218: NbBundle
219: .getBundle(
220: SimpleTargetChooserPanel.class)
221: .getString(
222: "LBL_SimpleTargetChooserPanel_Name") }); // NOI18N
223:
224: if (bottomPanel != null) {
225: bottomPanel.readSettings(settings);
226: }
227: }
228:
229: public void storeSettings(Object settings) {
230: if (WizardDescriptor.PREVIOUS_OPTION
231: .equals(((WizardDescriptor) settings).getValue())) {
232: return;
233: }
234: if (isValid()) {
235: if (bottomPanel != null) {
236: bottomPanel.storeSettings(settings);
237: }
238:
239: Templates.setTargetFolder((WizardDescriptor) settings,
240: getTargetFolderFromGUI());
241: Templates.setTargetName((WizardDescriptor) settings, gui
242: .getTargetName());
243: }
244: ((WizardDescriptor) settings).putProperty(
245: "NewFileWizard_Title", null); // NOI18N
246: }
247:
248: public void stateChanged(ChangeEvent e) {
249: fireChange();
250: }
251:
252: private FileObject getTargetFolderFromGUI() {
253: FileObject rootFolder = gui.getTargetGroup().getRootFolder();
254: String folderName = gui.getTargetFolder();
255:
256: FileObject targetFolder;
257: if (folderName == null) {
258: targetFolder = rootFolder;
259: } else {
260: targetFolder = rootFolder.getFileObject(folderName);
261: }
262:
263: if (targetFolder == null) {
264: // XXX add deletion of the file in uninitalize ow the generic
265: try {
266: targetFolder = FileUtil.createFolder(rootFolder,
267: folderName);
268: } catch (IOException ioe) {
269: // XXX
270: // Can't create the folder
271: }
272: }
273:
274: return targetFolder;
275: }
276: }
|