CubeTwister 2.0alpha142 2012-02-11

ch.randelshofer.gui.text
Class StyledDocumentProxy

java.lang.Object
  extended by ch.randelshofer.gui.text.DocumentProxy
      extended by ch.randelshofer.gui.text.StyledDocumentProxy
All Implemented Interfaces:
java.lang.Cloneable, java.util.EventListener, javax.swing.event.DocumentListener, javax.swing.event.UndoableEditListener, javax.swing.text.Document, javax.swing.text.StyledDocument

public class StyledDocumentProxy
extends DocumentProxy
implements javax.swing.text.StyledDocument

This class is designed to be less memory expensive than a standard StyledDocument object. This class tries to represent the document in form of a String. If the document is in use, the String is converted into a StyledDocument. If the document is no longer in use, then the PlainDocument is converted into a String (losing all styles).

Version:
1-2 2007-01-14 Property ignoreAttributeEdits added
1.1 2004-04-04 Creates now a styled document with a proportional font instead of with a fixed width font.
1.0.1 2002-02-02 Clone method returned DocumentProxy instead of StyledDocumentProxy.
Author:
Werner Randelshofer, Hausmatt 10, CH-6405 Immensee, Switzerland

Field Summary
 
Fields inherited from interface javax.swing.text.Document
StreamDescriptionProperty, TitleProperty
 
Constructor Summary
StyledDocumentProxy()
          Creates new StyledDocumentProxy
 
Method Summary
 javax.swing.text.Style addStyle(java.lang.String nm, javax.swing.text.Style parent)
          Adds a new style into the logical style hierarchy.
protected  javax.swing.text.Document createDocumentModel()
           
 void fireUndoableEditHappened(javax.swing.undo.UndoableEdit edit)
          Notify all listeners that have registered interest for notification on this event type.
 java.awt.Color getBackground(javax.swing.text.AttributeSet attr)
          Takes a set of attributes and turn it into a background color specification.
 javax.swing.text.Element getCharacterElement(int pos)
          Gets the element that represents the character that is at the given offset within the document.
 java.awt.Font getFont(javax.swing.text.AttributeSet attr)
          Takes a set of attributes and turn it into a font specification.
 java.awt.Color getForeground(javax.swing.text.AttributeSet attr)
          Takes a set of attributes and turn it into a foreground color specification.
 javax.swing.text.Style getLogicalStyle(int p)
          Gets a logical style for a given position in a paragraph.
 javax.swing.text.Element getParagraphElement(int pos)
          Gets the element that represents the paragraph that encloses the given offset within the document.
 javax.swing.text.Style getStyle(java.lang.String nm)
          Fetches a named style previously added.
 void removeStyle(java.lang.String nm)
          Removes a named style previously added to the document.
 void setCharacterAttributes(int offset, int length, javax.swing.text.AttributeSet s, boolean replace)
          Changes the content element attributes used for the given range of existing content in the document.
 void setIgnoreAttributeEdits(boolean newValue)
          Sets whether AttributeEdits should be ignored.
 void setLogicalStyle(int pos, javax.swing.text.Style s)
          Sets the logical style to use for the paragraph at the given position.
 void setParagraphAttributes(int offset, int length, javax.swing.text.AttributeSet s, boolean replace)
          Sets paragraph attributes.
 
Methods inherited from class ch.randelshofer.gui.text.DocumentProxy
addChangeListener, addDocumentListener, addUndoableEditListener, changedUpdate, clone, createPosition, fireChangedUpdate, fireInsertUpdate, fireRemoveUpdate, fireStateChanged, flushDocumentModel, getDefaultRootElement, getDocumentModel, getEndPosition, getLength, getProperty, getRootElements, getStartPosition, getText, getText, getText, hasDocumentListeners, insertString, insertUpdate, putProperty, remove, removeChangeListener, removeDocumentListener, removeUndoableEditListener, removeUpdate, render, setText, undoableEditHappened
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.swing.text.Document
addDocumentListener, addUndoableEditListener, createPosition, getDefaultRootElement, getEndPosition, getLength, getProperty, getRootElements, getStartPosition, getText, getText, insertString, putProperty, remove, removeDocumentListener, removeUndoableEditListener, render
 

Constructor Detail

StyledDocumentProxy

public StyledDocumentProxy()
Creates new StyledDocumentProxy

Method Detail

setIgnoreAttributeEdits

public void setIgnoreAttributeEdits(boolean newValue)
Sets whether AttributeEdits should be ignored. If this is set to true, the DocumentProxy will not fire edit events that result from attribute changes.


addStyle

public javax.swing.text.Style addStyle(java.lang.String nm,
                                       javax.swing.text.Style parent)
Adds a new style into the logical style hierarchy. Style attributes resolve from bottom up so an attribute specified in a child will override an attribute specified in the parent.

