src/share/classes/javax/swing/text/Document.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing.text;
  26 
  27 import javax.swing.event.*;
  28 
  29 /**
  30  * <p>
  31  * The <code>Document</code> is a container for text that serves
  32  * as the model for swing text components.  The goal for this
  33  * interface is to scale from very simple needs (a plain text textfield)
  34  * to complex needs (an HTML or XML document, for example).
  35  *
  36  * <p><b><font size=+1>Content</font></b>
  37  * <p>
  38  * At the simplest level, text can be
  39  * modeled as a linear sequence of characters. To support
  40  * internationalization, the Swing text model uses
  41  * <a href="http://www.unicode.org/">unicode</a> characters.
  42  * The sequence of characters displayed in a text component is
  43  * generally referred to as the component's <em>content</em>.
  44  * <p>
  45  * To refer to locations within the sequence, the coordinates
  46  * used are the location between two characters.  As the diagram
  47  * below shows, a location in a text document can be referred to
  48  * as a position, or an offset. This position is zero-based.
  49  * <p style="text-align:center"><img src="doc-files/Document-coord.gif"
  50  * alt="The following text describes this graphic.">
  51  * <p>
  52  * In the example, if the content of a document is the
  53  * sequence "The quick brown fox," as shown in the preceding diagram,
  54  * the location just before the word "The" is 0, and the location after
  55  * the word "The" and before the whitespace that follows it is 3.
  56  * The entire sequence of characters in the sequence "The" is called a
  57  * <em>range</em>.
  58  * <p>The following methods give access to the character data
  59  * that makes up the content.
  60  * <ul>
  61  * <li>{@link #getLength()}
  62  * <li>{@link #getText(int, int)}
  63  * <li>{@link #getText(int, int, javax.swing.text.Segment)}
  64  * </ul>
  65  * <p><b><font size=+1>Structure</font></b>
  66  * <p>
  67  * Text is rarely represented simply as featureless content. Rather,
  68  * text typically has some sort of structure associated with it.
  69  * Exactly what structure is modeled is up to a particular Document
  70  * implementation.  It might be as simple as no structure (i.e. a
  71  * simple text field), or it might be something like diagram below.
  72  * <p style="text-align:center"><img src="doc-files/Document-structure.gif"
  73  * alt="Diagram shows Book->Chapter->Paragraph">
  74  * <p>
  75  * The unit of structure (i.e. a node of the tree) is referred to
  76  * by the <a href="Element.html">Element</a> interface.  Each Element
  77  * can be tagged with a set of attributes.  These attributes
  78  * (name/value pairs) are defined by the
  79  * <a href="AttributeSet.html">AttributeSet</a> interface.
  80  * <p>The following methods give access to the document structure.
  81  * <ul>
  82  * <li>{@link #getDefaultRootElement()}
  83  * <li>{@link #getRootElements()}
  84  * </ul>
  85  *
  86  * <p><b><font size=+1>Mutations</font></b>
  87  * <p>
  88  * All documents need to be able to add and remove simple text.
  89  * Typically, text is inserted and removed via gestures from
  90  * a keyboard or a mouse.  What effect the insertion or removal
  91  * has upon the document structure is entirely up to the
  92  * implementation of the document.
  93  * <p>The following methods are related to mutation of the
  94  * document content:
  95  * <ul>
  96  * <li>{@link #insertString(int, java.lang.String, javax.swing.text.AttributeSet)}
  97  * <li>{@link #remove(int, int)}
  98  * <li>{@link #createPosition(int)}
  99  * </ul>
 100  *
 101  * <p><b><font size=+1>Notification</font></b>
 102  * <p>
 103  * Mutations to the <code>Document</code> must be communicated to
 104  * interested observers.  The notification of change follows the event model
 105  * guidelines that are specified for JavaBeans.  In the JavaBeans
 106  * event model, once an event notification is dispatched, all listeners
 107  * must be notified before any further mutations occur to the source
 108  * of the event.  Further, order of delivery is not guaranteed.
 109  * <p>
 110  * Notification is provided as two separate events,
 111  * <a href="../event/DocumentEvent.html">DocumentEvent</a>, and
 112  * <a href="../event/UndoableEditEvent.html">UndoableEditEvent</a>.
 113  * If a mutation is made to a <code>Document</code> through its api,
 114  * a <code>DocumentEvent</code> will be sent to all of the registered
 115  * <code>DocumentListeners</code>.  If the <code>Document</code>
 116  * implementation supports undo/redo capabilities, an
 117  * <code>UndoableEditEvent</code> will be sent
 118  * to all of the registered <code>UndoableEditListener</code>s.
 119  * If an undoable edit is undone, a <code>DocumentEvent</code> should be
 120  * fired from the Document to indicate it has changed again.
 121  * In this case however, there should be no <code>UndoableEditEvent</code>


 135  * document.  Again, the document dispatches a DocumentEvent to both
 136  * component views and sends an UndoableEditEvent to the listening logic
 137  * that is maintaining the history buffer.
 138  * <p>
 139  * If the history buffer is then rolled back (i.e. the last UndoableEdit
 140  * undone), a DocumentEvent is sent to both views, causing both of them to
 141  * reflect the undone mutation to the document (that is, the
 142  * removal of the right component's mutation). If the history buffer again
 143  * rolls back another change, another DocumentEvent is sent to both views,
 144  * causing them to reflect the undone mutation to the document -- that is,
 145  * the removal of the left component's mutation.
 146  * <p>
 147  * The methods related to observing mutations to the document are:
 148  * <ul>
 149  * <li><a href="#addDocumentListener(javax.swing.event.DocumentListener)">addDocumentListener(DocumentListener)</a>
 150  * <li><a href="#removeDocumentListener(javax.swing.event.DocumentListener)">removeDocumentListener(DocumentListener)</a>
 151  * <li><a href="#addUndoableEditListener(javax.swing.event.UndoableEditListener)">addUndoableEditListener(UndoableEditListener)</a>
 152  * <li><a href="#removeUndoableEditListener(javax.swing.event.UndoableEditListener)">removeUndoableEditListener(UndoableEditListener)</a>
 153  * </ul>
 154  *
 155  * <p><b><font size=+1>Properties</font></b>
 156  * <p>
 157  * Document implementations will generally have some set of properties
 158  * associated with them at runtime.  Two well known properties are the
 159  * <a href="#StreamDescriptionProperty">StreamDescriptionProperty</a>,
 160  * which can be used to describe where the <code>Document</code> came from,
 161  * and the <a href="#TitleProperty">TitleProperty</a>, which can be used to
 162  * name the <code>Document</code>.  The methods related to the properties are:
 163  * <ul>
 164  * <li>{@link #getProperty(java.lang.Object)}
 165  * <li>{@link #putProperty(java.lang.Object, java.lang.Object)}
 166  * </ul>
 167  *
 168  * <p>For more information on the <code>Document</code> class, see
 169  * <a href="http://java.sun.com/products/jfc/tsc">The Swing Connection</a>
 170  * and most particularly the article,
 171  * <a href="http://java.sun.com/products/jfc/tsc/articles/text/element_interface">
 172  * The Element Interface</a>.
 173  *
 174  * @author  Timothy Prinzing
 175  *


   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing.text;
  26 
  27 import javax.swing.event.*;
  28 
  29 /**
  30  * <p>
  31  * The <code>Document</code> is a container for text that serves
  32  * as the model for swing text components.  The goal for this
  33  * interface is to scale from very simple needs (a plain text textfield)
  34  * to complex needs (an HTML or XML document, for example).
  35  *
  36  * <p><b>Content</b>
  37  * <p>
  38  * At the simplest level, text can be
  39  * modeled as a linear sequence of characters. To support
  40  * internationalization, the Swing text model uses
  41  * <a href="http://www.unicode.org/">unicode</a> characters.
  42  * The sequence of characters displayed in a text component is
  43  * generally referred to as the component's <em>content</em>.
  44  * <p>
  45  * To refer to locations within the sequence, the coordinates
  46  * used are the location between two characters.  As the diagram
  47  * below shows, a location in a text document can be referred to
  48  * as a position, or an offset. This position is zero-based.
  49  * <p style="text-align:center"><img src="doc-files/Document-coord.gif"
  50  * alt="The following text describes this graphic.">
  51  * <p>
  52  * In the example, if the content of a document is the
  53  * sequence "The quick brown fox," as shown in the preceding diagram,
  54  * the location just before the word "The" is 0, and the location after
  55  * the word "The" and before the whitespace that follows it is 3.
  56  * The entire sequence of characters in the sequence "The" is called a
  57  * <em>range</em>.
  58  * <p>The following methods give access to the character data
  59  * that makes up the content.
  60  * <ul>
  61  * <li>{@link #getLength()}
  62  * <li>{@link #getText(int, int)}
  63  * <li>{@link #getText(int, int, javax.swing.text.Segment)}
  64  * </ul>
  65  * <p><b>Structure</b>
  66  * <p>
  67  * Text is rarely represented simply as featureless content. Rather,
  68  * text typically has some sort of structure associated with it.
  69  * Exactly what structure is modeled is up to a particular Document
  70  * implementation.  It might be as simple as no structure (i.e. a
  71  * simple text field), or it might be something like diagram below.
  72  * <p style="text-align:center"><img src="doc-files/Document-structure.gif"
  73  * alt="Diagram shows Book->Chapter->Paragraph">
  74  * <p>
  75  * The unit of structure (i.e. a node of the tree) is referred to
  76  * by the <a href="Element.html">Element</a> interface.  Each Element
  77  * can be tagged with a set of attributes.  These attributes
  78  * (name/value pairs) are defined by the
  79  * <a href="AttributeSet.html">AttributeSet</a> interface.
  80  * <p>The following methods give access to the document structure.
  81  * <ul>
  82  * <li>{@link #getDefaultRootElement()}
  83  * <li>{@link #getRootElements()}
  84  * </ul>
  85  *
  86  * <p><b>Mutations</b>
  87  * <p>
  88  * All documents need to be able to add and remove simple text.
  89  * Typically, text is inserted and removed via gestures from
  90  * a keyboard or a mouse.  What effect the insertion or removal
  91  * has upon the document structure is entirely up to the
  92  * implementation of the document.
  93  * <p>The following methods are related to mutation of the
  94  * document content:
  95  * <ul>
  96  * <li>{@link #insertString(int, java.lang.String, javax.swing.text.AttributeSet)}
  97  * <li>{@link #remove(int, int)}
  98  * <li>{@link #createPosition(int)}
  99  * </ul>
 100  *
 101  * <p><b>Notification</b>
 102  * <p>
 103  * Mutations to the <code>Document</code> must be communicated to
 104  * interested observers.  The notification of change follows the event model
 105  * guidelines that are specified for JavaBeans.  In the JavaBeans
 106  * event model, once an event notification is dispatched, all listeners
 107  * must be notified before any further mutations occur to the source
 108  * of the event.  Further, order of delivery is not guaranteed.
 109  * <p>
 110  * Notification is provided as two separate events,
 111  * <a href="../event/DocumentEvent.html">DocumentEvent</a>, and
 112  * <a href="../event/UndoableEditEvent.html">UndoableEditEvent</a>.
 113  * If a mutation is made to a <code>Document</code> through its api,
 114  * a <code>DocumentEvent</code> will be sent to all of the registered
 115  * <code>DocumentListeners</code>.  If the <code>Document</code>
 116  * implementation supports undo/redo capabilities, an
 117  * <code>UndoableEditEvent</code> will be sent
 118  * to all of the registered <code>UndoableEditListener</code>s.
 119  * If an undoable edit is undone, a <code>DocumentEvent</code> should be
 120  * fired from the Document to indicate it has changed again.
 121  * In this case however, there should be no <code>UndoableEditEvent</code>


 135  * document.  Again, the document dispatches a DocumentEvent to both
 136  * component views and sends an UndoableEditEvent to the listening logic
 137  * that is maintaining the history buffer.
 138  * <p>
 139  * If the history buffer is then rolled back (i.e. the last UndoableEdit
 140  * undone), a DocumentEvent is sent to both views, causing both of them to
 141  * reflect the undone mutation to the document (that is, the
 142  * removal of the right component's mutation). If the history buffer again
 143  * rolls back another change, another DocumentEvent is sent to both views,
 144  * causing them to reflect the undone mutation to the document -- that is,
 145  * the removal of the left component's mutation.
 146  * <p>
 147  * The methods related to observing mutations to the document are:
 148  * <ul>
 149  * <li><a href="#addDocumentListener(javax.swing.event.DocumentListener)">addDocumentListener(DocumentListener)</a>
 150  * <li><a href="#removeDocumentListener(javax.swing.event.DocumentListener)">removeDocumentListener(DocumentListener)</a>
 151  * <li><a href="#addUndoableEditListener(javax.swing.event.UndoableEditListener)">addUndoableEditListener(UndoableEditListener)</a>
 152  * <li><a href="#removeUndoableEditListener(javax.swing.event.UndoableEditListener)">removeUndoableEditListener(UndoableEditListener)</a>
 153  * </ul>
 154  *
 155  * <p><b>Properties</b>
 156  * <p>
 157  * Document implementations will generally have some set of properties
 158  * associated with them at runtime.  Two well known properties are the
 159  * <a href="#StreamDescriptionProperty">StreamDescriptionProperty</a>,
 160  * which can be used to describe where the <code>Document</code> came from,
 161  * and the <a href="#TitleProperty">TitleProperty</a>, which can be used to
 162  * name the <code>Document</code>.  The methods related to the properties are:
 163  * <ul>
 164  * <li>{@link #getProperty(java.lang.Object)}
 165  * <li>{@link #putProperty(java.lang.Object, java.lang.Object)}
 166  * </ul>
 167  *
 168  * <p>For more information on the <code>Document</code> class, see
 169  * <a href="http://java.sun.com/products/jfc/tsc">The Swing Connection</a>
 170  * and most particularly the article,
 171  * <a href="http://java.sun.com/products/jfc/tsc/articles/text/element_interface">
 172  * The Element Interface</a>.
 173  *
 174  * @author  Timothy Prinzing
 175  *