org.pietschy.command.file

Class AbstractSaveAsCommand

Implemented Interfaces:
ActionCommandExecutor

public abstract class AbstractSaveAsCommand
extends AbstractFileCommand

This command provides generic behaviour for the standard "save as" operation. On execution, the command will display a JFileChooser and if successful performSave(File) will be invoked. If the file selected by the user already exists the command will prompt the user to confirm before performSave(File) is invoked. The message displayed to the user can be configured by overriding getFileExistsMessage(File) and AbstractSaveAsCommand.

If the filters specified are instances of ExtensionFileFilter, the command will also ensure the extension of the file is correct.

Version:
$Revision: 1.6 $
Author:
andrewp

Field Summary

Fields inherited from class org.pietschy.command.ActionCommand

HINT_ACTION_EVENT, HINT_INVOKER, HINT_INVOKER_WINDOW, HINT_MODIFIERS

Fields inherited from class org.pietschy.command.Command

internalLog, listenerList, pcs

Constructor Summary

AbstractSaveAsCommand(String id, FileFilter filter)
Creates new instance with the specified id and FileFilter.
AbstractSaveAsCommand(String id, FileFilter[] filters)
Creates new instance with the specified id and FileFilter list.
AbstractSaveAsCommand(CommandManager manager, String id, FileFilter filter)
Creates new instance with the specified id and FileFilter.
AbstractSaveAsCommand(CommandManager manager, String id, FileFilter[] filters)
Creates new instance with the specified id and FileFilters.

Method Summary

protected boolean
confirmOverwrite(Window invoker, File file)
This method is called to confirm the save if the selected file already exists.
protected String
getFileExistsMessage(File file)
Gets the text to display in the dialog that confirms file overwrite.
protected void
performFileAction(File[] selectedFiles, JFileChooser chooser, Window invoker)
protected abstract void
performSave(File file)
Called to perform the save operation.
protected int
showChooserDialog(JFileChooser chooser, Window invoker)
protected File
verifyFileExtension(File file, FileFilter selectedFilter)
Invoked to verify the extension of the file selected by the user.

Methods inherited from class org.pietschy.command.file.AbstractFileCommand

afterExecute, beforeExecute, confirmProceed, getDefaultFileFilter, getFileChooser, handleCancel, handleError, handleExecute, isAcceptAllFileFilterUsed, isCenterOnInvoker, isRememberLastFilter, performFileAction, setAcceptAllFileFilterUsed, setCenterOnInvoker, setDefaultFileFilter, setRememberLastFilter, showChooserDialog

Methods inherited from class org.pietschy.command.ActionCommand

addCommandListener, addInterceptor, areEqual, attach, configureButtonStates, detach, execute, execute, getActionAdapter, getActionAdapter, getActionCommand, getActionEvent, getHint, getHint, getHints, getInvoker, getInvokerWindow, getModifiers, handleExecute, installShortCut, installShortCut, postExecute, preExecute, putHint, putHints, removeCommandListener, removeInterceptor, requestDefautIn, setActionCommand, uninstallShortCut, uninstallShortCut

Methods inherited from class org.pietschy.command.Command

addFace, addHoverListener, addNewFace, addNotify, addPropertyChangeListener, addPropertyChangeListener, attach, buttonIterator, configureButtonAppearance, configureButtonAppearances, configureButtonAppearances, configureButtonStates, createButton, createButton, createButton, createButton, createMenuItem, createMenuItem, createMenuItem, createMenuItem, detach, export, faceExists, fireHoverEnded, fireHoverStarted, getAccelerator, getAlternativeFaceNames, getButtonFactory, getButtonIn, getCommandManager, getDefaultFace, getDefaultFace, getDescription, getFace, getFace, getIcon, getId, getLongDescription, getMenuFactory, getMnemonic, getMnemonicIndex, getProperty, getProperty, getPropertyNames, getSelectedIcon, getText, getTextPosition, getToolbarFactory, initCommandManager, installFace, isAnonymous, isAttachedTo, isEnabled, isVisible, putProperty, removeHoverListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, requestFocusIn, setAccelerator, setButtonFactory, setDescription, setEnabled, setIcon, setLongDescription, setMenuFactory, setMnemonic, setMnemonicIndex, setSelectedIcon, setText, setTextPosition, setToolbarFactory, setVisible, toString

Constructor Details

AbstractSaveAsCommand

public AbstractSaveAsCommand(String id,
                             FileFilter filter)
Creates new instance with the specified id and FileFilter. This command is bound to the default command manager.

AbstractSaveAsCommand

public AbstractSaveAsCommand(String id,
                             FileFilter[] filters)
Creates new instance with the specified id and FileFilter list. This command is bound to the default command manager.

AbstractSaveAsCommand

public AbstractSaveAsCommand(CommandManager manager,
                             String id,
                             FileFilter filter)
Creates new instance with the specified id and FileFilter.
Parameters:
id - the commands id.
filter - the FileFilter for the JFileChooser to use. If it is an instance of ExtensionFileFilter the command will ensure the selected file has the correct extension.

AbstractSaveAsCommand

public AbstractSaveAsCommand(CommandManager manager,
                             String id,
                             FileFilter[] filters)
Creates new instance with the specified id and FileFilters.
Parameters:
id - the commands id.
filters - the FileFilter list for the JFileChooser to use. If they are instances of ExtensionFileFilter the command will ensure the selected file has the correct extension.

Method Details

confirmOverwrite

protected boolean confirmOverwrite(Window invoker,
                                   File file)
This method is called to confirm the save if the selected file already exists. By default this method invokes javax.swing.JOptionPane.showConfirmDialog(java.awt.Component, Object) with getFileExistsTitle() and getFileExistsMessage(java.io.File).

Subclasses can override for complete control over the confirmation process.

Parameters:
invoker - the invoker window.
file - the file that is to be overwritten.
Returns:
true to overwrite the file, false to cancel the operation.

getFileExistsMessage

protected String getFileExistsMessage(File file)
Gets the text to display in the dialog that confirms file overwrite. This defaults to the string "File '" + file.getName() + "' already exists. Overwrite?"
Returns:
the string "File '" + file.getName() + "' already exists. Overwrite?".

performFileAction

protected void performFileAction(File[] selectedFiles,
                                 JFileChooser chooser,
                                 Window invoker)
Overrides:
performFileAction in interface AbstractFileCommand

performSave

protected abstract void performSave(File file)
Called to perform the save operation. Subclasses must implement this method to perform the required save behaviour.
Parameters:
file - the file to save.

showChooserDialog

protected int showChooserDialog(JFileChooser chooser,
                                Window invoker)
Overrides:
showChooserDialog in interface AbstractFileCommand

verifyFileExtension

protected File verifyFileExtension(File file,
                                   FileFilter selectedFilter)
Parameters:
file - the file the users has selected, that may or may not have an extension.
selectedFilter - the FileFilter selected in the chooser.
Returns:
a filename with the correct extension.