2 * (C) Copyright IBM Corp. 1998-2004. 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.Graphics;
\r
16 import java.awt.Rectangle;
\r
18 import java.awt.event.FocusEvent;
\r
19 import java.awt.event.KeyEvent;
\r
20 import java.awt.event.MouseEvent;
\r
22 /** A class that handles events for a BehaviorOwner.
\r
23 * A behavior enacpsulates some piece of the event-handling logic for a component.
\r
24 * This allows the client to separate event-handling logic out into separate classes
\r
25 * according to function, or to dynamically change the way a component handles
\r
26 * events without adding a lot of special-case code to the panel itself.
\r
27 * Behaviors are stored in a linked list, and all behaviors get a crack at an event before
\r
28 * the owner gets a crack at them (right now, we rely on objects that implement
\r
29 * BehaviorOwner to support these semantics).
\r
30 * Behavior provides all the same event-handling functions that Component provides, and
\r
31 * they all have exactly the same syntax and semantics. */
\r
32 abstract class Behavior {
\r
34 static final String COPYRIGHT =
\r
35 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
\r
36 private Behavior fNextBehavior = null;
\r
37 private BehaviorOwner fOwner = null;
\r
39 static class EventType {
\r
45 // events - should these be in TextPanel (or elsewhere)?
\r
47 // This event's WHAT parameter is a TextRange instance
\r
48 static final EventType SELECT = new EventType();
\r
50 // No WHAT param for these:
\r
51 static final EventType CUT = new EventType();
\r
52 static final EventType COPY = new EventType();
\r
53 static final EventType PASTE = new EventType();
\r
54 static final EventType CLEAR = new EventType();
\r
55 static final EventType UNDO = new EventType();
\r
56 static final EventType REDO = new EventType();
\r
57 static final EventType CLEAR_COMMAND_LOG = new EventType();
\r
59 // WHAT param is a StyleModifier
\r
60 static final EventType CHARACTER_STYLE_MOD = new EventType();
\r
61 static final EventType PARAGRAPH_STYLE_MOD = new EventType();
\r
63 // With this event, values of the WHAT parameter are
\r
64 // either Boolean.TRUE or Boolean.FALSE
\r
65 static final EventType SET_MODIFIED = new EventType();
\r
67 // WHAT param is a TextReplacement
\r
68 static final EventType REPLACE = new EventType();
\r
70 // WHAT param is an Integer
\r
71 static final EventType SET_COMMAND_LOG_SIZE = new EventType();
\r
76 public void addToOwner(BehaviorOwner owner) {
\r
79 setNextBehavior(owner.getBehavior());
\r
80 owner.setBehavior(this);
\r
83 public boolean focusGained(FocusEvent e) {
\r
84 if (fNextBehavior != null)
\r
85 return fNextBehavior.focusGained(e);
\r
90 public boolean focusLost(FocusEvent e) {
\r
91 if (fNextBehavior != null)
\r
92 return fNextBehavior.focusLost(e);
\r
97 public boolean keyPressed(KeyEvent e) {
\r
98 if (fNextBehavior != null)
\r
99 return fNextBehavior.keyPressed(e);
\r
104 public boolean keyTyped(KeyEvent e) {
\r
106 if (fNextBehavior != null) {
\r
107 return fNextBehavior.keyTyped(e);
\r
114 public boolean keyReleased(KeyEvent e) {
\r
115 if (fNextBehavior != null)
\r
116 return fNextBehavior.keyReleased(e);
\r
121 public boolean mouseDragged(MouseEvent e) {
\r
122 if (fNextBehavior != null)
\r
123 return fNextBehavior.mouseDragged(e);
\r
128 public boolean mouseEntered(MouseEvent e) {
\r
129 if (fNextBehavior != null)
\r
130 return fNextBehavior.mouseEntered(e);
\r
135 public boolean mouseExited(MouseEvent e) {
\r
136 if (fNextBehavior != null)
\r
137 return fNextBehavior.mouseExited(e);
\r
142 public boolean mouseMoved(MouseEvent e) {
\r
143 if (fNextBehavior != null)
\r
144 return fNextBehavior.mouseMoved(e);
\r
149 public boolean mousePressed(MouseEvent e) {
\r
150 if (fNextBehavior != null)
\r
151 return fNextBehavior.mousePressed(e);
\r
156 public boolean mouseReleased(MouseEvent e) {
\r
157 if (fNextBehavior != null)
\r
158 return fNextBehavior.mouseReleased(e);
\r
163 public final Behavior nextBehavior() {
\r
164 return fNextBehavior;
\r
167 public boolean paint(Graphics g, Rectangle drawRect) {
\r
168 if (fNextBehavior != null)
\r
169 return fNextBehavior.paint(g, drawRect);
\r
174 public void removeFromOwner() {
\r
175 if (fOwner != null) {
\r
176 if (fOwner.getBehavior() == this)
\r
177 fOwner.setBehavior(nextBehavior());
\r
179 Behavior current = fOwner.getBehavior();
\r
181 while (current != null && current.nextBehavior() != this)
\r
182 current = current.nextBehavior();
\r
183 if (current != null)
\r
184 current.setNextBehavior(nextBehavior());
\r
186 setNextBehavior(null);
\r
191 public final void setNextBehavior(Behavior next) {
\r
192 fNextBehavior = next;
\r
195 public boolean textControlEventOccurred(EventType event, Object data) {
\r
196 if (fNextBehavior != null)
\r
197 return fNextBehavior.textControlEventOccurred(event, data);
\r