org.pietschy.command.file
Class AbstractSaveAsCommand

java.lang.Object
  extended by org.pietschy.command.Command
      extended by org.pietschy.command.ActionCommand
          extended by org.pietschy.command.file.AbstractFileCommand
              extended by org.pietschy.command.file.AbstractSaveAsCommand
All 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(java.io.File) will be invoked. If the file selected by the user already exists the command will prompt the user to confirm before performSave(java.io.File) is invoked. The message displayed to the user can be configured by overriding getFileExistsMessage(java.io.File) and getFileExistsTitle().

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(CommandManager manager, java.lang.String id, javax.swing.filechooser.FileFilter filter)
          Creates new instance with the specified id and FileFilter.
AbstractSaveAsCommand(CommandManager manager, java.lang.String id, javax.swing.filechooser.FileFilter[] filters)
          Creates new instance with the specified id and FileFilters.
AbstractSaveAsCommand(java.lang.String id, javax.swing.filechooser.FileFilter filter)
          Creates new instance with the specified id and FileFilter.
AbstractSaveAsCommand(java.lang.String id, javax.swing.filechooser.FileFilter[] filters)
          Creates new instance with the specified id and FileFilter list.
 
Method Summary
protected  boolean confirmOverwrite(java.awt.Window invoker, java.io.File file)
          This method is called to confirm the save if the selected file already exists.
protected  java.lang.String getFileExistsMessage(java.io.File file)
          Gets the text to display in the dialog that confirms file overwrite.
protected  void performFileAction(java.io.File[] selectedFiles, javax.swing.JFileChooser chooser, java.awt.Window invoker)
          This method is invoked if the AbstractFileCommand.showChooserDialog(javax.swing.JFileChooser, java.awt.Window) returns JFileChooser.APPROVE_OPTION.
protected abstract  void performSave(java.io.File file)
          Called to perform the save operation.
protected  int showChooserDialog(javax.swing.JFileChooser chooser, java.awt.Window invoker)
          Called to display the JFileChooser.
protected  java.io.File verifyFileExtension(java.io.File file, javax.swing.filechooser.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, setAcceptAllFileFilterUsed, setCenterOnInvoker, setDefaultFileFilter, setRememberLastFilter
 
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, 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, buttonIterator, configureButtonAppearance, configureButtonAppearances, configureButtonAppearances, createButton, createButton, createButton, createButton, createMenuItem, createMenuItem, createMenuItem, createMenuItem, 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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.pietschy.command.ActionCommandExecutor
addPropertyChangeListener, addPropertyChangeListener, isEnabled, removePropertyChangeListener, removePropertyChangeListener
 

Constructor Detail

AbstractSaveAsCommand

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

Parameters:
id - the command 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(java.lang.String id,
                             javax.swing.filechooser.FileFilter[] filters)
Creates new instance with the specified id and FileFilter list. This command is bound to the default command manager.

Parameters:
id - the command 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.

AbstractSaveAsCommand

public AbstractSaveAsCommand(CommandManager manager,
                             java.lang.String id,
                             javax.swing.filechooser.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,
                             java.lang.String id,
                             javax.swing.filechooser.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 Detail

showChooserDialog

protected int showChooserDialog(javax.swing.JFileChooser chooser,
                                java.awt.Window invoker)
Description copied from class: AbstractFileCommand
Called to display the JFileChooser. Subclasses override to display an appropriate version of the chooser (such as an open or save dialog). This method must return the result of the JFileChooser show method. Eg.
protected int showChooserDialog(JFileChooser chooser, Window invoker)
{
   return chooser.showOpenDialog(invoker);
}

Specified by:
showChooserDialog in class AbstractFileCommand
Parameters:
chooser - the chooser to display
Returns:
the result of JFileChooser.showDialog(java.awt.Component, java.lang.String), JFileChooser.showOpenDialog(java.awt.Component) or JFileChooser.showSaveDialog(java.awt.Component).

performFileAction

protected void performFileAction(java.io.File[] selectedFiles,
                                 javax.swing.JFileChooser chooser,
                                 java.awt.Window invoker)
Description copied from class: AbstractFileCommand
This method is invoked if the AbstractFileCommand.showChooserDialog(javax.swing.JFileChooser, java.awt.Window) returns JFileChooser.APPROVE_OPTION. Subclasses must override to perform the specific file operation.

Specified by:
performFileAction in class AbstractFileCommand
Parameters:
selectedFiles - the files that were selected in the JFileChooser
chooser - the JFileChooser that was displayed.
invoker - the owner window.

verifyFileExtension

protected java.io.File verifyFileExtension(java.io.File file,
                                           javax.swing.filechooser.FileFilter selectedFilter)
Invoked to verify the extension of the file selected by the user. This is provided to catch the case where the user has typed the file name but has ommited the extension.

This implementation checks if the FileFilter is an instance of ExtensionFileFilter. If it is, it invokes ExtensionFileFilter.checkAndAddExtension(java.io.File) on the file, otherwise the file is returned as is.

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.
See Also:
ExtensionFileFilter

confirmOverwrite

protected boolean confirmOverwrite(java.awt.Window invoker,
                                   java.io.File file)
This method is called to confirm the save if the selected file already exists. By default this method invokes 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 java.lang.String getFileExistsMessage(java.io.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?".

performSave

protected abstract void performSave(java.io.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.