1 /* JTextPane.java -- A powerful text widget supporting styled text
2 Copyright (C) 2002, 2004, 2005 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)
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
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
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. */
41 import java
.awt
.Component
;
43 import javax
.swing
.text
.AttributeSet
;
44 import javax
.swing
.text
.BadLocationException
;
45 import javax
.swing
.text
.Caret
;
46 import javax
.swing
.text
.Document
;
47 import javax
.swing
.text
.EditorKit
;
48 import javax
.swing
.text
.Element
;
49 import javax
.swing
.text
.MutableAttributeSet
;
50 import javax
.swing
.text
.SimpleAttributeSet
;
51 import javax
.swing
.text
.Style
;
52 import javax
.swing
.text
.StyleConstants
;
53 import javax
.swing
.text
.StyledDocument
;
54 import javax
.swing
.text
.StyledEditorKit
;
57 * A powerful text component that supports styled content as well as
58 * embedding images and components. It is entirely based on a
59 * {@link StyledDocument} content model and a {@link StyledEditorKit}.
61 * @author Roman Kennke (roman@kennke.org)
62 * @author Andrew Selkirk
64 public class JTextPane
68 * Creates a new <code>JTextPane</code> with a <code>null</code> document.
76 * Creates a new <code>JTextPane</code> and sets the specified
77 * <code>document</code>.
79 * @param document the content model to use
81 public JTextPane(StyledDocument document
)
84 setStyledDocument(document
);
88 * Returns the UI class ID. This is <code>TextPaneUI</code>.
90 * @return <code>TextPaneUI</code>
92 public String
getUIClassID()
98 * Sets the content model for this <code>JTextPane</code>.
99 * <code>JTextPane</code> can only be used with {@link StyledDocument}s,
100 * if you try to set a different type of <code>Document</code>, an
101 * <code>IllegalArgumentException</code> is thrown.
103 * @param document the content model to set
105 * @throws IllegalArgumentException if <code>document</code> is not an
106 * instance of <code>StyledDocument</code>
108 * @see #setStyledDocument
110 public void setDocument(Document document
)
112 if (document
!= null && !(document
instanceof StyledDocument
))
113 throw new IllegalArgumentException
114 ("JTextPane can only handle StyledDocuments");
116 setStyledDocument((StyledDocument
) document
);
120 * Returns the {@link StyledDocument} that is the content model for
121 * this <code>JTextPane</code>. This is a typed wrapper for
122 * {@link #getDocument()}.
124 * @return the content model of this <code>JTextPane</code>
126 public StyledDocument
getStyledDocument()
128 return (StyledDocument
) super.getDocument();
132 * Sets the content model for this <code>JTextPane</code>.
134 * @param document the content model to set
136 public void setStyledDocument(StyledDocument document
)
138 super.setDocument(document
);
142 * Replaces the currently selected text with the specified
143 * <code>content</code>. If there is no selected text, this results
144 * in a simple insertion at the current caret position. If there is
145 * no <code>content</code> specified, this results in the selection
148 * @param content the text with which the selection is replaced
150 public void replaceSelection(String content
)
152 Caret caret
= getCaret();
153 StyledDocument doc
= getStyledDocument();
155 int dot
= caret
.getDot();
156 int mark
= caret
.getMark();
158 // If content is empty delete selection.
167 int start
= getSelectionStart();
168 int end
= getSelectionEnd();
169 int contentLength
= content
.length();
171 // Remove selected text.
173 doc
.remove(start
, end
- start
);
176 doc
.insertString(start
, content
, null);
177 // Set attributes for inserted text
178 doc
.setCharacterAttributes(start
, contentLength
, getInputAttributes(),
182 catch (BadLocationException e
)
184 throw new AssertionError
185 ("No BadLocationException should be thrown here");
190 * Inserts an AWT or Swing component into the text at the current caret
193 * @param component the component to be inserted
195 public void insertComponent(Component component
)
197 SimpleAttributeSet atts
= new SimpleAttributeSet();
198 atts
.addAttribute(StyleConstants
.ComponentAttribute
, component
);
199 atts
.addAttribute(StyleConstants
.NameAttribute
,
200 StyleConstants
.ComponentElementName
);
203 getDocument().insertString(getCaret().getDot(), " ", atts
);
205 catch (BadLocationException ex
)
207 AssertionError err
= new AssertionError("Unexpected bad location");
214 * Inserts an <code>Icon</code> into the text at the current caret position.
216 * @param icon the <code>Icon</code> to be inserted
218 public void insertIcon(Icon icon
)
220 SimpleAttributeSet atts
= new SimpleAttributeSet();
221 atts
.addAttribute(StyleConstants
.IconAttribute
, icon
);
222 atts
.addAttribute(StyleConstants
.NameAttribute
,
223 StyleConstants
.IconElementName
);
226 getDocument().insertString(getCaret().getDot(), " ", atts
);
228 catch (BadLocationException ex
)
230 AssertionError err
= new AssertionError("Unexpected bad location");
237 * Adds a style into the style hierarchy. Unspecified style attributes
238 * can be resolved in the <code>parent</code> style, if one is specified.
240 * While it is legal to add nameless styles (<code>nm == null</code),
241 * you must be aware that the client application is then responsible
242 * for managing the style hierarchy, since unnamed styles cannot be
243 * looked up by their name.
245 * @param nm the name of the style or <code>null</code> if the style should
247 * @param parent the parent in which unspecified style attributes are
248 * resolved, or <code>null</code> if that is not necessary
250 * @return the newly created <code>Style</code>
252 public Style
addStyle(String nm
, Style parent
)
254 return getStyledDocument().addStyle(nm
, parent
);
258 * Removes a named <code>Style</code> from the style hierarchy.
260 * @param nm the name of the <code>Style</code> to be removed
262 public void removeStyle(String nm
)
264 getStyledDocument().removeStyle(nm
);
268 * Looks up and returns a named <code>Style</code>.
270 * @param nm the name of the <code>Style</code>
272 * @return the found <code>Style</code> of <code>null</code> if no such
273 * <code>Style</code> exists
275 public Style
getStyle(String nm
)
277 return getStyledDocument().getStyle(nm
);
281 * Returns the logical style of the paragraph at the current caret position.
283 * @return the logical style of the paragraph at the current caret position
285 public Style
getLogicalStyle()
287 return getStyledDocument().getLogicalStyle(getCaretPosition());
291 * Sets the logical style for the paragraph at the current caret position.
293 * @param style the style to set for the current paragraph
295 public void setLogicalStyle(Style style
)
297 getStyledDocument().setLogicalStyle(getCaretPosition(), style
);
301 * Returns the text attributes for the character at the current caret
304 * @return the text attributes for the character at the current caret
307 public AttributeSet
getCharacterAttributes()
309 StyledDocument doc
= getStyledDocument();
310 Element el
= doc
.getCharacterElement(getCaretPosition());
311 return el
.getAttributes();
315 * Sets text attributes for the current selection. If there is no selection
316 * the text attributes are applied to newly inserted text
318 * @param attribute the text attributes to set
319 * @param replace if <code>true</code>, the attributes of the current
320 * selection are overridden, otherwise they are merged
322 * @see #getInputAttributes
324 public void setCharacterAttributes(AttributeSet attribute
,
327 int dot
= getCaret().getDot();
328 int start
= getSelectionStart();
329 int end
= getSelectionEnd();
330 if (start
== dot
&& end
== dot
)
331 // There is no selection, update insertAttributes instead
333 MutableAttributeSet inputAttributes
=
334 getStyledEditorKit().getInputAttributes();
335 inputAttributes
.addAttributes(attribute
);
338 getStyledDocument().setCharacterAttributes(start
, end
- start
, attribute
,
343 * Returns the text attributes of the paragraph at the current caret
346 * @return the attributes of the paragraph at the current caret position
348 public AttributeSet
getParagraphAttributes()
350 StyledDocument doc
= getStyledDocument();
351 Element el
= doc
.getParagraphElement(getCaretPosition());
352 return el
.getAttributes();
356 * Sets text attributes for the paragraph at the current selection.
357 * If there is no selection the text attributes are applied to
358 * the paragraph at the current caret position.
360 * @param attribute the text attributes to set
361 * @param replace if <code>true</code>, the attributes of the current
362 * selection are overridden, otherwise they are merged
364 public void setParagraphAttributes(AttributeSet attribute
,
371 * Returns the attributes that are applied to newly inserted text.
372 * This is a {@link MutableAttributeSet}, so you can easily modify these
375 * @return the attributes that are applied to newly inserted text
377 public MutableAttributeSet
getInputAttributes()
379 return getStyledEditorKit().getInputAttributes();
383 * Returns the {@link StyledEditorKit} that is currently used by this
384 * <code>JTextPane</code>.
386 * @return the current <code>StyledEditorKit</code> of this
387 * <code>JTextPane</code>
389 protected final StyledEditorKit
getStyledEditorKit()
391 return (StyledEditorKit
) getEditorKit();
395 * Creates the default {@link EditorKit} that is used in
396 * <code>JTextPane</code>s. This is an instance of {@link StyledEditorKit}.
398 * @return the default {@link EditorKit} that is used in
399 * <code>JTextPane</code>s
401 protected EditorKit
createDefaultEditorKit()
403 return new StyledEditorKit();
407 * Sets the {@link EditorKit} to use for this <code>JTextPane</code>.
408 * <code>JTextPane</code>s can only handle {@link StyledEditorKit}s,
409 * if client programs try to set a different type of <code>EditorKit</code>
410 * then an IllegalArgumentException is thrown
412 * @param editor the <code>EditorKit</code> to set
414 * @throws IllegalArgumentException if <code>editor</code> is no
415 * <code>StyledEditorKit</code>
417 public final void setEditorKit(EditorKit editor
)
419 if (!(editor
instanceof StyledEditorKit
))
420 throw new IllegalArgumentException
421 ("JTextPanes can only handle StyledEditorKits");
422 super.setEditorKit(editor
);
426 * Returns a param string that can be used for debugging.
428 * @return a param string that can be used for debugging.
430 protected String
paramString()
432 return super.paramString(); // TODO