38 |
package javax.swing.text; |
package javax.swing.text; |
39 |
|
|
40 |
import java.io.Serializable; |
import java.io.Serializable; |
41 |
|
import java.util.Collections; |
42 |
import java.util.Dictionary; |
import java.util.Dictionary; |
43 |
import java.util.Enumeration; |
import java.util.Enumeration; |
44 |
import java.util.EventListener; |
import java.util.EventListener; |
64 |
public static final String ParagraphElementName = "paragraph"; |
public static final String ParagraphElementName = "paragraph"; |
65 |
public static final String SectionElementName = "section"; |
public static final String SectionElementName = "section"; |
66 |
public static final String ElementNameAttribute = "$ename"; |
public static final String ElementNameAttribute = "$ename"; |
67 |
|
|
68 |
Content content; |
Content content; |
69 |
|
AttributeContext context; |
70 |
protected EventListenerList listenerList = new EventListenerList(); |
protected EventListenerList listenerList = new EventListenerList(); |
71 |
|
|
72 |
protected AbstractDocument(Content doc) |
protected AbstractDocument(Content doc) |
73 |
{ |
{ |
74 |
this(doc, null); |
this(doc, StyleContext.getDefaultStyleContext()); |
75 |
} |
} |
76 |
|
|
77 |
protected AbstractDocument(Content doc, AttributeContext context) |
protected AbstractDocument(Content doc, AttributeContext ctx) |
78 |
{ |
{ |
79 |
content = doc; |
content = doc; |
80 |
|
context = ctx; |
81 |
} |
} |
82 |
|
|
83 |
// these still need to be implemented by a derived class: |
// These still need to be implemented by a derived class: |
84 |
public abstract Element getParagraphElement(int pos); |
public abstract Element getParagraphElement(int pos); |
85 |
|
|
86 |
public abstract Element getDefaultRootElement(); |
public abstract Element getDefaultRootElement(); |
87 |
|
|
88 |
protected Element createBranchElement(Element parent, AttributeSet a) |
protected Element createBranchElement(Element parent, |
89 |
|
AttributeSet attributes) |
90 |
{ |
{ |
91 |
return new BranchElement(parent, a, 0, 0); |
return new BranchElement(parent, attributes); |
92 |
} |
} |
93 |
|
|
94 |
protected Element createLeafElement(Element parent, AttributeSet a, int p0, |
protected Element createLeafElement(Element parent, AttributeSet attributes, |
95 |
int p1) |
int start, int end) |
96 |
{ |
{ |
97 |
return new LeafElement(parent, a, p0, p1 - p0); |
return new LeafElement(parent, attributes, start, end); |
98 |
} |
} |
99 |
|
|
100 |
public Position createPosition(final int offset) throws BadLocationException |
public Position createPosition(final int offset) throws BadLocationException |
150 |
|
|
151 |
protected AttributeContext getAttributeContext() |
protected AttributeContext getAttributeContext() |
152 |
{ |
{ |
153 |
return null; |
return context; |
154 |
} |
} |
155 |
|
|
156 |
public Element getBidiRootElement() |
public Element getBidiRootElement() |
175 |
|
|
176 |
public Position getEndPosition() |
public Position getEndPosition() |
177 |
{ |
{ |
178 |
return null; |
return new Position() |
179 |
|
{ |
180 |
|
public int getOffset() |
181 |
|
{ |
182 |
|
return getLength(); |
183 |
|
} |
184 |
|
}; |
185 |
} |
} |
186 |
|
|
187 |
public int getLength() |
public int getLength() |
201 |
|
|
202 |
public Element[] getRootElements() |
public Element[] getRootElements() |
203 |
{ |
{ |
204 |
return null; |
Element[] elements = new Element[1]; |
205 |
|
elements[0] = getDefaultRootElement(); |
206 |
|
return elements; |
207 |
} |
} |
208 |
|
|
209 |
public Position getStartPosition() |
public Position getStartPosition() |
210 |
{ |
{ |
211 |
return null; |
return new Position() |
212 |
|
{ |
213 |
|
public int getOffset() |
214 |
|
{ |
215 |
|
return 0; |
216 |
|
} |
217 |
|
}; |
218 |
} |
} |
219 |
|
|
220 |
public String getText(int offset, int length) throws BadLocationException |
public String getText(int offset, int length) throws BadLocationException |
222 |
return content.getString(offset, length); |
return content.getString(offset, length); |
223 |
} |
} |
224 |
|
|
225 |
public void getText(int offset, int length, Segment txt) |
public void getText(int offset, int length, Segment segment) |
226 |
throws BadLocationException |
throws BadLocationException |
227 |
{ |
{ |
228 |
String a = getText(offset, length); |
content.getChars(offset, length, segment); |
|
|
|
|
if (a == null) |
|
|
{ |
|
|
txt.offset = 0; |
|
|
txt.count = 0; |
|
|
txt.array = new char[0]; |
|
|
|
|
|
return; |
|
|
} |
|
|
|
|
|
txt.offset = offset; |
|
|
txt.count = length; |
|
|
|
|
|
char[] chars = new char[a.length()]; |
|
|
|
|
|
a.getChars(0, a.length(), chars, 0); |
|
|
|
|
|
txt.array = chars; |
|
229 |
} |
} |
230 |
|
|
231 |
public void insertString(int offs, String str, AttributeSet a) |
public void insertString(int offset, String text, AttributeSet attributes) |
232 |
throws BadLocationException |
throws BadLocationException |
233 |
{ |
{ |
234 |
content.insertString(offs, str); |
// Just return when no text to insert was given. |
235 |
|
if (text == null || text.length() == 0) |
236 |
|
return; |
237 |
|
|
238 |
|
DefaultDocumentEvent event = |
239 |
|
new DefaultDocumentEvent(offset, text.length(), |
240 |
|
DocumentEvent.EventType.INSERT); |
241 |
|
content.insertString(offset, text); |
242 |
|
insertUpdate(event, attributes); |
243 |
|
fireInsertUpdate(event); |
244 |
} |
} |
245 |
|
|
246 |
protected void insertUpdate(DefaultDocumentEvent chng, AttributeSet attr) |
protected void insertUpdate(DefaultDocumentEvent chng, AttributeSet attr) |
265 |
|
|
266 |
public void remove(int offset, int length) throws BadLocationException |
public void remove(int offset, int length) throws BadLocationException |
267 |
{ |
{ |
268 |
|
DefaultDocumentEvent event = |
269 |
|
new DefaultDocumentEvent(offset, length, |
270 |
|
DocumentEvent.EventType.REMOVE); |
271 |
|
removeUpdate(event); |
272 |
|
content.remove(offset, length); |
273 |
|
postRemoveUpdate(event); |
274 |
|
fireRemoveUpdate(event); |
275 |
|
} |
276 |
|
|
277 |
|
/** |
278 |
|
* Replaces some text in the document. |
279 |
|
* |
280 |
|
* @since 1.4 |
281 |
|
*/ |
282 |
|
public void replace(int offset, int length, String text, |
283 |
|
AttributeSet attributes) |
284 |
|
throws BadLocationException |
285 |
|
{ |
286 |
|
remove(offset, length); |
287 |
|
insertString(offset, text, attributes); |
288 |
} |
} |
289 |
|
|
290 |
/** |
/** |
405 |
} |
} |
406 |
|
|
407 |
public abstract class AbstractElement |
public abstract class AbstractElement |
408 |
implements Element, TreeNode, Serializable |
implements Element, MutableAttributeSet, TreeNode, Serializable |
409 |
{ |
{ |
410 |
private static final long serialVersionUID = 1265312733007397733L; |
private static final long serialVersionUID = 1265312733007397733L; |
411 |
int count; |
int count; |
412 |
int offset; |
int offset; |
413 |
AttributeSet attr; |
|
414 |
Vector elts = new Vector(); |
AttributeSet attributes; |
415 |
String name; |
|
416 |
Element parent; |
Element element_parent; |
417 |
Vector kids = new Vector(); |
Vector element_children; |
418 |
|
|
419 |
TreeNode tree_parent; |
TreeNode tree_parent; |
420 |
|
Vector tree_children; |
421 |
|
|
422 |
public AbstractElement(Element p, AttributeSet s) |
public AbstractElement(Element p, AttributeSet s) |
423 |
{ |
{ |
424 |
parent = p; |
element_parent = p; |
425 |
attr = s; |
attributes = s; |
426 |
} |
} |
427 |
|
|
428 |
|
// TreeNode implementation |
429 |
|
|
430 |
public Enumeration children() |
public Enumeration children() |
431 |
{ |
{ |
432 |
return kids.elements(); |
return Collections.enumeration(tree_children); |
433 |
} |
} |
434 |
|
|
435 |
public boolean getAllowsChildren() |
public boolean getAllowsChildren() |
436 |
{ |
{ |
437 |
return true; |
return true; |
438 |
} |
} |
439 |
|
|
440 |
public TreeNode getChildAt(int index) |
public TreeNode getChildAt(int index) |
441 |
{ |
{ |
442 |
return (TreeNode) kids.elementAt(index); |
return (TreeNode) tree_children.get(index); |
443 |
} |
} |
444 |
|
|
445 |
public int getChildCount() |
public int getChildCount() |
446 |
{ |
{ |
447 |
return kids.size(); |
return tree_children.size(); |
448 |
} |
} |
449 |
|
|
450 |
public int getIndex(TreeNode node) |
public int getIndex(TreeNode node) |
451 |
{ |
{ |
452 |
return kids.indexOf(node); |
return tree_children.indexOf(node); |
453 |
} |
} |
454 |
|
|
455 |
public TreeNode getParent() |
public TreeNode getParent() |
457 |
return tree_parent; |
return tree_parent; |
458 |
} |
} |
459 |
|
|
460 |
|
public abstract boolean isLeaf(); |
461 |
|
|
462 |
|
|
463 |
|
// MutableAttributeSet support |
464 |
|
|
465 |
|
public void addAttribute(Object name, Object value) |
466 |
|
{ |
467 |
|
attributes = getAttributeContext().addAttribute(attributes, name, value); |
468 |
|
} |
469 |
|
|
470 |
|
public void addAttributes(AttributeSet attrs) |
471 |
|
{ |
472 |
|
attributes = getAttributeContext().addAttributes(attributes, attrs); |
473 |
|
} |
474 |
|
|
475 |
|
public void removeAttribute(Object name) |
476 |
|
{ |
477 |
|
attributes = getAttributeContext().removeAttribute(attributes, name); |
478 |
|
} |
479 |
|
|
480 |
|
public void removeAttributes(AttributeSet attrs) |
481 |
|
{ |
482 |
|
attributes = getAttributeContext().removeAttributes(attributes, attrs); |
483 |
|
} |
484 |
|
|
485 |
|
public void removeAttributes(Enumeration names) |
486 |
|
{ |
487 |
|
attributes = getAttributeContext().removeAttributes(attributes, names); |
488 |
|
} |
489 |
|
|
490 |
|
public void setResolveParent(AttributeSet parent) |
491 |
|
{ |
492 |
|
attributes = getAttributeContext().addAttribute(attributes, ResolveAttribute, parent); |
493 |
|
} |
494 |
|
|
495 |
|
|
496 |
|
// AttributeSet interface support |
497 |
|
|
498 |
|
public boolean containsAttribute(Object name, Object value) |
499 |
|
{ |
500 |
|
return attributes.containsAttribute(name, value); |
501 |
|
} |
502 |
|
|
503 |
|
public boolean containsAttributes(AttributeSet attrs) |
504 |
|
{ |
505 |
|
return attributes.containsAttributes(attrs); |
506 |
|
} |
507 |
|
|
508 |
|
public AttributeSet copyAttributes() |
509 |
|
{ |
510 |
|
return attributes.copyAttributes(); |
511 |
|
} |
512 |
|
|
513 |
|
public Object getAttribute(Object key) |
514 |
|
{ |
515 |
|
return attributes.getAttribute(key); |
516 |
|
} |
517 |
|
|
518 |
|
public int getAttributeCount() |
519 |
|
{ |
520 |
|
return attributes.getAttributeCount(); |
521 |
|
} |
522 |
|
|
523 |
|
public Enumeration getAttributeNames() |
524 |
|
{ |
525 |
|
return attributes.getAttributeNames(); |
526 |
|
} |
527 |
|
|
528 |
|
public AttributeSet getResolveParent() |
529 |
|
{ |
530 |
|
return attributes.getResolveParent(); |
531 |
|
} |
532 |
|
|
533 |
|
public boolean isDefined(Object attrName) |
534 |
|
{ |
535 |
|
return attributes.isDefined(attrName); |
536 |
|
} |
537 |
|
|
538 |
|
public boolean isEqual(AttributeSet attrs) |
539 |
|
{ |
540 |
|
return attributes.isEqual(attrs); |
541 |
|
} |
542 |
|
|
543 |
|
// Element interface support |
544 |
|
|
545 |
public AttributeSet getAttributes() |
public AttributeSet getAttributes() |
546 |
{ |
{ |
547 |
return attr; |
return attributes; |
548 |
} |
} |
549 |
|
|
550 |
public Document getDocument() |
public Document getDocument() |
551 |
{ |
{ |
552 |
return AbstractDocument.this; |
return AbstractDocument.this; |
553 |
} |
} |
554 |
|
|
555 |
public Element getElement(int index) |
public Element getElement(int index) |
556 |
{ |
{ |
557 |
return (Element) elts.elementAt(index); |
return (Element) element_children.get(index); |
558 |
} |
} |
559 |
|
|
560 |
public String getName() |
public String getName() |
561 |
{ |
{ |
562 |
return name; |
return (String) getAttribute(NameAttribute); |
563 |
} |
} |
564 |
|
|
565 |
public Element getParentElement() |
public Element getParentElement() |
566 |
{ |
{ |
567 |
return parent; |
return element_parent; |
568 |
} |
} |
569 |
|
|
|
public abstract boolean isLeaf(); |
|
|
|
|
570 |
public abstract int getEndOffset(); |
public abstract int getEndOffset(); |
571 |
|
|
572 |
public abstract int getElementCount(); |
public abstract int getElementCount(); |
573 |
|
|
574 |
public abstract int getElementIndex(int offset); |
public abstract int getElementIndex(int offset); |
575 |
|
|
576 |
public abstract int getStartOffset(); |
public abstract int getStartOffset(); |
577 |
} |
} |
578 |
|
|
579 |
public class BranchElement extends AbstractElement |
public class BranchElement extends AbstractElement |
580 |
{ |
{ |
581 |
private static final long serialVersionUID = -8595176318868717313L; |
private static final long serialVersionUID = -8595176318868717313L; |
582 |
private int start; |
|
|
private int end; |
|
583 |
private Vector children = new Vector(); |
private Vector children = new Vector(); |
584 |
|
|
585 |
public BranchElement(Element parent, AttributeSet attributes, int start, |
public BranchElement(Element parent, AttributeSet attributes) |
|
int end) |
|
586 |
{ |
{ |
587 |
super(parent, attributes); |
super(parent, attributes); |
|
this.start = start; |
|
|
this.end = end; |
|
588 |
} |
} |
589 |
|
|
590 |
public Enumeration children() |
public Enumeration children() |
599 |
|
|
600 |
public Element getElement(int index) |
public Element getElement(int index) |
601 |
{ |
{ |
602 |
|
if (index < 0 || index >= children.size()) |
603 |
|
return null; |
604 |
|
|
605 |
return (Element) children.get(index); |
return (Element) children.get(index); |
606 |
} |
} |
607 |
|
|
612 |
|
|
613 |
public int getElementIndex(int offset) |
public int getElementIndex(int offset) |
614 |
{ |
{ |
615 |
return children.indexOf(positionToElement(offset)); |
if (children.size() == 0) |
616 |
|
return 0; |
617 |
|
|
618 |
|
Element element = positionToElement(offset); |
619 |
|
|
620 |
|
if (element == null) |
621 |
|
return 0; |
622 |
|
|
623 |
|
return children.indexOf(element); |
624 |
} |
} |
625 |
|
|
626 |
public int getEndOffset() |
public int getEndOffset() |
627 |
{ |
{ |
628 |
return end; |
return ((Element) children.lastElement()).getEndOffset(); |
629 |
} |
} |
630 |
|
|
631 |
public String getName() |
public String getName() |
635 |
|
|
636 |
public int getStartOffset() |
public int getStartOffset() |
637 |
{ |
{ |
638 |
return start; |
return ((Element) children.firstElement()).getStartOffset(); |
639 |
} |
} |
640 |
|
|
641 |
public boolean isLeaf() |
public boolean isLeaf() |
678 |
implements DocumentEvent |
implements DocumentEvent |
679 |
{ |
{ |
680 |
private static final long serialVersionUID = -7406103236022413522L; |
private static final long serialVersionUID = -7406103236022413522L; |
681 |
public int len; |
|
682 |
public int off; |
private int offset; |
683 |
|
private int length; |
684 |
|
private DocumentEvent.EventType type; |
685 |
|
|
686 |
|
public DefaultDocumentEvent(int offset, int length, |
687 |
|
DocumentEvent.EventType type) |
688 |
|
{ |
689 |
|
this.offset = offset; |
690 |
|
this.length = length; |
691 |
|
this.type = type; |
692 |
|
} |
693 |
|
|
694 |
public Document getDocument() |
public Document getDocument() |
695 |
{ |
{ |
698 |
|
|
699 |
public int getLength() |
public int getLength() |
700 |
{ |
{ |
701 |
return len; |
return length; |
702 |
} |
} |
703 |
|
|
704 |
public int getOffset() |
public int getOffset() |
705 |
{ |
{ |
706 |
return off; |
return offset; |
707 |
} |
} |
708 |
|
|
709 |
public DocumentEvent.EventType getType() |
public DocumentEvent.EventType getType() |
710 |
{ |
{ |
711 |
return null; |
return type; |
712 |
} |
} |
713 |
|
|
714 |
public DocumentEvent.ElementChange getChange(Element elem) |
public DocumentEvent.ElementChange getChange(Element elem) |
718 |
} |
} |
719 |
|
|
720 |
public static class ElementEdit extends AbstractUndoableEdit |
public static class ElementEdit extends AbstractUndoableEdit |
721 |
|
implements DocumentEvent.ElementChange |
722 |
{ |
{ |
723 |
private static final long serialVersionUID = -1216620962142928304L; |
private static final long serialVersionUID = -1216620962142928304L; |
724 |
|
|
725 |
|
private Element elem; |
726 |
|
private int index; |
727 |
|
private Element[] removed; |
728 |
|
private Element[] added; |
729 |
|
|
730 |
|
public ElementEdit(Element elem, int index, |
731 |
|
Element[] removed, Element[] added) |
732 |
|
{ |
733 |
|
this.elem = elem; |
734 |
|
this.index = index; |
735 |
|
this.removed = removed; |
736 |
|
this.added = added; |
737 |
|
} |
738 |
|
|
739 |
|
public Element[] getChildrenAdded() |
740 |
|
{ |
741 |
|
return added; |
742 |
|
} |
743 |
|
|
744 |
|
public Element[] getChildrenRemoved() |
745 |
|
{ |
746 |
|
return removed; |
747 |
|
} |
748 |
|
|
749 |
|
public Element getElement() |
750 |
|
{ |
751 |
|
return elem; |
752 |
|
} |
753 |
|
|
754 |
|
public int getIndex() |
755 |
|
{ |
756 |
|
return index; |
757 |
|
} |
758 |
} |
} |
759 |
|
|
760 |
public class LeafElement extends AbstractElement |
public class LeafElement extends AbstractElement |