Mercurial > projects > dwt-addons
annotate dwtx/jface/dialogs/TrayDialog.d @ 70:46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 22 May 2008 01:36:46 +0200 |
parents | ea8ff534f622 |
children |
rev | line source |
---|---|
11 | 1 /******************************************************************************* |
2 * Copyright (c) 2005, 2007 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 dwtx.jface.dialogs.TrayDialog; | |
14 | |
15 import dwtx.jface.dialogs.Dialog; | |
16 import dwtx.jface.dialogs.DialogTray; | |
17 import dwtx.jface.dialogs.IDialogConstants; | |
18 | |
19 import dwt.DWT; | |
20 import dwt.events.DisposeEvent; | |
21 import dwt.events.DisposeListener; | |
22 import dwt.events.SelectionAdapter; | |
23 import dwt.events.SelectionEvent; | |
24 import dwt.graphics.Cursor; | |
25 import dwt.graphics.Image; | |
26 import dwt.graphics.Rectangle; | |
27 import dwt.layout.GridData; | |
28 import dwt.layout.GridLayout; | |
29 import dwt.widgets.Composite; | |
30 import dwt.widgets.Control; | |
31 import dwt.widgets.Event; | |
32 import dwt.widgets.Label; | |
33 import dwt.widgets.Layout; | |
34 import dwt.widgets.Link; | |
35 import dwt.widgets.Listener; | |
36 import dwt.widgets.Sash; | |
37 import dwt.widgets.Shell; | |
38 import dwt.widgets.ToolBar; | |
39 import dwt.widgets.ToolItem; | |
40 import dwtx.jface.resource.JFaceResources; | |
41 import dwtx.jface.window.IShellProvider; | |
42 | |
43 import dwt.dwthelper.utils; | |
44 import tango.core.Exception; | |
45 | |
46 /** | |
47 * A <code>TrayDialog</code> is a specialized <code>Dialog</code> that can contain | |
48 * a tray on its side. The tray's content is provided as a <code>DialogTray</code>. | |
49 * <p> | |
50 * It is recommended to subclass this class instead of <code>Dialog</code> in all | |
51 * cases except where the dialog should never show a tray. For example, dialogs | |
52 * which are very short, simple, and quick to dismiss (e.g. a message dialog with | |
53 * an OK button) should subclass <code>Dialog</code>. | |
54 * </p> | |
55 * <p> | |
56 * Note: Trays are not supported on dialogs that use a custom layout on the <code> | |
57 * Shell</code> by overriding <code>Window#getLayout()</code>. | |
58 * </p> | |
59 * | |
60 * @see dwtx.jface.dialogs.DialogTray | |
61 * @see dwtx.jface.window.Window#getLayout() | |
62 * @since 3.2 | |
63 */ | |
64 public abstract class TrayDialog : Dialog { | |
65 | |
66 private static bool dialogHelpAvailable = false; | |
67 | |
68 /* | |
69 * The dialog's tray (null if none). | |
70 */ | |
71 private DialogTray tray; | |
72 | |
73 /* | |
74 * The tray's control. | |
75 */ | |
76 private Control trayControl; | |
77 | |
78 /* | |
79 * The separator to the left of the sash. | |
80 */ | |
81 private Label leftSeparator; | |
82 | |
83 /* | |
84 * The separator to the right of the sash. | |
85 */ | |
86 private Label rightSeparator; | |
87 | |
88 /* | |
89 * The sash that allows the user to resize the tray. | |
90 */ | |
91 private Sash sash; | |
92 | |
93 /* | |
94 * Whether or not help is available for this dialog. | |
95 */ | |
96 private bool helpAvailable; | |
97 | |
98 /** | |
99 * Creates a tray dialog instance. Note that the window will have no visual | |
100 * representation (no widgets) until it is told to open. | |
101 * | |
102 * @param shell the parent shell, or <code>null</code> to create a top-level shell | |
103 */ | |
104 protected this(Shell shell) { | |
105 super(shell); | |
106 helpAvailable = isDialogHelpAvailable(); | |
107 } | |
108 | |
109 /** | |
110 * Creates a tray dialog with the given parent. | |
111 * | |
112 * @param parentShell the object that returns the current parent shell | |
113 */ | |
114 protected this(IShellProvider parentShell) { | |
115 super(parentShell); | |
116 helpAvailable = isDialogHelpAvailable(); | |
117 } | |
118 | |
119 /** | |
120 * Closes this dialog's tray, disposing its widgets. | |
121 * | |
122 * @throws IllegalStateException if the tray was not open | |
123 */ | |
124 public void closeTray() { | |
125 if (getTray() is null) { | |
126 throw new IllegalStateException("Tray was not open"); //$NON-NLS-1$ | |
127 } | |
128 int trayWidth = trayControl.getSize().x + leftSeparator.getSize().x + sash.getSize().x + rightSeparator.getSize().x; | |
129 trayControl.dispose(); | |
130 trayControl = null; | |
131 tray = null; | |
132 leftSeparator.dispose(); | |
133 leftSeparator = null; | |
134 rightSeparator.dispose(); | |
135 rightSeparator = null; | |
136 sash.dispose(); | |
137 sash = null; | |
138 Shell shell = getShell(); | |
139 Rectangle bounds = shell.getBounds(); | |
140 shell.setBounds(bounds.x + ((getDefaultOrientation() is DWT.RIGHT_TO_LEFT) ? trayWidth : 0), bounds.y, bounds.width - trayWidth, bounds.height); | |
141 } | |
142 | |
143 /* (non-Javadoc) | |
144 * @see dwtx.jface.dialogs.Dialog#close() | |
145 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
146 public override bool close() { |
11 | 147 /* |
148 * Close the tray to ensure that those dialogs that remember their | |
149 * size do not store the tray size. | |
150 */ | |
151 if (getTray() !is null) { | |
152 closeTray(); | |
153 } | |
154 return super.close(); | |
155 } | |
156 | |
157 /* (non-Javadoc) | |
158 * @see dwtx.jface.dialogs.Dialog#createButtonBar(dwt.widgets.Composite) | |
159 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
160 protected override Control createButtonBar(Composite parent) { |
11 | 161 Composite composite = new Composite(parent, DWT.NONE); |
162 GridLayout layout = new GridLayout(); | |
163 layout.marginWidth = 0; | |
164 layout.marginHeight = 0; | |
165 layout.horizontalSpacing = 0; | |
166 composite.setLayout(layout); | |
167 composite.setLayoutData(new GridData(DWT.FILL, DWT.CENTER, false, false)); | |
168 composite.setFont(parent.getFont()); | |
169 | |
170 // create help control if needed | |
171 if (isHelpAvailable()) { | |
172 Control helpControl = createHelpControl(composite); | |
173 (cast(GridData) helpControl.getLayoutData()).horizontalIndent = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); | |
174 } | |
175 Control buttonSection = super.createButtonBar(composite); | |
176 (cast(GridData) buttonSection.getLayoutData()).grabExcessHorizontalSpace = true; | |
177 return composite; | |
178 } | |
179 | |
180 /** | |
181 * Creates a new help control that provides access to context help. | |
182 * <p> | |
183 * The <code>TrayDialog</code> implementation of this method creates | |
184 * the control, registers it for selection events including selection, | |
185 * Note that the parent's layout is assumed to be a <code>GridLayout</code> | |
186 * and the number of columns in this layout is incremented. Subclasses may | |
187 * override. | |
188 * </p> | |
189 * | |
190 * @param parent the parent composite | |
191 * @return the help control | |
192 */ | |
193 protected Control createHelpControl(Composite parent) { | |
194 Image helpImage = JFaceResources.getImage(DLG_IMG_HELP); | |
195 if (helpImage !is null) { | |
196 return createHelpImageButton(parent, helpImage); | |
197 } | |
198 return createHelpLink(parent); | |
199 } | |
200 | |
201 /* | |
202 * Creates a button with a help image. This is only used if there | |
203 * is an image available. | |
204 */ | |
205 private ToolBar createHelpImageButton(Composite parent, Image image) { | |
206 ToolBar toolBar = new ToolBar(parent, DWT.FLAT | DWT.NO_FOCUS); | |
207 (cast(GridLayout) parent.getLayout()).numColumns++; | |
208 toolBar.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER)); | |
209 Cursor cursor = new Cursor(parent.getDisplay(), DWT.CURSOR_HAND); | |
210 toolBar.setCursor(cursor); | |
39 | 211 toolBar.addDisposeListener(new class(cursor) DisposeListener { |
11 | 212 Cursor cursor_; |
39 | 213 this(Cursor c){ |
214 cursor_=c; | |
11 | 215 } |
216 public void widgetDisposed(DisposeEvent e) { | |
217 cursor_.dispose(); | |
218 } | |
219 }); | |
220 ToolItem item = new ToolItem(toolBar, DWT.NONE); | |
221 item.setImage(image); | |
222 item.setToolTipText(JFaceResources.getString("helpToolTip")); //$NON-NLS-1$ | |
223 item.addSelectionListener(new class SelectionAdapter { | |
224 public void widgetSelected(SelectionEvent e) { | |
225 helpPressed(); | |
226 } | |
227 }); | |
228 return toolBar; | |
229 } | |
230 | |
231 /* | |
232 * Creates a help link. This is used when there is no help image | |
233 * available. | |
234 */ | |
235 private Link createHelpLink(Composite parent) { | |
236 Link link = new Link(parent, DWT.WRAP | DWT.NO_FOCUS); | |
237 (cast(GridLayout) parent.getLayout()).numColumns++; | |
238 link.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER)); | |
239 link.setText("<a>"~IDialogConstants.HELP_LABEL~"</a>"); //$NON-NLS-1$ //$NON-NLS-2$ | |
240 link.setToolTipText(IDialogConstants.HELP_LABEL); | |
241 link.addSelectionListener(new class SelectionAdapter { | |
242 public void widgetSelected(SelectionEvent e) { | |
243 helpPressed(); | |
244 } | |
245 }); | |
246 return link; | |
247 } | |
248 | |
249 /* | |
250 * Returns whether or not the given layout can support the addition of a tray. | |
251 */ | |
252 private bool isCompatibleLayout(Layout layout) { | |
253 if (layout !is null && cast(GridLayout)layout ) { | |
254 GridLayout grid = cast(GridLayout)layout; | |
255 return !grid.makeColumnsEqualWidth && (grid.horizontalSpacing is 0) && | |
256 (grid.marginWidth is 0) && (grid.marginHeight is 0) && | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
257 (grid.numColumns is 5); |
11 | 258 } |
259 return false; | |
260 } | |
261 | |
262 /** | |
263 * Returns whether or not context help is available for this dialog. This | |
264 * can affect whether or not the dialog will display additional help | |
265 * mechanisms such as a help control in the button bar. | |
266 * | |
267 * @return whether or not context help is available for this dialog | |
268 */ | |
269 public bool isHelpAvailable() { | |
270 return helpAvailable; | |
271 } | |
272 | |
273 /** | |
274 * The tray dialog's default layout is a modified version of the default | |
275 * <code>Window</code> layout that can accomodate a tray, however it still | |
276 * conforms to the description of the <code>Window</code> default layout. | |
277 * <p> | |
278 * Note: Trays may not be supported with all custom layouts on the dialog's | |
279 * Shell. To avoid problems, use a single outer <code>Composite</code> for | |
280 * your dialog area, and set your custom layout on that <code>Composite</code>. | |
281 * </p> | |
282 * | |
283 * @see dwtx.jface.window.Window#getLayout() | |
284 * @return a newly created layout or <code>null</code> for no layout | |
285 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
286 protected override Layout getLayout() { |
11 | 287 GridLayout layout = cast(GridLayout)super.getLayout(); |
288 layout.numColumns = 5; | |
289 layout.horizontalSpacing = 0; | |
290 return layout; | |
291 } | |
292 | |
293 /** | |
294 * Returns the tray currently shown in the dialog, or <code>null</code> | |
295 * if there is no tray. | |
296 * | |
297 * @return the dialog's current tray, or <code>null</code> if there is none | |
298 */ | |
299 public DialogTray getTray() { | |
300 return tray; | |
301 } | |
302 | |
303 /* | |
304 * Called when the help control is invoked. This emulates the keyboard | |
305 * context help behavior (e.g. F1 on Windows). It traverses the widget | |
306 * tree upward until it finds a widget that has a help listener on it, | |
307 * then invokes a help event on that widget. | |
308 */ | |
309 private void helpPressed() { | |
310 if (getShell() !is null) { | |
311 Control c = getShell().getDisplay().getFocusControl(); | |
312 while (c !is null) { | |
313 if (c.isListening(DWT.Help)) { | |
314 c.notifyListeners(DWT.Help, new Event()); | |
315 break; | |
316 } | |
317 c = c.getParent(); | |
318 } | |
319 } | |
320 } | |
321 | |
322 /** | |
323 * Constructs the tray's widgets and displays the tray in this dialog. The | |
324 * dialog's size will be adjusted to accomodate the tray. | |
325 * | |
326 * @param tray the tray to show in this dialog | |
327 * @throws IllegalStateException if the dialog already has a tray open | |
328 * @throws UnsupportedOperationException if the dialog does not support trays, | |
329 * for example if it uses a custom layout. | |
330 */ | |
331 public void openTray(DialogTray tray) { | |
332 if (tray is null) { | |
333 throw new NullPointerException("Tray was null"); //$NON-NLS-1$ | |
334 } | |
335 if (getTray() !is null) { | |
336 throw new IllegalStateException("Tray was already open"); //$NON-NLS-1$ | |
337 } | |
338 if (!isCompatibleLayout(getShell().getLayout())) { | |
339 throw new UnsupportedOperationException("Trays not supported with custom layouts"); //$NON-NLS-1$ | |
340 } | |
341 Shell shell = getShell(); | |
342 leftSeparator = new Label(shell, DWT.SEPARATOR | DWT.VERTICAL); | |
343 leftSeparator.setLayoutData(new GridData(GridData.FILL_VERTICAL)); | |
344 sash = new Sash(shell, DWT.VERTICAL); | |
345 sash.setLayoutData(new GridData(GridData.FILL_VERTICAL)); | |
346 rightSeparator = new Label(shell, DWT.SEPARATOR | DWT.VERTICAL); | |
347 rightSeparator.setLayoutData(new GridData(GridData.FILL_VERTICAL)); | |
348 trayControl = tray.createContents_package(shell); | |
349 Rectangle clientArea = shell.getClientArea(); | |
350 GridData data = new GridData(GridData.FILL_VERTICAL); | |
351 data.widthHint = trayControl.computeSize(DWT.DEFAULT, clientArea.height).x; | |
352 trayControl.setLayoutData(data); | |
353 int trayWidth = leftSeparator.computeSize(DWT.DEFAULT, clientArea.height).x + sash.computeSize(DWT.DEFAULT, clientArea.height).x + rightSeparator.computeSize(DWT.DEFAULT, clientArea.height).x + data.widthHint; | |
354 Rectangle bounds = shell.getBounds(); | |
355 shell.setBounds(bounds.x - ((getDefaultOrientation() is DWT.RIGHT_TO_LEFT) ? trayWidth : 0), bounds.y, bounds.width + trayWidth, bounds.height); | |
39 | 356 sash.addListener(DWT.Selection, new class(shell, data) Listener { |
11 | 357 Shell shell_; |
358 GridData data_; | |
39 | 359 this(Shell a,GridData b){ |
360 shell_=a; | |
361 data_=b; | |
11 | 362 } |
363 public void handleEvent(Event event) { | |
364 if (event.detail !is DWT.DRAG) { | |
365 Rectangle clientArea = shell_.getClientArea(); | |
366 int newWidth = clientArea.width - event.x - (sash.getSize().x + rightSeparator.getSize().x); | |
367 if (newWidth !is data_.widthHint) { | |
368 data_.widthHint = newWidth; | |
369 shell_.layout(); | |
370 } | |
371 } | |
372 } | |
373 }); | |
374 this.tray = tray; | |
375 } | |
376 | |
377 /** | |
378 * Sets whether or not context help is available for this dialog. This | |
379 * can affect whether or not the dialog will display additional help | |
380 * mechanisms such as a help control in the button bar. | |
381 * | |
382 * @param helpAvailable whether or not context help is available for the dialog | |
383 */ | |
384 public void setHelpAvailable(bool helpAvailable) { | |
385 this.helpAvailable = helpAvailable; | |
386 } | |
387 | |
388 /** | |
389 * Tests if dialogs that have help control should show it | |
390 * all the time or only when explicitly requested for | |
391 * each dialog instance. | |
392 * | |
393 * @return <code>true</code> if dialogs that support help | |
394 * control should show it by default, <code>false</code> otherwise. | |
395 * @since 3.2 | |
396 */ | |
397 public static bool isDialogHelpAvailable() { | |
398 return dialogHelpAvailable; | |
399 } | |
400 | |
401 /** | |
402 * Sets whether JFace dialogs that support help control should | |
403 * show the control by default. If set to <code>false</code>, | |
404 * help control can still be shown on a per-dialog basis. | |
405 * | |
406 * @param helpAvailable <code>true</code> to show the help | |
407 * control, <code>false</code> otherwise. | |
408 * @since 3.2 | |
409 */ | |
410 public static void setDialogHelpAvailable(bool helpAvailable) { | |
411 dialogHelpAvailable = helpAvailable; | |
412 } | |
413 } |