Specified by:
addStyle in interface javax.swing.text.StyledDocument
Parameters:
nm - the name of the style (must be unique within the collection of named styles). The name may be null if the style is unnamed, but the caller is responsible for managing the reference returned as an unnamed style can't be fetched by name. An unnamed style may be useful for things like character attribute overrides such as found in a style run.
parent - the parent style. This may be null if unspecified attributes need not be resolved in some other style.
Returns:
the style

getBackground

public java.awt.Color getBackground(javax.swing.text.AttributeSet attr)
Takes a set of attributes and turn it into a background color specification. This might be used to specify things like brighter, more hue, etc.

Specified by:
getBackground in interface javax.swing.text.StyledDocument
Parameters:
attr - the set of attributes
Returns:
the color

getCharacterElement

public javax.swing.text.Element getCharacterElement(int pos)
Gets the element that represents the character that is at the given offset within the document.

Specified by:
getCharacterElement in interface javax.swing.text.StyledDocument
Parameters:
pos - the offset >= 0
Returns:
the element

getFont

public java.awt.Font getFont(javax.swing.text.AttributeSet attr)
Takes a set of attributes and turn it into a font specification. This can be used to turn things like family, style, size, etc into a font that is available on the system the document is currently being used on.

Specified by:
getFont in interface javax.swing.text.StyledDocument
Parameters:
attr - the set of attributes
Returns:
the font

getForeground

public java.awt.Color getForeground(javax.swing.text.AttributeSet attr)
Takes a set of attributes and turn it into a foreground color specification. This might be used to specify things like brighter, more hue, etc.

Specified by:
getForeground in interface javax.swing.text.StyledDocument
Parameters:
attr - the set of attributes
Returns:
the color

getLogicalStyle

public javax.swing.text.Style getLogicalStyle(int p)
Gets a logical style for a given position in a paragraph.

Specified by:
getLogicalStyle in interface javax.swing.text.StyledDocument
Parameters:
p - the position >= 0
Returns:
the style

getParagraphElement

public javax.swing.text.Element getParagraphElement(int pos)
Gets the element that represents the paragraph that encloses the given offset within the document.

Specified by:
getParagraphElement in interface javax.swing.text.StyledDocument
Parameters:
pos - the offset >= 0
Returns:
the element

getStyle

public javax.swing.text.Style getStyle(java.lang.String nm)
Fetches a named style previously added.

Specified by:
getStyle in interface javax.swing.text.StyledDocument
Parameters:
nm - the name of the style
Returns:
the style

removeStyle

public void removeStyle(java.lang.String nm)
Removes a named style previously added to the document.

Specified by:
removeStyle in interface javax.swing.text.StyledDocument
Parameters:
nm - the name of the style to remove

setCharacterAttributes

public void setCharacterAttributes(int offset,
                                   int length,
                                   javax.swing.text.AttributeSet s,
                                   boolean replace)
Changes the content element attributes used for the given range of existing content in the document. All of the attributes defined in the given Attributes argument are applied to the given range. This method can be used to completely remove all content level attributes for the given range by giving an Attributes argument that has no attributes defined and setting replace to true.

Specified by:
setCharacterAttributes in interface javax.swing.text.StyledDocument
Parameters:
offset - the start of the change >= 0
length - the length of the change >= 0
s - the non-null attributes to change to. Any attributes defined will be applied to the text for the given range.
replace - indicates whether or not the previous attributes should be cleared before the new attributes as set. If true, the operation will replace the previous attributes entirely. If false, the new attributes will be merged with the previous attributes.

setLogicalStyle

public void setLogicalStyle(int pos,
                            javax.swing.text.Style s)
Sets the logical style to use for the paragraph at the given position. If attributes aren't explicitly set for character and paragraph attributes they will resolve through the logical style assigned to the paragraph, which in turn may resolve through some hierarchy completely independent of the element hierarchy in the document.

Specified by:
setLogicalStyle in interface javax.swing.text.StyledDocument
Parameters:
pos - the starting position >= 0
s - the style to set

setParagraphAttributes

public void setParagraphAttributes(int offset,
                                   int length,
                                   javax.swing.text.AttributeSet s,
                                   boolean replace)
Sets paragraph attributes.

Specified by:
setParagraphAttributes in interface javax.swing.text.StyledDocument
Parameters:
offset - the start of the change >= 0
length - the length of the change >= 0
s - the non-null attributes to change to. Any attributes defined will be applied to the text for the given range.
replace - indicates whether or not the previous attributes should be cleared before the new attributes are set. If true, the operation will replace the previous attributes entirely. If false, the new attributes will be merged with the previous attributes.

createDocumentModel

protected javax.swing.text.Document createDocumentModel()
Overrides:
createDocumentModel in class DocumentProxy

fireUndoableEditHappened

public void fireUndoableEditHappened(javax.swing.undo.UndoableEdit edit)
Notify all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.

Overrides:
fireUndoableEditHappened in class DocumentProxy

(c) Werner Randelshofer.
All rights reserved.