1 11 package org.eclipse.debug.ui.memory; 12 13 import org.eclipse.core.runtime.IProgressMonitor; 14 import org.eclipse.core.runtime.IStatus; 15 import org.eclipse.core.runtime.ListenerList; 16 import org.eclipse.core.runtime.PlatformObject; 17 import org.eclipse.core.runtime.SafeRunner; 18 import org.eclipse.core.runtime.Status; 19 import org.eclipse.core.runtime.jobs.Job; 20 import org.eclipse.debug.core.DebugException; 21 import org.eclipse.debug.core.model.IMemoryBlock; 22 import org.eclipse.debug.core.model.IMemoryBlockExtension; 23 import org.eclipse.debug.internal.ui.views.memory.PropertyChangeNotifier; 24 import org.eclipse.debug.ui.DebugUITools; 25 import org.eclipse.jface.action.IMenuListener; 26 import org.eclipse.jface.action.IMenuManager; 27 import org.eclipse.jface.action.MenuManager; 28 import org.eclipse.jface.action.Separator; 29 import org.eclipse.jface.util.IPropertyChangeListener; 30 import org.eclipse.jface.util.PropertyChangeEvent; 31 import org.eclipse.jface.viewers.ILabelDecorator; 32 import org.eclipse.jface.viewers.ISelectionProvider; 33 import org.eclipse.swt.graphics.Image; 34 import org.eclipse.swt.widgets.Control; 35 import org.eclipse.swt.widgets.Menu; 36 import org.eclipse.ui.IWorkbenchActionConstants; 37 38 55 public abstract class AbstractMemoryRendering extends PlatformObject implements IMemoryRendering{ 56 57 private IMemoryBlock fMemoryBlock; 58 private IMemoryRenderingContainer fContainer; 59 private ListenerList fPropertyListeners; 60 private boolean fVisible = true; 61 private MenuManager fPopupMenuMgr; 62 private String fRenderingId; 63 64 private class ConnectionJob extends Job 65 { 66 Runnable fRunnable; 67 ConnectionJob(Runnable runnable) 68 { 69 super("Connect/Disconnect MemoryBlock"); fRunnable = runnable; 71 setSystem(true); 72 } 73 74 protected IStatus run(IProgressMonitor monitor) { 75 fRunnable.run(); 76 return Status.OK_STATUS; 77 } 78 } 79 80 85 private ILabelDecorator fLabelDecorator; 86 private IMenuListener fMenuListener; 87 88 93 public AbstractMemoryRendering(String renderingId) 94 { 95 fRenderingId = renderingId; 96 } 97 98 101 public void init(IMemoryRenderingContainer container, IMemoryBlock block) { 102 fContainer = container; 103 fMemoryBlock = block; 104 105 fLabelDecorator = (ILabelDecorator)fMemoryBlock.getAdapter(ILabelDecorator.class); 106 } 107 108 111 public void dispose() 112 { 113 if (fMemoryBlock instanceof IMemoryBlockExtension) 115 { 116 Runnable runnable = new Runnable (){ 117 public void run() { 118 ((IMemoryBlockExtension)fMemoryBlock).disconnect(this); 119 }}; 120 new ConnectionJob(runnable).schedule(); 121 } 122 123 if (fPopupMenuMgr != null) 124 { 125 fPopupMenuMgr.removeMenuListener(fMenuListener); 126 fPopupMenuMgr.removeAll(); 127 fPopupMenuMgr.dispose(); 128 fPopupMenuMgr = null; 129 } 130 131 if (fPropertyListeners != null) 132 fPropertyListeners = null; 133 } 134 135 138 public void activated() { 139 if (fContainer.getMemoryRenderingSite().getSynchronizationService() != null) 140 fContainer.getMemoryRenderingSite().getSynchronizationService().setSynchronizationProvider(this); 141 } 142 143 146 public void deactivated() { 147 } 153 154 157 public void becomesVisible() { 158 fVisible = true; 159 160 if (fMemoryBlock instanceof IMemoryBlockExtension) 161 { 162 Runnable runnable = new Runnable (){ 163 public void run() { 164 ((IMemoryBlockExtension)fMemoryBlock).connect(this); 165 }}; 166 new ConnectionJob(runnable).schedule(); 167 } 168 } 169 170 173 public void becomesHidden() { 174 fVisible = false; 175 if (fMemoryBlock instanceof IMemoryBlockExtension) 176 { 177 Runnable runnable = new Runnable (){ 178 public void run() { 179 ((IMemoryBlockExtension)fMemoryBlock).disconnect(this); 180 }}; 181 new ConnectionJob(runnable).schedule(); 182 } 183 } 184 185 188 public IMemoryBlock getMemoryBlock() { 189 return fMemoryBlock; 190 } 191 192 195 public String getRenderingId() 196 { 197 return fRenderingId; 198 } 199 200 203 public void addPropertyChangeListener(IPropertyChangeListener listener) { 204 205 if (fPropertyListeners == null) 206 fPropertyListeners = new ListenerList(); 207 208 fPropertyListeners.add(listener); 209 } 210 211 214 public void removePropertyChangeListener(IPropertyChangeListener listener) { 215 216 if (fPropertyListeners == null) 217 return; 218 fPropertyListeners.remove(listener); 219 } 220 221 224 public Image getImage() { 225 return decorateImage(null); 226 } 227 228 235 protected Image decorateImage(Image image) { 236 if (fLabelDecorator != null) 237 return fLabelDecorator.decorateImage(image, this); 238 return image; 239 } 240 241 244 public String getLabel() 245 { 246 if (fMemoryBlock == null) 247 return ""; 249 StringBuffer label = new StringBuffer (""); 251 if (fMemoryBlock instanceof IMemoryBlockExtension) 252 { 253 String expression = ((IMemoryBlockExtension)fMemoryBlock).getExpression(); 254 255 if (expression == null) 256 expression = ""; 258 label.append(expression); 259 260 if (expression.startsWith("&")) label.insert(0, "&"); 263 try { 266 if (fVisible && ((IMemoryBlockExtension)fMemoryBlock).getBigBaseAddress() != null) 267 { 268 label.append(" : 0x"); label.append(((IMemoryBlockExtension)fMemoryBlock).getBigBaseAddress().toString(16).toUpperCase()); 270 } 271 } catch (DebugException e) { 272 } 274 } 275 else 276 { 277 long address = fMemoryBlock.getStartAddress(); 278 label.append(Long.toHexString(address).toUpperCase()); 279 } 280 281 IMemoryRenderingType type = DebugUITools.getMemoryRenderingManager().getRenderingType(getRenderingId()); 282 283 if (type != null) 284 { 285 String preName = type.getLabel(); 286 287 if (preName != null) 288 { 289 label.append(" <"); label.append(preName); 291 label.append(">"); } 293 } 294 295 return decorateLabel(label.toString()); 296 } 297 298 305 protected String decorateLabel(String label) { 306 if (fLabelDecorator != null) 307 return fLabelDecorator.decorateText(label.toString(), this); 308 return label.toString(); 309 } 310 311 330 protected void createPopupMenu(Control control) 331 { 332 if (fPopupMenuMgr == null) 333 { 334 fPopupMenuMgr = new MenuManager("#PopupMenu"); fPopupMenuMgr.setRemoveAllWhenShown(true); 336 IMemoryRenderingSite site = fContainer.getMemoryRenderingSite(); 337 String menuId = fContainer.getId(); 338 339 ISelectionProvider selProvider = site.getSite().getSelectionProvider(); 340 341 fMenuListener = new IMenuListener() { 342 public void menuAboutToShow(IMenuManager manager) { 343 manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); 344 }}; 345 fPopupMenuMgr.addMenuListener(fMenuListener); 346 347 site.getSite().registerContextMenu(menuId, fPopupMenuMgr, selProvider); 348 } 349 350 Menu popupMenu = fPopupMenuMgr.createContextMenu(control); 351 control.setMenu(popupMenu); 352 } 353 354 360 protected MenuManager getPopupMenuManager() 361 { 362 return fPopupMenuMgr; 363 } 364 365 370 protected void firePropertyChangedEvent(PropertyChangeEvent event) 371 { 372 if (fPropertyListeners == null) 373 return; 374 375 Object [] listeners = fPropertyListeners.getListeners(); 376 377 for (int i=0; i<listeners.length; i++) 378 { 379 PropertyChangeNotifier notifier = new PropertyChangeNotifier((IPropertyChangeListener)listeners[i], event); 380 SafeRunner.run(notifier); 381 } 382 } 383 384 389 public IMemoryRenderingContainer getMemoryRenderingContainer() 390 { 391 return fContainer; 392 } 393 394 399 public boolean isVisible() { 400 return fVisible; 401 } 402 } 403 | Popular Tags |