2 * (C) Copyright IBM Corp. 1998-2008. All Rights Reserved.
\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
13 package com.ibm.richtext.textpanel;
\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
21 import java.awt.datatransfer.Clipboard;
\r
23 import com.ibm.richtext.textlayout.attributes.AttributeMap;
\r
25 import com.ibm.richtext.styledtext.StyleModifier;
\r
26 import com.ibm.richtext.styledtext.MConstText;
\r
29 * TextPanel is an implementation of MTextPanel in an AWT Panel.
\r
32 public final class TextPanel extends Panel implements MTextPanel {
\r
34 private static final long serialVersionUID = -8134791570789588104L;
\r
36 private ATextPanelImpl fImpl;
\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
45 public static TextPanelSettings getDefaultSettings() {
\r
47 return ATextPanelImpl.getDefaultSettings();
\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
56 public TextPanel(MConstText initialText,
\r
57 Clipboard clipboard) {
\r
59 this(ATextPanelImpl.fgDefaultSettings, initialText, clipboard);
\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
70 public TextPanel(TextPanelSettings settings,
\r
71 MConstText initialText,
\r
72 Clipboard clipboard) {
\r
74 Scrollbar horzSb = null;
\r
75 Scrollbar vertSb = null;
\r
77 if (settings.getScrollable()) {
\r
79 setLayout(new ScrollBarLayout());
\r
81 boolean scrollBarsVisible = settings.getScrollBarsVisible();
\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
91 setLayout(new BorderLayout());
\r
94 fImpl = new ATextPanelImpl(new RunStrategy(),
\r
102 final FakeComponent textComponent = fImpl.getTextComponent();
\r
104 Component textHost = new Component() {
\r
106 textComponent.setHost(this);
\r
108 public void addNotify() {
\r
110 textComponent.addNotify();
\r
112 public void paint(Graphics g) {
\r
113 textComponent.paint(g);
\r
115 private static final long serialVersionUID = 1L;
\r
118 add("Center", textHost);
\r
120 textHost.requestFocus();
\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
128 public void addListener(TextPanelListener listener) {
\r
130 fImpl.addListener(listener);
\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
138 public void removeListener(TextPanelListener listener) {
\r
140 fImpl.removeListener(listener);
\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
154 public void setText(MConstText newText) {
\r
156 fImpl.setText(newText);
\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
164 public void append(MConstText newText) {
\r
166 fImpl.append(newText);
\r
170 * Insert the given text into the document at the given position.
\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
177 public void insert(MConstText newText, int position) {
\r
179 fImpl.insert(newText, position);
\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
190 public void replaceRange(MConstText newText, int start, int end) {
\r
192 fImpl.replaceRange(newText, start, end);
\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
199 public int getTextLength() {
\r
201 return fImpl.getTextLength();
\r
205 * Return the text document in the TextPanel.
\r
206 * @return the text document in the TextPanel.
\r
208 public MConstText getText() {
\r
210 return fImpl.getText();
\r
214 // Selection Access
\r
218 * Return the offset of the start of the selection.
\r
220 public int getSelectionStart() {
\r
222 return fImpl.getSelectionStart();
\r
226 * Return the offset of the end of the selection.
\r
228 public int getSelectionEnd() {
\r
230 return fImpl.getSelectionEnd();
\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
238 public void setSelectionStart(int selectionStart) {
\r
240 fImpl.setSelectionStart(selectionStart);
\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
248 public void setSelectionEnd(int selectionEnd) {
\r
250 fImpl.setSelectionEnd(selectionEnd);
\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
259 public void setCaretPosition(int position) {
\r
261 fImpl.setCaretPosition(position);
\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
273 public void select(int selectionStart, int selectionEnd) {
\r
275 fImpl.select(selectionStart, selectionEnd);
\r
279 * Select all of the text in the document. This method has no effect if
\r
280 * the text is not selectable.
\r
282 public void selectAll() {
\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
297 public int getFormatWidth() {
\r
299 return fImpl.getFormatWidth();
\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
307 public boolean paragraphIsLeftToRight(int offset) {
\r
309 return fImpl.paragraphIsLeftToRight(offset);
\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
316 public boolean canUndo() {
\r
318 return fImpl.canUndo();
\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
325 public boolean canRedo() {
\r
327 return fImpl.canRedo();
\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
335 public boolean clipboardNotEmpty() {
\r
337 return fImpl.clipboardNotEmpty();
\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
346 public AttributeMap getDefaultValues() {
\r
348 return fImpl.getDefaultValues();
\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
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
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
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
369 public Object getCharacterStyleOverSelection(Object key) {
\r
371 return fImpl.getCharacterStyleOverSelection(key);
\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
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
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
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
392 public Object getParagraphStyleOverSelection(Object key) {
\r
394 return fImpl.getParagraphStyleOverSelection(key);
\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
402 public void cut() {
\r
407 * Place the selected text on the clipboard. This method has
\r
408 * no effect if no text is selected.
\r
410 public void copy() {
\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
419 public void paste() {
\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
428 public void clear() {
\r
433 * Undo the most recent text change. This method has no effect if
\r
434 * there is no change to undo.
\r
436 public void undo() {
\r
441 * Redo the most recent text change. This method has no effect if
\r
442 * there is no change to redo.
\r
444 public void redo() {
\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
452 public int getCommandLogSize() {
\r
454 return fImpl.getCommandLogSize();
\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
462 public void setCommandLogSize(int size) {
\r
463 fImpl.setCommandLogSize(size);
\r
467 * Remove all commands from the command log.
\r
469 public void clearCommandLog() {
\r
470 fImpl.clearCommandLog();
\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
478 public void modifyCharacterStyleOnSelection(StyleModifier modifier) {
\r
479 fImpl.modifyCharacterStyleOnSelection(modifier);
\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
487 public void modifyParagraphStyleOnSelection(StyleModifier modifier) {
\r
488 fImpl.modifyParagraphStyleOnSelection(modifier);
\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
496 public KeyRemap getKeyRemap() {
\r
498 return fImpl.getKeyRemap();
\r
502 * Use the given KeyRemap to map key events to characters.
\r
504 * events are affected by the remap; other text entering the
\r
505 * control (via the clipboard, for example) is not affected
\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
514 public void setKeyRemap(KeyRemap remap) {
\r
516 fImpl.setKeyRemap(remap);
\r
520 * Return the modification flag of the current text change.
\r
521 * @see #setModified
\r
523 public boolean isModified() {
\r
525 return fImpl.isModified();
\r
529 * Set the modification flag of the current text change.
\r
531 public void setModified(boolean modified) {
\r
533 fImpl.setModified(modified);
\r
537 * This method is for KeyEventForwarder's use only!
\r
539 ATextPanelImpl getImpl() {
\r