KickJava   Java API By Example, From Geeks To Geeks.

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


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.runtime.CoreException;
14 import org.eclipse.debug.core.model.ISuspendResume;
15 import org.eclipse.debug.ui.DebugUITools;
16 import org.eclipse.debug.ui.actions.IRunToLineTarget;
17 import org.eclipse.debug.ui.contexts.DebugContextEvent;
18 import org.eclipse.debug.ui.contexts.IDebugContextListener;
19 import org.eclipse.debug.ui.contexts.IDebugContextService;
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.ui.IWorkbenchPart;
24 import org.eclipse.ui.IWorkbenchWindow;
25
26 /**
27  * Global retargettable run to line action.
28  *
29  * @since 3.0
30  */

31 public class RetargetRunToLineAction extends RetargetAction {
32     
33     private DebugContextListener fContextListener = new DebugContextListener();
34     private ISuspendResume fTargetElement = null;
35     
36     class DebugContextListener implements IDebugContextListener {
37
38         protected void contextActivated(ISelection selection) {
39             fTargetElement = null;
40             if (selection instanceof IStructuredSelection) {
41                 IStructuredSelection ss = (IStructuredSelection) selection;
42                 if (ss.size() == 1) {
43                     Object JavaDoc object = ss.getFirstElement();
44                     if (object instanceof ISuspendResume) {
45                         fTargetElement = (ISuspendResume) object;
46                     }
47                 }
48             }
49             IAction action = getAction();
50             if (action != null) {
51                 action.setEnabled(fTargetElement != null && hasTargetAdapter());
52             }
53         }
54
55         public void debugContextChanged(DebugContextEvent event) {
56             contextActivated(event.getContext());
57         }
58         
59         
60     }
61     
62     /* (non-Javadoc)
63      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
64      */

65     public void dispose() {
66         DebugUITools.getDebugContextManager().getContextService(fWindow).removeDebugContextListener(fContextListener);
67         super.dispose();
68     }
69     /* (non-Javadoc)
70      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
71      */

72     public void init(IWorkbenchWindow window) {
73         super.init(window);
74         IDebugContextService service = DebugUITools.getDebugContextManager().getContextService(window);
75         service.addDebugContextListener(fContextListener);
76         ISelection activeContext = service.getActiveContext();
77         fContextListener.contextActivated(activeContext);
78     }
79         
80     /* (non-Javadoc)
81      * @see org.eclipse.debug.internal.ui.actions.RetargetAction#canPerformAction(java.lang.Object, org.eclipse.jface.viewers.ISelection, org.eclipse.ui.IWorkbenchPart)
82      */

83     protected boolean canPerformAction(Object JavaDoc target, ISelection selection, IWorkbenchPart part) {
84         return fTargetElement != null &&
85             ((IRunToLineTarget)target).canRunToLine(part, selection, fTargetElement);
86     }
87     
88     /* (non-Javadoc)
89      * @see org.eclipse.debug.internal.ui.actions.RetargetAction#getAdapterClass()
90      */

91     protected Class JavaDoc getAdapterClass() {
92         return IRunToLineTarget.class;
93     }
94     /* (non-Javadoc)
95      * @see org.eclipse.debug.internal.ui.actions.RetargetAction#performAction(java.lang.Object, org.eclipse.jface.viewers.ISelection, org.eclipse.ui.IWorkbenchPart)
96      */

97     protected void performAction(Object JavaDoc target, ISelection selection, IWorkbenchPart part) throws CoreException {
98         ((IRunToLineTarget)target).runToLine(part, selection, fTargetElement);
99     }
100     
101     /* (non-Javadoc)
102      * @see org.eclipse.debug.internal.ui.actions.RetargetAction#getOperationUnavailableMessage()
103      */

104     protected String JavaDoc getOperationUnavailableMessage() {
105         return ActionMessages.RetargetRunToLineAction_0;
106     }
107     
108     /* (non-Javadoc)
109      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
110      */

111     public void selectionChanged(IAction action, ISelection selection) {
112         if (fTargetElement == null) {
113             action.setEnabled(false);
114         } else {
115             super.selectionChanged(action, selection);
116         }
117     }
118 }
119
Popular Tags