1 11 12 package org.eclipse.debug.internal.ui.views.memory; 13 14 import org.eclipse.core.runtime.IAdaptable; 15 import org.eclipse.core.runtime.IProgressMonitor; 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.NullProgressMonitor; 18 import org.eclipse.core.runtime.Status; 19 import org.eclipse.core.runtime.jobs.Job; 20 import org.eclipse.debug.core.DebugPlugin; 21 import org.eclipse.debug.core.IMemoryBlockListener; 22 import org.eclipse.debug.core.model.IDebugElement; 23 import org.eclipse.debug.core.model.IMemoryBlock; 24 import org.eclipse.debug.core.model.IMemoryBlockExtension; 25 import org.eclipse.debug.core.model.IMemoryBlockRetrieval; 26 import org.eclipse.debug.internal.ui.DebugUIMessages; 27 import org.eclipse.debug.ui.DebugUITools; 28 import org.eclipse.debug.ui.contexts.DebugContextEvent; 29 import org.eclipse.debug.ui.contexts.IDebugContextListener; 30 import org.eclipse.debug.ui.memory.IMemoryRendering; 31 import org.eclipse.jface.action.Action; 32 import org.eclipse.jface.action.ActionContributionItem; 33 import org.eclipse.jface.action.IAction; 34 import org.eclipse.jface.action.IMenuCreator; 35 import org.eclipse.jface.viewers.ILabelDecorator; 36 import org.eclipse.jface.viewers.ISelection; 37 import org.eclipse.jface.viewers.IStructuredSelection; 38 import org.eclipse.jface.viewers.StructuredSelection; 39 import org.eclipse.jface.viewers.StructuredViewer; 40 import org.eclipse.swt.widgets.Control; 41 import org.eclipse.swt.widgets.Event; 42 import org.eclipse.swt.widgets.Menu; 43 import org.eclipse.ui.IActionDelegate2; 44 import org.eclipse.ui.IViewActionDelegate; 45 import org.eclipse.ui.IViewPart; 46 import org.eclipse.ui.progress.UIJob; 47 import org.eclipse.ui.progress.WorkbenchJob; 48 49 52 public class SwitchMemoryBlockAction extends Action implements IViewActionDelegate, IActionDelegate2 { 53 54 59 class UpdateActionEnablementJob extends UIJob { 60 61 64 public UpdateActionEnablementJob() { 65 super("Update Action Enablement"); setSystem(true); 67 } 68 69 72 public IStatus runInUIThread(IProgressMonitor monitor) { 73 if (fAction != null) { 74 IAdaptable context = DebugUITools.getDebugContext(); 75 if (context != null) { 76 IMemoryBlockRetrieval retrieval = null; 77 if (context.getAdapter(IMemoryBlockRetrieval.class) != null) { 78 retrieval = (IMemoryBlockRetrieval)context.getAdapter(IMemoryBlockRetrieval.class); 79 } 80 if (retrieval == null) { 81 if(context instanceof IMemoryBlockRetrieval) { 82 retrieval = (IMemoryBlockRetrieval)context; 83 } 84 else if(context instanceof IDebugElement) { 85 retrieval = ((IDebugElement)context).getDebugTarget(); 86 } 87 } 88 if (retrieval != null) { 89 IMemoryBlock[] memoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(retrieval); 90 fAction.setEnabled(memoryBlocks.length > 0); 91 return Status.OK_STATUS; 92 } 93 else if (getViewer() != null) { 94 Object input = getViewer().getInput(); 95 if (input instanceof IMemoryBlockRetrieval) { 96 retrieval = (IMemoryBlockRetrieval)input; 97 IMemoryBlock[] memoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(retrieval); 98 fAction.setEnabled(memoryBlocks.length > 0); 99 return Status.OK_STATUS; 100 } 101 } 102 } 103 fAction.setEnabled(false); 104 } 105 return Status.CANCEL_STATUS; 106 } 107 } 108 109 private IViewPart fView; 110 private MenuCreator fMenuCreator; 111 private IAction fAction; 112 private UpdateActionEnablementJob fUpdateJob = new UpdateActionEnablementJob(); 113 114 117 private IMemoryBlockListener fListener = new IMemoryBlockListener() { 118 public void memoryBlocksAdded(IMemoryBlock[] memory) { 119 if (fAction != null) { 120 fUpdateJob.schedule(); 121 } 122 } 123 124 public void memoryBlocksRemoved(IMemoryBlock[] memory) { 125 if (fAction != null) { 126 fUpdateJob.schedule(); 127 } 128 } 129 }; 130 131 134 private IDebugContextListener fDebugContextListener = new IDebugContextListener() { 135 public void debugContextChanged(DebugContextEvent event) { 136 if (fAction != null) { 137 fUpdateJob.schedule(); 138 } 139 } 140 }; 141 142 145 class SwitchToAction extends Action { 146 private IMemoryBlock fMemoryblock; 147 148 151 public void run() { 152 if (fView == null) { 153 return; 154 } 155 fView.getSite().getSelectionProvider().setSelection(new StructuredSelection(fMemoryblock)); 157 } 158 159 public SwitchToAction(final IMemoryBlock memBlk, boolean buildLabel) { 160 super(); 161 if (buildLabel) { 162 setText(DebugUIMessages.SwitchMemoryBlockAction_4); 163 Job job = new Job("SwtichToAction"){ protected IStatus run(IProgressMonitor monitor) { 165 getLabels(memBlk); 166 return Status.OK_STATUS; 167 }}; 168 job.setSystem(true); 169 job.schedule(); 170 } 171 fMemoryblock = memBlk; 172 } 173 174 public SwitchToAction(final IMemoryBlock memBlk, String label) { 175 super(label); 176 fMemoryblock = memBlk; 177 } 178 179 private void getLabels(final IMemoryBlock memBlk) { 180 StringBuffer text = new StringBuffer (""); String label = new String (""); if (memBlk instanceof IMemoryBlockExtension) { 183 String expression = ((IMemoryBlockExtension)memBlk).getExpression(); 184 if (expression == null) { 185 expression = DebugUIMessages.SwitchMemoryBlockAction_0; 186 } 187 text.append(expression); 188 } 189 else { 190 long address = memBlk.getStartAddress(); 191 text.append(Long.toHexString(address)); 192 } 193 194 label = text.toString(); 195 label = decorateLabel(memBlk, label); 196 197 final String finalLabel = label; 198 WorkbenchJob job = new WorkbenchJob("SwtichToAction Update Label") { public IStatus runInUIThread(IProgressMonitor monitor) { 200 SwitchToAction.super.setText(finalLabel); 201 return Status.OK_STATUS; 202 }}; 203 job.setSystem(true); 204 job.schedule(); 205 } 206 } 207 208 211 class MenuCreator implements IMenuCreator { 212 Menu dropdown; 213 214 217 public void dispose() { 218 if (dropdown != null) 219 dropdown.dispose(); 220 } 221 222 225 public Menu getMenu(Control parent) { 226 if (dropdown != null) { 227 dropdown.dispose(); 228 dropdown = null; 229 } 230 if (dropdown == null) { 231 dropdown = new Menu(parent); 232 233 IMemoryBlock[] allMemoryBlocks = null; 235 236 IMemoryBlock memoryBlock = getCurrentMemoryBlock(); 238 239 Object context = DebugUITools.getDebugContext(); 240 IMemoryBlockRetrieval retrieval = MemoryViewUtil.getMemoryBlockRetrieval(context); 241 if (retrieval != null) { 242 allMemoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(retrieval); 243 } 244 if (allMemoryBlocks != null) { 245 for (int i=0; i<allMemoryBlocks.length; i++) { 246 SwitchToAction action = new SwitchToAction(allMemoryBlocks[i], true); 247 if (allMemoryBlocks[i] == memoryBlock) { 248 action.setChecked(true); 249 } 250 ActionContributionItem item = new ActionContributionItem(action); 251 item.fill(dropdown, -1); 252 item.getAction().setChecked(true); 253 } 254 } 255 } 256 return dropdown; 257 } 258 259 262 public Menu getMenu(Menu parent) { 263 return null; 264 } 265 266 } 267 268 271 public void init(IViewPart view) { 272 fView = view; 273 DebugUITools.getDebugContextManager().getContextService(fView.getViewSite().getWorkbenchWindow()).addDebugContextListener(fDebugContextListener); 274 DebugPlugin.getDefault().getMemoryBlockManager().addListener(fListener); 275 fUpdateJob.runInUIThread(new NullProgressMonitor()); 276 } 277 278 282 private StructuredViewer getViewer() { 283 if (fView instanceof MemoryView) { 284 MemoryView memView = (MemoryView)fView; 285 IMemoryViewPane pane = memView.getViewPane(MemoryBlocksTreeViewPane.PANE_ID); 286 if (pane instanceof MemoryBlocksTreeViewPane) { 287 StructuredViewer viewer = ((MemoryBlocksTreeViewPane)pane).getViewer(); 288 return viewer; 289 } 290 } 291 return null; 292 } 293 294 297 public void run(IAction action) { 298 switchToNext(); 299 } 300 301 304 public void run() { 305 switchToNext(); 306 } 307 308 private void switchToNext() { 309 IAdaptable context = DebugUITools.getDebugContext(); 310 if (context instanceof IDebugElement) { 311 IDebugElement debugContext = (IDebugElement)context; 312 IMemoryBlockRetrieval retrieval = (IMemoryBlockRetrieval)debugContext.getAdapter(IMemoryBlockRetrieval.class); 313 if (retrieval == null) { 314 retrieval = debugContext.getDebugTarget(); 315 } 316 if (retrieval != null) { 317 IMemoryBlock[] memoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(retrieval); 318 doSwitchToNext(memoryBlocks); 319 } 320 } 321 } 322 323 326 private void doSwitchToNext(IMemoryBlock[] memoryBlocks) { 327 if (memoryBlocks.length > 1) { 329 IMemoryBlock current = getCurrentMemoryBlock(); 330 int next = 0; 331 if (current != null) { 332 for (int i=0; i<memoryBlocks.length; i++) { 333 if (memoryBlocks[i] == current) { 334 next = i+1; 335 } 336 } 337 } 338 if (next > memoryBlocks.length-1) { 339 next = 0; 340 } 341 SwitchToAction switchAction = new SwitchToAction(memoryBlocks[next], false); 342 switchAction.run(); 343 } 344 } 345 346 349 public void selectionChanged(IAction action, ISelection selection) {} 350 351 354 public void init(IAction action) { 355 fAction = action; 356 fUpdateJob.runInUIThread(new NullProgressMonitor()); 357 fMenuCreator = new MenuCreator(); 358 action.setMenuCreator(fMenuCreator); 359 } 360 361 364 public void dispose() { 365 fAction = null; 366 DebugPlugin.getDefault().getMemoryBlockManager().removeListener(fListener); 367 DebugUITools.getDebugContextManager().getContextService(fView.getViewSite().getWorkbenchWindow()).removeDebugContextListener(fDebugContextListener); 368 if (fMenuCreator != null) { 369 fMenuCreator.dispose(); 370 } 371 } 372 373 376 public void runWithEvent(IAction action, Event event) { 377 switchToNext(); 378 } 379 380 384 private IMemoryBlock getCurrentMemoryBlock() { 385 if (fView == null) { 386 return null; 387 } 388 ISelection memBlkSelection = fView.getSite().getSelectionProvider().getSelection(); 389 IMemoryBlock memoryBlock = null; 390 391 if (memBlkSelection != null) { 392 if (!memBlkSelection.isEmpty() && memBlkSelection instanceof IStructuredSelection) { 393 Object obj = ((IStructuredSelection)memBlkSelection).getFirstElement(); 394 if (obj instanceof IMemoryBlock) { 395 memoryBlock = (IMemoryBlock)obj; 396 } 397 else if (obj instanceof IMemoryRendering) { 398 memoryBlock = ((IMemoryRendering)obj).getMemoryBlock(); 399 } 400 } 401 } 402 return memoryBlock; 403 } 404 405 411 private String decorateLabel(final IMemoryBlock memBlk, String label) { 412 ILabelDecorator decorator = (ILabelDecorator)memBlk.getAdapter(ILabelDecorator.class); 413 if (decorator != null) { 414 label = decorator.decorateText(label, memBlk); 415 } 416 return label; 417 } 418 419 } 420 | Popular Tags |