1 11 package org.eclipse.debug.internal.ui.views.memory; 12 13 import org.eclipse.core.runtime.IAdaptable; 14 import org.eclipse.core.runtime.IProgressMonitor; 15 import org.eclipse.core.runtime.IStatus; 16 import org.eclipse.core.runtime.Status; 17 import org.eclipse.core.runtime.jobs.Job; 18 import org.eclipse.debug.core.DebugException; 19 import org.eclipse.debug.core.DebugPlugin; 20 import org.eclipse.debug.core.IMemoryBlockListener; 21 import org.eclipse.debug.core.model.IDebugElement; 22 import org.eclipse.debug.core.model.IMemoryBlock; 23 import org.eclipse.debug.core.model.IMemoryBlockExtension; 24 import org.eclipse.debug.internal.ui.DebugUIMessages; 25 import org.eclipse.debug.internal.ui.DebugUIPlugin; 26 import org.eclipse.debug.ui.DebugUITools; 27 import org.eclipse.debug.ui.IDebugUIConstants; 28 import org.eclipse.debug.ui.memory.IMemoryRendering; 29 import org.eclipse.debug.ui.memory.IMemoryRenderingBindingsListener; 30 import org.eclipse.debug.ui.memory.IMemoryRenderingSite; 31 import org.eclipse.debug.ui.memory.IMemoryRenderingType; 32 import org.eclipse.jface.dialogs.IDialogConstants; 33 import org.eclipse.jface.viewers.DoubleClickEvent; 34 import org.eclipse.jface.viewers.IDoubleClickListener; 35 import org.eclipse.jface.viewers.ILabelDecorator; 36 import org.eclipse.jface.viewers.ILabelProvider; 37 import org.eclipse.jface.viewers.ILabelProviderListener; 38 import org.eclipse.jface.viewers.ISelection; 39 import org.eclipse.jface.viewers.ISelectionChangedListener; 40 import org.eclipse.jface.viewers.ISelectionProvider; 41 import org.eclipse.jface.viewers.IStructuredContentProvider; 42 import org.eclipse.jface.viewers.IStructuredSelection; 43 import org.eclipse.jface.viewers.ListViewer; 44 import org.eclipse.jface.viewers.SelectionChangedEvent; 45 import org.eclipse.jface.viewers.Viewer; 46 import org.eclipse.swt.SWT; 47 import org.eclipse.swt.events.SelectionAdapter; 48 import org.eclipse.swt.events.SelectionEvent; 49 import org.eclipse.swt.events.SelectionListener; 50 import org.eclipse.swt.graphics.Image; 51 import org.eclipse.swt.layout.GridData; 52 import org.eclipse.swt.layout.GridLayout; 53 import org.eclipse.swt.widgets.Button; 54 import org.eclipse.swt.widgets.Combo; 55 import org.eclipse.swt.widgets.Composite; 56 import org.eclipse.swt.widgets.Control; 57 import org.eclipse.swt.widgets.Label; 58 import org.eclipse.swt.widgets.Shell; 59 import org.eclipse.ui.PlatformUI; 60 import org.eclipse.ui.dialogs.SelectionDialog; 61 import org.eclipse.ui.progress.UIJob; 62 import org.eclipse.ui.progress.WorkbenchJob; 63 64 67 public class AddMemoryRenderingDialog extends SelectionDialog { 68 69 private IMemoryBlock[] fMemoryBlocks; 70 private Combo memoryBlock; 71 private ListViewer fViewer; 72 private IMemoryBlock fSelectedMemoryBlock; 73 private Button addNew; 74 75 private ISelectionChangedListener fSelectionChangedListener; 76 private SelectionListener fSelectionListener; 77 private SelectionAdapter fAddNewSelectionAdapter; 78 private IMemoryRenderingSite fSite; 79 80 private IMemoryBlockListener fMemoryBlockListener = new IMemoryBlockListener(){ 81 82 public void memoryBlocksAdded(final IMemoryBlock[] memory) 83 { 84 if (memory.length > 0) 85 { 86 IMemoryBlock currentBlock = getMemoryBlockToSelect(memory[0]); 87 if (currentBlock == null) 88 { 89 addNew(); 90 } 91 else 92 { 93 populateDialog(currentBlock); 94 } 95 } 96 } 97 public void memoryBlocksRemoved(IMemoryBlock[] memory) 98 { 99 } 100 }; 101 102 private IMemoryRenderingBindingsListener fBindingListener = new IMemoryRenderingBindingsListener() 103 { 104 public void memoryRenderingBindingsChanged() { 105 UIJob job = new UIJob("refresh"){ 107 public IStatus runInUIThread(IProgressMonitor monitor) { 108 fViewer.refresh(); 109 return Status.OK_STATUS; 110 } 111 }; 112 job.setSystem(true); 113 job.schedule(); 114 } 115 116 }; 117 118 class MemoryRenderingLabelProvider implements ILabelProvider 119 { 120 121 124 public Image getImage(Object element) { 125 return null; 126 } 127 128 131 public String getText(Object element) { 132 if (element instanceof IMemoryRenderingType) 133 { 134 String label = ((IMemoryRenderingType)element).getLabel(); 135 return label; 136 } 137 return element.toString(); 138 } 139 140 143 public void addListener(ILabelProviderListener listener) { 144 } 145 146 149 public void dispose() { 150 } 151 152 155 public boolean isLabelProperty(Object element, String property) { 156 return false; 157 } 158 159 162 public void removeListener(ILabelProviderListener listener) { 163 } 164 165 } 166 167 class MemoryRenderingContentProvider implements IStructuredContentProvider 168 { 169 170 173 public Object [] getElements(Object inputElement) { 174 IMemoryRenderingType[] renderings = DebugUITools.getMemoryRenderingManager().getRenderingTypes((IMemoryBlock)inputElement); 175 return renderings; 176 } 177 178 181 public void dispose() { 182 } 183 184 187 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 188 } 189 190 } 191 192 193 196 public boolean close() { 197 198 fViewer.removeSelectionChangedListener(fSelectionChangedListener); 199 memoryBlock.removeSelectionListener(fSelectionListener); 200 addNew.removeSelectionListener(fAddNewSelectionAdapter); 201 DebugPlugin.getDefault().getMemoryBlockManager().removeListener(fMemoryBlockListener); 202 DebugUITools.getMemoryRenderingManager().removeListener(fBindingListener); 203 204 return super.close(); 205 } 206 207 210 protected void createButtonsForButtonBar(Composite parent) { 211 super.createButtonsForButtonBar(parent); 212 getButton(IDialogConstants.OK_ID).setEnabled(false); 213 } 214 215 218 public Object [] getResult() { 219 220 Object [] results = super.getResult(); 221 222 if (results != null) 223 { 224 Object [] renderings = ((IStructuredSelection)results[0]).toArray(); 225 return renderings; 226 } 227 return new Object [0]; 228 } 229 230 233 protected void cancelPressed() { 234 235 setResult(null); 236 237 super.cancelPressed(); 238 } 239 240 243 protected void okPressed() { 244 245 ISelection select = fViewer.getSelection(); 246 setSelectionResult(new Object []{select}); 247 248 super.okPressed(); 249 } 250 251 254 protected Control createDialogArea(Composite parent) { 255 256 PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, DebugUIPlugin.getUniqueIdentifier() + ".AddMemoryRenderingDialog_context"); Composite composite = new Composite(parent, SWT.NONE); 258 GridLayout compositeLayout = new GridLayout(); 259 compositeLayout.numColumns = 3; 260 compositeLayout.makeColumnsEqualWidth = true; 261 composite.setLayout(compositeLayout); 262 263 GridData comositeSpec= new GridData(); 264 comositeSpec.grabExcessVerticalSpace= true; 265 comositeSpec.grabExcessHorizontalSpace= true; 266 comositeSpec.horizontalAlignment= GridData.FILL; 267 comositeSpec.verticalAlignment= GridData.CENTER; 268 composite.setLayoutData(comositeSpec); 269 270 Label textLabel = new Label(composite, SWT.NONE); 271 textLabel.setText(DebugUIMessages.AddMemoryRenderingDialog_Memory_Monitor); 272 GridData textLayout = new GridData(); 273 textLayout.verticalAlignment=GridData.CENTER; 274 textLayout.horizontalAlignment=GridData.BEGINNING; 275 textLabel.setLayoutData(textLayout); 276 277 memoryBlock = new Combo(composite, SWT.BORDER | SWT.READ_ONLY); 278 GridData spec= new GridData(GridData.FILL_HORIZONTAL); 279 spec.grabExcessVerticalSpace= false; 280 spec.grabExcessHorizontalSpace= false; 281 spec.horizontalAlignment= GridData.FILL; 282 spec.verticalAlignment= GridData.FILL; 283 spec.horizontalSpan = 4; 284 memoryBlock.setLayoutData(spec); 285 286 Label filler = new Label(composite, SWT.NONE); 287 filler.setText(" "); GridData fillerData = new GridData(GridData.FILL_HORIZONTAL); 289 fillerData.horizontalSpan = 2; 290 filler.setLayoutData(fillerData); 291 292 addNew = new Button(composite, SWT.NONE); 293 addNew.setText(DebugUIMessages.AddMemoryRenderingDialog_Add_New); 294 GridData specButton= new GridData(); 295 specButton.horizontalAlignment= GridData.END; 296 specButton.verticalAlignment= GridData.CENTER; 297 addNew.setLayoutData(specButton); 298 299 fAddNewSelectionAdapter = new SelectionAdapter() { 300 301 public void widgetSelected(SelectionEvent e) { 302 RetargetAddMemoryBlockAction action = new RetargetAddMemoryBlockAction(fSite, false); 303 action.run(); 304 action.dispose(); 305 }}; 306 307 addNew.addSelectionListener(fAddNewSelectionAdapter); 308 309 fSelectionListener = new SelectionListener(){ 310 311 public void widgetSelected(SelectionEvent e) { 312 313 int idx = memoryBlock.getSelectionIndex(); 314 315 if (fMemoryBlocks == null) 317 return; 318 319 fSelectedMemoryBlock = fMemoryBlocks[idx]; 320 321 fViewer.setInput(fSelectedMemoryBlock); 322 323 } 324 325 public void widgetDefaultSelected(SelectionEvent e) { 326 }}; 327 328 memoryBlock.addSelectionListener(fSelectionListener); 329 330 Label renderingLabel = new Label(composite, SWT.NONE); 331 renderingLabel.setText(DebugUIMessages.AddMemoryRenderingDialog_Memory_renderings); 332 GridData renderingLayout = new GridData(); 333 renderingLayout.horizontalAlignment = GridData.BEGINNING; 334 renderingLayout.verticalAlignment = GridData.CENTER; 335 renderingLayout.horizontalSpan = 3; 336 renderingLabel.setLayoutData(renderingLayout); 337 338 fViewer = new ListViewer(composite); 339 fViewer.setContentProvider(new MemoryRenderingContentProvider()); 340 fViewer.setLabelProvider(new MemoryRenderingLabelProvider()); 341 342 GridData listLayout = new GridData(GridData.FILL_BOTH); 343 listLayout.horizontalSpan = 3; 344 listLayout.heightHint =140; 345 fViewer.getControl().setLayoutData(listLayout); 346 347 fViewer.addDoubleClickListener(new IDoubleClickListener (){ 348 349 public void doubleClick(DoubleClickEvent event) { 350 okPressed(); 351 }}); 352 353 IMemoryBlock currentBlock = getMemoryBlockToSelect(null); 354 if (currentBlock == null) 355 { 356 addNew(); 357 } 358 else 359 { 360 populateDialog(currentBlock); 361 } 362 363 364 fSelectionChangedListener = new ISelectionChangedListener() { 365 366 public void selectionChanged(SelectionChangedEvent event) { 367 ISelection selection = fViewer.getSelection(); 368 369 if (selection.isEmpty()) 370 { 371 getButton(IDialogConstants.OK_ID).setEnabled(false); 372 } 373 else 374 { 375 getButton(IDialogConstants.OK_ID).setEnabled(true); 376 } 377 }}; 378 379 fViewer.addSelectionChangedListener(fSelectionChangedListener); 380 381 DebugPlugin.getDefault().getMemoryBlockManager().addListener(fMemoryBlockListener); 382 DebugUITools.getMemoryRenderingManager().addListener(fBindingListener); 383 384 return composite; 385 } 386 387 public AddMemoryRenderingDialog(Shell parent, IMemoryRenderingSite site) { 388 super(parent); 389 super.setTitle(DebugUIMessages.AddMemoryRenderingDialog_Add_memory_rendering); 390 setShellStyle(getShellStyle() | SWT.RESIZE); 391 fSite = site; 392 } 393 394 395 private void doPopulateDialog(Combo combo, ListViewer viewer, String [] labels, int selectionIdx, IMemoryBlock currentBlock) 396 { 397 combo.removeAll(); 399 400 for (int i=0; i<labels.length; i++) 401 { 402 combo.add(labels[i]); 403 } 404 405 combo.select(selectionIdx); 406 fSelectedMemoryBlock = currentBlock; 407 408 viewer.setInput(currentBlock); 409 } 410 411 private IMemoryBlock getMemoryBlockToSelect(IMemoryBlock lastAdded) 412 { 413 IMemoryBlock currentBlock = null; 414 415 if (lastAdded != null) 416 currentBlock = lastAdded; 417 else 418 { 419 ISelectionProvider selectionProvider = fSite.getSite().getSelectionProvider(); 421 ISelection selection = null; 422 423 if (selectionProvider != null) 424 selection = selectionProvider.getSelection(); 425 else selection = DebugUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(IDebugUIConstants.ID_MEMORY_VIEW); 427 428 IDebugElement element = getMemoryBlock(selection); 429 430 if (!(element instanceof IMemoryBlock)) 431 { 432 433 IAdaptable context = DebugUITools.getDebugContext(); 434 435 if (context != null) 436 { 437 IDebugElement debugElement = (IDebugElement)context.getAdapter(IDebugElement.class); 438 439 if (debugElement != null) 440 { 441 IMemoryBlock[] blocks = MemoryViewUtil.getMemoryBlockManager().getMemoryBlocks(debugElement.getDebugTarget()); 442 443 if (blocks.length > 0) 444 currentBlock = blocks[0]; 445 } 446 } 447 } 448 else 449 { 450 currentBlock = (IMemoryBlock)element; 451 } 452 } 453 return currentBlock; 454 } 455 456 private String [] getLabels(IMemoryBlock[] memoryBlocks) 457 { 458 String [] labels = new String [memoryBlocks.length]; 459 for (int i=0; i<memoryBlocks.length; i++) 460 { 461 String text = ""; if (memoryBlocks[i] instanceof IMemoryBlockExtension) 463 { 464 try { 465 text = ((IMemoryBlockExtension)memoryBlocks[i]).getExpression(); 466 467 if (text == null) 468 text = DebugUIMessages.AddMemoryRenderingDialog_Unknown; 469 470 if (((IMemoryBlockExtension)memoryBlocks[i]).getBigBaseAddress() != null) 471 { 472 text += " : 0x"; text += ((IMemoryBlockExtension)memoryBlocks[i]).getBigBaseAddress().toString(16); 474 } 475 } catch (DebugException e) { 476 long address = memoryBlocks[i].getStartAddress(); 477 text = Long.toHexString(address); 478 } 479 } 480 else 481 { 482 long address = memoryBlocks[i].getStartAddress(); 483 text = Long.toHexString(address); 484 } 485 486 ILabelDecorator decorator = (ILabelDecorator)fMemoryBlocks[i].getAdapter(ILabelDecorator.class); 488 if (decorator != null) 489 text = decorator.decorateText(text, fMemoryBlocks[i]); 490 491 labels[i] = text; 492 } 493 return labels; 494 } 495 496 private IMemoryBlock getMemoryBlock(ISelection selection) 497 { 498 if (!(selection instanceof IStructuredSelection)) 499 return null; 500 501 if (selection.isEmpty() || ((IStructuredSelection)selection).size() > 1) 503 { 504 return null; 505 } 506 507 Object elem = ((IStructuredSelection)selection).getFirstElement(); 508 509 if (elem instanceof IMemoryBlock) 510 return (IMemoryBlock)elem; 511 else if (elem instanceof IMemoryRendering) 512 return ((IMemoryRendering)elem).getMemoryBlock(); 513 else 514 return null; 515 } 516 517 public IMemoryBlock getMemoryBlock() 518 { 519 return fSelectedMemoryBlock; 520 } 521 522 525 private void populateDialog(IMemoryBlock currentBlock) { 526 final IMemoryBlock selectMB = currentBlock; 527 Job job = new Job("Populate dialog") { 529 protected IStatus run(IProgressMonitor monitor) { 530 fMemoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(selectMB.getDebugTarget()); 531 int selectionIdx = 0; 532 for (int i=0; i<fMemoryBlocks.length; i++) 533 { 534 if (fMemoryBlocks[i] == selectMB) 535 { 536 selectionIdx = i; 537 break; 538 } 539 } 540 541 final String [] labels = getLabels(fMemoryBlocks); 542 final int idx = selectionIdx; 543 final IMemoryBlock selectedBlk = selectMB; 544 WorkbenchJob wbJob = new WorkbenchJob("populate dialog"){ 546 public IStatus runInUIThread(IProgressMonitor wbMonitor) { 547 doPopulateDialog(memoryBlock, fViewer, labels, idx, selectedBlk); 548 return Status.OK_STATUS; 549 }}; 550 wbJob.setSystem(true); 551 wbJob.schedule(); 552 return Status.OK_STATUS; 553 }}; 554 job.setSystem(true); 555 job.schedule(); 556 } 557 558 561 private void addNew() { 562 WorkbenchJob job = new WorkbenchJob("populate dialog"){ 564 public IStatus runInUIThread(IProgressMonitor monitor) { 565 memoryBlock.add(DebugUIMessages.AddMemoryRenderingDialog_Add_New); 566 memoryBlock.select(0); 567 return Status.OK_STATUS; 568 }}; 569 job.setSystem(true); 570 job.schedule(); 571 } 572 } 573 | Popular Tags |