The class DocumentPane
DocumentPane implements an application centric approach of a document, wrapping the classes of javax.swing together with application and GUI functionality into one class.
DocumentPane follows the idea of having a single interface between an application and its documents. It serves as the single point for displaying, editing, opening, saving etc. For this, it combines GUI elements and 'MVC' elements to form one consistent and compact class to work with in applications.
Elements of DocumentPane
The major element of DocumentPane is a JEditorPane. DocumentPane extends JPanel and combines a JScrollPane and the J EditorPane as the only visible components.
In addition it has fields for storing the name of the document, the source where it has been loaded from and a field indicating whether or not the document has changed. Finally constants for a default document name as well as two cursor definitions are contained as fields in the DocumentPane.
There are two fields reflecting save operations as well: saveInProgress is set to true while a save operation runs and saveSuccessful is set by a save operation before (false) and after a save (true, if and only if all went fine) to indicate any errors while saving.
Last but not least, DocumentPane implements interface DocumentListener to recognize changes.
Interface DocumentListener
In field textChanged class DocumentPane 'remembers' if there have been changes to it's document since its creation or since the last save to file operation. But the DocumentPane needs to be notified about changes for being able to 'remember' them. It implements interface DocumentListener for that purpose.
Interface DocumentListener defines what a class would have to do to listen to DocumentEvents . DocumentEvents are fired to registered listeners whenever a document is changed in any way for instance.
Class DocumentPane defines methods insertUpdate, changedUpdate and removeUpdate to implement the DocumentListener interface. By doing that, class DocumentPane is a DocumentListener and can register itself with any document to listen for changes.
DocumentPane sets field textChanged to true whenever it is notified of a DocumentEvent.