KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > core > memory > MemoryBlockManager


1 /*******************************************************************************
2  * Copyright (c) 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.debug.internal.core.memory;
13
14 import java.util.ArrayList JavaDoc;
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 /**
27  * Implementation of IMemoryBlockManager
28  * The manager is responsible to manage all memory blocks in the workbench.
29  *
30  * @since 3.0
31  *
32  */

33 public class MemoryBlockManager implements IMemoryBlockManager, IDebugEventSetListener {
34     
35     private ArrayList JavaDoc listeners = new ArrayList JavaDoc(); // list of all IMemoryBlockListener
36
private ArrayList JavaDoc memoryBlocks = new ArrayList JavaDoc(); // list of all memory blocks
37

38     private static final int ADDED = 0;
39     private static final int REMOVED = 1;
40     
41     
42     /**
43      * The singleton memory block manager.
44      */

45     private static MemoryBlockManager fgMemoryBlockManager;
46     
47     /**
48      * The singleton memory rendering manager.
49      */

50     private static MemoryRenderingManager fgMemoryRenderingManager;
51     
52     /**
53      * Returns the memory block manager.
54      * @return the memory block manager.
55      * @see IMemoryBlockManager
56      * @since 3.0
57      */

58     public static IMemoryBlockManager getMemoryBlockManager(){
59         if (fgMemoryBlockManager == null)
60         {
61             fgMemoryBlockManager = new MemoryBlockManager();
62         }
63         
64         return fgMemoryBlockManager;
65     }
66     
67     /**
68      * Returns the memory rendering manager.
69      * @return the memory rendering manager.
70      * @see IMemoryRenderingManager
71      * @since 3.0
72      */

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     /**
93      * Notifies a memory block listener in a safe runnable to
94      * handle exceptions.
95      */

96     class MemoryBlockNotifier implements ISafeRunnable {
97         
98         private IMemoryBlockListener fListener;
99         private int fType;
100         private IMemoryBlock fMemoryBlock;
101         
102         /**
103          * @see org.eclipse.core.runtime.ISafeRunnable#handleException(java.lang.Throwable)
104          */

105         public void handleException(Throwable JavaDoc exception) {
106             DebugPlugin.log(exception);
107         }
108
109         /**
110          * @see org.eclipse.core.runtime.ISafeRunnable#run()
111          */

112         public void run() throws Exception JavaDoc {
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         /**
124          * Notify listeners of added/removed memory block events
125          */

126         public void notify(IMemoryBlock memoryBlock, int update) {
127             if (listeners != null) {
128                 fType = update;
129                 Object JavaDoc[] 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     /* (non-Javadoc)
147      * @see org.eclipse.debug.ui.IMemoryBlockManager#addMemoryBlock(org.eclipse.debug.core.model.IMemoryBlock)
148      */

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); //$NON-NLS-1$
156
return;
157         }
158         
159         // do not allow duplicates
160
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); //$NON-NLS-1$
176
return;
177         }
178         
179         memoryBlocks.add(mem);
180         
181         // add listener for the first memory block added
182
if (memoryBlocks.size() == 1)
183         {
184             DebugPlugin.getDefault().addDebugEventListener(this);
185         }
186         
187         notifyListeners(mem, ADDED);
188
189         // always set it back to true
190
renderingManager.setHandleMemoryBlockAddedEvent(true);
191     }
192
193     /* (non-Javadoc)
194      * @see org.eclipse.debug.ui.IMemoryBlockManager#removeMemoryBlock(org.eclipse.debug.core.model.IMemoryBlock)
195      */

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); //$NON-NLS-1$
203
return;
204         }
205         
206         memoryBlocks.remove(mem);
207         
208         // remove listener after the last memory block has been removed
209
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     /* (non-Javadoc)
223      * @see org.eclipse.debug.ui.IMemoryBlockManager#addListener(org.eclipse.debug.ui.IMemoryBlockListener)
224      */

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); //$NON-NLS-1$
232
return;
233         }
234         
235         if (!listeners.contains(listener))
236             listeners.add(listener);
237     }
238
239     /* (non-Javadoc)
240      * @see org.eclipse.debug.ui.IMemoryBlockManager#removeListener(org.eclipse.debug.ui.IMemoryBlockListener)
241      */

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); //$NON-NLS-1$
249
return;
250         }
251         
252         if (listeners.contains(listener))
253             listeners.remove(listener);
254     }
255
256     /* (non-Javadoc)
257      * @see org.eclipse.debug.ui.IMemoryBlockManager#getAllMemoryBlocks()
258      */

259     public IMemoryBlock[] getAllMemoryBlocks() {
260         
261         IMemoryBlock[] blocks = (IMemoryBlock[])memoryBlocks.toArray(new IMemoryBlock[memoryBlocks.size()]);
262         
263         return blocks;
264     }
265
266     /* (non-Javadoc)
267      * @see org.eclipse.debug.ui.IMemoryBlockManager#getMemoryBlocks(org.eclipse.debug.core.model.IDebugTarget)
268      */

269     public IMemoryBlock[] getMemoryBlocks(IDebugTarget debugTarget) {
270         
271         IMemoryBlock[] blocks = (IMemoryBlock[])memoryBlocks.toArray(new IMemoryBlock[memoryBlocks.size()]);
272         
273         ArrayList JavaDoc memoryBlocksList = new ArrayList JavaDoc();
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     /* (non-Javadoc)
286      * @see org.eclipse.debug.ui.IMemoryBlockManager#getMemoryBlocks(org.eclipse.debug.core.model.IMemoryBlockRetrieval)
287      */

288     public IMemoryBlock[] getMemoryBlocks(IMemoryBlockRetrieval retrieve) {
289         IMemoryBlock[] blocks = (IMemoryBlock[])memoryBlocks.toArray(new IMemoryBlock[memoryBlocks.size()]);
290         
291         ArrayList JavaDoc memoryBlocksList = new ArrayList JavaDoc(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                 // standard memory block always uses the debug target as the memory block retrieval
306
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     /* (non-Javadoc)
322      * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[])
323      */

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 JavaDoc obj = event.getSource();
333         IDebugTarget dt = null;
334         
335         if (event.getKind() == DebugEvent.TERMINATE)
336         {
337             // a terminate event could happen from an IThread or IDebugTarget
338
// only handle a debug event from the debug target
339
if (obj instanceof IDebugTarget)
340             {
341                 dt = ((IDebugTarget)obj);
342             }
343             
344             // getMemoryBlocks will return an empty array if dt is null
345
IMemoryBlock[] deletedMemoryBlocks = getMemoryBlocks(dt);
346             
347             for (int i=0; i<deletedMemoryBlocks.length; i++)
348             {
349                 removeMemoryBlock(deletedMemoryBlocks[i]);
350             }
351         }
352     }
353     
354     /**
355      * Clean up when the plugin is shut down
356      */

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