1
/*******************************************************************************
2
* Copyright (c) 2006, 2007 Wind River Systems, Inc. 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
9
* Markus Schorn - initial API and implementation
10
*******************************************************************************/
12
package org.eclipse.cdt.internal.ui.callhierarchy;
14
import org.eclipse.core.runtime.IAdaptable;
15
import org.eclipse.jface.text.ITextSelection;
16
import org.eclipse.jface.viewers.IStructuredSelection;
17
import org.eclipse.ui.IWorkbenchSite;
18
import org.eclipse.ui.texteditor.ITextEditor;
20
import org.eclipse.cdt.core.model.ICElement;
21
import org.eclipse.cdt.core.model.IEnumeration;
22
import org.eclipse.cdt.core.model.IEnumerator;
23
import org.eclipse.cdt.core.model.IFunctionDeclaration;
24
import org.eclipse.cdt.core.model.IVariableDeclaration;
25
import org.eclipse.cdt.ui.CUIPlugin;
26
import org.eclipse.cdt.ui.actions.SelectionDispatchAction;
29
public class OpenCallHierarchyAction extends SelectionDispatchAction {
31
private ITextEditor fEditor;
33
public OpenCallHierarchyAction(IWorkbenchSite site) {
35
setText(CHMessages.OpenCallHierarchyAction_label);
36
setToolTipText(CHMessages.OpenCallHierarchyAction_tooltip);
39
public OpenCallHierarchyAction(ITextEditor editor) {
40
this(editor.getSite());
42
setEnabled(fEditor != null && CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editor.getEditorInput()) != null);
46
public void run(ITextSelection sel) {
47
CallHierarchyUI.open(fEditor, sel);
51
public void run(IStructuredSelection selection) {
52
if (!selection.isEmpty()) {
53
Object selectedObject= selection.getFirstElement();
54
ICElement elem= (ICElement) getAdapter(selectedObject, ICElement.class);
56
CallHierarchyUI.open(getSite().getWorkbenchWindow(), elem);
62
public void selectionChanged(ITextSelection sel) {
66
public void selectionChanged(IStructuredSelection selection) {
67
if (selection.isEmpty()) {
72
Object selectedObject= selection.getFirstElement();
73
ICElement elem= (ICElement) getAdapter(selectedObject, ICElement.class);
75
setEnabled(isValidElement(elem));
82
private boolean isValidElement(ICElement elem) {
83
if (elem instanceof IFunctionDeclaration) {
86
if (elem instanceof IVariableDeclaration) {
87
return !(elem instanceof IEnumeration);
89
if (elem instanceof IEnumerator) {
95
@SuppressWarnings("unchecked")
96
private Object getAdapter(Object object, Class desiredClass) {
97
if (desiredClass.isInstance(object)) {
100
if (object instanceof IAdaptable) {
101
IAdaptable adaptable= (IAdaptable) object;
102
return adaptable.getAdapter(desiredClass);