KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > view > ShowInWorkspaceAction


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.pde.internal.ui.view;
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.IResource;
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.ISelectionProvider;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.pde.core.plugin.IPluginModelBase;
24 import org.eclipse.pde.internal.core.ModelEntry;
25 import org.eclipse.pde.internal.ui.PDEPlugin;
26 import org.eclipse.ui.IViewPart;
27 import org.eclipse.ui.IWorkbenchPage;
28 import org.eclipse.ui.PartInitException;
29 import org.eclipse.ui.part.ISetSelectionTarget;
30
31 public class ShowInWorkspaceAction extends Action {
32     private String JavaDoc viewId;
33     private ISelectionProvider provider;
34
35     /**
36      * Constructor for ShowInWorkspaceAction.
37      */

38     public ShowInWorkspaceAction(String JavaDoc viewId, ISelectionProvider provider) {
39         this.viewId = viewId;
40         this.provider = provider;
41     }
42
43     /**
44      * Constructor for ShowInWorkspaceAction.
45      * @param text
46      */

47     protected ShowInWorkspaceAction(String JavaDoc text) {
48         super(text);
49     }
50
51     public boolean isApplicable() {
52         IStructuredSelection selection = (IStructuredSelection) provider.getSelection();
53         if (selection.isEmpty())
54             return false;
55         for (Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
56             Object JavaDoc obj = iter.next();
57             if (!(obj instanceof ModelEntry))
58                 return false;
59             ModelEntry entry = (ModelEntry) obj;
60             IPluginModelBase model = entry.getActiveModel();
61             if (model.getUnderlyingResource() == null)
62                 return false;
63         }
64         return true;
65     }
66
67     public void run() {
68         List JavaDoc v = collectResources();
69         IWorkbenchPage page = PDEPlugin.getActivePage();
70         try {
71             IViewPart view = page.showView(viewId);
72             if (view instanceof ISetSelectionTarget) {
73                 ISelection selection = new StructuredSelection(v);
74                 ((ISetSelectionTarget) view).selectReveal(selection);
75             }
76         } catch (PartInitException e) {
77             PDEPlugin.logException(e);
78         }
79     }
80
81     private List JavaDoc collectResources() {
82         ArrayList JavaDoc list = new ArrayList JavaDoc();
83         IStructuredSelection selection = (IStructuredSelection)provider.getSelection();
84         if (selection.isEmpty())
85             return list;
86         for (Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
87             Object JavaDoc obj = iter.next();
88             if (obj instanceof ModelEntry) {
89                 ModelEntry entry = (ModelEntry) obj;
90                 IPluginModelBase model = entry.getActiveModel();
91                 IResource resource = model.getUnderlyingResource();
92                 if (resource != null)
93                     list.add(resource);
94             }
95
96         }
97         return list;
98     }
99 }
100
Popular Tags