comparison org.eclipse.jface/src/org/eclipse/jface/dialogs/TitleAreaDialog.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, 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 * Konstantin Scheglov <scheglov_ke@nlmk.ru > - Fix for bug 41172
11 * [Dialogs] Bug with Image in TitleAreaDialog
12 * Sebastian Davids <sdavids@gmx.de> - Fix for bug 82064
13 * [Dialogs] TitleAreaDialog#setTitleImage cannot be called before open()
14 * Port to the D programming language:
15 * Frank Benoit <benoit@tionex.de>
16 *******************************************************************************/
17 module org.eclipse.jface.dialogs.TitleAreaDialog;
18
19 import org.eclipse.jface.dialogs.IDialogConstants;
20 import org.eclipse.jface.dialogs.TrayDialog;
21 import org.eclipse.jface.dialogs.IMessageProvider;
22
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.DisposeEvent;
25 import org.eclipse.swt.events.DisposeListener;
26 import org.eclipse.swt.graphics.Color;
27 import org.eclipse.swt.graphics.Image;
28 import org.eclipse.swt.graphics.Point;
29 import org.eclipse.swt.graphics.RGB;
30 import org.eclipse.swt.layout.FormAttachment;
31 import org.eclipse.swt.layout.FormData;
32 import org.eclipse.swt.layout.FormLayout;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.Control;
37 import org.eclipse.swt.widgets.Display;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Shell;
40 import org.eclipse.swt.widgets.Text;
41 import org.eclipse.jface.resource.JFaceColors;
42 import org.eclipse.jface.resource.JFaceResources;
43
44 import java.lang.all;
45 import java.util.Set;
46
47 /**
48 * A dialog that has a title area for displaying a title and an image as well as
49 * a common area for displaying a description, a message, or an error message.
50 * <p>
51 * This dialog class may be subclassed.
52 */
53 public class TitleAreaDialog : TrayDialog {
54 /**
55 * Image registry key for error message image.
56 */
57 public static const String DLG_IMG_TITLE_ERROR = DLG_IMG_MESSAGE_ERROR;
58
59 /**
60 * Image registry key for banner image (value
61 * <code>"dialog_title_banner_image"</code>).
62 */
63 public static const String DLG_IMG_TITLE_BANNER = "dialog_title_banner_image";//$NON-NLS-1$
64
65 /**
66 * Message type constant used to display an info icon with the message.
67 *
68 * @since 2.0
69 * @deprecated
70 */
71 public const static String INFO_MESSAGE = "INFO_MESSAGE"; //$NON-NLS-1$
72
73 /**
74 * Message type constant used to display a warning icon with the message.
75 *
76 * @since 2.0
77 * @deprecated
78 */
79 public const static String WARNING_MESSAGE = "WARNING_MESSAGE"; //$NON-NLS-1$
80
81 // Space between an image and a label
82 private static const int H_GAP_IMAGE = 5;
83
84 // Minimum dialog width (in dialog units)
85 private static const int MIN_DIALOG_WIDTH = 350;
86
87 // Minimum dialog height (in dialog units)
88 private static const int MIN_DIALOG_HEIGHT = 150;
89
90 private Label titleLabel;
91
92 private Label titleImageLabel;
93
94 private Label bottomFillerLabel;
95
96 private Label leftFillerLabel;
97
98 private RGB titleAreaRGB;
99
100 Color titleAreaColor;
101
102 private String message = ""; //$NON-NLS-1$
103
104 private String errorMessage;
105
106 private Text messageLabel;
107
108 private Composite workArea;
109
110 private Label messageImageLabel;
111
112 private Image messageImage;
113
114 private bool showingError = false;
115
116 private bool titleImageLargest = true;
117
118 private int messageLabelHeight;
119
120 private Image titleAreaImage;
121
122 /**
123 * Instantiate a new title area dialog.
124 *
125 * @param parentShell
126 * the parent SWT shell
127 */
128 public this(Shell parentShell) {
129 super(parentShell);
130 }
131
132 /*
133 * @see Dialog.createContents(Composite)
134 */
135 protected override Control createContents(Composite parent) {
136 // create the overall composite
137 Composite contents = new Composite(parent, SWT.NONE);
138 contents.setLayoutData(new GridData(GridData.FILL_BOTH));
139 // initialize the dialog units
140 initializeDialogUnits(contents);
141 FormLayout layout = new FormLayout();
142 contents.setLayout(layout);
143 // Now create a work area for the rest of the dialog
144 workArea = new Composite(contents, SWT.NONE);
145 GridLayout childLayout = new GridLayout();
146 childLayout.marginHeight = 0;
147 childLayout.marginWidth = 0;
148 childLayout.verticalSpacing = 0;
149 workArea.setLayout(childLayout);
150 Control top = createTitleArea(contents);
151 resetWorkAreaAttachments(top);
152 workArea.setFont(JFaceResources.getDialogFont());
153 // initialize the dialog units
154 initializeDialogUnits(workArea);
155 // create the dialog area and button bar
156 dialogArea = createDialogArea(workArea);
157 buttonBar = createButtonBar(workArea);
158 return contents;
159 }
160
161 /**
162 * Creates and returns the contents of the upper part of this dialog (above
163 * the button bar).
164 * <p>
165 * The <code>Dialog</code> implementation of this framework method creates
166 * and returns a new <code>Composite</code> with no margins and spacing.
167 * Subclasses should override.
168 * </p>
169 *
170 * @param parent
171 * The parent composite to contain the dialog area
172 * @return the dialog area control
173 */
174 protected override Control createDialogArea(Composite parent) {
175 // create the top level composite for the dialog area
176 Composite composite = new Composite(parent, SWT.NONE);
177 GridLayout layout = new GridLayout();
178 layout.marginHeight = 0;
179 layout.marginWidth = 0;
180 layout.verticalSpacing = 0;
181 layout.horizontalSpacing = 0;
182 composite.setLayout(layout);
183 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
184 composite.setFont(parent.getFont());
185 // Build the separator line
186 Label titleBarSeparator = new Label(composite, SWT.HORIZONTAL
187 | SWT.SEPARATOR);
188 titleBarSeparator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
189 return composite;
190 }
191
192 /**
193 * Creates the dialog's title area.
194 *
195 * @param parent
196 * the SWT parent for the title area widgets
197 * @return Control with the highest x axis value.
198 */
199 private Control createTitleArea(Composite parent) {
200
201 // add a dispose listener
202 parent.addDisposeListener(new class DisposeListener {
203 public void widgetDisposed(DisposeEvent e) {
204 if (titleAreaColor !is null) {
205 titleAreaColor.dispose();
206 }
207 }
208 });
209 // Determine the background color of the title bar
210 Display display = parent.getDisplay();
211 Color background;
212 Color foreground;
213 if (titleAreaRGB !is null) {
214 titleAreaColor = new Color(display, titleAreaRGB);
215 background = titleAreaColor;
216 foreground = null;
217 } else {
218 background = JFaceColors.getBannerBackground(display);
219 foreground = JFaceColors.getBannerForeground(display);
220 }
221
222 parent.setBackground(background);
223 int verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
224 int horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
225 // Dialog image @ right
226 titleImageLabel = new Label(parent, SWT.CENTER);
227 titleImageLabel.setBackground(background);
228 if (titleAreaImage is null)
229 titleImageLabel.setImage(JFaceResources
230 .getImage(DLG_IMG_TITLE_BANNER));
231 else
232 titleImageLabel.setImage(titleAreaImage);
233
234 FormData imageData = new FormData();
235 imageData.top = new FormAttachment(0, 0);
236 // Note: do not use horizontalSpacing on the right as that would be a
237 // regression from
238 // the R2.x style where there was no margin on the right and images are
239 // flush to the right
240 // hand side. see reopened comments in 41172
241 imageData.right = new FormAttachment(100, 0); // horizontalSpacing
242 titleImageLabel.setLayoutData(imageData);
243 // Title label @ top, left
244 titleLabel = new Label(parent, SWT.LEFT);
245 JFaceColors.setColors(titleLabel, foreground, background);
246 titleLabel.setFont(JFaceResources.getBannerFont());
247 titleLabel.setText(" ");//$NON-NLS-1$
248 FormData titleData = new FormData();
249 titleData.top = new FormAttachment(0, verticalSpacing);
250 titleData.right = new FormAttachment(titleImageLabel);
251 titleData.left = new FormAttachment(0, horizontalSpacing);
252 titleLabel.setLayoutData(titleData);
253 // Message image @ bottom, left
254 messageImageLabel = new Label(parent, SWT.CENTER);
255 messageImageLabel.setBackground(background);
256 // Message label @ bottom, center
257 messageLabel = new Text(parent, SWT.WRAP | SWT.READ_ONLY);
258 JFaceColors.setColors(messageLabel, foreground, background);
259 messageLabel.setText(" \n "); // two lines//$NON-NLS-1$
260 messageLabel.setFont(JFaceResources.getDialogFont());
261 messageLabelHeight = messageLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
262 // Filler labels
263 leftFillerLabel = new Label(parent, SWT.CENTER);
264 leftFillerLabel.setBackground(background);
265 bottomFillerLabel = new Label(parent, SWT.CENTER);
266 bottomFillerLabel.setBackground(background);
267 setLayoutsForNormalMessage(verticalSpacing, horizontalSpacing);
268 determineTitleImageLargest();
269 if (titleImageLargest)
270 return titleImageLabel;
271 return messageLabel;
272 }
273
274 /**
275 * Determine if the title image is larger than the title message and message
276 * area. This is used for layout decisions.
277 */
278 private void determineTitleImageLargest() {
279 int titleY = titleImageLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
280 int verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
281 int labelY = titleLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
282 labelY += verticalSpacing;
283 labelY += messageLabelHeight;
284 labelY += verticalSpacing;
285 titleImageLargest = titleY > labelY;
286 }
287
288 /**
289 * Set the layout values for the messageLabel, messageImageLabel and
290 * fillerLabel for the case where there is a normal message.
291 *
292 * @param verticalSpacing
293 * int The spacing between widgets on the vertical axis.
294 * @param horizontalSpacing
295 * int The spacing between widgets on the horizontal axis.
296 */
297 private void setLayoutsForNormalMessage(int verticalSpacing,
298 int horizontalSpacing) {
299 FormData messageImageData = new FormData();
300 messageImageData.top = new FormAttachment(titleLabel, verticalSpacing);
301 messageImageData.left = new FormAttachment(0, H_GAP_IMAGE);
302 messageImageLabel.setLayoutData(messageImageData);
303 FormData messageLabelData = new FormData();
304 messageLabelData.top = new FormAttachment(titleLabel, verticalSpacing);
305 messageLabelData.right = new FormAttachment(titleImageLabel);
306 messageLabelData.left = new FormAttachment(messageImageLabel,
307 horizontalSpacing);
308 messageLabelData.height = messageLabelHeight;
309 if (titleImageLargest)
310 messageLabelData.bottom = new FormAttachment(titleImageLabel, 0,
311 SWT.BOTTOM);
312 messageLabel.setLayoutData(messageLabelData);
313 FormData fillerData = new FormData();
314 fillerData.left = new FormAttachment(0, horizontalSpacing);
315 fillerData.top = new FormAttachment(messageImageLabel, 0);
316 fillerData.bottom = new FormAttachment(messageLabel, 0, SWT.BOTTOM);
317 bottomFillerLabel.setLayoutData(fillerData);
318 FormData data = new FormData();
319 data.top = new FormAttachment(messageImageLabel, 0, SWT.TOP);
320 data.left = new FormAttachment(0, 0);
321 data.bottom = new FormAttachment(messageImageLabel, 0, SWT.BOTTOM);
322 data.right = new FormAttachment(messageImageLabel, 0);
323 leftFillerLabel.setLayoutData(data);
324 }
325
326 /**
327 * The <code>TitleAreaDialog</code> implementation of this
328 * <code>Window</code> methods returns an initial size which is at least
329 * some reasonable minimum.
330 *
331 * @return the initial size of the dialog
332 */
333 protected override Point getInitialSize() {
334 Point shellSize = super.getInitialSize();
335 return new Point(Math.max(
336 convertHorizontalDLUsToPixels(MIN_DIALOG_WIDTH), shellSize.x),
337 Math.max(convertVerticalDLUsToPixels(MIN_DIALOG_HEIGHT),
338 shellSize.y));
339 }
340
341 /**
342 * Retained for backward compatibility.
343 *
344 * Returns the title area composite. There is no composite in this
345 * implementation so the shell is returned.
346 *
347 * @return Composite
348 * @deprecated
349 */
350 protected Composite getTitleArea() {
351 return getShell();
352 }
353
354 /**
355 * Returns the title image label.
356 *
357 * @return the title image label
358 */
359 protected Label getTitleImageLabel() {
360 return titleImageLabel;
361 }
362
363 /**
364 * Display the given error message. The currently displayed message is saved
365 * and will be redisplayed when the error message is set to
366 * <code>null</code>.
367 *
368 * @param newErrorMessage
369 * the newErrorMessage to display or <code>null</code>
370 */
371 public void setErrorMessage(String newErrorMessage) {
372 // Any change?
373 if (errorMessage is null ? newErrorMessage is null : errorMessage
374 .equals(newErrorMessage))
375 return;
376 errorMessage = newErrorMessage;
377
378 // Clear or set error message.
379 if (errorMessage is null) {
380 if (showingError) {
381 // we were previously showing an error
382 showingError = false;
383 }
384 // show the message
385 // avoid calling setMessage in case it is overridden to call
386 // setErrorMessage,
387 // which would result in a recursive infinite loop
388 if (message is null) // this should probably never happen since
389 // setMessage does this conversion....
390 message = ""; //$NON-NLS-1$
391 updateMessage(message);
392 messageImageLabel.setImage(messageImage);
393 setImageLabelVisible(messageImage !is null);
394 } else {
395 // Add in a space for layout purposes but do not
396 // change the instance variable
397 String displayedErrorMessage = " " ~ errorMessage; //$NON-NLS-1$
398 updateMessage(displayedErrorMessage);
399 if (!showingError) {
400 // we were not previously showing an error
401 showingError = true;
402 messageImageLabel.setImage(JFaceResources
403 .getImage(DLG_IMG_TITLE_ERROR));
404 setImageLabelVisible(true);
405 }
406 }
407 layoutForNewMessage();
408 }
409
410 /**
411 * Re-layout the labels for the new message.
412 */
413 private void layoutForNewMessage() {
414 int verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
415 int horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
416 // If there are no images then layout as normal
417 if (errorMessage is null && messageImage is null) {
418 setImageLabelVisible(false);
419 setLayoutsForNormalMessage(verticalSpacing, horizontalSpacing);
420 } else {
421 messageImageLabel.setVisible(true);
422 bottomFillerLabel.setVisible(true);
423 leftFillerLabel.setVisible(true);
424 /**
425 * Note that we do not use horizontalSpacing here as when the
426 * background of the messages changes there will be gaps between the
427 * icon label and the message that are the background color of the
428 * shell. We add a leading space elsewhere to compendate for this.
429 */
430 FormData data = new FormData();
431 data.left = new FormAttachment(0, H_GAP_IMAGE);
432 data.top = new FormAttachment(titleLabel, verticalSpacing);
433 messageImageLabel.setLayoutData(data);
434 data = new FormData();
435 data.top = new FormAttachment(messageImageLabel, 0);
436 data.left = new FormAttachment(0, 0);
437 data.bottom = new FormAttachment(messageLabel, 0, SWT.BOTTOM);
438 data.right = new FormAttachment(messageImageLabel, 0, SWT.RIGHT);
439 bottomFillerLabel.setLayoutData(data);
440 data = new FormData();
441 data.top = new FormAttachment(messageImageLabel, 0, SWT.TOP);
442 data.left = new FormAttachment(0, 0);
443 data.bottom = new FormAttachment(messageImageLabel, 0, SWT.BOTTOM);
444 data.right = new FormAttachment(messageImageLabel, 0);
445 leftFillerLabel.setLayoutData(data);
446 FormData messageLabelData = new FormData();
447 messageLabelData.top = new FormAttachment(titleLabel,
448 verticalSpacing);
449 messageLabelData.right = new FormAttachment(titleImageLabel);
450 messageLabelData.left = new FormAttachment(messageImageLabel, 0);
451 messageLabelData.height = messageLabelHeight;
452 if (titleImageLargest)
453 messageLabelData.bottom = new FormAttachment(titleImageLabel,
454 0, SWT.BOTTOM);
455 messageLabel.setLayoutData(messageLabelData);
456 }
457 // Do not layout before the dialog area has been created
458 // to avoid incomplete calculations.
459 if (dialogArea !is null)
460 workArea.getParent().layout(true);
461 }
462
463 /**
464 * Set the message text. If the message line currently displays an error,
465 * the message is saved and will be redisplayed when the error message is
466 * set to <code>null</code>.
467 * <p>
468 * Shortcut for <code>setMessage(newMessage, IMessageProvider.NONE)</code>
469 * </p>
470 * This method should be called after the dialog has been opened as it
471 * updates the message label immediately.
472 *
473 * @param newMessage
474 * the message, or <code>null</code> to clear the message
475 */
476 public void setMessage(String newMessage) {
477 setMessage(newMessage, IMessageProvider.NONE);
478 }
479
480 /**
481 * Sets the message for this dialog with an indication of what type of
482 * message it is.
483 * <p>
484 * The valid message types are one of <code>NONE</code>,
485 * <code>INFORMATION</code>,<code>WARNING</code>, or
486 * <code>ERROR</code>.
487 * </p>
488 * <p>
489 * Note that for backward compatibility, a message of type
490 * <code>ERROR</code> is different than an error message (set using
491 * <code>setErrorMessage</code>). An error message overrides the current
492 * message until the error message is cleared. This method replaces the
493 * current message and does not affect the error message.
494 * </p>
495 *
496 * @param newMessage
497 * the message, or <code>null</code> to clear the message
498 * @param newType
499 * the message type
500 * @since 2.0
501 */
502 public void setMessage(String newMessage, int newType) {
503 Image newImage = null;
504 if (newMessage !is null) {
505 switch (newType) {
506 case IMessageProvider.NONE:
507 break;
508 case IMessageProvider.INFORMATION:
509 newImage = JFaceResources.getImage(DLG_IMG_MESSAGE_INFO);
510 break;
511 case IMessageProvider.WARNING:
512 newImage = JFaceResources.getImage(DLG_IMG_MESSAGE_WARNING);
513 break;
514 case IMessageProvider.ERROR:
515 newImage = JFaceResources.getImage(DLG_IMG_MESSAGE_ERROR);
516 break;
517 default:
518 }
519 }
520 showMessage(newMessage, newImage);
521 }
522
523 /**
524 * Show the new message and image.
525 *
526 * @param newMessage
527 * @param newImage
528 */
529 private void showMessage(String newMessage, Image newImage) {
530 // Any change?
531 if (message.equals(newMessage) && messageImage is newImage) {
532 return;
533 }
534 message = newMessage;
535 if (message is null)
536 message = "";//$NON-NLS-1$
537 // Message string to be shown - if there is an image then add in
538 // a space to the message for layout purposes
539 String shownMessage = (newImage is null) ? message : " " ~ message; //$NON-NLS-1$
540 messageImage = newImage;
541 if (!showingError) {
542 // we are not showing an error
543 updateMessage(shownMessage);
544 messageImageLabel.setImage(messageImage);
545 setImageLabelVisible(messageImage !is null);
546 layoutForNewMessage();
547 }
548 }
549
550 /**
551 * Update the contents of the messageLabel.
552 *
553 * @param newMessage
554 * the message to use
555 */
556 private void updateMessage(String newMessage) {
557 messageLabel.setText(newMessage);
558 }
559
560 /**
561 * Sets the title to be shown in the title area of this dialog.
562 *
563 * @param newTitle
564 * the title show
565 */
566 public void setTitle(String newTitle) {
567 if (titleLabel is null)
568 return;
569 String title = newTitle;
570 if (title is null)
571 title = "";//$NON-NLS-1$
572 titleLabel.setText(title);
573 }
574
575 /**
576 * Sets the title bar color for this dialog.
577 *
578 * @param color
579 * the title bar color
580 */
581 public void setTitleAreaColor(RGB color) {
582 titleAreaRGB = color;
583 }
584
585 /**
586 * Sets the title image to be shown in the title area of this dialog.
587 *
588 * @param newTitleImage
589 * the title image to be shown
590 */
591 public void setTitleImage(Image newTitleImage) {
592
593 titleAreaImage = newTitleImage;
594 if (titleImageLabel !is null) {
595 titleImageLabel.setImage(newTitleImage);
596 titleImageLabel.setVisible(newTitleImage !is null);
597 if (newTitleImage !is null) {
598 determineTitleImageLargest();
599 Control top;
600 if (titleImageLargest)
601 top = titleImageLabel;
602 else
603 top = messageLabel;
604 resetWorkAreaAttachments(top);
605 }
606 }
607 }
608
609 /**
610 * Make the label used for displaying error images visible depending on
611 * bool.
612 *
613 * @param visible
614 * If <code>true</code> make the image visible, if not then
615 * make it not visible.
616 */
617 private void setImageLabelVisible(bool visible) {
618 messageImageLabel.setVisible(visible);
619 bottomFillerLabel.setVisible(visible);
620 leftFillerLabel.setVisible(visible);
621 }
622
623 /**
624 * Reset the attachment of the workArea to now attach to top as the top
625 * control.
626 *
627 * @param top
628 */
629 private void resetWorkAreaAttachments(Control top) {
630 FormData childData = new FormData();
631 childData.top = new FormAttachment(top);
632 childData.right = new FormAttachment(100, 0);
633 childData.left = new FormAttachment(0, 0);
634 childData.bottom = new FormAttachment(100, 0);
635 workArea.setLayoutData(childData);
636 }
637 }