* Removes a set of attributes for the element. * * @param names the attribute names * @see MutableAttributeSet#removeAttributes */ public void removeAttributes(Enumeration<?> names) { checkForIllegalCast(); AttributeContext context = getAttributeContext(); attributes = context.removeAttributes(attributes, names); } /** * Removes a set of attributes for the element. * * @param attrs the attributes * @see MutableAttributeSet#removeAttributes */ public void removeAttributes(AttributeSet attrs) { checkForIllegalCast(); AttributeContext context = getAttributeContext(); if (attrs == this) { attributes = context.getEmptySet(); } else { attributes = context.removeAttributes(attributes, attrs); } } /** * Sets the resolving parent. * * @param parent the parent, null if none * @see MutableAttributeSet#setResolveParent */ public void setResolveParent(AttributeSet parent) { checkForIllegalCast(); AttributeContext context = getAttributeContext(); if (parent != null) { attributes = context.addAttribute(attributes, StyleConstants.ResolveAttribute, parent); } else { attributes = context.removeAttribute(attributes, StyleConstants.ResolveAttribute); } } private final void checkForIllegalCast() { Thread t = getCurrentWriter(); if ((t == null) || (t != Thread.currentThread())) { throw new StateInvariantError("Illegal cast to MutableAttributeSet"); } } // --- Element methods ------------------------------------- /** * Retrieves the underlying model. * * @return the model */ public Document getDocument() { return AbstractDocument.this; } /** * Gets the parent of the element. * * @return the parent */ public Element getParentElement() { return parent; } /** * Gets the attributes for the element. * * @return the attribute set */ public AttributeSet getAttributes() { return this; } /** * Gets the name of the element. * * @return the name, null if none */ public String getName() { if (attributes.isDefined(ElementNameAttribute)) { return (String) attributes.getAttribute(ElementNameAttribute); } return null; } /** * Gets the starting offset in the model for the element. * * @return the offset >= 0 */ public abstract int getStartOffset(); /** * Gets the ending offset in the model for the element. * * @return the offset >= 0 */ public abstract int getEndOffset(); /** * Gets a child element. * * @param index the child index, >= 0 && < getElementCount() * @return the child element */ public abstract Element getElement(int index); /** * Gets the number of children for the element. * * @return the number of children >= 0 */ public abstract int getElementCount(); /** * Gets the child element index closest to the given model offset. * * @param offset the offset >= 0 * @return the element index >= 0 */ public abstract int getElementIndex(int offset); /** * Checks whether the element is a leaf. * * @return true if a leaf */ public abstract boolean isLeaf(); // --- TreeNode methods ------------------------------------- /** * Returns the child <code>TreeNode</code> at index * <code>childIndex</code>. */ public TreeNode getChildAt(int childIndex) { return (TreeNode)getElement(childIndex); } /** * Returns the number of children <code>TreeNode</code>'s * receiver contains. * @return the number of children <code>TreeNodews</code>'s * receiver contains */ public int getChildCount() { return getElementCount(); } /** * Returns the parent <code>TreeNode</code> of the receiver. * @return the parent <code>TreeNode</code> of the receiver */ public TreeNode getParent() { return (TreeNode)getParentElement(); } /** * Returns the index of <code>node</code> in the receivers children. * If the receiver does not contain <code>node</code>, -1 will be * returned. * @param node the location of interest * @return the index of <code>node</code> in the receiver's * children, or -1 if absent */ public int getIndex(TreeNode node) { for(int counter = getChildCount() - 1; counter >= 0; counter--) if(getChildAt(counter) == node) return counter; return -1; } /** * Returns true if the receiver allows children. * @return true if the receiver allows children, otherwise false */ public abstract boolean getAllowsChildren(); /** * Returns the children of the receiver as an * <code>Enumeration</code>. * @return the children of the receiver as an <code>Enumeration</code> */ public abstract Enumeration children(); // --- serialization --------------------------------------------- private void writeObject(ObjectOutputStream s) throws IOException { s.defaultWriteObject(); StyleContext.writeAttributeSet(s, attributes); } private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException { s.defaultReadObject(); MutableAttributeSet attr = new SimpleAttributeSet(); StyleContext.readAttributeSet(s, attr); AttributeContext context = getAttributeContext(); attributes = context.addAttributes(SimpleAttributeSet.EMPTY, attr); } // ---- variables ----------------------------------------------------- private Element parent; private transient AttributeSet attributes; } /** * Implements a composite element that contains other elements. * <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 class BranchElement extends AbstractElement { /** * Constructs a composite element that initially contains * no children. * * @param parent The parent element * @param a the attributes for the element * @since 1.4 */ public BranchElement(Element parent, AttributeSet a) { super(parent, a); children = new AbstractElement[1]; nchildren = 0; lastIndex = -1; } /** * Gets the child element that contains * the given model position. * * @param pos the position >= 0 * @return the element, null if none */ public Element positionToElement(int pos) { int index = getElementIndex(pos); Element child = children[index]; int p0 = child.getStartOffset(); int p1 = child.getEndOffset(); if ((pos >= p0) && (pos < p1)) { return child; } return null; } /** * Replaces content with a new set of elements. * * @param offset the starting offset >= 0 * @param length the length to replace >= 0 * @param elems the new elements */ public void replace(int offset, int length, Element[] elems) { int delta = elems.length - length; int src = offset + length; int nmove = nchildren - src; int dest = src + delta; if ((nchildren + delta) >= children.length) { // need to grow the array int newLength = Math.max(2*children.length, nchildren + delta); AbstractElement[] newChildren = new AbstractElement[newLength]; System.arraycopy(children, 0, newChildren, 0, offset); System.arraycopy(elems, 0, newChildren, offset, elems.length); System.arraycopy(children, src, newChildren, dest, nmove); children = newChildren; } else { // patch the existing array System.arraycopy(children, src, children, dest, nmove); System.arraycopy(elems, 0, children, offset, elems.length); } nchildren = nchildren + delta; } /** * Converts the element to a string. * * @return the string */ public String toString() { return "BranchElement(" + getName() + ") " + getStartOffset() + "," + getEndOffset() + "\n"; } // --- Element methods ----------------------------------- /** * Gets the element name. * * @return the element name */ public String getName() { String nm = super.getName(); if (nm == null) { nm = ParagraphElementName; } return nm; } /** * Gets the starting offset in the model for the element. * * @return the offset >= 0 */ public int getStartOffset() { return children[0].getStartOffset(); } /** * Gets the ending offset in the model for the element. * @throws NullPointerException if this element has no children * * @return the offset >= 0 */ public int getEndOffset() { Element child = (nchildren > 0) ? children[nchildren - 1] : children[0]; return child.getEndOffset(); } /** * Gets a child element. * * @param index the child index, >= 0 && < getElementCount() * @return the child element, null if none */ public Element getElement(int index) { if (index < nchildren) { return children[index]; } return null; } /** * Gets the number of children for the element. * * @return the number of children >= 0 */ public int getElementCount() { return nchildren; } /** * Gets the child element index closest to the given model offset. * * @param offset the offset >= 0 * @return the element index >= 0 */ public int getElementIndex(int offset) { int index; int lower = 0; int upper = nchildren - 1; int mid = 0; int p0 = getStartOffset(); int p1; if (nchildren == 0) { return 0; } if (offset >= getEndOffset()) { return nchildren - 1; } // see if the last index can be used. if ((lastIndex >= lower) && (lastIndex <= upper)) { Element lastHit = children[lastIndex]; p0 = lastHit.getStartOffset(); p1 = lastHit.getEndOffset(); if ((offset >= p0) && (offset < p1)) { return lastIndex; } // last index wasn't a hit, but it does give useful info about // where a hit (if any) would be. if (offset < p0) { upper = lastIndex; } else { lower = lastIndex; } } while (lower <= upper) { mid = lower + ((upper - lower) / 2); Element elem = children[mid]; p0 = elem.getStartOffset(); p1 = elem.getEndOffset(); if ((offset >= p0) && (offset < p1)) { // found the location index = mid; lastIndex = index; return index; } else if (offset < p0) { upper = mid - 1; } else { lower = mid + 1; } } // didn't find it, but we indicate the index of where it would belong if (offset < p0) { index = mid; } else { index = mid + 1; } lastIndex = index; return index; } /** * Checks whether the element is a leaf. * * @return true if a leaf */ public boolean isLeaf() { return false; } // ------ TreeNode ---------------------------------------------- /** * Returns true if the receiver allows children. * @return true if the receiver allows children, otherwise false */ public boolean getAllowsChildren() { return true; } /** * Returns the children of the receiver as an * <code>Enumeration</code>. * @return the children of the receiver */ public Enumeration children() { if(nchildren == 0) return null; Vector<AbstractElement> tempVector = new Vector<AbstractElement>(nchildren); for(int counter = 0; counter < nchildren; counter++) tempVector.addElement(children[counter]); return tempVector.elements(); } // ------ members ---------------------------------------------- private AbstractElement[] children; private int nchildren; private int lastIndex; } /** * Implements an element that directly represents content of * some kind. * <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}. * * @see Element */ public class LeafElement extends AbstractElement { /** * Constructs an element that represents content within the * document (has no children). * * @param parent The parent element * @param a The element attributes * @param offs0 The start offset >= 0 * @param offs1 The end offset >= offs0 * @since 1.4 */Lines 2001–2500 of 3118. Previous lines Load more
* Removes a set of attributes for the element. * * @param names the attribute names * @see MutableAttributeSet#removeAttributes */ public void removeAttributes(Enumeration<?> names) { checkForIllegalCast(); AttributeContext context = getAttributeContext(); attributes = context.removeAttributes(attributes, names); } /** * Removes a set of attributes for the element. * * @param attrs the attributes * @see MutableAttributeSet#removeAttributes */ public void removeAttributes(AttributeSet attrs) { checkForIllegalCast(); AttributeContext context = getAttributeContext(); if (attrs == this) { attributes = context.getEmptySet(); } else { attributes = context.removeAttributes(attributes, attrs); } } /** * Sets the resolving parent. * * @param parent the parent, null if none * @see MutableAttributeSet#setResolveParent */ public void setResolveParent(AttributeSet parent) { checkForIllegalCast(); AttributeContext context = getAttributeContext(); if (parent != null) { attributes = context.addAttribute(attributes, StyleConstants.ResolveAttribute, parent); } else { attributes = context.removeAttribute(attributes, StyleConstants.ResolveAttribute); } } private final void checkForIllegalCast() { Thread t = getCurrentWriter(); if ((t == null) || (t != Thread.currentThread())) { throw new StateInvariantError("Illegal cast to MutableAttributeSet"); } } // --- Element methods ------------------------------------- /** * Retrieves the underlying model. * * @return the model */ public Document getDocument() { return AbstractDocument.this; } /** * Gets the parent of the element. * * @return the parent */ public Element getParentElement() { return parent; } /** * Gets the attributes for the element. * * @return the attribute set */ public AttributeSet getAttributes() { return this; } /** * Gets the name of the element. * * @return the name, null if none */ public String getName() { if (attributes.isDefined(ElementNameAttribute)) { return (String) attributes.getAttribute(ElementNameAttribute); } return null; } /** * Gets the starting offset in the model for the element. * * @return the offset >= 0 */ public abstract int getStartOffset(); /** * Gets the ending offset in the model for the element. * * @return the offset >= 0 */ public abstract int getEndOffset(); /** * Gets a child element. * * @param index the child index, >= 0 && < getElementCount() * @return the child element */ public abstract Element getElement(int index); /** * Gets the number of children for the element. * * @return the number of children >= 0 */ public abstract int getElementCount(); /** * Gets the child element index closest to the given model offset. * * @param offset the offset >= 0 * @return the element index >= 0 */ public abstract int getElementIndex(int offset); /** * Checks whether the element is a leaf. * * @return true if a leaf */ public abstract boolean isLeaf(); // --- TreeNode methods ------------------------------------- /** * Returns the child <code>TreeNode</code> at index * <code>childIndex</code>. */ public TreeNode getChildAt(int childIndex) { return (TreeNode)getElement(childIndex); } /** * Returns the number of children <code>TreeNode</code>'s * receiver contains. * @return the number of children <code>TreeNodews</code>'s * receiver contains */ public int getChildCount() { return getElementCount(); } /** * Returns the parent <code>TreeNode</code> of the receiver. * @return the parent <code>TreeNode</code> of the receiver */ public TreeNode getParent() { return (TreeNode)getParentElement(); } /** * Returns the index of <code>node</code> in the receivers children. * If the receiver does not contain <code>node</code>, -1 will be * returned. * @param node the location of interest * @return the index of <code>node</code> in the receiver's * children, or -1 if absent */ public int getIndex(TreeNode node) { for(int counter = getChildCount() - 1; counter >= 0; counter--) if(getChildAt(counter) == node) return counter; return -1; } /** * Returns true if the receiver allows children. * @return true if the receiver allows children, otherwise false */ public abstract boolean getAllowsChildren(); /** * Returns the children of the receiver as an * <code>Enumeration</code>. * @return the children of the receiver as an <code>Enumeration</code> */ public abstract Enumeration children(); // --- serialization --------------------------------------------- private void writeObject(ObjectOutputStream s) throws IOException { s.defaultWriteObject(); StyleContext.writeAttributeSet(s, attributes); } private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException { s.defaultReadObject(); MutableAttributeSet attr = new SimpleAttributeSet(); StyleContext.readAttributeSet(s, attr); AttributeContext context = getAttributeContext(); attributes = context.addAttributes(SimpleAttributeSet.EMPTY, attr); } // ---- variables ----------------------------------------------------- private Element parent; private transient AttributeSet attributes; } /** * Implements a composite element that contains other elements. * <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 class BranchElement extends AbstractElement { /** * Constructs a composite element that initially contains * no children. * * @param parent The parent element * @param a the attributes for the element * @since 1.4 */ public BranchElement(Element parent, AttributeSet a) { super(parent, a); children = new AbstractElement[1]; nchildren = 0; lastIndex = -1; } /** * Gets the child element that contains * the given model position. * * @param pos the position >= 0 * @return the element, null if none */ public Element positionToElement(int pos) { int index = getElementIndex(pos); Element child = children[index]; int p0 = child.getStartOffset(); int p1 = child.getEndOffset(); if ((pos >= p0) && (pos < p1)) { return child; } return null; } /** * Replaces content with a new set of elements. * * @param offset the starting offset >= 0 * @param length the length to replace >= 0 * @param elems the new elements */ public void replace(int offset, int length, Element[] elems) { int delta = elems.length - length; int src = offset + length; int nmove = nchildren - src; int dest = src + delta; if ((nchildren + delta) >= children.length) { // need to grow the array int newLength = Math.max(2*children.length, nchildren + delta); AbstractElement[] newChildren = new AbstractElement[newLength]; System.arraycopy(children, 0, newChildren, 0, offset); System.arraycopy(elems, 0, newChildren, offset, elems.length); System.arraycopy(children, src, newChildren, dest, nmove); children = newChildren; } else { // patch the existing array System.arraycopy(children, src, children, dest, nmove); System.arraycopy(elems, 0, children, offset, elems.length); } nchildren = nchildren + delta; } /** * Converts the element to a string. * * @return the string */ public String toString() { return "BranchElement(" + getName() + ") " + getStartOffset() + "," + getEndOffset() + "\n"; } // --- Element methods ----------------------------------- /** * Gets the element name. * * @return the element name */ public String getName() { String nm = super.getName(); if (nm == null) { nm = ParagraphElementName; } return nm; } /** * Gets the starting offset in the model for the element. * * @return the offset >= 0 */ public int getStartOffset() { return children[0].getStartOffset(); } /** * Gets the ending offset in the model for the element. * @throws NullPointerException if this element has no children * * @return the offset >= 0 */ public int getEndOffset() { Element child = (nchildren > 0) ? children[nchildren - 1] : children[0]; return child.getEndOffset(); } /** * Gets a child element. * * @param index the child index, >= 0 && < getElementCount() * @return the child element, null if none */ public Element getElement(int index) { if (index < nchildren) { return children[index]; } return null; } /** * Gets the number of children for the element. * * @return the number of children >= 0 */ public int getElementCount() { return nchildren; } /** * Gets the child element index closest to the given model offset. * * @param offset the offset >= 0 * @return the element index >= 0 */ public int getElementIndex(int offset) { int index; int lower = 0; int upper = nchildren - 1; int mid = 0; int p0 = getStartOffset(); int p1; if (nchildren == 0) { return 0; } if (offset >= getEndOffset()) { return nchildren - 1; } // see if the last index can be used. if ((lastIndex >= lower) && (lastIndex <= upper)) { Element lastHit = children[lastIndex]; p0 = lastHit.getStartOffset(); p1 = lastHit.getEndOffset(); if ((offset >= p0) && (offset < p1)) { return lastIndex; } // last index wasn't a hit, but it does give useful info about // where a hit (if any) would be. if (offset < p0) { upper = lastIndex; } else { lower = lastIndex; } } while (lower <= upper) { mid = lower + ((upper - lower) / 2); Element elem = children[mid]; p0 = elem.getStartOffset(); p1 = elem.getEndOffset(); if ((offset >= p0) && (offset < p1)) { // found the location index = mid; lastIndex = index; return index; } else if (offset < p0) { upper = mid - 1; } else { lower = mid + 1; } } // didn't find it, but we indicate the index of where it would belong if (offset < p0) { index = mid; } else { index = mid + 1; } lastIndex = index; return index; } /** * Checks whether the element is a leaf. * * @return true if a leaf */ public boolean isLeaf() { return false; } // ------ TreeNode ---------------------------------------------- /** * Returns true if the receiver allows children. * @return true if the receiver allows children, otherwise false */ public boolean getAllowsChildren() { return true; } /** * Returns the children of the receiver as an * <code>Enumeration</code>. * @return the children of the receiver */ public Enumeration children() { if(nchildren == 0) return null; Vector<AbstractElement> tempVector = new Vector<AbstractElement>(nchildren); for(int counter = 0; counter < nchildren; counter++) tempVector.addElement(children[counter]); return tempVector.elements(); } // ------ members ---------------------------------------------- private AbstractElement[] children; private int nchildren; private int lastIndex; } /** * Implements an element that directly represents content of * some kind. * <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}. * * @see Element */ public class LeafElement extends AbstractElement { /** * Constructs an element that represents content within the * document (has no children). * * @param parent The parent element * @param a The element attributes * @param offs0 The start offset >= 0 * @param offs1 The end offset >= offs0 * @since 1.4 */