File Commands

GUI Commands provides standard command implementations for opening and saving files as well as a recent files group. These commands perform a lot of the grunt work associated with opening and saving files by displaying the file chooser and checking if the file already exists. The RecentFileList makes it easy to display the list of recently opened files and provides convenience methods for storing the list in a Preference node.

AbstractFileOpenCommand

The following shows a basic implementation of a file open command:

   public class OpenCommand extends AbstractFileOpenCommand
   {
      public OpenCommand()
      {
         super("open");
      }

      /**
       * Invoked after the user has selected
       * the file in the JFileChooser
       */
      public void performOpen(File[] files)
      {
         ...open the file
      }
   }

AbstractSaveAsCommand

A basic implementation of a "save as" command:

   public class SaveAsCommand extends AbstractSaveAsCommand
   {
      public SaveAsCommand()
      {
         super("save-as");
      }

      /**
       * Invoked after the user has selected the
       * file in the JFileChooser and confirmed
       * overwrite if the file already exists
       */
      public void performSave(File file)
      {
         ...save the file
      }
   }

You can control the dialog shown to users when the selected file already exists by defining the properties overwriteTitle and overwriteMessage. The overwriteMessage property is passed through a MessageFormat with the first parameter being the file name as per File.getName(). The following is an example configuration:

   save-as@face.text=Save _As
   save-as@properties[overwriteTitle]=Are you sure?
   save-as@properties[overwriteMessage]=\
      The file ''{0}'' exists, \
      do you want to overwrite it?

If you need more control you can override the confirmOverwrite() method to display your own custom dialog.