KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > callhierarchy > FocusOnSelectionAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  * Jesper Kamstrup Linnet (eclipse@kamstrup-linnet.dk) - initial API and implementation
10  * (report 36180: Callers/Callees view)
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.ui.callhierarchy;
13
14 import org.eclipse.jface.action.Action;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.ISelectionProvider;
17
18 import org.eclipse.ui.PlatformUI;
19
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.IMember;
22 import org.eclipse.jdt.core.IMethod;
23
24 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
25 import org.eclipse.jdt.internal.ui.util.SelectionUtil;
26
27 import org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper;
28 import org.eclipse.jdt.internal.corext.util.Messages;
29
30 class FocusOnSelectionAction extends Action {
31     private CallHierarchyViewPart fPart;
32
33     public FocusOnSelectionAction(CallHierarchyViewPart part) {
34         super(CallHierarchyMessages.FocusOnSelectionAction_focusOnSelection_text);
35         fPart= part;
36         setDescription(CallHierarchyMessages.FocusOnSelectionAction_focusOnSelection_description);
37         setToolTipText(CallHierarchyMessages.FocusOnSelectionAction_focusOnSelection_tooltip);
38         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.CALL_HIERARCHY_FOCUS_ON_SELECTION_ACTION);
39     }
40
41     public boolean canActionBeAdded() {
42         Object JavaDoc element = SelectionUtil.getSingleElement(getSelection());
43
44         IMethod method = getSelectedMethod(element);
45         
46         if (method != null) {
47             setText(Messages.format(CallHierarchyMessages.FocusOnSelectionAction_focusOn_text, method.getElementName()));
48
49             return true;
50         }
51
52         return false;
53     }
54
55     private IMethod getSelectedMethod(Object JavaDoc element) {
56         IMethod method = null;
57         
58         if (element instanceof IMethod) {
59             method= (IMethod) element;
60         } else if (element instanceof MethodWrapper) {
61             IMember member= ((MethodWrapper) element).getMember();
62             if (member.getElementType() == IJavaElement.METHOD) {
63                 method= (IMethod) member;
64             }
65         }
66         return method;
67     }
68
69     /*
70      * @see Action#run
71      */

72     public void run() {
73         Object JavaDoc element = SelectionUtil.getSingleElement(getSelection());
74
75         IMethod method= getSelectedMethod(element);
76         if (method != null) {
77                 fPart.setMethod(method);
78         }
79     }
80
81     private ISelection getSelection() {
82         ISelectionProvider provider = fPart.getSite().getSelectionProvider();
83
84         if (provider != null) {
85             return provider.getSelection();
86         }
87
88         return null;
89     }
90 }
91
Popular Tags