KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > packageview > GotoResourceAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.internal.ui.packageview;
12
13 import org.eclipse.core.resources.IContainer;
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.jdt.core.IJavaElement;
19 import org.eclipse.jdt.core.IJavaModel;
20 import org.eclipse.jdt.core.JavaCore;
21 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
22
23 import org.eclipse.jface.action.Action;
24 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.eclipse.jface.viewers.StructuredViewer;
26 import org.eclipse.jface.viewers.TreeViewer;
27 import org.eclipse.swt.widgets.Shell;
28
29 import org.eclipse.ui.dialogs.FilteredResourcesSelectionDialog;
30 import org.eclipse.ui.PlatformUI;
31
32 public class GotoResourceAction extends Action {
33
34     private PackageExplorerPart fPackageExplorer;
35
36     private static class GotoResourceDialog extends FilteredResourcesSelectionDialog {
37         private IJavaModel fJavaModel;
38         public GotoResourceDialog(Shell parentShell, IContainer container, StructuredViewer viewer) {
39             super(parentShell, false, container, IResource.FILE | IResource.FOLDER | IResource.PROJECT);
40             fJavaModel= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
41             setTitle(PackagesMessages.GotoResource_dialog_title);
42             PlatformUI.getWorkbench().getHelpSystem().setHelp(parentShell, IJavaHelpContextIds.GOTO_RESOURCE_DIALOG);
43         }
44         
45         protected ItemsFilter createFilter() {
46             return new GotoResourceFilter();
47         }
48         
49         private class GotoResourceFilter extends ResourceFilter {
50
51             /*
52              * (non-Javadoc)
53              *
54              * @see org.eclipse.ui.dialogs.FilteredResourcesSelectionDialog.ResourceFilter#matchItem(java.lang.Object)
55              */

56             public boolean matchItem(Object JavaDoc item) {
57                 IResource resource = (IResource) item;
58                 return super.matchItem(item) && select(resource);
59             }
60             
61             /**
62              * This is the orignal <code>select</code> method. Since
63              * <code>GotoResourceDialog</code> needs to extend
64              * <code>FilteredResourcesSelectionDialog</code> result of this
65              * method must be combined with the <code>matchItem</code> method
66              * from super class (<code>ResourceFilter</code>).
67              *
68              * @param resource
69              * A resource
70              * @return <code>true</code> if item matches against given
71              * conditions <code>false</code> otherwise
72              */

73             private boolean select(IResource resource) {
74                 IProject project= resource.getProject();
75                 try {
76                     if (project.getNature(JavaCore.NATURE_ID) != null)
77                         return fJavaModel.contains(resource);
78                 } catch (CoreException e) {
79                     // do nothing. Consider resource;
80
}
81                 return true;
82             }
83
84             /*
85              * (non-Javadoc)
86              *
87              * @see org.eclipse.ui.dialogs.FilteredResourcesSelectionDialog.ResourceFilter#equalsFilter(org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter)
88              */

89             public boolean equalsFilter(ItemsFilter filter) {
90                 if (!super.equalsFilter(filter)) {
91                     return false;
92                 }
93                 if (!(filter instanceof GotoResourceFilter)) {
94                     return false;
95                 }
96                 return true;
97             }
98         }
99         
100     }
101
102     public GotoResourceAction(PackageExplorerPart explorer) {
103         setText(PackagesMessages.GotoResource_action_label);
104         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.GOTO_RESOURCE_ACTION);
105         fPackageExplorer= explorer;
106     }
107     
108     public void run() {
109         TreeViewer viewer= fPackageExplorer.getTreeViewer();
110         GotoResourceDialog dialog= new GotoResourceDialog(fPackageExplorer.getSite().getShell(),
111             ResourcesPlugin.getWorkspace().getRoot(), viewer);
112         dialog.open();
113         Object JavaDoc[] result = dialog.getResult();
114         if (result == null || result.length == 0 || !(result[0] instanceof IResource))
115             return;
116         StructuredSelection selection= null;
117         IJavaElement element = JavaCore.create((IResource)result[0]);
118         if (element != null && element.exists())
119             selection= new StructuredSelection(element);
120         else
121             selection= new StructuredSelection(result[0]);
122         viewer.setSelection(selection, true);
123     }
124 }
125
Popular Tags