|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun * Microsystems, Inc. All Rights Reserved. */ package org.openide.explorer.propertysheet.editors; import java.awt.*; import java.awt.event.*; import java.beans.*; import java.io.File; import javax.swing.JFileChooser; import org.openide.nodes.Node; import org.openide.util.*; import org.openide.windows.WindowManager; /** Property editor for java.io.File. It can be configured to look for file or directories * or both. Default constructor scans for both. * * This class is being migrated to the core. The functionality should be * provided by PropertyPanel and ExPropertyEditor. Example of use of the * new approach [PENDING]. * * @deprecated Property editor moved to core * @author Jaroslav Tulach * @version 0.10 */ public class FileEditor extends PropertyEditorSupport implements NodePropertyEditor { /** mode to look for */ private int mode = JFileChooser.FILES_AND_DIRECTORIES; /** currently attached nodes, if any */ private Node[] nodes = null; /** Editor that accepts files or directories. */ public FileEditor() { } /** Editor in special mode. It can be either JFileChooser.FILES_ONLY, * JFileChooser.DIRECTORIES_ONLY or JFileChooser.FILES_AND_DIRECTORIES. */ public FileEditor(int mode) { this.mode = mode; } /* Informs the editor that the property that it * is displaying belongs to following nodes. * * @param nodes array of nodes having the property */ public void attach (Node[] nodes) { this.nodes = nodes; } /* sets new value */ public void setAsText(String s) { setValue(new File (s)); } /* gets string value */ public String getAsText () { Object obj = getValue (); if (obj instanceof File) return obj.toString (); return null; } public boolean supportsCustomEditor () { // [PENDING] this is a problem. If the property is actually read-only, this // will display a chooser anyway, complete with Open button, which if actually // pressed will just display an exception dialog! Normally property editors // do not have to deal with this, because the property displayer will just // not provide the accept button in this case. In our case, we provide the // whole dialog, so are responsible for the button. But it is impossible to // reliably tell whether the Node.Property we are editing is writable or not. // Hence this hack. NodePropertyEditor would work 100% if it gave the exact // property name, but unfortunately it does not at the moment. if (nodes != null) { for (int i = 0; i < nodes.length; i++) { Node.PropertySet[] propsets = nodes[i].getPropertySets (); for (int j = 0; j < propsets.length; j++) { Node.Property[] props = propsets[j].getProperties (); for (int k = 0; k < props.length; k++) { Node.Property prop = props[k]; if (File.class.equals (prop.getValueType ()) && (! prop.canRead () || ! prop.canWrite ())) { // I.e.: if any of the nodes have any read-only File-valued // properties, do not show the chooser, just to be safe. // A node with one writable and one read-only File property // will not show the chooser in either case, but oh well. return false; } } } } } return true; } /* @return file chooser in dialog */ public Component getCustomEditor() { final JFileChooser chooser = createFileChooser (); // [PENDING] this is ugly--a property editor component ought to be a standalone // embeddable component placed wherever the container likes. Unfortunately // the required call is setControlButtonsAreShown, only available on 1.3. // Otherwise we could simply return the chooser itself; fire property changes // whenever the selected file changes; and permit the IDE (container) to add // its own OK/close/help buttons as in a normal proped. final javax.swing.JDialog dialog = new javax.swing.JDialog ( WindowManager.getDefault().getMainWindow (), chooser.getDialogTitle (), true ); // attach cancel also to Escape key dialog.getRootPane().registerKeyboardAction( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { dialog.setVisible (false); dialog.dispose (); } }, javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0, true), javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW ); dialog.addKeyListener (new java.awt.event.KeyAdapter () { public void keyPressed (java.awt.event.KeyEvent evt) { if (evt.getKeyCode () == java.awt.event.KeyEvent.VK_ESCAPE) { dialog.setVisible (false); dialog.dispose (); } } } ); dialog.getContentPane ().setLayout (new java.awt.BorderLayout ()); dialog.getContentPane ().add (chooser, java.awt.BorderLayout.CENTER); chooser.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent evt) { if (JFileChooser.APPROVE_SELECTION.equals (evt.getActionCommand ())) { File f = chooser.getSelectedFile (); setValue (f); dialog.setVisible (false); dialog.dispose (); } else if (JFileChooser.CANCEL_SELECTION.equals (evt.getActionCommand ())) { dialog.setVisible (false); dialog.dispose (); } } } ); HelpCtx.setHelpIDString (dialog.getRootPane (), getHelpCtx ().getHelpID ()); return dialog; } /* Java initialization string. */ public String getJavaInitializationString () { File value = (File) getValue (); if (value == null) { return "null"; // NOI18N } else { // [PENDING] not a full escape of filenames, but enough to at least // handle normal Windows backslashes return "new java.io.File (\"" + // NOI18N Utilities.replaceString (value.getAbsolutePath (), "\\", "\\\\") // NOI18N + "\")"; // NOI18N } } /** Allows subclasses to modify the chooser to suit their needs. */ protected JFileChooser createFileChooser () { File originalFile = (File)getValue (); final JFileChooser chooser = new JFileChooser (); chooser.setFileSelectionMode(mode); if (originalFile != null && originalFile.getParent () != null) chooser.setCurrentDirectory (new File (originalFile.getParent ())); chooser.setSelectedFile (originalFile); chooser.setApproveButtonText (getString ("CTL_ApproveSelect")); chooser.setApproveButtonToolTipText (getString ("CTL_ApproveSelectToolTip")); switch (mode) { case JFileChooser.FILES_AND_DIRECTORIES: chooser.setDialogTitle (getString ("CTL_DialogTitleFilesAndDirs")); break; case JFileChooser.FILES_ONLY: chooser.setDialogTitle (getString ("CTL_DialogTitleFiles")); break; case JFileChooser.DIRECTORIES_ONLY: chooser.setDialogTitle (getString ("CTL_DialogTitleDirs")); break; } return chooser; } /** Permits subclasses to associate a help context with the whole chooser. * @return suitable context help */ protected HelpCtx getHelpCtx () { return new HelpCtx (FileEditor.class); } private static String getString(String key) { return NbBundle.getBundle(FileEditor.class).getString(key); } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.