KickJava   Java API By Example, From Geeks To Geeks.

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


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  *******************************************************************************/

11 package org.eclipse.jface.dialogs;
12
13 import org.eclipse.jface.resource.StringConverter;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.ModifyEvent;
16 import org.eclipse.swt.events.ModifyListener;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.widgets.Button;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Label;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.swt.widgets.Text;
24
25 /**
26  * A simple input dialog for soliciting an input string from the user.
27  * <p>
28  * This concrete dialog class can be instantiated as is, or further subclassed as
29  * required.
30  * </p>
31  */

32 public class InputDialog extends Dialog {
33     /**
34      * The title of the dialog.
35      */

36     private String JavaDoc title;
37
38     /**
39      * The message to display, or <code>null</code> if none.
40      */

41     private String JavaDoc message;
42
43     /**
44      * The input value; the empty string by default.
45      */

46     private String JavaDoc value = "";//$NON-NLS-1$
47

48     /**
49      * The input validator, or <code>null</code> if none.
50      */

51     private IInputValidator validator;
52
53     /**
54      * Ok button widget.
55      */

56     private Button okButton;
57
58     /**
59      * Input text widget.
60      */

61     private Text text;
62
63     /**
64      * Error message label widget.
65      */

66     private Text errorMessageText;
67     
68     /**
69      * Error message string.
70      */

71     private String JavaDoc errorMessage;
72
73     /**
74      * Creates an input dialog with OK and Cancel buttons. Note that the dialog
75      * will have no visual representation (no widgets) until it is told to open.
76      * <p>
77      * Note that the <code>open</code> method blocks for input dialogs.
78      * </p>
79      *
80      * @param parentShell
81      * the parent shell, or <code>null</code> to create a top-level
82      * shell
83      * @param dialogTitle
84      * the dialog title, or <code>null</code> if none
85      * @param dialogMessage
86      * the dialog message, or <code>null</code> if none
87      * @param initialValue
88      * the initial input value, or <code>null</code> if none
89      * (equivalent to the empty string)
90      * @param validator
91      * an input validator, or <code>null</code> if none
92      */

93     public InputDialog(Shell parentShell, String JavaDoc dialogTitle,
94             String JavaDoc dialogMessage, String JavaDoc initialValue, IInputValidator validator) {
95         super(parentShell);
96         this.title = dialogTitle;
97         message = dialogMessage;
98         if (initialValue == null) {
99             value = "";//$NON-NLS-1$
100
} else {
101             value = initialValue;
102         }
103         this.validator = validator;
104     }
105
106     /*
107      * (non-Javadoc) Method declared on Dialog.
108      */

109     protected void buttonPressed(int buttonId) {
110         if (buttonId == IDialogConstants.OK_ID) {
111             value = text.getText();
112         } else {
113             value = null;
114         }
115         super.buttonPressed(buttonId);
116     }
117
118     /*
119      * (non-Javadoc)
120      *
121      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
122      */

123     protected void configureShell(Shell shell) {
124         super.configureShell(shell);
125         if (title != null) {
126             shell.setText(title);
127         }
128     }
129
130     /*
131      * (non-Javadoc)
132      *
133      * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
134      */

135     protected void createButtonsForButtonBar(Composite parent) {
136         // create OK and Cancel buttons by default
137
okButton = createButton(parent, IDialogConstants.OK_ID,
138                 IDialogConstants.OK_LABEL, true);
139         createButton(parent, IDialogConstants.CANCEL_ID,
140                 IDialogConstants.CANCEL_LABEL, false);
141         //do this here because setting the text will set enablement on the ok
142
// button
143
text.setFocus();
144         if (value != null) {
145             text.setText(value);
146             text.selectAll();
147         }
148     }
149
150     /*
151      * (non-Javadoc) Method declared on Dialog.
152      */

153     protected Control createDialogArea(Composite parent) {
154         // create composite
155
Composite composite = (Composite) super.createDialogArea(parent);
156         // create message
157
if (message != null) {
158             Label label = new Label(composite, SWT.WRAP);
159             label.setText(message);
160             GridData data = new GridData(GridData.GRAB_HORIZONTAL
161                     | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL
162                     | GridData.VERTICAL_ALIGN_CENTER);
163             data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
164             label.setLayoutData(data);
165             label.setFont(parent.getFont());
166         }
167         text = new Text(composite, SWT.SINGLE | SWT.BORDER);
168         text.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
169                 | GridData.HORIZONTAL_ALIGN_FILL));
170         text.addModifyListener(new ModifyListener() {
171             public void modifyText(ModifyEvent e) {
172                 validateInput();
173             }
174         });
175         errorMessageText = new Text(composite, SWT.READ_ONLY | SWT.WRAP);
176         errorMessageText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
177                 | GridData.HORIZONTAL_ALIGN_FILL));
178         errorMessageText.setBackground(errorMessageText.getDisplay()
179                 .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
180         // Set the error message text
181
// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=66292
182
setErrorMessage(errorMessage);
183
184         applyDialogFont(composite);
185         return composite;
186     }
187
188     /**
189      * Returns the error message label.
190      *
191      * @return the error message label
192      * @deprecated use setErrorMessage(String) instead
193      */

