001: /*
002: * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
003: *
004: * The program is provided "as is" without any warranty express or
005: * implied, including the warranty of non-infringement and the implied
006: * warranties of merchantibility and fitness for a particular purpose.
007: * IBM will not be liable for any damages suffered by you as a result
008: * of using the Program. In no event will IBM be liable for any
009: * special, indirect or consequential damages or lost profits even if
010: * IBM has been advised of the possibility of their occurrence. IBM
011: * will not be liable for any third party claims against you.
012: */
013: package com.ibm.richtext.textpanel;
014:
015: import java.awt.Graphics;
016: import java.awt.Rectangle;
017:
018: import java.awt.event.FocusEvent;
019: import java.awt.event.KeyEvent;
020: import java.awt.event.MouseEvent;
021:
022: /** A class that handles events for a BehaviorOwner.
023: * A behavior enacpsulates some piece of the event-handling logic for a component.
024: * This allows the client to separate event-handling logic out into separate classes
025: * according to function, or to dynamically change the way a component handles
026: * events without adding a lot of special-case code to the panel itself.
027: * Behaviors are stored in a linked list, and all behaviors get a crack at an event before
028: * the owner gets a crack at them (right now, we rely on objects that implement
029: * BehaviorOwner to support these semantics).
030: * Behavior provides all the same event-handling functions that Component provides, and
031: * they all have exactly the same syntax and semantics. */
032: abstract class Behavior {
033:
034: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
035: private Behavior fNextBehavior = null;
036: private BehaviorOwner fOwner = null;
037:
038: static class EventType {
039:
040: EventType() {
041: }
042: }
043:
044: // events - should these be in TextPanel (or elsewhere)?
045:
046: // This event's WHAT parameter is a TextRange instance
047: static final EventType SELECT = new EventType();
048:
049: // No WHAT param for these:
050: static final EventType CUT = new EventType();
051: static final EventType COPY = new EventType();
052: static final EventType PASTE = new EventType();
053: static final EventType CLEAR = new EventType();
054: static final EventType UNDO = new EventType();
055: static final EventType REDO = new EventType();
056: static final EventType CLEAR_COMMAND_LOG = new EventType();
057:
058: // WHAT param is a StyleModifier
059: static final EventType CHARACTER_STYLE_MOD = new EventType();
060: static final EventType PARAGRAPH_STYLE_MOD = new EventType();
061:
062: // With this event, values of the WHAT parameter are
063: // either Boolean.TRUE or Boolean.FALSE
064: static final EventType SET_MODIFIED = new EventType();
065:
066: // WHAT param is a TextReplacement
067: static final EventType REPLACE = new EventType();
068:
069: // WHAT param is an Integer
070: static final EventType SET_COMMAND_LOG_SIZE = new EventType();
071:
072: public Behavior() {
073: }
074:
075: public void addToOwner(BehaviorOwner owner) {
076: removeFromOwner();
077: fOwner = owner;
078: setNextBehavior(owner.getBehavior());
079: owner.setBehavior(this );
080: }
081:
082: public boolean focusGained(FocusEvent e) {
083: if (fNextBehavior != null)
084: return fNextBehavior.focusGained(e);
085: else
086: return false;
087: }
088:
089: public boolean focusLost(FocusEvent e) {
090: if (fNextBehavior != null)
091: return fNextBehavior.focusLost(e);
092: else
093: return false;
094: }
095:
096: public boolean keyPressed(KeyEvent e) {
097: if (fNextBehavior != null)
098: return fNextBehavior.keyPressed(e);
099: else
100: return false;
101: }
102:
103: public boolean keyTyped(KeyEvent e) {
104:
105: if (fNextBehavior != null) {
106: return fNextBehavior.keyTyped(e);
107: } else {
108: return false;
109: }
110: }
111:
112: public boolean keyReleased(KeyEvent e) {
113: if (fNextBehavior != null)
114: return fNextBehavior.keyReleased(e);
115: else
116: return false;
117: }
118:
119: public boolean mouseDragged(MouseEvent e) {
120: if (fNextBehavior != null)
121: return fNextBehavior.mouseDragged(e);
122: else
123: return false;
124: }
125:
126: public boolean mouseEntered(MouseEvent e) {
127: if (fNextBehavior != null)
128: return fNextBehavior.mouseEntered(e);
129: else
130: return false;
131: }
132:
133: public boolean mouseExited(MouseEvent e) {
134: if (fNextBehavior != null)
135: return fNextBehavior.mouseExited(e);
136: else
137: return false;
138: }
139:
140: public boolean mouseMoved(MouseEvent e) {
141: if (fNextBehavior != null)
142: return fNextBehavior.mouseMoved(e);
143: else
144: return false;
145: }
146:
147: public boolean mousePressed(MouseEvent e) {
148: if (fNextBehavior != null)
149: return fNextBehavior.mousePressed(e);
150: else
151: return false;
152: }
153:
154: public boolean mouseReleased(MouseEvent e) {
155: if (fNextBehavior != null)
156: return fNextBehavior.mouseReleased(e);
157: else
158: return false;
159: }
160:
161: public final Behavior nextBehavior() {
162: return fNextBehavior;
163: }
164:
165: public boolean paint(Graphics g, Rectangle drawRect) {
166: if (fNextBehavior != null)
167: return fNextBehavior.paint(g, drawRect);
168: else
169: return false;
170: }
171:
172: public void removeFromOwner() {
173: if (fOwner != null) {
174: if (fOwner.getBehavior() == this )
175: fOwner.setBehavior(nextBehavior());
176: else {
177: Behavior current = fOwner.getBehavior();
178:
179: while (current != null
180: && current.nextBehavior() != this )
181: current = current.nextBehavior();
182: if (current != null)
183: current.setNextBehavior(nextBehavior());
184: }
185: setNextBehavior(null);
186: fOwner = null;
187: }
188: }
189:
190: public final void setNextBehavior(Behavior next) {
191: fNextBehavior = next;
192: }
193:
194: public boolean textControlEventOccurred(EventType event, Object data) {
195: if (fNextBehavior != null)
196: return fNextBehavior.textControlEventOccurred(event, data);
197: else
198: return false;
199: }
200: }
|