KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > ui > actions > AddMemoryRenderingActionDelegate


1 /*******************************************************************************
2  * Copyright (c) 2005, 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
12 package org.eclipse.debug.ui.actions;
13
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IAdaptable;
16 import org.eclipse.debug.core.model.IDebugElement;
17 import org.eclipse.debug.core.model.IMemoryBlockRetrieval;
18 import org.eclipse.debug.internal.ui.DebugPluginImages;
19 import org.eclipse.debug.internal.ui.DebugUIPlugin;
20 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
21 import org.eclipse.debug.internal.ui.actions.ActionMessages;
22 import org.eclipse.debug.ui.DebugUITools;
23 import org.eclipse.debug.ui.contexts.DebugContextEvent;
24 import org.eclipse.debug.ui.contexts.IDebugContextListener;
25 import org.eclipse.debug.ui.memory.IMemoryRenderingType;
26 import org.eclipse.jface.action.Action;
27 import org.eclipse.jface.action.ActionContributionItem;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.action.IMenuCreator;
30 import org.eclipse.jface.viewers.ISelection;
31 import org.eclipse.jface.viewers.IStructuredSelection;
32 import org.eclipse.swt.events.MenuAdapter;
33 import org.eclipse.swt.events.MenuEvent;
34 import org.eclipse.swt.widgets.Control;
35 import org.eclipse.swt.widgets.Event;
36 import org.eclipse.swt.widgets.Menu;
37 import org.eclipse.swt.widgets.MenuItem;
38 import org.eclipse.ui.IActionDelegate2;
39 import org.eclipse.ui.IEditorActionDelegate;
40 import org.eclipse.ui.IEditorPart;
41 import org.eclipse.ui.IObjectActionDelegate;
42 import org.eclipse.ui.IViewActionDelegate;
43 import org.eclipse.ui.IViewPart;
44 import org.eclipse.ui.IWorkbenchPart;
45 import org.eclipse.ui.IWorkbenchWindow;
46
47 /**
48  * A cascade menu to add a memory rendering to the memory view. This action delegate can be
49  * contributed to a an editor, view or object via standard workbench extension points.
50  * The action works on the {@link IAddMemoryRenderingsTarget} adapter provided
51  * by the active debug context, creating a context menu to add applicable renderings
52  * to the memory view.
53  * <p>
54  * Clients may reference/contribute this class as an action delegate
55  * in plug-in XML. This class is not intended to be subclassed.
56  * </p>
57  * @since 3.2
58  */

