Mercurial > projects > dwt-addons
annotate dwtx/jface/dialogs/InputDialog.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 |
---|---|
15 | 1 /******************************************************************************* |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
15 | 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.dialogs.InputDialog; | |
14 | |
15 import dwtx.jface.dialogs.IDialogConstants; | |
16 import dwtx.jface.dialogs.Dialog; | |
17 import dwtx.jface.dialogs.IInputValidator; | |
18 | |
19 import dwt.DWT; | |
20 import dwt.events.ModifyEvent; | |
21 import dwt.events.ModifyListener; | |
22 import dwt.layout.GridData; | |
23 import dwt.widgets.Button; | |
24 import dwt.widgets.Composite; | |
25 import dwt.widgets.Control; | |
26 import dwt.widgets.Label; | |
27 import dwt.widgets.Shell; | |
28 import dwt.widgets.Text; | |
29 import dwtx.jface.resource.StringConverter; | |
30 | |
31 import dwt.dwthelper.utils; | |
32 | |
33 /** | |
34 * A simple input dialog for soliciting an input string from the user. | |
35 * <p> | |
36 * This concrete dialog class can be instantiated as is, or further subclassed as | |
37 * required. | |
38 * </p> | |
39 */ | |
40 public class InputDialog : Dialog { | |
41 /** | |
42 * The title of the dialog. | |
43 */ | |
44 private String title; | |
45 | |
46 /** | |
47 * The message to display, or <code>null</code> if none. | |
48 */ | |
49 private String message; | |
50 | |
51 /** | |
52 * The input value; the empty string by default. | |
53 */ | |
54 private String value = "";//$NON-NLS-1$ | |
55 | |
56 /** | |
57 * The input validator, or <code>null</code> if none. | |
58 */ | |
59 private IInputValidator validator; | |
60 | |
61 /** | |
62 * Ok button widget. | |
63 */ | |
64 private Button okButton; | |
65 | |
66 /** | |
67 * Input text widget. | |
68 */ | |
69 private Text text; | |
70 | |
71 /** | |
72 * Error message label widget. | |
73 */ | |
74 private Text errorMessageText; | |
75 | |
76 /** | |
77 * Error message string. | |
78 */ | |
79 private String errorMessage; | |
80 | |
81 /** | |
82 * Creates an input dialog with OK and Cancel buttons. Note that the dialog | |
83 * will have no visual representation (no widgets) until it is told to open. | |
84 * <p> | |
85 * Note that the <code>open</code> method blocks for input dialogs. | |
86 * </p> | |
87 * | |
88 * @param parentShell | |
89 * the parent shell, or <code>null</code> to create a top-level | |
90 * shell | |
91 * @param dialogTitle | |
92 * the dialog title, or <code>null</code> if none | |
93 * @param dialogMessage | |
94 * the dialog message, or <code>null</code> if none | |
95 * @param initialValue | |
96 * the initial input value, or <code>null</code> if none | |
97 * (equivalent to the empty string) | |
98 * @param validator | |
99 * an input validator, or <code>null</code> if none | |
100 */ | |
101 public this(Shell parentShell, String dialogTitle, | |
102 String dialogMessage, String initialValue, IInputValidator validator) { | |
103 super(parentShell); | |
104 this.title = dialogTitle; | |
105 message = dialogMessage; | |
106 if (initialValue is null) { | |
107 value = "";//$NON-NLS-1$ | |
108 } else { | |
109 value = initialValue; | |
110 } | |
111 this.validator = validator; | |
112 } | |
113 | |
114 /* | |
115 * (non-Javadoc) Method declared on Dialog. | |
116 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
117 protected override void buttonPressed(int buttonId) { |
15 | 118 if (buttonId is IDialogConstants.OK_ID) { |
119 value = text.getText(); | |
120 } else { | |
121 value = null; | |
122 } | |
123 super.buttonPressed(buttonId); | |
124 } | |
125 | |
126 /* | |
127 * (non-Javadoc) | |
128 * | |
129 * @see dwtx.jface.window.Window#configureShell(dwt.widgets.Shell) | |
130 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
131 protected override void configureShell(Shell shell) { |
15 | 132 super.configureShell(shell); |
133 if (title !is null) { | |
134 shell.setText(title); | |
135 } | |
136 } | |
137 | |
138 /* | |
139 * (non-Javadoc) | |
140 * | |
141 * @see dwtx.jface.dialogs.Dialog#createButtonsForButtonBar(dwt.widgets.Composite) | |
142 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
143 protected override void createButtonsForButtonBar(Composite parent) { |
15 | 144 // create OK and Cancel buttons by default |
145 okButton = createButton(parent, IDialogConstants.OK_ID, | |
146 IDialogConstants.OK_LABEL, true); | |
147 createButton(parent, IDialogConstants.CANCEL_ID, | |
148 IDialogConstants.CANCEL_LABEL, false); | |
149 //do this here because setting the text will set enablement on the ok | |
150 // button | |
151 text.setFocus(); | |
152 if (value !is null) { | |
153 text.setText(value); | |
154 text.selectAll(); | |
155 } | |
156 } | |
157 | |
158 /* | |
159 * (non-Javadoc) Method declared on Dialog. | |
160 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
161 protected override Control createDialogArea(Composite parent) { |
15 | 162 // create composite |
163 Composite composite = cast(Composite) super.createDialogArea(parent); | |
164 // create message | |
165 if (message !is null) { | |
166 Label label = new Label(composite, DWT.WRAP); | |
167 label.setText(message); | |
168 GridData data = new GridData(GridData.GRAB_HORIZONTAL | |
169 | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL | |
170 | GridData.VERTICAL_ALIGN_CENTER); | |
171 data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH); | |
172 label.setLayoutData(data); | |
173 label.setFont(parent.getFont()); | |
174 } | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
175 text = new Text(composite, getInputTextStyle()); |
15 | 176 text.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
177 | GridData.HORIZONTAL_ALIGN_FILL)); | |
178 text.addModifyListener(new class ModifyListener { | |
179 public void modifyText(ModifyEvent e) { | |
180 validateInput(); | |
181 } | |
182 }); | |
183 errorMessageText = new Text(composite, DWT.READ_ONLY | DWT.WRAP); | |
184 errorMessageText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | |
185 | GridData.HORIZONTAL_ALIGN_FILL)); | |
186 errorMessageText.setBackground(errorMessageText.getDisplay() | |
187 .getSystemColor(DWT.COLOR_WIDGET_BACKGROUND)); | |
188 // Set the error message text | |
189 // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=66292 | |
190 setErrorMessage(errorMessage); | |
191 | |
192 applyDialogFont(composite); | |
193 return composite; | |
194 } | |
195 | |
196 /** | |
197 * Returns the error message label. | |
198 * | |
199 * @return the error message label | |
200 * @deprecated use setErrorMessage(String) instead | |
201 */ | |
202 protected Label getErrorMessageLabel() { | |
203 return null; | |
204 } | |
205 | |
206 /** | |
207 * Returns the ok button. | |
208 * | |
209 * @return the ok button | |
210 */ | |
211 protected Button getOkButton() { | |
212 return okButton; | |
213 } | |
214 | |
215 /** | |
216 * Returns the text area. | |
217 * | |
218 * @return the text area | |
219 */ | |
220 protected Text getText() { | |
221 return text; | |
222 } | |
223 | |
224 /** | |
225 * Returns the validator. | |
226 * | |
227 * @return the validator | |
228 */ | |
229 protected IInputValidator getValidator() { | |
230 return validator; | |
231 } | |
232 | |
233 /** | |
234 * Returns the string typed into this input dialog. | |
235 * | |
236 * @return the input string | |
237 */ | |
238 public String getValue() { | |
239 return value; | |
240 } | |
241 | |
242 /** | |
243 * Validates the input. | |
244 * <p> | |
245 * The default implementation of this framework method delegates the request | |
246 * to the supplied input validator object; if it finds the input invalid, | |
247 * the error message is displayed in the dialog's message line. This hook | |
248 * method is called whenever the text changes in the input field. | |
249 * </p> | |
250 */ | |
251 protected void validateInput() { | |
252 String errorMessage = null; | |
253 if (validator !is null) { | |
254 errorMessage = validator.isValid(text.getText()); | |
255 } | |
256 // Bug 16256: important not to treat "" (blank error) the same as null | |
257 // (no error) | |
258 setErrorMessage(errorMessage); | |
259 } | |
260 | |
261 /** | |
262 * Sets or clears the error message. | |
263 * If not <code>null</code>, the OK button is disabled. | |
264 * | |
265 * @param errorMessage | |
266 * the error message, or <code>null</code> to clear | |
267 * @since 3.0 | |
268 */ | |
269 public void setErrorMessage(String errorMessage) { | |
270 this.errorMessage = errorMessage; | |
271 if (errorMessageText !is null && !errorMessageText.isDisposed()) { | |
272 errorMessageText.setText(errorMessage is null ? " \n " : errorMessage); //$NON-NLS-1$ | |
273 // Disable the error message text control if there is no error, or | |
274 // no error text (empty or whitespace only). Hide it also to avoid | |
275 // color change. | |
276 // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=130281 | |
277 bool hasError = errorMessage !is null && (StringConverter.removeWhiteSpaces(errorMessage)).length > 0; | |
278 errorMessageText.setEnabled(hasError); | |
279 errorMessageText.setVisible(hasError); | |
280 errorMessageText.getParent().update(); | |
281 // Access the ok button by id, in case clients have overridden button creation. | |
282 // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=113643 | |
283 Control button = getButton(IDialogConstants.OK_ID); | |
284 if (button !is null) { | |
285 button.setEnabled(errorMessage is null); | |
286 } | |
287 } | |
288 } | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
289 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
290 /** |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
291 * Returns the style bits that should be used for the input text field. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
292 * Defaults to a single line entry. Subclasses may override. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
293 * |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
294 * @return the integer style bits that should be used when creating the |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
295 * input text |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
296 * |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
297 * @since 3.4 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
298 */ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
299 protected int getInputTextStyle() { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
300 return DWT.SINGLE | DWT.BORDER; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
301 } |
15 | 302 } |