KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.ibm.icu.text.MessageFormat;
14
15 import org.eclipse.core.commands.AbstractHandler;
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.IHandler;
18 import org.eclipse.debug.core.DebugException;
19 import org.eclipse.debug.core.model.IValue;
20 import org.eclipse.jdt.debug.core.IJavaVariable;
21 import org.eclipse.jdt.internal.debug.core.model.JDINullValue;
22 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
23 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
24 import org.eclipse.jdt.internal.debug.ui.JDISourceViewer;
25 import org.eclipse.jdt.internal.debug.ui.contentassist.CurrentFrameContext;
26 import org.eclipse.jdt.internal.debug.ui.contentassist.JavaDebugContentAssistProcessor;
27 import org.eclipse.jdt.internal.debug.ui.display.DisplayViewerConfiguration;
28 import org.eclipse.jdt.ui.text.IJavaPartitions;
29 import org.eclipse.jdt.ui.text.JavaTextTools;
30 import org.eclipse.jface.dialogs.Dialog;
31 import org.eclipse.jface.dialogs.IDialogConstants;
32 import org.eclipse.jface.dialogs.IDialogSettings;
33 import org.eclipse.jface.dialogs.TrayDialog;
34 import org.eclipse.jface.resource.JFaceResources;
35 import org.eclipse.jface.text.Document;
36 import org.eclipse.jface.text.DocumentEvent;
37 import org.eclipse.jface.text.IDocument;
38 import org.eclipse.jface.text.IDocumentListener;
39 import org.eclipse.jface.text.ITextOperationTarget;
40 import org.eclipse.jface.text.IUndoManager;
41 import org.eclipse.jface.text.TextViewerUndoManager;
42 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
43 import org.eclipse.jface.text.source.ISourceViewer;
44 import org.eclipse.swt.SWT;
45 import org.eclipse.swt.layout.GridData;
46 import org.eclipse.swt.layout.GridLayout;
47 import org.eclipse.swt.widgets.Composite;
48 import org.eclipse.swt.widgets.Control;
49 import org.eclipse.swt.widgets.Label;
50 import org.eclipse.swt.widgets.Shell;
51 import org.eclipse.swt.widgets.Text;
52 import org.eclipse.ui.IWorkbench;
53 import org.eclipse.ui.PlatformUI;
54 import org.eclipse.ui.handlers.IHandlerActivation;
55 import org.eclipse.ui.handlers.IHandlerService;
56 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds;
57
58 /**
59  * A dialog which prompts the user to enter an expression for
60  * evaluation.
61  */

62 public class ExpressionInputDialog extends TrayDialog {
63
64     protected IJavaVariable fVariable;
65     protected String JavaDoc fResult= null;
66     
67     // Input area composite which acts as a placeholder for
68
// input widgetry that is created/disposed dynamically.
69
protected Composite fInputArea;
70     // Source viewer widgets
71
protected Label fEvaluateLabel;
72     protected JDISourceViewer fSourceViewer;
73     protected IContentAssistProcessor fCompletionProcessor;
74     protected IDocumentListener fDocumentListener;
75     protected IHandlerService fService;
76     protected IHandlerActivation fActivation;
77 // protected HandlerSubmission fSubmission;
78
// Text for error reporting
79
protected Text fErrorText;
80     
81     /**
82      * @param parentShell
83      */

84     protected ExpressionInputDialog(Shell parentShell, IJavaVariable variable) {
85         super(parentShell);
86         setShellStyle(SWT.CLOSE|SWT.MIN|SWT.MAX|SWT.RESIZE);
87         fVariable= variable;
88     }
89
90     /**
91      * Creates and populates the dialog area
92      */

93     protected Control createDialogArea(Composite parent) {
94         IWorkbench workbench = PlatformUI.getWorkbench();
95         workbench.getHelpSystem().setHelp(
96                 parent,
97                 IJavaDebugHelpContextIds.EXPRESSION_INPUT_DIALOG);
98         
99         Composite composite= (Composite) super.createDialogArea(parent);
100         
101         // Create the composite which will hold the input widgetry
102
createInputArea(composite);
103         // Create the error reporting text area
104
createErrorText(composite);
105
106         // Create the source viewer after creating the error text so that any
107
// necessary error messages can be set.
108
populateInputArea();
109         return composite;
110     }
111     
112     /**
113      * Creates the text widget for reporting errors
114      */

115     protected void createErrorText(Composite parent) {
116         fErrorText= new Text(parent, SWT.READ_ONLY);
117         fErrorText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
118                 | GridData.HORIZONTAL_ALIGN_FILL));
119         fErrorText.setBackground(fErrorText.getDisplay()
120                 .getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
121         fErrorText.setFont(parent.getFont());
122     }
123
124     /**
125      * Creates the composite that will be used to contain the
126      * input widgetry.
127      * @param composite the parent composite
128      */

