1 11 12 package org.eclipse.debug.internal.core; 13 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.runtime.ISafeRunnable; 17 import org.eclipse.core.runtime.SafeRunner; 18 import org.eclipse.debug.core.DebugEvent; 19 import org.eclipse.debug.core.DebugException; 20 import org.eclipse.debug.core.DebugPlugin; 21 import org.eclipse.debug.core.IDebugEventSetListener; 22 import org.eclipse.debug.core.IMemoryBlockListener; 23 import org.eclipse.debug.core.IMemoryBlockManager; 24 import org.eclipse.debug.core.model.IDebugTarget; 25 import org.eclipse.debug.core.model.IMemoryBlock; 26 import org.eclipse.debug.core.model.IMemoryBlockExtension; 27 import org.eclipse.debug.core.model.IMemoryBlockRetrieval; 28 29 30 37 public class MemoryBlockManager implements IMemoryBlockManager, IDebugEventSetListener { 38 39 private ArrayList listeners = new ArrayList (); private ArrayList memoryBlocks = new ArrayList (); 42 private static final int ADDED = 0; 43 private static final int REMOVED = 1; 44 48 class MemoryBlockNotifier implements ISafeRunnable { 49 50 private IMemoryBlockListener fListener; 51 private int fType; 52 private IMemoryBlock[] fMemoryBlocks; 53 54 57 public void handleException(Throwable exception) { 58 DebugPlugin.log(exception); 59 } 60 61 64 public void run() throws Exception { 65 switch (fType) { 66 case ADDED: 67 fListener.memoryBlocksAdded(fMemoryBlocks); 68 break; 69 case REMOVED: 70 fListener.memoryBlocksRemoved(fMemoryBlocks); 71 break; 72 } 73 } 74 75 81 public void notify(IMemoryBlock[] memBlocks, int update) { 82 if (listeners != null) { 83 fType = update; 84 Object [] copiedListeners= listeners.toArray(new IMemoryBlockListener[listeners.size()]); 85 for (int i= 0; i < copiedListeners.length; i++) { 86 fListener = (IMemoryBlockListener)copiedListeners[i]; 87 fMemoryBlocks = memBlocks; 88 SafeRunner.run(this); 89 } 90 } 91 fListener = null; 92 fMemoryBlocks = null; 93 } 94 } 95 96 103 private MemoryBlockNotifier getMemoryBlockNotifier() { 104 return new MemoryBlockNotifier(); 105 } 106 107 110 public void addMemoryBlocks(IMemoryBlock[] mem) { 111 if (memoryBlocks == null) { 112 return; 113 } 114 if (mem == null) { 115 DebugPlugin.logMessage("Null argument passed into IMemoryBlockManager.addMemoryBlock", null); return; 117 } 118 119 if(mem.length > 0) { 120 ArrayList newMemoryBlocks = new ArrayList (); 121 for (int i=0; i<mem.length; i++) { 122 if (!memoryBlocks.contains(mem[i])) { 124 newMemoryBlocks.add(mem[i]); 125 memoryBlocks.add(mem[i]); 126 if (memoryBlocks.size() == 1) { 128 DebugPlugin.getDefault().addDebugEventListener(this); 129 } 130 } 131 } 132 notifyListeners((IMemoryBlock[])newMemoryBlocks.toArray(new IMemoryBlock[newMemoryBlocks.size()]), ADDED); 133 } 134 } 135 136 139 public void removeMemoryBlocks(IMemoryBlock[] memBlocks) { 140 if (memoryBlocks == null) { 141 return; 142 } 143 if (memBlocks == null){ 144 DebugPlugin.logMessage("Null argument passed into IMemoryBlockManager.removeMemoryBlock", null); return; 146 } 147 148 if(memBlocks.length > 0) { 149 for (int i=0; i<memBlocks.length; i++) { 150 memoryBlocks.remove(memBlocks[i]); 151 if (memoryBlocks.size() == 0) { 153 DebugPlugin.getDefault().removeDebugEventListener(this); 154 } 155 if (memBlocks[i] instanceof IMemoryBlockExtension) { 156 try { 157 ((IMemoryBlockExtension)memBlocks[i]).dispose(); 158 } catch (DebugException e) { 159 DebugPlugin.log(e); 160 } 161 } 162 } 163 notifyListeners(memBlocks, REMOVED); 164 } 165 } 166 167 170 public void addListener(IMemoryBlockListener listener) { 171 172 if(listeners == null) { 173 return; 174 } 175 if(listener == null) { 176 DebugPlugin.logMessage("Null argument passed into IMemoryBlockManager.addListener", null); return; 178 } 179 if (!listeners.contains(listener)) { 180 listeners.add(listener); 181 } 182 } 183 184 187 public void removeListener(IMemoryBlockListener listener) { 188 189 if(listeners == null) { 190 return; 191 } 192 if(listener == null) { 193 DebugPlugin.logMessage("Null argument passed into IMemoryBlockManager.removeListener", null); return; 195 } 196 if (listeners.contains(listener)) { 197 listeners.remove(listener); 198 } 199 } 200 201 204 public IMemoryBlock[] getMemoryBlocks() { 205 return (IMemoryBlock[])memoryBlocks.toArray(new IMemoryBlock[memoryBlocks.size()]); 206 } 207 208 211 public IMemoryBlock[] getMemoryBlocks(IDebugTarget debugTarget) { 212 IMemoryBlock[] blocks = (IMemoryBlock[])memoryBlocks.toArray(new IMemoryBlock[memoryBlocks.size()]); 213 ArrayList memoryBlocksList = new ArrayList (); 214 for (int i=0; i<blocks.length; i++) { 215 if (blocks[i].getDebugTarget() == debugTarget) { 216 memoryBlocksList.add(blocks[i]); 217 } 218 } 219 return (IMemoryBlock[])memoryBlocksList.toArray(new IMemoryBlock[memoryBlocksList.size()]); 220 } 221 222 225 public IMemoryBlock[] getMemoryBlocks(IMemoryBlockRetrieval retrieve) { 226 IMemoryBlock[] blocks = (IMemoryBlock[])memoryBlocks.toArray(new IMemoryBlock[memoryBlocks.size()]); 227 ArrayList memoryBlocksList = new ArrayList (blocks.length); 228 for (int i=0; i<blocks.length; i++) { 229 if (blocks[i] instanceof IMemoryBlockExtension) { 230 if (((IMemoryBlockExtension)blocks[i]).getMemoryBlockRetrieval() == retrieve) { 231 memoryBlocksList.add(blocks[i]); 232 } 233 } 234 else { 235 if (blocks[i].getDebugTarget() == retrieve) { 237 memoryBlocksList.add(blocks[i]); 238 } 239 } 240 } 241 return (IMemoryBlock[])memoryBlocksList.toArray(new IMemoryBlock[memoryBlocksList.size()]); 242 } 243 244 249 private void notifyListeners(IMemoryBlock[] memBlocks, int event) { 250 getMemoryBlockNotifier().notify(memBlocks, event); 251 } 252 253 256 public void handleDebugEvents(DebugEvent[] events) { 257 for (int i=0; i < events.length; i++) { 258 handleDebugEvent(events[i]); 259 } 260 } 261 262 266 public void handleDebugEvent(DebugEvent event) { 267 Object obj = event.getSource(); 268 IDebugTarget dt = null; 269 270 if (event.getKind() == DebugEvent.TERMINATE) { 271 if (obj instanceof IDebugTarget) { 274 dt = ((IDebugTarget)obj); 275 276 IMemoryBlock[] deletedMemoryBlocks = getMemoryBlocks(dt); 278 removeMemoryBlocks(deletedMemoryBlocks); 279 } 280 } 281 } 282 283 286 public void shutdown() { 287 if (listeners != null) { 288 listeners.clear(); 289 listeners = null; 290 } 291 292 if (memoryBlocks != null) { 293 memoryBlocks.clear(); 294 memoryBlocks = null; 295 } 296 } 297 298 } 299 | Popular Tags |