I've been reading the javadocs trying to grasp around the swing Document API but I cant get something sensible out of it because there's so many classes: Document, StyledDocument, AbstractDocument, DefaultStyledDocument, PlainDocument, HTMLDocument, and someone mentioned DocumentFilter. This question is more on a general basis so can someone give an overview of the differences between the implementations and when the different interfaces and abstracts are for?
For my specific case what I want to achieve is a data structure that will:
Anything that i can use for this or is it better to extend something? If so, what?
All of the Document classes you list have the same base functionality and each expands based on a niche that needs to be filled. Really, it's just a matter of realizing what you need to do and use the appropriate document type. For instance, if I am editing an HTML file, then I would use the HTMLDocument class.
I included a brief description of each of the Document classes you requested in your question below.
Document
The Document interface represents the entire HTML or XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document's data.
This is the interface that all other Document types will inherit from. It provides the contract for all other Document types to follow.
AbstractDocument
This class implements a locking mechanism for the document it allows multiple readers or one writer, and writers must wait until all observers of the document have been notified of a previous change before beginning another mutation to the document.
This class allows you to work with different types of documents and uses a very lose ruleset. This class is more difficult to implement because it is so generic.
StyledDocument
Another interface that provides a contract for all styled documents. DefaultStyledDocument implements this interface, so we'll get to that next.
DefaultStyledDocument
A document that can be marked up with character and paragraph styles in a manner similar to the Rich Text Format. The element structure for this document represents style crossings for style runs. These style runs are mapped into a paragraph element structure (which may reside in some other structure). The style runs break at paragraph boundaries since logical styles are assigned to paragraph boundaries.
DefaultStyledDocument allows you to place special characters within the document to help with formatting etc... Think Microsoft Word when you think about DefaultStyledDocument.
DocumentFilter
hen a Document containing a DocumentFilter is modified (either through insert or remove), it forwards the appropriate method invocation to the DocumentFilter.
This is an extremely useful class that "listens" for events to occur against your document (i.e. modification) and will perform an action when each event occurs.
PlainDocument
implements AbstractDocument and does not contain any kind of formatting special characters (Think notepad vs. Word). You should use this when you just want to store text (log file, etc.)
HTMLDocument
A document that models HTML. The purpose of this model is to support both browsing and editing.
HTMLDocument should be used when you are creating/modifying documents that contain HTML code and are intended to be viewed in a browser.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With