~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to results/plugins/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/callhierarchy/OpenCallHierarchyAction.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
7
 
 *
8
 
 * Contributors:
9
 
 *    Markus Schorn - initial API and implementation
10
 
 *******************************************************************************/ 
11
 
 
12
 
package org.eclipse.cdt.internal.ui.callhierarchy;
13
 
 
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;
19
 
 
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;
27
 
 
28
 
 
29
 
public class OpenCallHierarchyAction extends SelectionDispatchAction {
30
 
 
31
 
        private ITextEditor fEditor;
32
 
 
33
 
        public OpenCallHierarchyAction(IWorkbenchSite site) {
34
 
                super(site);
35
 
                setText(CHMessages.OpenCallHierarchyAction_label);
36
 
                setToolTipText(CHMessages.OpenCallHierarchyAction_tooltip);
37
 
        }
38
 
        
39
 
        public OpenCallHierarchyAction(ITextEditor editor) {
40
 
                this(editor.getSite());
41
 
                fEditor= editor;
42
 
                setEnabled(fEditor != null && CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editor.getEditorInput()) != null);
43
 
        }
44
 
 
45
 
        @Override
46
 
        public void run(ITextSelection sel) {
47
 
                CallHierarchyUI.open(fEditor, sel);
48
 
        }
49
 
        
50
 
        @Override
51
 
        public void run(IStructuredSelection selection) {
52
 
                if (!selection.isEmpty()) {
53
 
                        Object selectedObject= selection.getFirstElement();
54
 
                        ICElement elem= (ICElement) getAdapter(selectedObject, ICElement.class);
55
 
                        if (elem != null) {
56
 
                                CallHierarchyUI.open(getSite().getWorkbenchWindow(), elem);
57
 
                        }
58
 
                }
59
 
        }
60
 
 
61
 
        @Override
62
 
        public void selectionChanged(ITextSelection sel) {
63
 
        }
64
 
                        
65
 
        @Override
66
 
        public void selectionChanged(IStructuredSelection selection) {
67
 
                if (selection.isEmpty()) {
68
 
                        setEnabled(false);
69
 
                        return;
70
 
                }
71
 
                
72
 
                Object selectedObject= selection.getFirstElement();
73
 
                ICElement elem= (ICElement) getAdapter(selectedObject, ICElement.class);
74
 
                if (elem != null) {
75
 
                        setEnabled(isValidElement(elem));
76
 
                }
77
 
                else {
78
 
                        setEnabled(false);
79
 
                }
80
 
        }
81
 
 
82
 
        private boolean isValidElement(ICElement elem) {
83
 
                if (elem instanceof IFunctionDeclaration) {
84
 
                        return true;
85
 
                }
86
 
                if (elem instanceof IVariableDeclaration) {
87
 
                        return !(elem instanceof IEnumeration);
88
 
                }
89
 
                if (elem instanceof IEnumerator) {
90
 
                        return true;
91
 
                }
92
 
                return false;
93
 
        }
94
 
 
95
 
        @SuppressWarnings("unchecked")
96
 
        private Object getAdapter(Object object, Class desiredClass) {
97
 
                if (desiredClass.isInstance(object)) {
98
 
                        return object;
99
 
                }
100
 
                if (object instanceof IAdaptable) {
101
 
                        IAdaptable adaptable= (IAdaptable) object;
102
 
                        return adaptable.getAdapter(desiredClass);
103
 
                }
104
 
                return null;
105
 
        }
106
 
}