1 11 12 package org.eclipse.debug.internal.ui.views.memory; 13 14 import java.math.BigInteger ; 15 import java.util.ArrayList ; 16 import java.util.StringTokenizer ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IAdaptable; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.Status; 23 import org.eclipse.core.runtime.jobs.Job; 24 import org.eclipse.debug.core.DebugEvent; 25 import org.eclipse.debug.core.DebugException; 26 import org.eclipse.debug.core.DebugPlugin; 27 import org.eclipse.debug.core.IDebugEventSetListener; 28 import org.eclipse.debug.core.model.IDebugElement; 29 import org.eclipse.debug.core.model.IDebugTarget; 30 import org.eclipse.debug.core.model.IMemoryBlock; 31 import org.eclipse.debug.core.model.IMemoryBlockExtension; 32 import org.eclipse.debug.core.model.IMemoryBlockRetrieval; 33 import org.eclipse.debug.core.model.IMemoryBlockRetrievalExtension; 34 import org.eclipse.debug.core.model.ITerminate; 35 import org.eclipse.debug.internal.ui.DebugPluginImages; 36 import org.eclipse.debug.internal.ui.DebugUIMessages; 37 import org.eclipse.debug.internal.ui.DebugUIPlugin; 38 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants; 39 import org.eclipse.debug.ui.DebugUITools; 40 import org.eclipse.debug.ui.IDebugUIConstants; 41 import org.eclipse.debug.ui.contexts.DebugContextEvent; 42 import org.eclipse.debug.ui.contexts.IDebugContextListener; 43 import org.eclipse.debug.ui.memory.IMemoryRendering; 44 import org.eclipse.debug.ui.memory.IMemoryRenderingContainer; 45 import org.eclipse.debug.ui.memory.IMemoryRenderingSite; 46 import org.eclipse.debug.ui.memory.IMemoryRenderingType; 47 import org.eclipse.jface.action.Action; 48 import org.eclipse.jface.window.Window; 49 import org.eclipse.swt.widgets.Shell; 50 import org.eclipse.ui.PlatformUI; 51 52 53 58 public class AddMemoryBlockAction extends Action implements IDebugContextListener, IDebugEventSetListener{ 59 60 protected IAdaptable fCurrentContext = null; 61 protected IMemoryBlock fLastMemoryBlock; 62 private boolean fAddDefaultRenderings = true; 63 protected IMemoryRenderingSite fSite; 64 65 public AddMemoryBlockAction(IMemoryRenderingSite site) 66 { 67 initialize(site); 68 } 69 70 74 AddMemoryBlockAction(IMemoryRenderingSite site, boolean addDefaultRenderings) 75 { 76 initialize(site); 77 fAddDefaultRenderings = addDefaultRenderings; 78 } 79 80 83 private void initialize(IMemoryRenderingSite site) { 84 setText(DebugUIMessages.AddMemoryBlockAction_title); 85 doInitialization(site); 86 } 87 88 89 public AddMemoryBlockAction(String text, int style, IMemoryRenderingSite site) 90 { 91 super(text, style); 92 doInitialization(site); 93 } 94 95 98 private void doInitialization(IMemoryRenderingSite site) { 99 fSite = site; 100 setToolTipText(DebugUIMessages.AddMemoryBlockAction_tooltip); 101 setImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_ELCL_MONITOR_EXPRESSION)); 102 setHoverImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_LCL_MONITOR_EXPRESSION)); 103 setDisabledImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_DLCL_MONITOR_EXPRESSION)); 104 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IDebugUIConstants.PLUGIN_ID + ".addMemoryMonitorAction_context"); 106 DebugUITools.getDebugContextManager().getContextService(site.getSite().getWorkbenchWindow()).addDebugContextListener(this); 108 109 fCurrentContext = DebugUITools.getDebugContext(); 111 112 updateAction(fCurrentContext); 114 115 DebugPlugin.getDefault().addDebugEventListener(this); 116 } 117 118 121 public void run() { 122 boolean exit = false; 123 String prefillExp = null; 124 String prefillLength = null; 125 while (!exit) 126 { 127 exit = true; 128 129 Object elem = DebugUITools.getDebugContext(); 130 131 IMemoryBlockRetrieval retrieval = MemoryViewUtil.getMemoryBlockRetrieval(elem); 132 133 if (retrieval == null) 134 return; 135 136 Shell shell= DebugUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell(); 137 MonitorMemoryBlockDialog dialog = new MonitorMemoryBlockDialog(shell, retrieval, prefillExp, prefillLength); 139 dialog.open(); 140 int returnCode = dialog.getReturnCode(); 141 if (returnCode == Window.CANCEL) 142 { 143 return; 144 } 145 String input = dialog.getExpression(); 147 148 prefillExp = input; 150 prefillLength = dialog.getLength(); 151 152 ArrayList expressions = new ArrayList (); 153 154 if (input.length() == 0) 155 { 156 expressions.add(""); } 158 else 159 { 160 StringTokenizer tokenizer = new StringTokenizer (input, ","); while (tokenizer.hasMoreTokens()) 162 { 163 expressions.add(tokenizer.nextToken()); 164 } 165 } 166 final String [] expressionsArray = (String [])expressions.toArray(new String [expressions.size()]); 167 exit = true; 168 169 final boolean finalExit = exit; 170 final Object finalElement = elem; 171 final IMemoryBlockRetrieval finalRetrieval = retrieval; 172 final MonitorMemoryBlockDialog finalDialog = dialog; 173 Job job = new Job("Add Memory Block") { protected IStatus run(IProgressMonitor monitor) { 175 addMemoryBlocks(finalExit, finalElement, finalRetrieval, finalDialog, expressionsArray); 176 return Status.OK_STATUS; 177 }}; 178 job.setSystem(true); 179 job.schedule(); 180 } 181 } 182 183 191 private boolean addMemoryBlocks(boolean exit, Object elem, IMemoryBlockRetrieval standardMemRetrieval, MonitorMemoryBlockDialog dialog, final String [] expressionsArray) { 192 for (int i=0; i<expressionsArray.length; i++) 193 { 194 String expression = expressionsArray[i].trim(); 195 try { 196 if (standardMemRetrieval instanceof IMemoryBlockRetrievalExtension) 197 { 198 IMemoryBlockRetrievalExtension memRetrieval = (IMemoryBlockRetrievalExtension)standardMemRetrieval; 200 201 IMemoryBlockExtension memBlock = memRetrieval.getExtendedMemoryBlock(expression, elem); 203 204 if (memBlock != null) 206 { 207 fLastMemoryBlock = memBlock; 208 209 IMemoryBlock[] memArray = new IMemoryBlock[]{memBlock}; 210 211 MemoryViewUtil.getMemoryBlockManager().addMemoryBlocks(memArray); 212 if (fAddDefaultRenderings) 213 addDefaultRenderings(memBlock); 214 } 215 else 216 { 217 MemoryViewUtil.openError(DebugUIMessages.AddMemoryBlockAction_title, DebugUIMessages.AddMemoryBlockAction_noMemoryBlock, null); 219 exit = false; 220 } 221 } 222 else 223 { 224 expression = expression.toUpperCase(); 226 String hexPrefix = "0X"; if (expression.startsWith(hexPrefix)) 228 { 229 expression = expression.substring(hexPrefix.length()); 230 } 231 232 BigInteger address = new BigInteger (expression, 16); 234 235 long longAddress = address.longValue(); 236 237 String strLength = dialog.getLength(); 239 240 long length = Long.parseLong(strLength); 241 242 if (length <= 0) 244 { 245 String message = DebugUIMessages.AddMemoryBlockAction_failed + "\n" + DebugUIMessages.AddMemoryBlockAction_input_invalid; MemoryViewUtil.openError(DebugUIMessages.AddMemoryBlockAction_title, message, null); 247 exit = false; 248 continue; 249 } 250 251 IMemoryBlock memBlock = standardMemRetrieval.getMemoryBlock(longAddress, length); 253 254 if (memBlock instanceof IMemoryBlockExtension) 256 { 257 Status status = new Status(IStatus.WARNING, DebugUIPlugin.getUniqueIdentifier(), 0, 258 "IMemoryBlockRetrieval returns IMemoryBlockExtension. This may result in unexpected behavior.", null); DebugUIPlugin.log(status); 260 } 261 262 if (memBlock != null) 263 { 264 fLastMemoryBlock = memBlock; 266 IMemoryBlock[] memArray = new IMemoryBlock[]{memBlock}; 267 268 MemoryViewUtil.getMemoryBlockManager().addMemoryBlocks(memArray); 269 if (fAddDefaultRenderings) 270 addDefaultRenderings(memBlock); 271 } 272 else 273 { 274 MemoryViewUtil.openError(DebugUIMessages.AddMemoryBlockAction_title, DebugUIMessages.AddMemoryBlockAction_noMemoryBlock, null); 276 exit = false; 277 } 278 } 279 } catch (DebugException e1) { 280 MemoryViewUtil.openError(DebugUIMessages.AddMemoryBlockAction_title, DebugUIMessages.AddMemoryBlockAction_failed, e1); 281 exit = false; 282 } 283 catch(NumberFormatException e2) 284 { 285 String message = DebugUIMessages.AddMemoryBlockAction_failed + "\n" + DebugUIMessages.AddMemoryBlockAction_input_invalid; MemoryViewUtil.openError(DebugUIMessages.AddMemoryBlockAction_title, message, null); 287 exit = false; 288 } 289 } 290 return exit; 291 } 292 293 294 297 public void handleDebugEvents(DebugEvent[] events) { 298 for (int i=0; i < events.length; i++) 299 handleDebugEvent(events[i]); 300 } 301 302 private void handleDebugEvent(DebugEvent event) 303 { 304 Object src = event.getSource(); 306 IDebugTarget srcDT = null; 307 IDebugTarget selectionDT = null; 308 309 if (event.getKind() == DebugEvent.TERMINATE) 310 { 311 if (src instanceof ITerminate && src instanceof IDebugElement) 312 { 313 srcDT = ((IDebugElement)src).getDebugTarget(); 314 } 315 316 if (fCurrentContext instanceof IDebugElement) 317 { 318 selectionDT = ((IDebugElement)fCurrentContext).getDebugTarget(); 319 } 320 321 if (srcDT == selectionDT) 323 { 324 setEnabled(false); 325 } 326 } 327 } 328 329 333 public IMemoryBlock getLastMemoryBlock() { 334 return fLastMemoryBlock; 335 } 336 337 protected void dispose() { 338 339 DebugPlugin.getDefault().removeDebugEventListener(this); 341 DebugUITools.getDebugContextManager().getContextService(fSite.getSite().getWorkbenchWindow()).removeDebugContextListener(this); 342 } 343 344 private void addDefaultRenderings(IMemoryBlock memoryBlock) 345 { 346 IMemoryRenderingType primaryType = DebugUITools.getMemoryRenderingManager().getPrimaryRenderingType(memoryBlock); 347 IMemoryRenderingType renderingTypes[] = DebugUITools.getMemoryRenderingManager().getDefaultRenderingTypes(memoryBlock); 348 349 try { 351 if (primaryType != null) 352 { 353 createRenderingInContainer(memoryBlock, primaryType, IDebugUIConstants.ID_RENDERING_VIEW_PANE_1); 354 } 355 else if (renderingTypes.length > 0) 356 { 357 primaryType = renderingTypes[0]; 358 createRenderingInContainer(memoryBlock, renderingTypes[0], IDebugUIConstants.ID_RENDERING_VIEW_PANE_1); 359 } 360 } catch (CoreException e1) { 361 DebugUIPlugin.log(e1); 362 } 363 364 for (int i = 0; i<renderingTypes.length; i++) 365 { 366 try { 367 boolean create = true; 368 if (primaryType != null) 369 { 370 if (primaryType.getId().equals(renderingTypes[i].getId())) 371 create = false; 372 } 373 if (create) 374 createRenderingInContainer(memoryBlock, renderingTypes[i], IDebugUIConstants.ID_RENDERING_VIEW_PANE_2); 375 } catch (CoreException e) { 376 DebugUIPlugin.log(e); 377 } 378 } 379 } 380 381 386 private void createRenderingInContainer(IMemoryBlock memoryBlock, IMemoryRenderingType primaryType, String paneId) throws CoreException { 387 IMemoryRendering rendering = primaryType.createRendering(); 388 IMemoryRenderingContainer container = fSite.getContainer(paneId); 389 rendering.init(container, memoryBlock); 390 container.addMemoryRendering(rendering); 391 } 392 393 protected MemoryView getMemoryView() 394 { 395 if (fSite instanceof MemoryView) 396 return (MemoryView)fSite; 397 return null; 398 } 399 400 protected void updateAction(final Object debugContext) 401 { 402 Job job = new Job("Update Add Memory Block Action") { protected IStatus run(IProgressMonitor monitor) { 404 setEnabled(MemoryViewUtil.isValidContext(debugContext)); 405 return Status.OK_STATUS; 406 }}; 407 job.setSystem(true); 408 job.schedule(); 409 } 410 411 414 public void debugContextChanged(DebugContextEvent event) { 415 if ((event.getFlags() & DebugContextEvent.ACTIVATED) > 0) { 416 IAdaptable context = DebugUITools.getDebugContext(); 417 updateAction(context); 418 fCurrentContext = context; 419 } 420 } 421 } 422 | Popular Tags |