edu.cs.ai.kreator.control
Class FileController

java.lang.Object
  extended by edu.cs.ai.kreator.control.FileController
All Implemented Interfaces:
java.awt.event.ActionListener, java.util.EventListener

public class FileController
extends java.lang.Object
implements java.awt.event.ActionListener

This Controller handles every file-operation, like opening, saving or closing DataFiles.
In addition to that, it contains methods to export files to latex or spirit.

Author:
Niklas Wulf, Adam Chachaj

Field Summary
static java.lang.String CMD_CLOSEALLFILES
           
static java.lang.String CMD_CLOSEFILE
           
static java.lang.String CMD_CLOSEOTHERFILES
           
static java.lang.String CMD_EXPORTTOLATEXFROMTAB
           
static java.lang.String CMD_EXPORTTOLATEXFROMTREE
           
static java.lang.String CMD_NEWFILE
           
static java.lang.String CMD_NEWKNOWLEGEBASEFILE
           
static java.lang.String CMD_NEWSAMPLECOLLECTIONFILE
           
static java.lang.String CMD_NEWSCRIPTFILE
           
static java.lang.String CMD_OPENFILES
           
static java.lang.String CMD_PRINT
           
static java.lang.String CMD_REMOVEFILEFROMFILETAB
           
static java.lang.String CMD_REMOVEFILEFROMTREE
           
static java.lang.String CMD_RENAMEFILE
           
static java.lang.String CMD_RENAMEFILEFROMFILETAB
           
static java.lang.String CMD_SAVEALLFILES
           
static java.lang.String CMD_SAVEFILE
           
static java.lang.String CMD_SAVEFILEAS
           
static org.apache.log4j.Logger LOG
           
 
Constructor Summary
FileController()
           
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
           
 boolean checkForExistingAndSaved(DataFile file)
          Checks whether the given file exists and has no unsaved changes.
protected  boolean closeAllDataFiles()
          Tries to close all DataFiles that are opened in the GUI.
protected  boolean closeAllProjectDataFiles()
          Check out, whether the file is contained in the current project
 boolean closeDataFile()
          Tells the GUI to close the current DataFile.
 boolean closeDataFile(DataFile file)
          Tells the GUI to close a DataFile.
protected  void closeDataFileForced(DataFile file)
          Tells the GUI to close a DataFile.
protected  boolean closeOtherDataFiles()
          Tries to close all DataFiles that are opened in the GUI except the current working file
<T extends DataFile>
T
createDataFile(java.io.File file, java.lang.Class<T> Class)
          Creates an appropriate instance of DataFile based on the extension of the given path (e.g. a KnowledgebaseFile for *.rme, ...).
 void deleteAndRemoveDataFileConfirmed(DataFile file)
          Deletes the given file from the project and file-system.
protected  void deleteDataFile(DataFile file)
          Deletes the give DataFile.
 boolean deleteExistingFile(DataFile file, java.lang.String message)
          Checks whether a given file exists.
 java.lang.String exportToLatexFromConsole(java.lang.String file)
          Exports the given data file to LaTeX and
prints the LaTex code in Console.
 DataFile getDataFile(java.io.File file)
          Generates an appropriate instance of DataFile based on the extension of the file (e.g. a KnowledgebaseFile for *.rme, ...).
<T extends DataFile>
T
getDataFile(java.io.File file, java.lang.Class<T> Class)
          Generates an appropriate instance of DataFile based on the extension of the file (e.g. a KnowledgebaseFile for *.rme, ...).
 DataFile getDataFile(java.lang.String path)
          Generates an appropriate instance of DataFile based on the extension of the given path (e.g. a KnowledgebaseFile for *.rme, ...).
<T extends DataFile>
T
getDataFile(java.lang.String path, java.lang.Class<T> Class)
          Generates an appropriate instance of DataFile based on the extension of the given path (e.g. a KnowledgebaseFile for *.rme, ...).
<T extends DataFile>
T
getExistingDataFile(java.io.File file, java.lang.Class<T> Class)
          Generates an appropriate instance of DataFile based on the extension of the given path (e.g. a KnowledgebaseFile for *.rme, ...).
<T extends DataFile>
T
getExistingDataFile(java.lang.String path, java.lang.Class<T> Class)
          Generates an appropriate instance of DataFile based on the extension of the given path (e.g. a KnowledgebaseFile for *.rme, ...).
 DataFile getNewDataFileName(java.io.File fileOrDir, AbstractFileFilter ff)
          Shows the user a "New file..."
