001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
004: */
005: package org.netbeans.modules.etl.ui.view.wizards;
006:
007: import org.netbeans.api.project.Project;
008: import org.netbeans.api.project.SourceGroup;
009:
010: import org.netbeans.spi.project.ui.templates.support.Templates;
011:
012: import org.openide.WizardDescriptor;
013:
014: import org.openide.filesystems.FileObject;
015: import org.openide.filesystems.FileUtil;
016:
017: import org.openide.util.HelpCtx;
018: import java.awt.Component;
019:
020: import java.io.File;
021: import java.io.IOException;
022:
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import javax.swing.event.ChangeEvent;
028: import javax.swing.event.ChangeListener;
029: import net.java.hulp.i18n.Logger;
030: import org.netbeans.modules.etl.logger.Localizer;
031: import org.netbeans.modules.etl.logger.LogUtil;
032:
033: /**
034: * DOCUMENT ME!
035: *
036: * @author Petr Hrebejk
037: */
038: final class SimpleTargetChooserPanel implements
039: WizardDescriptor.FinishablePanel, ChangeListener {
040:
041: private final List /*<ChangeListener>*/listeners = new ArrayList();
042: private SimpleTargetChooserPanelGUI gui;
043: private Project project;
044: private SourceGroup[] folders;
045: private WizardDescriptor.Panel bottomPanel;
046: private WizardDescriptor wizard;
047: private boolean isFolder;
048: private static transient final Logger mLogger = LogUtil
049: .getLogger(SimpleTargetChooserPanel.class.getName());
050: private static transient final Localizer mLoc = Localizer.get();
051:
052: /**
053: * Creates a new SimpleTargetChooserPanel object.
054: *
055: * @param project DOCUMENT ME!
056: * @param folders DOCUMENT ME!
057: * @param bottomPanel DOCUMENT ME!
058: * @param isFolder DOCUMENT ME!
059: */
060: SimpleTargetChooserPanel(Project project, SourceGroup[] folders,
061: WizardDescriptor.Panel bottomPanel, boolean isFolder) {
062: this .folders = folders;
063: this .project = project;
064: this .bottomPanel = bottomPanel;
065:
066: if (bottomPanel != null) {
067: bottomPanel.addChangeListener(this );
068: }
069:
070: this .isFolder = isFolder;
071: this .gui = null;
072: }
073:
074: public boolean isFinishPanel() {
075: return true;
076: }
077:
078: /**
079: * DOCUMENT ME!
080: *
081: * @return DOCUMENT ME!
082: */
083: public Component getComponent() {
084: if (gui == null) {
085: gui = new SimpleTargetChooserPanelGUI(project, folders,
086: (bottomPanel == null) ? null : bottomPanel
087: .getComponent(), isFolder);
088: gui.addChangeListener(this );
089: }
090:
091: return gui;
092: }
093:
094: /**
095: * DOCUMENT ME!
096: *
097: * @return DOCUMENT ME!
098: */
099: public HelpCtx getHelp() {
100: if (bottomPanel != null) {
101: HelpCtx bottomHelp = bottomPanel.getHelp();
102:
103: if (bottomHelp != null) {
104: return bottomHelp;
105: }
106: }
107:
108: //XXX
109: return null;
110: }
111:
112: /**
113: * Checks if the given file name can be created in the target folder.
114: *
115: * @param targetFolder target folder (e.g. source group)
116: * @param folderName name of the folder relative to target folder
117: * @param newObjectName name of created file
118: * @param extension extension of created file
119: *
120: * @return localized error message or null if all right
121: */
122: private String canUseFileName(FileObject targetFolder,
123: String folderName, String newObjectName, String extension) {
124: if ((extension != null) && (extension.length() > 0)) {
125: StringBuilder sb = new StringBuilder();
126: sb.append(newObjectName);
127: sb.append('.'); // NOI18N
128: sb.append(extension);
129: newObjectName = sb.toString();
130: }
131:
132: String relFileName = (folderName == null) ? newObjectName
133: : (folderName + "/" + newObjectName); // NOI18N
134:
135: // test whether the selected folder on selected filesystem already exists
136: if (targetFolder == null) {
137: String nbBundle1 = mLoc
138: .t("PRSR001: The target folder does not exist.");
139: return Localizer.parse(nbBundle1); // NOI18N
140: }
141:
142: // target filesystem should be writable
143: if (!targetFolder.canWrite()) {
144: String nbBundle2 = mLoc
145: .t("PRSR001: The target folder is read-only.");
146: return Localizer.parse(nbBundle2); // NOI18N
147: }
148:
149: if (existFileName(targetFolder, relFileName)) {
150: String nbBundle3 = mLoc.t(
151: "PRSR001: The file {0} already exists.",
152: newObjectName);
153: return Localizer.parse(nbBundle3); // NOI18N
154: }
155:
156: // all ok
157: return null;
158: }
159:
160: private static boolean existFileName(FileObject targetFolder,
161: String relFileName) {
162: boolean result = false;
163: File fileForTargetFolder = FileUtil.toFile(targetFolder);
164:
165: if (fileForTargetFolder.exists()) {
166: result = new File(fileForTargetFolder, relFileName)
167: .exists();
168: } else {
169: result = targetFolder.getFileObject(relFileName) != null;
170: }
171:
172: return result;
173: }
174:
175: /**
176: * DOCUMENT ME!
177: *
178: * @return DOCUMENT ME!
179: */
180: public boolean isValid() {
181: boolean ok = ((gui != null) && (gui.getTargetName() != null) && ((bottomPanel == null) || bottomPanel
182: .isValid()));
183:
184: if (!ok) {
185: return false;
186: }
187:
188: // check if the file name can be created
189: FileObject template = Templates.getTemplate(wizard);
190:
191: // String errorMessage = ProjectUtilities.canUseFileName (gui.getTargetGroup().getRootFolder(), gui.getTargetFolder(), gui.getTargetName(), template.getExt ());
192: String errorMessage = canUseFileName(gui.getTargetGroup()
193: .getRootFolder(), gui.getTargetFolder(), gui
194: .getTargetName(), template.getExt());
195: wizard.putProperty("WizardPanel_errorMessage", errorMessage); // NOI18N
196:
197: return errorMessage == null;
198: }
199:
200: /**
201: * DOCUMENT ME!
202: *
203: * @param l DOCUMENT ME!
204: */
205: public synchronized void addChangeListener(ChangeListener l) {
206: listeners.add(l);
207: }
208:
209: /**
210: * DOCUMENT ME!
211: *
212: * @param l DOCUMENT ME!
213: */
214: public synchronized void removeChangeListener(ChangeListener l) {
215: listeners.remove(l);
216: }
217:
218: private void fireChange() {
219: ChangeEvent e = new ChangeEvent(this );
220: List templist;
221:
222: synchronized (this ) {
223: templist = new ArrayList(listeners);
224: }
225:
226: Iterator it = templist.iterator();
227:
228: while (it.hasNext()) {
229: ((ChangeListener) it.next()).stateChanged(e);
230: }
231: }
232:
233: /**
234: * DOCUMENT ME!
235: *
236: * @param settings DOCUMENT ME!
237: */
238: public void readSettings(Object settings) {
239: wizard = (WizardDescriptor) settings;
240:
241: if (gui == null) {
242: getComponent();
243: }
244:
245: // Try to preselect a folder
246: FileObject preselectedTarget = Templates
247: .getTargetFolder(wizard);
248:
249: // Init values
250: gui
251: .initValues(Templates.getTemplate(wizard),
252: preselectedTarget);
253:
254: // XXX hack, TemplateWizard in final setTemplateImpl() forces new generic's title
255: // this name is used in NewFileWizard to modify the title
256: Object substitute = gui.getClientProperty("NewEtlWizard_Title"); // NOI18N
257:
258: String nbBundle1 = mLoc.t("PRSR001: Choose File Type");
259: String nbBundle2 = mLoc.t("PRSR001: Name and Location");
260: if (substitute != null) {
261: wizard.putProperty("NewEtlWizard_Title", substitute); // NOI18N
262: }
263: wizard.putProperty("WizardPanel_contentData", new String[] { // NOI18N
264: Localizer.parse(nbBundle1), // NOI18N
265: Localizer.parse(nbBundle2) }); // NOI18N
266:
267: if (bottomPanel != null) {
268: bottomPanel.readSettings(settings);
269: }
270: String collabName = (String) wizard
271: .getProperty(ETLCollaborationWizard.COLLABORATION_NAME);
272: if (!(collabName == null || "".equals(collabName))) {
273: gui.setDocumentName(collabName);
274: }
275: }
276:
277: /**
278: * DOCUMENT ME!
279: *
280: * @param settings DOCUMENT ME!
281: */
282: public void storeSettings(Object settings) {
283: if (WizardDescriptor.PREVIOUS_OPTION
284: .equals(((WizardDescriptor) settings).getValue())) {
285: return;
286: }
287:
288: if (isValid()) {
289: if (bottomPanel != null) {
290: bottomPanel.storeSettings(settings);
291: }
292:
293: Templates.setTargetFolder((WizardDescriptor) settings,
294: getTargetFolderFromGUI());
295: Templates.setTargetName((WizardDescriptor) settings, gui
296: .getTargetName());
297: }
298:
299: ((WizardDescriptor) settings).putProperty(
300: ETLCollaborationWizard.COLLABORATION_NAME, gui
301: .getTargetName());
302:
303: ((WizardDescriptor) settings).putProperty(
304: "NewDtelWizard_Title", null); // NOI18N
305: }
306:
307: /**
308: * DOCUMENT ME!
309: *
310: * @param e DOCUMENT ME!
311: */
312: public void stateChanged(ChangeEvent e) {
313: fireChange();
314: }
315:
316: private FileObject getTargetFolderFromGUI() {
317: FileObject rootFolder = gui.getTargetGroup().getRootFolder();
318: String folderName = gui.getTargetFolder();
319:
320: FileObject targetFolder;
321:
322: if (folderName == null) {
323: targetFolder = rootFolder;
324: } else {
325: targetFolder = rootFolder.getFileObject(folderName);
326: }
327:
328: if (targetFolder == null) {
329: // XXX add deletion of the file in uninitalize ow the generic
330: try {
331: targetFolder = FileUtil.createFolder(rootFolder,
332: folderName);
333: } catch (IOException ioe) {
334: // XXX
335: // Can't create the folder
336: }
337: }
338:
339: return targetFolder;
340: }
341: }
|