2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.beaninfo.editors;
44
import java.awt.event.ActionEvent;
45
import java.awt.event.KeyEvent;
46
import java.awt.Component;
47
import java.awt.Container;
48
import java.awt.Dialog;
49
import java.awt.KeyboardFocusManager;
50
import java.beans.PropertyChangeEvent;
51
import java.beans.PropertyChangeListener;
52
import java.beans.PropertyEditorSupport;
54
import java.io.FilenameFilter;
55
import java.util.logging.Level;
56
import java.util.logging.Logger;
57
import javax.swing.AbstractAction;
58
import javax.swing.AbstractButton;
59
import javax.swing.Action;
60
import javax.swing.JComboBox;
61
import javax.swing.JComponent;
62
import javax.swing.JFileChooser;
63
import javax.swing.JScrollBar;
64
import javax.swing.KeyStroke;
65
import org.openide.nodes.Node;
66
import org.openide.explorer.propertysheet.ExPropertyEditor;
67
import org.openide.explorer.propertysheet.PropertyEnv;
68
import org.openide.util.HelpCtx;
69
import org.openide.util.NbBundle;
72
* PropertyEditor for <code>java.io.File</code>.
74
* @author Jaroslav Tulach, David Strupl, Peter Zavadsky, Jesse Glick
76
public class FileEditor extends PropertyEditorSupport implements ExPropertyEditor, PropertyChangeListener {
78
/** Name of the property obtained from the feature descriptor.*/
79
static final String PROPERTY_SHOW_DIRECTORIES = "directories"; //NOI18N
81
/** Name of the property obtained from the feature descriptor.*/
82
static final String PROPERTY_SHOW_FILES = "files"; //NOI18N
84
/** Name of the property obtained from the feature descriptor.*/
85
static final String PROPERTY_FILTER = "filter"; //NOI18N
87
/** Name of the property obtained from the feature descriptor.*/
88
static final String PROPERTY_CURRENT_DIR = "currentDir"; //NOI18N
90
/** Name of the property obtained from the feature descriptor. */
91
static final String PROPERTY_BASE_DIR = "baseDir"; // NOI18N
93
/** Name of the property obtained from the feature descriptor. */
94
static final String PROPERTY_FILE_HIDING = "file_hiding"; // NOI18N
97
private int mode = JFileChooser.FILES_AND_DIRECTORIES;
99
/** Flag indicating whether to choose directories. Default value is <code>true</code>. */
100
private boolean directories = true;
101
/** Flag indicating whether to choose files. Default value is <code>true</code>. */
102
private boolean files = true;
103
/** Flag indicating whether to hide files marked as hidden. Default value is <code>false</code>. */
104
private boolean fileHiding = false;
105
/** Filter for files to show. */
106
private javax.swing.filechooser.FileFilter fileFilter;
107
/** Current firectory. */
108
private File currentDirectory;
109
/** Base directory to which to show relative path, if is set. */
110
private File baseDirectory;
112
/** Caches last used directory. */
113
static File lastCurrentDir;
115
private PropertyEnv env;
118
* If you don't cache it, MountIterator in core flickers and behaves weirdly,
119
* because apparently PropertyPanel will call getCustomEditor repeatedly and
120
* refresh the display each time.
121
* XXX MountIterator is dead so is this still necessary? -jglick
123
private JFileChooser chooser;
125
/** whether the value can be edited -- default to true */
126
private boolean editable = true;
129
* This method is called by the IDE to pass
130
* the environment to the property editor.
131
* @param env Environment passed by the ide.
133
public void attachEnv(PropertyEnv env) {
136
// clearing to defaults
142
Object dirs = env.getFeatureDescriptor().getValue(PROPERTY_SHOW_DIRECTORIES);
143
if (dirs instanceof Boolean) {
144
directories = ((Boolean)dirs).booleanValue();
145
} // XXX else if != null, warn
146
Object fil = env.getFeatureDescriptor().getValue(PROPERTY_SHOW_FILES);
147
if (fil instanceof Boolean) {
148
files = ((Boolean)fil).booleanValue();
149
} // XXX else if != null, warn
150
Object filter = env.getFeatureDescriptor().getValue(PROPERTY_FILTER);
151
if (filter instanceof FilenameFilter) {
152
fileFilter = new DelegatingFilenameFilter((FilenameFilter)filter);
153
} else if (filter instanceof javax.swing.filechooser.FileFilter) {
154
fileFilter = (javax.swing.filechooser.FileFilter)filter;
155
} else if (filter instanceof java.io.FileFilter) {
156
fileFilter = new DelegatingFileFilter((java.io.FileFilter)filter);
157
} // XXX else if != null, warn
159
Object curDir = env.getFeatureDescriptor().getValue(PROPERTY_CURRENT_DIR);
160
if (curDir instanceof File) {
161
currentDirectory = (File)curDir;
162
if(! currentDirectory.isDirectory()) {
163
Logger.getAnonymousLogger().warning("java.io.File will not accept currentDir=" + currentDirectory); // NOI18N
164
currentDirectory = null;
166
} // XXX else if != null, warn
168
Object baseDir = env.getFeatureDescriptor().getValue(PROPERTY_BASE_DIR);
169
if(baseDir instanceof File) {
170
baseDirectory = (File)baseDir;
171
// As baseDir accept only directories in their absolute form.
172
if(!baseDirectory.isDirectory() || !baseDirectory.isAbsolute()) {
173
Logger.getAnonymousLogger().warning("java.io.File will not accept baseDir=" + baseDirectory); // NOI18N
174
baseDirectory = null;
176
} // XXX else if != null, warn
178
mode = directories ? JFileChooser.FILES_AND_DIRECTORIES :
179
JFileChooser.FILES_ONLY;
181
mode = directories ? JFileChooser.DIRECTORIES_ONLY :
182
JFileChooser.FILES_AND_DIRECTORIES; // both false, what now? XXX warn
185
Object fileHide = env.getFeatureDescriptor().getValue(PROPERTY_FILE_HIDING);
186
if (fileHide instanceof Boolean) {
187
fileHiding = ((Boolean)fileHide).booleanValue();
190
if (env.getFeatureDescriptor() instanceof Node.Property){
191
Node.Property prop = (Node.Property)env.getFeatureDescriptor();
192
editable = prop.canWrite();
196
/** Returns human readable form of the edited value.
197
* @return string reprezentation
199
public String getAsText() {
200
File file = (File)getValue();
204
String path = file.getPath();
205
// Dot is more friendly to people though Java itself would prefer blank:
206
if ("".equals(path)) path = "."; // NOI18N
210
/** Parses the given string and should create a new instance of the
212
* @param str string reprezentation of the file (used as a parameter for File).
213
* @throws IllegalArgumentException If the given string cannot be parsed
215
public void setAsText(String str) throws IllegalArgumentException {
217
throw new IllegalArgumentException("null"); // NOI18N
219
if ("".equals(str)) { // NOI18N
224
if (".".equals(str)) str = ""; // NOI18N
225
setValue(new File(str));
229
* @return Returns custom editor component.
231
public Component getCustomEditor() {
234
Object curVal = getValue();
235
if (curVal instanceof java.io.File) {
236
info = ((java.io.File)curVal).getAbsolutePath();
238
return new StringCustomEditor(info, false, true, null, this, env);
240
if (chooser == null) {
241
chooser = createHackedFileChooser();
243
File originalFile = (File)getValue ();
244
if (originalFile != null && ! originalFile.isAbsolute() && baseDirectory != null) {
245
originalFile = new File(baseDirectory, originalFile.getPath());
247
if (currentDirectory != null) {
248
chooser.setCurrentDirectory (currentDirectory);
249
} else if (originalFile != null && originalFile.getParentFile() != null) {
250
chooser.setCurrentDirectory (originalFile.getParentFile());
251
chooser.setSelectedFile (originalFile);
252
} else if (lastCurrentDir != null) {
253
chooser.setCurrentDirectory(lastCurrentDir);
255
chooser.setFileSelectionMode(mode);
256
if (fileFilter != null) {
257
chooser.setFileFilter(fileFilter);
260
case JFileChooser.FILES_AND_DIRECTORIES:
261
chooser.setDialogTitle (getString ("CTL_DialogTitleFilesAndDirs"));
263
case JFileChooser.FILES_ONLY:
264
chooser.setDialogTitle (getString ("CTL_DialogTitleFiles"));
266
case JFileChooser.DIRECTORIES_ONLY:
267
chooser.setDialogTitle (getString ("CTL_DialogTitleDirs"));
270
chooser.setFileHidingEnabled(fileHiding);
272
chooser.setControlButtonsAreShown(false);
274
chooser.addPropertyChangeListener(
275
JFileChooser.SELECTED_FILE_CHANGED_PROPERTY,
279
HelpCtx.setHelpIDString (chooser, getHelpCtx ().getHelpID ());
285
/** Implements PropertyEditor method.
286
* @return Returns true.
288
public boolean supportsCustomEditor() {
292
/** Should create a string insertable to the newly generated source code.
293
* @return initialization string
295
public String getJavaInitializationString() {
296
File value = (File) getValue ();
298
return "null"; // NOI18N
300
// [PENDING] not a full escape of filenames, but enough to at least
301
// handle normal Windows backslashes
302
if (baseDirectory != null && !value.isAbsolute()) {
303
return "new java.io.File(" // NOI18N
304
+ stringify(baseDirectory.getPath())
306
+ stringify(value.getPath())
309
return "new java.io.File(" // NOI18N
310
+ stringify(value.getAbsolutePath())
315
static String stringify(String in) {
316
StringBuffer buf = new StringBuffer(in.length() * 2 + 2);
317
buf.append('"'); // NOI18N
318
for (int i = 0; i < in.length(); i++) {
319
char c = in.charAt(i);
320
if (c == '\\' || c == '"') { // NOI18N
321
buf.append('\\'); // NOI18N
325
buf.append('"'); // NOI18N
326
return buf.toString();
329
/** Gets help context. */
330
private HelpCtx getHelpCtx () {
331
return new HelpCtx (FileEditor.class);
334
/** Gets localized string. Helper method. */
335
private static String getString(String key) {
336
return NbBundle.getBundle(FileEditor.class).getString(key);
339
/** Gets relative path of file to specified directory only for case the file
340
* is in directory tree.
341
* @param baseDir base directory
342
* @param file file which relative path to <code>baseDir</code> is needed
343
* @return relative path or <code>null</code> can't be resolved
344
* or if the <code>file</code> is not under <code>baseDir</code> tree */
345
static String getChildRelativePath(File baseDir, File file) {
346
// Handle hypothetical weird situations where file is in baseDir
347
// but the prefixes do not match. E.g.:
348
// file=\foo\bar.txt (assumed to be on C:) baseDir=c:\foo
349
if (file.equals(baseDir)) {
350
// The empty pathname, not ".", is correct here I think...
351
// Try making new File(new File("/tmp", x)) for x in {".", ""}
354
StringBuffer buf = new StringBuffer(file.getPath().length());
355
buf.append(file.getName());
356
for (File parent = file.getParentFile(); parent != null; parent = parent.getParentFile()) {
357
if (parent.equals(baseDir)) {
358
return buf.toString();
360
buf.insert(0, File.separatorChar);
361
buf.insert(0, parent.getName());
366
/** Property change listaner attached to the JFileChooser chooser. */
367
public void propertyChange(PropertyChangeEvent e) {
368
JFileChooser chooser = (JFileChooser)e.getSource();
369
File f = chooser.getSelectedFile();
373
if (!files && f.isFile ()) return;
374
if (!directories && f.isDirectory ()) return;
376
if (baseDirectory != null) {
377
String rel = getChildRelativePath(baseDirectory, f);
383
// use to be setValue(f) - the next line is
384
// workaround for JDK bug 4533419
385
// it should be returned back to setValue(f) after the
386
// mentioned bug is fixed in JDK.
387
setValue(new File(f.getPath()));
389
lastCurrentDir = chooser.getCurrentDirectory();
392
// XXX #18270. Enter doesn't work when expecting folder change,
393
// Accessibility problem. We hack default behaviour here.
394
/** Creates hacked fileChooser, responding on Enter the way it
395
* performs folder change. */
396
public static JFileChooser createHackedFileChooser() {
397
JFileChooser chooser = new JFileChooser();
398
hackFileChooser(chooser);
402
/** Hacks fileChooser, responding on Enter the way it
403
* performs folder change. */
404
public static void hackFileChooser(final JFileChooser chooser) {
405
chooser.getAccessibleContext().setAccessibleDescription( getString("ACSD_FileEditor") );
407
//issue 31605 - make escape work properly
408
//Get the existing action key on ESCAPE
409
// XXX is this hack still necessary?
410
final Object key = chooser.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).get(
411
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0));
413
Action close = new AbstractAction() {
414
public void actionPerformed(ActionEvent ae) {
415
Component comp = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
417
//if there was an action, do it first
418
Action a = chooser.getActionMap().get(key);
420
a.actionPerformed(ae);
423
if (comp.getParent() == null) {
424
//then we were editing a file name, and the editor
425
//was removed - we don't want to close the dialog
429
Container c = chooser.getTopLevelAncestor();
430
//The action *may* have already hidden the panel (works
432
if (c instanceof Dialog) {
433
if (((Dialog) c).isVisible()) {
434
((Dialog) c).setVisible (false);
435
((Dialog) c).dispose();
440
chooser.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
441
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close");
442
chooser.getActionMap().put("close", close);
445
private static class ButtonHider implements PropertyChangeListener {
446
public void propertyChange (PropertyChangeEvent pce) {
447
if (JFileChooser.CONTROL_BUTTONS_ARE_SHOWN_CHANGED_PROPERTY.equals(pce.getPropertyName())) {
448
JFileChooser jfc = (JFileChooser) pce.getSource();
450
hideShowButtons(jfc, Boolean.TRUE.equals(pce.getNewValue()));
451
} catch (Exception e) {
452
Logger.getLogger(FileEditor.class.getName()).log(Level.WARNING, null, e);
457
private void hideShowButtons (Container cont, boolean val) {
458
if (cont instanceof JComboBox || cont instanceof JScrollBar) {
461
Component[] c = cont.getComponents();
462
for (int i=0; i < c.length; i++) {
463
if (c[i] instanceof Container) {
464
hideShowButtons ((Container) c[i], val);
466
if (c[i] instanceof AbstractButton) {
467
c[i].setVisible(val);
473
/** Wraps java.io.FileFilter to javax.swing.filechooser.FileFilter. */
474
static class DelegatingFileFilter extends javax.swing.filechooser.FileFilter {
475
private java.io.FileFilter filter;
477
public DelegatingFileFilter(java.io.FileFilter f) {
481
public boolean accept(File f) {
482
return filter.accept(f);
485
public String getDescription() {
486
// [PENDING] what should we return?
490
} // End of class DelegatingFileFilter.
493
/** Wraps FilenameFilter to javax.swing.filechooser.FileFilter. */
494
static class DelegatingFilenameFilter extends javax.swing.filechooser.FileFilter {
495
private FilenameFilter filter;
497
public DelegatingFilenameFilter(FilenameFilter f) {
500
/** Calls the filenameFilter's accept method with arguments
501
* created from the original object f.
503
public boolean accept(File f) {
504
return filter.accept(f.getParentFile(), f.getName());
507
public String getDescription() {
508
// [PENDING] what should we return?
511
} // End of class DelegatingFilenameFilter.