ewe.io
Class FileSaver

java.lang.Object
  extended byewe.util.Errorable
      extended byewe.io.FileSaver
All Implemented Interfaces:
EventListener

public class FileSaver
extends Errorable
implements EventListener


Field Summary
 String defaultExtension
          This is the default extension for the file name.
protected  String defaultFileName
           
 boolean dontAppendLineFeed
          This applies for saving text.
 Exception exception
          This is set to any exception that may have occured during loading/saving.
 File fileModel
          This is the file model to use.
 boolean hasChanged
          This says whether the current file has been changed.
 boolean ignoreParentFrame
          Set this true to always ignore the parent Frame and have all new Frames appear in their own window.
 String lastSaved
          This is the last file that was saved.
 Vector masks
          This is a list of masks to be sent to the FileChooser.
 String openTitle
          This is the title of the file open box.
 String persistentHistoryKey
          This is assigned to the persistentHistoryKey of the FileChooser created.
 boolean reportIOErrors
          If this is true then any IO errors encountered during doSave() or doOpen() is reported.
 String saveTitle
          This is the title of the file save box.
 TextCodec textCodec
          You can set this to a textCodec to use for reading and writing text data using this FileSaver.
 
Fields inherited from class ewe.util.Errorable
error
 
Constructor Summary
FileSaver()
           
FileSaver(String defaultFileName)
           
FileSaver(String defaultFileName, String actualFileName)
           
 
Method Summary
 boolean checkExit(Form f, Object dataToSave, int retCode)
          A Form can call this when the user has requested to close the Form.
 boolean checkSave(Object data, Frame parent)
          If hasChanged is true, this will attempt to save the current object by prompting the user to confirm saving changes.
protected  boolean closeAndReturn(String fileName, Frame parent, Stream s, boolean success)
           
 boolean doOpen(Object toOpen, String fileName, Frame parent)
          This attempts an open operation on the data - and you should override this as necessary.
 boolean doSave(Object toSave, String fileName, Frame parent)
          This attempts a save operation on the data - and you should override this as necessary.
 Exception getException()
           
protected  FileChooserParameters getFileChooser(String type, String initial, File model)
           
 boolean getHasChanged()
           
 String getInitialFile()
           
protected  Stream getInputStream(String fileName, Frame parent)
          This creates a Stream to read from to the source file name.
 String getLastSaved()
           
protected  Stream getOutputStream(String fileName, Frame parent)
          This creates a Stream to write to the destination file name.
 File getSavedFile()
          This returns the last saved or opened File.
 boolean newData(Object oldData, String newFileName, Frame parent)
          This tells the FileSaver that new data is about to be created and that the old data should be saved if it has been changed.
 void newData(String newFileName)
          This sets hasChanged to false and lastSaved to newFileName.
 void onEvent(Event ev)
           
 String open(Frame parent)
          This brings up a FileChooser box to let the user select a file to open.
 boolean open(Object oldData, Object newData, Frame parent)
          This attempts to open a file, after saving the old file if necessary.
 String open(String oldData, Frame parent)
          This opens and reads the file as a string.
 String openText(String fileName, Frame parent)
           
protected  boolean returnError(String error, Frame parent)
           
 boolean save(boolean saveAs, Object data, Frame parent)
          This attempts to save the file.
 String save(Frame parent)
          This returns a String which is the file name of the destination to save to.
 String saveAs(Frame parent)
          This brings up the FileChooser box and lets the user select a file to save to.
 void setDefaultFileType(String extension, String description)
          This will sets the default file type and adds the mask to the list of masks.
 void setHasChanged(boolean hasChanged)
           
 void setLastSaved(String lastSaved)
           
 boolean tryOpen(Object data, String file, Frame parent)
           
 String tryOpen(String file, Frame parent)
          This checks if the specified file is readable.
 
Methods inherited from class ewe.util.Errorable
returnError, returnError, returnError
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Field Detail

fileModel

public File fileModel
This is the file model to use. By default it is a normal local file.


lastSaved

public String lastSaved
This is the last file that was saved. If it is null then the FileChooser box will be brought up on saving.


defaultFileName

protected String defaultFileName

defaultExtension

public String defaultExtension
This is the default extension for the file name. It can be null for no extension.


saveTitle

public String saveTitle
This is the title of the file save box. By default it is "Save file as...";


openTitle

public String openTitle
This is the title of the file open box. By default it is "Open file...";


masks

public Vector masks
This is a list of masks to be sent to the FileChooser. You can explicitly put masks in here or you can use the setDefaultFileType() method.


exception

public Exception exception
This is set to any exception that may have occured during loading/saving.


persistentHistoryKey

public String persistentHistoryKey
This is assigned to the persistentHistoryKey of the FileChooser created.


ignoreParentFrame

public boolean ignoreParentFrame
Set this true to always ignore the parent Frame and have all new Frames appear in their own window.


textCodec

public TextCodec textCodec
You can set this to a textCodec to use for reading and writing text data using this FileSaver.


dontAppendLineFeed

public boolean dontAppendLineFeed
This applies for saving text. If this is true then an ending LineFeed will not be appended to the saved text.


hasChanged

public boolean hasChanged
This says whether the current file has been changed.


