KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > dialogs > IconAndMessageDialog


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  * Stefan Xenos, IBM - bug 156790: Adopt GridLayoutFactory within JFace
11  *******************************************************************************/

12 package org.eclipse.jface.dialogs;
13
14 import org.eclipse.jface.layout.GridDataFactory;
15 import org.eclipse.jface.layout.GridLayoutFactory;
16 import org.eclipse.jface.layout.LayoutConstants;
17 import org.eclipse.jface.resource.JFaceResources;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.accessibility.AccessibleAdapter;
20 import org.eclipse.swt.accessibility.AccessibleEvent;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Display;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.swt.widgets.Shell;
28
29 /**
30  * The IconAndMessageDialog is the abstract superclass of dialogs that have an
31  * icon and a message as the first two widgets. In this dialog the icon and
32  * message are direct children of the shell in order that they can be read by
33  * accessibility tools more easily.
34  */

35 public abstract class IconAndMessageDialog extends Dialog {
36     /**
37      * Message (a localized string).
38      */

39     protected String JavaDoc message;
40
41     /**
42      * Message label is the label the message is shown on.
43      */

44     protected Label messageLabel;
45
46     /**
47      * Return the label for the image.
48      */

49     protected Label imageLabel;
50
51     /**
52      * Constructor for IconAndMessageDialog.
53      *
54      * @param parentShell
55      * the parent shell, or <code>null</code> to create a top-level
56      * shell
57      */

58     public IconAndMessageDialog(Shell parentShell) {
59         super(parentShell);
60     }
61
62     /**
63      * Create the area the message will be shown in.
64      * <p>
65      * The parent composite is assumed to use GridLayout as its layout manager,
66      * since the parent is typically the composite created in
67      * {@link Dialog#createDialogArea}.
68      * </p>
69      * @param composite
70      * The composite to parent from.
71      * @return Control
72      */

73     protected Control createMessageArea(Composite composite) {
74         // create composite
75
// create image
76
Image image = getImage();
77         if (image != null) {
78             imageLabel = new Label(composite, SWT.NULL);
79             image.setBackground(imageLabel.getBackground());
80             imageLabel.setImage(image);
81             addAccessibleListeners(imageLabel, image);
82             GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.BEGINNING)
83                     .applyTo(imageLabel);
84         }
85         // create message
86
if (message != null) {
87             messageLabel = new Label(composite, getMessageLabelStyle());
88             messageLabel.setText(message);
89             GridDataFactory
90                     .fillDefaults()
91                     .align(SWT.FILL, SWT.BEGINNING)
92                     .grab(true, false)
93                     .hint(
94                             convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH),
95                             SWT.DEFAULT).applyTo(messageLabel);
96         }
97         return composite;
98     }
99
100     private String JavaDoc getAccessibleMessageFor(Image image) {
101         if (image.equals(getErrorImage())) {
102             return JFaceResources.getString("error");//$NON-NLS-1$
103
}
104
105         if (image.equals(getWarningImage())) {
106             return JFaceResources.getString("warning");//$NON-NLS-1$
107
}
108
109         if (image.equals(getInfoImage())) {
110             return JFaceResources.getString("info");//$NON-NLS-1$
111
}
112
113         if (image.equals(getQuestionImage())) {
114             return JFaceResources.getString("question"); //$NON-NLS-1$
115
}
116
117         return null;
118     }
119
120     /**
121      * Add an accessible listener to the label if it can be inferred from the
122      * image.
123      *
124      * @param label
125      * @param image
126      */

127     private void addAccessibleListeners(Label label, final Image image) {
128         label.getAccessible().addAccessibleListener(new AccessibleAdapter() {
129             public void getName(AccessibleEvent event) {
130                 final String JavaDoc accessibleMessage = getAccessibleMessageFor(image);
131                 if (accessibleMessage == null) {
132                     return;
133                 }
134                 event.result = accessibleMessage;
135             }
136         });
137     }
138
139     /**
140      * Returns the style for the message label.
141      *
142      * @return the style for the message label
143      *
144      * @since 3.0
145      */

