Mercurial > projects > dwt-addons
comparison dwtx/jface/dialogs/IconAndMessageDialog.d @ 13:6886832e1ed8
ErrorDialog
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 31 Mar 2008 01:41:52 +0200 |
parents | |
children | 644f1334b451 |
comparison
equal
deleted
inserted
replaced
12:8ec40848221b | 13:6886832e1ed8 |
---|---|
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 * Port to the D programming language: | |
12 * Frank Benoit <benoit@tionex.de> | |
13 *******************************************************************************/ | |
14 module dwtx.jface.dialogs.IconAndMessageDialog; | |
15 | |
16 import dwtx.jface.dialogs.Dialog; | |
17 import dwtx.jface.dialogs.IDialogConstants; | |
18 | |
19 import dwt.DWT; | |
20 import dwt.accessibility.AccessibleAdapter; | |
21 import dwt.accessibility.AccessibleEvent; | |
22 import dwt.graphics.Image; | |
23 import dwt.graphics.Point; | |
24 import dwt.widgets.Composite; | |
25 import dwt.widgets.Control; | |
26 import dwt.widgets.Display; | |
27 import dwt.widgets.Label; | |
28 import dwt.widgets.Shell; | |
29 import dwtx.jface.layout.GridDataFactory; | |
30 import dwtx.jface.layout.GridLayoutFactory; | |
31 import dwtx.jface.layout.LayoutConstants; | |
32 import dwtx.jface.resource.JFaceResources; | |
33 | |
34 import dwt.dwthelper.utils; | |
35 import dwt.dwthelper.Runnable; | |
36 | |
37 /** | |
38 * The IconAndMessageDialog is the abstract superclass of dialogs that have an | |
39 * icon and a message as the first two widgets. In this dialog the icon and | |
40 * message are direct children of the shell in order that they can be read by | |
41 * accessibility tools more easily. | |
42 */ | |
43 public abstract class IconAndMessageDialog : Dialog { | |
44 /** | |
45 * Message (a localized string). | |
46 */ | |
47 protected String message; | |
48 | |
49 /** | |
50 * Message label is the label the message is shown on. | |
51 */ | |
52 protected Label messageLabel; | |
53 | |
54 /** | |
55 * Return the label for the image. | |
56 */ | |
57 protected Label imageLabel; | |
58 | |
59 /** | |
60 * Constructor for IconAndMessageDialog. | |
61 * | |
62 * @param parentShell | |
63 * the parent shell, or <code>null</code> to create a top-level | |
64 * shell | |
65 */ | |
66 public this(Shell parentShell) { | |
67 super(parentShell); | |
68 } | |
69 | |
70 /** | |
71 * Create the area the message will be shown in. | |
72 * <p> | |
73 * The parent composite is assumed to use GridLayout as its layout manager, | |
74 * since the parent is typically the composite created in | |
75 * {@link Dialog#createDialogArea}. | |
76 * </p> | |
77 * @param composite | |
78 * The composite to parent from. | |
79 * @return Control | |
80 */ | |
81 protected Control createMessageArea(Composite composite) { | |
82 // create composite | |
83 // create image | |
84 Image image = getImage(); | |
85 if (image !is null) { | |
86 imageLabel = new Label(composite, DWT.NULL); | |
87 image.setBackground(imageLabel.getBackground()); | |
88 imageLabel.setImage(image); | |
89 addAccessibleListeners(imageLabel, image); | |
90 GridDataFactory.fillDefaults().align_(DWT.CENTER, DWT.BEGINNING) | |
91 .applyTo(imageLabel); | |
92 } | |
93 // create message | |
94 if (message !is null) { | |
95 messageLabel = new Label(composite, getMessageLabelStyle()); | |
96 messageLabel.setText(message); | |
97 GridDataFactory | |
98 .fillDefaults() | |
99 .align_(DWT.FILL, DWT.BEGINNING) | |
100 .grab(true, false) | |
101 .hint( | |
102 convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH), | |
103 DWT.DEFAULT).applyTo(messageLabel); | |
104 } | |
105 return composite; | |
106 } | |
107 | |
108 private String getAccessibleMessageFor(Image image) { | |
109 if (image.opEquals(getErrorImage())) { | |
110 return JFaceResources.getString("error");//$NON-NLS-1$ | |
111 } | |
112 | |
113 if (image.opEquals(getWarningImage())) { | |
114 return JFaceResources.getString("warning");//$NON-NLS-1$ | |
115 } | |
116 | |
117 if (image.opEquals(getInfoImage())) { | |
118 return JFaceResources.getString("info");//$NON-NLS-1$ | |
119 } | |
120 | |
121 if (image.opEquals(getQuestionImage())) { | |
122 return JFaceResources.getString("question"); //$NON-NLS-1$ | |
123 } | |
124 | |
125 return null; | |
126 } | |
127 | |
128 /** | |
129 * Add an accessible listener to the label if it can be inferred from the | |
130 * image. | |
131 * | |
132 * @param label | |
133 * @param image | |
134 */ | |
135 private void addAccessibleListeners(Label label, Image image) { | |
136 label.getAccessible().addAccessibleListener(new class AccessibleAdapter { | |
137 Image image_; | |
138 this(){ | |
139 image_ = image; | |
140 } | |
141 public void getName(AccessibleEvent event) { | |
142 String accessibleMessage = getAccessibleMessageFor(image_); | |
143 if (accessibleMessage is null) { | |
144 return; | |
145 } | |
146 event.result = accessibleMessage; | |
147 } | |
148 }); | |
149 } | |
150 | |
151 /** | |
152 * Returns the style for the message label. | |
153 * | |
154 * @return the style for the message label | |
155 * | |
156 * @since 3.0 | |
157 */ | |
158 protected int getMessageLabelStyle() { | |
159 return DWT.WRAP; | |
160 } | |
161 | |
162 /* | |
163 * @see Dialog.createButtonBar() | |
164 */ | |
165 protected Control createButtonBar(Composite parent) { | |
166 Composite composite = new Composite(parent, DWT.NONE); | |
167 GridLayoutFactory.fillDefaults().numColumns(0) // this is incremented | |
168 // by createButton | |
169 .equalWidth(true).applyTo(composite); | |
170 | |
171 GridDataFactory.fillDefaults().align_(DWT.END, DWT.CENTER).span( | |
172 2, 1).applyTo(composite); | |
173 composite.setFont(parent.getFont()); | |
174 // Add the buttons to the button bar. | |
175 createButtonsForButtonBar(composite); | |
176 return composite; | |
177 } | |
178 | |
179 /** | |
180 * Returns the image to display beside the message in this dialog. | |
181 * <p> | |
182 * Subclasses may override. | |
183 * </p> | |
184 * | |
185 * @return the image to display beside the message | |
186 * @since 2.0 | |
187 */ | |
188 protected abstract Image getImage(); | |
189 | |
190 /* | |
191 * @see Dialog.createContents(Composite) | |
192 */ | |
193 protected Control createContents(Composite parent) { | |
194 // initialize the dialog units | |
195 initializeDialogUnits(parent); | |
196 Point defaultMargins = LayoutConstants.getMargins(); | |
197 Point defaultSpacing = LayoutConstants.getSpacing(); | |
198 GridLayoutFactory.fillDefaults().margins(defaultMargins.x, | |
199 defaultMargins.y * 3 / 2).spacing(defaultSpacing.x * 2, | |
200 defaultSpacing.y).numColumns(getColumnCount()).applyTo( | |
201 parent); | |
202 | |
203 GridDataFactory.fillDefaults().grab(true, true).applyTo(parent); | |
204 createDialogAndButtonArea(parent); | |
205 return parent; | |
206 } | |
207 | |
208 /** | |
209 * Get the number of columns in the layout of the Shell of the dialog. | |
210 * | |
211 * @return int | |
212 * @since 3.3 | |
213 */ | |
214 int getColumnCount() { | |
215 return 2; | |
216 } | |
217 | |
218 /** | |
219 * Create the dialog area and the button bar for the receiver. | |
220 * | |
221 * @param parent | |
222 */ | |
223 protected void createDialogAndButtonArea(Composite parent) { | |
224 // create the dialog area and button bar | |
225 dialogArea = createDialogArea(parent); | |
226 buttonBar = createButtonBar(parent); | |
227 // Apply to the parent so that the message gets it too. | |
228 applyDialogFont(parent); | |
229 } | |
230 | |
231 /** | |
232 * Return the <code>Image</code> to be used when displaying an error. | |
233 * | |
234 * @return image the error image | |
235 */ | |
236 public Image getErrorImage() { | |
237 return getSWTImage(DWT.ICON_ERROR); | |
238 } | |
239 | |
240 /** | |
241 * Return the <code>Image</code> to be used when displaying a warning. | |
242 * | |
243 * @return image the warning image | |
244 */ | |
245 public Image getWarningImage() { | |
246 return getSWTImage(DWT.ICON_WARNING); | |
247 } | |
248 | |
249 /** | |
250 * Return the <code>Image</code> to be used when displaying information. | |
251 * | |
252 * @return image the information image | |
253 */ | |
254 public Image getInfoImage() { | |
255 return getSWTImage(DWT.ICON_INFORMATION); | |
256 } | |
257 | |
258 /** | |
259 * Return the <code>Image</code> to be used when displaying a question. | |
260 * | |
261 * @return image the question image | |
262 */ | |
263 public Image getQuestionImage() { | |
264 return getSWTImage(DWT.ICON_QUESTION); | |
265 } | |
266 | |
267 /** | |
268 * Get an <code>Image</code> from the provide DWT image constant. | |
269 * | |
270 * @param imageID | |
271 * the DWT image constant | |
272 * @return image the image | |
273 */ | |
274 private Image getSWTImage(int imageID) { | |
275 Shell shell = getShell(); | |
276 Display display; | |
277 if (shell is null) { | |
278 shell = getParentShell(); | |
279 } | |
280 if (shell is null) { | |
281 display = Display.getCurrent(); | |
282 } else { | |
283 display = shell.getDisplay(); | |
284 } | |
285 | |
286 Image[1] image; | |
287 display.syncExec(new class Runnable { | |
288 int imageID_; | |
289 Display display_; | |
290 this(){ | |
291 display_=display; | |
292 imageID_=imageID; | |
293 } | |
294 public void run() { | |
295 image[0] = display_.getSystemImage(imageID_); | |
296 } | |
297 }); | |
298 | |
299 return image[0]; | |
300 | |
301 } | |
302 | |
303 } |