1 11 12 package org.eclipse.debug.internal.ui.views.memory; 13 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.runtime.IAdaptable; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Status; 20 import org.eclipse.debug.core.DebugEvent; 21 import org.eclipse.debug.core.DebugPlugin; 22 import org.eclipse.debug.core.IDebugEventSetListener; 23 import org.eclipse.debug.core.IMemoryBlockListener; 24 import org.eclipse.debug.core.model.IDebugElement; 25 import org.eclipse.debug.core.model.IDebugTarget; 26 import org.eclipse.debug.core.model.IMemoryBlock; 27 import org.eclipse.debug.core.model.IMemoryBlockRetrieval; 28 import org.eclipse.debug.internal.ui.DebugPluginImages; 29 import org.eclipse.debug.internal.ui.DebugUIMessages; 30 import org.eclipse.debug.internal.ui.DebugUIPlugin; 31 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants; 32 import org.eclipse.debug.internal.ui.memory.provisional.MemoryViewPresentationContext; 33 import org.eclipse.debug.ui.DebugUITools; 34 import org.eclipse.debug.ui.IDebugUIConstants; 35 import org.eclipse.debug.ui.contexts.DebugContextEvent; 36 import org.eclipse.debug.ui.contexts.IDebugContextListener; 37 import org.eclipse.debug.ui.memory.IMemoryRendering; 38 import org.eclipse.debug.ui.memory.IMemoryRenderingContainer; 39 import org.eclipse.debug.ui.memory.IMemoryRenderingSite; 40 import org.eclipse.jface.action.Action; 41 import org.eclipse.jface.action.IAction; 42 import org.eclipse.jface.action.IMenuListener; 43 import org.eclipse.jface.action.IMenuManager; 44 import org.eclipse.jface.action.MenuManager; 45 import org.eclipse.jface.action.Separator; 46 import org.eclipse.jface.dialogs.MessageDialog; 47 import org.eclipse.jface.viewers.ISelection; 48 import org.eclipse.jface.viewers.ISelectionChangedListener; 49 import org.eclipse.jface.viewers.ISelectionProvider; 50 import org.eclipse.jface.viewers.IStructuredSelection; 51 import org.eclipse.jface.viewers.SelectionChangedEvent; 52 import org.eclipse.jface.viewers.StructuredViewer; 53 import org.eclipse.swt.SWT; 54 import org.eclipse.swt.layout.GridData; 55 import org.eclipse.swt.widgets.Composite; 56 import org.eclipse.swt.widgets.Control; 57 import org.eclipse.swt.widgets.Menu; 58 import org.eclipse.ui.ISelectionListener; 59 import org.eclipse.ui.IViewPart; 60 import org.eclipse.ui.IWorkbenchActionConstants; 61 import org.eclipse.ui.IWorkbenchPart; 62 import org.eclipse.ui.IWorkbenchWindow; 63 import org.eclipse.ui.PlatformUI; 64 import org.eclipse.ui.progress.UIJob; 65 66 67 70 public class MemoryBlocksTreeViewPane implements ISelectionListener, ISelectionChangedListener, IMemoryViewPane, IMemoryRenderingContainer{ 71 72 public static final String PANE_ID = DebugUIPlugin.getUniqueIdentifier() + ".MemoryView.MemoryBlocksTreeViewPane"; 74 private IViewPart fParent; 75 private MemoryViewTreeViewer fTreeViewer; 76 protected IMemoryBlockRetrieval fRetrieval; 77 private ViewPaneSelectionProvider fSelectionProvider; 78 private AddMemoryBlockAction fAddMemoryBlockAction; 79 private IAction fRemoveMemoryBlockAction; 80 private IAction fRemoveAllMemoryBlocksAction; 81 private String fPaneId; 82 private boolean fVisible = true; 83 private TreeViewPaneContextListener fDebugContextListener; 84 private ViewPaneEventHandler fEvtHandler; 85 private String fLabel; 86 87 class TreeViewerRemoveMemoryBlocksAction extends Action 88 { 89 TreeViewerRemoveMemoryBlocksAction() 90 { 91 super(); 92 setText(DebugUIMessages.RemoveMemoryBlockAction_title); 93 94 setToolTipText(DebugUIMessages.RemoveMemoryBlockAction_tooltip); 95 setImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_ELCL_REMOVE_MEMORY)); 96 setHoverImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_LCL_REMOVE_MEMORY)); 97 setDisabledImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_DLCL_REMOVE_MEMORY)); 98 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugUIConstants.PLUGIN_ID + ".RemoveMemoryBlockAction_context"); setEnabled(true); 100 } 101 102 105 public void run() { 106 ISelection selected = fTreeViewer.getSelection(); 107 108 if (selected != null && selected instanceof IStructuredSelection) 109 { 110 Object [] selectedMemBlks = ((IStructuredSelection)selected).toArray(); 111 ArrayList memoryBlocks = new ArrayList (); 112 for (int i=0; i<selectedMemBlks.length; i++) 113 { 114 if (selectedMemBlks[i] instanceof IMemoryBlock) 115 memoryBlocks.add(selectedMemBlks[i]); 116 } 117 118 DebugPlugin.getDefault().getMemoryBlockManager().removeMemoryBlocks((IMemoryBlock[])memoryBlocks.toArray(new IMemoryBlock[memoryBlocks.size()])); 119 } 120 } 121 } 122 123 class TreeViewerRemoveAllMemoryBlocksAction extends Action 124 { 125 TreeViewerRemoveAllMemoryBlocksAction() 126 { 127 super(); 128 setText(DebugUIMessages.MemoryBlocksTreeViewPane_2); 129 130 setToolTipText(DebugUIMessages.MemoryBlocksTreeViewPane_2); 131 setImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_ELCL_REMOVE_ALL)); 132 setHoverImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_ELCL_REMOVE_ALL)); 133 setDisabledImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_DLCL_REMOVE_ALL)); 134 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugUIConstants.PLUGIN_ID + ".RemoveAllMemoryBlocksAction_context"); } 136 137 140 public void run() { 141 142 IWorkbenchWindow window= DebugUIPlugin.getActiveWorkbenchWindow(); 143 if (window == null) { 144 return; 145 } 146 boolean proceed = MessageDialog.openQuestion(window.getShell(), DebugUIMessages.MemoryBlocksTreeViewPane_0, DebugUIMessages.MemoryBlocksTreeViewPane_1); if (proceed) { 148 IMemoryBlock[] memoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(fRetrieval); 149 DebugPlugin.getDefault().getMemoryBlockManager().removeMemoryBlocks(memoryBlocks); 150 } 151 } 152 } 153 154 class ViewPaneEventHandler implements IMemoryBlockListener, IDebugEventSetListener 155 { 156 private boolean fDisposed = false; 157 158 public ViewPaneEventHandler() 159 { 160 DebugPlugin.getDefault().getMemoryBlockManager().addListener(this); 161 DebugPlugin.getDefault().addDebugEventListener(this); 162 } 163 164 167 public void dispose() { 168 fDisposed = true; 169 DebugPlugin.getDefault().getMemoryBlockManager().removeListener(this); 170 DebugPlugin.getDefault().removeDebugEventListener(this); 171 } 172 173 176 public void memoryBlocksAdded(final IMemoryBlock[] memory) { 177 if (fDisposed) 179 return; 180 updateActionsEnablement(); 181 } 182 183 186 public void memoryBlocksRemoved(final IMemoryBlock[] memory) { 187 if (fDisposed) 188 return; 189 updateActionsEnablement(); 190 } 191 192 195 protected void doHandleDebugEvent(DebugEvent event) { 196 197 if (fDisposed) 199 return; 200 201 if (event.getKind() == DebugEvent.TERMINATE) 202 { 203 if (event.getSource() instanceof IDebugTarget) 205 { 206 IMemoryBlockRetrieval srcRetrieval = getMemoryBlockRetrieval(event.getSource()); 207 if (srcRetrieval == fRetrieval) 208 { 209 UIJob job = new UIJob("setInput"){ public IStatus runInUIThread(IProgressMonitor monitor) { 212 213 if (fTreeViewer.getContentProvider() == null) 215 return Status.OK_STATUS; 216 217 fTreeViewer.setInput(null); 218 return Status.OK_STATUS; 219 }}; 220 221 job.setSystem(true); 222 job.schedule(); 223 } 224 } 225 } 226 } 227 228 public void handleDebugEvents(DebugEvent[] events) { 229 for (int i=0; i<events.length; i++) 230 { 231 doHandleDebugEvent(events[i]); 232 } 233 } 234 } 235 236 class TreeViewPaneContextListener implements IDebugContextListener 237 { 238 public void contextActivated(ISelection selection) { 239 240 if (selection.isEmpty() && fRetrieval != null) 241 { 242 fRetrieval = null; 243 if (fTreeViewer != null && fTreeViewer.getContentProvider() != null) 244 fTreeViewer.setInput(fRetrieval); 245 updateActionsEnablement(); 246 return; 247 } 248 249 if (selection instanceof IStructuredSelection) 250 { 251 Object obj = ((IStructuredSelection)selection).getFirstElement(); 252 if (obj instanceof IAdaptable) 253 { 254 IAdaptable context = (IAdaptable)obj; 255 IMemoryBlockRetrieval retrieval = getMemoryBlockRetrieval(context); 256 if (retrieval != null && retrieval != fRetrieval && 257 fTreeViewer != null && fTreeViewer.getContentProvider() != null) 258 { 259 fRetrieval = retrieval; 261 fTreeViewer.setInput(fRetrieval); 262 } 263 updateActionsEnablement(); 264 } 265 } 266 } 267 268 271 public void debugContextChanged(DebugContextEvent event) { 272 if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0) { 273 contextActivated(event.getContext()); 274 } 275 } 276 } 277 278 public MemoryBlocksTreeViewPane(IViewPart parent) 279 { 280 fParent = parent; 281 fSelectionProvider = new ViewPaneSelectionProvider(); 282 } 283 284 public Control createViewPane(Composite parent, String paneId, String label) 285 { 286 fPaneId = paneId; 287 int style = SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL | SWT.VIRTUAL; 288 289 fLabel = label; 290 291 IMemoryRenderingSite site = getMemoryRenderingSite(); 292 MemoryViewPresentationContext presentationContext = new MemoryViewPresentationContext(site, this, null); 293 fTreeViewer = new MemoryViewTreeViewer(parent, style, presentationContext); 294 295 IAdaptable context = DebugUITools.getDebugContext(); 296 IMemoryBlockRetrieval retrieval = getMemoryBlockRetrieval(context); 297 fTreeViewer.setInput(retrieval); 298 fRetrieval = retrieval; 299 300 fParent.getViewSite().getSelectionProvider().addSelectionChangedListener(this); 301 fParent.getViewSite().getPage().addSelectionListener(this); 302 303 fDebugContextListener = new TreeViewPaneContextListener(); 304 DebugUITools.getDebugContextManager().getContextService(fParent.getSite().getWorkbenchWindow()).addDebugContextListener(fDebugContextListener); 305 306 fTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() { 307 308 public void selectionChanged(SelectionChangedEvent event) { 309 ISelection treeSelected = event.getSelection(); 310 fSelectionProvider.setSelection(treeSelected); 311 }}); 312 313 populateViewPane(); 314 fEvtHandler = new ViewPaneEventHandler(); 315 316 MenuManager mgr = createContextMenuManager(); 318 Menu menu = mgr.createContextMenu(fTreeViewer.getControl()); 319 fTreeViewer.getControl().setMenu(menu); 320 321 GridData data = new GridData(); 322 data.grabExcessHorizontalSpace = true; 323 data.grabExcessVerticalSpace = true; 324 data.horizontalAlignment = SWT.FILL; 325 data.verticalAlignment = SWT.FILL; 326 fTreeViewer.getControl().setLayoutData(data); 327 328 updateActionsEnablement(); 329 330 return fTreeViewer.getControl(); 331 } 332 333 334 337 private void populateViewPane() { 338 339 Object context = DebugUITools.getDebugContext(); 340 fRetrieval = MemoryViewUtil.getMemoryBlockRetrieval(context); 341 342 ISelection selection = null; 343 if (fParent.getSite().getSelectionProvider() != null) 344 selection = fParent.getSite().getSelectionProvider().getSelection(); 345 346 IMemoryBlock memoryBlock = null; 347 348 if (selection == null) 349 { 350 return; 351 } 352 353 memoryBlock = getMemoryBlock(selection); 355 356 if (memoryBlock == null) 357 { 358 IMemoryBlock [] memoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(fRetrieval); 359 if (memoryBlocks.length > 0) 360 memoryBlock = memoryBlocks[0]; 361 } 362 } 363 364 private IMemoryBlock getMemoryBlock(ISelection selection) 365 { 366 if (!(selection instanceof IStructuredSelection)) 367 return null; 368 369 if (selection.isEmpty() || ((IStructuredSelection)selection).size() > 1) 371 { 372 return null; 373 } 374 375 Object elem = ((IStructuredSelection)selection).getFirstElement(); 376 377 if (elem instanceof IMemoryBlock) 378 return (IMemoryBlock)elem; 379 else if (elem instanceof IMemoryRendering) 380 return ((IMemoryRendering)elem).getMemoryBlock(); 381 else 382 return null; 383 } 384 385 protected MenuManager createContextMenuManager() { 386 MenuManager menuMgr = new MenuManager("#PopupMenu"); menuMgr.setRemoveAllWhenShown(true); 388 menuMgr.addMenuListener(new IMenuListener() { 389 public void menuAboutToShow(IMenuManager manager) { 390 manager.add(fAddMemoryBlockAction); 391 manager.add(fRemoveMemoryBlockAction); 392 manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); 393 } 394 }); 395 396 fParent.getSite().registerContextMenu(getId(), menuMgr, fSelectionProvider); 398 return menuMgr; 399 } 400 401 public void dispose() 402 { 403 fParent.getViewSite().getSelectionProvider().removeSelectionChangedListener(this); 404 fParent.getViewSite().getPage().removeSelectionListener(this); 405 fAddMemoryBlockAction.dispose(); 406 DebugUITools.getDebugContextManager().getContextService(fParent.getSite().getWorkbenchWindow()).removeDebugContextListener(fDebugContextListener); 407 fEvtHandler.dispose(); 408 } 409 410 413 public void selectionChanged(IWorkbenchPart part, ISelection selection) { 414 415 416 417 if (selection instanceof IStructuredSelection) 418 { 419 Object obj = ((IStructuredSelection)selection).getFirstElement(); 420 421 if (obj instanceof IMemoryBlock) 422 { 423 if (part == fParent) 425 { 426 ISelection treeSel = fTreeViewer.getSelection(); 428 if (treeSel instanceof IStructuredSelection) 429 { 430 if (((IStructuredSelection)treeSel).getFirstElement() == obj) 431 return; 432 } 433 removeSelctionListener(this); 435 fTreeViewer.setSelection(selection); 436 addSelectionListener(this); 438 } 439 } 440 } 441 442 } 443 444 private IMemoryBlockRetrieval getMemoryBlockRetrieval(Object obj) { 445 IAdaptable adaptable = (IAdaptable)obj; 446 IMemoryBlockRetrieval retrieval = null; 447 if (adaptable != null) 448 { 449 retrieval = (IMemoryBlockRetrieval)adaptable.getAdapter(IMemoryBlockRetrieval.class); 450 if(retrieval == null && obj instanceof IDebugElement) 451 { 452 IDebugTarget debugTarget = ((IDebugElement)obj).getDebugTarget(); 453 if (debugTarget != null) 454 retrieval = debugTarget; 455 } 456 } 457 return retrieval; 458 } 459 460 public String getId() 461 { 462 return fPaneId; 463 } 464 465 468 public IAction[] getActions() { 469 470 if (fAddMemoryBlockAction == null) 471 fAddMemoryBlockAction = new RetargetAddMemoryBlockAction((IMemoryRenderingSite)fParent); 472 473 if (fRemoveMemoryBlockAction == null) 474 { 475 fRemoveMemoryBlockAction = new TreeViewerRemoveMemoryBlocksAction(); 476 } 477 478 if (fRemoveAllMemoryBlocksAction == null) 479 { 480 fRemoveAllMemoryBlocksAction = new TreeViewerRemoveAllMemoryBlocksAction(); 481 } 482 483 updateActionsEnablement(); 484 485 return new IAction[]{fAddMemoryBlockAction, fRemoveMemoryBlockAction, fRemoveAllMemoryBlocksAction}; 486 } 487 488 491 public void addSelectionListener(ISelectionChangedListener listener) 492 { 493 if (fSelectionProvider == null) 494 fSelectionProvider = new ViewPaneSelectionProvider(); 495 496 fSelectionProvider.addSelectionChangedListener(listener); 497 } 498 499 502 public void removeSelctionListener(ISelectionChangedListener listener) 503 { 504 if (fSelectionProvider == null) 505 return; 506 507 fSelectionProvider.removeSelectionChangedListener(listener); 508 } 509 510 513 public ISelectionProvider getSelectionProvider() { 514 return fSelectionProvider; 515 } 516 517 520 public void restoreViewPane() { 521 populateViewPane(); 522 updateActionsEnablement(); 523 } 524 525 528 public Control getControl() { 529 return fTreeViewer.getControl(); 530 } 531 532 535 public void setVisible(boolean visible) { 536 if (fVisible != visible) 537 { 538 fVisible = visible; 539 540 if(fVisible) 541 { 542 fTreeViewer.refresh(); 543 fTreeViewer.getControl().setFocus(); 544 } 545 } 546 } 547 548 551 public boolean isVisible() { 552 return fVisible; 553 } 554 555 private void updateActionsEnablement() 556 { 557 if (fRemoveMemoryBlockAction == null) 558 return; 559 560 if (fRemoveAllMemoryBlocksAction == null) 561 return; 562 563 if (fRetrieval != null) 564 { 565 IMemoryBlock[] memBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(fRetrieval); 566 if(memBlocks.length > 0) 567 { 568 fRemoveMemoryBlockAction.setEnabled(true); 569 fRemoveAllMemoryBlocksAction.setEnabled(true); 570 } 571 else 572 { 573 fRemoveMemoryBlockAction.setEnabled(false); 574 fRemoveAllMemoryBlocksAction.setEnabled(false); 575 } 576 } 577 else 578 { 579 fRemoveMemoryBlockAction.setEnabled(false); 580 fRemoveAllMemoryBlocksAction.setEnabled(false); 581 } 582 } 583 584 public void selectionChanged(SelectionChangedEvent event) { 585 if (event.getSource() == fParent.getSite().getSelectionProvider()) 587 { 588 MemoryBlocksTreeViewPane.this.selectionChanged(fParent, event.getSelection()); 589 } 590 } 591 592 public StructuredViewer getViewer() 593 { 594 return fTreeViewer; 595 } 596 597 public IMemoryRenderingSite getMemoryRenderingSite() { 598 if (fParent instanceof IMemoryRenderingSite) 599 return (IMemoryRenderingSite)fParent; 600 return null; 601 } 602 603 public void addMemoryRendering(IMemoryRendering rendering) { 604 } 606 607 public void removeMemoryRendering(IMemoryRendering rendering) { 608 } 610 611 public IMemoryRendering[] getRenderings() { 612 return new IMemoryRendering[0]; 613 } 614 615 public IMemoryRendering getActiveRendering() { 616 return null; 617 } 618 619 public String getLabel() { 620 return fLabel; 621 } 622 } 623 | Popular Tags |