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: /*
021: *
022: * Copyright 2005 Sun Microsystems, Inc.
023: *
024: * Licensed under the Apache License, Version 2.0 (the "License");
025: * you may not use this file except in compliance with the License.
026: * You may obtain a copy of the License at
027: *
028: * http://www.apache.org/licenses/LICENSE-2.0
029: *
030: * Unless required by applicable law or agreed to in writing, software
031: * distributed under the License is distributed on an "AS IS" BASIS,
032: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
033: * See the License for the specific language governing permissions and
034: * limitations under the License.
035: *
036: */
037:
038: package org.netbeans.modules.jdbcwizard.wizards;
039:
040: import org.netbeans.api.project.Project;
041: import org.netbeans.api.project.SourceGroup;
042: import org.netbeans.spi.project.ui.templates.support.Templates;
043: import org.openide.WizardDescriptor;
044: import org.openide.filesystems.FileObject;
045: import org.openide.filesystems.FileUtil;
046: import org.openide.util.HelpCtx;
047: import org.openide.util.NbBundle;
048: import java.awt.Component;
049:
050: import java.io.File;
051: import java.io.IOException;
052: import java.util.ArrayList;
053: import java.util.Iterator;
054: import java.util.List;
055: import javax.swing.event.ChangeEvent;
056: import javax.swing.event.ChangeListener;
057:
058: /**
059: * DOCUMENT ME!
060: *
061: * @author
062: */
063: final class SimpleTargetChooserPanel implements
064: WizardDescriptor.FinishablePanel, ChangeListener {
065: private final List /* <ChangeListener> */listeners = new ArrayList();
066:
067: private SimpleTargetChooserPanelGUI gui;
068:
069: private Project project;
070:
071: private SourceGroup[] folders;
072:
073: private final WizardDescriptor.Panel bottomPanel;
074:
075: private WizardDescriptor wizard;
076:
077: private final boolean isFolder;
078:
079: /**
080: * Creates a new SimpleTargetChooserPanel object.
081: *
082: * @param project DOCUMENT ME!
083: * @param folders DOCUMENT ME!
084: * @param bottomPanel DOCUMENT ME!
085: * @param isFolder DOCUMENT ME!
086: */
087: SimpleTargetChooserPanel(Project project, SourceGroup[] folders,
088: WizardDescriptor.Panel bottomPanel, boolean isFolder) {
089: this .folders = folders;
090: this .project = project;
091: this .bottomPanel = bottomPanel;
092:
093: if (bottomPanel != null) {
094: bottomPanel.addChangeListener(this );
095: }
096:
097: this .isFolder = isFolder;
098: this .gui = null;
099: }
100:
101: public boolean isFinishPanel() {
102: return true;
103: }
104:
105: /**
106: * DOCUMENT ME!
107: *
108: * @return DOCUMENT ME!
109: */
110: public Component getComponent() {
111: if (this .gui == null) {
112: this .gui = new SimpleTargetChooserPanelGUI(project,
113: folders, this .bottomPanel == null ? null
114: : this .bottomPanel.getComponent(),
115: this .isFolder);
116: this .gui.addChangeListener(this );
117: }
118:
119: return this .gui;
120: }
121:
122: /**
123: * DOCUMENT ME!
124: *
125: * @return DOCUMENT ME!
126: */
127: public HelpCtx getHelp() {
128: return new HelpCtx(SimpleTargetChooserPanel.class);
129: }
130:
131: /**
132: * Checks if the given file name can be created in the target folder.
133: *
134: * @param targetFolder target folder (e.g. source group)
135: * @param folderName name of the folder relative to target folder
136: * @param newObjectName name of created file
137: * @param extension extension of created file
138: * @return localized error message or null if all right
139: */
140: private String canUseFileName(FileObject targetFolder,
141: String folderName, String newObjectName, String extension) {
142: if (extension != null && extension.length() > 0) {
143: StringBuffer sb = new StringBuffer();
144: sb.append(newObjectName);
145: sb.append('.'); // NOI18N
146: sb.append(extension);
147: newObjectName = sb.toString();
148: }
149:
150: String relFileName = folderName == null ? newObjectName
151: : folderName + "/" + newObjectName; // NOI18N
152:
153: // test whether the selected folder on selected filesystem already exists
154: if (targetFolder == null) {
155: return NbBundle.getMessage(SimpleTargetChooserPanel.class,
156: "MSG_fs_or_folder_does_not_exist"); // NOI18N
157: }
158:
159: // target filesystem should be writable
160: if (!targetFolder.canWrite()) {
161: return NbBundle.getMessage(SimpleTargetChooserPanel.class,
162: "MSG_fs_is_readonly"); // NOI18N
163: }
164:
165: if (existFileName(targetFolder, relFileName)) {
166: return NbBundle.getMessage(SimpleTargetChooserPanel.class,
167: "MSG_file_already_exist", newObjectName); // NOI18N
168: }
169:
170: // all ok
171: return null;
172: }
173:
174: private static boolean existFileName(FileObject targetFolder,
175: String relFileName) {
176: boolean result = false;
177: File fileForTargetFolder = FileUtil.toFile(targetFolder);
178:
179: if (fileForTargetFolder.exists()) {
180: result = new File(fileForTargetFolder, relFileName)
181: .exists();
182: } else {
183: result = targetFolder.getFileObject(relFileName) != null;
184: }
185:
186: return result;
187: }
188:
189: /**
190: * DOCUMENT ME!
191: *
192: * @return DOCUMENT ME!
193: */
194: public boolean isValid() {
195: boolean ok = this .gui != null
196: && this .gui.getTargetName() != null
197: && (this .bottomPanel == null || this .bottomPanel
198: .isValid());
199:
200: if (!ok) {
201: return false;
202: }
203:
204: // check if the file name can be created
205: FileObject template = Templates.getTemplate(this .wizard);
206:
207: // String errorMessage = ProjectUtilities.canUseFileName
208: // (gui.getTargetGroup().getRootFolder(), gui.getTargetFolder(), gui.getTargetName(),
209: // template.getExt ());
210: final String errorMessage = canUseFileName(this .gui
211: .getTargetGroup().getRootFolder(), this .gui
212: .getTargetFolder(), this .gui.getTargetName(), template
213: .getExt());
214: this .wizard.putProperty("WizardPanel_errorMessage",
215: errorMessage); // NOI18N
216:
217: return errorMessage == null;
218: }
219:
220: /**
221: * DOCUMENT ME!
222: *
223: * @param l DOCUMENT ME!
224: */
225: public synchronized void addChangeListener(final ChangeListener l) {
226: this .listeners.add(l);
227: }
228:
229: /**
230: * DOCUMENT ME!
231: *
232: * @param l DOCUMENT ME!
233: */
234: public synchronized void removeChangeListener(final ChangeListener l) {
235: this .listeners.remove(l);
236: }
237:
238: private void fireChange() {
239: final ChangeEvent e = new ChangeEvent(this );
240: List templist;
241:
242: synchronized (this ) {
243: templist = new ArrayList(this .listeners);
244: }
245:
246: final Iterator it = templist.iterator();
247:
248: while (it.hasNext()) {
249: ((ChangeListener) it.next()).stateChanged(e);
250: }
251: }
252:
253: /**
254: * DOCUMENT ME!
255: *
256: * @param settings DOCUMENT ME!
257: */
258: public void readSettings(final Object settings) {
259: this .wizard = (WizardDescriptor) settings;
260:
261: if (this .gui == null) {
262: this .getComponent();
263: }
264:
265: // Try to preselect a folder
266: FileObject preselectedTarget = Templates
267: .getTargetFolder(this .wizard);
268:
269: // Init values
270: this .gui.initValues(Templates.getTemplate(this .wizard),
271: preselectedTarget);
272:
273: // XXX hack, TemplateWizard in final setTemplateImpl() forces new generic's title
274: // this name is used in NewFileWizard to modify the title
275: final Object substitute = this .gui
276: .getClientProperty("NewDtelWizard_Title"); // NOI18N
277:
278: if (substitute != null) {
279: this .wizard.putProperty("NewDtelWizard_Title", substitute); // NOI18N
280: }
281:
282: this .wizard
283: .putProperty(
284: "WizardPanel_contentData",
285: new String[] { // NOI18N
286:
287: NbBundle
288: .getBundle(
289: SimpleTargetChooserPanel.class)
290: .getString(
291: "LBL_TemplatesPanel_Name"), // NOI18N
292: NbBundle
293: .getBundle(
294: SimpleTargetChooserPanel.class)
295: .getString(
296: "LBL_SimpleTargetChooserPanel_Name") }); // NOI18N
297:
298: if (this .bottomPanel != null) {
299: this .bottomPanel.readSettings(settings);
300: }
301: }
302:
303: /**
304: * DOCUMENT ME!
305: *
306: * @param settings DOCUMENT ME!
307: */
308: public void storeSettings(final Object settings) {
309: if (WizardDescriptor.PREVIOUS_OPTION
310: .equals(((WizardDescriptor) settings).getValue())) {
311: return;
312: }
313:
314: if (this .isValid()) {
315: if (this .bottomPanel != null) {
316: this .bottomPanel.storeSettings(settings);
317: }
318:
319: Templates.setTargetFolder((WizardDescriptor) settings,
320: getTargetFolderFromGUI());
321: Templates.setTargetName((WizardDescriptor) settings,
322: this .gui.getTargetName());
323: }
324:
325: ((WizardDescriptor) settings).putProperty(
326: JDBCWizardContext.COLLABORATION_NAME, this .gui
327: .getTargetName());
328: ((WizardDescriptor) settings).putProperty(
329: JDBCWizardContext.TARGETFOLDER_PATH, (FileUtil
330: .toFile(getTargetFolderFromGUI()))
331: .getAbsolutePath());
332: ((WizardDescriptor) settings).putProperty(
333: "NewDtelWizard_Title", null); // NOI18N
334: }
335:
336: /**
337: * DOCUMENT ME!
338: *
339: * @param e DOCUMENT ME!
340: */
341: public void stateChanged(final ChangeEvent e) {
342: this .fireChange();
343: }
344:
345: private FileObject getTargetFolderFromGUI() {
346: FileObject rootFolder = gui.getTargetGroup().getRootFolder();
347: String folderName = gui.getTargetFolder();
348:
349: FileObject targetFolder;
350:
351: if (folderName == null) {
352: targetFolder = rootFolder;
353: } else {
354: targetFolder = rootFolder.getFileObject(folderName);
355: }
356:
357: if (targetFolder == null) {
358: // XXX add deletion of the file in uninitalize ow the generic
359: try {
360: targetFolder = FileUtil.createFolder(rootFolder,
361: folderName);
362: } catch (IOException ioe) {
363: // XXX
364: // Can't create the folder
365: }
366: }
367:
368: return targetFolder;
369: }
370: }
|