1 11 package org.eclipse.debug.internal.ui.views.memory; 12 13 import java.io.IOException ; 14 import java.util.ArrayList ; 15 16 import javax.xml.parsers.ParserConfigurationException ; 17 import javax.xml.transform.TransformerException ; 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 ; 36 import org.w3c.dom.Element ; 37 import org.w3c.dom.Node ; 38 import org.w3c.dom.NodeList ; 39 40 50 public class ViewPaneRenderingMgr implements IDebugEventSetListener{ 51 52 private ArrayList fRenderings = new ArrayList (); 53 private IMemoryRenderingContainer fViewPane; 54 55 private static final String RENDERINGS_TAG = "persistedMemoryRenderings"; private static final String MEMORY_RENDERING_TAG = "memoryRendering"; private static final String MEMORY_BLOCK = "memoryBlock"; private static final String RENDERING_ID = "renderingId"; 60 public ViewPaneRenderingMgr(IMemoryRenderingContainer viewPane) 61 { 62 fViewPane = viewPane; 63 loadPersistedRenderings(getPrefId()); 64 } 65 66 public void removeMemoryBlockRendering(IMemoryBlock mem, String 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 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 if (fRenderings.contains(rendering)) 92 return; 93 94 fRenderings.add(rendering); 95 96 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 if (fRenderings.size() == 0) 117 { 118 DebugPlugin.getDefault().removeDebugEventListener(this); 119 } 120 121 storeRenderings(); 122 } 123 124 public IMemoryRendering[] getRenderings(IMemoryBlock mem, String renderingId) 125 { 126 if (renderingId == null) 127 { 128 return getRenderingsFromMemoryBlock(mem); 129 } 130 131 ArrayList ret = new ArrayList (); 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 ret = new ArrayList (); 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 ret = new ArrayList (); 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 obj = event.getSource(); 200 IDebugTarget dt = null; 201 202 if (event.getKind() == DebugEvent.TERMINATE) 203 { 204 if (obj instanceof IDebugTarget) 207 { 208 dt = ((IDebugTarget)obj); 209 210 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 fRenderings.clear(); 226 227 String secondaryId = getViewSiteSecondaryId(); 228 if (secondaryId != null) 229 { 230 String prefid = getPrefId(); 232 Preferences prefs = DebugUIPlugin.getDefault().getPluginPreferences(); 233 prefs.setToDefault(prefid); 234 } 235 236 DebugPlugin.getDefault().removeDebugEventListener(this); 237 } 238 239 243 private void storeRenderings() 244 { 245 Preferences prefs = DebugUIPlugin.getDefault().getPluginPreferences(); 246 String renderingsStr= ""; try { 248 renderingsStr= getRenderingsAsXML(); 249 } catch (IOException e) { 250 DebugUIPlugin.log(e); 251 } catch (ParserConfigurationException e) { 252 DebugUIPlugin.log(e); 253 } catch (TransformerException e) { 254 DebugUIPlugin.log(e); 255 } 256 257 String prefid = getPrefId(); 258 259 if (renderingsStr != null) 260 prefs.setValue(prefid, renderingsStr); 261 else 262 prefs.setToDefault(prefid); 263 } 264 265 private String getPrefId() { 266 StringBuffer id = new StringBuffer (); 269 IMemoryRenderingSite renderingSite = fViewPane.getMemoryRenderingSite(); 270 IWorkbenchPartSite ps = renderingSite.getSite(); 271 if (ps instanceof IViewSite) 272 { 273 IViewSite vs = (IViewSite)ps; 274 String secondaryId = vs.getSecondaryId(); 275 if (secondaryId != null) 276 { 277 id.append(secondaryId); 278 id.append(":"); } 280 281 } 282 id.append(fViewPane.getId()); 283 String prefId = id.toString(); 284 return prefId; 285 } 286 287 294 private String getRenderingsAsXML() throws IOException , ParserConfigurationException , TransformerException { 295 IMemoryRendering[] renderings= (IMemoryRendering[])fRenderings.toArray(new IMemoryRendering[fRenderings.size()]); 296 297 if (renderings.length == 0) 298 return null; 299 300 Document document= LaunchManager.getDocument(); 301 Element 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 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 316 private void loadPersistedRenderings(String prefId) { 317 String renderingsStr= DebugUIPlugin.getDefault().getPluginPreferences().getString(prefId); 318 if (renderingsStr.length() == 0) { 319 return; 320 } 321 Element root; 322 try { 323 root = DebugPlugin.parseDocument(renderingsStr); 324 } catch (CoreException e) { 325 DebugUIPlugin.logErrorMessage("An exception occurred while loading memory renderings."); return; 327 } 328 if (!root.getNodeName().equals(RENDERINGS_TAG)) { 329 DebugUIPlugin.logErrorMessage("Invalid format encountered while loading memory renderings."); return; 331 } 332 NodeList list= root.getChildNodes(); 333 boolean renderingsAdded= false; 334 for (int i= 0, numItems= list.getLength(); i < numItems; i++) { 335 Node node= list.item(i); 336 if (node.getNodeType() == Node.ELEMENT_NODE) { 337 Element element= (Element ) node; 338 if (!element.getNodeName().equals(MEMORY_RENDERING_TAG)) { 339 DebugUIPlugin.logErrorMessage("Invalid XML element encountered while loading memory rendering."); continue; 341 } 342 String memoryBlockHashCode= element.getAttribute(MEMORY_BLOCK); 343 String 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 (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 394 private String 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 secondaryId = vs.getSecondaryId(); 402 return secondaryId; 403 } 404 return null; 405 } 406 } 407 | Popular Tags |