KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > actions > RetargetAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.debug.internal.ui.actions;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IAdaptable;
16 import org.eclipse.core.runtime.IAdapterManager;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Platform;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.debug.internal.ui.DebugUIPlugin;
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.ISelectionProvider;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.swt.widgets.Event;
26 import org.eclipse.ui.IActionDelegate2;
27 import org.eclipse.ui.IEditorPart;
28 import org.eclipse.ui.IPartListener;
29 import org.eclipse.ui.IPartService;
30 import org.eclipse.ui.IWorkbenchPart;
31 import org.eclipse.ui.IWorkbenchWindow;
32 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
33
34 /**
35  * Global retargettable debug action.
36  *
37  * @since 3.0
38  */

39 public abstract class RetargetAction implements IWorkbenchWindowActionDelegate, IPartListener, IActionDelegate2 {
40     
41     protected IWorkbenchWindow fWindow = null;
42     private IWorkbenchPart fActivePart = null;
43     private Object JavaDoc fTargetAdapter = null;
44     private IAction fAction = null;
45     private static final ISelection EMPTY_SELECTION = new EmptySelection();
46     
47     static class EmptySelection implements ISelection {
48
49         /* (non-Javadoc)
50          * @see org.eclipse.jface.viewers.ISelection#isEmpty()
51          */

52         public boolean isEmpty() {
53             return true;
54         }
55     }
56     
57     /**
58      * Returns the current selection in the active part, possibly
59      * and empty selection, but never <code>null</code>.
60      *
61      * @return the selection in the active part, possibly empty
62      */

63     private ISelection getTargetSelection() {
64         if (fActivePart != null) {
65             ISelectionProvider selectionProvider = fActivePart.getSite().getSelectionProvider();
66             if (selectionProvider != null) {
67                 return selectionProvider.getSelection();
68             }
69         }
70         return EMPTY_SELECTION;
71     }
72     
73     /* (non-Javadoc)
74      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
75      */

76     public void dispose() {
77         fWindow.getPartService().removePartListener(this);
78         fActivePart = null;
79         fTargetAdapter = null;
80         
81     }
82     /* (non-Javadoc)
83      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
84      */

85     public void init(IWorkbenchWindow window) {
86         this.fWindow = window;
87         IPartService partService = window.getPartService();
88         partService.addPartListener(this);
89         IWorkbenchPart part = partService.getActivePart();
90         if (part != null) {
91             partActivated(part);
92         }
93     }
94     /* (non-Javadoc)
95      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
96      */

97     public void run(IAction action) {
98         if (fTargetAdapter != null) {
99             try {
100                 if (isTargetEnabled()) {
101                     performAction(fTargetAdapter, getTargetSelection(), fActivePart);
102                 } else {
103                     String JavaDoc message = getOperationUnavailableMessage();
104                     IStatus status = new Status(IStatus.INFO, DebugUIPlugin.getUniqueIdentifier(), message);
105                     DebugUIPlugin.errorDialog(DebugUIPlugin.getShell(), DebugUIPlugin.removeAccelerators(action.getText()), message, status);
106                 }
107             } catch (CoreException e) {
108                 DebugUIPlugin.errorDialog(fWindow.getShell(), ActionMessages.RetargetAction_2, ActionMessages.RetargetAction_3, e.getStatus()); //
109
}
110         }
111     }
112     
113     /**
114      * Returns a message to display when we find that the operation is not enabled
115      * when invoked in an editor (we check enabled state before running in this case,
116      * rather than updating on each selection change - see bug 180441).
117      *
118      * @return information message when unavailable
119      */

120     protected abstract String JavaDoc getOperationUnavailableMessage();
121     
122     /**
123      * Performs the specific breakpoint toggling.
124      *
125      * @param selection selection in the active part
126      * @param part active part
127      * @throws CoreException if an exception occurs
128      */

129     protected abstract void performAction(Object JavaDoc target, ISelection selection, IWorkbenchPart part) throws CoreException;
130     
131     /* (non-Javadoc)
132      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
133      */

134     public void selectionChanged(IAction action, ISelection selection) {
135         // if the active part did not provide an adapter, see if the selection does
136
if (fTargetAdapter == null && selection instanceof IStructuredSelection) {
137             IStructuredSelection ss = (IStructuredSelection) selection;
138             if (!ss.isEmpty()) {
139                 Object JavaDoc object = ss.getFirstElement();
140                 if (object instanceof IAdaptable) {
141                     fTargetAdapter = getAdapter((IAdaptable) object);
142                 }
143             }
144         }
145         boolean enabled = fTargetAdapter != null;
146         if (selection instanceof IStructuredSelection) {
147             enabled = isTargetEnabled();
148         }
149         action.setEnabled(enabled);
150     }
151     
152     /* (non-Javadoc)
153      * @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
154      */

155     public void partActivated(IWorkbenchPart part) {
156         fActivePart = part;
157         IResource resource = (IResource) part.getAdapter(IResource.class);
158         if (resource == null && part instanceof IEditorPart) {
159             resource = (IResource) ((IEditorPart)part).getEditorInput().getAdapter(IResource.class);
160         }
161         if (resource != null) {
162             fTargetAdapter = getAdapter(resource);
163         }
164         if (fTargetAdapter == null) {
165             fTargetAdapter = getAdapter(part);
166         }
167         if (fAction != null) {
168             fAction.setEnabled(fTargetAdapter != null);
169         }
170     }
171     
172     protected Object JavaDoc getAdapter(IAdaptable adaptable) {
173         Object JavaDoc adapter = adaptable.getAdapter(getAdapterClass());
174         if (adapter == null) {
175             IAdapterManager adapterManager = Platform.getAdapterManager();
176             if (adapterManager.hasAdapter(adaptable, getAdapterClass().getName())) {
177                 fTargetAdapter = adapterManager.loadAdapter(adaptable, getAdapterClass().getName());
178             }
179         }
180         return adapter;
181     }
182     
183     /**
184      * Returns the type of adapter (target) this action works on.
185      *
186      * @return the type of adapter this action works on
187      */

188     protected abstract Class JavaDoc getAdapterClass();
189     
190     /* (non-Javadoc)
191      * @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
192      */

193     public void partBroughtToTop(IWorkbenchPart part) {
194     }
195     /* (non-Javadoc)
196      * @see org.eclipse.ui.IPartListener#partClosed(org.eclipse.ui.IWorkbenchPart)
197      */

198     public void partClosed(IWorkbenchPart part) {
199         clearPart(part);
200     }
201     
202     /**
203      * Clears reference to active part and adapter when a relevant part
204      * is closed or no longer active.
205      *
206      * @param part workbench part that has been closed or no longer active
207      */

208     protected void clearPart(IWorkbenchPart part) {
209         if (part.equals(fActivePart)) {
210             fActivePart = null;
211             fTargetAdapter = null;
212         }
213     }
214     /* (non-Javadoc)
215      * @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
216      */

217     public void partDeactivated(IWorkbenchPart part) {
218         clearPart(part);
219     }
220     /* (non-Javadoc)
221      * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
222      */

223     public void partOpened(IWorkbenchPart part) {
224     }
225
226     /**
227      * Returns whether the target adapter is enabled
228      *
229      * @return whether target adapter is enabled
230      */

231     protected boolean isTargetEnabled() {
232         if (fTargetAdapter != null) {
233             if (fActivePart != null) {
234                 return canPerformAction(fTargetAdapter, getTargetSelection(), fActivePart);
235             }
236         }
237         return false;
238     }
239     
240     /**
241      * Returns whether the specific operation is supported.
242      *
243      * @param target the target adapter
244      * @param selection the selection to verify the operation on
245      * @param part the part the operation has been requested on
246      * @return whether the operation can be performed
247      */

248     protected abstract boolean canPerformAction(Object JavaDoc target, ISelection selection, IWorkbenchPart part);
249     
250     /* (non-Javadoc)
251      * @see org.eclipse.ui.IActionDelegate2#init(org.eclipse.jface.action.IAction)
252      */

253     public void init(IAction action) {
254         fAction = action;
255     }
256
257     /* (non-Javadoc)
258      * @see org.eclipse.ui.IActionDelegate2#runWithEvent(org.eclipse.jface.action.IAction, org.eclipse.swt.widgets.Event)
259      */

260     public void runWithEvent(IAction action, Event event) {
261         run(action);
262     }
263
264     /**
265      * Returns the proxy to this action delegate or <code>null</code>
266      *
267      * @return action proxy or <code>null</code>
268      */

269     protected IAction getAction() {
270         return fAction;
271     }
272
273     /**
274      * Returns whether there is currently a target adapter for this action.
275      *
276      * @return whether the action has a target adapter.
277      */

278     protected boolean hasTargetAdapter() {
279         return fTargetAdapter != null;
280     }
281
282 }
283
Popular Tags