1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 package com.touchgraph.graphlayout.interaction;
51
52 import java.awt.Point;
53 import java.awt.event.*;
54
55 import com.touchgraph.graphlayout.TGPanel;
56
57 /*** TGAbstractMousePausedUI allows one to handle MousePaused events.
58 *
59 * @author Alexander Shapiro
60 * @version 1.21 $Id: TGAbstractMousePausedUI.java,v 1.1.1.1 2004/02/06 08:44:07 keesj Exp $
61 */
62 public abstract class TGAbstractMousePausedUI extends TGUserInterface {
63
64 private AMPUIMouseMotionListener mml;
65 private AMPUIMouseListener ml;
66 protected TGPanel tgPanel;
67 Point mousePos=null;
68 PauseThread pauseThread=null;
69
70
71
72 /*** Constructor with TGPanel <tt>tgp</tt>.
73 */
74 public TGAbstractMousePausedUI( TGPanel tgp ) {
75
76 tgPanel=tgp;
77 ml = new AMPUIMouseListener();
78 mml = new AMPUIMouseMotionListener();
79 }
80
81 public final void activate() {
82 preActivate();
83 tgPanel.addMouseMotionListener(mml);
84 tgPanel.addMouseListener(ml);
85 }
86
87 public final void deactivate() {
88 tgPanel.removeMouseMotionListener(mml);
89 tgPanel.removeMouseListener(ml);
90 postDeactivate();
91 super.deactivate();
92 }
93
94 public void preActivate() {}
95 public void postDeactivate() {}
96 public abstract void mousePaused(MouseEvent e);
97 public abstract void mouseMoved(MouseEvent e);
98 public abstract void mouseDragged(MouseEvent e);
99
100 class PauseThread extends Thread{
101 boolean resetSleep;
102 boolean cancelled;
103 PauseThread() { cancelled = false; start(); }
104
105 void reset() { resetSleep = true; cancelled = false; }
106 void cancel() { cancelled = true; }
107
108 public void run() {
109 try {
110 do { resetSleep=false; sleep(250); } while (resetSleep);
111 if (!cancelled) {
112 MouseEvent pausedEvent =
113 new MouseEvent(tgPanel,MouseEvent.MOUSE_ENTERED, 0,0, mousePos.x,mousePos.y,0,false);
114 mousePaused(pausedEvent);
115 }
116 }
117 catch (Exception e) {e.printStackTrace();}
118 }
119 }
120
121 public void resetPause() {
122 if (pauseThread!=null && pauseThread.isAlive()) pauseThread.reset();
123 else pauseThread = new PauseThread();
124 }
125
126 public void cancelPause() {
127 if (pauseThread!=null && pauseThread.isAlive()) pauseThread.cancel();
128 }
129
130 private class AMPUIMouseMotionListener implements MouseMotionListener {
131 public void mouseMoved(MouseEvent e) {
132 mousePos=e.getPoint();
133 resetPause();
134 TGAbstractMousePausedUI.this.mouseMoved(e);
135 }
136
137 public void mouseDragged(MouseEvent e) {
138 mousePos=e.getPoint();
139 resetPause();
140 TGAbstractMousePausedUI.this.mouseDragged(e);
141 }
142 }
143
144 private class AMPUIMouseListener extends MouseAdapter {
145 public void mousePressed(MouseEvent e) {
146 cancelPause();
147 }
148 public void mouseReleased(MouseEvent e) {
149 cancelPause();
150 }
151 public void mouseExited(MouseEvent e) {
152
153 }
154 }
155
156 }