Class TextDocument

  • All Implemented Interfaces:
    Document

    public class TextDocument
    extends AbstractDocument
    A example document that contains plain boring old text and saves it to ascii files. Here you can see some sample implementations of the open, save, saveAs, and close methods. You should also notice that this class adds methods for accessing the contained document's data in an application-useful format. (In this case, there are just simple getText and setText methods.) The setText properly realized that the document's data has been changed and sets the Dirty flag. This is used in the application tutorial by the StoragePolicy to prevent a user from closing the document without saving changes.

    This class also contains a DocumentFactory for documents of this type. The document factory is used by an application to create documents of this type.

    Version:
    $Id$
    Author:
    Steve Neuendorffer
    • Constructor Detail

      • TextDocument

        public TextDocument​(Application application)
        Create an text document for the given application containing an empty string.
    • Method Detail

      • close

        public void close()
        Close the document. Do not attempt to save the document first or do any other user-interface things like that. This method can thrown an exception if there is a failure, but it should only do do if there is no way it can recover. Note that actions such as querying the user to save a modified document and so on are the responsibility of the application, not the Document implementation.
        Specified by:
        close in interface Document
        Overrides:
        close in class AbstractDocument
      • getText

        public java.lang.String getText()
        Return the text contained in this document.
      • open

        public void open()
                  throws java.lang.Exception
        Open the document from its current file or URL. Throw an exception if the operation failed.
        Specified by:
        open in interface Document
        Specified by:
        open in class AbstractDocument
        Throws:
        java.lang.Exception - If the close operation fails.
      • save

        public void save()
                  throws java.lang.Exception
        Save the document to its current file or URL. Throw an exception if the operation failed. Reasons for failure might include the fact that the file is not writable, or that the document has a URL but we haven't implemented HTTP-DAV support yet...
        Specified by:
        save in interface Document
        Specified by:
        save in class AbstractDocument
        Throws:
        java.lang.Exception - If the save operation fails.
      • saveAs

        public void saveAs​(java.io.File file)
                    throws java.lang.Exception
        Save the document to the given file. Throw an exception if the operation failed. Return true if successful, false if not. Do not change the file attribute to the new File object as that is the responsibility of the application, which it will do according to its storage policy.
        Specified by:
        saveAs in interface Document
        Specified by:
        saveAs in class AbstractDocument
        Throws:
        java.lang.Exception - If the save-as operation fails.
        See Also:
        save()
      • saveAs

        public void saveAs​(java.net.URL url)
                    throws java.lang.Exception
        Save the document to the given URL. Throw an exception if the operation failed. Do not change the URL attribute to the new URL object as that is the responsibility of the application, which it will do according to its storage policy.
        Specified by:
        saveAs in interface Document
        Specified by:
        saveAs in class AbstractDocument
        Throws:
        java.lang.Exception - If the save-as operation fails.
        See Also:
        save()
      • setText

        public void setText​(java.lang.String text)
        Set the text contained by this document. If the given text is different from the previously contained text, then set the dirty flag.