com.taursys.swing
Class MDocument

java.lang.Object
  extended byjavax.swing.text.AbstractDocument
      extended byjavax.swing.text.PlainDocument
          extended bycom.taursys.swing.MDocument
All Implemented Interfaces:
javax.swing.event.ChangeListener, javax.swing.text.Document, java.util.EventListener, java.io.Serializable

public class MDocument
extends javax.swing.text.PlainDocument
implements javax.swing.event.ChangeListener

MDocument is a Document Implementation which binds to a value in a TextModel

Version:
1.0
Author:
Marty Phelan
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class javax.swing.text.AbstractDocument
javax.swing.text.AbstractDocument.AbstractElement, javax.swing.text.AbstractDocument.AttributeContext, javax.swing.text.AbstractDocument.BranchElement, javax.swing.text.AbstractDocument.Content, javax.swing.text.AbstractDocument.DefaultDocumentEvent, javax.swing.text.AbstractDocument.ElementEdit, javax.swing.text.AbstractDocument.LeafElement
 
Field Summary
 
Fields inherited from class javax.swing.text.PlainDocument
lineLimitAttribute, tabSizeAttribute
 
Fields inherited from class javax.swing.text.AbstractDocument
BAD_LOCATION, BidiElementName, ContentElementName, ElementNameAttribute, listenerList, ParagraphElementName, SectionElementName
 
Fields inherited from interface javax.swing.text.Document
StreamDescriptionProperty, TitleProperty
 
Constructor Summary
MDocument()
          Constructs a new MDocument and its default TextModel (not Document).
MDocument(int javaDataType)
          Creates a new MDocument and a DefaultTextModel with a VariantValueHolder of the given type.
 
Method Summary
 void addEnableListener(EnableListener l)
          Adds the given listener to the list that is notified each time the enabled state changes.
protected  javax.swing.text.Element createBranchElement(javax.swing.text.Element parent, javax.swing.text.AttributeSet a)
          Creates a document branch element, that can contain other elements.
protected  TextModel createDefaultTextModel()
          Creates the default model used by this component
protected  javax.swing.text.Element createLeafElement(javax.swing.text.Element parent, javax.swing.text.AttributeSet a, int p0, int p1)
          Creates a document leaf element.
protected  void fireEnableChange(EnableEvent e)
          Notify the listeners that the enabled state has changed.
 java.text.Format getFormat()
          Returns the Format of the TextModel.
 java.lang.String getFormatPattern()
          Returns the Format pattern of the TextModel.
 TextModel getModel()
          Returns the model for this component
 java.lang.String getPropertyName()
          Returns the propertyName in the valueHolder where the model stores the value.
 ValueHolder getValueHolder()
          Returns the valueHolder for the model.
 void insertString(int offs, java.lang.String str, javax.swing.text.AttributeSet a)
          Inserts some content into the document.
protected  void insertUpdate(javax.swing.text.AbstractDocument.DefaultDocumentEvent chng, javax.swing.text.AttributeSet attr)
          Updates document structure as a result of text insertion.
 boolean isModified()
          Get the modified flag which indicates whether or not this document is modified.
 void remove(int offs, int len)
          Removes some content from the document.
 void removeEnableListener(EnableListener l)
          Removes the given listener from the list that is notified each time the enabled state changes.
protected  void removeUpdate(javax.swing.text.AbstractDocument.DefaultDocumentEvent chng)
          Updates any document structure as a result of text removal.
 void retrieveValue()
          Retrieves text from TextModel and stores in this Document
 void setFormat(java.text.Format format)
          Sets the Format of the TextModel.
 void setFormatPattern(java.lang.String newPattern)
          Sets the Format patten of the TextModel.
 void setModel(TextModel newModel)
          Sets the TextModel used by this Document.
 void setModified(boolean newModified)
          Set the modified flag which indicates whether or not this document is modified.
 void setPropertyName(java.lang.String newPropertyName)
          Sets the propertyName in the valueHolder where the model stores the value.
 void setValueHolder(ValueHolder newValueHolder)
          Sets the valueHolder for the model.
 void stateChanged(javax.swing.event.ChangeEvent e)
          Invoked whenever a change event occurs so the value will be retrieved from the TextModel.
 void storeValue()
          Stores Document value in TextModel
 
