KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > memory > ViewPaneRenderingMgr


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

11 package org.eclipse.debug.internal.ui.views.memory;
12
13 import java.io.IOException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import javax.xml.parsers.ParserConfigurationException JavaDoc;
17 import javax.xml.transform.TransformerException JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.Preferences;
21 import org.eclipse.debug.core.DebugEvent;
22 import org.eclipse.debug.core.DebugPlugin;
23 import org.eclipse.debug.core.IDebugEventSetListener;
24 import org.eclipse.debug.core.model.IDebugTarget;
25 import org.eclipse.debug.core.model.IMemoryBlock;
26 import org.eclipse.debug.internal.core.LaunchManager;
27 import org.eclipse.debug.internal.ui.DebugUIPlugin;
28 import org.eclipse.debug.ui.DebugUITools;
29 import org.eclipse.debug.ui.memory.IMemoryRendering;
30 import org.eclipse.debug.ui.memory.IMemoryRenderingContainer;
31 import org.eclipse.debug.ui.memory.IMemoryRenderingSite;
32 import org.eclipse.debug.ui.memory.IMemoryRenderingType;
33 import org.eclipse.ui.IViewSite;
34 import org.eclipse.ui.IWorkbenchPartSite;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39
40 /**
41  * A View Pane Rendering Manager manages all the rendering from a view pane.
42  * It is responsible for handling debug events and removing renderings
43  * from the view pane as a debug session is terminated.
44  * In addition, the rendering manager is responsible for persisting memory renderings.
45  * Renderings need to be persisted when the memory view is disposed. If the view is
46  * opened again, the same set of renderings will be created in the view pane if
47  * the renderings are still valid.
48  * @since 3.1
49  */

