KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  * Mikhail Khodjaiants (QNX) - https://bugs.eclipse.org/bugs/show_bug.cgi?id=83464
11  *******************************************************************************/

12 package org.eclipse.debug.ui.actions;
13
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IAdapterManager;
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.debug.core.model.ISuspendResume;
18 import org.eclipse.debug.internal.ui.DebugUIPlugin;
19 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
20 import org.eclipse.debug.internal.ui.actions.ActionMessages;
21 import org.eclipse.debug.ui.DebugUITools;
22 import org.eclipse.debug.ui.contexts.DebugContextEvent;
23 import org.eclipse.debug.ui.contexts.IDebugContextListener;
24 import org.eclipse.debug.ui.contexts.IDebugContextManager;
25 import org.eclipse.debug.ui.contexts.IDebugContextService;
26 import org.eclipse.jface.action.IAction;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.ISelectionProvider;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.swt.widgets.Event;
31 import org.eclipse.ui.IActionDelegate2;
32 import org.eclipse.ui.IEditorActionDelegate;
33 import org.eclipse.ui.IEditorPart;
34 import org.eclipse.ui.IViewActionDelegate;
35 import org.eclipse.ui.IViewPart;
36 import org.eclipse.ui.IWorkbenchPart;
37 import org.eclipse.ui.IWorkbenchPartSite;
38 import org.eclipse.ui.IWorkbenchWindow;
39
40 /**
41  * A run to line action that can be contributed to a an editor or view. The action
42  * will perform the "run to line" operation for parts that provide
43  * an appropriate <code>IRunToLineTarget</code> adapter.
44  * <p>
45  * Clients may reference/contribute this class as an action delegate
46  * in plug-in XML. This class is not intended to be subclassed.
47  * </p>
48  * <p>
49  * Since 3.1, this action also implements {@link org.eclipse.ui.IViewActionDelegate}.
50  * </p>
51  * @since 3.0
52  */

