Project

General

Profile

Download (5.96 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 * IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.gef.internal.ui.palette.editparts;
12

    
13
import org.eclipse.swt.events.PaintEvent;
14
import org.eclipse.swt.events.PaintListener;
15
import org.eclipse.swt.events.ShellAdapter;
16
import org.eclipse.swt.events.ShellEvent;
17
import org.eclipse.swt.events.ShellListener;
18
import org.eclipse.swt.graphics.Point;
19
import org.eclipse.swt.widgets.Control;
20
import org.eclipse.swt.widgets.Display;
21
import org.eclipse.swt.widgets.Shell;
22

    
23
import org.eclipse.draw2d.IFigure;
24
import org.eclipse.draw2d.geometry.Dimension;
25
import org.eclipse.draw2d.rap.swt.SWT;
26

    
27
class EditPartTipHelper extends org.eclipse.draw2d.PopUpHelper {
28

    
29
	private static EditPartTipHelper currentHelper;
30
	private ShellListener shellListener;
31

    
32
	private static void setHelper(EditPartTipHelper helper) {
33
		if (currentHelper != null && currentHelper != helper
34
				&& currentHelper.isShowing())
35
			currentHelper.hide();
36
		currentHelper = helper;
37
	}
38

    
39
	public EditPartTipHelper(Control c) {
40
		super(c, SWT.ON_TOP | SWT.TOOL | SWT.NO_TRIM);
41
	}
42

    
43
	/**
44
	 * Sets the LightWeightSystem object's contents to the passed tooltip, and
45
	 * displays the tip at the coordianates specified by tipPosX and tipPosY.
46
	 * The given coordinates will be adjusted if the tip cannot be completely
47
	 * visible on the screen.
48
	 * 
49
	 * @param tip
50
	 *            The tool tip to be displayed.
51
	 * @param tipPosX
52
	 *            X coordiante of tooltip to be displayed
53
	 * @param tipPosY
54
	 *            Y coordinate of tooltip to be displayed
55
	 */
56
	public void displayToolTipAt(IFigure tip, int tipPosX, int tipPosY) {
57
		if (tip != null) {
58
			// Adjust the position if the tip will not be completely visible on
59
			// the screen
60
			int shiftX = 0;
61
			int shiftY = 0;
62
			Dimension tipSize = tip.getPreferredSize();
63
			getShell();
64
			tipSize = tipSize.getExpanded(getShellTrimSize());
65
			org.eclipse.swt.graphics.Rectangle area = control.getDisplay()
66
					.getClientArea();
67
			org.eclipse.swt.graphics.Point end = new org.eclipse.swt.graphics.Point(
68
					tipPosX + tipSize.width, tipPosY + tipSize.height);
69
			if (!area.contains(end)) {
70
				shiftX = end.x - (area.x + area.width);
71
				shiftY = end.y - (area.y + area.height);
72
				shiftX = shiftX < 0 ? 0 : shiftX;
73
				shiftY = shiftY < 0 ? 0 : shiftY;
74
			}
75
			tipPosX -= shiftX;
76
			tipPosY -= shiftY;
77

    
78
			// Display the tip
79
			EditPartTipHelper.setHelper(this);
80
			getLightweightSystem().setContents(tip);
81
			setShellBounds(tipPosX, tipPosY, tipSize.width, tipSize.height);
82
			show();
83
			// UNSUPPORTED - api not implemented in RAP
84
			// getShell().setCapture(true);
85
		}
86
	}
87

    
88
	public void dispose() {
89
		if (shellListener != null) {
90
			control.getShell().removeShellListener(shellListener);
91
			shellListener = null;
92
		}
93
		super.dispose();
94
	}
95

    
96
	/**
97
	 * @see org.eclipse.draw2d.PopUpHelper#hide()
98
	 */
99
	protected void hide() {
100
		super.hide();
101
		currentHelper = null;
102
	}
103

    
104
	protected void hookShellListeners() {
105

    
106
		/*
107
		 * If the cursor leaves the tip window, hide the tooltip and dispose of
108
		 * its shell
109
		 */
110
		// UNSUPPORTED - api not implemented in RAP
111
		// getShell().addMouseTrackListener(new MouseTrackAdapter() {
112
		// public void mouseExit(MouseEvent e) {
113
		// getShell().setCapture(false);
114
		// dispose();
115
		// }
116
		// });
117
		/*
118
		 * If the mouseExit listener does not get called, dispose of the shell
119
		 * if the cursor is no longer in the tooltip. This occurs in the rare
120
		 * case that a mouseEnter is not received on the tooltip when it
121
		 * appears.
122
		 */
123
		// UNSUPPORTED - api not implemented in RAP
124
		// getShell().addMouseMoveListener(new MouseMoveListener() {
125
		// public void mouseMove(MouseEvent e) {
126
		// Point eventPoint = getShell().toDisplay(new Point(e.x, e.y));
127
		// if (!getShell().getBounds().contains(eventPoint)) {
128
		// if (isShowing())
129
		// getShell().setCapture(false);
130
		// dispose();
131
		// }
132
		// }
133
		// });
134

    
135
		// This is to dispose of the tooltip when the user ALT-TABs to another
136
		// window.
137
		if (shellListener == null) {
138
			shellListener = new ShellAdapter() {
139
				public void shellDeactivated(ShellEvent event) {
140
					Display.getCurrent().asyncExec(new Runnable() {
141
						public void run() {
142
							Shell active = Display.getCurrent()
143
									.getActiveShell();
144
							if (getShell() == active
145
									|| control.getShell() == active
146
									|| getShell().isDisposed())
147
								return;
148
							// UNSUPPORTED - api not implemented in RAP
149
							// if (isShowing())
150
							// getShell().setCapture(false);
151
							dispose();
152
						}
153
					});
154
				}
155
			};
156
			control.getShell().addShellListener(shellListener);
157
			getShell().addShellListener(shellListener);
158
		}
159

    
160
		/*
161
		 * Workaround for GTK Bug - Control.setCapture(boolean) not implemented:
162
		 * If the cursor is not over the shell when it is first painted, hide
163
		 * the tooltip and dispose of the shell.
164
		 */
165
		if (SWT.getPlatform().equals("gtk")) { //$NON-NLS-1$
166
			getShell().addPaintListener(new PaintListener() {
167
				public void paintControl(PaintEvent event) {
168
					Point cursorLoc = Display.getCurrent().getCursorLocation();
169
					if (!getShell().getBounds().contains(cursorLoc)) {
170
						// This must be run asynchronously. If not, other paint
171
						// listeners may attempt to paint on a disposed control.
172
						Display.getCurrent().asyncExec(new Runnable() {
173
							public void run() {
174
								// UNSUPPORTED - api not implemented in RAP
175
								// if (isShowing())
176
								// getShell().setCapture(false);
177
								dispose();
178
							}
179
						});
180
					}
181
				}
182
			});
183
		}
184
	}
185

    
186
	public static boolean isCurrent(EditPartTipHelper helper) {
187
		return currentHelper != null && helper == currentHelper;
188
	}
189

    
190
}
(6-6/22)