KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > navigator > ShowInNavigatorAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation 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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.views.navigator;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.resources.IMarker;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.IAdaptable;
21 import org.eclipse.jface.dialogs.ErrorDialog;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.ISelectionProvider;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.viewers.StructuredSelection;
26 import org.eclipse.ui.IPageLayout;
27 import org.eclipse.ui.IViewPart;
28 import org.eclipse.ui.IWorkbenchPage;
29 import org.eclipse.ui.PartInitException;
30 import org.eclipse.ui.actions.SelectionProviderAction;
31 import org.eclipse.ui.internal.views.navigator.ResourceNavigatorMessages;
32 import org.eclipse.ui.part.ISetSelectionTarget;
33
34 /**
35  * An action which shows the current selection in the Navigator view.
36  * For each element in the selection, if it is an <code>IResource</code>
37  * it uses it directly, otherwise if it is an <code>IMarker</code> it uses the marker's resource,
38  * otherwise if it is an <code>IAdaptable</code>, it tries to get the <code>IResource.class</code> adapter.
39  */

40 public class ShowInNavigatorAction extends SelectionProviderAction {
41     private IWorkbenchPage page;
42
43     /**
44      * Create a new instance of this class.
45      *
46      * @param page the page
47      * @param viewer the viewer
48      */

49     public ShowInNavigatorAction(IWorkbenchPage page, ISelectionProvider viewer) {
50         super(viewer, ResourceNavigatorMessages.ShowInNavigator_text);
51         Assert.isNotNull(page);
52         this.page = page;
53         setDescription(ResourceNavigatorMessages.ShowInNavigator_toolTip);
54         page.getWorkbenchWindow().getWorkbench().getHelpSystem().setHelp(this,
55                 INavigatorHelpContextIds.SHOW_IN_NAVIGATOR_ACTION);
56     }
57
58     /**
59      * Returns the resources in the given selection.
60      *
61      * @return a list of <code>IResource</code>
62      */

63     List JavaDoc getResources(IStructuredSelection selection) {
64         List JavaDoc v = new ArrayList JavaDoc();
65         for (Iterator JavaDoc i = selection.iterator(); i.hasNext();) {
66             Object JavaDoc o = i.next();
67             if (o instanceof IResource) {
68                 v.add(o);
69             } else if (o instanceof IMarker) {
70                 IResource resource = ((IMarker) o).getResource();
71                 v.add(resource);
72             } else if (o instanceof IAdaptable) {
73                 IResource resource = (IResource) ((IAdaptable) o)
74                         .getAdapter(IResource.class);
75                 if (resource != null) {
76                     v.add(resource);
77                 }
78             }
79         }
80         return v;
81     }
82
83     /*
84      * (non-Javadoc)
85      * Method declared on IAction.
86      */

87     /**
88      * Shows the Navigator view and sets its selection to the resources
89      * selected in this action's selection provider.
90      */

91     public void run() {
92         List JavaDoc v = getResources(getStructuredSelection());
93         if (v.isEmpty()) {
94             return;
95         }
96         try {
97             IViewPart view = page.showView(IPageLayout.ID_RES_NAV);
98             if (view instanceof ISetSelectionTarget) {
99                 ISelection selection = new StructuredSelection(v);
100                 ((ISetSelectionTarget) view).selectReveal(selection);
101             }
102         } catch (PartInitException e) {
103             ErrorDialog.openError(page.getWorkbenchWindow().getShell(),
104                     ResourceNavigatorMessages.ShowInNavigator_errorMessage,
105                     e.getMessage(), e.getStatus());
106         }
107     }
108
109     /*
110      * (non-Javadoc)
111      * Method declared on SelectionProviderAction.
112      */

113     public void selectionChanged(IStructuredSelection selection) {
114         setEnabled(!getResources(selection).isEmpty());
115     }
116 }
117
Popular Tags