1 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 62 public class ExpressionInputDialog extends TrayDialog { 63 64 protected IJavaVariable fVariable; 65 protected String fResult= null; 66 67 protected Composite fInputArea; 70 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 Text fErrorText; 80 81 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 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 createInputArea(composite); 103 createErrorText(composite); 105 106 populateInputArea(); 109 return composite; 110 } 111 112 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 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 145 protected void populateInputArea() { 146 createSourceViewer(); 147 } 148 149 153 protected void createSourceViewer() { 154 Composite parent= fInputArea; 155 String 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 [] {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 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 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 233 protected String getInitialText(IJavaVariable variable) { 234 try { 235 String signature = variable.getSignature(); 236 if (signature.equals("Ljava/lang/String;")) { IValue value = variable.getValue(); 238 if (!(value instanceof JDINullValue)) { 239 String currentValue= value.getValueString(); 240 StringBuffer buffer= new StringBuffer (currentValue.length()); 241 buffer.append('"'); 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"); } else if (c == '\t') { 248 buffer.append("\\t"); } else if (c == '\n') { 250 buffer.append("\\n"); } else if (c == '\f') { 252 buffer.append("\\f"); } else if (c == '\r') { 254 buffer.append("\\r"); } else if (c == '"') { 256 buffer.append("\\\""); } else if (c == '\'') { 258 buffer.append("\\\'"); } else if (c == '\\') { 260 buffer.append("\\\\"); } else { 262 buffer.append(c); 263 } 264 } 265 buffer.append('"'); return buffer.toString(); 267 } 268 } 269 } catch (DebugException e) { 270 } 271 return null; 272 } 273 274 278 protected IContentAssistProcessor getCompletionProcessor() { 279 if (fCompletionProcessor == null) { 280 fCompletionProcessor= new JavaDebugContentAssistProcessor(new CurrentFrameContext()); 281 } 282 return fCompletionProcessor; 283 } 284 285 288 protected void refreshValidState() { 289 String errorMessage= null; 290 String 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 304 protected void setErrorMessage(String message) { 305 if (message == null) { 306 message= ""; } 308 fErrorText.setText(message); 309 getButton(IDialogConstants.OK_ID).setEnabled(message.length() == 0); 310 } 311 312 315 protected void okPressed() { 316 fResult= getText(); 317 dispose(); 318 super.okPressed(); 319 } 320 321 325 protected String getText() { 326 return fSourceViewer.getDocument().get(); 327 } 328 329 333 protected void dispose() { 334 disposeSourceViewer(); 335 } 336 337 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 362 public String getResult() { 363 return fResult; 364 } 365 366 369 protected void configureShell(Shell newShell) { 370 super.configureShell(newShell); 371 newShell.setText(ActionMessages.ExpressionInputDialog_2); 372 } 373 374 378 protected void createButtonsForButtonBar(Composite parent) { 379 super.createButtonsForButtonBar(parent); 380 refreshValidState(); 383 } 384 385 388 public boolean close() { 389 dispose(); 390 return super.close(); 391 } 392 393 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 getDialogSettingsSectionName() { 406 return "EXPRESSION_INPUT_DIALOG"; } 408 } 409 | Popular Tags |