129     protected void createInputArea(Composite parent) {
130         fInputArea= new Composite(parent, SWT.NONE);
131         GridData gridData = new GridData(GridData.FILL_BOTH);
132         fInputArea.setLayoutData(gridData);
133         GridLayout layout = new GridLayout();
134         layout.marginHeight= 0;
135         layout.marginWidth= 0;
136         fInputArea.setLayout(layout);
137         Dialog.applyDialogFont(fInputArea);
138     }
139     
140     /**
141      * Creates the appropriate widgetry in the input area. This
142      * method is intended to be overridden by subclasses who wish
143      * to use alternate input widgets.
144      */

145     protected void populateInputArea() {
146         createSourceViewer();
147     }
148
149     /**
150      * Creates the source viewer that allows the user to enter
151      * an evaluation expression.
152      */

153     protected void createSourceViewer() {
154         Composite parent= fInputArea;
155         String JavaDoc name= ActionMessages.ExpressionInputDialog_3;
156         try {
157             name= fVariable.getName();
158         } catch (DebugException e) {
159             JDIDebugUIPlugin.log(e);
160         }
161         
162         fEvaluateLabel= new Label(parent, SWT.WRAP);
163         fEvaluateLabel.setText(MessageFormat.format(ActionMessages.ExpressionInputDialog_0, new String JavaDoc[] {name}));
164         GridData data = new GridData(GridData.FILL_HORIZONTAL);
165         data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
166         fEvaluateLabel.setLayoutData(data);
167         fEvaluateLabel.setFont(parent.getFont());
168         
169         fSourceViewer= new JDISourceViewer(parent, null, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
170         fSourceViewer.setInput(parent);
171         configureSourceViewer();
172         fSourceViewer.doOperation(ITextOperationTarget.SELECT_ALL);
173     }
174     
175     /**
176      * Initializes the source viewer. This method is based on code in BreakpointConditionEditor.
177      */

178     private void configureSourceViewer() {
179         JavaTextTools tools= JDIDebugUIPlugin.getDefault().getJavaTextTools();
180         IDocument document= new Document();
181         tools.setupJavaDocumentPartitioner(document, IJavaPartitions.JAVA_PARTITIONING);
182         fSourceViewer.configure(new DisplayViewerConfiguration() {
183             public IContentAssistProcessor getContentAssistantProcessor() {
184                 return getCompletionProcessor();
185             }
186         });
187         fSourceViewer.setEditable(true);
188         fSourceViewer.setDocument(document);
189         final IUndoManager undoManager= new TextViewerUndoManager(10);
190         fSourceViewer.setUndoManager(undoManager);
191         undoManager.connect(fSourceViewer);
192         
193         fSourceViewer.getTextWidget().setFont(JFaceResources.getTextFont());
194             
195         Control control= fSourceViewer.getControl();
196         GridData gd = new GridData(GridData.FILL_BOTH);
197         control.setLayoutData(gd);
198             
199         gd= (GridData)fSourceViewer.getControl().getLayoutData();
200         gd.heightHint= convertHeightInCharsToPixels(10);
201         gd.widthHint= convertWidthInCharsToPixels(40);
202         document.set(getInitialText(fVariable));
203         
204         fDocumentListener= new IDocumentListener() {
205             public void documentAboutToBeChanged(DocumentEvent event) {
206             }
207             public void documentChanged(DocumentEvent event) {
208                 refreshValidState();
209             }
210         };
211         fSourceViewer.getDocument().addDocumentListener(fDocumentListener);
212         
213         IHandler handler = new AbstractHandler() {
214             public Object JavaDoc execute(ExecutionEvent event) throws org.eclipse.core.commands.ExecutionException {
215                  fSourceViewer.doOperation(ISourceViewer.CONTENTASSIST_PROPOSALS);
216                 return null;
217             }
218         };
219         IWorkbench workbench = PlatformUI.getWorkbench();
220         fService = (IHandlerService)workbench.getAdapter(IHandlerService.class);
221         fActivation = fService.activateHandler(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS, handler);
222     }
223     
224     /**
225      * Returns the text that should be shown in the source viewer upon
226      * initialization. The text should be presented in such a way that
227      * it can be used as an evaluation expression which will return the
228      * current value.
229      * @param variable the variable
230      * @return the initial text to display in the source viewer or <code>null</code>
231      * if none.
232      */

233     protected String JavaDoc getInitialText(IJavaVariable variable) {
234         try {
235             String JavaDoc signature = variable.getSignature();
236             if (signature.equals("Ljava/lang/String;")) { //$NON-NLS-1$
237
IValue value = variable.getValue();
238                 if (!(value instanceof JDINullValue)) {
239                     String JavaDoc currentValue= value.getValueString();
240                     StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(currentValue.length());
241                     buffer.append('"'); // Surround value in quotes
242
char[] chars = currentValue.toCharArray();
243                     for (int i = 0; i < chars.length; i++) {
244                         char c = chars[i];
245                         if (c == '\b') {
246                             buffer.append("\\b"); //$NON-NLS-1$
247
} else if (c == '\t') {
248                             buffer.append("\\t"); //$NON-NLS-1$
249
} else if (c == '\n') {
250                             buffer.append("\\n"); //$NON-NLS-1$
251
} else if (c == '\f') {
252                             buffer.append("\\f"); //$NON-NLS-1$
253
} else if (c == '\r') {
254                             buffer.append("\\r"); //$NON-NLS-1$
255
} else if (c == '"') {
256                             buffer.append("\\\""); //$NON-NLS-1$
257
} else if (c == '\'') {
258                             buffer.append("\\\'"); //$NON-NLS-1$
259
} else if (c == '\\') {
260                             buffer.append("\\\\"); //$NON-NLS-1$
261
} else {
262                             buffer.append(c);
263                         }
264                     }
265                     buffer.append('"'); // Surround value in quotes
266
return buffer.toString();
267                 }
268             }
269         } catch (DebugException e) {
270         }
271         return null;
272     }
273
274     /**
275      * Return the completion processor associated with this viewer.
276      * @return DisplayConditionCompletionProcessor
277      */

278     protected IContentAssistProcessor getCompletionProcessor() {
279         if (fCompletionProcessor == null) {
280             fCompletionProcessor= new JavaDebugContentAssistProcessor(new CurrentFrameContext());
281         }
282         return fCompletionProcessor;
283     }
284     
285     /**
286      * @see org.eclipse.jface.preference.FieldEditor#refreshValidState()
287      */

288     protected void refreshValidState() {
289         String JavaDoc errorMessage= null;
290         String JavaDoc text= fSourceViewer.getDocument().get();
291         boolean valid= text != null && text.trim().length() > 0;
292         if (!valid) {
293             errorMessage= ActionMessages.ExpressionInputDialog_1;
294         }
295         setErrorMessage(errorMessage);
296     }
297     
298     /**
299      * Sets the error message to display to the user. <code>null</code>
300      * is the same as the empty string.
301      * @param message the error message to display to the user or
302      * <code>null</code> if the error message should be cleared
303      */

304     protected void setErrorMessage(String JavaDoc message) {
305         if (message == null) {
306             message= ""; //$NON-NLS-1$
307
}
308         fErrorText.setText(message);
309         getButton(IDialogConstants.OK_ID).setEnabled(message.length() == 0);
310     }
311     
312     /**
313      * Persist the dialog size and store the user's input on OK is pressed.
314      */

315     protected void okPressed() {
316         fResult= getText();
317         dispose();
318         super.okPressed();
319     }
320     
321     /**
322      * Returns the text that is currently displayed in the source viewer.
323      * @return the text that is currently displayed in the source viewer
324      */

325     protected String JavaDoc getText() {
326         return fSourceViewer.getDocument().get();
327     }
328     
329     /**
330      * Disposes the source viewer. This method is intended to be overridden
331      * by subclasses.
332      */

333     protected void dispose() {
334         disposeSourceViewer();
335     }
336
337     /**
338      * Disposes the source viewer and all associated widgetry.
339      */

340     protected void disposeSourceViewer() {
341         if(fActivation != null) {
342             fService.deactivateHandler(fActivation);
343         }
344         if (fSourceViewer != null) {
345             fSourceViewer.getDocument().removeDocumentListener(fDocumentListener);
346             fSourceViewer.getTextWidget().dispose();
347             fSourceViewer.dispose();
348             fSourceViewer= null;
349         }
350         if (fEvaluateLabel != null) {
351             fEvaluateLabel.dispose();
352             fEvaluateLabel= null;
353         }
354         fDocumentListener= null;
355         fCompletionProcessor= null;
356     }
357     
358     /**
359      * Returns the text entered by the user or <code>null</code> if the user cancelled.
360      * @return the text entered by the user or <code>null</code> if the user cancelled
361      */

362     public String JavaDoc getResult() {
363         return fResult;
364     }
365     
366     /**
367      * Initializes the dialog shell with a title.
368      */

369     protected void configureShell(Shell newShell) {
370         super.configureShell(newShell);
371         newShell.setText(ActionMessages.ExpressionInputDialog_2);
372     }
373     
374     /**
375      * Override method to initialize the enablement of the OK button after
376      * it is created.
377      */

378     protected void createButtonsForButtonBar(Composite parent) {
379         super.createButtonsForButtonBar(parent);
380         //do this here because setting the text will set enablement on the ok
381
// button
382
refreshValidState();
383     }
384     
385     /* (non-Javadoc)
386      * @see org.eclipse.jface.window.Window#close()
387      */

388     public boolean close() {
389         dispose();
390         return super.close();
391     }
392     
393     /* (non-Javadoc)
394      * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsSettings()
395      */

396     protected IDialogSettings getDialogBoundsSettings() {
397          IDialogSettings settings = JDIDebugUIPlugin.getDefault().getDialogSettings();
398          IDialogSettings section = settings.getSection(getDialogSettingsSectionName());
399          if (section == null) {
400              section = settings.addNewSection(getDialogSettingsSectionName());
401          }
402          return section;
403     }
404     
405     protected String JavaDoc getDialogSettingsSectionName() {
406         return "EXPRESSION_INPUT_DIALOG"; //$NON-NLS-1$
407
}
408 }
409
Popular Tags