* with other documents. */ private AttributeContext context; /** * The root of the bidirectional structure for this document. Its children * represent character runs with the same Unicode bidi level. */ private transient BranchElement bidiRoot; /** * Filter for inserting/removing of text. */ private DocumentFilter documentFilter; /** * Used by DocumentFilter to do actual insert/remove. */ private transient DocumentFilter.FilterBypass filterBypass; private static final String BAD_LOCK_STATE = "document lock failure"; /** * Error message to indicate a bad location. */ protected static final String BAD_LOCATION = "document location failure"; /** * Name of elements used to represent paragraphs */ public static final String ParagraphElementName = "paragraph"; /** * Name of elements used to represent content */ public static final String ContentElementName = "content"; /** * Name of elements used to hold sections (lines/paragraphs). */ public static final String SectionElementName = "section"; /** * Name of elements used to hold a unidirectional run */ public static final String BidiElementName = "bidi level"; /** * Name of the attribute used to specify element * names. */ public static final String ElementNameAttribute = "$ename"; /** * Document property that indicates whether internationalization * functions such as text reordering or reshaping should be * performed. This property should not be publicly exposed, * since it is used for implementation convenience only. As a * side effect, copies of this property may be in its subclasses * that live in different packages (e.g. HTMLDocument as of now), * so those copies should also be taken care of when this property * needs to be modified. */ static final String I18NProperty = "i18n"; /** * Document property that indicates if a character has been inserted * into the document that is more than one byte long. GlyphView uses * this to determine if it should use BreakIterator. */ static final Object MultiByteProperty = "multiByte"; /** * Document property that indicates asynchronous loading is * desired, with the thread priority given as the value. */ static final String AsyncLoadPriority = "load priority"; /** * Interface to describe a sequence of character content that * can be edited. Implementations may or may not support a * history mechanism which will be reflected by whether or not * mutations return an UndoableEdit implementation. * @see AbstractDocument */ public interface Content { /** * Creates a position within the content that will * track change as the content is mutated. * * @param offset the offset in the content >= 0 * @return a Position * @exception BadLocationException for an invalid offset */ public Position createPosition(int offset) throws BadLocationException; /** * Current length of the sequence of character content. * * @return the length >= 0 */ public int length(); /** * Inserts a string of characters into the sequence. * * @param where offset into the sequence to make the insertion >= 0 * @param str string to insert * @return if the implementation supports a history mechanism, * a reference to an <code>Edit</code> implementation will be returned, * otherwise returns <code>null</code> * @exception BadLocationException thrown if the area covered by * the arguments is not contained in the character sequence */ public UndoableEdit insertString(int where, String str) throws BadLocationException; /** * Removes some portion of the sequence. * * @param where The offset into the sequence to make the * insertion >= 0. * @param nitems The number of items in the sequence to remove >= 0. * @return If the implementation supports a history mechansim, * a reference to an Edit implementation will be returned, * otherwise null. * @exception BadLocationException Thrown if the area covered by * the arguments is not contained in the character sequence. */ public UndoableEdit remove(int where, int nitems) throws BadLocationException; /** * Fetches a string of characters contained in the sequence. * * @param where Offset into the sequence to fetch >= 0. * @param len number of characters to copy >= 0. * @return the string * @exception BadLocationException Thrown if the area covered by * the arguments is not contained in the character sequence. */ public String getString(int where, int len) throws BadLocationException; /** * Gets a sequence of characters and copies them into a Segment. * * @param where the starting offset >= 0 * @param len the number of characters >= 0 * @param txt the target location to copy into * @exception BadLocationException Thrown if the area covered by * the arguments is not contained in the character sequence. */ public void getChars(int where, int len, Segment txt) throws BadLocationException; } /** * An interface that can be used to allow MutableAttributeSet * implementations to use pluggable attribute compression * techniques. Each mutation of the attribute set can be * used to exchange a previous AttributeSet instance with * another, preserving the possibility of the AttributeSet * remaining immutable. An implementation is provided by * the StyleContext class. * * The Element implementations provided by this class use * this interface to provide their MutableAttributeSet * implementations, so that different AttributeSet compression * techniques can be employed. The method * <code>getAttributeContext</code> should be implemented to * return the object responsible for implementing the desired * compression technique. * * @see StyleContext */ public interface AttributeContext { /** * Adds an attribute to the given set, and returns * the new representative set. * * @param old the old attribute set * @param name the non-null attribute name * @param value the attribute value * @return the updated attribute set * @see MutableAttributeSet#addAttribute */ public AttributeSet addAttribute(AttributeSet old, Object name, Object value); /** * Adds a set of attributes to the element. * * @param old the old attribute set * @param attr the attributes to add * @return the updated attribute set * @see MutableAttributeSet#addAttribute */ public AttributeSet addAttributes(AttributeSet old, AttributeSet attr); /** * Removes an attribute from the set. * * @param old the old attribute set * @param name the non-null attribute name * @return the updated attribute set * @see MutableAttributeSet#removeAttribute */ public AttributeSet removeAttribute(AttributeSet old, Object name); /** * Removes a set of attributes for the element. * * @param old the old attribute set * @param names the attribute names * @return the updated attribute set * @see MutableAttributeSet#removeAttributes */ public AttributeSet removeAttributes(AttributeSet old, Enumeration<?> names); /** * Removes a set of attributes for the element. * * @param old the old attribute set * @param attrs the attributes * @return the updated attribute set * @see MutableAttributeSet#removeAttributes */ public AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs); /** * Fetches an empty AttributeSet. * * @return the attribute set */ public AttributeSet getEmptySet(); /** * Reclaims an attribute set. * This is a way for a MutableAttributeSet to mark that it no * longer need a particular immutable set. This is only necessary * in 1.1 where there are no weak references. A 1.1 implementation * would call this in its finalize method. * * @param a the attribute set to reclaim */ public void reclaim(AttributeSet a); } /** * Implements the abstract part of an element. By default elements * support attributes by having a field that represents the immutable * part of the current attribute set for the element. The element itself * implements MutableAttributeSet which can be used to modify the set * by fetching a new immutable set. The immutable sets are provided * by the AttributeContext associated with the document. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing. As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. */ public abstract class AbstractElement implements Element, MutableAttributeSet, Serializable, TreeNode { /** * Creates a new AbstractElement. * * @param parent the parent element * @param a the attributes for the element * @since 1.4 */ public AbstractElement(Element parent, AttributeSet a) { this.parent = parent; attributes = getAttributeContext().getEmptySet(); if (a != null) { addAttributes(a); } } private final void indent(PrintWriter out, int n) { for (int i = 0; i < n; i++) { out.print(" "); } } /** * Dumps a debugging representation of the element hierarchy. * * @param psOut the output stream * @param indentAmount the indentation level >= 0 */ public void dump(PrintStream psOut, int indentAmount) { PrintWriter out; try { out = new PrintWriter(new OutputStreamWriter(psOut,"JavaEsc"), true); } catch (UnsupportedEncodingException e){ out = new PrintWriter(psOut,true); } indent(out, indentAmount); if (getName() == null) { out.print("<??"); } else { out.print("<" + getName()); } if (getAttributeCount() > 0) { out.println(""); // dump the attributes Enumeration names = attributes.getAttributeNames(); while (names.hasMoreElements()) { Object name = names.nextElement(); indent(out, indentAmount + 1); out.println(name + "=" + getAttribute(name)); } indent(out, indentAmount); } out.println(">"); if (isLeaf()) { indent(out, indentAmount+1); out.print("[" + getStartOffset() + "," + getEndOffset() + "]"); Content c = getContent(); try { String contentStr = c.getString(getStartOffset(), getEndOffset() - getStartOffset())/*.trim()*/; if (contentStr.length() > 40) { contentStr = contentStr.substring(0, 40) + "..."; } out.println("["+contentStr+"]"); } catch (BadLocationException e) { } } else { int n = getElementCount(); for (int i = 0; i < n; i++) { AbstractElement e = (AbstractElement) getElement(i); e.dump(psOut, indentAmount+1); } } } // --- AttributeSet ---------------------------- // delegated to the immutable field "attributes" /** * Gets the number of attributes that are defined. * * @return the number of attributes >= 0 * @see AttributeSet#getAttributeCount */ public int getAttributeCount() { return attributes.getAttributeCount(); } /** * Checks whether a given attribute is defined. * * @param attrName the non-null attribute name * @return true if the attribute is defined * @see AttributeSet#isDefined */ public boolean isDefined(Object attrName) { return attributes.isDefined(attrName); } /** * Checks whether two attribute sets are equal. * * @param attr the attribute set to check against * @return true if the same * @see AttributeSet#isEqual */ public boolean isEqual(AttributeSet attr) { return attributes.isEqual(attr); } /** * Copies a set of attributes. * * @return the copy * @see AttributeSet#copyAttributes */ public AttributeSet copyAttributes() { return attributes.copyAttributes(); } /** * Gets the value of an attribute. * * @param attrName the non-null attribute name * @return the attribute value * @see AttributeSet#getAttribute */ public Object getAttribute(Object attrName) { Object value = attributes.getAttribute(attrName); if (value == null) { // The delegate nor it's resolvers had a match, // so we'll try to resolve through the parent // element. AttributeSet a = (parent != null) ? parent.getAttributes() : null; if (a != null) { value = a.getAttribute(attrName); } } return value; } /** * Gets the names of all attributes. * * @return the attribute names as an enumeration * @see AttributeSet#getAttributeNames */ public Enumeration<?> getAttributeNames() { return attributes.getAttributeNames(); } /** * Checks whether a given attribute name/value is defined. * * @param name the non-null attribute name * @param value the attribute value * @return true if the name/value is defined * @see AttributeSet#containsAttribute */ public boolean containsAttribute(Object name, Object value) { return attributes.containsAttribute(name, value); } /** * Checks whether the element contains all the attributes. * * @param attrs the attributes to check * @return true if the element contains all the attributes * @see AttributeSet#containsAttributes */ public boolean containsAttributes(AttributeSet attrs) { return attributes.containsAttributes(attrs); } /** * Gets the resolving parent. * If not overridden, the resolving parent defaults to * the parent element. * * @return the attributes from the parent, <code>null</code> if none * @see AttributeSet#getResolveParent */ public AttributeSet getResolveParent() { AttributeSet a = attributes.getResolveParent(); if ((a == null) && (parent != null)) { a = parent.getAttributes(); } return a; } // --- MutableAttributeSet ---------------------------------- // should fetch a new immutable record for the field // "attributes". /** * Adds an attribute to the element. * * @param name the non-null attribute name * @param value the attribute value * @see MutableAttributeSet#addAttribute */ public void addAttribute(Object name, Object value) { checkForIllegalCast(); AttributeContext context = getAttributeContext(); attributes = context.addAttribute(attributes, name, value); } /** * Adds a set of attributes to the element. * * @param attr the attributes to add * @see MutableAttributeSet#addAttribute */ public void addAttributes(AttributeSet attr) { checkForIllegalCast(); AttributeContext context = getAttributeContext(); attributes = context.addAttributes(attributes, attr); } /** * Removes an attribute from the set. * * @param name the non-null attribute name * @see MutableAttributeSet#removeAttribute */ public void removeAttribute(Object name) { checkForIllegalCast(); AttributeContext context = getAttributeContext(); attributes = context.removeAttribute(attributes, name); } /**Lines 1501–2000 of 3118. Previous lines Load more
* with other documents. */ private AttributeContext context; /** * The root of the bidirectional structure for this document. Its children * represent character runs with the same Unicode bidi level. */ private transient BranchElement bidiRoot; /** * Filter for inserting/removing of text. */ private DocumentFilter documentFilter; /** * Used by DocumentFilter to do actual insert/remove. */ private transient DocumentFilter.FilterBypass filterBypass; private static final String BAD_LOCK_STATE = "document lock failure"; /** * Error message to indicate a bad location. */ protected static final String BAD_LOCATION = "document location failure"; /** * Name of elements used to represent paragraphs */ public static final String ParagraphElementName = "paragraph"; /** * Name of elements used to represent content */ public static final String ContentElementName = "content"; /** * Name of elements used to hold sections (lines/paragraphs). */ public static final String SectionElementName = "section"; /** * Name of elements used to hold a unidirectional run */ public static final String BidiElementName = "bidi level"; /** * Name of the attribute used to specify element * names. */ public static final String ElementNameAttribute = "$ename"; /** * Document property that indicates whether internationalization * functions such as text reordering or reshaping should be * performed. This property should not be publicly exposed, * since it is used for implementation convenience only. As a * side effect, copies of this property may be in its subclasses * that live in different packages (e.g. HTMLDocument as of now), * so those copies should also be taken care of when this property * needs to be modified. */ static final String I18NProperty = "i18n"; /** * Document property that indicates if a character has been inserted * into the document that is more than one byte long. GlyphView uses * this to determine if it should use BreakIterator. */ static final Object MultiByteProperty = "multiByte"; /** * Document property that indicates asynchronous loading is * desired, with the thread priority given as the value. */ static final String AsyncLoadPriority = "load priority"; /** * Interface to describe a sequence of character content that * can be edited. Implementations may or may not support a * history mechanism which will be reflected by whether or not * mutations return an UndoableEdit implementation. * @see AbstractDocument */ public interface Content { /** * Creates a position within the content that will * track change as the content is mutated. * * @param offset the offset in the content >= 0 * @return a Position * @exception BadLocationException for an invalid offset */ public Position createPosition(int offset) throws BadLocationException; /** * Current length of the sequence of character content. * * @return the length >= 0 */ public int length(); /** * Inserts a string of characters into the sequence. * * @param where offset into the sequence to make the insertion >= 0 * @param str string to insert * @return if the implementation supports a history mechanism, * a reference to an <code>Edit</code> implementation will be returned, * otherwise returns <code>null</code> * @exception BadLocationException thrown if the area covered by * the arguments is not contained in the character sequence */ public UndoableEdit insertString(int where, String str) throws BadLocationException; /** * Removes some portion of the sequence. * * @param where The offset into the sequence to make the * insertion >= 0. * @param nitems The number of items in the sequence to remove >= 0. * @return If the implementation supports a history mechansim, * a reference to an Edit implementation will be returned, * otherwise null. * @exception BadLocationException Thrown if the area covered by * the arguments is not contained in the character sequence. */ public UndoableEdit remove(int where, int nitems) throws BadLocationException; /** * Fetches a string of characters contained in the sequence. * * @param where Offset into the sequence to fetch >= 0. * @param len number of characters to copy >= 0. * @return the string * @exception BadLocationException Thrown if the area covered by * the arguments is not contained in the character sequence. */ public String getString(int where, int len) throws BadLocationException; /** * Gets a sequence of characters and copies them into a Segment. * * @param where the starting offset >= 0 * @param len the number of characters >= 0 * @param txt the target location to copy into * @exception BadLocationException Thrown if the area covered by * the arguments is not contained in the character sequence. */ public void getChars(int where, int len, Segment txt) throws BadLocationException; } /** * An interface that can be used to allow MutableAttributeSet * implementations to use pluggable attribute compression * techniques. Each mutation of the attribute set can be * used to exchange a previous AttributeSet instance with * another, preserving the possibility of the AttributeSet * remaining immutable. An implementation is provided by * the StyleContext class. * * The Element implementations provided by this class use * this interface to provide their MutableAttributeSet * implementations, so that different AttributeSet compression * techniques can be employed. The method * <code>getAttributeContext</code> should be implemented to * return the object responsible for implementing the desired * compression technique. * * @see StyleContext */ public interface AttributeContext { /** * Adds an attribute to the given set, and returns * the new representative set. * * @param old the old attribute set * @param name the non-null attribute name * @param value the attribute value * @return the updated attribute set * @see MutableAttributeSet#addAttribute */ public AttributeSet addAttribute(AttributeSet old, Object name, Object value); /** * Adds a set of attributes to the element. * * @param old the old attribute set * @param attr the attributes to add * @return the updated attribute set * @see MutableAttributeSet#addAttribute */ public AttributeSet addAttributes(AttributeSet old, AttributeSet attr); /** * Removes an attribute from the set. * * @param old the old attribute set * @param name the non-null attribute name * @return the updated attribute set * @see MutableAttributeSet#removeAttribute */ public AttributeSet removeAttribute(AttributeSet old, Object name); /** * Removes a set of attributes for the element. * * @param old the old attribute set * @param names the attribute names * @return the updated attribute set * @see MutableAttributeSet#removeAttributes */ public AttributeSet removeAttributes(AttributeSet old, Enumeration<?> names); /** * Removes a set of attributes for the element. * * @param old the old attribute set * @param attrs the attributes * @return the updated attribute set * @see MutableAttributeSet#removeAttributes */ public AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs); /** * Fetches an empty AttributeSet. * * @return the attribute set */ public AttributeSet getEmptySet(); /** * Reclaims an attribute set. * This is a way for a MutableAttributeSet to mark that it no * longer need a particular immutable set. This is only necessary * in 1.1 where there are no weak references. A 1.1 implementation * would call this in its finalize method. * * @param a the attribute set to reclaim */ public void reclaim(AttributeSet a); } /** * Implements the abstract part of an element. By default elements * support attributes by having a field that represents the immutable * part of the current attribute set for the element. The element itself * implements MutableAttributeSet which can be used to modify the set * by fetching a new immutable set. The immutable sets are provided * by the AttributeContext associated with the document. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing. As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. */ public abstract class AbstractElement implements Element, MutableAttributeSet, Serializable, TreeNode { /** * Creates a new AbstractElement. * * @param parent the parent element * @param a the attributes for the element * @since 1.4 */ public AbstractElement(Element parent, AttributeSet a) { this.parent = parent; attributes = getAttributeContext().getEmptySet(); if (a != null) { addAttributes(a); } } private final void indent(PrintWriter out, int n) { for (int i = 0; i < n; i++) { out.print(" "); } } /** * Dumps a debugging representation of the element hierarchy. * * @param psOut the output stream * @param indentAmount the indentation level >= 0 */ public void dump(PrintStream psOut, int indentAmount) { PrintWriter out; try { out = new PrintWriter(new OutputStreamWriter(psOut,"JavaEsc"), true); } catch (UnsupportedEncodingException e){ out = new PrintWriter(psOut,true); } indent(out, indentAmount); if (getName() == null) { out.print("<??"); } else { out.print("<" + getName()); } if (getAttributeCount() > 0) { out.println(""); // dump the attributes Enumeration names = attributes.getAttributeNames(); while (names.hasMoreElements()) { Object name = names.nextElement(); indent(out, indentAmount + 1); out.println(name + "=" + getAttribute(name)); } indent(out, indentAmount); } out.println(">"); if (isLeaf()) { indent(out, indentAmount+1); out.print("[" + getStartOffset() + "," + getEndOffset() + "]"); Content c = getContent(); try { String contentStr = c.getString(getStartOffset(), getEndOffset() - getStartOffset())/*.trim()*/; if (contentStr.length() > 40) { contentStr = contentStr.substring(0, 40) + "..."; } out.println("["+contentStr+"]"); } catch (BadLocationException e) { } } else { int n = getElementCount(); for (int i = 0; i < n; i++) { AbstractElement e = (AbstractElement) getElement(i); e.dump(psOut, indentAmount+1); } } } // --- AttributeSet ---------------------------- // delegated to the immutable field "attributes" /** * Gets the number of attributes that are defined. * * @return the number of attributes >= 0 * @see AttributeSet#getAttributeCount */ public int getAttributeCount() { return attributes.getAttributeCount(); } /** * Checks whether a given attribute is defined. * * @param attrName the non-null attribute name * @return true if the attribute is defined * @see AttributeSet#isDefined */ public boolean isDefined(Object attrName) { return attributes.isDefined(attrName); } /** * Checks whether two attribute sets are equal. * * @param attr the attribute set to check against * @return true if the same * @see AttributeSet#isEqual */ public boolean isEqual(AttributeSet attr) { return attributes.isEqual(attr); } /** * Copies a set of attributes. * * @return the copy * @see AttributeSet#copyAttributes */ public AttributeSet copyAttributes() { return attributes.copyAttributes(); } /** * Gets the value of an attribute. * * @param attrName the non-null attribute name * @return the attribute value * @see AttributeSet#getAttribute */ public Object getAttribute(Object attrName) { Object value = attributes.getAttribute(attrName); if (value == null) { // The delegate nor it's resolvers had a match, // so we'll try to resolve through the parent // element. AttributeSet a = (parent != null) ? parent.getAttributes() : null; if (a != null) { value = a.getAttribute(attrName); } } return value; } /** * Gets the names of all attributes. * * @return the attribute names as an enumeration * @see AttributeSet#getAttributeNames */ public Enumeration<?> getAttributeNames() { return attributes.getAttributeNames(); } /** * Checks whether a given attribute name/value is defined. * * @param name the non-null attribute name * @param value the attribute value * @return true if the name/value is defined * @see AttributeSet#containsAttribute */ public boolean containsAttribute(Object name, Object value) { return attributes.containsAttribute(name, value); } /** * Checks whether the element contains all the attributes. * * @param attrs the attributes to check * @return true if the element contains all the attributes * @see AttributeSet#containsAttributes */ public boolean containsAttributes(AttributeSet attrs) { return attributes.containsAttributes(attrs); } /** * Gets the resolving parent. * If not overridden, the resolving parent defaults to * the parent element. * * @return the attributes from the parent, <code>null</code> if none * @see AttributeSet#getResolveParent */ public AttributeSet getResolveParent() { AttributeSet a = attributes.getResolveParent(); if ((a == null) && (parent != null)) { a = parent.getAttributes(); } return a; } // --- MutableAttributeSet ---------------------------------- // should fetch a new immutable record for the field // "attributes". /** * Adds an attribute to the element. * * @param name the non-null attribute name * @param value the attribute value * @see MutableAttributeSet#addAttribute */ public void addAttribute(Object name, Object value) { checkForIllegalCast(); AttributeContext context = getAttributeContext(); attributes = context.addAttribute(attributes, name, value); } /** * Adds a set of attributes to the element. * * @param attr the attributes to add * @see MutableAttributeSet#addAttribute */ public void addAttributes(AttributeSet attr) { checkForIllegalCast(); AttributeContext context = getAttributeContext(); attributes = context.addAttributes(attributes, attr); } /** * Removes an attribute from the set. * * @param name the non-null attribute name * @see MutableAttributeSet#removeAttribute */ public void removeAttribute(Object name) { checkForIllegalCast(); AttributeContext context = getAttributeContext(); attributes = context.removeAttribute(attributes, name); } /**