Class: TextDocument
Before we actually dive into writing an application, we need some way for the application to store its data. In many cases, applications using the same type of data can share a document class, but every application must have at least one type of document it can handle. Some complex applications might even be able to handle a wide variety of documents. Since we will be writing a text-editing application, we need a document that can store some text. We start by extending AbstractDocument, which implements the basic property methods and can propagate property change events to its listeners.
public class TextDocument extends AbstractDocument {
To this we are going to add a Text property which will contain our application data. The getText() method is boring, but the setText() method does something interesting:
public void setText(String text) { if(_text != text) { setDirty(true); _text = text; } }
When the text changes, the data in the document is "dirty" since it hasn't been saved yet. The application that we write will use this flag to try to prevent the application from be exited without saving the text that is being edited. The implementations of the open and saveAs methods are fairly straightforward and simply save the text property to a file using a FileWriter and read it back using a FileReader. Note that from the point of view of the application, exactly how the data is stored is unimportant. It might as well be GZIP'd Postscript, as long as we can read it and write it.
The save method is simply implemented in terms of the saveAs method. The close method does nothing because this document implementation makes use of no persistent resources. Notice that the close operation from the point of view of the document is not the same as the close operationg from the point of view of the application. When the application closes a document, its storage policy will almost always save the document first, and then call the document's close method.
public void close () { // DO NOTHING. } public void save () throws Exception { saveAs(getFile()); }
The final noteworthy part of the class is the document factory inner class. The implementation is rather boring, but it allows the application infrastructure to create new documents in an abstract way. (For an example of how this is used, look at the DefaultActions.netAction method.)
public static class Factory implements DocumentFactory { public Document createDocument (Application app) { TextDocument d = new TextDocument(app); return d; } ... }