1
/*******************************************************************************
2
* Copyright (c) 2007, 2009 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.includebrowser;
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.ISourceReference;
22
import org.eclipse.cdt.ui.CUIPlugin;
23
import org.eclipse.cdt.ui.actions.SelectionDispatchAction;
26
public class OpenIncludeBrowserAction extends SelectionDispatchAction {
28
private ITextEditor fEditor;
30
public OpenIncludeBrowserAction(IWorkbenchSite site) {
32
setText(IBMessages.OpenIncludeBrowserAction_label);
33
setToolTipText(IBMessages.OpenIncludeBrowserAction_tooltip);
36
public OpenIncludeBrowserAction(ITextEditor editor) {
37
this(editor.getSite());
39
setEnabled(fEditor != null && CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editor.getEditorInput()) != null);
43
public void run(ITextSelection sel) {
44
IncludeBrowserUI.open(fEditor, sel);
48
public void run(IStructuredSelection selection) {
49
if (!selection.isEmpty()) {
50
Object selectedObject= selection.getFirstElement();
51
ICElement elem= (ICElement) getAdapter(selectedObject, ICElement.class);
53
IncludeBrowserUI.open(getSite().getWorkbenchWindow(), elem);
59
public void selectionChanged(ITextSelection sel) {
63
public void selectionChanged(IStructuredSelection selection) {
64
if (selection.isEmpty()) {
69
Object selectedObject= selection.getFirstElement();
70
ICElement elem= (ICElement) getAdapter(selectedObject, ICElement.class);
72
setEnabled(isValidElement(elem));
79
private boolean isValidElement(ICElement elem) {
80
if (elem instanceof ISourceReference) {
86
@SuppressWarnings("rawtypes")
87
private Object getAdapter(Object object, Class desiredClass) {
88
if (desiredClass.isInstance(object)) {
91
if (object instanceof IAdaptable) {
92
IAdaptable adaptable= (IAdaptable) object;
93
return adaptable.getAdapter(desiredClass);