KickJava   Java API By Example, From Geeks To Geeks.

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


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 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.variables;
12
13 import org.eclipse.debug.internal.ui.DebugUIPlugin;
14 import org.eclipse.debug.ui.IDebugUIConstants;
15 import org.eclipse.jface.dialogs.IDialogConstants;
16 import org.eclipse.jface.dialogs.IDialogSettings;
17 import org.eclipse.jface.dialogs.IInputValidator;
18 import org.eclipse.jface.dialogs.TrayDialog;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.ModifyEvent;
21 import org.eclipse.swt.events.ModifyListener;
22 import org.eclipse.swt.graphics.Font;
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 TrayDialog {
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         return composite;
184     }
185     /**
186      * Returns the error message label.
187      *
188      * @return the error message label
189      */

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

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

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

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

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

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

262     protected String JavaDoc getDialogSettingsSectionName() {
263         return IDebugUIConstants.PLUGIN_ID + ".CHANGE_VARIABLE_VALUE_DIALOG_SECTION"; //$NON-NLS-1$
264
}
265     
266      /* (non-Javadoc)
267      * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsSettings()
268      */

269     protected IDialogSettings getDialogBoundsSettings() {
270          IDialogSettings settings = DebugUIPlugin.getDefault().getDialogSettings();
271          IDialogSettings section = settings.getSection(getDialogSettingsSectionName());
272          if (section == null) {
273              section = settings.addNewSection(getDialogSettingsSectionName());
274          }
275          return section;
276     }
277     
278     /* (non-Javadoc)
279      * @see org.eclipse.jface.window.Window#handleShellCloseEvent()
280      */

281     protected void handleShellCloseEvent() {
282         value= null;
283         super.handleShellCloseEvent();
284     }
285 }
286
Popular Tags