comparison org.eclipse.draw2d/src/org/eclipse/draw2d/PopUpHelper.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.draw2d.PopUpHelper;
14
15 import java.lang.all;
16
17
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Color;
21 import org.eclipse.swt.graphics.Rectangle;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.draw2d.geometry.Dimension;
25 import org.eclipse.draw2d.LightweightSystem;
26
27 /**
28 * Provides abstract support for classes that manage popups. Popups in Draw2d consist of a
29 * LightweightSystem object with an SWT shell as its Control. Desired popup behavior is
30 * attained by adding appropriate listeners to this shell.
31 */
32 public abstract class PopUpHelper {
33
34 private Shell shell;
35 private LightweightSystem lws;
36 private bool tipShowing;
37 /**
38 * The Control this PopUpHelper's tooltip will belong to.
39 */
40 protected Control control;
41
42 /**
43 * These style bits should be used when creating the Shell.
44 * @see #createShell()
45 */
46 protected const int shellStyle;
47
48 /**
49 * Constructs a PopUpHelper to assist with popups on Control c.
50 *
51 * @param c the Control
52 * @since 2.0
53 */
54 protected this(Control c) {
55 this (c, SWT.ON_TOP | SWT.NO_TRIM);
56 }
57
58 /**
59 * Constructs a PopUpHelper to display the given shell style popup.
60 * @param c the control on which the popup is active.
61 * @param shellStyle the SWT style bits for the shell
62 * @since 3.1
63 */
64 protected this(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 draw upon.
71 *
72 * @return the newly created LightweightSystem
73 * @since 2.0
74 */
75 protected LightweightSystem createLightweightSystem() {
76 return new LightweightSystem();
77 }
78
79 /**
80 * Creates a new Shell object with the style specified for this helper.
81 *
82 * @return the newly created Shell
83 * @since 2.0
84 */
85 protected Shell createShell() {
86 return new Shell(control.getShell(), shellStyle);
87 }
88
89 /**
90 * Dispose of this PopUpHelper object.
91 *
92 * @since 2.0
93 */
94 public void dispose() {
95 if (isShowing())
96 hide();
97 if (shell !is null && !shell.isDisposed())
98 shell.dispose();
99 }
100
101 /**
102 * Returns this PopUpHelper's shell. If no shell exists for this PopUpHelper, a new shell
103 * is created and hookShellListeners() is called.
104 *
105 * @return the Shell
106 * @since 2.0
107 */
108 protected Shell getShell() {
109 if (shell is null) {
110 shell = createShell();
111 hookShellListeners();
112 }
113 return shell;
114 }
115
116 /**
117 * Returns the size needed to display the shell's trim. This method should not be called
118 * until the shell has been created.
119 * @return the size of the shells trim.
120 * @since 3.1
121 */
122 protected Dimension getShellTrimSize() {
123 Rectangle trim = shell.computeTrim(0, 0, 0, 0);
124 return new Dimension(trim.width, trim.height);
125 }
126
127 /**
128 * Returns this PopUpHelper's LightweightSystem. If no LightweightSystem exists for this
129 * PopUpHelper, a new LightweightSystem is created with this PopUpHelper's Shell as its
130 * Control.
131 *
132 * @return the LightweightSystem
133 * @since 2.0
134 */
135 protected LightweightSystem getLightweightSystem() {
136 if (lws is null) {
137 lws = createLightweightSystem();
138 lws.setControl(getShell());
139 }
140 return lws;
141 }
142
143 /**
144 * Hides this PopUpHelper's Shell.
145 *
146 * @since 2.0
147 */
148 protected void hide() {
149 if (shell !is null && !shell.isDisposed())
150 shell.setVisible(false);
151 tipShowing = false;
152 }
153
154 /**
155 * Desired popup helper behavior is achieved by writing listeners that manipulate the
156 * behavior of the PopUpHelper's Shell. Override this method and add these listeners here.
157 *
158 * @since 2.0
159 */
160 protected abstract void hookShellListeners();
161
162 /**
163 * Returns <code>true</code> if this PopUpHelper's Shell is visible, <code>false</code>
164 * otherwise.
165 *
166 * @return <code>true</code> if this PopUpHelper's Shell is visible
167 * @since 2.0
168 */
169 public bool isShowing() {
170 return tipShowing;
171 }
172
173 /**
174 * Sets the background color of this PopUpHelper's Shell.
175 *
176 * @param c the new background color
177 * @since 2.0
178 */
179 public void setBackgroundColor(Color c) {
180 getShell().setBackground(c);
181 }
182
183 /**
184 * Sets the foreground color of this PopUpHelper's Shell.
185 *
186 * @param c the new foreground color
187 * @since 2.0
188 */
189 public void setForegroundColor(Color c) {
190 getShell().setForeground(c);
191 }
192
193 /**
194 * Sets the bounds on this PopUpHelper's Shell.
195 *
196 * @param x the x coordinate
197 * @param y the y coordinate
198 * @param width the width
199 * @param height the height
200 * @since 2.0
201 */
202 protected void setShellBounds(int x, int y, int width, int height) {
203 getShell().setBounds(x, y, width, height);
204 }
205
206 /**
207 * Displays this PopUpHelper's Shell.
208 *
209 * @since 2.0
210 */
211 protected void show() {
212 getShell().setVisible(true);
213 tipShowing = true;
214 }
215
216 }