Methods inherited from class javax.swing.text.PlainDocument
createDefaultRoot, getDefaultRootElement, getParagraphElement
 
Methods inherited from class javax.swing.text.AbstractDocument
addDocumentListener, addUndoableEditListener, createPosition, dump, fireChangedUpdate, fireInsertUpdate, fireRemoveUpdate, fireUndoableEditUpdate, getAsynchronousLoadPriority, getAttributeContext, getBidiRootElement, getContent, getCurrentWriter, getDocumentFilter, getDocumentListeners, getDocumentProperties, getEndPosition, getLength, getListeners, getProperty, getRootElements, getStartPosition, getText, getText, getUndoableEditListeners, postRemoveUpdate, putProperty, readLock, readUnlock, removeDocumentListener, removeUndoableEditListener, render, replace, setAsynchronousLoadPriority, setDocumentFilter, setDocumentProperties, writeLock, writeUnlock
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MDocument

public MDocument()
Constructs a new MDocument and its default TextModel (not Document). The default model, a DefaultTextModel, is created via the createDefaultTextModel method. By default, the DefaultTextModel creates and uses a VariantValueHolder of type String.


MDocument

public MDocument(int javaDataType)
          throws UnsupportedDataTypeException
Creates a new MDocument and a DefaultTextModel with a VariantValueHolder of the given type. See com.taursys.util.DataTypes for defined data type constants TYPE_XXXXXX.

Throws:
UnsupportedDataTypeException - if invalid javaDataType is given
Method Detail

getModel

public TextModel getModel()
Returns the model for this component


setModel

public void setModel(TextModel newModel)
Sets the TextModel used by this Document. If the given TextModel does not have a defined format, the format and pattern are copied from the current TextModel. Removes this Document as a change listener from the current TextModel (if any) and adds this as a change listener to the given TextModel.


createDefaultTextModel

protected TextModel createDefaultTextModel()
Creates the default model used by this component


setFormat

public void setFormat(java.text.Format format)
Sets the Format of the TextModel.


getFormat

public java.text.Format getFormat()
Returns the Format of the TextModel.


setFormatPattern

public void setFormatPattern(java.lang.String newPattern)
Sets the Format patten of the TextModel.


getFormatPattern

public java.lang.String getFormatPattern()
Returns the Format pattern of the TextModel.


setValueHolder

public void setValueHolder(ValueHolder newValueHolder)
Sets the valueHolder for the model. The valueHolder is the object which holds the Object where the model stores the value. The default valueHolder is a VariantValueHolder with a javaDataType of String.


getValueHolder

public ValueHolder getValueHolder()
Returns the valueHolder for the model. The valueHolder is the object which holds the Object where the model stores the value. The default valueHolder is a VariantValueHolder with a javaDataType of String.


setPropertyName

public void setPropertyName(java.lang.String newPropertyName)
Sets the propertyName in the valueHolder where the model stores the value. This name is ignored if you are using the default model (A DefaultTextModel with a VariantValueHolder).


getPropertyName

public java.lang.String getPropertyName()
Returns the propertyName in the valueHolder where the model stores the value. This name is ignored if you are using the default model (A DefaultTextModel with a VariantValueHolder).


retrieveValue

public void retrieveValue()
Retrieves text from TextModel and stores in this Document


storeValue

public void storeValue()
                throws ModelException
Stores Document value in TextModel

Throws:
ModelException - if value cannot be stored because of invalid format or other reasons

stateChanged

public void stateChanged(javax.swing.event.ChangeEvent e)
Invoked whenever a change event occurs so the value will be retrieved from the TextModel. Only responds if the event is a ContentChangeEvent and ignoreChangeEvent is not true. The value retrieved from the text model will be stored in the document.

Specified by:
stateChanged in interface javax.swing.event.ChangeListener

setModified

public void setModified(boolean newModified)
Set the modified flag which indicates whether or not this document is modified.

Parameters:
newModified - the modified flag which indicates whether or not this document is modified.

isModified

