Class GFFileChooser

Object
  extended by Component
      extended by Container
          extended by Window
              extended by Dialog
                  extended by JDialog
                      extended by GFFileChooser
All Implemented Interfaces:
ActionListener, ItemListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible, CaretListener, ListSelectionListener, RootPaneContainer, WindowConstants

public class GFFileChooser
extends JDialog
implements ActionListener, ListSelectionListener, CaretListener, ItemListener

This Swing class enables file name selection in a manner similar to JFileChooser, but with some additional features.

The Filter

The filter is similar to what is available in Unix and MS-DOS filename matching commands. It accepts the special characters:
* will match any number of characters
+ will match one or more characters
? will match any single character
\ flags the next character to be taken literally.

By default, the filter will be "*", giving all files in the current directory. The user is able to change the filter, eg. to "*.txt" or "Fred?". The filter feature will not work or be displayed on systems earlier than Java 1.4 which lack the Pattern class, though this will not cause loading this class to fail.

By default, "hidden" files and directories will not be listed. If all names are wanted, there is a check-box to request the hidden files.

The parent

When the selector is created, the parent component must be indicated, to ensure that if the parent is iconified, the selector goes with it. Also required is the directory in which the selector will commence, which by default will be the user's home directory.

Modes of operation

The selector is displayed by calling one of the three showDialog methods. The parameters to showDialog determine how the selector will behave, so a single dialog can be used for different purposes by choosing the appropriate parameters each time it is displayed. The selector can be used to select only filenames, only directory names, only existing names, or any names including ones that may not already exist.

The single-parameter showDialog method will allow anything to be selected, including files that do not yet exist, which might possibly be in a new directory which must first be created.

In the two-parameter showDialog method, the second parameter indicates whether or not the chosen name must exist on the file system.

In the three-parameter method, the third parameter allows the programmer to impose the requirement whether it is a file or a directory that is being chosen. When directory selection is wanted, the names of files in the directory are shown but they cannot be selected. An explicit click on the accept button is required, and the directory name at the top of the window will be returned to the program.

The intent of the design is that, wherever possible, selection will be based on a single click.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class Dialog
Dialog.ModalExclusionType, Dialog.ModalityType
 
Nested classes/interfaces inherited from class Component
Component.BaselineResizeBehavior
 
Field Summary
 
Fields inherited from class Dialog
DEFAULT_MODALITY_TYPE
 
Fields inherited from class Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface WindowConstants
DISPOSE_ON_CLOSE, DO_NOTHING_ON_CLOSE, EXIT_ON_CLOSE, HIDE_ON_CLOSE
 
Fields inherited from interface ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
GFFileChooser(JFrame owner)
          Simple constructor for the file chooser.
GFFileChooser(JFrame owner, String dir)
          Constructor for the file chooser, allowing the initial directory to be given.
 
Method Summary
 void actionPerformed(ActionEvent e)
          Event handler for buttons and text-fields.
 void caretUpdate(CaretEvent e)
          Event handler for the name filter.
 File getFile()
          Return a File object representing the file name chosen.
 String getFilter()
          Return the filter string that the user has given.
 String getName()
          Return the full file name chosen.
 String getShortName()
          Return the short form of the file name chosen, without directory prefixes.
 void itemStateChanged(ItemEvent e)
          Event handler for the checkboxes, eg for hidden name display.
static void main(String[] args)
          Simple demonstration to test the interface.
 void setCurrentDirectory(File dir)
          Set the current directory prior to displaying the chooser.
 void setFile(File selected)
          Set the initial name of the selected file.
 void setFilter(String text)
          Set the filter string which limits which files can be selected.
 boolean showDialog(String approveButtonText)
          Display the file selection dialog, allowing any file name, existing or otherwise to be typed or selected.
 boolean showDialog(String approveButtonText, boolean fileMustExist)
          Display the file selection dialog, with a parameter controlling whether the selected name must already exist on the file system.
 boolean showDialog(String approveButtonText, boolean dirMustExist, boolean dirSelection)
          Display the file selection dialog, with a parameter controlling whether the name chosen must already exist on the file system, and another parameter determining whether it is a file or a directory that is being selected.
 void valueChanged(ListSelectionEvent e)
          Event handler for the lists.
 
Methods inherited from class JDialog
getAccessibleContext, getContentPane, getDefaultCloseOperation, getGlassPane, getGraphics, getJMenuBar, getLayeredPane, getRootPane, getTransferHandler, isDefaultLookAndFeelDecorated, remove, repaint, setContentPane, setDefaultCloseOperation, setDefaultLookAndFeelDecorated, setGlassPane, setJMenuBar, setLayeredPane, setLayout, setTransferHandler, update
 
