comparison dwtx/jface/dialogs/TitleAreaDialog.d @ 9:6c14e54dfc11

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