KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > StringValueInputDialog


1 /*******************************************************************************
2  * Copyright (c) 2004, 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 implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui.actions;
12
13 import org.eclipse.debug.core.DebugException;
14 import org.eclipse.jdt.debug.core.IJavaVariable;
15 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
16 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
17 import org.eclipse.jface.dialogs.Dialog;
18 import org.eclipse.jface.dialogs.IDialogSettings;
19 import org.eclipse.jface.text.Document;
20 import org.eclipse.jface.text.TextViewer;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.custom.StyledText;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Group;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.ui.IWorkbench;
33 import org.eclipse.ui.PlatformUI;
34
35 /**
36  * A dialog which prompts the user to enter a new value for a
37  * String variable. The user is given the option of entering the
38  * literal value that they want assigned to the String or entering
39  * an expression for evaluation.
40  */

41 public class StringValueInputDialog extends ExpressionInputDialog {
42
43     private TextViewer fTextViewer;
44     private Button fTextButton;
45     private Button fEvaluationButton;
46     private Button fWrapText;
47     private Group fTextGroup;
48     
49     private boolean fUseLiteralValue= true;
50     private static final String JavaDoc USE_EVALUATION = "USE_EVALUATION"; //$NON-NLS-1$
51
private static final String JavaDoc WRAP_TEXT = "WRAP_TEXT"; //$NON-NLS-1$
52

53     /**
54      * @param parentShell
55      * @param variable
56      */

57     protected StringValueInputDialog(Shell parentShell, IJavaVariable variable) {
58         super(parentShell, variable);
59     }
60     
61     /* (non-Javadoc)
62      * @see org.eclipse.jdt.internal.debug.ui.actions.ExpressionInputDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
63      */

64     protected Control createDialogArea(Composite parent) {
65         IWorkbench workbench = PlatformUI.getWorkbench();
66         workbench.getHelpSystem().setHelp(
67                 parent,
68                 IJavaDebugHelpContextIds.STRING_VALUE_INPUT_DIALOG);
69         return super.createDialogArea(parent);
70     }
71     
72     /**
73      * Override superclass method to insert toggle buttons
74      * immediately after the input area.
75      */

76     protected void createInputArea(Composite parent) {
77         super.createInputArea(parent);
78         createRadioButtons(parent);
79         Dialog.applyDialogFont(parent);
80     }
81
82     /**
83      * Override superclass method to create the appropriate viewer
84      * (source viewer or simple text viewer) in the input area.
85      */

86     protected void populateInputArea() {
87         boolean useEvaluation= false;
88         IDialogSettings settings = getDialogSettings();
89         if (settings != null) {
90             useEvaluation= settings.getBoolean(USE_EVALUATION);
91         }
92         if (useEvaluation) {
93             createSourceViewer();
94             fUseLiteralValue= false;
95             fEvaluationButton.setSelection(true);
96         } else {
97             createTextViewer();
98             fTextButton.setSelection(true);
99         }
100     }
101
102     /**
103      * Creates the text viewer that allows the user to enter a new String
104      * value.
105      */

106     private void createTextViewer() {
107         fTextGroup= new Group(fInputArea, SWT.NONE);
108         fTextGroup.setLayout(new GridLayout());
109         fTextGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
110         fTextGroup.setText(ActionMessages.StringValueInputDialog_0);
111
112         Composite parent= fTextGroup;
113         
114         fTextViewer= new TextViewer(parent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
115         fTextViewer.setDocument(new Document());
116         GridData gridData = new GridData(GridData.FILL_BOTH);
117         gridData.widthHint= 300;
118         gridData.heightHint= 150;
119         fTextViewer.getTextWidget().setLayoutData(gridData);
120         try {
121             String JavaDoc valueString = fVariable.getValue().getValueString();
122             fTextViewer.getDocument().set(valueString);
123             fTextViewer.setSelectedRange(0, valueString.length());
124         } catch (DebugException e) {
125             JDIDebugUIPlugin.log(e);
126         }
127         fTextViewer.getControl().setFocus();
128         fWrapText= new Button(parent, SWT.CHECK);
129         fWrapText.setText(ActionMessages.StringValueInputDialog_4);
130         boolean wrap= true;
131         IDialogSettings settings = getDialogSettings();
132         if (settings != null) {
133             wrap= settings.getBoolean(WRAP_TEXT);
134         }
135         fWrapText.setSelection(wrap);
136         updateWordWrap();
137         fWrapText.addSelectionListener(new SelectionAdapter() {
138             public void widgetSelected(SelectionEvent e) {
139                 updateWordWrap();
140             }
141         });
142         
143         Dialog.applyDialogFont(fInputArea);
144     }
145     
146     private void updateWordWrap() {
147         fTextViewer.getTextWidget().setWordWrap(fWrapText.getSelection());
148     }
149
150     /**
151      * Creates the radio buttons that allow the user to choose between
152      * simple text mode and evaluation mode.
153      */

154     protected void createRadioButtons(Composite parent) {
155         fTextButton= new Button(parent, SWT.RADIO);
156         fTextButton.setText(ActionMessages.StringValueInputDialog_1);
157         fTextButton.addSelectionListener(new SelectionAdapter() {
158             public void widgetSelected(SelectionEvent e) {
159                 handleRadioSelectionChanged();
160             }
161         });
162         fEvaluationButton= new Button(parent, SWT.RADIO);
163         fEvaluationButton.setText(ActionMessages.StringValueInputDialog_2);
164     }
165     
166     /**
167      * The radio button selection has changed update the input widgetry
168      * to reflect the user's selection.
169      */

170     private void handleRadioSelectionChanged() {
171         boolean literal = fTextButton.getSelection();
172         if (literal != fUseLiteralValue) {
173             fUseLiteralValue= literal;
174             if (fUseLiteralValue) {
175                 disposeSourceViewer();
176                 createTextViewer();
177             } else {
178                 // Evaluation button selected
179
disposeTextViewer();
180                 createSourceViewer();
181             }
182             fInputArea.layout(true, true);
183         }
184     }
185     
186     /**
187      * Disposes of the text viewer and associated widgets.
188      */

189     protected void disposeTextViewer() {
190         if (fTextGroup != null) {
191             fTextGroup.dispose();
192             fTextGroup= null;
193         }
194        
195         if (fTextViewer != null) {
196             StyledText textWidget = fTextViewer.getTextWidget();
197             if (textWidget != null) {
198                 textWidget.dispose();
199             }
200             fTextViewer= null;
201         }
202         if (fWrapText != null) {
203             fWrapText.dispose();
204             fWrapText= null;
205         }
206     }
207     
208     /**
209      * Updates the error message based on the user's input.
210      */

211     protected void refreshValidState() {
212         if (fSourceViewer != null) {
213             super.refreshValidState();
214             return;
215         }
216         // even an empty string is valid
217
setErrorMessage(null);
218     }
219     
220     /**
221      * Override superclass method to persist user's evaluation/literal mode
222      * selection.
223      */

224     protected void okPressed() {
225         IDialogSettings settings= getDialogSettings();
226         if (settings == null) {
227             settings= JDIDebugUIPlugin.getDefault().getDialogSettings().addNewSection(getDialogSettingsSectionName());
228         }
229         settings.put(USE_EVALUATION, fEvaluationButton.getSelection());
230         if (fWrapText != null) {
231             settings.put(WRAP_TEXT, fWrapText.getSelection());
232         }
233         super.okPressed();
234     }
235     
236     /**
237      * Returns <code>true</code> if this dialog's result should be interpreted
238      * as a literal value and <code>false</code> if the result should be interpreted
239      * as an expression for evaluation.
240      *
241      * @return whether or not this dialog's result is a literal value.
242      */

243     public boolean isUseLiteralValue() {
244         return fUseLiteralValue;
245     }
246     
247     /**
248      * Override superclass method to return text from the simple text
249      * viewer if appropriate.
250      * @see ExpressionInputDialog#getText()
251      */

252     protected String JavaDoc getText() {
253         if (fTextButton.getSelection()) {
254             return fTextViewer.getDocument().get();
255         }
256         return super.getText();
257     }
258     
259     /**
260      * Override superclass method to dispose of the simple text viewer
261      * if appropriate.
262      * @see ExpressionInputDialog#dispose()
263      */

264     protected void dispose() {
265         if (fTextButton.getSelection()) {
266             disposeTextViewer();
267         } else {
268             super.dispose();
269         }
270     }
271     
272     /**
273      * Returns the dialog settings used for this dialog
274      * @return the dialog settings used for this dialog
275      */

276     protected IDialogSettings getDialogSettings() {
277         return JDIDebugUIPlugin.getDefault().getDialogSettings().getSection(getDialogSettingsSectionName());
278     }
279     
280     /* (non-Javadoc)
281      * @see org.eclipse.jdt.internal.debug.ui.actions.ExpressionInputDialog#getDialogSettingsSectionName()
282      */

283     protected String JavaDoc getDialogSettingsSectionName() {
284         return "STRING_VALUE_INPUT_DIALOG"; //$NON-NLS-1$
285
}
286 }
287
Popular Tags