]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/textpanel/TextPanel.java
go
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / textpanel / TextPanel.java
1 /*\r
2  * (C) Copyright IBM Corp. 1998-2008.  All Rights Reserved.\r
3  *\r
4  * The program is provided "as is" without any warranty express or\r
5  * implied, including the warranty of non-infringement and the implied\r
6  * warranties of merchantibility and fitness for a particular purpose.\r
7  * IBM will not be liable for any damages suffered by you as a result\r
8  * of using the Program. In no event will IBM be liable for any\r
9  * special, indirect or consequential damages or lost profits even if\r
10  * IBM has been advised of the possibility of their occurrence. IBM\r
11  * will not be liable for any third party claims against you.\r
12  */\r
13 package com.ibm.richtext.textpanel;\r
14 \r
15 import java.awt.BorderLayout;\r
16 import java.awt.Component;\r
17 import java.awt.Graphics;\r
18 import java.awt.Panel;\r
19 import java.awt.Scrollbar;\r
20 \r
21 import java.awt.datatransfer.Clipboard;\r
22 \r
23 import com.ibm.richtext.textlayout.attributes.AttributeMap;\r
24 \r
25 import com.ibm.richtext.styledtext.StyleModifier;\r
26 import com.ibm.richtext.styledtext.MConstText;\r
27 \r
28 /**\r
29  * TextPanel is an implementation of MTextPanel in an AWT Panel.\r
30  * @see MTextPanel\r
31  */\r
32 public final class TextPanel extends Panel implements MTextPanel {\r
33 \r
34     private static final long serialVersionUID = -8134791570789588104L;\r
35 \r
36     private ATextPanelImpl fImpl;\r
37     \r
38     /**\r
39      * Return a TextPanelSettings instance with all settings set\r
40      * to the default values.  Clients can modify this object;\r
41      * modifications will not affect the default values.\r
42      * @return a TextPanelSettings instance set to default values\r
43      * @see TextPanelSettings\r
44      */\r
45     public static TextPanelSettings getDefaultSettings() {\r
46 \r
47         return ATextPanelImpl.getDefaultSettings();\r
48     }\r
49 \r
50     /**\r
51      * Create a new TextPanel with the default settings.\r
52      * @param initialText the text document.  If null document text is empty.\r
53      * @param clipboard the clipboard to use for cut, copy, and paste\r
54      *  operations.  If null this panel will use a private clipboard.\r
55      */\r
56     public TextPanel(MConstText initialText,\r
57                      Clipboard clipboard) {\r
58 \r
59         this(ATextPanelImpl.fgDefaultSettings, initialText, clipboard);\r
60     }\r
61 \r
62     /**\r
63      * Create a new TextPanel.\r
64      * @param settings the settings for this TextPanel\r
65      * @param initialText the text document.  If null document text is empty.\r
66      * @param clipboard the clipboard to use for cut, copy, and paste\r
67      *  operations.  If null this panel will use a private clipboard.\r
68      * @see TextPanelSettings\r
69      */\r
70     public TextPanel(TextPanelSettings settings,\r
71                      MConstText initialText,\r
72                      Clipboard clipboard) {\r
73 \r
74         Scrollbar horzSb = null;\r
75         Scrollbar vertSb = null;\r
76         \r
77         if (settings.getScrollable()) {\r
78 \r
79             setLayout(new ScrollBarLayout());\r
80 \r
81             boolean scrollBarsVisible = settings.getScrollBarsVisible();\r
82 \r
83             if (scrollBarsVisible) {\r
84                 horzSb = new Scrollbar(Scrollbar.HORIZONTAL);\r
85                 vertSb = new Scrollbar(Scrollbar.VERTICAL);\r
86                 add("South", horzSb);\r
87                 add("East", vertSb);\r
88             }\r
89         }\r
90         else {\r
91             setLayout(new BorderLayout());\r
92         }\r
93 \r
94         fImpl = new ATextPanelImpl(new RunStrategy(),\r
95                                    settings,\r
96                                    initialText,\r
97                                    clipboard,\r
98                                    this,\r
99                                    horzSb,\r
100                                    vertSb);\r
101                                    \r
102         final FakeComponent textComponent = fImpl.getTextComponent();\r
103         \r
104         Component textHost = new Component() {\r
105             {\r
106                 textComponent.setHost(this);\r
107             }\r
108             public void addNotify() {\r
109                 super.addNotify();\r
110                 textComponent.addNotify();\r
111             }\r
112             public void paint(Graphics g) {\r
113                 textComponent.paint(g);\r
114             }\r
115             private static final long serialVersionUID = 1L;\r
116         };\r
117         \r
118         add("Center", textHost);\r
119 \r
120         textHost.requestFocus();\r
121     }\r
122 \r
123     /**\r
124      * Add the given TextPanelListener to the listeners which will\r
125      * receive update notifications from this TextPanel.\r
126      * @param listener the listener to add\r
127      */\r
128     public void addListener(TextPanelListener listener) {\r
129 \r
130         fImpl.addListener(listener);\r
131     }\r
132 \r
133     /**\r
134      * Remove the given TextPanelListener from the listeners which will\r
135      * receive update notifications from this TextPanel.\r
136      * @param listener the listener to remove\r
137      */\r
138     public void removeListener(TextPanelListener listener) {\r
139 \r
140         fImpl.removeListener(listener);\r
141     }\r
142 \r
143 //============\r
144 // Text Access\r
145 //============\r
146 \r
147     /**\r
148      * Set the document to <tt>newText</tt>.  This operation\r
149      * modifies the text in the TextPanel.  It does not modify or adopt\r
150      * <tt>newText</tt>.  This method sets the selection an insertion point at\r
151      * the end of the text.\r
152      * @param newText the text which will replace the current text.\r
153      */\r
154     public void setText(MConstText newText) {\r
155 \r
156         fImpl.setText(newText);\r
157     }\r
158 \r
159     /**\r
160      * Append the given text to the end of the document.  Equivalent to\r
161      * <tt>insert(newText, getTextLength())</tt>.\r
162      * @param newText the text to append to the document\r
163      */\r
164     public void append(MConstText newText) {\r
165 \r
166         fImpl.append(newText);\r
167     }\r
168 \r
169     /**\r
170      * Insert the given text into the document at the given position.\r
171      * Equivalent to\r
172      * <tt>replaceRange(newText, position, position)</tt>.\r
173      * @param newText the text to insert into the document.\r
174      * @param position the position in the document where the\r
175      *     text will be inserted\r
176      */\r
177     public void insert(MConstText newText, int position) {\r
178 \r
179         fImpl.insert(newText, position);\r
180     }\r
181 \r
182     /**\r
183      * Replace the given range with <tt>newText</tt>.  After this\r
184      * operation the selection range is an insertion point at the\r
185      * end of the new text.\r
186      * @param newText the text with which to replace the range\r
187      * @param start the beginning of the range to replace\r
188      * @param end the end of the range to replace\r
189      */\r
190     public void replaceRange(MConstText newText, int start, int end) {\r
191 \r
192         fImpl.replaceRange(newText, start, end);\r
193     }\r
194 \r
195     /**\r
196      * Return the length of the text document in the TextPanel.\r
197      * @return the length of the text document in the TextPanel\r
198      */\r
199     public int getTextLength() {\r
200 \r
201         return fImpl.getTextLength();\r
202     }\r
203 \r
204     /**\r
205      * Return the text document in the TextPanel.\r
206      * @return the text document in the TextPanel.\r
207      */\r
208     public MConstText getText() {\r
209 \r
210         return fImpl.getText();\r
211     }\r
212 \r
213 //============\r
214 // Selection Access\r
215 //============\r
216 \r
217     /**\r
218      * Return the offset of the start of the selection.\r
219      */\r
220     public int getSelectionStart() {\r
221 \r
222         return fImpl.getSelectionStart();\r
223     }\r
224 \r
225     /**\r
226      * Return the offset of the end of the selection.\r
227      */\r
228     public int getSelectionEnd() {\r
229 \r
230         return fImpl.getSelectionEnd();\r
231     }\r
232 \r
233     /**\r
234      * Set the beginning of the selection range.  This is\r
235      * equivalent to <tt>select(selectionStart, getSelectionEnd())</tt>.\r
236      * @param selectionStart the start of the new selection range\r
237      */\r
238     public void setSelectionStart(int selectionStart) {\r
239 \r
240         fImpl.setSelectionStart(selectionStart);\r
241     }\r
242 \r
243     /**\r
244      * Set the end of the selection range.  This is\r
245      * equivalent to <tt>select(getSelectionStart(), selectionEnd)</tt>.\r
246      * @param selectionEnd the end of the new selection range\r
247      */\r
248     public void setSelectionEnd(int selectionEnd) {\r
249 \r
250         fImpl.setSelectionEnd(selectionEnd);\r
251     }\r
252 \r
253     /**\r
254      * Set the selection range to an insertion point at the given\r
255      * offset.  This is equivalent to\r
256      * <tt>select(position, position)</tt>.\r
257      * @param position the offset of the new insertion point\r
258      */\r
259     public void setCaretPosition(int position) {\r
260 \r
261         fImpl.setCaretPosition(position);\r
262     }\r
263 \r
264     /**\r
265      * Set the selection range to the given range.  The range start\r
266      * is pinned between 0 and the text length;  the range end is pinned\r
267      * between the range start and the end of the text.  These semantics\r
268      * are identical to those of <tt>java.awt.TextComponent</tt>.\r
269      * This method has no effect if the text is not selectable.\r
270      * @param selectionStart the beginning of the selection range\r
271      * @param selectionEnd the end of the selection range\r
272      */\r
273     public void select(int selectionStart, int selectionEnd) {\r
274 \r
275         fImpl.select(selectionStart, selectionEnd);\r
276     }\r
277 \r
278     /**\r
279      * Select all of the text in the document.  This method has no effect if\r
280      * the text is not selectable.\r
281      */\r
282     public void selectAll() {\r
283 \r
284         fImpl.selectAll();\r
285     }\r
286 \r
287 \r
288 //============\r
289 // Format Width\r
290 //============\r
291 \r
292     /**\r
293      * Return the total format width, in pixels.  The format width is the\r
294      * width to which text is wrapped.\r
295      * @return the format width\r
296      */\r
297     public int getFormatWidth() {\r
298 \r
299         return fImpl.getFormatWidth();\r
300     }\r
301     \r
302     /**\r
303      * Return true if the paragraph at the given offset is left-to-right.\r
304      * @param offset an offset in the text\r
305      * @return true if the paragraph at the given offset is left-to-right\r
306      */\r
307     public boolean paragraphIsLeftToRight(int offset) {\r
308         \r
309         return fImpl.paragraphIsLeftToRight(offset);\r
310     }\r
311 \r
312     /**\r
313      * Return true if there is a change which can be undone.\r
314      * @return true if there is a change which can be undone.\r
315      */\r
316     public boolean canUndo() {\r
317 \r
318         return fImpl.canUndo();\r
319     }\r
320 \r
321     /**\r
322      * Return true if there is a change which can be redone.\r
323      * @return true if there is a change which can be redone.\r
324      */\r
325     public boolean canRedo() {\r
326 \r
327         return fImpl.canRedo();\r
328     }\r
329 \r
330     /**\r
331      * Return true if the clipboard contains contents which could be\r
332      * transfered into the text.\r
333      * @return true if the clipboard has text content.\r
334      */\r
335     public boolean clipboardNotEmpty() {\r
336 \r
337         return fImpl.clipboardNotEmpty();\r
338     }\r
339 \r
340     /**\r
341      * Return an AttributeMap of keys with default values.  The default\r
342      * values are used when displaying text for values which are not\r
343      * specified in the text.\r
344      * @return an AttributeMap of default key-value pairs\r
345      */\r
346     public AttributeMap getDefaultValues() {\r
347 \r
348         return fImpl.getDefaultValues();\r
349     }\r
350 \r
351     /**\r
352      * This method inspects the character style runs in the selection\r
353      * range (or the typing style at the insertion point).  It returns:\r
354      * <ul>\r
355      * <li>The value of <tt>key</tt>, if the value of <tt>key</tt>\r
356      * is the same in all of the style runs in the selection, or</li>\r
357      * <li><tt>MULTIPLE_VALUES</tt>, if two or more style runs have different \r
358      * values for <tt>key</tt>.</li>\r
359      * </ul>\r
360      * If a style run does not contain <tt>key</tt>,\r
361      * its value is considered to be the default style for <tt>key</tt>,\r
362      * as defined by the default values AttributeMap.  Note that if\r
363      * <tt>key</tt> does not have a default value this method may return\r
364      * null.\r
365      * This method is useful for configuring style menus.\r
366      * @param key the key used to retrieve values for comparison\r
367      * @see MTextPanel#MULTIPLE_VALUES\r
368      */\r
369     public Object getCharacterStyleOverSelection(Object key) {\r
370         \r
371         return fImpl.getCharacterStyleOverSelection(key);\r
372     }\r
373 \r
374     /**\r
375      * This method inspects the paragraph style runs in the selection\r
376      * range (or the typing style at the insertion point).  It returns:\r
377      * <ul>\r
378      * <li>The value of <tt>key</tt>, if the value of <tt>key</tt>\r
379      * is the same in all of the style runs in the selection, or</li>\r
380      * <li><tt>MULTIPLE_VALUES</tt>, if two or more style runs have \r
381      * different values for <tt>key</tt>.</li>\r
382      * </ul>\r
383      * If a style run does not contain <tt>key</tt>,\r
384      * its value is considered to be the default style for <tt>key</tt>,\r
385      * as defined by the default values AttributeMap.  Note that if\r
386      * <tt>key</tt> does not have a default value this method may return\r
387      * null.\r
388      * This method is useful for configuring style menus.\r
389      * @param key the key used to retrieve values for comparison\r
390      * @see MTextPanel#MULTIPLE_VALUES\r
391      */\r
392     public Object getParagraphStyleOverSelection(Object key) {\r
393         \r
394         return fImpl.getParagraphStyleOverSelection(key);\r
395     }\r
396 \r
397     /**\r
398      * Remove the selected text from the document and place it\r
399      * on the clipboard.  This method has no effect if the text\r
400      * is not editable, or if no text is selected.\r
401      */\r
402     public void cut() {\r
403         fImpl.cut();\r
404     }\r
405 \r
406     /**\r
407      * Place the selected text on the clipboard.  This method has\r
408      * no effect if no text is selected.\r
409      */\r
410     public void copy() {\r
411         fImpl.copy();\r
412     }\r
413 \r
414     /**\r
415      * Replace the currently selected text with the text on the clipboard.\r
416      * This method has no effect if the text is not editable, or if no\r
417      * text is on the clipboard.\r
418      */\r
419     public void paste() {\r
420         fImpl.paste();\r
421     }\r
422 \r
423     /**\r
424      * Remove selected text from the document, without altering the clipboard.\r
425      * This method has no effect if the\r
426      * text is not editable.\r
427      */\r
428     public void clear() {\r
429         fImpl.clear();\r
430     }\r
431 \r
432     /**\r
433      * Undo the most recent text change.  This method has no effect if\r
434      * there is no change to undo.\r
435      */\r
436     public void undo() {\r
437         fImpl.undo();\r
438     }\r
439 \r
440     /**\r
441      * Redo the most recent text change.  This method has no effect if\r
442      * there is no change to redo.\r
443      */\r
444     public void redo() {\r
445         fImpl.redo();\r
446     }\r
447 \r
448     /**\r
449      * Return the number of commands the command log can hold.\r
450      * @return the number of commands the command log can hold\r
451      */\r
452     public int getCommandLogSize() {\r
453 \r
454         return fImpl.getCommandLogSize();\r
455     }\r
456 \r
457     /**\r
458      * Set the number of commands the command log can hold.  All\r
459      * redoable commands are removed when this method is called.\r
460      * @param size the number of commands kept in the command log\r
461      */\r
462     public void setCommandLogSize(int size) {\r
463         fImpl.setCommandLogSize(size);\r
464     }\r
465 \r
466     /**\r
467      * Remove all commands from the command log.\r
468      */\r
469     public void clearCommandLog() {\r
470         fImpl.clearCommandLog();\r
471     }\r
472 \r
473     /**\r
474      * Modify the character styles on the selected characters.  If no characters\r
475      * are selected, modify the typing style.\r
476      * @param modifier the StyleModifier with which to modify the styles\r
477      */\r
478     public void modifyCharacterStyleOnSelection(StyleModifier modifier) {\r
479         fImpl.modifyCharacterStyleOnSelection(modifier);\r
480     }\r
481 \r
482     /**\r
483      * Modify the paragraph styles in paragraphs containing selected characters, or\r
484      * the paragraph containing the insertion point.\r
485      * @param modifier the StyleModifier with which to modify the styles\r
486      */\r
487     public void modifyParagraphStyleOnSelection(StyleModifier modifier) {\r
488         fImpl.modifyParagraphStyleOnSelection(modifier);\r
489     }\r
490 \r
491     /**\r
492      * Return the KeyRemap used to process key events.\r
493      * @return the key remap used to process key events\r
494      * @see #setKeyRemap\r
495      */\r
496     public KeyRemap getKeyRemap() {\r
497 \r
498         return fImpl.getKeyRemap();\r
499     }\r
500 \r
501     /**\r
502      * Use the given KeyRemap to map key events to characters.\r
503      * Only key\r
504      * events are affected by the remap;  other text entering the\r
505      * control (via the clipboard, for example) is not affected\r
506      * by the KeyRemap.\r
507      * <p>\r
508      * Do not pass <tt>null</tt> to this method to leave key\r
509      * events unmapped.  Instead, use <tt>KeyRemap.getIdentityRemap()</tt>\r
510      * @param remap the KeyRemap to use for mapping key events to characters\r
511      * @exception java.lang.NullPointerException if parameter is null\r
512      * @see KeyRemap\r
513      */\r
514     public void setKeyRemap(KeyRemap remap) {\r
515 \r
516         fImpl.setKeyRemap(remap);\r
517     }\r
518 \r
519     /**\r
520      * Return the modification flag of the current text change.\r
521      * @see #setModified\r
522      */\r
523     public boolean isModified() {\r
524 \r
525         return fImpl.isModified();\r
526     }\r
527 \r
528     /**\r
529      * Set the modification flag of the current text change.\r
530      */\r
531     public void setModified(boolean modified) {\r
532         \r
533         fImpl.setModified(modified);\r
534     }\r
535 \r
536     /**\r
537      * This method is for KeyEventForwarder's use only!\r
538      */\r
539     ATextPanelImpl getImpl() {\r
540         \r
541         return fImpl;\r
542     }\r
543 }\r