public boolean isModified()
Get the modified flag which indicates whether or not this document is modified.

Returns:
the modified flag which indicates whether or not this document is modified.

insertUpdate

protected void insertUpdate(javax.swing.text.AbstractDocument.DefaultDocumentEvent chng,
                            javax.swing.text.AttributeSet attr)
Updates document structure as a result of text insertion. This will happen within a write lock. Since this document simply maps out lines, we refresh the line map. Also marks this document as modified.

Parameters:
chng - the change event describing the dit
attr - the set of attributes for the inserted text

removeUpdate

protected void removeUpdate(javax.swing.text.AbstractDocument.DefaultDocumentEvent chng)
Updates any document structure as a result of text removal. This will happen within a write lock. Since the structure represents a line map, this just checks to see if the removal spans lines. If it does, the two lines outside of the removal area are joined together. Also marks this document as modified.

Parameters:
chng - the change event describing the edit

createLeafElement

protected javax.swing.text.Element createLeafElement(javax.swing.text.Element parent,
                                                     javax.swing.text.AttributeSet a,
                                                     int p0,
                                                     int p1)
Creates a document leaf element. Hook through which elements are created to represent the document structure. Because this implementation keeps structure and content seperate, elements grow automatically when content is extended so splits of existing elements follow. The document itself gets to decide how to generate elements to give flexibility in the type of elements used. Also marks this document as modified.

Parameters:
parent - the parent element
a - the attributes for the element
p0 - the beginning of the range >= 0
p1 - the end of the range >= p0
Returns:
the new element

remove

public void remove(int offs,
                   int len)
            throws javax.swing.text.BadLocationException
Removes some content from the document. Removing content causes a write lock to be held while the actual changes are taking place. Observers are notified of the change on the thread that called this method. Also marks this document as modified.

This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.

Specified by:
remove in interface javax.swing.text.Document
Parameters:
offs - the starting offset >= 0
len - the number of characters to remove >= 0
Throws:
javax.swing.text.BadLocationException - the given remove position is not a valid position within the document
See Also:
Document.remove(int, int)

insertString

public void insertString(int offs,
                         java.lang.String str,
                         javax.swing.text.AttributeSet a)
                  throws javax.swing.text.BadLocationException
Inserts some content into the document. Inserting content causes a write lock to be held while the actual changes are taking place, followed by notification to the observers on the thread that grabbed the write lock. Also marks this document as modified.

This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.

Specified by:
insertString in interface javax.swing.text.Document
Parameters:
offs - the starting offset >= 0
str - the string to insert; does nothing with null/empty strings
a - the attributes for the inserted content
Throws:
javax.swing.text.BadLocationException - the given insert position is not a valid position within the document
See Also:
Document.insertString(int, java.lang.String, javax.swing.text.AttributeSet)

createBranchElement

protected javax.swing.text.Element createBranchElement(javax.swing.text.Element parent,
                                                       javax.swing.text.AttributeSet a)
Creates a document branch element, that can contain other elements. Also marks this document as modified.

Parameters:
parent - the parent element
a - the attributes
Returns:
the element

removeEnableListener

public void removeEnableListener(EnableListener l)
Removes the given listener from the list that is notified each time the enabled state changes. EnableEvents are generated whenever the contents of the ValueHolder change. They indicate whether or not a component can modify the current contents of the ValueHolder (not-null).

Parameters:
l - the EnableListener to remove from the notify list.

addEnableListener

public void addEnableListener(EnableListener l)
Adds the given listener to the list that is notified each time the enabled state changes. EnableEvents are generated whenever the contents of the ValueHolder change. They indicate whether or not a component can modify the current contents of the ValueHolder (not-null).

Parameters:
l - the EnableListener to add to the notify list.

fireEnableChange

protected void fireEnableChange(EnableEvent e)
Notify the listeners that the enabled state has changed. EnableEvents are generated whenever the contents of the ValueHolder change. They indicate whether or not a component can modify the current contents of the ValueHolder (not-null).

Parameters:
e - the EnableEvent to send to the listeners on the notify list.


Copyright © 2007 Martin T Phelan. All Rights Reserved.