1 11 package org.eclipse.jdt.internal.debug.ui.display; 12 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.ResourceBundle ; 20 21 import org.eclipse.core.commands.AbstractHandler; 22 import org.eclipse.core.commands.ExecutionEvent; 23 import org.eclipse.core.commands.ExecutionException; 24 import org.eclipse.core.commands.IHandler; 25 import org.eclipse.core.commands.operations.IUndoContext; 26 import org.eclipse.debug.ui.DebugUITools; 27 import org.eclipse.debug.ui.IDebugUIConstants; 28 import org.eclipse.jdt.debug.ui.IJavaDebugUIConstants; 29 import org.eclipse.jdt.internal.debug.ui.EvaluationContextManager; 30 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds; 31 import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; 32 import org.eclipse.jdt.internal.debug.ui.JDISourceViewer; 33 import org.eclipse.jdt.ui.text.IJavaPartitions; 34 import org.eclipse.jdt.ui.text.JavaTextTools; 35 import org.eclipse.jface.action.IAction; 36 import org.eclipse.jface.action.IMenuListener; 37 import org.eclipse.jface.action.IMenuManager; 38 import org.eclipse.jface.action.IToolBarManager; 39 import org.eclipse.jface.action.MenuManager; 40 import org.eclipse.jface.action.Separator; 41 import org.eclipse.jface.text.BadLocationException; 42 import org.eclipse.jface.text.Document; 43 import org.eclipse.jface.text.DocumentEvent; 44 import org.eclipse.jface.text.IDocument; 45 import org.eclipse.jface.text.IDocumentListener; 46 import org.eclipse.jface.text.IFindReplaceTarget; 47 import org.eclipse.jface.text.ITextInputListener; 48 import org.eclipse.jface.text.ITextOperationTarget; 49 import org.eclipse.jface.text.ITextSelection; 50 import org.eclipse.jface.text.ITextViewer; 51 import org.eclipse.jface.text.IUndoManager; 52 import org.eclipse.jface.text.IUndoManagerExtension; 53 import org.eclipse.jface.text.source.ISourceViewer; 54 import org.eclipse.jface.viewers.ISelectionChangedListener; 55 import org.eclipse.jface.viewers.SelectionChangedEvent; 56 import org.eclipse.swt.SWT; 57 import org.eclipse.swt.widgets.Composite; 58 import org.eclipse.swt.widgets.Menu; 59 import org.eclipse.ui.IActionBars; 60 import org.eclipse.ui.IMemento; 61 import org.eclipse.ui.IPerspectiveDescriptor; 62 import org.eclipse.ui.IPerspectiveListener2; 63 import org.eclipse.ui.IViewReference; 64 import org.eclipse.ui.IViewSite; 65 import org.eclipse.ui.IWorkbenchActionConstants; 66 import org.eclipse.ui.IWorkbenchPage; 67 import org.eclipse.ui.IWorkbenchPartReference; 68 import org.eclipse.ui.PartInitException; 69 import org.eclipse.ui.PlatformUI; 70 import org.eclipse.ui.XMLMemento; 71 import org.eclipse.ui.actions.ActionFactory; 72 import org.eclipse.ui.console.actions.ClearOutputAction; 73 import org.eclipse.ui.handlers.IHandlerActivation; 74 import org.eclipse.ui.handlers.IHandlerService; 75 import org.eclipse.ui.operations.OperationHistoryActionHandler; 76 import org.eclipse.ui.operations.RedoActionHandler; 77 import org.eclipse.ui.operations.UndoActionHandler; 78 import org.eclipse.ui.part.ViewPart; 79 import org.eclipse.ui.texteditor.FindReplaceAction; 80 import org.eclipse.ui.texteditor.IAbstractTextEditorHelpContextIds; 81 import org.eclipse.ui.texteditor.ITextEditorActionConstants; 82 import org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds; 83 import org.eclipse.ui.texteditor.IUpdate; 84 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds; 85 86 public class DisplayView extends ViewPart implements ITextInputListener, IPerspectiveListener2 { 87 88 class DataDisplay implements IDataDisplay { 89 92 public void clear() { 93 IDocument document= fSourceViewer.getDocument(); 94 if (document != null) { 95 document.set(""); } 97 } 98 99 102 public void displayExpression(String expression) { 103 IDocument document= fSourceViewer.getDocument(); 104 int offset= document.getLength(); 105 try { 106 if (offset != document.getLineInformationOfOffset(offset).getOffset()) { 108 expression= System.getProperty("line.separator") + expression.trim(); } 110 fSourceViewer.getDocument().replace(offset, 0, expression); 111 fSourceViewer.setSelectedRange(offset + expression.length(), 0); 112 fSourceViewer.revealRange(offset, expression.length()); 113 } catch (BadLocationException ble) { 114 JDIDebugUIPlugin.log(ble); 115 } 116 } 117 118 121 public void displayExpressionValue(String value) { 122 value= System.getProperty("line.separator") + '\t' + value; ITextSelection selection= (ITextSelection)fSourceViewer.getSelection(); 124 125 int offset= selection.getOffset() + selection.getLength(); 126 int length= value.length(); 127 try { 128 fSourceViewer.getDocument().replace(offset, 0, value); 129 } catch (BadLocationException ble) { 130 JDIDebugUIPlugin.log(ble); 131 } 132 fSourceViewer.setSelectedRange(offset + length, 0); 133 fSourceViewer.revealRange(offset, length); 134 } 135 } 136 137 protected IDataDisplay fDataDisplay= new DataDisplay(); 138 protected IDocumentListener fDocumentListener= null; 139 140 protected JDISourceViewer fSourceViewer; 141 protected IAction fClearDisplayAction; 142 protected DisplayViewAction fContentAssistAction; 143 144 protected Map fGlobalActions= new HashMap (4); 145 protected List fSelectionActions= new ArrayList (3); 146 147 protected String fRestoredContents= null; 148 154 private static IMemento fgMemento; 155 private IHandlerActivation fHandlerActivation; 156 157 160 public void createPartControl(Composite parent) { 161 162 int styles= SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.FULL_SELECTION; 163 fSourceViewer= new JDISourceViewer(parent, null, styles); 164 fSourceViewer.configure(new DisplayViewerConfiguration()); 165 fSourceViewer.getSelectionProvider().addSelectionChangedListener(getSelectionChangedListener()); 166 IDocument doc= getRestoredDocument(); 167 fSourceViewer.setDocument(doc); 168 fSourceViewer.addTextInputListener(this); 169 fRestoredContents= null; 170 createActions(); 171 createUndoRedoActions(); 172 initializeToolBar(); 173 174 MenuManager menuMgr = new MenuManager("#PopUp"); menuMgr.setRemoveAllWhenShown(true); 177 menuMgr.addMenuListener(new IMenuListener() { 178 public void menuAboutToShow(IMenuManager mgr) { 179 fillContextMenu(mgr); 180 } 181 }); 182 183 Menu menu = menuMgr.createContextMenu(fSourceViewer.getTextWidget()); 184 fSourceViewer.getTextWidget().setMenu(menu); 185 getSite().registerContextMenu(menuMgr, fSourceViewer.getSelectionProvider()); 186 187 getSite().setSelectionProvider(fSourceViewer.getSelectionProvider()); 188 PlatformUI.getWorkbench().getHelpSystem().setHelp(fSourceViewer.getTextWidget(), IJavaDebugHelpContextIds.DISPLAY_VIEW); 189 getSite().getWorkbenchWindow().addPerspectiveListener(this); 190 } 191 192 protected IDocument getRestoredDocument() { 193 IDocument doc= null; 194 if (fRestoredContents != null) { 195 doc= new Document(fRestoredContents); 196 } else { 197 doc= new Document(); 198 } 199 JavaTextTools tools= JDIDebugUIPlugin.getDefault().getJavaTextTools(); 200 tools.setupJavaDocumentPartitioner(doc, IJavaPartitions.JAVA_PARTITIONING); 201 fDocumentListener= new IDocumentListener() { 202 205 public void documentAboutToBeChanged(DocumentEvent event) { 206 } 207 210 public void documentChanged(DocumentEvent event) { 211 updateAction(ActionFactory.FIND.getId()); 212 } 213 }; 214 doc.addDocumentListener(fDocumentListener); 215 216 return doc; 217 } 218 219 222 public void setFocus() { 223 if (fSourceViewer != null) { 224 fSourceViewer.getControl().setFocus(); 225 } 226 } 227 228 231 protected void createActions() { 232 233 fClearDisplayAction= new ClearOutputAction(fSourceViewer); 234 235 IAction action= new DisplayViewAction(this, ITextOperationTarget.CUT); 236 action.setText(DisplayMessages.DisplayView_Cut_label); 237 action.setToolTipText(DisplayMessages.DisplayView_Cut_tooltip); 238 action.setDescription(DisplayMessages.DisplayView_Cut_description); 239 setGlobalAction(ActionFactory.CUT.getId(), action); 240 241 action= new DisplayViewAction(this, ITextOperationTarget.COPY); 242 action.setText(DisplayMessages.DisplayView_Copy_label); 243 action.setToolTipText(DisplayMessages.DisplayView_Copy_tooltip); 244 action.setDescription(DisplayMessages.DisplayView_Copy_description); 245 setGlobalAction(ActionFactory.COPY.getId(), action); 246 247 action= new DisplayViewAction(this, ITextOperationTarget.PASTE); 248 action.setText(DisplayMessages.DisplayView_Paste_label); 249 action.setToolTipText(DisplayMessages.DisplayView_Paste_tooltip); 250 action.setDescription(DisplayMessages.DisplayView_Paste_Description); 251 setGlobalAction(ActionFactory.PASTE.getId(), action); 252 253 action= new DisplayViewAction(this, ITextOperationTarget.SELECT_ALL); 254 action.setText(DisplayMessages.DisplayView_SelectAll_label); 255 action.setToolTipText(DisplayMessages.DisplayView_SelectAll_tooltip); 256 action.setDescription(DisplayMessages.DisplayView_SelectAll_description); 257 setGlobalAction(ActionFactory.SELECT_ALL.getId(), action); 258 259 ResourceBundle bundle= ResourceBundle.getBundle("org.eclipse.jdt.internal.debug.ui.display.DisplayResourceBundleMessages"); FindReplaceAction findReplaceAction = new FindReplaceAction(bundle, "find_replace_action_", this); findReplaceAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_REPLACE); 263 setGlobalAction(ActionFactory.FIND.getId(), findReplaceAction); 264 265 fSelectionActions.add(ActionFactory.CUT.getId()); 266 fSelectionActions.add(ActionFactory.COPY.getId()); 267 fSelectionActions.add(ActionFactory.PASTE.getId()); 268 269 fContentAssistAction= new DisplayViewAction(this, ISourceViewer.CONTENTASSIST_PROPOSALS); 270 fContentAssistAction.setActionDefinitionId(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS); 271 fContentAssistAction.setText(DisplayMessages.DisplayView_Co_ntent_Assist_Ctrl_Space_1); 272 fContentAssistAction.setDescription(DisplayMessages.DisplayView_Content_Assist_2); 273 fContentAssistAction.setToolTipText(DisplayMessages.DisplayView_Content_Assist_2); 274 fContentAssistAction.setImageDescriptor(DebugUITools.getImageDescriptor(IDebugUIConstants.IMG_ELCL_CONTENT_ASSIST)); 275 fContentAssistAction.setHoverImageDescriptor(DebugUITools.getImageDescriptor(IDebugUIConstants.IMG_LCL_CONTENT_ASSIST)); 276 fContentAssistAction.setDisabledImageDescriptor(DebugUITools.getImageDescriptor(IDebugUIConstants.IMG_DLCL_CONTENT_ASSIST)); 277 getViewSite().getActionBars().updateActionBars(); 278 279 IHandler handler = new AbstractHandler() { 280 public Object execute(ExecutionEvent event) throws ExecutionException { 281 fContentAssistAction.run(); 282 return null; 283 } 284 285 }; 286 287 IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class); 288 fHandlerActivation = handlerService.activateHandler(ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS, handler); 289 } 290 291 298 protected void createUndoRedoActions() { 299 IUndoContext undoContext= getUndoContext(); 300 if (undoContext != null) { 301 303 OperationHistoryActionHandler undoAction= new UndoActionHandler(getSite(), undoContext); 305 PlatformUI.getWorkbench().getHelpSystem().setHelp(undoAction, IAbstractTextEditorHelpContextIds.UNDO_ACTION); 306 undoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.UNDO); 307 setGlobalAction(ITextEditorActionConstants.UNDO, undoAction); 308 309 OperationHistoryActionHandler redoAction= new RedoActionHandler(getSite(), undoContext); 311 PlatformUI.getWorkbench().getHelpSystem().setHelp(redoAction, IAbstractTextEditorHelpContextIds.REDO_ACTION); 312 redoAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.REDO); 313 setGlobalAction(ITextEditorActionConstants.REDO, redoAction); 314 } 315 } 316 317 323 private IUndoContext getUndoContext() { 324 IUndoManager undoManager= fSourceViewer.getUndoManager(); 325 if (undoManager instanceof IUndoManagerExtension) 326 return ((IUndoManagerExtension)undoManager).getUndoContext(); 327 return null; 328 } 329 330 protected void setGlobalAction(String actionID, IAction action) { 331 IActionBars actionBars = getViewSite().getActionBars(); 332 fGlobalActions.put(actionID, action); 333 actionBars.setGlobalActionHandler(actionID, action); 334 } 335 336 339 private void initializeToolBar() { 340 IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager(); 341 tbm.add(new Separator(IJavaDebugUIConstants.EVALUATION_GROUP)); 342 tbm.add(fClearDisplayAction); 343 getViewSite().getActionBars().updateActionBars(); 344 } 345 346 349 protected void fillContextMenu(IMenuManager menu) { 350 351 if (fSourceViewer.getDocument() == null) { 352 return; 353 } 354 menu.add(new Separator(IJavaDebugUIConstants.EVALUATION_GROUP)); 355 if (EvaluationContextManager.getEvaluationContext(this) != null) { 356 menu.add(fContentAssistAction); 357 } 358 menu.add(new Separator()); 359 menu.add((IAction) fGlobalActions.get(ActionFactory.CUT.getId())); 360 menu.add((IAction) fGlobalActions.get(ActionFactory.COPY.getId())); 361 menu.add((IAction) fGlobalActions.get(ActionFactory.PASTE.getId())); 362 menu.add((IAction) fGlobalActions.get(ActionFactory.SELECT_ALL.getId())); 363 menu.add(new Separator()); 364 menu.add((IAction) fGlobalActions.get(ActionFactory.FIND.getId())); 365 menu.add(fClearDisplayAction); 366 menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); 367 } 368 369 372 public Object getAdapter(Class required) { 373 374 if (ITextOperationTarget.class.equals(required)) { 375 return fSourceViewer.getTextOperationTarget(); 376 } 377 378 if (IFindReplaceTarget.class.equals(required)) { 379 return fSourceViewer.getFindReplaceTarget(); 380 } 381 382 if (IDataDisplay.class.equals(required)) { 383 return fDataDisplay; 384 } 385 if (ITextViewer.class.equals(required)) { 386 return fSourceViewer; 387 } 388 389 return super.getAdapter(required); 390 } 391 392 protected void updateActions() { 393 Iterator iterator = fSelectionActions.iterator(); 394 while (iterator.hasNext()) { 395 IAction action = (IAction) fGlobalActions.get(iterator.next()); 396 if (action instanceof IUpdate) { 397 ((IUpdate) action).update(); 398 } 399 } 400 } 401 402 407 public void saveState(IMemento memento) { 408 if (fSourceViewer != null) { 409 String contents= getContents(); 410 if (contents != null) { 411 memento.putTextData(contents); 412 } 413 } else if (fRestoredContents != null) { 414 memento.putTextData(fRestoredContents); 415 } 416 } 417 418 423 public void init(IViewSite site, IMemento memento) throws PartInitException { 424 init(site); 425 if (fgMemento != null) { 426 memento= fgMemento; 427 } 428 if (memento != null) { 429 fRestoredContents= memento.getTextData(); 430 } 431 } 432 433 437 private String getContents() { 438 if (fSourceViewer != null) { 439 IDocument doc= fSourceViewer.getDocument(); 440 if (doc != null) { 441 String contents= doc.get().trim(); 442 if (contents.length() > 0) { 443 return contents; 444 } 445 } 446 } 447 return null; 448 } 449 450 protected final ISelectionChangedListener getSelectionChangedListener() { 451 return new ISelectionChangedListener() { 452 public void selectionChanged(SelectionChangedEvent event) { 453 updateSelectionDependentActions(); 454 } 455 }; 456 } 457 458 protected void updateSelectionDependentActions() { 459 Iterator iterator= fSelectionActions.iterator(); 460 while (iterator.hasNext()) 461 updateAction((String )iterator.next()); 462 } 463 464 465 protected void updateAction(String actionId) { 466 IAction action= (IAction)fGlobalActions.get(actionId); 467 if (action instanceof IUpdate) { 468 ((IUpdate) action).update(); 469 } 470 } 471 474 public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) { 475 } 476 477 480 public void inputDocumentChanged(IDocument oldInput, IDocument newInput) { 481 oldInput.removeDocumentListener(fDocumentListener); 482 } 483 486 public void dispose() { 487 getSite().getWorkbenchWindow().removePerspectiveListener(this); 488 if (fSourceViewer != null) { 489 fSourceViewer.dispose(); 490 fSourceViewer = null; 491 } 492 493 IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class); 494 handlerService.deactivateHandler(fHandlerActivation); 495 496 super.dispose(); 497 } 498 499 502 public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective, IWorkbenchPartReference partRef, String changeId) { 503 if (partRef instanceof IViewReference && changeId.equals(IWorkbenchPage.CHANGE_VIEW_HIDE)) { 504 String id = ((IViewReference) partRef).getId(); 505 if (id.equals(getViewSite().getId())) { 506 String contents= getContents(); 508 if (contents != null) { 509 fgMemento= XMLMemento.createWriteRoot("DisplayViewMemento"); fgMemento.putTextData(contents); 511 } 512 } 513 } 514 } 515 516 519 public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) { 520 } 521 522 525 public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective, String changeId) { 526 } 527 528 } 529 | Popular Tags |