libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / javax / swing / text / AbstractDocument.java
blob29b20b321fbf4da219bd6f196ca96a17d51b0f62
1 /* AbstractDocument.java --
2 Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.text;
41 import gnu.java.lang.CPStringBuilder;
43 import java.awt.font.TextAttribute;
44 import java.io.PrintStream;
45 import java.io.Serializable;
46 import java.text.Bidi;
47 import java.util.ArrayList;
48 import java.util.Dictionary;
49 import java.util.Enumeration;
50 import java.util.EventListener;
51 import java.util.HashMap;
52 import java.util.Hashtable;
53 import java.util.Vector;
55 import javax.swing.event.DocumentEvent;
56 import javax.swing.event.DocumentListener;
57 import javax.swing.event.EventListenerList;
58 import javax.swing.event.UndoableEditEvent;
59 import javax.swing.event.UndoableEditListener;
60 import javax.swing.text.DocumentFilter;
61 import javax.swing.tree.TreeNode;
62 import javax.swing.undo.AbstractUndoableEdit;
63 import javax.swing.undo.CompoundEdit;
64 import javax.swing.undo.UndoableEdit;
66 /**
67 * An abstract base implementation for the {@link Document} interface.
68 * This class provides some common functionality for all <code>Element</code>s,
69 * most notably it implements a locking mechanism to make document modification
70 * thread-safe.
72 * @author original author unknown
73 * @author Roman Kennke (roman@kennke.org)
75 public abstract class AbstractDocument implements Document, Serializable
77 /** The serialization UID (compatible with JDK1.5). */
78 private static final long serialVersionUID = 6842927725919637215L;
80 /**
81 * Standard error message to indicate a bad location.
83 protected static final String BAD_LOCATION = "document location failure";
85 /**
86 * Standard name for unidirectional <code>Element</code>s.
88 public static final String BidiElementName = "bidi level";
90 /**
91 * Standard name for content <code>Element</code>s. These are usually
92 * {@link LeafElement}s.
94 public static final String ContentElementName = "content";
96 /**
97 * Standard name for paragraph <code>Element</code>s. These are usually
98 * {@link BranchElement}s.
100 public static final String ParagraphElementName = "paragraph";
103 * Standard name for section <code>Element</code>s. These are usually
104 * {@link DefaultStyledDocument.SectionElement}s.
106 public static final String SectionElementName = "section";
109 * Attribute key for storing the element name.
111 public static final String ElementNameAttribute = "$ename";
114 * Standard name for the bidi root element.
116 private static final String BidiRootName = "bidi root";
119 * Key for storing the asynchronous load priority.
121 private static final String AsyncLoadPriority = "load priority";
124 * Key for storing the I18N state.
126 private static final String I18N = "i18n";
129 * The actual content model of this <code>Document</code>.
131 Content content;
134 * The AttributeContext for this <code>Document</code>.
136 AttributeContext context;
139 * The currently installed <code>DocumentFilter</code>.
141 DocumentFilter documentFilter;
144 * The documents properties.
146 Dictionary properties;
149 * Manages event listeners for this <code>Document</code>.
151 protected EventListenerList listenerList = new EventListenerList();
154 * Stores the current writer thread. Used for locking.
156 private Thread currentWriter = null;
159 * The number of readers. Used for locking.
161 private int numReaders = 0;
164 * The number of current writers. If this is > 1 then the same thread entered
165 * the write lock more than once.
167 private int numWriters = 0;
169 /** An instance of a DocumentFilter.FilterBypass which allows calling
170 * the insert, remove and replace method without checking for an installed
171 * document filter.
173 private DocumentFilter.FilterBypass bypass;
176 * The bidi root element.
178 private BidiRootElement bidiRoot;
181 * True when we are currently notifying any listeners. This is used
182 * to detect illegal situations in writeLock().
184 private transient boolean notifyListeners;
187 * Creates a new <code>AbstractDocument</code> with the specified
188 * {@link Content} model.
190 * @param doc the <code>Content</code> model to be used in this
191 * <code>Document<code>
193 * @see GapContent
194 * @see StringContent
196 protected AbstractDocument(Content doc)
198 this(doc, StyleContext.getDefaultStyleContext());
202 * Creates a new <code>AbstractDocument</code> with the specified
203 * {@link Content} model and {@link AttributeContext}.
205 * @param doc the <code>Content</code> model to be used in this
206 * <code>Document<code>
207 * @param ctx the <code>AttributeContext</code> to use
209 * @see GapContent
210 * @see StringContent
212 protected AbstractDocument(Content doc, AttributeContext ctx)
214 content = doc;
215 context = ctx;
217 // FIXME: Fully implement bidi.
218 bidiRoot = new BidiRootElement();
220 // FIXME: This is determined using a Mauve test. Make the document
221 // actually use this.
222 putProperty(I18N, Boolean.FALSE);
224 // Add one child to the bidi root.
225 writeLock();
228 Element[] children = new Element[1];
229 children[0] = new BidiElement(bidiRoot, 0, 1, 0);
230 bidiRoot.replace(0, 0, children);
232 finally
234 writeUnlock();
238 /** Returns the DocumentFilter.FilterBypass instance for this
239 * document and create it if it does not exist yet.
241 * @return This document's DocumentFilter.FilterBypass instance.
243 private DocumentFilter.FilterBypass getBypass()
245 if (bypass == null)
246 bypass = new Bypass();
248 return bypass;
252 * Returns the paragraph {@link Element} that holds the specified position.
254 * @param pos the position for which to get the paragraph element
256 * @return the paragraph {@link Element} that holds the specified position
258 public abstract Element getParagraphElement(int pos);
261 * Returns the default root {@link Element} of this <code>Document</code>.
262 * Usual <code>Document</code>s only have one root element and return this.
263 * However, there may be <code>Document</code> implementations that
264 * support multiple root elements, they have to return a default root element
265 * here.
267 * @return the default root {@link Element} of this <code>Document</code>
269 public abstract Element getDefaultRootElement();
272 * Creates and returns a branch element with the specified
273 * <code>parent</code> and <code>attributes</code>. Note that the new
274 * <code>Element</code> is linked to the parent <code>Element</code>
275 * through {@link Element#getParentElement}, but it is not yet added
276 * to the parent <code>Element</code> as child.
278 * @param parent the parent <code>Element</code> for the new branch element
279 * @param attributes the text attributes to be installed in the new element
281 * @return the new branch <code>Element</code>
283 * @see BranchElement
285 protected Element createBranchElement(Element parent,
286 AttributeSet attributes)
288 return new BranchElement(parent, attributes);
292 * Creates and returns a leaf element with the specified
293 * <code>parent</code> and <code>attributes</code>. Note that the new
294 * <code>Element</code> is linked to the parent <code>Element</code>
295 * through {@link Element#getParentElement}, but it is not yet added
296 * to the parent <code>Element</code> as child.
298 * @param parent the parent <code>Element</code> for the new branch element
299 * @param attributes the text attributes to be installed in the new element
301 * @return the new branch <code>Element</code>
303 * @see LeafElement
305 protected Element createLeafElement(Element parent, AttributeSet attributes,
306 int start, int end)
308 return new LeafElement(parent, attributes, start, end);
312 * Creates a {@link Position} that keeps track of the location at the
313 * specified <code>offset</code>.
315 * @param offset the location in the document to keep track by the new
316 * <code>Position</code>
318 * @return the newly created <code>Position</code>
320 * @throws BadLocationException if <code>offset</code> is not a valid
321 * location in the documents content model
323 public synchronized Position createPosition(final int offset)
324 throws BadLocationException
326 return content.createPosition(offset);
330 * Notifies all registered listeners when the document model changes.
332 * @param event the <code>DocumentEvent</code> to be fired
334 protected void fireChangedUpdate(DocumentEvent event)
336 notifyListeners = true;
339 DocumentListener[] listeners = getDocumentListeners();
340 for (int index = 0; index < listeners.length; ++index)
341 listeners[index].changedUpdate(event);
343 finally
345 notifyListeners = false;
350 * Notifies all registered listeners when content is inserted in the document
351 * model.
353 * @param event the <code>DocumentEvent</code> to be fired
355 protected void fireInsertUpdate(DocumentEvent event)
357 notifyListeners = true;
360 DocumentListener[] listeners = getDocumentListeners();
361 for (int index = 0; index < listeners.length; ++index)
362 listeners[index].insertUpdate(event);
364 finally
366 notifyListeners = false;
371 * Notifies all registered listeners when content is removed from the
372 * document model.
374 * @param event the <code>DocumentEvent</code> to be fired
376 protected void fireRemoveUpdate(DocumentEvent event)
378 notifyListeners = true;
381 DocumentListener[] listeners = getDocumentListeners();
382 for (int index = 0; index < listeners.length; ++index)
383 listeners[index].removeUpdate(event);
385 finally
387 notifyListeners = false;
392 * Notifies all registered listeners when an <code>UndoableEdit</code> has
393 * been performed on this <code>Document</code>.
395 * @param event the <code>UndoableEditEvent</code> to be fired
397 protected void fireUndoableEditUpdate(UndoableEditEvent event)
399 UndoableEditListener[] listeners = getUndoableEditListeners();
401 for (int index = 0; index < listeners.length; ++index)
402 listeners[index].undoableEditHappened(event);
406 * Returns the asynchronous loading priority. Returns <code>-1</code> if this
407 * document should not be loaded asynchronously.
409 * @return the asynchronous loading priority
411 public int getAsynchronousLoadPriority()
413 Object val = getProperty(AsyncLoadPriority);
414 int prio = -1;
415 if (val != null)
416 prio = ((Integer) val).intValue();
417 return prio;
421 * Returns the {@link AttributeContext} used in this <code>Document</code>.
423 * @return the {@link AttributeContext} used in this <code>Document</code>
425 protected final AttributeContext getAttributeContext()
427 return context;
431 * Returns the root element for bidirectional content.
433 * @return the root element for bidirectional content
435 public Element getBidiRootElement()
437 return bidiRoot;
441 * Returns the {@link Content} model for this <code>Document</code>
443 * @return the {@link Content} model for this <code>Document</code>
445 * @see GapContent
446 * @see StringContent
448 protected final Content getContent()
450 return content;
454 * Returns the thread that currently modifies this <code>Document</code>
455 * if there is one, otherwise <code>null</code>. This can be used to
456 * distinguish between a method call that is part of an ongoing modification
457 * or if it is a separate modification for which a new lock must be aquired.
459 * @return the thread that currently modifies this <code>Document</code>
460 * if there is one, otherwise <code>null</code>
462 protected final synchronized Thread getCurrentWriter()
464 return currentWriter;
468 * Returns the properties of this <code>Document</code>.
470 * @return the properties of this <code>Document</code>
472 public Dictionary<Object, Object> getDocumentProperties()
474 // FIXME: make me thread-safe
475 if (properties == null)
476 properties = new Hashtable();
478 return properties;
482 * Returns a {@link Position} which will always mark the end of the
483 * <code>Document</code>.
485 * @return a {@link Position} which will always mark the end of the
486 * <code>Document</code>
488 public final Position getEndPosition()
490 Position p;
493 p = createPosition(content.length());
495 catch (BadLocationException ex)
497 // Shouldn't really happen.
498 p = null;
500 return p;
504 * Returns the length of this <code>Document</code>'s content.
506 * @return the length of this <code>Document</code>'s content
508 public int getLength()
510 // We return Content.getLength() -1 here because there is always an
511 // implicit \n at the end of the Content which does count in Content
512 // but not in Document.
513 return content.length() - 1;
517 * Returns all registered listeners of a given listener type.
519 * @param listenerType the type of the listeners to be queried
521 * @return all registered listeners of the specified type
523 public <T extends EventListener> T[] getListeners(Class<T> listenerType)
525 return listenerList.getListeners(listenerType);
529 * Returns a property from this <code>Document</code>'s property list.
531 * @param key the key of the property to be fetched
533 * @return the property for <code>key</code> or <code>null</code> if there
534 * is no such property stored
536 public final Object getProperty(Object key)
538 // FIXME: make me thread-safe
539 Object value = null;
540 if (properties != null)
541 value = properties.get(key);
543 return value;
547 * Returns all root elements of this <code>Document</code>. By default
548 * this just returns the single root element returned by
549 * {@link #getDefaultRootElement()}. <code>Document</code> implementations
550 * that support multiple roots must override this method and return all roots
551 * here.
553 * @return all root elements of this <code>Document</code>
555 public Element[] getRootElements()
557 Element[] elements = new Element[2];
558 elements[0] = getDefaultRootElement();
559 elements[1] = getBidiRootElement();
560 return elements;
564 * Returns a {@link Position} which will always mark the beginning of the
565 * <code>Document</code>.
567 * @return a {@link Position} which will always mark the beginning of the
568 * <code>Document</code>
570 public final Position getStartPosition()
572 Position p;
575 p = createPosition(0);
577 catch (BadLocationException ex)
579 // Shouldn't really happen.
580 p = null;
582 return p;
586 * Returns a piece of this <code>Document</code>'s content.
588 * @param offset the start offset of the content
589 * @param length the length of the content
591 * @return the piece of content specified by <code>offset</code> and
592 * <code>length</code>
594 * @throws BadLocationException if <code>offset</code> or <code>offset +
595 * length</code> are invalid locations with this
596 * <code>Document</code>
598 public String getText(int offset, int length) throws BadLocationException
600 return content.getString(offset, length);
604 * Fetches a piece of this <code>Document</code>'s content and stores
605 * it in the given {@link Segment}.
607 * @param offset the start offset of the content
608 * @param length the length of the content
609 * @param segment the <code>Segment</code> to store the content in
611 * @throws BadLocationException if <code>offset</code> or <code>offset +
612 * length</code> are invalid locations with this
613 * <code>Document</code>
615 public void getText(int offset, int length, Segment segment)
616 throws BadLocationException
618 content.getChars(offset, length, segment);
622 * Inserts a String into this <code>Document</code> at the specified
623 * position and assigning the specified attributes to it.
625 * <p>If a {@link DocumentFilter} is installed in this document, the
626 * corresponding method of the filter object is called.</p>
628 * <p>The method has no effect when <code>text</code> is <code>null</code>
629 * or has a length of zero.</p>
632 * @param offset the location at which the string should be inserted
633 * @param text the content to be inserted
634 * @param attributes the text attributes to be assigned to that string
636 * @throws BadLocationException if <code>offset</code> is not a valid
637 * location in this <code>Document</code>
639 public void insertString(int offset, String text, AttributeSet attributes)
640 throws BadLocationException
642 // Bail out if we have a bogus insertion (Behavior observed in RI).
643 if (text == null || text.length() == 0)
644 return;
646 writeLock();
649 if (documentFilter == null)
650 insertStringImpl(offset, text, attributes);
651 else
652 documentFilter.insertString(getBypass(), offset, text, attributes);
654 finally
656 writeUnlock();
660 void insertStringImpl(int offset, String text, AttributeSet attributes)
661 throws BadLocationException
663 // Just return when no text to insert was given.
664 if (text == null || text.length() == 0)
665 return;
666 DefaultDocumentEvent event =
667 new DefaultDocumentEvent(offset, text.length(),
668 DocumentEvent.EventType.INSERT);
670 UndoableEdit undo = content.insertString(offset, text);
671 if (undo != null)
672 event.addEdit(undo);
674 // Check if we need bidi layout.
675 if (getProperty(I18N).equals(Boolean.FALSE))
677 Object dir = getProperty(TextAttribute.RUN_DIRECTION);
678 if (TextAttribute.RUN_DIRECTION_RTL.equals(dir))
679 putProperty(I18N, Boolean.TRUE);
680 else
682 char[] chars = text.toCharArray();
683 if (Bidi.requiresBidi(chars, 0, chars.length))
684 putProperty(I18N, Boolean.TRUE);
688 insertUpdate(event, attributes);
690 fireInsertUpdate(event);
692 if (undo != null)
693 fireUndoableEditUpdate(new UndoableEditEvent(this, undo));
697 * Called to indicate that text has been inserted into this
698 * <code>Document</code>. The default implementation does nothing.
699 * This method is executed within a write lock.
701 * @param chng the <code>DefaultDocumentEvent</code> describing the change
702 * @param attr the attributes of the changed content
704 protected void insertUpdate(DefaultDocumentEvent chng, AttributeSet attr)
706 if (Boolean.TRUE.equals(getProperty(I18N)))
707 updateBidi(chng);
711 * Called after some content has been removed from this
712 * <code>Document</code>. The default implementation does nothing.
713 * This method is executed within a write lock.
715 * @param chng the <code>DefaultDocumentEvent</code> describing the change
717 protected void postRemoveUpdate(DefaultDocumentEvent chng)
719 if (Boolean.TRUE.equals(getProperty(I18N)))
720 updateBidi(chng);
724 * Stores a property in this <code>Document</code>'s property list.
726 * @param key the key of the property to be stored
727 * @param value the value of the property to be stored
729 public final void putProperty(Object key, Object value)
731 // FIXME: make me thread-safe
732 if (properties == null)
733 properties = new Hashtable();
735 if (value == null)
736 properties.remove(key);
737 else
738 properties.put(key, value);
740 // Update bidi structure if the RUN_DIRECTION is set.
741 if (TextAttribute.RUN_DIRECTION.equals(key))
743 if (TextAttribute.RUN_DIRECTION_RTL.equals(value)
744 && Boolean.FALSE.equals(getProperty(I18N)))
745 putProperty(I18N, Boolean.TRUE);
747 if (Boolean.TRUE.equals(getProperty(I18N)))
749 writeLock();
752 DefaultDocumentEvent ev =
753 new DefaultDocumentEvent(0, getLength(),
754 DocumentEvent.EventType.INSERT);
755 updateBidi(ev);
757 finally
759 writeUnlock();
766 * Updates the bidi element structure.
768 * @param ev the document event for the change
770 private void updateBidi(DefaultDocumentEvent ev)
772 // Determine start and end offset of the paragraphs to be scanned.
773 int start = 0;
774 int end = 0;
775 DocumentEvent.EventType type = ev.getType();
776 if (type == DocumentEvent.EventType.INSERT
777 || type == DocumentEvent.EventType.CHANGE)
779 int offs = ev.getOffset();
780 int endOffs = offs + ev.getLength();
781 start = getParagraphElement(offs).getStartOffset();
782 end = getParagraphElement(endOffs).getEndOffset();
784 else if (type == DocumentEvent.EventType.REMOVE)
786 Element par = getParagraphElement(ev.getOffset());
787 start = par.getStartOffset();
788 end = par.getEndOffset();
790 else
791 assert false : "Unknown event type";
793 // Determine the bidi levels for the affected range.
794 Bidi[] bidis = getBidis(start, end);
796 int removeFrom = 0;
797 int removeTo = 0;
799 int offs = 0;
800 int lastRunStart = 0;
801 int lastRunEnd = 0;
802 int lastRunLevel = 0;
803 ArrayList newEls = new ArrayList();
804 for (int i = 0; i < bidis.length; i++)
806 Bidi bidi = bidis[i];
807 int numRuns = bidi.getRunCount();
808 for (int r = 0; r < numRuns; r++)
810 if (r == 0 && i == 0)
812 if (start > 0)
814 // Try to merge with the previous element if it has the
815 // same bidi level as the first run.
816 int prevElIndex = bidiRoot.getElementIndex(start - 1);
817 removeFrom = prevElIndex;
818 Element prevEl = bidiRoot.getElement(prevElIndex);
819 AttributeSet atts = prevEl.getAttributes();
820 int prevElLevel = StyleConstants.getBidiLevel(atts);
821 if (prevElLevel == bidi.getRunLevel(r))
823 // Merge previous element with current run.
824 lastRunStart = prevEl.getStartOffset() - start;
825 lastRunEnd = bidi.getRunLimit(r);
826 lastRunLevel = bidi.getRunLevel(r);
828 else if (prevEl.getEndOffset() > start)
830 // Split previous element and replace by 2 new elements.
831 lastRunStart = 0;
832 lastRunEnd = bidi.getRunLimit(r);
833 lastRunLevel = bidi.getRunLevel(r);
834 newEls.add(new BidiElement(bidiRoot,
835 prevEl.getStartOffset(),
836 start, prevElLevel));
838 else
840 // Simply start new run at start location.
841 lastRunStart = 0;
842 lastRunEnd = bidi.getRunLimit(r);
843 lastRunLevel = bidi.getRunLevel(r);
844 removeFrom++;
847 else
849 // Simply start new run at start location.
850 lastRunStart = 0;
851 lastRunEnd = bidi.getRunLimit(r);
852 lastRunLevel = bidi.getRunLevel(r);
853 removeFrom = 0;
856 if (i == bidis.length - 1 && r == numRuns - 1)
858 if (end <= getLength())
860 // Try to merge last element with next element.
861 int nextIndex = bidiRoot.getElementIndex(end);
862 Element nextEl = bidiRoot.getElement(nextIndex);
863 AttributeSet atts = nextEl.getAttributes();
864 int nextLevel = StyleConstants.getBidiLevel(atts);
865 int level = bidi.getRunLevel(r);
866 if (lastRunLevel == level && level == nextLevel)
868 // Merge runs together.
869 if (lastRunStart + start == nextEl.getStartOffset())
870 removeTo = nextIndex - 1;
871 else
873 newEls.add(new BidiElement(bidiRoot, start + lastRunStart,
874 nextEl.getEndOffset(), level));
875 removeTo = nextIndex;
878 else if (lastRunLevel == level)
880 // Merge current and last run.
881 int endOffs = offs + bidi.getRunLimit(r);
882 newEls.add(new BidiElement(bidiRoot, start + lastRunStart,
883 start + endOffs, level));
884 if (start + endOffs == nextEl.getStartOffset())
885 removeTo = nextIndex - 1;
886 else
888 newEls.add(new BidiElement(bidiRoot, start + endOffs,
889 nextEl.getEndOffset(),
890 nextLevel));
891 removeTo = nextIndex;
894 else if (level == nextLevel)
896 // Merge current and next run.
897 newEls.add(new BidiElement(bidiRoot, start + lastRunStart,
898 start + lastRunEnd,
899 lastRunLevel));
900 newEls.add(new BidiElement(bidiRoot, start + lastRunEnd,
901 nextEl.getEndOffset(), level));
902 removeTo = nextIndex;
904 else
906 // Split next element.
907 int endOffs = offs + bidi.getRunLimit(r);
908 newEls.add(new BidiElement(bidiRoot, start + lastRunStart,
909 start + lastRunEnd,
910 lastRunLevel));
911 newEls.add(new BidiElement(bidiRoot, start + lastRunEnd,
912 start + endOffs, level));
913 newEls.add(new BidiElement(bidiRoot, start + endOffs,
914 nextEl.getEndOffset(),
915 nextLevel));
916 removeTo = nextIndex;
919 else
921 removeTo = bidiRoot.getElementIndex(end);
922 int level = bidi.getRunLevel(r);
923 int runEnd = offs + bidi.getRunLimit(r);
925 if (level == lastRunLevel)
927 // Merge with previous.
928 lastRunEnd = offs + runEnd;
929 newEls.add(new BidiElement(bidiRoot,
930 start + lastRunStart,
931 start + runEnd, level));
933 else
935 // Create element for last run and current run.
936 newEls.add(new BidiElement(bidiRoot, start + lastRunStart,
937 start + lastRunEnd,
938 lastRunLevel));
939 newEls.add(new BidiElement(bidiRoot,
940 start + lastRunEnd,
941 start + runEnd,
942 level));
946 else
948 int level = bidi.getRunLevel(r);
949 int runEnd = bidi.getRunLimit(r);
951 if (level == lastRunLevel)
953 // Merge with previous.
954 lastRunEnd = offs + runEnd;
956 else
958 // Create element for last run and update values for
959 // current run.
960 newEls.add(new BidiElement(bidiRoot, start + lastRunStart,
961 start + lastRunEnd,
962 lastRunLevel));
963 lastRunStart = lastRunEnd;
964 lastRunEnd = offs + runEnd;
965 lastRunLevel = level;
969 offs += bidi.getLength();
972 // Determine the bidi elements which are to be removed.
973 int numRemoved = 0;
974 if (bidiRoot.getElementCount() > 0)
975 numRemoved = removeTo - removeFrom + 1;
976 Element[] removed = new Element[numRemoved];
977 for (int i = 0; i < numRemoved; i++)
978 removed[i] = bidiRoot.getElement(removeFrom + i);
980 Element[] added = new Element[newEls.size()];
981 added = (Element[]) newEls.toArray(added);
983 // Update the event.
984 ElementEdit edit = new ElementEdit(bidiRoot, removeFrom, removed, added);
985 ev.addEdit(edit);
987 // Update the structure.
988 bidiRoot.replace(removeFrom, numRemoved, added);
992 * Determines the Bidi objects for the paragraphs in the specified range.
994 * @param start the start of the range
995 * @param end the end of the range
997 * @return the Bidi analysers for the paragraphs in the range
999 private Bidi[] getBidis(int start, int end)
1001 // Determine the default run direction from the document property.
1002 Boolean defaultDir = null;
1003 Object o = getProperty(TextAttribute.RUN_DIRECTION);
1004 if (o instanceof Boolean)
1005 defaultDir = (Boolean) o;
1007 // Scan paragraphs and add their level arrays to the overall levels array.
1008 ArrayList bidis = new ArrayList();
1009 Segment s = new Segment();
1010 for (int i = start; i < end;)
1012 Element par = getParagraphElement(i);
1013 int pStart = par.getStartOffset();
1014 int pEnd = par.getEndOffset();
1016 // Determine the default run direction of the paragraph.
1017 Boolean dir = defaultDir;
1018 o = par.getAttributes().getAttribute(TextAttribute.RUN_DIRECTION);
1019 if (o instanceof Boolean)
1020 dir = (Boolean) o;
1022 // Bidi over the paragraph.
1025 getText(pStart, pEnd - pStart, s);
1027 catch (BadLocationException ex)
1029 assert false : "Must not happen";
1031 int flag = Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT;
1032 if (dir != null)
1034 if (TextAttribute.RUN_DIRECTION_LTR.equals(dir))
1035 flag = Bidi.DIRECTION_LEFT_TO_RIGHT;
1036 else
1037 flag = Bidi.DIRECTION_RIGHT_TO_LEFT;
1039 Bidi bidi = new Bidi(s.array, s.offset, null, 0, s.count, flag);
1040 bidis.add(bidi);
1041 i = pEnd;
1043 Bidi[] ret = new Bidi[bidis.size()];
1044 ret = (Bidi[]) bidis.toArray(ret);
1045 return ret;
1049 * Blocks until a read lock can be obtained. Must block if there is
1050 * currently a writer modifying the <code>Document</code>.
1052 public final synchronized void readLock()
1056 while (currentWriter != null)
1058 if (currentWriter == Thread.currentThread())
1059 return;
1060 wait();
1062 numReaders++;
1064 catch (InterruptedException ex)
1066 throw new Error("Interrupted during grab read lock");
1071 * Releases the read lock. If this was the only reader on this
1072 * <code>Document</code>, writing may begin now.
1074 public final synchronized void readUnlock()
1076 // Note we could have a problem here if readUnlock was called without a
1077 // prior call to readLock but the specs simply warn users to ensure that
1078 // balance by using a finally block:
1079 // readLock()
1080 // try
1081 // {
1082 // doSomethingHere
1083 // }
1084 // finally
1085 // {
1086 // readUnlock();
1087 // }
1089 // All that the JDK seems to check for is that you don't call unlock
1090 // more times than you've previously called lock, but it doesn't make
1091 // sure that the threads calling unlock were the same ones that called lock
1093 // If the current thread holds the write lock, and attempted to also obtain
1094 // a readLock, then numReaders hasn't been incremented and we don't need
1095 // to unlock it here.
1096 if (currentWriter == Thread.currentThread())
1097 return;
1099 // FIXME: the reference implementation throws a
1100 // javax.swing.text.StateInvariantError here
1101 if (numReaders <= 0)
1102 throw new IllegalStateException("document lock failure");
1104 // If currentWriter is not null, the application code probably had a
1105 // writeLock and then tried to obtain a readLock, in which case
1106 // numReaders wasn't incremented
1107 numReaders--;
1108 notify();
1112 * Removes a piece of content from this <code>Document</code>.
1114 * <p>If a {@link DocumentFilter} is installed in this document, the
1115 * corresponding method of the filter object is called. The
1116 * <code>DocumentFilter</code> is called even if <code>length</code>
1117 * is zero. This is different from {@link #replace}.</p>
1119 * <p>Note: When <code>length</code> is zero or below the call is not
1120 * forwarded to the underlying {@link AbstractDocument.Content} instance
1121 * of this document and no exception is thrown.</p>
1123 * @param offset the start offset of the fragment to be removed
1124 * @param length the length of the fragment to be removed
1126 * @throws BadLocationException if <code>offset</code> or
1127 * <code>offset + length</code> or invalid locations within this
1128 * document
1130 public void remove(int offset, int length) throws BadLocationException
1132 writeLock();
1135 DocumentFilter f = getDocumentFilter();
1136 if (f == null)
1137 removeImpl(offset, length);
1138 else
1139 f.remove(getBypass(), offset, length);
1141 finally
1143 writeUnlock();
1147 void removeImpl(int offset, int length) throws BadLocationException
1149 // The RI silently ignores all requests that have a negative length.
1150 // Don't ask my why, but that's how it is.
1151 if (length > 0)
1153 if (offset < 0 || offset > getLength())
1154 throw new BadLocationException("Invalid remove position", offset);
1156 if (offset + length > getLength())
1157 throw new BadLocationException("Invalid remove length", offset);
1159 DefaultDocumentEvent event =
1160 new DefaultDocumentEvent(offset, length,
1161 DocumentEvent.EventType.REMOVE);
1163 // The order of the operations below is critical!
1164 removeUpdate(event);
1165 UndoableEdit temp = content.remove(offset, length);
1167 postRemoveUpdate(event);
1168 fireRemoveUpdate(event);
1173 * Replaces a piece of content in this <code>Document</code> with
1174 * another piece of content.
1176 * <p>If a {@link DocumentFilter} is installed in this document, the
1177 * corresponding method of the filter object is called.</p>
1179 * <p>The method has no effect if <code>length</code> is zero (and
1180 * only zero) and, at the same time, <code>text</code> is
1181 * <code>null</code> or has zero length.</p>
1183 * @param offset the start offset of the fragment to be removed
1184 * @param length the length of the fragment to be removed
1185 * @param text the text to replace the content with
1186 * @param attributes the text attributes to assign to the new content
1188 * @throws BadLocationException if <code>offset</code> or
1189 * <code>offset + length</code> or invalid locations within this
1190 * document
1192 * @since 1.4
1194 public void replace(int offset, int length, String text,
1195 AttributeSet attributes)
1196 throws BadLocationException
1198 // Bail out if we have a bogus replacement (Behavior observed in RI).
1199 if (length == 0
1200 && (text == null || text.length() == 0))
1201 return;
1203 writeLock();
1206 if (documentFilter == null)
1208 // It is important to call the methods which again do the checks
1209 // of the arguments and the DocumentFilter because subclasses may
1210 // have overridden these methods and provide crucial behavior
1211 // which would be skipped if we call the non-checking variants.
1212 // An example for this is PlainDocument where insertString can
1213 // provide a filtering of newlines.
1214 remove(offset, length);
1215 insertString(offset, text, attributes);
1217 else
1218 documentFilter.replace(getBypass(), offset, length, text, attributes);
1220 finally
1222 writeUnlock();
1226 void replaceImpl(int offset, int length, String text,
1227 AttributeSet attributes)
1228 throws BadLocationException
1230 removeImpl(offset, length);
1231 insertStringImpl(offset, text, attributes);
1235 * Adds a <code>DocumentListener</code> object to this document.
1237 * @param listener the listener to add
1239 public void addDocumentListener(DocumentListener listener)
1241 listenerList.add(DocumentListener.class, listener);
1245 * Removes a <code>DocumentListener</code> object from this document.
1247 * @param listener the listener to remove
1249 public void removeDocumentListener(DocumentListener listener)
1251 listenerList.remove(DocumentListener.class, listener);
1255 * Returns all registered <code>DocumentListener</code>s.
1257 * @return all registered <code>DocumentListener</code>s
1259 public DocumentListener[] getDocumentListeners()
1261 return (DocumentListener[]) getListeners(DocumentListener.class);
1265 * Adds an {@link UndoableEditListener} to this <code>Document</code>.
1267 * @param listener the listener to add
1269 public void addUndoableEditListener(UndoableEditListener listener)
1271 listenerList.add(UndoableEditListener.class, listener);
1275 * Removes an {@link UndoableEditListener} from this <code>Document</code>.
1277 * @param listener the listener to remove
1279 public void removeUndoableEditListener(UndoableEditListener listener)
1281 listenerList.remove(UndoableEditListener.class, listener);
1285 * Returns all registered {@link UndoableEditListener}s.
1287 * @return all registered {@link UndoableEditListener}s
1289 public UndoableEditListener[] getUndoableEditListeners()
1291 return (UndoableEditListener[]) getListeners(UndoableEditListener.class);
1295 * Called before some content gets removed from this <code>Document</code>.
1296 * The default implementation does nothing but may be overridden by
1297 * subclasses to modify the <code>Document</code> structure in response
1298 * to a remove request. The method is executed within a write lock.
1300 * @param chng the <code>DefaultDocumentEvent</code> describing the change
1302 protected void removeUpdate(DefaultDocumentEvent chng)
1304 // Do nothing here. Subclasses may wish to override this.
1308 * Called to render this <code>Document</code> visually. It obtains a read
1309 * lock, ensuring that no changes will be made to the <code>document</code>
1310 * during the rendering process. It then calls the {@link Runnable#run()}
1311 * method on <code>runnable</code>. This method <em>must not</em> attempt
1312 * to modifiy the <code>Document</code>, since a deadlock will occur if it
1313 * tries to obtain a write lock. When the {@link Runnable#run()} method
1314 * completes (either naturally or by throwing an exception), the read lock
1315 * is released. Note that there is nothing in this method related to
1316 * the actual rendering. It could be used to execute arbitrary code within
1317 * a read lock.
1319 * @param runnable the {@link Runnable} to execute
1321 public void render(Runnable runnable)
1323 readLock();
1326 runnable.run();
1328 finally
1330 readUnlock();
1335 * Sets the asynchronous loading priority for this <code>Document</code>.
1336 * A value of <code>-1</code> indicates that this <code>Document</code>
1337 * should be loaded synchronously.
1339 * @param p the asynchronous loading priority to set
1341 public void setAsynchronousLoadPriority(int p)
1343 Integer val = p >= 0 ? new Integer(p) : null;
1344 putProperty(AsyncLoadPriority, val);
1348 * Sets the properties of this <code>Document</code>.
1350 * @param p the document properties to set
1352 public void setDocumentProperties(Dictionary<Object, Object> p)
1354 // FIXME: make me thread-safe
1355 properties = p;
1359 * Blocks until a write lock can be obtained. Must wait if there are
1360 * readers currently reading or another thread is currently writing.
1362 protected synchronized final void writeLock()
1366 while (numReaders > 0 || currentWriter != null)
1368 if (Thread.currentThread() == currentWriter)
1370 if (notifyListeners)
1371 throw new IllegalStateException("Mutation during notify");
1372 numWriters++;
1373 return;
1375 wait();
1377 currentWriter = Thread.currentThread();
1378 numWriters = 1;
1380 catch (InterruptedException ex)
1382 throw new Error("Interupted during grab write lock");
1387 * Releases the write lock. This allows waiting readers or writers to
1388 * obtain the lock.
1390 protected final synchronized void writeUnlock()
1392 if (--numWriters <= 0)
1394 numWriters = 0;
1395 currentWriter = null;
1396 notifyAll();
1401 * Returns the currently installed {@link DocumentFilter} for this
1402 * <code>Document</code>.
1404 * @return the currently installed {@link DocumentFilter} for this
1405 * <code>Document</code>
1407 * @since 1.4
1409 public DocumentFilter getDocumentFilter()
1411 return documentFilter;
1415 * Sets the {@link DocumentFilter} for this <code>Document</code>.
1417 * @param filter the <code>DocumentFilter</code> to set
1419 * @since 1.4
1421 public void setDocumentFilter(DocumentFilter filter)
1423 this.documentFilter = filter;
1427 * Dumps diagnostic information to the specified <code>PrintStream</code>.
1429 * @param out the stream to write the diagnostic information to
1431 public void dump(PrintStream out)
1433 ((AbstractElement) getDefaultRootElement()).dump(out, 0);
1434 ((AbstractElement) getBidiRootElement()).dump(out, 0);
1438 * Defines a set of methods for managing text attributes for one or more
1439 * <code>Document</code>s.
1441 * Replicating {@link AttributeSet}s throughout a <code>Document</code> can
1442 * be very expensive. Implementations of this interface are intended to
1443 * provide intelligent management of <code>AttributeSet</code>s, eliminating
1444 * costly duplication.
1446 * @see StyleContext
1448 public interface AttributeContext
1451 * Returns an {@link AttributeSet} that contains the attributes
1452 * of <code>old</code> plus the new attribute specified by
1453 * <code>name</code> and <code>value</code>.
1455 * @param old the attribute set to be merged with the new attribute
1456 * @param name the name of the attribute to be added
1457 * @param value the value of the attribute to be added
1459 * @return the old attributes plus the new attribute
1461 AttributeSet addAttribute(AttributeSet old, Object name, Object value);
1464 * Returns an {@link AttributeSet} that contains the attributes
1465 * of <code>old</code> plus the new attributes in <code>attributes</code>.
1467 * @param old the set of attributes where to add the new attributes
1468 * @param attributes the attributes to be added
1470 * @return an {@link AttributeSet} that contains the attributes
1471 * of <code>old</code> plus the new attributes in
1472 * <code>attributes</code>
1474 AttributeSet addAttributes(AttributeSet old, AttributeSet attributes);
1477 * Returns an empty {@link AttributeSet}.
1479 * @return an empty {@link AttributeSet}
1481 AttributeSet getEmptySet();
1484 * Called to indicate that the attributes in <code>attributes</code> are
1485 * no longer used.
1487 * @param attributes the attributes are no longer used
1489 void reclaim(AttributeSet attributes);
1492 * Returns a {@link AttributeSet} that has the attribute with the specified
1493 * <code>name</code> removed from <code>old</code>.
1495 * @param old the attribute set from which an attribute is removed
1496 * @param name the name of the attribute to be removed
1498 * @return the attributes of <code>old</code> minus the attribute
1499 * specified by <code>name</code>
1501 AttributeSet removeAttribute(AttributeSet old, Object name);
1504 * Removes all attributes in <code>attributes</code> from <code>old</code>
1505 * and returns the resulting <code>AttributeSet</code>.
1507 * @param old the set of attributes from which to remove attributes
1508 * @param attributes the attributes to be removed from <code>old</code>
1510 * @return the attributes of <code>old</code> minus the attributes in
1511 * <code>attributes</code>
1513 AttributeSet removeAttributes(AttributeSet old, AttributeSet attributes);
1516 * Removes all attributes specified by <code>names</code> from
1517 * <code>old</code> and returns the resulting <code>AttributeSet</code>.
1519 * @param old the set of attributes from which to remove attributes
1520 * @param names the names of the attributes to be removed from
1521 * <code>old</code>
1523 * @return the attributes of <code>old</code> minus the attributes in
1524 * <code>attributes</code>
1526 AttributeSet removeAttributes(AttributeSet old, Enumeration<?> names);
1530 * A sequence of data that can be edited. This is were the actual content
1531 * in <code>AbstractDocument</code>'s is stored.
1533 public interface Content
1536 * Creates a {@link Position} that keeps track of the location at
1537 * <code>offset</code>.
1539 * @return a {@link Position} that keeps track of the location at
1540 * <code>offset</code>.
1542 * @throw BadLocationException if <code>offset</code> is not a valid
1543 * location in this <code>Content</code> model
1545 Position createPosition(int offset) throws BadLocationException;
1548 * Returns the length of the content.
1550 * @return the length of the content
1552 int length();
1555 * Inserts a string into the content model.
1557 * @param where the offset at which to insert the string
1558 * @param str the string to be inserted
1560 * @return an <code>UndoableEdit</code> or <code>null</code> if undo is
1561 * not supported by this <code>Content</code> model
1563 * @throws BadLocationException if <code>where</code> is not a valid
1564 * location in this <code>Content</code> model
1566 UndoableEdit insertString(int where, String str)
1567 throws BadLocationException;
1570 * Removes a piece of content from the content model.
1572 * @param where the offset at which to remove content
1573 * @param nitems the number of characters to be removed
1575 * @return an <code>UndoableEdit</code> or <code>null</code> if undo is
1576 * not supported by this <code>Content</code> model
1578 * @throws BadLocationException if <code>where</code> is not a valid
1579 * location in this <code>Content</code> model
1581 UndoableEdit remove(int where, int nitems) throws BadLocationException;
1584 * Returns a piece of content.
1586 * @param where the start offset of the requested fragment
1587 * @param len the length of the requested fragment
1589 * @return the requested fragment
1590 * @throws BadLocationException if <code>offset</code> or
1591 * <code>offset + len</code>is not a valid
1592 * location in this <code>Content</code> model
1594 String getString(int where, int len) throws BadLocationException;
1597 * Fetches a piece of content and stores it in <code>txt</code>.
1599 * @param where the start offset of the requested fragment
1600 * @param len the length of the requested fragment
1601 * @param txt the <code>Segment</code> where to fragment is stored into
1603 * @throws BadLocationException if <code>offset</code> or
1604 * <code>offset + len</code>is not a valid
1605 * location in this <code>Content</code> model
1607 void getChars(int where, int len, Segment txt) throws BadLocationException;
1611 * An abstract base implementation of the {@link Element} interface.
1613 public abstract class AbstractElement
1614 implements Element, MutableAttributeSet, TreeNode, Serializable
1616 /** The serialization UID (compatible with JDK1.5). */
1617 private static final long serialVersionUID = 1712240033321461704L;
1619 /** The number of characters that this Element spans. */
1620 int count;
1622 /** The starting offset of this Element. */
1623 int offset;
1625 /** The attributes of this Element. */
1626 AttributeSet attributes;
1628 /** The parent element. */
1629 Element element_parent;
1631 /** The parent in the TreeNode interface. */
1632 TreeNode tree_parent;
1634 /** The children of this element. */
1635 Vector tree_children;
1638 * Creates a new instance of <code>AbstractElement</code> with a
1639 * specified parent <code>Element</code> and <code>AttributeSet</code>.
1641 * @param p the parent of this <code>AbstractElement</code>
1642 * @param s the attributes to be assigned to this
1643 * <code>AbstractElement</code>
1645 public AbstractElement(Element p, AttributeSet s)
1647 element_parent = p;
1648 AttributeContext ctx = getAttributeContext();
1649 attributes = ctx.getEmptySet();
1650 if (s != null)
1651 addAttributes(s);
1655 * Returns the child nodes of this <code>Element</code> as an
1656 * <code>Enumeration</code> of {@link TreeNode}s.
1658 * @return the child nodes of this <code>Element</code> as an
1659 * <code>Enumeration</code> of {@link TreeNode}s
1661 public abstract Enumeration children();
1664 * Returns <code>true</code> if this <code>AbstractElement</code>
1665 * allows children.
1667 * @return <code>true</code> if this <code>AbstractElement</code>
1668 * allows children
1670 public abstract boolean getAllowsChildren();
1673 * Returns the child of this <code>AbstractElement</code> at
1674 * <code>index</code>.
1676 * @param index the position in the child list of the child element to
1677 * be returned
1679 * @return the child of this <code>AbstractElement</code> at
1680 * <code>index</code>
1682 public TreeNode getChildAt(int index)
1684 return (TreeNode) tree_children.get(index);
1688 * Returns the number of children of this <code>AbstractElement</code>.
1690 * @return the number of children of this <code>AbstractElement</code>
1692 public int getChildCount()
1694 return tree_children.size();
1698 * Returns the index of a given child <code>TreeNode</code> or
1699 * <code>-1</code> if <code>node</code> is not a child of this
1700 * <code>AbstractElement</code>.
1702 * @param node the node for which the index is requested
1704 * @return the index of a given child <code>TreeNode</code> or
1705 * <code>-1</code> if <code>node</code> is not a child of this
1706 * <code>AbstractElement</code>
1708 public int getIndex(TreeNode node)
1710 return tree_children.indexOf(node);
1714 * Returns the parent <code>TreeNode</code> of this
1715 * <code>AbstractElement</code> or <code>null</code> if this element
1716 * has no parent.
1718 * @return the parent <code>TreeNode</code> of this
1719 * <code>AbstractElement</code> or <code>null</code> if this
1720 * element has no parent
1722 public TreeNode getParent()
1724 return tree_parent;
1728 * Returns <code>true</code> if this <code>AbstractElement</code> is a
1729 * leaf element, <code>false</code> otherwise.
1731 * @return <code>true</code> if this <code>AbstractElement</code> is a
1732 * leaf element, <code>false</code> otherwise
1734 public abstract boolean isLeaf();
1737 * Adds an attribute to this element.
1739 * @param name the name of the attribute to be added
1740 * @param value the value of the attribute to be added
1742 public void addAttribute(Object name, Object value)
1744 attributes = getAttributeContext().addAttribute(attributes, name, value);
1748 * Adds a set of attributes to this element.
1750 * @param attrs the attributes to be added to this element
1752 public void addAttributes(AttributeSet attrs)
1754 attributes = getAttributeContext().addAttributes(attributes, attrs);
1758 * Removes an attribute from this element.
1760 * @param name the name of the attribute to be removed
1762 public void removeAttribute(Object name)
1764 attributes = getAttributeContext().removeAttribute(attributes, name);
1768 * Removes a set of attributes from this element.
1770 * @param attrs the attributes to be removed
1772 public void removeAttributes(AttributeSet attrs)
1774 attributes = getAttributeContext().removeAttributes(attributes, attrs);
1778 * Removes a set of attribute from this element.
1780 * @param names the names of the attributes to be removed
1782 public void removeAttributes(Enumeration<?> names)
1784 attributes = getAttributeContext().removeAttributes(attributes, names);
1788 * Sets the parent attribute set against which the element can resolve
1789 * attributes that are not defined in itself.
1791 * @param parent the resolve parent to set
1793 public void setResolveParent(AttributeSet parent)
1795 attributes = getAttributeContext().addAttribute(attributes,
1796 ResolveAttribute,
1797 parent);
1801 * Returns <code>true</code> if this element contains the specified
1802 * attribute.
1804 * @param name the name of the attribute to check
1805 * @param value the value of the attribute to check
1807 * @return <code>true</code> if this element contains the specified
1808 * attribute
1810 public boolean containsAttribute(Object name, Object value)
1812 return attributes.containsAttribute(name, value);
1816 * Returns <code>true</code> if this element contains all of the
1817 * specified attributes.
1819 * @param attrs the attributes to check
1821 * @return <code>true</code> if this element contains all of the
1822 * specified attributes
1824 public boolean containsAttributes(AttributeSet attrs)
1826 return attributes.containsAttributes(attrs);
1830 * Returns a copy of the attributes of this element.
1832 * @return a copy of the attributes of this element
1834 public AttributeSet copyAttributes()
1836 return attributes.copyAttributes();
1840 * Returns the attribute value with the specified key. If this attribute
1841 * is not defined in this element and this element has a resolving
1842 * parent, the search goes upward to the resolve parent chain.
1844 * @param key the key of the requested attribute
1846 * @return the attribute value for <code>key</code> of <code>null</code>
1847 * if <code>key</code> is not found locally and cannot be resolved
1848 * in this element's resolve parents
1850 public Object getAttribute(Object key)
1852 Object result = attributes.getAttribute(key);
1853 if (result == null)
1855 AttributeSet resParent = getResolveParent();
1856 if (resParent != null)
1857 result = resParent.getAttribute(key);
1859 return result;
1863 * Returns the number of defined attributes in this element.
1865 * @return the number of defined attributes in this element
1867 public int getAttributeCount()
1869 return attributes.getAttributeCount();
1873 * Returns the names of the attributes of this element.
1875 * @return the names of the attributes of this element
1877 public Enumeration<?> getAttributeNames()
1879 return attributes.getAttributeNames();
1883 * Returns the resolve parent of this element.
1884 * This is taken from the AttributeSet, but if this is null,
1885 * this method instead returns the Element's parent's
1886 * AttributeSet
1888 * @return the resolve parent of this element
1890 * @see #setResolveParent(AttributeSet)
1892 public AttributeSet getResolveParent()
1894 return attributes.getResolveParent();
1898 * Returns <code>true</code> if an attribute with the specified name
1899 * is defined in this element, <code>false</code> otherwise.
1901 * @param attrName the name of the requested attributes
1903 * @return <code>true</code> if an attribute with the specified name
1904 * is defined in this element, <code>false</code> otherwise
1906 public boolean isDefined(Object attrName)
1908 return attributes.isDefined(attrName);
1912 * Returns <code>true</code> if the specified <code>AttributeSet</code>
1913 * is equal to this element's <code>AttributeSet</code>, <code>false</code>
1914 * otherwise.
1916 * @param attrs the attributes to compare this element to
1918 * @return <code>true</code> if the specified <code>AttributeSet</code>
1919 * is equal to this element's <code>AttributeSet</code>,
1920 * <code>false</code> otherwise
1922 public boolean isEqual(AttributeSet attrs)
1924 return attributes.isEqual(attrs);
1928 * Returns the attributes of this element.
1930 * @return the attributes of this element
1932 public AttributeSet getAttributes()
1934 return this;
1938 * Returns the {@link Document} to which this element belongs.
1940 * @return the {@link Document} to which this element belongs
1942 public Document getDocument()
1944 return AbstractDocument.this;
1948 * Returns the child element at the specified <code>index</code>.
1950 * @param index the index of the requested child element
1952 * @return the requested element
1954 public abstract Element getElement(int index);
1957 * Returns the name of this element.
1959 * @return the name of this element
1961 public String getName()
1963 return (String) attributes.getAttribute(ElementNameAttribute);
1967 * Returns the parent element of this element.
1969 * @return the parent element of this element
1971 public Element getParentElement()
1973 return element_parent;
1977 * Returns the offset inside the document model that is after the last
1978 * character of this element.
1980 * @return the offset inside the document model that is after the last
1981 * character of this element
1983 public abstract int getEndOffset();
1986 * Returns the number of child elements of this element.
1988 * @return the number of child elements of this element
1990 public abstract int getElementCount();
1993 * Returns the index of the child element that spans the specified
1994 * offset in the document model.
1996 * @param offset the offset for which the responsible element is searched
1998 * @return the index of the child element that spans the specified
1999 * offset in the document model
2001 public abstract int getElementIndex(int offset);
2004 * Returns the start offset if this element inside the document model.
2006 * @return the start offset if this element inside the document model
2008 public abstract int getStartOffset();
2011 * Prints diagnostic output to the specified stream.
2013 * @param stream the stream to write to
2014 * @param indent the indentation level
2016 public void dump(PrintStream stream, int indent)
2018 CPStringBuilder b = new CPStringBuilder();
2019 for (int i = 0; i < indent; ++i)
2020 b.append(' ');
2021 b.append('<');
2022 b.append(getName());
2023 // Dump attributes if there are any.
2024 if (getAttributeCount() > 0)
2026 b.append('\n');
2027 Enumeration attNames = getAttributeNames();
2028 while (attNames.hasMoreElements())
2030 for (int i = 0; i < indent + 2; ++i)
2031 b.append(' ');
2032 Object attName = attNames.nextElement();
2033 b.append(attName);
2034 b.append('=');
2035 Object attribute = getAttribute(attName);
2036 b.append(attribute);
2037 b.append('\n');
2040 if (getAttributeCount() > 0)
2042 for (int i = 0; i < indent; ++i)
2043 b.append(' ');
2045 b.append(">\n");
2047 // Dump element content for leaf elements.
2048 if (isLeaf())
2050 for (int i = 0; i < indent + 2; ++i)
2051 b.append(' ');
2052 int start = getStartOffset();
2053 int end = getEndOffset();
2054 b.append('[');
2055 b.append(start);
2056 b.append(',');
2057 b.append(end);
2058 b.append("][");
2061 b.append(getDocument().getText(start, end - start));
2063 catch (BadLocationException ex)
2065 AssertionError err = new AssertionError("BadLocationException "
2066 + "must not be thrown "
2067 + "here.");
2068 err.initCause(ex);
2069 throw err;
2071 b.append("]\n");
2073 stream.print(b.toString());
2075 // Dump child elements if any.
2076 int count = getElementCount();
2077 for (int i = 0; i < count; ++i)
2079 Element el = getElement(i);
2080 if (el instanceof AbstractElement)
2081 ((AbstractElement) el).dump(stream, indent + 2);
2087 * An implementation of {@link Element} to represent composite
2088 * <code>Element</code>s that contain other <code>Element</code>s.
2090 public class BranchElement extends AbstractElement
2092 /** The serialization UID (compatible with JDK1.5). */
2093 private static final long serialVersionUID = -6037216547466333183L;
2096 * The child elements of this BranchElement.
2098 private Element[] children;
2101 * The number of children in the branch element.
2103 private int numChildren;
2106 * The last found index in getElementIndex(). Used for faster searching.
2108 private int lastIndex;
2111 * Creates a new <code>BranchElement</code> with the specified
2112 * parent and attributes.
2114 * @param parent the parent element of this <code>BranchElement</code>
2115 * @param attributes the attributes to set on this
2116 * <code>BranchElement</code>
2118 public BranchElement(Element parent, AttributeSet attributes)
2120 super(parent, attributes);
2121 children = new Element[1];
2122 numChildren = 0;
2123 lastIndex = -1;
2127 * Returns the children of this <code>BranchElement</code>.
2129 * @return the children of this <code>BranchElement</code>
2131 public Enumeration children()
2133 if (numChildren == 0)
2134 return null;
2136 Vector tmp = new Vector();
2138 for (int index = 0; index < numChildren; ++index)
2139 tmp.add(children[index]);
2141 return tmp.elements();
2145 * Returns <code>true</code> since <code>BranchElements</code> allow
2146 * child elements.
2148 * @return <code>true</code> since <code>BranchElements</code> allow
2149 * child elements
2151 public boolean getAllowsChildren()
2153 return true;
2157 * Returns the child element at the specified <code>index</code>.
2159 * @param index the index of the requested child element
2161 * @return the requested element
2163 public Element getElement(int index)
2165 if (index < 0 || index >= numChildren)
2166 return null;
2168 return children[index];
2172 * Returns the number of child elements of this element.
2174 * @return the number of child elements of this element
2176 public int getElementCount()
2178 return numChildren;
2182 * Returns the index of the child element that spans the specified
2183 * offset in the document model.
2185 * @param offset the offset for which the responsible element is searched
2187 * @return the index of the child element that spans the specified
2188 * offset in the document model
2190 public int getElementIndex(int offset)
2192 // Implemented using an improved linear search.
2193 // This makes use of the fact that searches are not random but often
2194 // close to the previous search. So we try to start the binary
2195 // search at the last found index.
2197 int i0 = 0; // The lower bounds.
2198 int i1 = numChildren - 1; // The upper bounds.
2199 int index = -1; // The found index.
2201 int p0 = getStartOffset();
2202 int p1; // Start and end offset local variables.
2204 if (numChildren == 0)
2205 index = 0;
2206 else if (offset >= getEndOffset())
2207 index = numChildren - 1;
2208 else
2210 // Try lastIndex.
2211 if (lastIndex >= i0 && lastIndex <= i1)
2213 Element last = getElement(lastIndex);
2214 p0 = last.getStartOffset();
2215 p1 = last.getEndOffset();
2216 if (offset >= p0 && offset < p1)
2217 index = lastIndex;
2218 else
2220 // Narrow the search bounds using the lastIndex, even
2221 // if it hasn't been a hit.
2222 if (offset < p0)
2223 i1 = lastIndex;
2224 else
2225 i0 = lastIndex;
2228 // The actual search.
2229 int i = 0;
2230 while (i0 <= i1 && index == -1)
2232 i = i0 + (i1 - i0) / 2;
2233 Element el = getElement(i);
2234 p0 = el.getStartOffset();
2235 p1 = el.getEndOffset();
2236 if (offset >= p0 && offset < p1)
2238 // Found it!
2239 index = i;
2241 else if (offset < p0)
2242 i1 = i - 1;
2243 else
2244 i0 = i + 1;
2247 if (index == -1)
2249 // Didn't find it. Return the boundary index.
2250 if (offset < p0)
2251 index = i;
2252 else
2253 index = i + 1;
2256 lastIndex = index;
2258 return index;
2262 * Returns the offset inside the document model that is after the last
2263 * character of this element.
2264 * This is the end offset of the last child element. If this element
2265 * has no children, this method throws a <code>NullPointerException</code>.
2267 * @return the offset inside the document model that is after the last
2268 * character of this element
2270 * @throws NullPointerException if this branch element has no children
2272 public int getEndOffset()
2274 // This might accss one cached element or trigger an NPE for
2275 // numChildren == 0. This is checked by a Mauve test.
2276 Element child = numChildren > 0 ? children[numChildren - 1]
2277 : children[0];
2278 return child.getEndOffset();
2282 * Returns the name of this element. This is {@link #ParagraphElementName}
2283 * in this case.
2285 * @return the name of this element
2287 public String getName()
2289 return ParagraphElementName;
2293 * Returns the start offset of this element inside the document model.
2294 * This is the start offset of the first child element. If this element
2295 * has no children, this method throws a <code>NullPointerException</code>.
2297 * @return the start offset of this element inside the document model
2299 * @throws NullPointerException if this branch element has no children and
2300 * no startOffset value has been cached
2302 public int getStartOffset()
2304 // Do not explicitly throw an NPE here. If the first element is null
2305 // then the NPE gets thrown anyway. If it isn't, then it either
2306 // holds a real value (for numChildren > 0) or a cached value
2307 // (for numChildren == 0) as we don't fully remove elements in replace()
2308 // when removing single elements.
2309 // This is checked by a Mauve test.
2310 return children[0].getStartOffset();
2314 * Returns <code>false</code> since <code>BranchElement</code> are no
2315 * leafes.
2317 * @return <code>false</code> since <code>BranchElement</code> are no
2318 * leafes
2320 public boolean isLeaf()
2322 return false;
2326 * Returns the <code>Element</code> at the specified <code>Document</code>
2327 * offset.
2329 * @return the <code>Element</code> at the specified <code>Document</code>
2330 * offset
2332 * @see #getElementIndex(int)
2334 public Element positionToElement(int position)
2336 // XXX: There is surely a better algorithm
2337 // as beginning from first element each time.
2338 for (int index = 0; index < numChildren; ++index)
2340 Element elem = children[index];
2342 if ((elem.getStartOffset() <= position)
2343 && (position < elem.getEndOffset()))
2344 return elem;
2347 return null;
2351 * Replaces a set of child elements with a new set of child elemens.
2353 * @param offset the start index of the elements to be removed
2354 * @param length the number of elements to be removed
2355 * @param elements the new elements to be inserted
2357 public void replace(int offset, int length, Element[] elements)
2359 int delta = elements.length - length;
2360 int copyFrom = offset + length; // From where to copy.
2361 int copyTo = copyFrom + delta; // Where to copy to.
2362 int numMove = numChildren - copyFrom; // How many elements are moved.
2363 if (numChildren + delta > children.length)
2365 // Gotta grow the array.
2366 int newSize = Math.max(2 * children.length, numChildren + delta);
2367 Element[] target = new Element[newSize];
2368 System.arraycopy(children, 0, target, 0, offset);
2369 System.arraycopy(elements, 0, target, offset, elements.length);
2370 System.arraycopy(children, copyFrom, target, copyTo, numMove);
2371 children = target;
2373 else
2375 System.arraycopy(children, copyFrom, children, copyTo, numMove);
2376 System.arraycopy(elements, 0, children, offset, elements.length);
2378 numChildren += delta;
2382 * Returns a string representation of this element.
2384 * @return a string representation of this element
2386 public String toString()
2388 return ("BranchElement(" + getName() + ") "
2389 + getStartOffset() + "," + getEndOffset() + "\n");
2394 * Stores the changes when a <code>Document</code> is beeing modified.
2396 public class DefaultDocumentEvent extends CompoundEdit
2397 implements DocumentEvent
2399 /** The serialization UID (compatible with JDK1.5). */
2400 private static final long serialVersionUID = 5230037221564563284L;
2403 * The threshold that indicates when we switch to using a Hashtable.
2405 private static final int THRESHOLD = 10;
2407 /** The starting offset of the change. */
2408 private int offset;
2410 /** The length of the change. */
2411 private int length;
2413 /** The type of change. */
2414 private DocumentEvent.EventType type;
2417 * Maps <code>Element</code> to their change records. This is only
2418 * used when the changes array gets too big. We can use an
2419 * (unsync'ed) HashMap here, since changes to this are (should) always
2420 * be performed inside a write lock.
2422 private HashMap changes;
2425 * Indicates if this event has been modified or not. This is used to
2426 * determine if this event is thrown.
2428 private boolean modified;
2431 * Creates a new <code>DefaultDocumentEvent</code>.
2433 * @param offset the starting offset of the change
2434 * @param length the length of the change
2435 * @param type the type of change
2437 public DefaultDocumentEvent(int offset, int length,
2438 DocumentEvent.EventType type)
2440 this.offset = offset;
2441 this.length = length;
2442 this.type = type;
2443 modified = false;
2447 * Adds an UndoableEdit to this <code>DocumentEvent</code>. If this
2448 * edit is an instance of {@link ElementEdit}, then this record can
2449 * later be fetched by calling {@link #getChange}.
2451 * @param edit the undoable edit to add
2453 public boolean addEdit(UndoableEdit edit)
2455 // Start using Hashtable when we pass a certain threshold. This
2456 // gives a good memory/performance compromise.
2457 if (changes == null && edits.size() > THRESHOLD)
2459 changes = new HashMap();
2460 int count = edits.size();
2461 for (int i = 0; i < count; i++)
2463 Object o = edits.elementAt(i);
2464 if (o instanceof ElementChange)
2466 ElementChange ec = (ElementChange) o;
2467 changes.put(ec.getElement(), ec);
2472 if (changes != null && edit instanceof ElementChange)
2474 ElementChange elEdit = (ElementChange) edit;
2475 changes.put(elEdit.getElement(), elEdit);
2477 return super.addEdit(edit);
2481 * Returns the document that has been modified.
2483 * @return the document that has been modified
2485 public Document getDocument()
2487 return AbstractDocument.this;
2491 * Returns the length of the modification.
2493 * @return the length of the modification
2495 public int getLength()
2497 return length;
2501 * Returns the start offset of the modification.
2503 * @return the start offset of the modification
2505 public int getOffset()
2507 return offset;
2511 * Returns the type of the modification.
2513 * @return the type of the modification
2515 public DocumentEvent.EventType getType()
2517 return type;
2521 * Returns the changes for an element.
2523 * @param elem the element for which the changes are requested
2525 * @return the changes for <code>elem</code> or <code>null</code> if
2526 * <code>elem</code> has not been changed
2528 public ElementChange getChange(Element elem)
2530 ElementChange change = null;
2531 if (changes != null)
2533 change = (ElementChange) changes.get(elem);
2535 else
2537 int count = edits.size();
2538 for (int i = 0; i < count && change == null; i++)
2540 Object o = edits.get(i);
2541 if (o instanceof ElementChange)
2543 ElementChange ec = (ElementChange) o;
2544 if (elem.equals(ec.getElement()))
2545 change = ec;
2549 return change;
2553 * Returns a String description of the change event. This returns the
2554 * toString method of the Vector of edits.
2556 public String toString()
2558 return edits.toString();
2563 * An implementation of {@link DocumentEvent.ElementChange} to be added
2564 * to {@link DefaultDocumentEvent}s.
2566 public static class ElementEdit extends AbstractUndoableEdit
2567 implements DocumentEvent.ElementChange
2569 /** The serial version UID of ElementEdit. */
2570 private static final long serialVersionUID = -1216620962142928304L;
2573 * The changed element.
2575 private Element elem;
2578 * The index of the change.
2580 private int index;
2583 * The removed elements.
2585 private Element[] removed;
2588 * The added elements.
2590 private Element[] added;
2593 * Creates a new <code>ElementEdit</code>.
2595 * @param elem the changed element
2596 * @param index the index of the change
2597 * @param removed the removed elements
2598 * @param added the added elements
2600 public ElementEdit(Element elem, int index,
2601 Element[] removed, Element[] added)
2603 this.elem = elem;
2604 this.index = index;
2605 this.removed = removed;
2606 this.added = added;
2610 * Returns the added elements.
2612 * @return the added elements
2614 public Element[] getChildrenAdded()
2616 return added;
2620 * Returns the removed elements.
2622 * @return the removed elements
2624 public Element[] getChildrenRemoved()
2626 return removed;
2630 * Returns the changed element.
2632 * @return the changed element
2634 public Element getElement()
2636 return elem;
2640 * Returns the index of the change.
2642 * @return the index of the change
2644 public int getIndex()
2646 return index;
2651 * An implementation of {@link Element} that represents a leaf in the
2652 * document structure. This is used to actually store content.
2654 public class LeafElement extends AbstractElement
2656 /** The serialization UID (compatible with JDK1.5). */
2657 private static final long serialVersionUID = -8906306331347768017L;
2660 * Manages the start offset of this element.
2662 private Position startPos;
2665 * Manages the end offset of this element.
2667 private Position endPos;
2670 * Creates a new <code>LeafElement</code>.
2672 * @param parent the parent of this <code>LeafElement</code>
2673 * @param attributes the attributes to be set
2674 * @param start the start index of this element inside the document model
2675 * @param end the end index of this element inside the document model
2677 public LeafElement(Element parent, AttributeSet attributes, int start,
2678 int end)
2680 super(parent, attributes);
2683 startPos = createPosition(start);
2684 endPos = createPosition(end);
2686 catch (BadLocationException ex)
2688 AssertionError as;
2689 as = new AssertionError("BadLocationException thrown "
2690 + "here. start=" + start
2691 + ", end=" + end
2692 + ", length=" + getLength());
2693 as.initCause(ex);
2694 throw as;
2699 * Returns <code>null</code> since <code>LeafElement</code>s cannot have
2700 * children.
2702 * @return <code>null</code> since <code>LeafElement</code>s cannot have
2703 * children
2705 public Enumeration children()
2707 return null;
2711 * Returns <code>false</code> since <code>LeafElement</code>s cannot have
2712 * children.
2714 * @return <code>false</code> since <code>LeafElement</code>s cannot have
2715 * children
2717 public boolean getAllowsChildren()
2719 return false;
2723 * Returns <code>null</code> since <code>LeafElement</code>s cannot have
2724 * children.
2726 * @return <code>null</code> since <code>LeafElement</code>s cannot have
2727 * children
2729 public Element getElement(int index)
2731 return null;
2735 * Returns <code>0</code> since <code>LeafElement</code>s cannot have
2736 * children.
2738 * @return <code>0</code> since <code>LeafElement</code>s cannot have
2739 * children
2741 public int getElementCount()
2743 return 0;
2747 * Returns <code>-1</code> since <code>LeafElement</code>s cannot have
2748 * children.
2750 * @return <code>-1</code> since <code>LeafElement</code>s cannot have
2751 * children
2753 public int getElementIndex(int offset)
2755 return -1;
2759 * Returns the end offset of this <code>Element</code> inside the
2760 * document.
2762 * @return the end offset of this <code>Element</code> inside the
2763 * document
2765 public int getEndOffset()
2767 return endPos.getOffset();
2771 * Returns the name of this <code>Element</code>. This is
2772 * {@link #ContentElementName} in this case.
2774 * @return the name of this <code>Element</code>
2776 public String getName()
2778 String name = super.getName();
2779 if (name == null)
2780 name = ContentElementName;
2781 return name;
2785 * Returns the start offset of this <code>Element</code> inside the
2786 * document.
2788 * @return the start offset of this <code>Element</code> inside the
2789 * document
2791 public int getStartOffset()
2793 return startPos.getOffset();
2797 * Returns <code>true</code>.
2799 * @return <code>true</code>
2801 public boolean isLeaf()
2803 return true;
2807 * Returns a string representation of this <code>Element</code>.
2809 * @return a string representation of this <code>Element</code>
2811 public String toString()
2813 return ("LeafElement(" + getName() + ") "
2814 + getStartOffset() + "," + getEndOffset() + "\n");
2819 * The root element for bidirectional text.
2821 private class BidiRootElement
2822 extends BranchElement
2825 * Creates a new bidi root element.
2827 BidiRootElement()
2829 super(null, null);
2833 * Returns the name of the element.
2835 * @return the name of the element
2837 public String getName()
2839 return BidiRootName;
2844 * A leaf element for the bidi structure.
2846 private class BidiElement
2847 extends LeafElement
2850 * Creates a new BidiElement.
2852 * @param parent the parent element
2853 * @param start the start offset
2854 * @param end the end offset
2855 * @param level the bidi level
2857 BidiElement(Element parent, int start, int end, int level)
2859 super(parent, new SimpleAttributeSet(), start, end);
2860 addAttribute(StyleConstants.BidiLevel, new Integer(level));
2864 * Returns the name of the element.
2866 * @return the name of the element
2868 public String getName()
2870 return BidiElementName;
2874 /** A class whose methods delegate to the insert, remove and replace methods
2875 * of this document which do not check for an installed DocumentFilter.
2877 class Bypass extends DocumentFilter.FilterBypass
2880 public Document getDocument()
2882 return AbstractDocument.this;
2885 public void insertString(int offset, String string, AttributeSet attr)
2886 throws BadLocationException
2888 AbstractDocument.this.insertStringImpl(offset, string, attr);
2891 public void remove(int offset, int length)
2892 throws BadLocationException
2894 AbstractDocument.this.removeImpl(offset, length);
2897 public void replace(int offset, int length, String string,
2898 AttributeSet attrs)
2899 throws BadLocationException
2901 AbstractDocument.this.replaceImpl(offset, length, string, attrs);