| /******************************************************************************* |
| * Copyright (c) 2007, 2008 Wind River Systems, Inc. and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Markus Schorn - initial API and implementation |
| * IBM Corporation |
| *******************************************************************************/ |
| |
| /* -- ST-Origin -- |
| * Source folder: org.eclipse.cdt.ui/src |
| * Class: org.eclipse.cdt.internal.ui.includebrowser.OpenIncludeBrowserAction |
| * Version: 1.1 |
| */ |
| |
| package org.eclipse.ptp.internal.rdt.ui.includebrowser; |
| |
| import org.eclipse.cdt.core.model.ICElement; |
| import org.eclipse.cdt.core.model.ISourceReference; |
| import org.eclipse.cdt.internal.ui.includebrowser.IBMessages; |
| import org.eclipse.cdt.ui.CUIPlugin; |
| import org.eclipse.cdt.ui.actions.SelectionDispatchAction; |
| import org.eclipse.core.runtime.IAdaptable; |
| import org.eclipse.jface.text.ITextSelection; |
| import org.eclipse.jface.viewers.IStructuredSelection; |
| import org.eclipse.ptp.internal.rdt.core.includebrowser.IIncludeBrowserService; |
| import org.eclipse.ptp.internal.rdt.core.includebrowser.IncludeBrowserServiceFactory; |
| import org.eclipse.ptp.internal.rdt.core.includebrowser.LocalIncludeBrowserService; |
| import org.eclipse.ui.IWorkbenchSite; |
| import org.eclipse.ui.texteditor.ITextEditor; |
| |
| |
| public class OpenIncludeBrowserAction extends SelectionDispatchAction { |
| |
| private ITextEditor fEditor; |
| |
| public OpenIncludeBrowserAction(IWorkbenchSite site) { |
| super(site); |
| setText(IBMessages.OpenIncludeBrowserAction_label); |
| setToolTipText(IBMessages.OpenIncludeBrowserAction_tooltip); |
| } |
| |
| public OpenIncludeBrowserAction(ITextEditor editor) { |
| this(editor.getSite()); |
| fEditor= editor; |
| setEnabled(fEditor != null && CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editor.getEditorInput()) != null); |
| } |
| |
| public void run(ITextSelection sel) { |
| IncludeBrowserUI.open(fEditor, sel); |
| } |
| |
| public void run(IStructuredSelection selection) { |
| if (!selection.isEmpty()) { |
| Object selectedObject= selection.getFirstElement(); |
| ICElement elem= (ICElement) getAdapter(selectedObject, ICElement.class); |
| if (elem != null) { |
| IncludeBrowserUI.open(getSite().getWorkbenchWindow(), elem); |
| } |
| } |
| } |
| |
| public void selectionChanged(ITextSelection sel) { |
| } |
| |
| public void selectionChanged(IStructuredSelection selection) { |
| if (selection.isEmpty()) { |
| setEnabled(false); |
| return; |
| } |
| |
| Object selectedObject= selection.getFirstElement(); |
| ICElement elem= (ICElement) getAdapter(selectedObject, ICElement.class); |
| if (elem != null) { |
| setEnabled(isValidElement(elem)); |
| } |
| else { |
| setEnabled(false); |
| } |
| } |
| |
| private boolean isValidElement(ICElement elem) { |
| if (elem instanceof ISourceReference) { |
| return true; |
| } |
| return false; |
| } |
| |
| private Object getAdapter(Object object, Class desiredClass) { |
| if (desiredClass.isInstance(object)) { |
| return object; |
| } |
| if (object instanceof IAdaptable) { |
| IAdaptable adaptable= (IAdaptable) object; |
| return adaptable.getAdapter(desiredClass); |
| } |
| return null; |
| } |
| } |