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.event.FocusEvent;
\r
16 import java.awt.event.KeyEvent;
\r
17 import java.awt.event.MouseEvent;
\r
19 import com.ibm.richtext.textformat.TextOffset;
\r
21 final class SelectionDragInteractor extends Behavior implements Runnable {
\r
23 static final String COPYRIGHT =
\r
24 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
\r
25 private TextComponent fTextComponent;
\r
26 private TextSelection fSelection;
\r
27 private RunStrategy fRunStrategy;
\r
29 private TextOffset fAnchorStart; // aliases text offsets - client beware
\r
30 private TextOffset fAnchorEnd;
\r
31 private TextOffset fCurrent;
\r
33 private final boolean fWasZeroLength;
\r
35 private int fCurrentX;
\r
36 private int fCurrentY;
\r
37 private boolean fMouseOutside;
\r
39 private Thread fAutoscrollThread = null;
\r
40 private boolean fThreadRun = true;
\r
42 private static final int kScrollSleep = 300;
\r
44 public SelectionDragInteractor(TextSelection selection,
\r
45 TextComponent textComponent,
\r
46 RunStrategy runStrategy,
\r
47 TextOffset anchorStart,
\r
48 TextOffset anchorEnd,
\r
52 boolean wasZeroLength) {
\r
54 fTextComponent = textComponent;
\r
55 fSelection = selection;
\r
56 fRunStrategy = runStrategy;
\r
57 fAnchorStart = anchorStart;
\r
58 fAnchorEnd = anchorEnd;
\r
61 fCurrentX = initialX;
\r
62 fCurrentY = initialY;
\r
63 fMouseOutside = false;
\r
65 fWasZeroLength = wasZeroLength;
\r
70 public boolean textControlEventOccurred(Behavior.EventType event, Object what) {
\r
75 public boolean focusGained(FocusEvent event) {
\r
80 public boolean focusLost(FocusEvent event) {
\r
85 public boolean keyPressed(KeyEvent event) {
\r
90 public boolean keyTyped(KeyEvent event) {
\r
95 public boolean keyReleased(KeyEvent event) {
\r
100 public synchronized boolean mouseDragged(MouseEvent e) {
\r
102 int x = e.getX(), y = e.getY();
\r
103 if (fCurrentX != x || fCurrentY != y) {
\r
106 processMouseLocation();
\r
111 public synchronized boolean mouseEnter(MouseEvent e) {
\r
113 fMouseOutside = false;
\r
117 public synchronized boolean mouseExited(MouseEvent e) {
\r
119 if (fAutoscrollThread == null) {
\r
120 fAutoscrollThread = new Thread(this);
\r
121 fAutoscrollThread.start();
\r
123 fMouseOutside = true;
\r
129 public synchronized boolean mouseReleased(MouseEvent e) {
\r
131 fMouseOutside = false;
\r
132 fThreadRun = false;
\r
133 if (fAutoscrollThread != null) {
\r
134 fAutoscrollThread.interrupt();
\r
138 boolean isZeroLength = TextSelection.rangeIsZeroLength(fAnchorStart,
\r
141 fSelection.mouseReleased(isZeroLength != fWasZeroLength);
\r
146 private void processMouseLocation() {
\r
148 fTextComponent.scrollToShow(fCurrentX, fCurrentY);
\r
149 fTextComponent.pointToTextOffset(fCurrent, fCurrentX, fCurrentY, null, true);
\r
153 private void setSelection() {
\r
155 if (fCurrent.greaterThan(fAnchorEnd)) {
\r
156 fSelection.advanceToNextBoundary(fCurrent);
\r
157 fSelection.setSelRangeAndDraw(fAnchorStart, fCurrent, fAnchorStart);
\r
159 else if (fCurrent.lessThan(fAnchorStart)) {
\r
160 fSelection.advanceToPreviousBoundary(fCurrent);
\r
161 fSelection.setSelRangeAndDraw(fCurrent, fAnchorEnd, fAnchorStart);
\r
164 fCurrent.assign(fAnchorEnd);
\r
165 fSelection.setSelRangeAndDraw(fAnchorStart, fAnchorEnd, fAnchorStart);
\r
169 public void run() {
\r
171 Runnable doMouseLoc = new Runnable() {
\r
172 public void run() {
\r
173 processMouseLocation();
\r
177 while (fThreadRun) {
\r
180 Thread.sleep(kScrollSleep);
\r
182 catch(InterruptedException e) {
\r
183 return; // just quit scrolling
\r
186 synchronized(this) {
\r
188 while (!fMouseOutside) {
\r
192 catch(InterruptedException e) {
\r
193 return; // just quit scrolling
\r
197 fRunStrategy.doIt(doMouseLoc);
\r