KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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 org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.NullProgressMonitor;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.core.runtime.jobs.Job;
20 import org.eclipse.debug.core.DebugPlugin;
21 import org.eclipse.debug.core.IMemoryBlockListener;
22 import org.eclipse.debug.core.model.IDebugElement;
23 import org.eclipse.debug.core.model.IMemoryBlock;
24 import org.eclipse.debug.core.model.IMemoryBlockExtension;
25 import org.eclipse.debug.core.model.IMemoryBlockRetrieval;
26 import org.eclipse.debug.internal.ui.DebugUIMessages;
27 import org.eclipse.debug.ui.DebugUITools;
28 import org.eclipse.debug.ui.contexts.DebugContextEvent;
29 import org.eclipse.debug.ui.contexts.IDebugContextListener;
30 import org.eclipse.debug.ui.memory.IMemoryRendering;
31 import org.eclipse.jface.action.Action;
32 import org.eclipse.jface.action.ActionContributionItem;
33 import org.eclipse.jface.action.IAction;
34 import org.eclipse.jface.action.IMenuCreator;
35 import org.eclipse.jface.viewers.ILabelDecorator;
36 import org.eclipse.jface.viewers.ISelection;
37 import org.eclipse.jface.viewers.IStructuredSelection;
38 import org.eclipse.jface.viewers.StructuredSelection;
39 import org.eclipse.jface.viewers.StructuredViewer;
40 import org.eclipse.swt.widgets.Control;
41 import org.eclipse.swt.widgets.Event;
42 import org.eclipse.swt.widgets.Menu;
43 import org.eclipse.ui.IActionDelegate2;
44 import org.eclipse.ui.IViewActionDelegate;
45 import org.eclipse.ui.IViewPart;
46 import org.eclipse.ui.progress.UIJob;
47 import org.eclipse.ui.progress.WorkbenchJob;
48
49 /**
50  * The switch memory block action, used
51  */

