KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 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
12 package org.eclipse.debug.internal.ui.views.memory;
13
14 import java.math.BigInteger JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
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 /**
54  * Action for adding memory block.
55  *
56  * @since 3.0
57  */

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     /**
71      * @param addDefaultRenderings - specify if the action should add
72      * default renderings for the new memory block when it is run
73      */

74     AddMemoryBlockAction(IMemoryRenderingSite site, boolean addDefaultRenderings)
75     {
76         initialize(site);
77         fAddDefaultRenderings = addDefaultRenderings;
78     }
79     
80     /**
81      *
82      */

83     private void initialize(IMemoryRenderingSite site) {
84         setText(DebugUIMessages.AddMemoryBlockAction_title);
85         doInitialization(site);
86     }
87
88
89     public AddMemoryBlockAction(String JavaDoc text, int style, IMemoryRenderingSite site)
90     {
91         super(text, style);
92         doInitialization(site);
93     }
94     
95     /**
96      *
97      */

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"); //$NON-NLS-1$
105

106         // listen for context changed
107
DebugUITools.getDebugContextManager().getContextService(site.getSite().getWorkbenchWindow()).addDebugContextListener(this);
108         
109         // get current context
110
fCurrentContext = DebugUITools.getDebugContext();
111         
112         // set up enablement based on current selection
113
updateAction(fCurrentContext);
114         
115         DebugPlugin.getDefault().addDebugEventListener(this);
116     }
117
118     /* (non-Javadoc)
119      * @see org.eclipse.jface.action.IAction#run()
120      */

121     public void run() {
122         boolean exit = false;
123         String JavaDoc prefillExp = null;
124         String JavaDoc prefillLength = null;
125         while (!exit)
126         {
127             exit = true;
128             
129             Object JavaDoc 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             // create dialog to ask for expression/address to block
138
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             // get expression entered in dialog
146
String JavaDoc input = dialog.getExpression();
147             
148             // remember expression and length
149
prefillExp = input;
150             prefillLength = dialog.getLength();
151             
152             ArrayList JavaDoc expressions = new ArrayList JavaDoc();
153             
154             if (input.length() == 0)
155             {
156                 expressions.add(""); //$NON-NLS-1$
157
}
158             else
159             {
160                 StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(input, ","); //$NON-NLS-1$
161
while (tokenizer.hasMoreTokens())
162                 {
163                     expressions.add(tokenizer.nextToken());
164                 }
165             }
166             final String JavaDoc[] expressionsArray = (String JavaDoc[])expressions.toArray(new String JavaDoc[expressions.size()]);
167             exit = true;
168             
169             final boolean finalExit = exit;
170             final Object JavaDoc finalElement = elem;
171             final IMemoryBlockRetrieval finalRetrieval = retrieval;
172             final MonitorMemoryBlockDialog finalDialog = dialog;
173             Job job = new Job("Add Memory Block") { //$NON-NLS-1$
174
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     /**
184      * @param exit
185      * @param elem
186      * @param standardMemRetrieval
187      * @param dialog
188      * @param expressionsArray
189      * @return
190      */

191     private boolean addMemoryBlocks(boolean exit, Object JavaDoc elem, IMemoryBlockRetrieval standardMemRetrieval, MonitorMemoryBlockDialog dialog, final String JavaDoc[] expressionsArray) {
192         for (int i=0; i<expressionsArray.length; i++)
193         {
194             String JavaDoc expression = expressionsArray[i].trim();
195             try {
196                 if (standardMemRetrieval instanceof IMemoryBlockRetrievalExtension)
197                 {
198                     // if the debug session supports IMemoryBlockExtensionRetrieval
199
IMemoryBlockRetrievalExtension memRetrieval = (IMemoryBlockRetrievalExtension)standardMemRetrieval;
200                     
201                     // get extended memory block with the expression entered
202
IMemoryBlockExtension memBlock = memRetrieval.getExtendedMemoryBlock(expression, elem);
203                     
204                     // add block to memory block manager
205
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                         // open error if it failed to retrieve a memory block
218
MemoryViewUtil.openError(DebugUIMessages.AddMemoryBlockAction_title, DebugUIMessages.AddMemoryBlockAction_noMemoryBlock, null);
219                         exit = false;
220                     }
221                 }
222                 else
223                 {
224                     // if the debug session does not support IMemoryBlockExtensionRetrieval
225
expression = expression.toUpperCase();
226                     String JavaDoc hexPrefix = "0X"; //$NON-NLS-1$
227
if (expression.startsWith(hexPrefix))
228                     {
229                         expression = expression.substring(hexPrefix.length());
230                     }
231                     
232                     // convert the expression to an address
233
BigInteger JavaDoc address = new BigInteger JavaDoc(expression, 16);
234
235                     long longAddress = address.longValue();
236
237                     // get the length of memory to block
238
String JavaDoc strLength = dialog.getLength();
239                     
240                     long length = Long.parseLong(strLength);
241                     
242                     // must monitor at least one line
243
if (length <= 0)
244                     {
245                         String JavaDoc message = DebugUIMessages.AddMemoryBlockAction_failed + "\n" + DebugUIMessages.AddMemoryBlockAction_input_invalid; //$NON-NLS-1$
246
MemoryViewUtil.openError(DebugUIMessages.AddMemoryBlockAction_title, message, null);
247                         exit = false;
248                         continue;
249                     }
250                     
251                     // get standard memory block
252
IMemoryBlock memBlock = standardMemRetrieval.getMemoryBlock(longAddress, length);
253                     
254                     // make sure the memory block returned is not an instance of IMemoryBlockExtension
255
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); //$NON-NLS-1$
259
DebugUIPlugin.log(status);
260                     }
261                     
262                     if (memBlock != null)
263                     {
264                         // add memory block to memory block manager
265
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                         // otherwise open up an error doalog
275
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 JavaDoc e2)
284             {
285                 String JavaDoc message = DebugUIMessages.AddMemoryBlockAction_failed + "\n" + DebugUIMessages.AddMemoryBlockAction_input_invalid; //$NON-NLS-1$
286
MemoryViewUtil.openError(DebugUIMessages.AddMemoryBlockAction_title, message, null);
287                 exit = false;
288             }
289         }
290         return exit;
291     }
292
293
294     /* (non-Javadoc)
295      * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[])
296      */

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         // update action enablement based on debug event
305
Object JavaDoc 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             // disable action if the debug target is terminated.
322
if (srcDT == selectionDT)
323             {
324                 setEnabled(false);
325             }
326         }
327     }
328     
329     /**
330      * Return the last memory block added to memory block manager via this action.
331      * @return Returns the fLastMemoryBlock.
332      */

333     public IMemoryBlock getLastMemoryBlock() {
334         return fLastMemoryBlock;
335     }
336     
337     protected void dispose() {
338         
339         // remove listeners
340
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         // create primary rendering
350
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     /**
382      * @param memoryBlock
383      * @param primaryType
384      * @throws CoreException
385      */

386     private void createRenderingInContainer(IMemoryBlock memoryBlock, IMemoryRenderingType primaryType, String JavaDoc 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 JavaDoc debugContext)
401     {
402         Job job = new Job("Update Add Memory Block Action") { //$NON-NLS-1$
403
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     /* (non-Javadoc)
412      * @see org.eclipse.debug.internal.ui.contexts.provisional.IDebugContextListener#contextEvent(org.eclipse.debug.internal.ui.contexts.provisional.DebugContextEvent)
413      */

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