59 public class AddMemoryRenderingActionDelegate extends Action implements IViewActionDelegate, IEditorActionDelegate, IObjectActionDelegate, IActionDelegate2{
60     
61     private IAction fAction;
62     private IWorkbenchPart fPart;
63     private ISelection fCurrentSelection;
64     private IAddMemoryRenderingsTarget fActionDelegate;
65     private IMenuCreator fMenuCreator;
66     private IAdaptable fDebugContext;
67     private IWorkbenchWindow fWindow;
68     private DebugContextListener fDebugContextListener = new DebugContextListener();
69     
70     private class AddMemoryRenderingAction extends Action
71     {
72         private IMemoryRenderingType fRenderingType; // type of rendering to add
73
AddMemoryRenderingAction(IMemoryRenderingType renderingType)
74         {
75             super(renderingType.getLabel());
76             fRenderingType = renderingType;
77         }
78
79         public void runWithEvent(Event event) {
80             if (fActionDelegate != null)
81             {
82                 try {
83                     fActionDelegate.addMemoryRenderings(fPart, fCurrentSelection, new IMemoryRenderingType[]{fRenderingType});
84                 } catch (CoreException e) {
85                     DebugUIPlugin.errorDialog(DebugUIPlugin.getShell(), ActionMessages.AddMemoryRenderingActionDelegate_0, ActionMessages.AddMemoryRenderingActionDelegate_1, e);
86                 }
87             }
88         }
89     }
90     
91     private class AddMemoryRenderingMenuCreator implements IMenuCreator
92     {
93
94         public void dispose() {
95             
96         }
97
98         public Menu getMenu(Control parent) {
99             return null;
100         }
101
102         public Menu getMenu(Menu parent) {
103             Menu menu = new Menu(parent);
104             menu.addMenuListener(new MenuAdapter() {
105                 public void menuShown(MenuEvent e) {
106                     Menu m = (Menu)e.widget;
107                     MenuItem[] items = m.getItems();
108                     for (int i=0; i < items.length; i++) {
109                         items[i].dispose();
110                     }
111                     fillMenu(m);
112                 }
113             });
114             return menu;
115         }
116         
117         private void fillMenu(Menu parent)
118         {
119             if (fActionDelegate != null)
120             {
121                 IMemoryRenderingType[] types = fActionDelegate.getMemoryRenderingTypes(fPart, fCurrentSelection);
122                 
123                 for (int i=0; i<types.length; i++)
124                 {
125                     AddMemoryRenderingAction action = new AddMemoryRenderingAction(types[i]);
126                     ActionContributionItem item = new ActionContributionItem(action);
127                     item.fill(parent, -1);
128                 }
129             }
130         }
131     }
132     
133     private class DebugContextListener implements IDebugContextListener
134     {
135
136         private void contextActivated(ISelection selection) {
137             setupActionDelegate(selection);
138             updateAction(fAction, fCurrentSelection);
139             
140         }
141
142         public void debugContextChanged(DebugContextEvent event) {
143             contextActivated(event.getContext());
144         }
145
146         
147     }
148     
149     private void setupActionDelegate(ISelection context)
150     {
151         IAdaptable debugContext = null;
152         if (context instanceof IStructuredSelection)
153         {
154             if (((IStructuredSelection)context).getFirstElement() instanceof IAdaptable)
155                 debugContext = (IAdaptable)((IStructuredSelection)context).getFirstElement();
156         }
157         
158         if (debugContext == null)
159             fActionDelegate = null;
160         
161         if (debugContext == fDebugContext)
162             return;
163         
164         fDebugContext = debugContext;
165         
166         if (fDebugContext == null)
167             return;
168         
169         IMemoryBlockRetrieval retrieval = (IMemoryBlockRetrieval)fDebugContext.getAdapter(IMemoryBlockRetrieval.class);
170         if (retrieval == null && fDebugContext instanceof IDebugElement)
171         {
172             retrieval = ((IDebugElement)fDebugContext).getDebugTarget();
173         }
174         
175         if (retrieval == null)
176             return;
177         
178         IAddMemoryRenderingsTarget target = null;
179         if (fCurrentSelection instanceof IStructuredSelection)
180         {
181             // get target from current selection
182
IStructuredSelection strucSel = (IStructuredSelection)fCurrentSelection;
183             Object JavaDoc obj = strucSel.getFirstElement();
184             target = getAddMemoryRenderingTarget(obj);
185         }
186         if (target == null)
187         {
188             // get the target from Debug View
189
target = getAddMemoryRenderingTarget(fDebugContext);
190         }
191         if (target == null)
192         {
193             // try to get target from memory block retrieval
194
target = getAddMemoryRenderingTarget(retrieval);
195         }
196         
197         fActionDelegate = target;
198     }
199
200     /* (non-Javadoc)
201      * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
202      */

203     public void init(IViewPart view) {
204         bindPart(view);
205     }
206
207     /* (non-Javadoc)
208      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
209      */

210     public void run(IAction action) {
211         // do nothing
212
}
213
214     /* (non-Javadoc)
215      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
216      */

217     public void selectionChanged(IAction action, ISelection selection) {
218         bindAction(action);
219         fCurrentSelection = selection;
220         updateAction(action, selection);
221     }
222     
223     private void updateAction(IAction action, ISelection selection)
224     {
225         if (fActionDelegate != null)
226         {
227             action.setEnabled(fActionDelegate.canAddMemoryRenderings(fPart, selection));
228             bindAction(action);
229         }
230         else
231         {
232             action.setEnabled(false);
233         }
234     }
235
236     private void bindAction(IAction action) {
237         if (action == null)
238             return;
239         
240         if (action != fAction) {
241             if (fMenuCreator == null)
242                 fMenuCreator = new AddMemoryRenderingMenuCreator();
243             action.setMenuCreator(fMenuCreator);
244             fAction= action;
245         }
246     }
247
248     private IAddMemoryRenderingsTarget getAddMemoryRenderingTarget(Object JavaDoc elmt) {
249         IAddMemoryRenderingsTarget target = null;
250         
251         if (elmt instanceof IAddMemoryRenderingsTarget)
252         {
253             target = (IAddMemoryRenderingsTarget)elmt;
254         }
255         else if (elmt instanceof IAdaptable)
256         {
257             target = (IAddMemoryRenderingsTarget)((IAdaptable)elmt).getAdapter(IAddMemoryRenderingsTarget.class);
258         }
259         return target;
260     }
261
262     /* (non-Javadoc)
263      * @see org.eclipse.ui.IEditorActionDelegate#setActiveEditor(org.eclipse.jface.action.IAction, org.eclipse.ui.IEditorPart)
264      */

265     public void setActiveEditor(IAction action, IEditorPart targetEditor) {
266         bindPart(targetEditor);
267         bindAction(action);
268         updateAction(action, fCurrentSelection);
269     }
270
271     /* (non-Javadoc)
272      * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
273      */

274     public void setActivePart(IAction action, IWorkbenchPart targetPart) {
275         bindPart(targetPart);
276         bindAction(action);
277         updateAction(action, fCurrentSelection);
278     }
279
280     /* (non-Javadoc)
281      * @see org.eclipse.ui.IActionDelegate2#init(org.eclipse.jface.action.IAction)
282      */

283     public void init(IAction action) {
284         bindAction(action);
285         if (action != null)
286         {
287             action.setText(ActionMessages.AddMemoryRenderingActionDelegate_2);
288             action.setImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_ELCL_MONITOR_EXPRESSION));
289             action.setHoverImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_LCL_MONITOR_EXPRESSION));
290             action.setDisabledImageDescriptor(DebugPluginImages.getImageDescriptor(IInternalDebugUIConstants.IMG_DLCL_MONITOR_EXPRESSION));
291         }
292     }
293
294     /* (non-Javadoc)
295      * @see org.eclipse.ui.IActionDelegate2#dispose()
296      */