Methods inherited from class Dialog
addNotify, getModalityType, getTitle, hide, isModal, isResizable, isUndecorated, setModal, setModalityType, setResizable, setTitle, setUndecorated, setVisible, show, toBack
 
Methods inherited from class Window
addPropertyChangeListener, addPropertyChangeListener, addWindowFocusListener, addWindowListener, addWindowStateListener, applyResourceBundle, applyResourceBundle, createBufferStrategy, createBufferStrategy, dispose, getBufferStrategy, getFocusableWindowState, getFocusCycleRootAncestor, getFocusOwner, getFocusTraversalKeys, getGraphicsConfiguration, getIconImages, getInputContext, getListeners, getLocale, getModalExclusionType, getMostRecentFocusOwner, getOwnedWindows, getOwner, getOwnerlessWindows, getToolkit, getWarningString, getWindowFocusListeners, getWindowListeners, getWindows, getWindowStateListeners, isActive, isAlwaysOnTop, isAlwaysOnTopSupported, isFocusableWindow, isFocusCycleRoot, isFocused, isLocationByPlatform, isShowing, pack, postEvent, removeNotify, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, reshape, setAlwaysOnTop, setBounds, setBounds, setCursor, setFocusableWindowState, setFocusCycleRoot, setIconImage, setIconImages, setLocationByPlatform, setLocationRelativeTo, setMinimumSize, setModalExclusionType, setSize, setSize, toFront
 
Methods inherited from class Container
add, add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, preferredSize, print, printComponents, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusBackward, transferFocusDownCycle, validate
 
Methods inherited from class Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, dispatchEvent, enable, enable, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocusInWindow, resize, resize, setBackground, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setName, setPreferredSize, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GFFileChooser

public GFFileChooser(JFrame owner)
Simple constructor for the file chooser. The parameter indicates the window to which the dialog belongs, which is required by the window manager.


GFFileChooser

public GFFileChooser(JFrame owner,
                     String dir)
Constructor for the file chooser, allowing the initial directory to be given. The first parameter indicates the window to which the dialog belongs, which is required by the window manager.

Parameters:
dir - The initial directory to be shown when the file chooseer is first displayed.
Method Detail

actionPerformed

public void actionPerformed(ActionEvent e)
Event handler for buttons and text-fields.

Specified by:
actionPerformed in interface ActionListener

valueChanged

public void valueChanged(ListSelectionEvent e)
Event handler for the lists.

Specified by:
valueChanged in interface ListSelectionListener

caretUpdate

public void caretUpdate(CaretEvent e)
Event handler for the name filter.

Specified by:
caretUpdate in interface CaretListener

itemStateChanged

public void itemStateChanged(ItemEvent e)
Event handler for the checkboxes, eg for hidden name display.

Specified by:
itemStateChanged in interface ItemListener

showDialog

public boolean showDialog(String approveButtonText)
Display the file selection dialog, allowing any file name, existing or otherwise to be typed or selected. A field will be provided for the name of the file to be typed; a sub-directory can be created to hold a file. A button appears at the bottom to confirm selection.

Parameters:
approveButtonText - The text to display on the Accept button.
Returns:
false if the user cancels the selection, otherwise true.

showDialog

public boolean showDialog(String approveButtonText,
                          boolean fileMustExist)
Display the file selection dialog, with a parameter controlling whether the selected name must already exist on the file system.

Parameters:
approveButtonText - The text to display on the Accept button.
fileMustExist - If true, only existing files can be selected, and new directories cannot be created. Selection of a file will complete on the button press in the file list.
Returns:
false if the user cancels the selection, otherwise true.

showDialog

public boolean showDialog(String approveButtonText,
                          boolean dirMustExist,
                          boolean dirSelection)
Display the file selection dialog, with a parameter controlling whether the name chosen must already exist on the file system, and another parameter determining whether it is a file or a directory that is being selected.

Parameters:
approveButtonText - The text to display on the Accept button.
dirMustExist - If true, only existing directories can be selected, and new directories cannot be created.
dirSelection - If true, the purpose of the selector is to choose a directory name.
Returns:
false if the user cancels the selection, otherwise true.

getFilter

public String getFilter()
Return the filter string that the user has given.


setFilter

public void setFilter(String text)
Set the filter string which limits which files can be selected.


getName

public String getName()
Return the full file name chosen.

Overrides:
getName in class Component

getShortName

public String getShortName()
Return the short form of the file name chosen, without directory prefixes.


getFile

public File getFile()
Return a File object representing the file name chosen.


setCurrentDirectory

public void setCurrentDirectory(File dir)
Set the current directory prior to displaying the chooser. The files and sub-directories in this directory will be displayed.


setFile

public void setFile(File selected)
Set the initial name of the selected file.


main

public static void main(String[] args)
Simple demonstration to test the interface.