protected  java.io.File getStartingDirectory()
          Returns a directory, which should be used as the starting directory in file choosers.
 void init()
           
 DataFile newAndOpenDataFile(AbstractFileFilter ff)
          Shows the user a "New file..."
 void openDataFile(DataFile file)
          Opens a DataFile in the GUI.
 void saveDataFile(DataFile file)
          Tells the file to save itself.
 DataFile saveDataFileAs(DataFile file)
          Does everything to "save a file as...".
 boolean shutDown()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG

public static final org.apache.log4j.Logger LOG

CMD_NEWFILE

public static final java.lang.String CMD_NEWFILE
See Also:
Constant Field Values

CMD_NEWKNOWLEGEBASEFILE

public static final java.lang.String CMD_NEWKNOWLEGEBASEFILE
See Also:
Constant Field Values

CMD_NEWSCRIPTFILE

public static final java.lang.String CMD_NEWSCRIPTFILE
See Also:
Constant Field Values

CMD_NEWSAMPLECOLLECTIONFILE

public static final java.lang.String CMD_NEWSAMPLECOLLECTIONFILE
See Also:
Constant Field Values

CMD_OPENFILES

public static final java.lang.String CMD_OPENFILES
See Also:
Constant Field Values

CMD_CLOSEFILE

public static final java.lang.String CMD_CLOSEFILE
See Also:
Constant Field Values

CMD_CLOSEOTHERFILES

public static final java.lang.String CMD_CLOSEOTHERFILES
See Also:
Constant Field Values

CMD_CLOSEALLFILES

public static final java.lang.String CMD_CLOSEALLFILES
See Also:
Constant Field Values

CMD_SAVEFILE

public static final java.lang.String CMD_SAVEFILE
See Also:
Constant Field Values

CMD_SAVEFILEAS

public static final java.lang.String CMD_SAVEFILEAS
See Also:
Constant Field Values

CMD_SAVEALLFILES

public static final java.lang.String CMD_SAVEALLFILES
See Also:
Constant Field Values

CMD_PRINT

public static final java.lang.String CMD_PRINT
See Also:
Constant Field Values

CMD_RENAMEFILE

public static final java.lang.String CMD_RENAMEFILE
See Also:
Constant Field Values

CMD_RENAMEFILEFROMFILETAB

public static final java.lang.String CMD_RENAMEFILEFROMFILETAB
See Also:
Constant Field Values

CMD_REMOVEFILEFROMTREE

public static final java.lang.String CMD_REMOVEFILEFROMTREE
See Also:
Constant Field Values

CMD_REMOVEFILEFROMFILETAB

public static final java.lang.String CMD_REMOVEFILEFROMFILETAB
See Also:
Constant Field Values

CMD_EXPORTTOLATEXFROMTAB

public static final java.lang.String CMD_EXPORTTOLATEXFROMTAB
See Also:
Constant Field Values

CMD_EXPORTTOLATEXFROMTREE

public static final java.lang.String CMD_EXPORTTOLATEXFROMTREE
See Also:
Constant Field Values
Constructor Detail

FileController

public FileController()
Method Detail

init

public void init()

shutDown

public boolean shutDown()

getDataFile

public <T extends DataFile> T getDataFile(java.io.File file,
                                          java.lang.Class<T> Class)
                               throws DataFileNotGeneratedException,
                                      UnsupportedFileTypeException
Generates an appropriate instance of DataFile based on the extension of the file (e.g. a KnowledgebaseFile for *.rme, ...).

The generated file will be cast to and the return type will be of the given class automatically.
If the file extension belongs to a DataFile, which cannot be cast to this class an UnsupportedFileTypeException will be thrown.

The file will neither be created on the filesystem, nor will be checked, if it actually exists.
Use #getExistingDataFile() or #createDataFile() when needed.

Parameters:
file - The file, for which we want to generate an appropriate DataFile
Class - The class to which the given file should be cast. If this cast is not allowed an UnsupportedFileTypeException will be thrown.
Returns:
A DataFile representing this File
Throws:
UnsupportedFileTypeException - if the given file type (read: extension) cannot be cast to the given class
DataFileNotGeneratedException - if something different than unsupported file type goes wrong

getDataFile

public DataFile getDataFile(java.io.File file)
                     throws DataFileNotGeneratedException
Generates an appropriate instance of DataFile based on the extension of the file (e.g. a KnowledgebaseFile for *.rme, ...).

The file will neither be created on the filesystem, nor will be checked, if it actually exists.
Use #getExistingDataFile() or #createDataFile() when needed.

Parameters:
file -
Returns:
A DataFile with the given path.
Throws:
DataFileNotGeneratedException - when something goes wrong.
See Also:
getDataFile(File, Class)

getDataFile

public <T extends DataFile> T getDataFile(java.lang.String path,
                                          java.lang.Class<T> Class)
                               throws DataFileNotGeneratedException,
                                      UnsupportedFileTypeException