52 public class SwitchMemoryBlockAction extends Action implements IViewActionDelegate, IActionDelegate2 {
53
54     /**
55      * A job that updates the enablement of the of the backing action delegate in the UI thread
56      *
57      * @since 3.3.0
58      */

59     class UpdateActionEnablementJob extends UIJob {
60
61         /**
62          * Constructor
63          */

64         public UpdateActionEnablementJob() {
65             super("Update Action Enablement"); //$NON-NLS-1$
66
setSystem(true);
67         }
68
69         /* (non-Javadoc)
70          * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
71          */

72         public IStatus runInUIThread(IProgressMonitor monitor) {
73             if (fAction != null) {
74                 IAdaptable context = DebugUITools.getDebugContext();
75                 if (context != null) {
76                     IMemoryBlockRetrieval retrieval = null;
77                     if (context.getAdapter(IMemoryBlockRetrieval.class) != null) {
78                         retrieval = (IMemoryBlockRetrieval)context.getAdapter(IMemoryBlockRetrieval.class);
79                     }
80                     if (retrieval == null) {
81                         if(context instanceof IMemoryBlockRetrieval) {
82                             retrieval = (IMemoryBlockRetrieval)context;
83                         }
84                         else if(context instanceof IDebugElement) {
85                             retrieval = ((IDebugElement)context).getDebugTarget();
86                         }
87                     }
88                     if (retrieval != null) {
89                         IMemoryBlock[] memoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(retrieval);
90                         fAction.setEnabled(memoryBlocks.length > 0);
91                         return Status.OK_STATUS;
92                     }
93                     else if (getViewer() != null) {
94                         Object JavaDoc input = getViewer().getInput();
95                         if (input instanceof IMemoryBlockRetrieval) {
96                             retrieval = (IMemoryBlockRetrieval)input;
97                             IMemoryBlock[] memoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(retrieval);
98                             fAction.setEnabled(memoryBlocks.length > 0);
99                             return Status.OK_STATUS;
100                         }
101                     }
102                 }
103                 fAction.setEnabled(false);
104             }
105             return Status.CANCEL_STATUS;
106         }
107     }
108     
109     private IViewPart fView;
110     private MenuCreator fMenuCreator;
111     private IAction fAction;
112     private UpdateActionEnablementJob fUpdateJob = new UpdateActionEnablementJob();
113     
114     /**
115      * Memoryblock listener to update action delegate enablement
116      */

117     private IMemoryBlockListener fListener = new IMemoryBlockListener() {
118         public void memoryBlocksAdded(IMemoryBlock[] memory) {
119             if (fAction != null) {
120                 fUpdateJob.schedule();
121             }
122         }
123
124         public void memoryBlocksRemoved(IMemoryBlock[] memory) {
125             if (fAction != null) {
126                 fUpdateJob.schedule();
127             }
128         }
129     };
130     
131     /**
132      * Listens for debug context changes and updates action delegate enablement
133      */

134     private IDebugContextListener fDebugContextListener = new IDebugContextListener() {
135         public void debugContextChanged(DebugContextEvent event) {
136             if (fAction != null) {
137                 fUpdateJob.schedule();
138             }
139         }
140     };
141     
142     /**
143      * Switch tab folder for fMemoryBlock to the top in Memory Rendering View
144      */

145     class SwitchToAction extends Action {
146         private IMemoryBlock fMemoryblock;
147         
148         /* (non-Javadoc)
149          * @see org.eclipse.jface.action.IAction#run()
150          */

151         public void run() {
152             if (fView == null) {
153                 return;
154             }
155             // tell the view to switch memory block
156
fView.getSite().getSelectionProvider().setSelection(new StructuredSelection(fMemoryblock));
157         }
158
159         public SwitchToAction(final IMemoryBlock memBlk, boolean buildLabel) {
160             super();
161             if (buildLabel) {
162                 setText(DebugUIMessages.SwitchMemoryBlockAction_4);
163                 Job job = new Job("SwtichToAction"){ //$NON-NLS-1$
164
protected IStatus run(IProgressMonitor monitor) {
165                         getLabels(memBlk);
166                         return Status.OK_STATUS;
167                     }};
168                 job.setSystem(true);
169                 job.schedule();
170             }
171             fMemoryblock = memBlk;
172         }
173         
174         public SwitchToAction(final IMemoryBlock memBlk, String JavaDoc label) {
175             super(label);
176             fMemoryblock = memBlk;
177         }
178         
179         private void getLabels(final IMemoryBlock memBlk) {
180             StringBuffer JavaDoc text = new StringBuffer JavaDoc(""); //$NON-NLS-1$
181
String JavaDoc label = new String JavaDoc(""); //$NON-NLS-1$
182
if (memBlk instanceof IMemoryBlockExtension) {
183                 String JavaDoc expression = ((IMemoryBlockExtension)memBlk).getExpression();
184                 if (expression == null) {
185                     expression = DebugUIMessages.SwitchMemoryBlockAction_0;
186                 }
187                 text.append(expression);
188             }
189             else {
190                 long address = memBlk.getStartAddress();
191                 text.append(Long.toHexString(address));
192             }
193             
194             label = text.toString();
195             label = decorateLabel(memBlk, label);
196
197             final String JavaDoc finalLabel = label;
198             WorkbenchJob job = new WorkbenchJob("SwtichToAction Update Label") { //$NON-NLS-1$
199
public IStatus runInUIThread(IProgressMonitor monitor) {
200                     SwitchToAction.super.setText(finalLabel);
201                     return Status.OK_STATUS;
202                 }};
203             job.setSystem(true);
204             job.schedule();
205         }
206     }
207     
208     /**
209      * Menu creator for the action
210      */

211     class MenuCreator implements IMenuCreator {
212         Menu dropdown;
213
214         /* (non-Javadoc)
215          * @see org.eclipse.jface.action.IMenuCreator#dispose()
216          */

217         public void dispose() {
218             if (dropdown != null)
219                 dropdown.dispose();
220         }
221
222         /* (non-Javadoc)
223          * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control)
224          */

225         public Menu getMenu(Control parent) {
226             if (dropdown != null) {
227                 dropdown.dispose();
228                 dropdown = null;
229             }
230             if (dropdown == null) {
231                 dropdown = new Menu(parent);
232
233                 // get all memory blocks from tree viewer
234
IMemoryBlock[] allMemoryBlocks = null;
235                 
236                 // get selection from memory view
237
IMemoryBlock memoryBlock = getCurrentMemoryBlock();
238             
239                 Object JavaDoc context = DebugUITools.getDebugContext();
240                 IMemoryBlockRetrieval retrieval = MemoryViewUtil.getMemoryBlockRetrieval(context);
241                 if (retrieval != null) {
242                     allMemoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(retrieval);
243                 }
244                 if (allMemoryBlocks != null) {
245                     for (int i=0; i<allMemoryBlocks.length; i++) {
246                         SwitchToAction action = new SwitchToAction(allMemoryBlocks[i], true);
247                         if (allMemoryBlocks[i] == memoryBlock) {
248                             action.setChecked(true);
249                         }
250                         ActionContributionItem item = new ActionContributionItem(action);
251                         item.fill(dropdown, -1);
252                         item.getAction().setChecked(true);
253                     }
254                 }
255             }
256             return dropdown;
257         }
258
259         /* (non-Javadoc)
260          * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
261          */

262         public Menu getMenu(Menu parent) {
263             return null;
264         }
265         
266     }
267     
268     /* (non-Javadoc)
269      * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
270      */

271     public void init(IViewPart view) {
272         fView = view;
273         DebugUITools.getDebugContextManager().getContextService(fView.getViewSite().getWorkbenchWindow()).addDebugContextListener(fDebugContextListener);
274         DebugPlugin.getDefault().getMemoryBlockManager().addListener(fListener);
275         fUpdateJob.runInUIThread(new NullProgressMonitor());
276     }
277     
278     /**
279      * Returns the current memory blocks tree viewer, or <code>null</code>
280      * @return the memory blocks tree viewer or <code>null</code>
281      */

282     private StructuredViewer getViewer() {
283         if (fView instanceof MemoryView) {
284             MemoryView memView = (MemoryView)fView;
285             IMemoryViewPane pane = memView.getViewPane(MemoryBlocksTreeViewPane.PANE_ID);
286             if (pane instanceof MemoryBlocksTreeViewPane) {
287                  StructuredViewer viewer = ((MemoryBlocksTreeViewPane)pane).getViewer();
288                 return viewer;
289             }
290         }
291         return null;
292     }
293     
294     /* (non-Javadoc)
295      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
296      */

297     public void run(IAction action) {
298         switchToNext();
299     }
300
301     /* (non-Javadoc)
302      * @see org.eclipse.jface.action.Action#run()
303      */

304     public void run() {
305         switchToNext();
306     }
307
308     private void switchToNext() {
309         IAdaptable context = DebugUITools.getDebugContext();
310         if (context instanceof IDebugElement) {
311             IDebugElement debugContext = (IDebugElement)context;
312             IMemoryBlockRetrieval retrieval = (IMemoryBlockRetrieval)debugContext.getAdapter(IMemoryBlockRetrieval.class);
313             if (retrieval == null) {
314                 retrieval = debugContext.getDebugTarget();
315             }
316             if (retrieval != null) {
317                 IMemoryBlock[] memoryBlocks = DebugPlugin.getDefault().getMemoryBlockManager().getMemoryBlocks(retrieval);
318                 doSwitchToNext(memoryBlocks);
319             }
320         }
321     }
322
323     /**
324      * @param memoryBlocks
325      */

326     private void doSwitchToNext(IMemoryBlock[] memoryBlocks) {
327         // only run if there is more than one memory block
328
if (memoryBlocks.length > 1) {
329             IMemoryBlock current = getCurrentMemoryBlock();
330             int next = 0;
331             if (current != null) {
332                 for (int i=0; i<memoryBlocks.length; i++) {
333                     if (memoryBlocks[i] == current) {
334                         next = i+1;
335                     }
336                 }
337             }
338             if (next > memoryBlocks.length-1) {
339                 next = 0;
340             }
341             SwitchToAction switchAction = new SwitchToAction(memoryBlocks[next], false);
342             switchAction.run();
343         }
344     }
345
346     /* (non-Javadoc)
347      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
348      */

349     public void selectionChanged(IAction action, ISelection selection) {}
350
351     /* (non-Javadoc)
352      * @see org.eclipse.ui.IActionDelegate2#init(org.eclipse.jface.action.IAction)
353      */

354     public void init(IAction action) {
355         fAction = action;
356         fUpdateJob.runInUIThread(new NullProgressMonitor());
357         fMenuCreator = new MenuCreator();
358         action.setMenuCreator(fMenuCreator);
359     }
360
361     /* (non-Javadoc)
362      * @see org.eclipse.ui.IActionDelegate2#dispose()
363      */

364     public void dispose() {
365         fAction = null;
366         DebugPlugin.getDefault().getMemoryBlockManager().removeListener(fListener);
367         DebugUITools.getDebugContextManager().getContextService(fView.getViewSite().getWorkbenchWindow()).removeDebugContextListener(fDebugContextListener);
368         if (fMenuCreator != null) {
369             fMenuCreator.dispose();
370         }
371     }
372
373     /* (non-Javadoc)
374      * @see org.eclipse.ui.IActionDelegate2#runWithEvent(org.eclipse.jface.action.IAction, org.eclipse.swt.widgets.Event)
375      */

376     public void runWithEvent(IAction action, Event event) {
377         switchToNext();
378     }
379
380     /**
381      * Returns the current memory block
382      * @return the current memory block or <code>null</code>
383      */

384     private IMemoryBlock getCurrentMemoryBlock() {
385         if (fView == null) {
386             return null;
387         }
388         ISelection memBlkSelection = fView.getSite().getSelectionProvider().getSelection();
389         IMemoryBlock memoryBlock = null;
390         
391         if (memBlkSelection != null) {
392             if (!memBlkSelection.isEmpty() && memBlkSelection instanceof IStructuredSelection) {
393                 Object JavaDoc obj = ((IStructuredSelection)memBlkSelection).getFirstElement();
394                 if (obj instanceof IMemoryBlock) {
395                     memoryBlock = (IMemoryBlock)obj;
396                 }
397                 else if (obj instanceof IMemoryRendering) {
398                     memoryBlock = ((IMemoryRendering)obj).getMemoryBlock();
399                 }
400             }
401         }
402         return memoryBlock;
403     }
404
405     /**
406      * Decorate the label for the specified <code>IMemoryBlock</code>
407      * @param memBlk
408      * @param label
409      * @return the decorated label for the specified <code>IMemoryBlock</code>
410      */

411     private String JavaDoc decorateLabel(final IMemoryBlock memBlk, String JavaDoc label) {
412         ILabelDecorator decorator = (ILabelDecorator)memBlk.getAdapter(ILabelDecorator.class);
413         if (decorator != null) {
414             label = decorator.decorateText(label, memBlk);
415         }
416         return label;
417     }
418
419 }
420
Popular Tags