297     public void dispose() {
298         // remove as debug context listener
299
fAction = null;
300         fPart = null;
301         fCurrentSelection = null;
302         fActionDelegate = null;
303         
304         // remove debug context listener
305
bindPart(null);
306     }
307
308     /* (non-Javadoc)
309      * @see org.eclipse.ui.IActionDelegate2#runWithEvent(org.eclipse.jface.action.IAction, org.eclipse.swt.widgets.Event)
310      */

311     public void runWithEvent(IAction action, Event event) {
312         // do nothing
313
}
314     
315     private void bindPart(IWorkbenchPart part)
316     {
317         IWorkbenchWindow window = null;
318         if (part != null)
319         {
320             window = part.getSite().getWorkbenchWindow();
321         }
322         if (window != fWindow)
323         {
324                 
325             if (fWindow != null)
326             {
327                 DebugUITools.getDebugContextManager().getContextService(fWindow).removeDebugContextListener(fDebugContextListener);
328             }
329             
330             if (window != null)
331             {
332                 DebugUITools.getDebugContextManager().getContextService(window).addDebugContextListener(fDebugContextListener);
333             }
334             fWindow = window;
335         }
336         
337         if (part != fPart)
338             fPart = part;
339         
340         if (fWindow != null)
341             setupActionDelegate(DebugUITools.getDebugContextManager().getContextService(fWindow).getActiveContext());
342     }
343
344 }
345
346
Popular Tags