50 public class ViewPaneRenderingMgr implements IDebugEventSetListener{
51
52     private ArrayList JavaDoc fRenderings = new ArrayList JavaDoc();
53     private IMemoryRenderingContainer fViewPane;
54     
55     private static final String JavaDoc RENDERINGS_TAG = "persistedMemoryRenderings"; //$NON-NLS-1$
56
private static final String JavaDoc MEMORY_RENDERING_TAG = "memoryRendering"; //$NON-NLS-1$
57
private static final String JavaDoc MEMORY_BLOCK = "memoryBlock"; //$NON-NLS-1$
58
private static final String JavaDoc RENDERING_ID = "renderingId"; //$NON-NLS-1$
59

60     public ViewPaneRenderingMgr(IMemoryRenderingContainer viewPane)
61     {
62         fViewPane = viewPane;
63         loadPersistedRenderings(getPrefId());
64     }
65
66     public void removeMemoryBlockRendering(IMemoryBlock mem, String JavaDoc renderingId)
67     {
68         if(fRenderings == null)
69             return;
70         
71         IMemoryRendering[] toRemove = getRenderings(mem, renderingId);
72         
73         for (int i=0; i<toRemove.length; i++)
74         {
75             fRenderings.remove(toRemove[i]);
76             
77             // remove listener after the last memory block has been removed
78
if (fRenderings.size() == 0)
79             {
80                 DebugPlugin.getDefault().removeDebugEventListener(this);
81             }
82         }
83         
84         storeRenderings();
85     }
86     
87
88     public void addMemoryBlockRendering(IMemoryRendering rendering) {
89         
90         // do not allow duplicated objects
91
if (fRenderings.contains(rendering))
92             return;
93         
94         fRenderings.add(rendering);
95         
96         // add listener for the first memory block added
97
if (fRenderings.size() == 1)
98         {
99             DebugPlugin.getDefault().addDebugEventListener(this);
100         }
101         
102         storeRenderings();
103     }
104
105
106     public void removeMemoryBlockRendering(IMemoryRendering rendering) {
107         if(rendering == null)
108             return;
109         
110         if(!fRenderings.contains(rendering))
111             return;
112         
113         fRenderings.remove(rendering);
114         
115         // remove listener after the last memory block has been removed
116
if (fRenderings.size() == 0)
117         {
118             DebugPlugin.getDefault().removeDebugEventListener(this);
119         }
120         
121         storeRenderings();
122     }
123
124     public IMemoryRendering[] getRenderings(IMemoryBlock mem, String JavaDoc renderingId)
125     {
126         if (renderingId == null)
127         {
128             return getRenderingsFromMemoryBlock(mem);
129         }
130         
131         ArrayList JavaDoc ret = new ArrayList JavaDoc();
132         for (int i=0; i<fRenderings.size(); i++)
133         {
134             if (fRenderings.get(i) instanceof IMemoryRendering)
135             {
136                 IMemoryRendering rendering = (IMemoryRendering)fRenderings.get(i);
137                 if (rendering.getMemoryBlock() == mem && renderingId.equals(rendering.getRenderingId()))
138                 {
139                     ret.add(rendering);
140                 }
141             }
142         }
143         
144         return (IMemoryRendering[])ret.toArray(new IMemoryRendering[ret.size()]);
145     }
146     
147     public IMemoryRendering[] getRenderings()
148     {
149         return (IMemoryRendering[])fRenderings.toArray(new IMemoryRendering[fRenderings.size()]);
150     }
151
152     public IMemoryRendering[] getRenderingsFromDebugTarget(IDebugTarget target)
153     {
154         ArrayList JavaDoc ret = new ArrayList JavaDoc();
155         for (int i=0; i<fRenderings.size(); i++)
156         {
157             if (fRenderings.get(i) instanceof IMemoryRendering)
158             {
159                 IMemoryRendering rendering = (IMemoryRendering)fRenderings.get(i);
160                 if (rendering.getMemoryBlock().getDebugTarget() == target)
161                 {
162                     ret.add(rendering);
163                 }
164             }
165         }
166         
167         return (IMemoryRendering[])ret.toArray(new IMemoryRendering[ret.size()]);
168     }
169     
170
171     public IMemoryRendering[] getRenderingsFromMemoryBlock(IMemoryBlock block)
172     {
173         ArrayList JavaDoc ret = new ArrayList JavaDoc();
174         for (int i=0; i<fRenderings.size(); i++)
175         {
176             if (fRenderings.get(i) instanceof IMemoryRendering)
177             {
178                 IMemoryRendering rendering = (IMemoryRendering)fRenderings.get(i);
179                 if (rendering.getMemoryBlock() == block)
180                 {
181                     ret.add(rendering);
182                 }
183             }
184         }
185         
186         return (IMemoryRendering[])ret.toArray(new IMemoryRendering[ret.size()]);
187     }
188     
189     
190
191     public void handleDebugEvents(DebugEvent[] events) {
192         
193         for (int i=0; i < events.length; i++)
194             handleDebugEvent(events[i]);
195         
196     }
197     
198     public void handleDebugEvent(DebugEvent event) {
199         Object JavaDoc obj = event.getSource();
200         IDebugTarget dt = null;
201         
202         if (event.getKind() == DebugEvent.TERMINATE)
203         {
204             // a terminate event could happen from an IThread or IDebugTarget
205
// Only handle terminate event from debug target
206
if (obj instanceof IDebugTarget)
207             {
208                 dt = ((IDebugTarget)obj);
209                 
210                 // returns empty array if dt == null
211
IMemoryRendering[] deletedrendering = getRenderingsFromDebugTarget(dt);
212                 
213                 for (int i=0; i<deletedrendering.length; i++)
214                 {
215                     removeMemoryBlockRendering(deletedrendering[i].getMemoryBlock(), deletedrendering[i].getRenderingId());
216                     fViewPane.removeMemoryRendering(deletedrendering[i]);
217                 }
218             }
219         }
220     }
221
222     public void dispose()
223     {
224         // remove all renderings
225
fRenderings.clear();
226         
227         String JavaDoc secondaryId = getViewSiteSecondaryId();
228         if (secondaryId != null)
229         {
230             // do not save renderings if this is not the primary rendering view
231
String JavaDoc prefid = getPrefId();
232             Preferences prefs = DebugUIPlugin.getDefault().getPluginPreferences();
233             prefs.setToDefault(prefid);
234         }
235             
236         DebugPlugin.getDefault().removeDebugEventListener(this);
237     }
238     
239     /**
240      * Store renderings as preferences. If renderings are stored, renderings
241      * can be persisted even after the memory view is closed.
242      */

243     private void storeRenderings()
244     {
245         Preferences prefs = DebugUIPlugin.getDefault().getPluginPreferences();
246         String JavaDoc renderingsStr= ""; //$NON-NLS-1$
247
try {
248             renderingsStr= getRenderingsAsXML();
249         } catch (IOException JavaDoc e) {
250             DebugUIPlugin.log(e);
251         } catch (ParserConfigurationException JavaDoc e) {
252             DebugUIPlugin.log(e);
253         } catch (TransformerException JavaDoc e) {
254             DebugUIPlugin.log(e);
255         }
256         
257         String JavaDoc prefid = getPrefId();
258         
259         if (renderingsStr != null)
260             prefs.setValue(prefid, renderingsStr);
261         else
262             prefs.setToDefault(prefid);
263     }
264
265     private String JavaDoc getPrefId() {
266         // constructs id based on memory view's secondary id + the rendering view pane id
267
// format: secondaryId:viewPaneId
268
StringBuffer JavaDoc id = new StringBuffer JavaDoc();
269         IMemoryRenderingSite renderingSite = fViewPane.getMemoryRenderingSite();
270         IWorkbenchPartSite ps = renderingSite.getSite();
271         if (ps instanceof IViewSite)
272         {
273             IViewSite vs = (IViewSite)ps;
274             String JavaDoc secondaryId = vs.getSecondaryId();
275             if (secondaryId != null)
276             {
277                 id.append(secondaryId);
278                 id.append(":"); //$NON-NLS-1$
279
}
280             
281         }
282         id.append(fViewPane.getId());
283         String JavaDoc prefId = id.toString();
284         return prefId;
285     }
286     
287     /**
288      * Convert renderings to xml text
289      * @return
290      * @throws IOException
291      * @throws ParserConfigurationException
292      * @throws TransformerException
293      */

294     private String JavaDoc getRenderingsAsXML() throws IOException JavaDoc, ParserConfigurationException JavaDoc, TransformerException JavaDoc {
295         IMemoryRendering[] renderings= (IMemoryRendering[])fRenderings.toArray(new IMemoryRendering[fRenderings.size()]);
296         
297         if (renderings.length == 0)
298             return null;
299         
300         Document JavaDoc document= LaunchManager.getDocument();
301         Element JavaDoc rootElement= document.createElement(RENDERINGS_TAG);
302         document.appendChild(rootElement);
303         for (int i = 0; i < renderings.length; i++) {
304             IMemoryRendering rendering= renderings[i];
305             Element JavaDoc element= document.createElement(MEMORY_RENDERING_TAG);
306             element.setAttribute(MEMORY_BLOCK, Integer.toString(rendering.getMemoryBlock().hashCode()));
307             element.setAttribute(RENDERING_ID, rendering.getRenderingId());
308             rootElement.appendChild(element);
309         }
310         return LaunchManager.serializeDocument(document);
311     }
312
313     /**
314      * Load renderings currently stored.
315      */

316     private void loadPersistedRenderings(String JavaDoc prefId) {
317         String JavaDoc renderingsStr= DebugUIPlugin.getDefault().getPluginPreferences().getString(prefId);
318         if (renderingsStr.length() == 0) {
319             return;
320         }
321         Element JavaDoc root;
322         try {
323             root = DebugPlugin.parseDocument(renderingsStr);
324         } catch (CoreException e) {
325             DebugUIPlugin.logErrorMessage("An exception occurred while loading memory renderings."); //$NON-NLS-1$
326
return;
327         }
328         if (!root.getNodeName().equals(RENDERINGS_TAG)) {
329             DebugUIPlugin.logErrorMessage("Invalid format encountered while loading memory renderings."); //$NON-NLS-1$
330
return;
331         }
332         NodeList JavaDoc list= root.getChildNodes();
333         boolean renderingsAdded= false;
334         for (int i= 0, numItems= list.getLength(); i < numItems; i++) {
335             Node JavaDoc node= list.item(i);
336             if (node.getNodeType() == Node.ELEMENT_NODE) {
337                 Element JavaDoc element= (Element JavaDoc) node;
338                 if (!element.getNodeName().equals(MEMORY_RENDERING_TAG)) {
339                     DebugUIPlugin.logErrorMessage("Invalid XML element encountered while loading memory rendering."); //$NON-NLS-1$
340
continue;
341                 }
342                 String JavaDoc memoryBlockHashCode= element.getAttribute(MEMORY_BLOCK);
343                 String JavaDoc renderingId = element.getAttribute(RENDERING_ID);
344                 
345                 IMemoryBlock[] memoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks();
346                 IMemoryBlock memoryBlock = null;
347                 for (int j=0; j<memoryBlocks.length; j++)
348                 {
349                     if (Integer.toString(memoryBlocks[j].hashCode()).equals(memoryBlockHashCode))
350                         memoryBlock = memoryBlocks[j];
351                 }
352                 
353                 if (memoryBlock != null)
354                 {
355                     IMemoryRenderingType[] types = DebugUITools.getMemoryRenderingManager().getRenderingTypes(memoryBlock);
356                     IMemoryRenderingType type = null;
357                     for (int k=0; k<types.length; k++)
358                     {
359                         if (types[k].getId().equals(renderingId))
360                             type = types[k];
361                     }
362                 
363                     // if memory block is not found, the rendering is no longer valid
364
// simply ignore the rendering
365
if (type != null)
366                     {
367                         try {
368         
369                             IMemoryRendering rendering = type.createRendering();
370                             if (rendering != null)
371                             {
372                                 rendering.init(fViewPane, memoryBlock);
373                                 if (!fRenderings.contains(rendering))
374                                 {
375                                     fRenderings.add(rendering);
376                                     renderingsAdded= true;
377                                 }
378                             }
379                         } catch (CoreException e1) {
380                             DebugUIPlugin.log(e1);
381                         }
382                     }
383                 }
384             }
385         }
386         if (renderingsAdded) {
387             DebugPlugin.getDefault().addDebugEventListener(this);
388         }
389     }
390     
391     /**
392      * @return secondary id, or null if not available
393      */

394     private String JavaDoc getViewSiteSecondaryId()
395     {
396         IMemoryRenderingSite renderingSite = fViewPane.getMemoryRenderingSite();
397         IWorkbenchPartSite ps = renderingSite.getSite();
398         if (ps instanceof IViewSite)
399         {
400             IViewSite vs = (IViewSite)ps;
401             String JavaDoc secondaryId = vs.getSecondaryId();
402             return secondaryId;
403         }
404         return null;
405     }
406 }
407
Popular Tags