~berthold-daum/zora/trunk

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * This file is part of the ZoRa project: http://www.photozora.org.
 *
 * ZoRa is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * ZoRa is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ZoRa; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * (c) 2009 Berthold Daum  (berthold.daum@bdaum.de)
 */

package com.bdaum.zoom.ui.internal.hover;

import java.util.Arrays;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.ToolTip;

import com.bdaum.zoom.batch.internal.Daemon;
import com.bdaum.zoom.cat.model.asset.Region;
import com.bdaum.zoom.ui.internal.UiActivator;
import com.bdaum.zoom.ui.internal.views.IHoverSubject;

/**
 * A text hovering controller. The controller registers with the text widget's
 * control as a <code>MouseTrackListener<code>. When
 * receiving a mouse hover event, it opens a popup window using the
 * appropriate <code>IGalleryHover</code> to initialize the window's display
 * information. The controller closes the window if the mouse pointer leaves the
 * area for which the display information has been computed.
 * <p>
 */
@SuppressWarnings("restriction")
public class HoveringController extends MouseTrackAdapter {

	private static final int MINDURATION = 1000;
	private static final int ADDITIONALHOVERTIMEPERCHARACTER = 25;

	/**
	 * The window closer.
	 */
	class WindowCloser extends MouseTrackAdapter implements MouseListener,
			MouseMoveListener, ControlListener, KeyListener, FocusListener {

		Object fCoveredObject;
		String[] fCoveredRegions;

		/**
		 * Creates a new window closer for the given area.
		 */
		public WindowCloser(Object coveredObject, Region[] coveredRegions) {
			fCoveredObject = coveredObject;
			if (coveredRegions != null) {
				fCoveredRegions = new String[coveredRegions.length];
				for (int i = 0; i < coveredRegions.length; i++)
					fCoveredRegions[i] = coveredRegions[i].getStringId();
			} else
				fCoveredRegions = null;
		}

		/**
		 * Starts watching whether the popup window must be closed.
		 */
		public void start() {
			final Control control = viewer.getControl();
			control.addMouseListener(this);
			control.addMouseMoveListener(this);
			control.addMouseTrackListener(this);
			control.addControlListener(this);
			control.addKeyListener(this);
			control.addFocusListener(this);
			toolTip.setVisible(true);
			if (MINDURATION > 0) {
				final int msecs = MINDURATION
						+ (toolTip.getMessage().length() + toolTip.getText()
								.length()) * ADDITIONALHOVERTIMEPERCHARACTER;
				new Daemon(Messages.HoveringController_hover_control, -1L) {
					@Override
					protected void doRun(IProgressMonitor monitor) {
						if (!control.isDisposed())
							control.getDisplay().asyncExec(new Runnable() {
								public void run() {
									if (!control.isDisposed())
										stop();
								}
							});

					}
				}.schedule(msecs);
			}
		}

		/**
		 * Closes the popup window and stops watching.
		 */
		void stop() {
			Control control = viewer.getControl();
			control.removeMouseListener(this);
			control.removeMouseMoveListener(this);
			control.removeMouseTrackListener(this);
			control.removeControlListener(this);
			control.removeKeyListener(this);
			control.removeFocusListener(this);
			install();
			toolTip.setVisible(false);
		}

		/*
		 * @see MouseMoveListener#mouseMove
		 */

		public void mouseMove(MouseEvent event) {
			Object foundItem = viewer.findObject(event.x, event.y);
			String[] foundRegions = viewer.findRegions(event.x, event.y);
			if (foundItem != fCoveredObject
					|| !Arrays.equals(foundRegions, fCoveredRegions)) {
				stop();
			}
		}

		/*
		 * @see MouseListener#mouseUp(MouseEvent)
		 */

		public void mouseUp(MouseEvent event) {
			// do nothing
		}

		/*
		 * @see MouseListener#mouseDown(MouseEvent)
		 */

		public void mouseDown(MouseEvent event) {
			stop();
		}

		/*
		 * @see MouseListener#mouseDoubleClick(MouseEvent)
		 */

		public void mouseDoubleClick(MouseEvent event) {
			stop();
		}

		/*
		 * @see MouseTrackAdapter#mouseExit(MouseEvent)
		 */

		@Override
		public void mouseExit(MouseEvent event) {
			stop();
		}

		/*
		 * @see ControlListener#controlResized(ControlEvent)
		 */

		public void controlResized(ControlEvent event) {
			stop();
		}

		/*
		 * @see ControlListener#controlMoved(ControlEvent)
		 */

		public void controlMoved(ControlEvent event) {
			stop();
		}

		/*
		 * @see KeyListener#keyReleased(KeyEvent)
		 */

		public void keyReleased(KeyEvent event) {
			// do nothing
		}

		/*
		 * @see KeyListener#keyPressed(KeyEvent)
		 */

		public void keyPressed(KeyEvent event) {
			stop();
		}

		/*
		 * @see FocusListener#focusLost(FocusEvent)
		 */

		public void focusLost(FocusEvent event) {
			if (viewer.getControl() == event.widget) {
				event.display.asyncExec(new Runnable() {

					public void run() {
						stop();
					}
				});
			}
		}

		/*
		 * @see FocusListener#focusGained(FocusEvent)
		 */

		public void focusGained(FocusEvent event) {
			// do nothing
		}
	}