194     protected Label getErrorMessageLabel() {
195         return null;
196     }
197
198     /**
199      * Returns the ok button.
200      *
201      * @return the ok button
202      */

203     protected Button getOkButton() {
204         return okButton;
205     }
206
207     /**
208      * Returns the text area.
209      *
210      * @return the text area
211      */

212     protected Text getText() {
213         return text;
214     }
215
216     /**
217      * Returns the validator.
218      *
219      * @return the validator
220      */

221     protected IInputValidator getValidator() {
222         return validator;
223     }
224
225     /**
226      * Returns the string typed into this input dialog.
227      *
228      * @return the input string
229      */

230     public String JavaDoc getValue() {
231         return value;
232     }
233
234     /**
235      * Validates the input.
236      * <p>
237      * The default implementation of this framework method delegates the request
238      * to the supplied input validator object; if it finds the input invalid,
239      * the error message is displayed in the dialog's message line. This hook
240      * method is called whenever the text changes in the input field.
241      * </p>
242      */

243     protected void validateInput() {
244         String JavaDoc errorMessage = null;
245         if (validator != null) {
246             errorMessage = validator.isValid(text.getText());
247         }
248         // Bug 16256: important not to treat "" (blank error) the same as null
249
// (no error)
250
setErrorMessage(errorMessage);
251     }
252
253     /**
254      * Sets or clears the error message.
255      * If not <code>null</code>, the OK button is disabled.
256      *
257      * @param errorMessage
258      * the error message, or <code>null</code> to clear
259      * @since 3.0
260      */

261     public void setErrorMessage(String JavaDoc errorMessage) {
262         this.errorMessage = errorMessage;
263         if (errorMessageText != null && !errorMessageText.isDisposed()) {
264             errorMessageText.setText(errorMessage == null ? " \n " : errorMessage); //$NON-NLS-1$
265
// Disable the error message text control if there is no error, or
266
// no error text (empty or whitespace only). Hide it also to avoid
267
// color change.
268
// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=130281
269
boolean hasError = errorMessage != null && (StringConverter.removeWhiteSpaces(errorMessage)).length() > 0;
270             errorMessageText.setEnabled(hasError);
271             errorMessageText.setVisible(hasError);
272             errorMessageText.getParent().update();
273             // Access the ok button by id, in case clients have overridden button creation.
274
// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=113643
275
Control button = getButton(IDialogConstants.OK_ID);
276             if (button != null) {
277                 button.setEnabled(errorMessage == null);
278             }
279         }
280     }
281 }
282
Popular Tags