Generates an appropriate instance of DataFile based on the extension of the given path (e.g. a KnowledgebaseFile for *.rme, ...).

The generated file will be cast to and the return type will be of the given class automatically.
If the file extension belongs to a DataFile, which cannot be cast to this class an UnsupportedFileTypeException will be thrown.

The file will neither be created on the filesystem, nor will be checked, if it actually exists.
Use #getExistingDataFile() or #createDataFile() when needed.

Parameters:
path - the path of the file
Class - The class to which the given file should be cast. If this cast is not allowed an UnsupportedFileTypeException will be thrown.
Returns:
a DataFile with the given path
Throws:
UnsupportedFileTypeException - if the given file type (read: extension) cannot be cast to the given class
DataFileNotGeneratedException - if something different than unsupported file type goes wrong
See Also:
getDataFile(File)

getDataFile

public DataFile getDataFile(java.lang.String path)
                     throws DataFileNotGeneratedException
Generates an appropriate instance of DataFile based on the extension of the given path (e.g. a KnowledgebaseFile for *.rme, ...).

The file will neither be created on the filesystem, nor will be checked, if it actually exists.
Use #getExistingDataFile() or #createDataFile() when needed.

Parameters:
path - the path of the file
Class - The class to which the given file should be cast. If this cast is not allowed an UnsupportedFileTypeException will be thrown.
Returns:
a DataFile with the given path
Throws:
UnsupportedFileTypeException
java.io.FileNotFoundException
DataFileNotGeneratedException
See Also:
getDataFile(String)

getExistingDataFile

public <T extends DataFile> T getExistingDataFile(java.io.File file,
                                                  java.lang.Class<T> Class)
                                       throws java.io.FileNotFoundException,
                                              DataFileNotGeneratedException,
                                              UnsupportedFileTypeException
Generates an appropriate instance of DataFile based on the extension of the given path (e.g. a KnowledgebaseFile for *.rme, ...).
Checks, whether the file exists. If not, throws an UnsupportedFileTypeException.

Parameters:
file -
Class -
Returns:
Throws:
java.io.FileNotFoundException - if the file does not exist
UnsupportedFileTypeException - if the given file type (read: extension) cannot be cast to the given class
DataFileNotGeneratedException - if something different than unsupported file type goes wrong
See Also:
getDataFile(File, Class)

getExistingDataFile

public <T extends DataFile> T getExistingDataFile(java.lang.String path,
                                                  java.lang.Class<T> Class)
                                       throws DataFileNotGeneratedException,
                                              UnsupportedFileTypeException,
                                              java.io.FileNotFoundException
Generates an appropriate instance of DataFile based on the extension of the given path (e.g. a KnowledgebaseFile for *.rme, ...).
Checks, whether the file exists. If not, throws an UnsupportedFileTypeException.

Parameters:
file -
Returns:
Throws:
java.io.FileNotFoundException - if the file does not exist
UnsupportedFileTypeException - if the given file type (read: extension) cannot be cast to the given class
DataFileNotGeneratedException - if something different than unsupported file type goes wrong
java.io.FileNotFoundException
See Also:
getExistingDataFile(File, Class)

createDataFile

public <T extends DataFile> T createDataFile(java.io.File file,
                                             java.lang.Class<T> Class)
                                  throws DataFileNotGeneratedException,
                                         UnsupportedFileTypeException,
                                         FileExistsException
Creates an appropriate instance of DataFile based on the extension of the given path (e.g. a KnowledgebaseFile for *.rme, ...).
Creates the file on the filesystem. If the file already exists, it throws a FileExistsException

Parameters:
file -
Class -
Returns:
Throws:
java.io.FileNotFoundException - if the file does not exist
UnsupportedFileTypeException - if the given file type (read: extension) cannot be cast to the given class
DataFileNotGeneratedException - if something different than unsupported file type goes wrong
FileExistsException
See Also:
getDataFile(File, Class)

openDataFile

public void openDataFile(DataFile file)
Opens a DataFile in the GUI. Asks the user, if he wants to add the file to the current project, if it isn't already.

Parameters:
file -

closeDataFile

public boolean closeDataFile(DataFile file)
Tells the GUI to close a DataFile. This method respects the stained status of the file. If the file has stained views, it asks the user if he wants to save it.

If you don't want to respect the stained status of a file see closeDataFileForced(DataFile)

Parameters:
file -
Returns:
true if the file has been closed, false otherwise (e.g. the user chose to abort the closing operation)

closeDataFile

public boolean closeDataFile()
Tells the GUI to close the current DataFile. It respects the stained status of the file.
See closeDataFile(DataFile) for details.

