1 |
/* JTextPane.java -- |
/* JTextPane.java -- A powerful text widget supporting styled text |
2 |
Copyright (C) 2002, 2004 Free Software Foundation, Inc. |
Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
55 |
import javax.swing.text.StyledEditorKit; |
import javax.swing.text.StyledEditorKit; |
56 |
|
|
57 |
/** |
/** |
58 |
* JTextPane |
* A powerful text component that supports styled content as well as |
59 |
|
* embedding images and components. It is entirely based on a |
60 |
|
* {@link StyledDocument} content model and a {@link StyledEditorKit}. |
61 |
* |
* |
62 |
* @author Roman Kennke (roman@kennke.org) |
* @author Roman Kennke (roman@kennke.org) |
63 |
* @author Andrew Selkirk |
* @author Andrew Selkirk |
66 |
extends JEditorPane |
extends JEditorPane |
67 |
{ |
{ |
68 |
/** |
/** |
69 |
* Constructor JTextPane |
* Creates a new <code>JTextPane</code> with a <code>null</code> document. |
70 |
*/ |
*/ |
71 |
public JTextPane() |
public JTextPane() |
72 |
{ |
{ |
75 |
} |
} |
76 |
|
|
77 |
/** |
/** |
78 |
* Constructor JTextPane |
* Creates a new <code>JTextPane</code> and sets the specified |
79 |
|
* <code>document</code>. |
80 |
* |
* |
81 |
* @param document TODO |
* @param document the content model to use |
82 |
*/ |
*/ |
83 |
public JTextPane(StyledDocument document) |
public JTextPane(StyledDocument document) |
84 |
{ |
{ |
87 |
} |
} |
88 |
|
|
89 |
/** |
/** |
90 |
* getUIClassID |
* Returns the UI class ID. This is <code>TextPaneUI</code>. |
91 |
* |
* |
92 |
* @returns String |
* @return <code>TextPaneUI</code> |
93 |
*/ |
*/ |
94 |
public String getUIClassID() |
public String getUIClassID() |
95 |
{ |
{ |
97 |
} |
} |
98 |
|
|
99 |
/** |
/** |
100 |
* setDocument |
* Sets the content model for this <code>JTextPane</code>. |
101 |
|
* <code>JTextPane</code> can only be used with {@link StyledDocument}s, |
102 |
|
* if you try to set a different type of <code>Document</code>, an |
103 |
|
* <code>IllegalArgumentException</code> is thrown. |
104 |
* |
* |
105 |
* @param document TODO |
* @param document the content model to set |
106 |
|
* |
107 |
|
* @throws IllegalArgumentException if <code>document</code> is not an |
108 |
|
* instance of <code>StyledDocument</code> |
109 |
|
* |
110 |
|
* @see {@link #setStyledDocument} |
111 |
*/ |
*/ |
112 |
public void setDocument(Document document) |
public void setDocument(Document document) |
113 |
{ |
{ |
114 |
if (document != null && !(document instanceof StyledDocument)) |
if (document != null && !(document instanceof StyledDocument)) |
115 |
throw new IllegalArgumentException("JTextPane can only handle StyledDocuments"); // TODO: Figure out exception message |
throw new IllegalArgumentException |
116 |
|
("JTextPane can only handle StyledDocuments"); |
117 |
|
|
118 |
setStyledDocument((StyledDocument) document); |
setStyledDocument((StyledDocument) document); |
119 |
} |
} |
120 |
|
|
121 |
/** |
/** |
122 |
* getStyledDocument |
* Returns the {@link StyledDocument} that is the content model for |
123 |
|
* this <code>JTextPane</code>. This is a typed wrapper for |
124 |
|
* {@link #getDocument}. |
125 |
* |
* |
126 |
* @returns StyledDocument |
* @return the content model of this <code>JTextPane</code> |
127 |
*/ |
*/ |
128 |
public StyledDocument getStyledDocument() |
public StyledDocument getStyledDocument() |
129 |
{ |
{ |
131 |
} |
} |
132 |
|
|
133 |
/** |
/** |
134 |
* setStyledDocument |
* Sets the content model for this <code>JTextPane</code>. |
135 |
* |
* |
136 |
* @param document TODO |
* @param document the content model to set |
137 |
*/ |
*/ |
138 |
public void setStyledDocument(StyledDocument document) |
public void setStyledDocument(StyledDocument document) |
139 |
{ |
{ |
141 |
} |
} |
142 |
|
|
143 |
/** |
/** |
144 |
* replaceSelection |
* Replaces the currently selected text with the specified |
145 |
|
* <code>content</code>. If there is no selected text, this results |
146 |
|
* in a simple insertion at the current caret position. If there is |
147 |
|
* no <code>content</code> specified, this results in the selection |
148 |
|
* beeing deleted. |
149 |
* |
* |
150 |
* @param content TODO |
* @param content the text with which the selection is replaced |
151 |
*/ |
*/ |
152 |
public void replaceSelection(String content) |
public void replaceSelection(String content) |
153 |
{ |
{ |
191 |
} |
} |
192 |
|
|
193 |
/** |
/** |
194 |
* insertComponent |
* Inserts an AWT or Swing component into the text at the current caret |
195 |
|
* position. |
196 |
* |
* |
197 |
* @param component TODO |
* @param component the component to be inserted |
198 |
*/ |
*/ |
199 |
public void insertComponent(Component component) |
public void insertComponent(Component component) |
200 |
{ |
{ |
204 |
} |
} |
205 |
|
|
206 |
/** |
/** |
207 |
* insertIcon |
* Inserts an <code>Icon</code> into the text at the current caret position. |
208 |
* |
* |
209 |
* @param icon TODO |
* @param icon the <code>Icon</code> to be inserted |
210 |
*/ |
*/ |
211 |
public void insertIcon(Icon icon) |
public void insertIcon(Icon icon) |
212 |
{ |
{ |
216 |
} |
} |
217 |
|
|
218 |
/** |
/** |
219 |
* addStyle |
* Adds a style into the style hierarchy. Unspecified style attributes |
220 |
|
* can be resolved in the <code>parent</code> style, if one is specified. |
221 |
* |
* |
222 |
* @param nm TODO |
* While it is legal to add nameless styles (<code>nm == null</code), |
223 |
* @param parent TODO |
* you must be aware that the client application is then responsible |
224 |
|
* for managing the style hierarchy, since unnamed styles cannot be |
225 |
|
* looked up by their name. |
226 |
|
* |
227 |
|
* @param nm the name of the style or <code>null</code> if the style should |
228 |
|
* be unnamed |
229 |
|
* @param parent the parent in which unspecified style attributes are |
230 |
|
* resolved, or <code>null</code> if that is not necessary |
231 |
* |
* |
232 |
* @returns Style |
* @return the newly created <code>Style</code> |
233 |
*/ |
*/ |
234 |
public Style addStyle(String nm, Style parent) |
public Style addStyle(String nm, Style parent) |
235 |
{ |
{ |
237 |
} |
} |
238 |
|
|
239 |
/** |
/** |
240 |
* removeStyle |
* Removes a named <code>Style</code> from the style hierarchy. |
241 |
* |
* |
242 |
* @param nm TODO |
* @param nm the name of the <code>Style</code> to be removed |
243 |
*/ |
*/ |
244 |
public void removeStyle(String nm) |
public void removeStyle(String nm) |
245 |
{ |
{ |
247 |
} |
} |
248 |
|
|
249 |
/** |
/** |
250 |
* getStyle |
* Looks up and returns a named <code>Style</code>. |
251 |
* |
* |
252 |
* @param nm TODO |
* @param nm the name of the <code>Style</code> |
253 |
* |
* |
254 |
* @returns Style |
* @return the found <code>Style</code> of <code>null</code> if no such |
255 |
|
* <code>Style</code> exists |
256 |
*/ |
*/ |
257 |
public Style getStyle(String nm) |
public Style getStyle(String nm) |
258 |
{ |
{ |
260 |
} |
} |
261 |
|
|
262 |
/** |
/** |
263 |
* getLogicalStyle |
* Returns the logical style of the paragraph at the current caret position. |
264 |
* |
* |
265 |
* @returns Style |
* @return the logical style of the paragraph at the current caret position |
266 |
*/ |
*/ |
267 |
public Style getLogicalStyle() |
public Style getLogicalStyle() |
268 |
{ |
{ |
270 |
} |
} |
271 |
|
|
272 |
/** |
/** |
273 |
* setLogicalStyle |
* Sets the logical style for the paragraph at the current caret position. |
274 |
* |
* |
275 |
* @param style TODO |
* @param style the style to set for the current paragraph |
276 |
*/ |
*/ |
277 |
public void setLogicalStyle(Style style) |
public void setLogicalStyle(Style style) |
278 |
{ |
{ |
280 |
} |
} |
281 |
|
|
282 |
/** |
/** |
283 |
* getCharacterAttributes |
* Returns the text attributes for the character at the current caret |
284 |
|
* position. |
285 |
* |
* |
286 |
* @returns AttributeSet |
* @return the text attributes for the character at the current caret |
287 |
|
* position |
288 |
*/ |
*/ |
289 |
public AttributeSet getCharacterAttributes() |
public AttributeSet getCharacterAttributes() |
290 |
{ |
{ |
294 |
} |
} |
295 |
|
|
296 |
/** |
/** |
297 |
* setCharacterAttributes |
* Sets text attributes for the current selection. If there is no selection |
298 |
|
* the text attributes are applied to newly inserted text |
299 |
|
* |
300 |
|
* @param attribute the text attributes to set |
301 |
|
* @param replace if <code>true</code>, the attributes of the current |
302 |
|
* selection are overridden, otherwise they are merged |
303 |
* |
* |
304 |
* @param attribute TODO |
* @see {@link #getInputAttributes} |
|
* @param replace TODO |
|
305 |
*/ |
*/ |
306 |
public void setCharacterAttributes(AttributeSet attribute, |
public void setCharacterAttributes(AttributeSet attribute, |
307 |
boolean replace) |
boolean replace) |
322 |
} |
} |
323 |
|
|
324 |
/** |
/** |
325 |
* getParagraphAttributes |
* Returns the text attributes of the paragraph at the current caret |
326 |
|
* position. |
327 |
* |
* |
328 |
* @returns AttributeSet |
* @return the attributes of the paragraph at the current caret position |
329 |
*/ |
*/ |
330 |
public AttributeSet getParagraphAttributes() |
public AttributeSet getParagraphAttributes() |
331 |
{ |
{ |
335 |
} |
} |
336 |
|
|
337 |
/** |
/** |
338 |
* setParagraphAttributes |
* Sets text attributes for the paragraph at the current selection. |
339 |
* |
* If there is no selection the text attributes are applied to |
340 |
* @param attribute TODO |
* the paragraph at the current caret position. |
341 |
* @param replace TODO |
* |
342 |
|
* @param attribute the text attributes to set |
343 |
|
* @param replace if <code>true</code>, the attributes of the current |
344 |
|
* selection are overridden, otherwise they are merged |
345 |
*/ |
*/ |
346 |
public void setParagraphAttributes(AttributeSet attribute, |
public void setParagraphAttributes(AttributeSet attribute, |
347 |
boolean replace) |
boolean replace) |
350 |
} |
} |
351 |
|
|
352 |
/** |
/** |
353 |
* getInputAttributes |
* Returns the attributes that are applied to newly inserted text. |
354 |
|
* This is a {@link MutableAttributeSet}, so you can easily modify these |
355 |
|
* attributes. |
356 |
* |
* |
357 |
* @returns MutableAttributeSet |
* @return the attributes that are applied to newly inserted text |
358 |
*/ |
*/ |
359 |
public MutableAttributeSet getInputAttributes() |
public MutableAttributeSet getInputAttributes() |
360 |
{ |
{ |
362 |
} |
} |
363 |
|
|
364 |
/** |
/** |
365 |
* getStyledEditorKit |
* Returns the {@link StyledEditorKit} that is currently used by this |
366 |
|
* <code>JTextPane</code>. |
367 |
* |
* |
368 |
* @returns StyledEditorKit |
* @return the current <code>StyledEditorKit</code> of this |
369 |
|
* <code>JTextPane</code> |
370 |
*/ |
*/ |
371 |
protected final StyledEditorKit getStyledEditorKit() |
protected final StyledEditorKit getStyledEditorKit() |
372 |
{ |
{ |
374 |
} |
} |
375 |
|
|
376 |
/** |
/** |
377 |
* createDefaultEditorKit |
* Creates the default {@link EditorKit} that is used in |
378 |
|
* <code>JTextPane</code>s. This is an instance of {@link StyledEditorKit}. |
379 |
* |
* |
380 |
* @returns EditorKit |
* @return the default {@link EditorKit} that is used in |
381 |
|
* <code>JTextPane</code>s |
382 |
*/ |
*/ |
383 |
protected EditorKit createDefaultEditorKit() |
protected EditorKit createDefaultEditorKit() |
384 |
{ |
{ |
386 |
} |
} |
387 |
|
|
388 |
/** |
/** |
389 |
* setEditorKit |
* Sets the {@link EditorKit} to use for this <code>JTextPane</code>. |
390 |
|
* <code>JTextPane</code>s can only handle {@link StyledEditorKit}s, |
391 |
|
* if client programs try to set a different type of <code>EditorKit</code> |
392 |
|
* then an IllegalArgumentException is thrown |
393 |
|
* |
394 |
|
* @param editor the <code>EditorKit</code> to set |
395 |
* |
* |
396 |
* @param editor TODO |
* @throws IllegalArgumentException if <code>editor</code> is no |
397 |
|
* <code>StyledEditorKit</code> |
398 |
*/ |
*/ |
399 |
public final void setEditorKit(EditorKit editor) |
public final void setEditorKit(EditorKit editor) |
400 |
{ |
{ |
401 |
if (!(editor instanceof StyledEditorKit)) |
if (!(editor instanceof StyledEditorKit)) |
402 |
throw new IllegalArgumentException("JTextPanes can only handle StyledEditorKits"); // TODO: Figure out exception message |
throw new IllegalArgumentException |
403 |
|
("JTextPanes can only handle StyledEditorKits"); |
404 |
super.setEditorKit(editor); |
super.setEditorKit(editor); |
405 |
} |
} |
406 |
|
|
407 |
/** |
/** |
408 |
* paramString |
* Returns a param string that can be used for debugging. |
409 |
* |
* |
410 |
* @returns String |
* @return a param string that can be used for debugging. |
411 |
*/ |
*/ |
412 |
protected String paramString() |
protected String paramString() |
413 |
{ |
{ |