jdk/src/share/classes/javax/swing/JTextPane.java

Print this page




   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;
  26 
  27 import java.awt.*;
  28 import java.awt.event.ActionEvent;
  29 
  30 import java.io.ObjectOutputStream;
  31 import java.io.ObjectInputStream;
  32 import java.io.IOException;
  33 
  34 import javax.swing.text.*;
  35 import javax.swing.event.*;
  36 import javax.swing.plaf.*;
  37 
  38 /**
  39  * A text component that can be marked up with attributes that are
  40  * represented graphically.
  41  * You can find how-to information and examples of using text panes in
  42  * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/text.html">Using Text Components</a>,
  43  * a section in <em>The Java Tutorial.</em>
  44  *
  45  * <p>
  46  * This component models paragraphs
  47  * that are composed of runs of character level attributes.  Each
  48  * paragraph may have a logical style attached to it which contains
  49  * the default attributes to use if not overridden by attributes set
  50  * on the paragraph or character run.  Components and images may
  51  * be embedded in the flow of text.
  52  *
  53  * <dl>
  54  * <dt><b>Newlines</b>
  55  * <dd>
  56  * For a discussion on how newlines are handled, see
  57  * <a href="text/DefaultEditorKit.html">DefaultEditorKit</a>.
  58  * </dl>
  59  *
  60  * <p>
  61  * <strong>Warning:</strong> Swing is not thread safe. For more
  62  * information see <a
  63  * href="package-summary.html#threading">Swing's Threading
  64  * Policy</a>.
  65  * <p>
  66  * <strong>Warning:</strong>
  67  * Serialized objects of this class will not be compatible with
  68  * future Swing releases. The current serialization support is
  69  * appropriate for short term storage or RMI between applications running
  70  * the same version of Swing.  As of 1.4, support for long term storage
  71  * of all JavaBeans&trade;
  72  * has been added to the <code>java.beans</code> package.
  73  * Please see {@link java.beans.XMLEncoder}.
  74  *
  75  * @beaninfo
  76  *   attribute: isContainer true
  77  * description: A text component that can be marked up with attributes that are graphically represented.
  78  *
  79  * @author  Timothy Prinzing
  80  * @see javax.swing.text.StyledEditorKit
  81  * @since 1.2
  82  */


  83 @SuppressWarnings("serial") // Same-version serialization only
  84 public class JTextPane extends JEditorPane {
  85 
  86     /**
  87      * Creates a new <code>JTextPane</code>.  A new instance of
  88      * <code>StyledEditorKit</code> is
  89      * created and set, and the document model set to <code>null</code>.
  90      */
  91     public JTextPane() {
  92         super();
  93         EditorKit editorKit = createDefaultEditorKit();
  94         String contentType = editorKit.getContentType();
  95         if (contentType != null
  96             && getEditorKitClassNameForContentType(contentType) ==
  97                  defaultEditorKitMap.get(contentType)) {
  98             setEditorKitForContentType(contentType, editorKit);
  99         }
 100         setEditorKit(editorKit);
 101     }
 102 
 103     /**
 104      * Creates a new <code>JTextPane</code>, with a specified document model.
 105      * A new instance of <code>javax.swing.text.StyledEditorKit</code>
 106      *  is created and set.
 107      *
 108      * @param doc the document model
 109      */
 110     public JTextPane(StyledDocument doc) {
 111         this();
 112         setStyledDocument(doc);
 113     }
 114 
 115     /**
 116      * Returns the class ID for the UI.
 117      *
 118      * @return the string "TextPaneUI"
 119      *
 120      * @see JComponent#getUIClassID
 121      * @see UIDefaults#getUI
 122      */

 123     public String getUIClassID() {
 124         return uiClassID;
 125     }
 126 
 127     /**
 128      * Associates the editor with a text document.  This
 129      * must be a <code>StyledDocument</code>.
 130      *
 131      * @param doc  the document to display/edit
 132      * @exception IllegalArgumentException  if <code>doc</code> can't
 133      *   be narrowed to a <code>StyledDocument</code> which is the
 134      *   required type of model for this text component
 135      */
 136     public void setDocument(Document doc) {
 137         if (doc instanceof StyledDocument) {
 138             super.setDocument(doc);
 139         } else {
 140             throw new IllegalArgumentException("Model must be StyledDocument");
 141         }
 142     }


 321         doc.setLogicalStyle(getCaretPosition(), s);
 322     }
 323 
 324     /**
 325      * Fetches the logical style assigned to the paragraph represented
 326      * by the current position of the caret, or <code>null</code>.
 327      *
 328      * @return the <code>Style</code>
 329      */
 330     public Style getLogicalStyle() {
 331         StyledDocument doc = getStyledDocument();
 332         return doc.getLogicalStyle(getCaretPosition());
 333     }
 334 
 335     /**
 336      * Fetches the character attributes in effect at the
 337      * current location of the caret, or <code>null</code>.
 338      *
 339      * @return the attributes, or <code>null</code>
 340      */

 341     public AttributeSet getCharacterAttributes() {
 342         StyledDocument doc = getStyledDocument();
 343         Element run = doc.getCharacterElement(getCaretPosition());
 344         if (run != null) {
 345             return run.getAttributes();
 346         }
 347         return null;
 348     }
 349 
 350     /**
 351      * Applies the given attributes to character
 352      * content.  If there is a selection, the attributes
 353      * are applied to the selection range.  If there
 354      * is no selection, the attributes are applied to
 355      * the input attribute set which defines the attributes
 356      * for any new text that gets inserted.
 357      *
 358      * @param attr the attributes
 359      * @param replace if true, then replace the existing attributes first
 360      */


 362         int p0 = getSelectionStart();
 363         int p1 = getSelectionEnd();
 364         if (p0 != p1) {
 365             StyledDocument doc = getStyledDocument();
 366             doc.setCharacterAttributes(p0, p1 - p0, attr, replace);
 367         } else {
 368             MutableAttributeSet inputAttributes = getInputAttributes();
 369             if (replace) {
 370                 inputAttributes.removeAttributes(inputAttributes);
 371             }
 372             inputAttributes.addAttributes(attr);
 373         }
 374     }
 375 
 376     /**
 377      * Fetches the current paragraph attributes in effect
 378      * at the location of the caret, or <code>null</code> if none.
 379      *
 380      * @return the attributes
 381      */

 382     public AttributeSet getParagraphAttributes() {
 383         StyledDocument doc = getStyledDocument();
 384         Element paragraph = doc.getParagraphElement(getCaretPosition());
 385         if (paragraph != null) {
 386             return paragraph.getAttributes();
 387         }
 388         return null;
 389     }
 390 
 391     /**
 392      * Applies the given attributes to paragraphs.  If
 393      * there is a selection, the attributes are applied
 394      * to the paragraphs that intersect the selection.
 395      * If there is no selection, the attributes are applied
 396      * to the paragraph at the current caret position.
 397      *
 398      * @param attr the non-<code>null</code> attributes
 399      * @param replace if true, replace the existing attributes first
 400      */
 401     public void setParagraphAttributes(AttributeSet attr, boolean replace) {
 402         int p0 = getSelectionStart();
 403         int p1 = getSelectionEnd();
 404         StyledDocument doc = getStyledDocument();
 405         doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
 406     }
 407 
 408     /**
 409      * Gets the input attributes for the pane.
 410      *
 411      * @return the attributes
 412      */

 413     public MutableAttributeSet getInputAttributes() {
 414         return getStyledEditorKit().getInputAttributes();
 415     }
 416 
 417     /**
 418      * Gets the editor kit.
 419      *
 420      * @return the editor kit
 421      */
 422     protected final StyledEditorKit getStyledEditorKit() {
 423         return (StyledEditorKit) getEditorKit();
 424     }
 425 
 426     /**
 427      * @see #getUIClassID
 428      * @see #readObject
 429      */
 430     private static final String uiClassID = "TextPaneUI";
 431 
 432 




   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;
  26 
  27 import java.awt.*;
  28 import java.beans.JavaBean;
  29 import java.beans.BeanProperty;
  30 import java.io.ObjectOutputStream;

  31 import java.io.IOException;
  32 
  33 import javax.swing.text.*;


  34 
  35 /**
  36  * A text component that can be marked up with attributes that are
  37  * represented graphically.
  38  * You can find how-to information and examples of using text panes in
  39  * <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/text.html">Using Text Components</a>,
  40  * a section in <em>The Java Tutorial.</em>
  41  *
  42  * <p>
  43  * This component models paragraphs
  44  * that are composed of runs of character level attributes.  Each
  45  * paragraph may have a logical style attached to it which contains
  46  * the default attributes to use if not overridden by attributes set
  47  * on the paragraph or character run.  Components and images may
  48  * be embedded in the flow of text.
  49  *
  50  * <dl>
  51  * <dt><b>Newlines</b>
  52  * <dd>
  53  * For a discussion on how newlines are handled, see
  54  * <a href="text/DefaultEditorKit.html">DefaultEditorKit</a>.
  55  * </dl>
  56  *
  57  * <p>
  58  * <strong>Warning:</strong> Swing is not thread safe. For more
  59  * information see <a
  60  * href="package-summary.html#threading">Swing's Threading
  61  * Policy</a>.
  62  * <p>
  63  * <strong>Warning:</strong>
  64  * Serialized objects of this class will not be compatible with
  65  * future Swing releases. The current serialization support is
  66  * appropriate for short term storage or RMI between applications running
  67  * the same version of Swing.  As of 1.4, support for long term storage
  68  * of all JavaBeans&trade;
  69  * has been added to the <code>java.beans</code> package.
  70  * Please see {@link java.beans.XMLEncoder}.
  71  *




  72  * @author  Timothy Prinzing
  73  * @see javax.swing.text.StyledEditorKit
  74  * @since 1.2
  75  */
  76 @JavaBean(description = "A text component that can be marked up with attributes that are graphically represented.")
  77 @SwingContainer
  78 @SuppressWarnings("serial") // Same-version serialization only
  79 public class JTextPane extends JEditorPane {
  80 
  81     /**
  82      * Creates a new <code>JTextPane</code>.  A new instance of
  83      * <code>StyledEditorKit</code> is
  84      * created and set, and the document model set to <code>null</code>.
  85      */
  86     public JTextPane() {
  87         super();
  88         EditorKit editorKit = createDefaultEditorKit();
  89         String contentType = editorKit.getContentType();
  90         if (contentType != null
  91             && getEditorKitClassNameForContentType(contentType) ==
  92                  defaultEditorKitMap.get(contentType)) {
  93             setEditorKitForContentType(contentType, editorKit);
  94         }
  95         setEditorKit(editorKit);
  96     }
  97 
  98     /**
  99      * Creates a new <code>JTextPane</code>, with a specified document model.
 100      * A new instance of <code>javax.swing.text.StyledEditorKit</code>
 101      *  is created and set.
 102      *
 103      * @param doc the document model
 104      */
 105     public JTextPane(StyledDocument doc) {
 106         this();
 107         setStyledDocument(doc);
 108     }
 109 
 110     /**
 111      * Returns the class ID for the UI.
 112      *
 113      * @return the string "TextPaneUI"
 114      *
 115      * @see JComponent#getUIClassID
 116      * @see UIDefaults#getUI
 117      */
 118     @BeanProperty(bound = false)
 119     public String getUIClassID() {
 120         return uiClassID;
 121     }
 122 
 123     /**
 124      * Associates the editor with a text document.  This
 125      * must be a <code>StyledDocument</code>.
 126      *
 127      * @param doc  the document to display/edit
 128      * @exception IllegalArgumentException  if <code>doc</code> can't
 129      *   be narrowed to a <code>StyledDocument</code> which is the
 130      *   required type of model for this text component
 131      */
 132     public void setDocument(Document doc) {
 133         if (doc instanceof StyledDocument) {
 134             super.setDocument(doc);
 135         } else {
 136             throw new IllegalArgumentException("Model must be StyledDocument");
 137         }
 138     }


 317         doc.setLogicalStyle(getCaretPosition(), s);
 318     }
 319 
 320     /**
 321      * Fetches the logical style assigned to the paragraph represented
 322      * by the current position of the caret, or <code>null</code>.
 323      *
 324      * @return the <code>Style</code>
 325      */
 326     public Style getLogicalStyle() {
 327         StyledDocument doc = getStyledDocument();
 328         return doc.getLogicalStyle(getCaretPosition());
 329     }
 330 
 331     /**
 332      * Fetches the character attributes in effect at the
 333      * current location of the caret, or <code>null</code>.
 334      *
 335      * @return the attributes, or <code>null</code>
 336      */
 337     @BeanProperty(bound = false)
 338     public AttributeSet getCharacterAttributes() {
 339         StyledDocument doc = getStyledDocument();
 340         Element run = doc.getCharacterElement(getCaretPosition());
 341         if (run != null) {
 342             return run.getAttributes();
 343         }
 344         return null;
 345     }
 346 
 347     /**
 348      * Applies the given attributes to character
 349      * content.  If there is a selection, the attributes
 350      * are applied to the selection range.  If there
 351      * is no selection, the attributes are applied to
 352      * the input attribute set which defines the attributes
 353      * for any new text that gets inserted.
 354      *
 355      * @param attr the attributes
 356      * @param replace if true, then replace the existing attributes first
 357      */


 359         int p0 = getSelectionStart();
 360         int p1 = getSelectionEnd();
 361         if (p0 != p1) {
 362             StyledDocument doc = getStyledDocument();
 363             doc.setCharacterAttributes(p0, p1 - p0, attr, replace);
 364         } else {
 365             MutableAttributeSet inputAttributes = getInputAttributes();
 366             if (replace) {
 367                 inputAttributes.removeAttributes(inputAttributes);
 368             }
 369             inputAttributes.addAttributes(attr);
 370         }
 371     }
 372 
 373     /**
 374      * Fetches the current paragraph attributes in effect
 375      * at the location of the caret, or <code>null</code> if none.
 376      *
 377      * @return the attributes
 378      */
 379     @BeanProperty(bound = false)
 380     public AttributeSet getParagraphAttributes() {
 381         StyledDocument doc = getStyledDocument();
 382         Element paragraph = doc.getParagraphElement(getCaretPosition());
 383         if (paragraph != null) {
 384             return paragraph.getAttributes();
 385         }
 386         return null;
 387     }
 388 
 389     /**
 390      * Applies the given attributes to paragraphs.  If
 391      * there is a selection, the attributes are applied
 392      * to the paragraphs that intersect the selection.
 393      * If there is no selection, the attributes are applied
 394      * to the paragraph at the current caret position.
 395      *
 396      * @param attr the non-<code>null</code> attributes
 397      * @param replace if true, replace the existing attributes first
 398      */
 399     public void setParagraphAttributes(AttributeSet attr, boolean replace) {
 400         int p0 = getSelectionStart();
 401         int p1 = getSelectionEnd();
 402         StyledDocument doc = getStyledDocument();
 403         doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
 404     }
 405 
 406     /**
 407      * Gets the input attributes for the pane.
 408      *
 409      * @return the attributes
 410      */
 411     @BeanProperty(bound = false)
 412     public MutableAttributeSet getInputAttributes() {
 413         return getStyledEditorKit().getInputAttributes();
 414     }
 415 
 416     /**
 417      * Gets the editor kit.
 418      *
 419      * @return the editor kit
 420      */
 421     protected final StyledEditorKit getStyledEditorKit() {
 422         return (StyledEditorKit) getEditorKit();
 423     }
 424 
 425     /**
 426      * @see #getUIClassID
 427      * @see #readObject
 428      */
 429     private static final String uiClassID = "TextPaneUI";
 430 
 431