1 11 12 package org.eclipse.debug.internal.core.memory; 13 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.runtime.ISafeRunnable; 17 import org.eclipse.core.runtime.Platform; 18 import org.eclipse.debug.core.DebugEvent; 19 import org.eclipse.debug.core.DebugPlugin; 20 import org.eclipse.debug.core.IDebugEventSetListener; 21 import org.eclipse.debug.core.model.IDebugTarget; 22 import org.eclipse.debug.core.model.IMemoryBlock; 23 import org.eclipse.debug.core.model.IMemoryBlockRetrieval; 24 25 26 33 public class MemoryBlockManager implements IMemoryBlockManager, IDebugEventSetListener { 34 35 private ArrayList listeners = new ArrayList (); private ArrayList memoryBlocks = new ArrayList (); 38 private static final int ADDED = 0; 39 private static final int REMOVED = 1; 40 41 42 45 private static MemoryBlockManager fgMemoryBlockManager; 46 47 50 private static MemoryRenderingManager fgMemoryRenderingManager; 51 52 58 public static IMemoryBlockManager getMemoryBlockManager(){ 59 if (fgMemoryBlockManager == null) 60 { 61 fgMemoryBlockManager = new MemoryBlockManager(); 62 } 63 64 return fgMemoryBlockManager; 65 } 66 67 73 public static IMemoryRenderingManager getMemoryRenderingManager() { 74 if (fgMemoryRenderingManager == null) 75 { 76 fgMemoryRenderingManager = new MemoryRenderingManager(); 77 } 78 79 return fgMemoryRenderingManager; 80 } 81 82 public static void pluginShutdown() { 83 if (fgMemoryBlockManager != null) { 84 fgMemoryBlockManager.shutdown(); 85 } 86 if (fgMemoryRenderingManager != null) { 87 fgMemoryRenderingManager.shutdown(); 88 } 89 90 } 91 92 96 class MemoryBlockNotifier implements ISafeRunnable { 97 98 private IMemoryBlockListener fListener; 99 private int fType; 100 private IMemoryBlock fMemoryBlock; 101 102 105 public void handleException(Throwable exception) { 106 DebugPlugin.log(exception); 107 } 108 109 112 public void run() throws Exception { 113 switch (fType) { 114 case ADDED: 115 fListener.MemoryBlockAdded(fMemoryBlock); 116 break; 117 case REMOVED: 118 fListener.MemoryBlockRemoved(fMemoryBlock); 119 break; 120 } 121 } 122 123 126 public void notify(IMemoryBlock memoryBlock, int update) { 127 if (listeners != null) { 128 fType = update; 129 Object [] copiedListeners= listeners.toArray(new IMemoryBlockListener[listeners.size()]); 130 for (int i= 0; i < copiedListeners.length; i++) { 131 fListener = (IMemoryBlockListener)copiedListeners[i]; 132 fMemoryBlock = memoryBlock; 133 Platform.run(this); 134 } 135 } 136 fListener = null; 137 fMemoryBlock = null; 138 } 139 } 140 141 private MemoryBlockNotifier getMemoryBlockNotifier() { 142 return new MemoryBlockNotifier(); 143 } 144 145 146 149 public void addMemoryBlock(IMemoryBlock mem, boolean addDefaultRenderings) { 150 151 if (memoryBlocks == null) 152 return; 153 154 if (mem == null){ 155 DebugPlugin.logMessage("Null argument passed into IMemoryBlockManager.addMemoryBlock", null); return; 157 } 158 159 if (memoryBlocks.contains(mem)) 161 return; 162 163 MemoryRenderingManager renderingManager = (MemoryRenderingManager) getMemoryRenderingManager(); 164 165 if (!addDefaultRenderings) 166 { 167 renderingManager.setHandleMemoryBlockAddedEvent(false); 168 } 169 else 170 { 171 renderingManager.setHandleMemoryBlockAddedEvent(true); 172 } 173 174 if (mem == null){ 175 DebugPlugin.logMessage("Null argument passed into IMemoryBlockManager.addMemoryBlock", null); return; 177 } 178 179 memoryBlocks.add(mem); 180 181 if (memoryBlocks.size() == 1) 183 { 184 DebugPlugin.getDefault().addDebugEventListener(this); 185 } 186 187 notifyListeners(mem, ADDED); 188 189 renderingManager.setHandleMemoryBlockAddedEvent(true); 191 } 192 193 196 public void removeMemoryBlock(IMemoryBlock mem) { 197 198 if (memoryBlocks == null) 199 return; 200 201 if (mem == null){ 202 DebugPlugin.logMessage("Null argument passed into IMemoryBlockManager.removeMemoryBlock", null); return; 204 } 205 206 memoryBlocks.remove(mem); 207 208 if (memoryBlocks.size() == 0) 210 { 211 DebugPlugin.getDefault().removeDebugEventListener(this); 212 } 213 214 if (mem instanceof IExtendedMemoryBlock) 215 { 216 ((IExtendedMemoryBlock)mem).delete(); 217 } 218 219 notifyListeners(mem, REMOVED); 220 } 221 222 225 public void addListener(IMemoryBlockListener listener) { 226 227 if(listeners == null) 228 return; 229 230 if(listener == null){ 231 DebugPlugin.logMessage("Null argument passed into IMemoryBlockManager.addListener", null); return; 233 } 234 235 if (!listeners.contains(listener)) 236 listeners.add(listener); 237 } 238 239 242 public void removeListener(IMemoryBlockListener listener) { 243 244 if(listeners == null) 245 return; 246 247 if(listener == null){ 248 DebugPlugin.logMessage("Null argument passed into IMemoryBlockManager.removeListener", null); return; 250 } 251 252 if (listeners.contains(listener)) 253 listeners.remove(listener); 254 } 255 256 259 public IMemoryBlock[] getAllMemoryBlocks() { 260 261 IMemoryBlock[] blocks = (IMemoryBlock[])memoryBlocks.toArray(new IMemoryBlock[memoryBlocks.size()]); 262 263 return blocks; 264 } 265 266 269 public IMemoryBlock[] getMemoryBlocks(IDebugTarget debugTarget) { 270 271 IMemoryBlock[] blocks = (IMemoryBlock[])memoryBlocks.toArray(new IMemoryBlock[memoryBlocks.size()]); 272 273 ArrayList memoryBlocksList = new ArrayList (); 274 275 for (int i=0; i<blocks.length; i++) 276 { 277 if (blocks[i].getDebugTarget() == debugTarget) 278 memoryBlocksList.add(blocks[i]); 279 } 280 281 return (IMemoryBlock[])memoryBlocksList.toArray(new IMemoryBlock[memoryBlocksList.size()]); 282 283 } 284 285 288 public IMemoryBlock[] getMemoryBlocks(IMemoryBlockRetrieval retrieve) { 289 IMemoryBlock[] blocks = (IMemoryBlock[])memoryBlocks.toArray(new IMemoryBlock[memoryBlocks.size()]); 290 291 ArrayList memoryBlocksList = new ArrayList (blocks.length); 292 293 for (int i=0; i<blocks.length; i++) 294 { 295 if (blocks[i] instanceof IExtendedMemoryBlock) 296 { 297 298 if (((IExtendedMemoryBlock)blocks[i]).getMemoryBlockRetrieval() == retrieve) 299 { 300 memoryBlocksList.add(blocks[i]); 301 } 302 } 303 else 304 { 305 if (blocks[i].getDebugTarget() == retrieve) 307 memoryBlocksList.add(blocks[i]); 308 } 309 } 310 311 return (IMemoryBlock[])memoryBlocksList.toArray(new IMemoryBlock[memoryBlocksList.size()]); 312 } 313 314 315 private void notifyListeners(IMemoryBlock memoryBlock, int event) 316 { 317 getMemoryBlockNotifier().notify(memoryBlock, event); 318 } 319 320 321 324 public void handleDebugEvents(DebugEvent[] events) { 325 326 for (int i=0; i < events.length; i++) 327 handleDebugEvent(events[i]); 328 329 } 330 331 public void handleDebugEvent(DebugEvent event) { 332 Object obj = event.getSource(); 333 IDebugTarget dt = null; 334 335 if (event.getKind() == DebugEvent.TERMINATE) 336 { 337 if (obj instanceof IDebugTarget) 340 { 341 dt = ((IDebugTarget)obj); 342 } 343 344 IMemoryBlock[] deletedMemoryBlocks = getMemoryBlocks(dt); 346 347 for (int i=0; i<deletedMemoryBlocks.length; i++) 348 { 349 removeMemoryBlock(deletedMemoryBlocks[i]); 350 } 351 } 352 } 353 354 357 public void shutdown() 358 { 359 if (listeners != null) 360 { 361 listeners.clear(); 362 listeners = null; 363 } 364 365 if (memoryBlocks != null) 366 { 367 memoryBlocks.clear(); 368 memoryBlocks = null; 369 } 370 } 371 } 372 | Popular Tags |