Project

General

Profile

Download (5.33 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.draw2d;
12

    
13
import org.eclipse.swt.graphics.Color;
14
import org.eclipse.swt.graphics.Rectangle;
15
import org.eclipse.swt.widgets.Control;
16
import org.eclipse.swt.widgets.Shell;
17

    
18
import org.eclipse.draw2d.geometry.Dimension;
19
import org.eclipse.draw2d.rap.swt.SWT;
20

    
21
/**
22
 * Provides abstract support for classes that manage popups. Popups in Draw2d
23
 * consist of a LightweightSystem object with an SWT shell as its Control.
24
 * Desired popup behavior is attained by adding appropriate listeners to this
25
 * shell.
26
 */
27
public abstract class PopUpHelper {
28

    
29
	private Shell shell;
30
	private LightweightSystem lws;
31
	private boolean tipShowing;
32
	/**
33
	 * The Control this PopUpHelper's tooltip will belong to.
34
	 */
35
	protected Control control;
36

    
37
	/**
38
	 * These style bits should be used when creating the Shell.
39
	 * 
40
	 * @see #createShell()
41
	 */
42
	protected final int shellStyle;
43

    
44
	/**
45
	 * Constructs a PopUpHelper to assist with popups on Control c.
46
	 * 
47
	 * @param c
48
	 *            the Control
49
	 * @since 2.0
50
	 */
51
	protected PopUpHelper(Control c) {
52
		this(c, SWT.ON_TOP | SWT.NO_TRIM);
53
	}
54

    
55
	/**
56
	 * Constructs a PopUpHelper to display the given shell style popup.
57
	 * 
58
	 * @param c
59
	 *            the control on which the popup is active.
60
	 * @param shellStyle
61
	 *            the SWT style bits for the shell
62
	 * @since 3.1
63
	 */
64
	protected PopUpHelper(Control c, int shellStyle) {
65
		control = c;
66
		this.shellStyle = shellStyle | SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE;
67
	}
68

    
69
	/**
70
	 * Creates and returns the LightweightSystem object used by PopUpHelper to
71
	 * draw upon.
72
	 * 
73
	 * @return the newly created LightweightSystem
74
	 * @since 2.0
75
	 */
76
	protected LightweightSystem createLightweightSystem() {
77
		return new LightweightSystem();
78
	}
79

    
80
	/**
81
	 * Creates a new Shell object with the style specified for this helper.
82
	 * 
83
	 * @return the newly created Shell
84
	 * @since 2.0
85
	 */
86
	protected Shell createShell() {
87
		return new Shell(control.getShell(), shellStyle);
88
	}
89

    
90
	/**
91
	 * Dispose of this PopUpHelper object.
92
	 * 
93
	 * @since 2.0
94
	 */
95
	public void dispose() {
96
		if (isShowing())
97
			hide();
98
		if (shell != null && !shell.isDisposed())
99
			shell.dispose();
100
	}
101

    
102
	/**
103
	 * Returns this PopUpHelper's shell. If no shell exists for this
104
	 * PopUpHelper, a new shell is created and hookShellListeners() is called.
105
	 * 
106
	 * @return the Shell
107
	 * @since 2.0
108
	 */
109
	protected Shell getShell() {
110
		if (shell == null) {
111
			shell = createShell();
112
			hookShellListeners();
113
		}
114
		return shell;
115
	}
116

    
117
	/**
118
	 * Returns the size needed to display the shell's trim. This method should
119
	 * not be called until the shell has been created.
120
	 * 
121
	 * @return the size of the shells trim.
122
	 * @since 3.1
123
	 */
124
	protected Dimension getShellTrimSize() {
125
		Rectangle trim = shell.computeTrim(0, 0, 0, 0);
126
		return new Dimension(trim.width, trim.height);
127
	}
128

    
129
	/**
130
	 * Returns this PopUpHelper's LightweightSystem. If no LightweightSystem
131
	 * exists for this PopUpHelper, a new LightweightSystem is created with this
132
	 * PopUpHelper's Shell as its Control.
133
	 * 
134
	 * @return the LightweightSystem
135
	 * @since 2.0
136
	 */
137
	protected LightweightSystem getLightweightSystem() {
138
		if (lws == null) {
139
			lws = createLightweightSystem();
140
			lws.setControl(getShell());
141
		}
142
		return lws;
143
	}
144

    
145
	/**
146
	 * Hides this PopUpHelper's Shell.
147
	 * 
148
	 * @since 2.0
149
	 */
150
	protected void hide() {
151
		if (shell != null && !shell.isDisposed())
152
			shell.setVisible(false);
153
		tipShowing = false;
154
	}
155

    
156
	/**
157
	 * Desired popup helper behavior is achieved by writing listeners that
158
	 * manipulate the behavior of the PopUpHelper's Shell. Override this method
159
	 * and add these listeners here.
160
	 * 
161
	 * @since 2.0
162
	 */
163
	protected abstract void hookShellListeners();
164

    
165
	/**
166
	 * Returns <code>true</code> if this PopUpHelper's Shell is visible,
167
	 * <code>false</code> otherwise.
168
	 * 
169
	 * @return <code>true</code> if this PopUpHelper's Shell is visible
170
	 * @since 2.0
171
	 */
172
	public boolean isShowing() {
173
		return tipShowing;
174
	}
175

    
176
	/**
177
	 * Sets the background color of this PopUpHelper's Shell.
178
	 * 
179
	 * @param c
180
	 *            the new background color
181
	 * @since 2.0
182
	 */
183
	public void setBackgroundColor(Color c) {
184
		getShell().setBackground(c);
185
	}
186

    
187
	/**
188
	 * Sets the foreground color of this PopUpHelper's Shell.
189
	 * 
190
	 * @param c
191
	 *            the new foreground color
192
	 * @since 2.0
193
	 */
194
	public void setForegroundColor(Color c) {
195
		getShell().setForeground(c);
196
	}
197

    
198
	/**
199
	 * Sets the bounds on this PopUpHelper's Shell.
200
	 * 
201
	 * @param x
202
	 *            the x coordinate
203
	 * @param y
204
	 *            the y coordinate
205
	 * @param width
206
	 *            the width
207
	 * @param height
208
	 *            the height
209
	 * @since 2.0
210
	 */
211
	protected void setShellBounds(int x, int y, int width, int height) {
212
		getShell().setBounds(x, y, width, height);
213
	}
214

    
215
	/**
216
	 * Displays this PopUpHelper's Shell.
217
	 * 
218
	 * @since 2.0
219
	 */
220
	protected void show() {
221
		getShell().setVisible(true);
222
		tipShowing = true;
223
	}
224

    
225
}
(120-120/171)