Debian Codegraph @

plugins/org.eclipse.emf.edit.ui/src/org/eclipse/emf/edit/ui/provider/InputDialog.java

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