reportIOErrors

public boolean reportIOErrors
If this is true then any IO errors encountered during doSave() or doOpen() is reported.

Constructor Detail

FileSaver

public FileSaver()

FileSaver

public FileSaver(String defaultFileName)

FileSaver

public FileSaver(String defaultFileName,
                 String actualFileName)
Method Detail

setDefaultFileType

public void setDefaultFileType(String extension,
                               String description)
This will sets the default file type and adds the mask to the list of masks. By default, "*.* - All files." is initially added to the masks. Do NOT put a '.' before the extension.


save

public String save(Frame parent)
This returns a String which is the file name of the destination to save to. If lastSaved is null (i.e. no save has been done yet, it will call saveAs(parent).


getInitialFile

public String getInitialFile()

getFileChooser

protected FileChooserParameters getFileChooser(String type,
                                               String initial,
                                               File model)

saveAs

public String saveAs(Frame parent)
This brings up the FileChooser box and lets the user select a file to save to. If the user cancels this will return null, otherwise it will return the file name chosen by the user.


tryOpen

public String tryOpen(String file,
                      Frame parent)
This checks if the specified file is readable. If it is it will return the file. If it is not it will call open(parent) and return the file selected by the user.


tryOpen

public boolean tryOpen(Object data,
                       String file,
                       Frame parent)

open

public String open(Frame parent)
This brings up a FileChooser box to let the user select a file to open. If the user cancels it will return null.


newData

public void newData(String newFileName)
This sets hasChanged to false and lastSaved to newFileName.


checkSave

public boolean checkSave(Object data,
                         Frame parent)
If hasChanged is true, this will attempt to save the current object by prompting the user to confirm saving changes.

This method return true if:

  • hasChanged is false - meaning no save is necessary.
  • hasChanged is true and the user says "no" to saving the current data.
  • hasChanged is true and the user says "yes" to saving the current data, and the data is successfully saved.
  • If the user says "cancel" or a save fails the method will return false.


    returnError

    protected boolean returnError(String error,
                                  Frame parent)

    getOutputStream

    protected Stream getOutputStream(String fileName,
                                     Frame parent)
                              throws IOException
    This creates a Stream to write to the destination file name.

    Parameters:
    fileName - The file name to write to.
    parent - The parent frame (may be null).
    Returns:
    A Stream for writing to the destination file.
    Throws:
    IOException - if no Stream to the file could be created. If reportIOErrors is true, then the exception will be displayed in a standard exception dialog.

    getInputStream

    protected Stream getInputStream(String fileName,
                                    Frame parent)
                             throws IOException
    This creates a Stream to read from to the source file name.

    Parameters:
    fileName - The file name to read from.
    parent - The parent frame (may be null).
    Returns:
    A Stream for writing to the destination file.
    Throws:
    IOException - if no Stream to the file could be created. If reportIOErrors is true, then the exception will be displayed in a standard exception dialog.

    closeAndReturn

    protected boolean closeAndReturn(String fileName,
                                     Frame parent,
                                     Stream s,
                                     boolean success)

    doSave

    public boolean doSave(Object toSave,
                          String fileName,
                          Frame parent)
    This attempts a save operation on the data - and you should override this as necessary. It attempts to convert the data to a string and then saves it. Any error in saving is reported.


    doOpen

    public boolean doOpen(Object toOpen,
                          String fileName,
                          Frame parent)
    This attempts an open operation on the data - and you should override this as necessary. It attempts to read a line of text from the file and then call textDecode() on the object.


    open

    public boolean open(Object oldData,
                        Object newData,
                        Frame parent)
    This attempts to open a file, after saving the old file if necessary.


    openText

    public String openText(String fileName,
                           Frame parent)

    open

    public String open(String oldData,
                       Frame parent)
    This opens and reads the file as a string. It will call checkSave() on the oldData if it is not null.

    Parameters:
    oldData - Data to check for saving first (may be null).
    parent - The parent Frame (may be null).
    Returns:
    The text contained in the file or null on error or cancellation by the user.

    save

    public boolean save(boolean saveAs,
                        Object data,
                        Frame parent)
    This attempts to save the file.


    newData

    public boolean newData(Object oldData,
                           String newFileName,
                           Frame parent)
    This tells the FileSaver that new data is about to be created and that the old data should be saved if it has been changed. newFileName can be null to say that the new data is as yet unnamed.


    onEvent

    public void onEvent(Event ev)
    Specified by:
    onEvent in interface EventListener

    checkExit

    public boolean checkExit(Form f,
                             Object dataToSave,
                             int retCode)
    A Form can call this when the user has requested to close the Form. It should be called within the canClose() method.

    Parameters:
    f - The Form checking for exit.
    retCode - The return code the Form should exit with.
    Returns:
    true if the Form should exit, false if it should not.

    getSavedFile

    public File getSavedFile()
    This returns the last saved or opened File. If it is null then no save/open has been done.


    getLastSaved

    public String getLastSaved()

    getHasChanged

    public boolean getHasChanged()

    getException

    public Exception getException()

    setHasChanged

    public void setHasChanged(boolean hasChanged)

    setLastSaved

    public void setLastSaved(String lastSaved)