Parameters:
file -
Returns:
true if the file has been closed, false otherwise (e.g. the user chose to abort the closing operation)

closeDataFileForced

protected void closeDataFileForced(DataFile file)
Tells the GUI to close a DataFile. Does not respect the stained status of the file!

Parameters:
file -

closeAllDataFiles

protected boolean closeAllDataFiles()
Tries to close all DataFiles that are opened in the GUI. Respects their stained status.
See closeAllDataFiles(List).

Returns:
true if all files have been closed, false otherwise

closeOtherDataFiles

protected boolean closeOtherDataFiles()
Tries to close all DataFiles that are opened in the GUI except the current working file

Returns:
true if all other files have been closed, false otherwise

closeAllProjectDataFiles

protected boolean closeAllProjectDataFiles()
Check out, whether the file is contained in the current project

Returns:
true if the contained files have been closed

getNewDataFileName

public DataFile getNewDataFileName(java.io.File fileOrDir,
                                   AbstractFileFilter ff)
                            throws DataFileNotGeneratedException
Shows the user a "New file..."-dialog. Checks whether the chosen filename exists, and asks the user if he wants to overwrite it. Just creates a new DataFile, without saving it (i.e. the file does NOT exist on the file system after running this method!).

Parameters:
fileOrDir - if this is a directory the filechooser starts in it. If it is a file, the filechoose will use it as the initial filename.
ff - the FileFilter, which the dialog will use.
Returns:
a new not yet saved DataFile, or null, if the user does not want a new file.
Throws:
DataFileNotGeneratedException

newAndOpenDataFile

public DataFile newAndOpenDataFile(AbstractFileFilter ff)
                            throws FileNotSavedException,
                                   DataFileNotGeneratedException
Shows the user a "New file..."-dialog. Creates the file on the filesystem and opens it in the GUI.

Parameters:
ff - The FileFilter used by the "New file..."-dialog
Throws:
FileNotSavedException
DataFileNotGeneratedException
See Also:
#getnewDataFileName()

saveDataFile

public void saveDataFile(DataFile file)
                  throws FileNotSavedException
Tells the file to save itself.
Unfortunately we cannot be sure, the file exists after calling this method, due to possible IO errors. This method does nothing, if the file has no unsaved changes.

Parameters:
file -
Throws:
FileNotSavedException

checkForExistingAndSaved

public boolean checkForExistingAndSaved(DataFile file)
Checks whether the given file exists and has no unsaved changes. If it has, asks the user if he wants to save the file. Returns true, if the file has been saved and exists.

Parameters:
file - the file to be checked
Returns:
true, if the file has been saved and exists.

saveDataFileAs

public DataFile saveDataFileAs(DataFile file)
                        throws FileNotSavedException,
                               DataFileNotGeneratedException
Does everything to "save a file as...". First asks the user for a new filename. If this file exists, asks the user if he wants to overwrite it. Then saves the file's tab's content into the new file. Opens the new file in the GUI.

Parameters:
file -
Returns:
the saved DataFile or null, if the user chose to abort the operation.
Throws:
FileNotSavedException
DataFileNotGeneratedException

deleteDataFile

protected void deleteDataFile(DataFile file)
Deletes the give DataFile. Does neither confirm the closing nor the deletion of the file, even it it has unsaved changes.


deleteAndRemoveDataFileConfirmed

public void deleteAndRemoveDataFileConfirmed(DataFile file)
Deletes the given file from the project and file-system. Confirms both operations.

Parameters:
file -

deleteExistingFile

public boolean deleteExistingFile(DataFile file,
                                  java.lang.String message)
Checks whether a given file exists. Asks the user if he wants to delete the existing file. Returns true, if the file does not exist or the user chose to delete it. The confirmation dialog will display the given message.

Parameters:
file - the file to be deleted
message - the dialog's message
Returns:
true if the file does not exist after execution of this method

exportToLatexFromConsole

public java.lang.String exportToLatexFromConsole(java.lang.String file)
                                          throws ParsingException,
                                                 java.io.IOException,
                                                 DataFileNotGeneratedException
Exports the given data file to LaTeX and
prints the LaTex code in Console.

Parameters:
file -
Throws:
ParsingException
java.io.IOException
DataFileNotGeneratedException

getStartingDirectory

protected java.io.File getStartingDirectory()
Returns a directory, which should be used as the starting directory in file choosers. If a project is active, this is the project's directory. Otherwise KreatorMain.getLastChosenDir() is probed for a valid directory, and if this also fails Configuration.GENERAL__STARTING_DIRECTORY will be used.

Returns:
a directory, which should be used as the starting directory in file choosers.

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
Specified by:
actionPerformed in interface java.awt.event.ActionListener