Mercurial > projects > dwt-addons
annotate dwtx/jface/dialogs/IconAndMessageDialog.d @ 192:c3583c6ec027
Added missing default cases for switch statements
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 03 Nov 2008 22:52:26 +0100 |
parents | 46a6e0e6ccd4 |
children |
rev | line source |
---|---|
13 | 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> | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
77 * |
13 | 78 * @param composite |
79 * The composite to parent from. | |
80 * @return Control | |
81 */ | |
82 protected Control createMessageArea(Composite composite) { | |
83 // create composite | |
84 // create image | |
85 Image image = getImage(); | |
86 if (image !is null) { | |
87 imageLabel = new Label(composite, DWT.NULL); | |
88 image.setBackground(imageLabel.getBackground()); | |
89 imageLabel.setImage(image); | |
90 addAccessibleListeners(imageLabel, image); | |
91 GridDataFactory.fillDefaults().align_(DWT.CENTER, DWT.BEGINNING) | |
92 .applyTo(imageLabel); | |
93 } | |
94 // create message | |
95 if (message !is null) { | |
96 messageLabel = new Label(composite, getMessageLabelStyle()); | |
97 messageLabel.setText(message); | |
98 GridDataFactory | |
99 .fillDefaults() | |
100 .align_(DWT.FILL, DWT.BEGINNING) | |
101 .grab(true, false) | |
102 .hint( | |
103 convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH), | |
104 DWT.DEFAULT).applyTo(messageLabel); | |
105 } | |
106 return composite; | |
107 } | |
108 | |
109 private String getAccessibleMessageFor(Image image) { | |
110 if (image.opEquals(getErrorImage())) { | |
111 return JFaceResources.getString("error");//$NON-NLS-1$ | |
112 } | |
113 | |
114 if (image.opEquals(getWarningImage())) { | |
115 return JFaceResources.getString("warning");//$NON-NLS-1$ | |
116 } | |
117 | |
118 if (image.opEquals(getInfoImage())) { | |
119 return JFaceResources.getString("info");//$NON-NLS-1$ | |
120 } | |
121 | |
122 if (image.opEquals(getQuestionImage())) { | |
123 return JFaceResources.getString("question"); //$NON-NLS-1$ | |
124 } | |
125 | |
126 return null; | |
127 } | |
128 | |
129 /** | |
130 * Add an accessible listener to the label if it can be inferred from the | |
131 * image. | |
132 * | |
133 * @param label | |
134 * @param image | |
135 */ | |
136 private void addAccessibleListeners(Label label, Image image) { | |
39 | 137 label.getAccessible().addAccessibleListener(new class(image) AccessibleAdapter { |
13 | 138 Image image_; |
39 | 139 this(Image i){ |
140 image_ = i; | |
13 | 141 } |
142 public void getName(AccessibleEvent event) { | |
143 String accessibleMessage = getAccessibleMessageFor(image_); | |
144 if (accessibleMessage is null) { | |
145 return; | |
146 } | |
147 event.result = accessibleMessage; | |
148 } | |
149 }); | |
150 } | |
151 | |
152 /** | |
153 * Returns the style for the message label. | |
154 * | |
155 * @return the style for the message label | |
156 * | |
157 * @since 3.0 | |
158 */ | |
159 protected int getMessageLabelStyle() { | |
160 return DWT.WRAP; | |
161 } | |
162 | |
163 /* | |
164 * @see Dialog.createButtonBar() | |
165 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
166 protected override Control createButtonBar(Composite parent) { |
13 | 167 Composite composite = new Composite(parent, DWT.NONE); |
168 GridLayoutFactory.fillDefaults().numColumns(0) // this is incremented | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
169 // by createButton |
13 | 170 .equalWidth(true).applyTo(composite); |
171 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
172 GridDataFactory.fillDefaults().align_(DWT.END, DWT.CENTER).span(2, 1) |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
173 .applyTo(composite); |
13 | 174 composite.setFont(parent.getFont()); |
175 // Add the buttons to the button bar. | |
176 createButtonsForButtonBar(composite); | |
177 return composite; | |
178 } | |
179 | |
180 /** | |
181 * Returns the image to display beside the message in this dialog. | |
182 * <p> | |
183 * Subclasses may override. | |
184 * </p> | |
185 * | |
186 * @return the image to display beside the message | |
187 * @since 2.0 | |
188 */ | |
189 protected abstract Image getImage(); | |
190 | |
191 /* | |
192 * @see Dialog.createContents(Composite) | |
193 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
194 protected override Control createContents(Composite parent) { |
13 | 195 // initialize the dialog units |
196 initializeDialogUnits(parent); | |
197 Point defaultSpacing = LayoutConstants.getSpacing(); | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
198 GridLayoutFactory.fillDefaults().margins(LayoutConstants.getMargins()) |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
199 .spacing(defaultSpacing.x * 2, |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
200 defaultSpacing.y).numColumns(getColumnCount()).applyTo(parent); |
13 | 201 |
202 GridDataFactory.fillDefaults().grab(true, true).applyTo(parent); | |
203 createDialogAndButtonArea(parent); | |
204 return parent; | |
205 } | |
206 | |
207 /** | |
208 * Get the number of columns in the layout of the Shell of the dialog. | |
209 * | |
210 * @return int | |
211 * @since 3.3 | |
212 */ | |
213 int getColumnCount() { | |
214 return 2; | |
215 } | |
216 | |
217 /** | |
218 * Create the dialog area and the button bar for the receiver. | |
219 * | |
220 * @param parent | |
221 */ | |
222 protected void createDialogAndButtonArea(Composite parent) { | |
223 // create the dialog area and button bar | |
224 dialogArea = createDialogArea(parent); | |
225 buttonBar = createButtonBar(parent); | |
226 // Apply to the parent so that the message gets it too. | |
227 applyDialogFont(parent); | |
228 } | |
229 | |
230 /** | |
231 * Return the <code>Image</code> to be used when displaying an error. | |
232 * | |
233 * @return image the error image | |
234 */ | |
235 public Image getErrorImage() { | |
236 return getSWTImage(DWT.ICON_ERROR); | |
237 } | |
238 | |
239 /** | |
240 * Return the <code>Image</code> to be used when displaying a warning. | |
241 * | |
242 * @return image the warning image | |
243 */ | |
244 public Image getWarningImage() { | |
245 return getSWTImage(DWT.ICON_WARNING); | |
246 } | |
247 | |
248 /** | |
249 * Return the <code>Image</code> to be used when displaying information. | |
250 * | |
251 * @return image the information image | |
252 */ | |
253 public Image getInfoImage() { | |
254 return getSWTImage(DWT.ICON_INFORMATION); | |
255 } | |
256 | |
257 /** | |
258 * Return the <code>Image</code> to be used when displaying a question. | |
259 * | |
260 * @return image the question image | |
261 */ | |
262 public Image getQuestionImage() { | |
263 return getSWTImage(DWT.ICON_QUESTION); | |
264 } | |
265 | |
266 /** | |
267 * Get an <code>Image</code> from the provide DWT image constant. | |
268 * | |
269 * @param imageID | |
270 * the DWT image constant | |
271 * @return image the image | |
272 */ | |
273 private Image getSWTImage(int imageID) { | |
274 Shell shell = getShell(); | |
275 Display display; | |
276 if (shell is null) { | |
277 shell = getParentShell(); | |
278 } | |
279 if (shell is null) { | |
280 display = Display.getCurrent(); | |
281 } else { | |
282 display = shell.getDisplay(); | |
283 } | |
284 | |
285 Image[1] image; | |
39 | 286 display.syncExec(new class(display,imageID) Runnable { |
13 | 287 int imageID_; |
288 Display display_; | |
39 | 289 this(Display a,int b){ |
290 display_=a; | |
291 imageID_=b; | |
13 | 292 } |
293 public void run() { | |
294 image[0] = display_.getSystemImage(imageID_); | |
295 } | |
296 }); | |
297 | |
298 return image[0]; | |
299 | |
300 } | |
301 | |
302 } |