Class TextFileHolder

java.lang.Object
  |
  +--TextFileHolder

public final class TextFileHolder
extends Object

Provides ability to connect in read-only mode to files, much like the Acrobat Reader, but GUI is assumed to be implemented via interface TextFileView.

In particular, every file is held by at most one instance, there is only one instance pointing to no file, if it is the only instance at all. Closing the last instance only implies a system exit, if it is no more connected to a file.

Implementation Detail:
The set of currently active instances is held in a static private class variable of type LinkedList.

Author:
Christian Liebchen
See Also:
Acrobat Reader, LinkedList, TextFileView

Constructor Summary
TextFileHolder(String filename, TextFileView view)
          Opens a given file and shows it in a given TextFileView.
 
Method Summary
static File fetchFile(String filename)
          Provides a file object from a given filename.
 String getContent()
          Provides the contents of the file held by this object.
 String getTargetName()
          Provides the name of the file held by this object.
 boolean isEmpty()
          Tells whether this object does not hold any file.
 void processClose()
          Usually notified this object to clean up itself and destroy its view.
static void processExit()
          Terminated this Runtime.
static void processOpen(String filename, TextFileView view)
          Just create a new TextFileHolder showing file filename in the given TextFileView
static String readFile(File file)
          Provides the contents of a given file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TextFileHolder

public TextFileHolder(String filename,
                      TextFileView view)
               throws IOException
Opens a given file and shows it in a given TextFileView. Opening the null-String file results only in a new TextFileView, if currently no instance of TextFileHolder exists. If the given file is already held by another instance of TextFileHolder, its view will come into front.

Parameters:
filename - name of the file to be held by this new object and that should be shown in view
view - if a new TextFileHolder is accepted due to the policy described in this constructor's comments, the contents of its target will be shown in view
Throws:
if - I/O errors occur, in particular if filename does not exist, is not a regular file, or is not readable
IOException
Method Detail

getTargetName

public final String getTargetName()
Provides the name of the file held by this object.

Returns:
name of the file held by this object, or null if no file is held by this instance

getContent

public final String getContent()
Provides the contents of the file held by this object.

Returns:
contents of the file held by this object, or null if no file is held by this instance

processOpen

public static final void processOpen(String filename,
                                     TextFileView view)
                              throws IOException
Just create a new TextFileHolder showing file filename in the given TextFileView

Parameters:
filename - name of the file to be held by a new object and that should be shown in view
view - if a new TextFileHolder is accepted due to the policy described in the constructor's comments, the contents of its target will be shown in view
Throws:
if - I/O errors occur, in particular if filename does not exist, is not a regular file, or is not readable
IOException
See Also:
TextFileHolder(String, TextFileView)

processClose

public final void processClose()
Usually notified this object to clean up itself and destroy its view. If the current object is the last instance of TextFileHolder, then we distinguish two cases. Either this object still holds a file. Then the connection to that file will be dropped. Or this object did not held any file already before. Then this Runtime will be shut.

See Also:
processExit()

processExit

public static final void processExit()
Terminated this Runtime.

See Also:
System.exit(int)

isEmpty

public final boolean isEmpty()
Tells whether this object does not hold any file.

Returns:
true iff this object does not hold any file

readFile

public static String readFile(File file)
                       throws IOException
Provides the contents of a given file.

Parameters:
file - the file to provide its contents
Returns:
the contents of the given file
Throws:
IOException - if opening, reading, or closing file via a BufferedReader technically fails
See Also:
BufferedReader

fetchFile

public static File fetchFile(String filename)
                      throws IOException
Provides a file object from a given filename.

Parameters:
filename - the name of the file to get an object to
Returns:
an object representing the file with the given name
Throws:
IOException - if the given file does not exist, is not a regular file, or is not readable