53 public class RunToLineActionDelegate implements IEditorActionDelegate, IActionDelegate2, IViewActionDelegate {
54     
55     private IWorkbenchPart fActivePart = null;
56     private IRunToLineTarget fPartTarget = null;
57     private IAction fAction = null;
58     private DebugContextListener fContextListener = new DebugContextListener();
59     private ISuspendResume fTargetElement = null;
60     
61     class DebugContextListener implements IDebugContextListener {
62
63         protected void contextActivated(ISelection selection) {
64             fTargetElement = null;
65             if (selection instanceof IStructuredSelection) {
66                 IStructuredSelection ss = (IStructuredSelection) selection;
67                 if (ss.size() == 1) {
68                     Object JavaDoc object = ss.getFirstElement();
69                     if (object instanceof ISuspendResume) {
70                         fTargetElement = (ISuspendResume) object;
71                     }
72                 }
73             }
74             update();
75         }
76
77         public void debugContextChanged(DebugContextEvent event) {
78             contextActivated(event.getContext());
79         }
80         
81     }
82     
83     /*(non-Javadoc)
84      * @see org.eclipse.ui.IActionDelegate2#dispose()
85      */

86     public void dispose() {
87         DebugUITools.getDebugContextManager().getContextService(fActivePart.getSite().getWorkbenchWindow()).removeDebugContextListener(fContextListener);
88         fActivePart = null;
89         fPartTarget = null;
90         
91     }
92     /* (non-Javadoc)
93      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
94      */

95     public void run(IAction action) {
96         if (fPartTarget != null && fTargetElement != null) {
97             try {
98                 fPartTarget.runToLine(fActivePart, fActivePart.getSite().getSelectionProvider().getSelection(), fTargetElement);
99             } catch (CoreException e) {
100                 DebugUIPlugin.errorDialog(fActivePart.getSite().getWorkbenchWindow().getShell(), ActionMessages.RunToLineAction_0, ActionMessages.RunToLineAction_1, e.getStatus()); //
101
}
102         }
103     }
104     /* (non-Javadoc)
105      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
106      */

107     public void selectionChanged(IAction action, ISelection selection) {
108         this.fAction = action;
109         update();
110     }
111     
112     /* (non-Javadoc)
113      * @see org.eclipse.ui.texteditor.IUpdate#update()
114      */

115     public void update() {
116         if (fAction == null) {
117             return;
118         }
119         Runnable JavaDoc r = new Runnable JavaDoc() {
120             public void run() {
121                 boolean enabled = false;
122                 if (fPartTarget != null && fTargetElement != null) {
123                     IWorkbenchPartSite site = fActivePart.getSite();
124                     if (site != null) {
125                         ISelectionProvider selectionProvider = site.getSelectionProvider();
126                         if (selectionProvider != null) {
127                             ISelection selection = selectionProvider.getSelection();
128                             enabled = fTargetElement.isSuspended() && fPartTarget.canRunToLine(fActivePart, selection, fTargetElement);
129                         }
130                     }
131                 }
132                 fAction.setEnabled(enabled);
133             }
134         };
135         DebugUIPlugin.getStandardDisplay().asyncExec(r);
136     }
137         
138     /* (non-Javadoc)
139      * @see org.eclipse.ui.IActionDelegate2#init(org.eclipse.jface.action.IAction)
140      */

141     public void init(IAction action) {
142         this.fAction = action;
143         if (action != null) {
144             action.setText(ActionMessages.RunToLineActionDelegate_4);
145             action.setImageDescriptor(DebugUITools.getImageDescriptor(IInternalDebugUIConstants.IMG_LCL_RUN_TO_LINE));
146             action.setDisabledImageDescriptor(DebugUITools.getImageDescriptor(IInternalDebugUIConstants.IMG_DLCL_RUN_TO_LINE));
147         }
148     }
149     /* (non-Javadoc)
150      * @see org.eclipse.ui.IActionDelegate2#runWithEvent(org.eclipse.jface.action.IAction, org.eclipse.swt.widgets.Event)
151      */

152     public void runWithEvent(IAction action, Event event) {
153         run(action);
154     }
155     /* (non-Javadoc)
156      * @see org.eclipse.ui.IEditorActionDelegate#setActiveEditor(org.eclipse.jface.action.IAction, org.eclipse.ui.IEditorPart)
157      */

158     public void setActiveEditor(IAction action, IEditorPart targetEditor) {
159         init(action);
160         bindTo(targetEditor);
161     }
162     
163     /* (non-Javadoc)
164      * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
165      */

166     public void init(IViewPart view) {
167         bindTo(view);
168     }
169     
170     /**
171      * Binds this action to operate on the given part's run to line adapter.
172      *
173      * @param part
174      */

175     private void bindTo(IWorkbenchPart part) {
176         IDebugContextManager manager = DebugUITools.getDebugContextManager();
177         if (fActivePart != null && !fActivePart.equals(part)) {
178             manager.getContextService(fActivePart.getSite().getWorkbenchWindow()).removeDebugContextListener(fContextListener);
179         }
180         fPartTarget = null;
181         fActivePart = part;
182         if (part != null) {
183             IWorkbenchWindow workbenchWindow = part.getSite().getWorkbenchWindow();
184             IDebugContextService service = manager.getContextService(workbenchWindow);
185             service.addDebugContextListener(fContextListener);
186             fPartTarget = (IRunToLineTarget) part.getAdapter(IRunToLineTarget.class);
187             if (fPartTarget == null) {
188                 IAdapterManager adapterManager = Platform.getAdapterManager();
189                 // TODO: we could restrict loading to cases when the debugging context is on
190
if (adapterManager.hasAdapter(part, IRunToLineTarget.class.getName())) {
191                     fPartTarget = (IRunToLineTarget) adapterManager.loadAdapter(part, IRunToLineTarget.class.getName());
192                 }
193             }
194             ISelection activeContext = service.getActiveContext();
195             fContextListener.contextActivated(activeContext);
196         }
197         update();
198     }
199 }
200
Popular Tags