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.beaninfo.editors;
043:
044: import java.awt.Component;
045: import java.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeListener;
047: import java.beans.PropertyEditorSupport;
048: import java.io.File;
049: import java.io.FilenameFilter;
050: import java.util.logging.Logger;
051: import javax.swing.JFileChooser;
052:
053: import org.openide.nodes.Node;
054: import org.openide.explorer.propertysheet.ExPropertyEditor;
055: import org.openide.explorer.propertysheet.PropertyEnv;
056: import org.openide.util.HelpCtx;
057: import org.openide.util.NbBundle;
058:
059: /**
060: * PropertyEditor for <code>[java.io.File</code>.
061: *
062: * @author David Strupl
063: */
064: public class FileArrayEditor extends PropertyEditorSupport implements
065: ExPropertyEditor, PropertyChangeListener {
066:
067: /** Openning mode.*/
068: private int mode = JFileChooser.FILES_AND_DIRECTORIES;
069:
070: /** Flag indicating whether to choose directories. Default value is <code>true</code>. */
071: private boolean directories = true;
072: /** Flag indicating whether to choose files. Default value is <code>true</code>. */
073: private boolean files = true;
074: /** Flag indicating whether to hide files marked as hidden. Default value is <code>false</code>. */
075: private boolean fileHiding = false;
076: /** Filter for files to show. */
077: private javax.swing.filechooser.FileFilter fileFilter;
078: /** Current firectory. */
079: private File currentDirectory;
080: /** Base directory to which to show relative path, if is set. */
081: private File baseDirectory;
082:
083: /** Cached chooser.
084: * If you don't cache it, MountIterator in core flickers and behaves weirdly,
085: * because apparently PropertyPanel will call getCustomEditor repeatedly and
086: * refresh the display each time.
087: * XXX MountIterator is dead so is this still necessary? -jglick
088: */
089: private JFileChooser chooser;
090:
091: /** whether the value can be edited -- default to true */
092: private boolean editable = true;
093:
094: /**
095: * This method is called by the IDE to pass
096: * the environment to the property editor.
097: * @param env Environment passed by the ide.
098: */
099: public void attachEnv(PropertyEnv env) {
100: // clearing to defaults
101: directories = true;
102: files = true;
103: fileFilter = null;
104: fileHiding = false;
105:
106: Object dirs = env.getFeatureDescriptor().getValue(
107: FileEditor.PROPERTY_SHOW_DIRECTORIES);
108: if (dirs instanceof Boolean) {
109: directories = ((Boolean) dirs).booleanValue();
110: } // XXX else if != null, warn
111: Object fil = env.getFeatureDescriptor().getValue(
112: FileEditor.PROPERTY_SHOW_FILES);
113: if (fil instanceof Boolean) {
114: files = ((Boolean) fil).booleanValue();
115: } // XXX else if != null, warn
116: Object filter = env.getFeatureDescriptor().getValue(
117: FileEditor.PROPERTY_FILTER);
118: if (filter instanceof FilenameFilter) {
119: fileFilter = new FileEditor.DelegatingFilenameFilter(
120: (FilenameFilter) filter);
121: } else if (filter instanceof javax.swing.filechooser.FileFilter) {
122: fileFilter = (javax.swing.filechooser.FileFilter) filter;
123: } else if (filter instanceof java.io.FileFilter) {
124: fileFilter = new FileEditor.DelegatingFileFilter(
125: (java.io.FileFilter) filter);
126: } // XXX else if != null, warn
127:
128: Object curDir = env.getFeatureDescriptor().getValue(
129: FileEditor.PROPERTY_CURRENT_DIR);
130: if (curDir instanceof File) {
131: currentDirectory = (File) curDir;
132: if (!currentDirectory.isDirectory()) {
133: Logger.getAnonymousLogger().warning(
134: "java.io.File will not accept currentDir="
135: + baseDirectory); // NOI18N
136: currentDirectory = null;
137: }
138: } // XXX else if != null, warn
139:
140: Object baseDir = env.getFeatureDescriptor().getValue(
141: FileEditor.PROPERTY_BASE_DIR);
142: if (baseDir instanceof File) {
143: baseDirectory = (File) baseDir;
144: // As baseDir accept only directories in their absolute form.
145: if (!baseDirectory.isDirectory()
146: || !baseDirectory.isAbsolute()) {
147: Logger.getAnonymousLogger().warning(
148: "java.io.File will not accept baseDir="
149: + baseDirectory); // NOI18N
150: baseDirectory = null;
151: }
152: } // XXX else if != null, warn
153: if (files) {
154: mode = directories ? JFileChooser.FILES_AND_DIRECTORIES
155: : JFileChooser.FILES_ONLY;
156: } else {
157: mode = directories ? JFileChooser.DIRECTORIES_ONLY
158: : JFileChooser.FILES_AND_DIRECTORIES; // both false, what now? XXX warn
159: }
160:
161: Object fileHide = env.getFeatureDescriptor().getValue(
162: FileEditor.PROPERTY_FILE_HIDING);
163: if (fileHide instanceof Boolean) {
164: fileHiding = ((Boolean) fileHide).booleanValue();
165: }
166:
167: if (env.getFeatureDescriptor() instanceof Node.Property) {
168: Node.Property prop = (Node.Property) env
169: .getFeatureDescriptor();
170: editable = prop.canWrite();
171: }
172: }
173:
174: /** Returns human readable form of the edited value.
175: * @return string reprezentation
176: */
177: public String getAsText() {
178: File[] file = (File[]) getValue();
179: if (file == null) {
180: return ""; // NOI18N
181: }
182: StringBuilder path = new StringBuilder("[");
183: for (int i = 0; i < file.length; i++) {
184: path.append(file[i].getPath());
185: }
186: // Dot is more friendly to people though Java itself would prefer blank:
187: if (file.length == 0)
188: path.append('.'); // NOI18N
189: return path.append(']').toString(); // NOI18N
190: }
191:
192: /** Parses the given string and should create a new instance of the
193: * edited object.
194: * @param str string reprezentation of the file (used as a parameter for File).
195: * @throws IllegalArgumentException If the given string cannot be parsed
196: */
197: public void setAsText(String str) throws IllegalArgumentException {
198: if (str == null) {
199: throw new IllegalArgumentException("null"); // NOI18N
200: }
201: if ("".equals(str)) { // NOI18N
202: setValue(null);
203: return;
204: }
205: // See getAsText.
206:
207: // [PENDING] Add tokenizer here !!!
208: }
209:
210: /** Custon editor.
211: * @return Returns custom editor component.
212: */
213: public Component getCustomEditor() {
214: if (!editable) {
215: return new StringCustomEditor(getAsText(), false);
216: }
217: if (chooser == null) {
218: chooser = FileEditor.createHackedFileChooser();
219: chooser.setMultiSelectionEnabled(true);
220:
221: Object vv = getValue();
222: File originalFile = null;
223:
224: if (vv instanceof File[]) {
225: File[] ofile = (File[]) vv;
226: if (ofile.length > 0) {
227: originalFile = ofile[0];
228: if (originalFile != null
229: && !originalFile.isAbsolute()
230: && baseDirectory != null) {
231: originalFile = new File(baseDirectory,
232: originalFile.getPath());
233: }
234: }
235: }
236: if (currentDirectory != null) {
237: chooser.setCurrentDirectory(currentDirectory);
238: } else if (originalFile != null
239: && originalFile.getParentFile() != null) {
240: chooser.setCurrentDirectory(originalFile
241: .getParentFile());
242: chooser.setSelectedFile(originalFile);
243: } else if (FileEditor.lastCurrentDir != null) {
244: chooser.setCurrentDirectory(FileEditor.lastCurrentDir);
245: }
246: chooser.setFileSelectionMode(mode);
247: if (fileFilter != null) {
248: chooser.setFileFilter(fileFilter);
249: }
250: switch (mode) {
251: case JFileChooser.FILES_AND_DIRECTORIES:
252: chooser
253: .setDialogTitle(getString("CTL_DialogTitleFilesAndDirs"));
254: break;
255: case JFileChooser.FILES_ONLY:
256: chooser
257: .setDialogTitle(getString("CTL_DialogTitleFiles"));
258: break;
259: case JFileChooser.DIRECTORIES_ONLY:
260: chooser
261: .setDialogTitle(getString("CTL_DialogTitleDirs"));
262: break;
263: }
264: chooser.setFileHidingEnabled(fileHiding);
265:
266: chooser.setControlButtonsAreShown(false);
267:
268: chooser.addPropertyChangeListener(this );
269:
270: HelpCtx.setHelpIDString(chooser, getHelpCtx().getHelpID());
271: }
272: return chooser;
273: }
274:
275: /** Implements PropertyEditor method.
276: * @return Returns true.
277: */
278: public boolean supportsCustomEditor() {
279: return true;
280: }
281:
282: /** Should create a string insertable to the newly generated source code.
283: * @return initialization string
284: */
285: public String getJavaInitializationString() {
286: File[] value = (File[]) getValue();
287: if (value == null) {
288: return "null"; // NOI18N
289: } else {
290: // [PENDING] not a full escape of filenames, but enough to at least
291: // handle normal Windows backslashes
292: StringBuilder retVal = new StringBuilder(
293: "new java.io.File[] { "); // NOI18N
294: for (int i = 0; i < value.length; i++) {
295: if (baseDirectory != null && !value[i].isAbsolute()) {
296: retVal.append("new java.io.File(") // NOI18N
297: .append(
298: FileEditor.stringify(baseDirectory
299: .getPath())).append(", ") // NOI18N
300: .append(
301: FileEditor.stringify(value[i]
302: .getPath())).append("), "); // NOI18N
303: } else {
304: retVal.append("new java.io.File(")
305: // NOI18N
306: .append(
307: FileEditor.stringify(value[i]
308: .getAbsolutePath()))
309: .append("), "); // NOI18N
310: }
311: }
312: return retVal.append(" }").toString();
313: }
314: }
315:
316: /** Gets help context. */
317: private HelpCtx getHelpCtx() {
318: return new HelpCtx(FileEditor.class);
319: }
320:
321: /** Gets localized string. Helper method. */
322: private static String getString(String key) {
323: return NbBundle.getBundle(FileArrayEditor.class).getString(key);
324: }
325:
326: /** Property change listaner attached to the JFileChooser chooser. */
327: public void propertyChange(PropertyChangeEvent e) {
328: if (e.getSource() instanceof JFileChooser) {
329: JFileChooser jfc = (JFileChooser) e.getSource();
330: if (mode == jfc.DIRECTORIES_ONLY
331: && jfc.DIRECTORY_CHANGED_PROPERTY.equals(e
332: .getPropertyName())) {
333: if (jfc.getSelectedFile() == null) {
334: File dir = jfc.getCurrentDirectory();
335: if (dir != null) {
336: setValue(new File[] { new File(dir
337: .getAbsolutePath()) });
338: return;
339: }
340: }
341: }
342: }
343:
344: if ((!JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(e
345: .getPropertyName()))
346: && (!JFileChooser.SELECTED_FILE_CHANGED_PROPERTY
347: .equals(e.getPropertyName()))) {
348: return;
349: }
350: if (!(e.getSource() instanceof JFileChooser)) {
351: return;
352: }
353: JFileChooser chooser = (JFileChooser) e.getSource();
354: File[] f = (File[]) chooser.getSelectedFiles();
355: if (f == null) {
356: return;
357: }
358:
359: if ((f.length == 0) && (chooser.getSelectedFile() != null)) {
360: f = new File[] { chooser.getSelectedFile() };
361: }
362:
363: for (int i = 0; i < f.length; i++) {
364: if (!files && f[i].isFile())
365: return;
366: if (!directories && f[i].isDirectory())
367: return;
368: }
369:
370: if (baseDirectory != null) {
371: for (int i = 0; i < f.length; i++) {
372: String rel = FileEditor.getChildRelativePath(
373: baseDirectory, f[i]);
374: if (rel != null) {
375: f[i] = new File(rel);
376: }
377: }
378: }
379:
380: File[] nf = new File[f.length];
381: for (int i = 0; i < f.length; i++) {
382: // the next line is
383: // workaround for JDK bug 4533419
384: // it should be returned back to f[i] after the
385: // mentioned bug is fixed in JDK.
386: nf[i] = new File(f[i].getAbsolutePath());
387: }
388: setValue(nf);
389:
390: FileEditor.lastCurrentDir = chooser.getCurrentDirectory();
391: }
392: }
|