KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > ChangeVariableValueInputDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.debug.internal.ui.actions;
12
13 import org.eclipse.debug.internal.ui.DialogSettingsHelper;
14 import org.eclipse.debug.ui.IDebugUIConstants;
15 import org.eclipse.jface.dialogs.Dialog;
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.jface.dialogs.IInputValidator;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.ModifyEvent;
20 import org.eclipse.swt.events.ModifyListener;
21 import org.eclipse.swt.graphics.Font;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.swt.widgets.Text;
30
31 /**
32  * A simple input dialog for soliciting an input string
33  * from the user.
34  * <p>
35  * This concete dialog class can be instantiated as is,
36  * or further subclassed as required.
37  * </p>
38  */

39 public class ChangeVariableValueInputDialog extends Dialog {
40     
41     
42     /**
43      * The title of the dialog.
44      */

45     private String JavaDoc title;
46     
47     /**
48      * The message to display, or <code>null</code> if none.
49      */

50     private String JavaDoc message;
51     
52     /**
53      * The input value; the empty string by default.
54      */

55     private String JavaDoc value= "";//$NON-NLS-1$
56

57     /**
58      * The input validator, or <code>null</code> if none.
59      */

60     private IInputValidator validator;
61     
62     /**
63      * Ok button widget.
64      */

65     private Button okButton;
66     
67     /**
68      * Input text widget.
69      */

70     private Text text;
71     
72     /**
73      * Error message label widget.
74      */

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

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

106     protected void buttonPressed(int buttonId) {
107         if (buttonId == IDialogConstants.OK_ID) {
108             value= text.getText();
109         } else {
110             value= null;
111         }
112         super.buttonPressed(buttonId);
113     }
114     /* (non-Javadoc)
115      * Method declared in Window.
116      */

117     protected void configureShell(Shell shell) {
118         super.configureShell(shell);
119         if (title != null)
120             shell.setText(title);
121     }
122     /* (non-Javadoc)
123      * Method declared on Dialog.
124      */

125     protected void createButtonsForButtonBar(Composite parent) {
126         // create OK and Cancel buttons by default
127
okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
128         createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
129         
130         //do this here because setting the text will set enablement on the ok button
131
text.setFocus();
132         if (value != null) {
133             text.setText(value);
134             text.selectAll();
135         }
136     }
137     /* (non-Javadoc)
138      * Method declared on Dialog.
139      */

140     protected Control createDialogArea(Composite parent) {
141         Font font = parent.getFont();
142         // create composite
143
Composite composite = (Composite)super.createDialogArea(parent);
144         
145         // create message
146
if (message != null) {
147             Label label = new Label(composite, SWT.WRAP);
148             label.setText(message);
149             GridData data = new GridData(
150                     GridData.GRAB_HORIZONTAL |
151                     GridData.HORIZONTAL_ALIGN_FILL |
152                     GridData.VERTICAL_ALIGN_CENTER);
153             data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
154             label.setLayoutData(data);
155             label.setFont(font);
156         }
157         
158         text= new Text(composite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL| SWT.H_SCROLL);
159         
160         GridData gridData= new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_FILL);
161         gridData.heightHint = 50;
162         gridData.widthHint = 100;
163         text.setLayoutData(gridData);
164         text.setFont(font);
165         text.addModifyListener(
166                 new ModifyListener() {
167                     public void modifyText(ModifyEvent e) {
168                         if (okButton.isEnabled()) {
169                             return;
170                         }
171                         errorMessageLabel.setText(""); //$NON-NLS-1$
172
errorMessageLabel.getParent().update();
173                         okButton.setEnabled(true);
174                     }
175                 }
176         );
177         
178         errorMessageLabel = new Label(composite, SWT.NONE);
179         errorMessageLabel.setLayoutData(new GridData(
180                 GridData.GRAB_HORIZONTAL |
181                 GridData.HORIZONTAL_ALIGN_FILL));
182         errorMessageLabel.setFont(font);
183         
184         
185         return composite;
186     }
187     /**
188      * Returns the error message label.
189      *
190      * @return the error message label
191      */

192     protected Label getErrorMessageLabel() {
193         return errorMessageLabel;
194     }
195     /**
196      * Returns the ok button.
197      *
198      * @return the ok button
199      */

200     protected Button getOkButton() {
201         return okButton;
202     }
203     /**
204      * Returns the text area.
205      *
206      * @return the text area
207      */

208     protected Text getText() {
209         return text;
210     }
211     /**
212      * Returns the validator.
213      *
214      * @return the validator
215      */

216     protected IInputValidator getValidator() {
217         return validator;
218     }
219     /**
220      * Returns the string typed into this input dialog.
221      *
222      * @return the input string
223      */

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

238     protected void validateInput() {
239         
240         String JavaDoc errorMessage = null;
241         
242         if (validator != null) {
243             errorMessage = validator.isValid(text.getText());
244         }
245         
246         // Bug 16256: important not to treat "" (blank error) the same as null (no error)
247
errorMessageLabel.setText(errorMessage == null ? "" : errorMessage); //$NON-NLS-1$
248
okButton.setEnabled(errorMessage == null);
249         
250         errorMessageLabel.getParent().update();
251     }
252     protected void okPressed() {
253         validateInput();
254         if (okButton.isEnabled()) {
255             super.okPressed();
256         }
257     }
258     
259     /**
260      * Returns the name of the section that this dialog stores its settings in
261      *
262      * @return String
263      */

264     protected String JavaDoc getDialogSettingsSectionName() {
265         return IDebugUIConstants.PLUGIN_ID + ".CHANGE_VARIABLE_VALUE_DIALOG_SECTION"; //$NON-NLS-1$
266
}
267     
268     /* (non-Javadoc)
269      * @see org.eclipse.jface.window.Window#close()
270      */

271     public boolean close() {
272         DialogSettingsHelper.persistShellGeometry(getShell(), getDialogSettingsSectionName());
273         return super.close();
274     }
275     
276     /* (non-Javadoc)
277      * @see org.eclipse.jface.window.Window#getInitialLocation(org.eclipse.swt.graphics.Point)
278      */

279     protected Point getInitialLocation(Point initialSize) {
280         Point initialLocation= DialogSettingsHelper.getInitialLocation(getDialogSettingsSectionName());
281         if (initialLocation != null) {
282             return initialLocation;
283         }
284         return super.getInitialLocation(initialSize);
285     }
286     
287     
288     /* (non-Javadoc)
289      * @see org.eclipse.jface.window.Window#getInitialSize()
290      */

291     protected Point getInitialSize() {
292         Point size = super.getInitialSize();
293         return DialogSettingsHelper.getInitialSize(getDialogSettingsSectionName(), size);
294     }
295     
296     /* (non-Javadoc)
297      * @see org.eclipse.jface.window.Window#handleShellCloseEvent()
298      */

299     protected void handleShellCloseEvent() {
300         value= null;
301         super.handleShellCloseEvent();
302     }
303 }
304
Popular Tags