146     protected int getMessageLabelStyle() {
147         return SWT.WRAP;
148     }
149
150     /*
151      * @see Dialog.createButtonBar()
152      */

153     protected Control createButtonBar(Composite parent) {
154         Composite composite = new Composite(parent, SWT.NONE);
155         GridLayoutFactory.fillDefaults().numColumns(0) // this is incremented
156
// by createButton
157
.equalWidth(true).applyTo(composite);
158
159         GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).span(
160                 2, 1).applyTo(composite);
161         composite.setFont(parent.getFont());
162         // Add the buttons to the button bar.
163
createButtonsForButtonBar(composite);
164         return composite;
165     }
166
167     /**
168      * Returns the image to display beside the message in this dialog.
169      * <p>
170      * Subclasses may override.
171      * </p>
172      *
173      * @return the image to display beside the message
174      * @since 2.0
175      */

176     protected abstract Image getImage();
177
178     /*
179      * @see Dialog.createContents(Composite)
180      */

181     protected Control createContents(Composite parent) {
182         // initialize the dialog units
183
initializeDialogUnits(parent);
184         Point defaultMargins = LayoutConstants.getMargins();
185         Point defaultSpacing = LayoutConstants.getSpacing();
186         GridLayoutFactory.fillDefaults().margins(defaultMargins.x,
187                 defaultMargins.y * 3 / 2).spacing(defaultSpacing.x * 2,
188                 defaultSpacing.y).numColumns(getColumnCount()).applyTo(
189                 parent);
190
191         GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);
192         createDialogAndButtonArea(parent);
193         return parent;
194     }
195
196     /**
197      * Get the number of columns in the layout of the Shell of the dialog.
198      *
199      * @return int
200      * @since 3.3
201      */

202     int getColumnCount() {
203         return 2;
204     }
205
206     /**
207      * Create the dialog area and the button bar for the receiver.
208      *
209      * @param parent
210      */

211     protected void createDialogAndButtonArea(Composite parent) {
212         // create the dialog area and button bar
213
dialogArea = createDialogArea(parent);
214         buttonBar = createButtonBar(parent);
215         // Apply to the parent so that the message gets it too.
216
applyDialogFont(parent);
217     }
218
219     /**
220      * Return the <code>Image</code> to be used when displaying an error.
221      *
222      * @return image the error image
223      */

224     public Image getErrorImage() {
225         return getSWTImage(SWT.ICON_ERROR);
226     }
227
228     /**
229      * Return the <code>Image</code> to be used when displaying a warning.
230      *
231      * @return image the warning image
232      */

233     public Image getWarningImage() {
234         return getSWTImage(SWT.ICON_WARNING);
235     }
236
237     /**
238      * Return the <code>Image</code> to be used when displaying information.
239      *
240      * @return image the information image
241      */

242     public Image getInfoImage() {
243         return getSWTImage(SWT.ICON_INFORMATION);
244     }
245
246     /**
247      * Return the <code>Image</code> to be used when displaying a question.
248      *
249      * @return image the question image
250      */

251     public Image getQuestionImage() {
252         return getSWTImage(SWT.ICON_QUESTION);
253     }
254
255     /**
256      * Get an <code>Image</code> from the provide SWT image constant.
257      *
258      * @param imageID
259      * the SWT image constant
260      * @return image the image
261      */

262     private Image getSWTImage(final int imageID) {
263         Shell shell = getShell();
264         final Display display;
265         if (shell == null) {
266             shell = getParentShell();
267         }
268         if (shell == null) {
269             display = Display.getCurrent();
270         } else {
271             display = shell.getDisplay();
272         }
273
274         final Image[] image = new Image[1];
275         display.syncExec(new Runnable JavaDoc() {
276             public void run() {
277                 image[0] = display.getSystemImage(imageID);
278             }
279         });
280
281         return image[0];
282
283     }
284
285 }
286
Popular Tags