42 |
import java.util.Enumeration; |
import java.util.Enumeration; |
43 |
import java.util.EventListener; |
import java.util.EventListener; |
44 |
import java.util.Vector; |
import java.util.Vector; |
|
|
|
45 |
import javax.swing.event.DocumentEvent; |
import javax.swing.event.DocumentEvent; |
46 |
import javax.swing.event.DocumentListener; |
import javax.swing.event.DocumentListener; |
47 |
import javax.swing.event.EventListenerList; |
import javax.swing.event.EventListenerList; |
56 |
public abstract class AbstractDocument |
public abstract class AbstractDocument |
57 |
implements Document, Serializable |
implements Document, Serializable |
58 |
{ |
{ |
|
public abstract class AbstractElement |
|
|
implements Element, TreeNode, Serializable |
|
|
{ |
|
|
private static final long serialVersionUID = 1265312733007397733L; |
|
|
|
|
|
int count; |
|
|
int offset; |
|
|
AttributeSet attr; |
|
|
Vector elts = new Vector(); |
|
|
String name; |
|
|
Element parent; |
|
|
Vector kids = new Vector(); |
|
|
TreeNode tree_parent; |
|
|
|
|
|
public AbstractElement(Element p, AttributeSet s) |
|
|
{ |
|
|
parent = p; |
|
|
attr = s; |
|
|
} |
|
|
|
|
|
public Enumeration children() |
|
|
{ |
|
|
return kids.elements(); |
|
|
} |
|
|
|
|
|
public boolean getAllowsChildren() |
|
|
{ |
|
|
return true; |
|
|
} |
|
|
|
|
|
public TreeNode getChildAt(int index) |
|
|
{ |
|
|
return (TreeNode) kids.elementAt(index); |
|
|
} |
|
|
|
|
|
public int getChildCount() |
|
|
{ |
|
|
return kids.size(); |
|
|
} |
|
|
|
|
|
public int getIndex(TreeNode node) |
|
|
{ |
|
|
return kids.indexOf(node); |
|
|
} |
|
|
|
|
|
public TreeNode getParent() |
|
|
{ |
|
|
return tree_parent; |
|
|
} |
|
|
|
|
|
public AttributeSet getAttributes() |
|
|
{ |
|
|
return attr; |
|
|
} |
|
|
|
|
|
public Document getDocument() |
|
|
{ |
|
|
return AbstractDocument.this; |
|
|
} |
|
|
|
|
|
public Element getElement(int index) |
|
|
{ |
|
|
return (Element) elts.elementAt(index); |
|
|
} |
|
|
|
|
|
public String getName() |
|
|
{ |
|
|
return name; |
|
|
} |
|
|
|
|
|
public Element getParentElement() |
|
|
{ |
|
|
return parent; |
|
|
} |
|
|
|
|
|
public abstract boolean isLeaf(); |
|
|
|
|
|
public abstract int getEndOffset(); |
|
|
|
|
|
public abstract int getElementCount(); |
|
|
|
|
|
public abstract int getElementIndex(int offset); |
|
|
|
|
|
public abstract int getStartOffset(); |
|
|
} |
|
|
|
|
|
public interface AttributeContext |
|
|
{ |
|
|
} |
|
|
|
|
|
public class BranchElement extends AbstractElement |
|
|
{ |
|
|
private static final long serialVersionUID = -8595176318868717313L; |
|
|
|
|
|
public BranchElement(Element e, AttributeSet a, int s, int end) |
|
|
{ |
|
|
super(e, a); |
|
|
} |
|
|
|
|
|
public boolean isLeaf() |
|
|
{ |
|
|
return false; |
|
|
} |
|
|
|
|
|
public int getEndOffset() |
|
|
{ |
|
|
return 0; |
|
|
} |
|
|
|
|
|
public int getElementCount() |
|
|
{ |
|
|
return 0; |
|
|
} |
|
|
|
|
|
public int getElementIndex(int offset) |
|
|
{ |
|
|
return 0; |
|
|
} |
|
|
|
|
|
public int getStartOffset() |
|
|
{ |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
public interface Content |
|
|
{ |
|
|
Position createPosition(int offset) throws BadLocationException; |
|
|
|
|
|
int length(); |
|
|
|
|
|
UndoableEdit insertString(int where, String str) |
|
|
throws BadLocationException; |
|
|
|
|
|
UndoableEdit remove(int where, int nitems) throws BadLocationException; |
|
|
|
|
|
String getString(int where, int len) throws BadLocationException; |
|
|
|
|
|
void getChars(int where, int len, Segment txt) throws BadLocationException; |
|
|
} |
|
|
|
|
|
public class DefaultDocumentEvent extends CompoundEdit |
|
|
implements DocumentEvent |
|
|
{ |
|
|
private static final long serialVersionUID = -7406103236022413522L; |
|
|
|
|
|
public int len; |
|
|
public int off; |
|
|
|
|
|
public Document getDocument() |
|
|
{ |
|
|
return AbstractDocument.this; |
|
|
} |
|
|
|
|
|
public int getLength() |
|
|
{ |
|
|
return len; |
|
|
} |
|
|
|
|
|
public int getOffset() |
|
|
{ |
|
|
return off; |
|
|
} |
|
|
|
|
|
public DocumentEvent.EventType getType() |
|
|
{ |
|
|
return null; |
|
|
} |
|
|
|
|
|
public DocumentEvent.ElementChange getChange(Element elem) |
|
|
{ |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
public static class ElementEdit extends AbstractUndoableEdit |
|
|
{ |
|
|
private static final long serialVersionUID = -1216620962142928304L; |
|
|
} |
|
|
|
|
|
public class LeafElement extends AbstractElement |
|
|
{ |
|
|
private static final long serialVersionUID = 5115368706941283802L; |
|
|
|
|
|
public LeafElement(Element e, AttributeSet a, int s, int end) |
|
|
{ |
|
|
super(e, a); |
|
|
} |
|
|
|
|
|
public boolean isLeaf() |
|
|
{ |
|
|
return true; |
|
|
} |
|
|
|
|
|
public int getEndOffset() |
|
|
{ |
|
|
return 0; |
|
|
} |
|
|
|
|
|
public int getElementCount() |
|
|
{ |
|
|
return 0; |
|
|
} |
|
|
|
|
|
public int getElementIndex(int offset) |
|
|
{ |
|
|
return 0; |
|
|
} |
|
|
|
|
|
public int getStartOffset() |
|
|
{ |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
59 |
private static final long serialVersionUID = -116069779446114664L; |
private static final long serialVersionUID = -116069779446114664L; |
|
|
|
60 |
protected static final String BAD_LOCATION = "document location failure"; |
protected static final String BAD_LOCATION = "document location failure"; |
|
|
|
61 |
public static final String BidiElementName = "bidi level"; |
public static final String BidiElementName = "bidi level"; |
62 |
public static final String ContentElementName = "content"; |
public static final String ContentElementName = "content"; |
63 |
public static final String ParagraphElementName = "paragraph"; |
public static final String ParagraphElementName = "paragraph"; |
64 |
public static final String SectionElementName = "section"; |
public static final String SectionElementName = "section"; |
65 |
public static final String ElementNameAttribute = "$ename"; |
public static final String ElementNameAttribute = "$ename"; |
|
|
|
66 |
Content content; |
Content content; |
67 |
|
protected EventListenerList listenerList = new EventListenerList(); |
68 |
|
|
69 |
protected AbstractDocument(Content doc) |
protected AbstractDocument(Content doc) |
70 |
{ |
{ |
76 |
content = doc; |
content = doc; |
77 |
} |
} |
78 |
|
|
|
protected EventListenerList listenerList = new EventListenerList(); |
|
|
|
|
79 |
// these still need to be implemented by a derived class: |
// these still need to be implemented by a derived class: |
80 |
public abstract Element getParagraphElement(int pos); |
public abstract Element getParagraphElement(int pos); |
81 |
|
|
92 |
return new LeafElement(parent, a, p0, p1 - p0); |
return new LeafElement(parent, a, p0, p1 - p0); |
93 |
} |
} |
94 |
|
|
95 |
public Position createPosition(int offs) |
public Position createPosition(final int offset) throws BadLocationException |
96 |
{ |
{ |
97 |
final int a = offs; |
if (offset < 0 || offset > getLength()) |
98 |
|
throw new BadLocationException(getText(0, getLength()), offset); |
99 |
|
|
100 |
return new Position() |
return new Position() |
101 |
{ |
{ |
102 |
public int getOffset() |
public int getOffset() |
103 |
{ |
{ |
104 |
return a; |
return offset; |
105 |
} |
} |
106 |
}; |
}; |
107 |
} |
} |
198 |
return null; |
return null; |
199 |
} |
} |
200 |
|
|
201 |
public String getText(int offset, int length) |
public String getText(int offset, int length) throws BadLocationException |
|
{ |
|
|
try |
|
202 |
{ |
{ |
203 |
return content.getString(offset, length); |
return content.getString(offset, length); |
204 |
} |
} |
|
catch (Exception e) |
|
|
{ |
|
|
System.out.println("Hmmm, fail to getText: " + offset + " -> " |
|
|
+ length); |
|
|
return null; |
|
|
} |
|
|
} |
|
205 |
|
|
206 |
public void getText(int offset, int length, Segment txt) |
public void getText(int offset, int length, Segment txt) |
207 |
|
throws BadLocationException |
208 |
{ |
{ |
209 |
String a = getText(offset, length); |
String a = getText(offset, length); |
210 |
|
|
213 |
txt.offset = 0; |
txt.offset = 0; |
214 |
txt.count = 0; |
txt.count = 0; |
215 |
txt.array = new char[0]; |
txt.array = new char[0]; |
216 |
|
|
217 |
return; |
return; |
218 |
} |
} |
219 |
|
|
253 |
{ |
{ |
254 |
} |
} |
255 |
|
|
256 |
public void remove(int offs, int len) |
public void remove(int offset, int length) throws BadLocationException |
257 |
{ |
{ |
258 |
} |
} |
259 |
|
|
340 |
protected void writeUnlock() |
protected void writeUnlock() |
341 |
{ |
{ |
342 |
} |
} |
343 |
|
|
344 |
|
public interface AttributeContext |
345 |
|
{ |
346 |
|
AttributeSet addAttribute(AttributeSet old, Object name, Object value); |
347 |
|
|
348 |
|
AttributeSet addAttributes(AttributeSet old, AttributeSet attributes); |
349 |
|
|
350 |
|
AttributeSet getEmptySet(); |
351 |
|
|
352 |
|
void reclaim(AttributeSet attributes); |
353 |
|
|
354 |
|
AttributeSet removeAttribute(AttributeSet old, Object name); |
355 |
|
|
356 |
|
AttributeSet removeAttributes(AttributeSet old, AttributeSet attributes); |
357 |
|
|
358 |
|
AttributeSet removeAttributes(AttributeSet old, Enumeration names); |
359 |
|
} |
360 |
|
|
361 |
|
public interface Content |
362 |
|
{ |
363 |
|
Position createPosition(int offset) throws BadLocationException; |
364 |
|
|
365 |
|
int length(); |
366 |
|
|
367 |
|
UndoableEdit insertString(int where, String str) |
368 |
|
throws BadLocationException; |
369 |
|
|
370 |
|
UndoableEdit remove(int where, int nitems) throws BadLocationException; |
371 |
|
|
372 |
|
String getString(int where, int len) throws BadLocationException; |
373 |
|
|
374 |
|
void getChars(int where, int len, Segment txt) throws BadLocationException; |
375 |
|
} |
376 |
|
|
377 |
|
public abstract class AbstractElement |
378 |
|
implements Element, TreeNode, Serializable |
379 |
|
{ |
380 |
|
private static final long serialVersionUID = 1265312733007397733L; |
381 |
|
int count; |
382 |
|
int offset; |
383 |
|
AttributeSet attr; |
384 |
|
Vector elts = new Vector(); |
385 |
|
String name; |
386 |
|
Element parent; |
387 |
|
Vector kids = new Vector(); |
388 |
|
TreeNode tree_parent; |
389 |
|
|
390 |
|
public AbstractElement(Element p, AttributeSet s) |
391 |
|
{ |
392 |
|
parent = p; |
393 |
|
attr = s; |
394 |
|
} |
395 |
|
|
396 |
|
public Enumeration children() |
397 |
|
{ |
398 |
|
return kids.elements(); |
399 |
|
} |
400 |
|
|
401 |
|
public boolean getAllowsChildren() |
402 |
|
{ |
403 |
|
return true; |
404 |
|
} |
405 |
|
|
406 |
|
public TreeNode getChildAt(int index) |
407 |
|
{ |
408 |
|
return (TreeNode) kids.elementAt(index); |
409 |
|
} |
410 |
|
|
411 |
|
public int getChildCount() |
412 |
|
{ |
413 |
|
return kids.size(); |
414 |
|
} |
415 |
|
|
416 |
|
public int getIndex(TreeNode node) |
417 |
|
{ |
418 |
|
return kids.indexOf(node); |
419 |
|
} |
420 |
|
|
421 |
|
public TreeNode getParent() |
422 |
|
{ |
423 |
|
return tree_parent; |
424 |
|
} |
425 |
|
|
426 |
|
public AttributeSet getAttributes() |
427 |
|
{ |
428 |
|
return attr; |
429 |
|
} |
430 |
|
|
431 |
|
public Document getDocument() |
432 |
|
{ |
433 |
|
return AbstractDocument.this; |
434 |
|
} |
435 |
|
|
436 |
|
public Element getElement(int index) |
437 |
|
{ |
438 |
|
return (Element) elts.elementAt(index); |
439 |
|
} |
440 |
|
|
441 |
|
public String getName() |
442 |
|
{ |
443 |
|
return name; |
444 |
|
} |
445 |
|
|
446 |
|
public Element getParentElement() |
447 |
|
{ |
448 |
|
return parent; |
449 |
|
} |
450 |
|
|
451 |
|
public abstract boolean isLeaf(); |
452 |
|
|
453 |
|
public abstract int getEndOffset(); |
454 |
|
|
455 |
|
public abstract int getElementCount(); |
456 |
|
|
457 |
|
public abstract int getElementIndex(int offset); |
458 |
|
|
459 |
|
public abstract int getStartOffset(); |
460 |
|
} |
461 |
|
|
462 |
|
public class BranchElement extends AbstractElement |
463 |
|
{ |
464 |
|
private static final long serialVersionUID = -8595176318868717313L; |
465 |
|
private int start; |
466 |
|
private int end; |
467 |
|
private Vector children = new Vector(); |
468 |
|
|
469 |
|
public BranchElement(Element parent, AttributeSet attributes, int start, |
470 |
|
int end) |
471 |
|
{ |
472 |
|
super(parent, attributes); |
473 |
|
this.start = start; |
474 |
|
this.end = end; |
475 |
|
} |
476 |
|
|
477 |
|
public Enumeration children() |
478 |
|
{ |
479 |
|
return children.elements(); |
480 |
|
} |
481 |
|
|
482 |
|
public boolean getAllowsChildren() |
483 |
|
{ |
484 |
|
return true; |
485 |
|
} |
486 |
|
|
487 |
|
public Element getElement(int index) |
488 |
|
{ |
489 |
|
return (Element) children.get(index); |
490 |
|
} |
491 |
|
|
492 |
|
public int getElementCount() |
493 |
|
{ |
494 |
|
return children.size(); |
495 |
|
} |
496 |
|
|
497 |
|
public int getElementIndex(int offset) |
498 |
|
{ |
499 |
|
return children.indexOf(positionToElement(offset)); |
500 |
|
} |
501 |
|
|
502 |
|
public int getEndOffset() |
503 |
|
{ |
504 |
|
return end; |
505 |
|
} |
506 |
|
|
507 |
|
public String getName() |
508 |
|
{ |
509 |
|
return "AbstractDocument.BranchElement"; |
510 |
|
} |
511 |
|
|
512 |
|
public int getStartOffset() |
513 |
|
{ |
514 |
|
return start; |
515 |
|
} |
516 |
|
|
517 |
|
public boolean isLeaf() |
518 |
|
{ |
519 |
|
return false; |
520 |
|
} |
521 |
|
|
522 |
|
public Element positionToElement(int position) |
523 |
|
{ |
524 |
|
// XXX: There is surely a better algorithm |
525 |
|
// as beginning from first element each time. |
526 |
|
for (int index = 0; index < children.size(); ++index) |
527 |
|
{ |
528 |
|
Element elem = (Element) children.get(index); |
529 |
|
|
530 |
|
if ((elem.getStartOffset() <= position) |
531 |
|
&& (position < elem.getEndOffset())) |
532 |
|
return elem; |
533 |
|
} |
534 |
|
|
535 |
|
return null; |
536 |
|
} |
537 |
|
|
538 |
|
public void replace(int offset, int length, Element[] elems) |
539 |
|
{ |
540 |
|
for (int index = 0; index < length; ++index) |
541 |
|
children.removeElementAt(offset); |
542 |
|
|
543 |
|
for (int index = 0; index < elems.length; ++index) |
544 |
|
children.add(offset + index, elems[index]); |
545 |
|
} |
546 |
|
|
547 |
|
public String toString() |
548 |
|
{ |
549 |
|
return getName() + ": " + "content"; |
550 |
|
} |
551 |
|
} |
552 |
|
|
553 |
|
public class DefaultDocumentEvent extends CompoundEdit |
554 |
|
implements DocumentEvent |
555 |
|
{ |
556 |
|
private static final long serialVersionUID = -7406103236022413522L; |
557 |
|
public int len; |
558 |
|
public int off; |
559 |
|
|
560 |
|
public Document getDocument() |
561 |
|
{ |
562 |
|
return AbstractDocument.this; |
563 |
|
} |
564 |
|
|
565 |
|
public int getLength() |
566 |
|
{ |
567 |
|
return len; |
568 |
|
} |
569 |
|
|
570 |
|
public int getOffset() |
571 |
|
{ |
572 |
|
return off; |
573 |
|
} |
574 |
|
|
575 |
|
public DocumentEvent.EventType getType() |
576 |
|
{ |
577 |
|
return null; |
578 |
|
} |
579 |
|
|
580 |
|
public DocumentEvent.ElementChange getChange(Element elem) |
581 |
|
{ |
582 |
|
return null; |
583 |
|
} |
584 |
|
} |
585 |
|
|
586 |
|
public static class ElementEdit extends AbstractUndoableEdit |
587 |
|
{ |
588 |
|
private static final long serialVersionUID = -1216620962142928304L; |
589 |
|
} |
590 |
|
|
591 |
|
public class LeafElement extends AbstractElement |
592 |
|
{ |
593 |
|
private static final long serialVersionUID = 5115368706941283802L; |
594 |
|
private int start; |
595 |
|
private int end; |
596 |
|
|
597 |
|
public LeafElement(Element parent, AttributeSet attributes, int start, |
598 |
|
int end) |
599 |
|
{ |
600 |
|
super(parent, attributes); |
601 |
|
this.start = start; |
602 |
|
this.end = end; |
603 |
|
} |
604 |
|
|
605 |
|
public Enumeration children() |
606 |
|
{ |
607 |
|
return null; |
608 |
|
} |
609 |
|
|
610 |
|
public boolean getAllowsChildren() |
611 |
|
{ |
612 |
|
return false; |
613 |
|
} |
614 |
|
|
615 |
|
public Element getElement() |
616 |
|
{ |
617 |
|
return null; |
618 |
|
} |
619 |
|
|
620 |
|
public int getElementCount() |
621 |
|
{ |
622 |
|
return 0; |
623 |
|
} |
624 |
|
|
625 |
|
public int getElementIndex(int offset) |
626 |
|
{ |
627 |
|
return -1; |
628 |
|
} |
629 |
|
|
630 |
|
public int getEndOffset() |
631 |
|
{ |
632 |
|
return end; |
633 |
|
} |
634 |
|
|
635 |
|
public String getName() |
636 |
|
{ |
637 |
|
return "AbstractDocument.LeafElement"; |
638 |
|
} |
639 |
|
|
640 |
|
public int getStartOffset() |
641 |
|
{ |
642 |
|
return start; |
643 |
|
} |
644 |
|
|
645 |
|
public boolean isLeaf() |
646 |
|
{ |
647 |
|
return true; |
648 |
|
} |
649 |
|
|
650 |
|
public String toString() |
651 |
|
{ |
652 |
|
return getName() + ": " + "content"; |
653 |
|
} |
654 |
|
} |
655 |
} |
} |