	/** The popup window shell */
	// Shell fWindowShell;
	/** The label shown in the popup window shell */
	// private Canvas fWindowLabel;
	/** Remembers the previous mouse hover location */
	private Object fHoverObject = null;

	/** The hover information * */
	private HoverInfo info;

	private IHoverSubject viewer;

	private ToolTip toolTip;

	/**
	 * Creates a new text hovering controller for the given text viewer. The
	 * controller registers as mouse track listener on the text viewer's text
	 * widget. Initially, the popup window is invisible.
	 *
	 * @param viewer
	 *            the viewer for which the controller is created
	 */
	public HoveringController(IHoverSubject viewer) {
		this.viewer = viewer;
		toolTip = new ToolTip(viewer.getControl().getShell(), SWT.BALLOON);
		toolTip.setAutoHide(false);
	}

	/**
	 * Determines the location of the popup window depending on the size of the
	 * covered area and the coordinates at which the window has been requested.
	 *
	 * @param x
	 *            the x coordinate at which the window has been requested
	 * @param y
	 *            the y coordinate at which the window has been requested
	 * @param coveredArea
	 *            graphical area of the hover region
	 * @return the location of the hover popup window
	 */
	private Point computeWindowLocation(int x, int y) {
		return viewer.getControl().toDisplay(new Point(x + 20, y + 10));
	}

	/**
	 * Disposes this hovering controller
	 */
	public void dispose() {
		if (toolTip != null)
			toolTip.dispose();
	}

	/**
	 * Installs this hovering controller on its viewer.
	 */
	public void install() {
		fHoverObject = null;
		viewer.getControl().addMouseTrackListener(this);
	}

	/*
	 * @see MouseTrackAdapter#mouseHover
	 */

	@Override
	public void mouseHover(MouseEvent event) {
		if (UiActivator.getDefault().getShowHover()) {
			IGalleryHover hover = viewer.getGalleryHover(event);
			if (hover != null) {
				info = hover.getHoverInfo(viewer, event.x, event.y,
						event.stateMask);
				if (info != null && !info.object.equals(fHoverObject)) {
					fHoverObject = info.object;
					if (toolTip != null && !toolTip.isDisposed())
						showWindow(info.object, info.regions,
								computeWindowLocation(event.x, event.y));
				}
			}
		}
	}

	/**
	 * Opens the hover popup window at the specified location. The window closes
	 * if the mouse pointer leaves the specified area.
	 *
	 * @param coveredArea
	 *            the area about which the hover popup window presents
	 *            information
	 * @param coveredArea
	 *            a list of annotated subregions
	 * @param location
	 *            the location of the hover popup window will pop up
	 */
	private void showWindow(Object coveredObject, Region[] coveredRegions,
			Point location) {

		toolTip.setText(info.getTitle());
		toolTip.setMessage(info.getText());
		toolTip.setLocation(location);
		new WindowCloser(coveredObject, coveredRegions).start();
		uninstall();
	}

	/**
	 * Uninstalls this hovering controller from its text viewer.
	 */
	public void uninstall() {
		Control canvas = viewer.getControl();
		if (canvas != null && !canvas.isDisposed())
			canvas.removeMouseTrackListener(this);
	}

}