001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.views.navigator;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.core.resources.IMarker;
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.core.runtime.Assert;
019: import org.eclipse.core.runtime.IAdaptable;
020: import org.eclipse.jface.dialogs.ErrorDialog;
021: import org.eclipse.jface.viewers.ISelection;
022: import org.eclipse.jface.viewers.ISelectionProvider;
023: import org.eclipse.jface.viewers.IStructuredSelection;
024: import org.eclipse.jface.viewers.StructuredSelection;
025: import org.eclipse.ui.IPageLayout;
026: import org.eclipse.ui.IViewPart;
027: import org.eclipse.ui.IWorkbenchPage;
028: import org.eclipse.ui.PartInitException;
029: import org.eclipse.ui.actions.SelectionProviderAction;
030: import org.eclipse.ui.internal.views.navigator.ResourceNavigatorMessages;
031: import org.eclipse.ui.part.ISetSelectionTarget;
032:
033: /**
034: * An action which shows the current selection in the Navigator view.
035: * For each element in the selection, if it is an <code>IResource</code>
036: * it uses it directly, otherwise if it is an <code>IMarker</code> it uses the marker's resource,
037: * otherwise if it is an <code>IAdaptable</code>, it tries to get the <code>IResource.class</code> adapter.
038: */
039: public class ShowInNavigatorAction extends SelectionProviderAction {
040: private IWorkbenchPage page;
041:
042: /**
043: * Create a new instance of this class.
044: *
045: * @param page the page
046: * @param viewer the viewer
047: */
048: public ShowInNavigatorAction(IWorkbenchPage page,
049: ISelectionProvider viewer) {
050: super (viewer, ResourceNavigatorMessages.ShowInNavigator_text);
051: Assert.isNotNull(page);
052: this .page = page;
053: setDescription(ResourceNavigatorMessages.ShowInNavigator_toolTip);
054: page
055: .getWorkbenchWindow()
056: .getWorkbench()
057: .getHelpSystem()
058: .setHelp(
059: this ,
060: INavigatorHelpContextIds.SHOW_IN_NAVIGATOR_ACTION);
061: }
062:
063: /**
064: * Returns the resources in the given selection.
065: *
066: * @return a list of <code>IResource</code>
067: */
068: List getResources(IStructuredSelection selection) {
069: List v = new ArrayList();
070: for (Iterator i = selection.iterator(); i.hasNext();) {
071: Object o = i.next();
072: if (o instanceof IResource) {
073: v.add(o);
074: } else if (o instanceof IMarker) {
075: IResource resource = ((IMarker) o).getResource();
076: v.add(resource);
077: } else if (o instanceof IAdaptable) {
078: IResource resource = (IResource) ((IAdaptable) o)
079: .getAdapter(IResource.class);
080: if (resource != null) {
081: v.add(resource);
082: }
083: }
084: }
085: return v;
086: }
087:
088: /*
089: * (non-Javadoc)
090: * Method declared on IAction.
091: */
092: /**
093: * Shows the Navigator view and sets its selection to the resources
094: * selected in this action's selection provider.
095: */
096: public void run() {
097: List v = getResources(getStructuredSelection());
098: if (v.isEmpty()) {
099: return;
100: }
101: try {
102: IViewPart view = page.showView(IPageLayout.ID_RES_NAV);
103: if (view instanceof ISetSelectionTarget) {
104: ISelection selection = new StructuredSelection(v);
105: ((ISetSelectionTarget) view).selectReveal(selection);
106: }
107: } catch (PartInitException e) {
108: ErrorDialog
109: .openError(
110: page.getWorkbenchWindow().getShell(),
111: ResourceNavigatorMessages.ShowInNavigator_errorMessage,
112: e.getMessage(), e.getStatus());
113: }
114: }
115:
116: /*
117: * (non-Javadoc)
118: * Method declared on SelectionProviderAction.
119: */
120: public void selectionChanged(IStructuredSelection selection) {
121: setEnabled(!getResources(selection).isEmpty());
122: }
123: }
|