📄 abstractdocument.java
字号:
* Returns the <code>Element</code> at the specified <code>Document</code> * offset. * * @return the <code>Element</code> at the specified <code>Document</code> * offset * * @see #getElementIndex(int) */ public Element positionToElement(int position) { // XXX: There is surely a better algorithm // as beginning from first element each time. for (int index = 0; index < children.length; ++index) { Element elem = children[index]; if ((elem.getStartOffset() <= position) && (position < elem.getEndOffset())) return elem; } return null; } /** * Replaces a set of child elements with a new set of child elemens. * * @param offset the start index of the elements to be removed * @param length the number of elements to be removed * @param elements the new elements to be inserted */ public void replace(int offset, int length, Element[] elements) { Element[] target = new Element[children.length - length + elements.length]; System.arraycopy(children, 0, target, 0, offset); System.arraycopy(elements, 0, target, offset, elements.length); System.arraycopy(children, offset + length, target, offset + elements.length, children.length - offset - length); children = target; } /** * Returns a string representation of this element. * * @return a string representation of this element */ public String toString() { return ("BranchElement(" + getName() + ") " + getStartOffset() + "," + getEndOffset() + "\n"); } } /** * Stores the changes when a <code>Document</code> is beeing modified. */ public class DefaultDocumentEvent extends CompoundEdit implements DocumentEvent { /** The serialization UID (compatible with JDK1.5). */ private static final long serialVersionUID = 5230037221564563284L; /** The starting offset of the change. */ private int offset; /** The length of the change. */ private int length; /** The type of change. */ private DocumentEvent.EventType type; /** * Maps <code>Element</code> to their change records. */ Hashtable changes; /** * Creates a new <code>DefaultDocumentEvent</code>. * * @param offset the starting offset of the change * @param length the length of the change * @param type the type of change */ public DefaultDocumentEvent(int offset, int length, DocumentEvent.EventType type) { this.offset = offset; this.length = length; this.type = type; changes = new Hashtable(); } /** * Adds an UndoableEdit to this <code>DocumentEvent</code>. If this * edit is an instance of {@link ElementEdit}, then this record can * later be fetched by calling {@link #getChange}. * * @param edit the undoable edit to add */ public boolean addEdit(UndoableEdit edit) { // XXX - Fully qualify ElementChange to work around gcj bug #2499. if (edit instanceof DocumentEvent.ElementChange) { DocumentEvent.ElementChange elEdit = (DocumentEvent.ElementChange) edit; changes.put(elEdit.getElement(), elEdit); } return super.addEdit(edit); } /** * Returns the document that has been modified. * * @return the document that has been modified */ public Document getDocument() { return AbstractDocument.this; } /** * Returns the length of the modification. * * @return the length of the modification */ public int getLength() { return length; } /** * Returns the start offset of the modification. * * @return the start offset of the modification */ public int getOffset() { return offset; } /** * Returns the type of the modification. * * @return the type of the modification */ public DocumentEvent.EventType getType() { return type; } /** * Returns the changes for an element. * * @param elem the element for which the changes are requested * * @return the changes for <code>elem</code> or <code>null</code> if * <code>elem</code> has not been changed */ public DocumentEvent.ElementChange getChange(Element elem) { // XXX - Fully qualify ElementChange to work around gcj bug #2499. return (DocumentEvent.ElementChange) changes.get(elem); } } /** * An implementation of {@link DocumentEvent.ElementChange} to be added * to {@link DefaultDocumentEvent}s. */ public static class ElementEdit extends AbstractUndoableEdit implements DocumentEvent.ElementChange { /** The serial version UID of ElementEdit. */ private static final long serialVersionUID = -1216620962142928304L; /** * The changed element. */ private Element elem; /** * The index of the change. */ private int index; /** * The removed elements. */ private Element[] removed; /** * The added elements. */ private Element[] added; /** * Creates a new <code>ElementEdit</code>. * * @param elem the changed element * @param index the index of the change * @param removed the removed elements * @param added the added elements */ public ElementEdit(Element elem, int index, Element[] removed, Element[] added) { this.elem = elem; this.index = index; this.removed = removed; this.added = added; } /** * Returns the added elements. * * @return the added elements */ public Element[] getChildrenAdded() { return added; } /** * Returns the removed elements. * * @return the removed elements */ public Element[] getChildrenRemoved() { return removed; } /** * Returns the changed element. * * @return the changed element */ public Element getElement() { return elem; } /** * Returns the index of the change. * * @return the index of the change */ public int getIndex() { return index; } } /** * An implementation of {@link Element} that represents a leaf in the * document structure. This is used to actually store content. */ public class LeafElement extends AbstractElement { /** The serialization UID (compatible with JDK1.5). */ private static final long serialVersionUID = -8906306331347768017L; /** Manages the start offset of this element. */ Position startPos; /** Manages the end offset of this element. */ Position endPos; /** * Creates a new <code>LeafElement</code>. * * @param parent the parent of this <code>LeafElement</code> * @param attributes the attributes to be set * @param start the start index of this element inside the document model * @param end the end index of this element inside the document model */ public LeafElement(Element parent, AttributeSet attributes, int start, int end) { super(parent, attributes); { try { if (parent != null) { startPos = parent.getDocument().createPosition(start); endPos = parent.getDocument().createPosition(end); } else { startPos = createPosition(start); endPos = createPosition(end); } } catch (BadLocationException ex) { AssertionError as; as = new AssertionError("BadLocationException thrown " + "here. start=" + start + ", end=" + end + ", length=" + getLength()); as.initCause(ex); throw as; } } } /** * Returns <code>null</code> since <code>LeafElement</code>s cannot have * children. * * @return <code>null</code> since <code>LeafElement</code>s cannot have * children */ public Enumeration children() { return null; } /** * Returns <code>false</code> since <code>LeafElement</code>s cannot have * children. * * @return <code>false</code> since <code>LeafElement</code>s cannot have * children */ public boolean getAllowsChildren() { return false; } /** * Returns <code>null</code> since <code>LeafElement</code>s cannot have * children. * * @return <code>null</code> since <code>LeafElement</code>s cannot have * children */ public Element getElement(int index) { return null; } /** * Returns <code>0</code> since <code>LeafElement</code>s cannot have * children. * * @return <code>0</code> since <code>LeafElement</code>s cannot have * children */ public int getElementCount() { return 0; } /** * Returns <code>-1</code> since <code>LeafElement</code>s cannot have * children. * * @return <code>-1</code> since <code>LeafElement</code>s cannot have * children */ public int getElementIndex(int offset) { return -1; } /** * Returns the end offset of this <code>Element</code> inside the * document. * * @return the end offset of this <code>Element</code> inside the * document */ public int getEndOffset() { return endPos.getOffset(); } /** * Returns the name of this <code>Element</code>. This is * {@link #ContentElementName} in this case. * * @return the name of this <code>Element</code> */ public String getName() { String name = super.getName(); if (name == null) name = ContentElementName; return name; } /** * Returns the start offset of this <code>Element</code> inside the * document. * * @return the start offset of this <code>Element</code> inside the * document */ public int getStartOffset() { return startPos.getOffset(); } /** * Returns <code>true</code>. * * @return <code>true</code> */ public boolean isLeaf() { return true; } /** * Returns a string representation of this <code>Element</code>. * * @return a string representation of this <code>Element</code> */ public String toString() { return ("LeafElement(" + getName() + ") " + getStartOffset() + "," + getEndOffset() + "\n"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -