1 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 56 public boolean matchItem(Object item) { 57 IResource resource = (IResource) item; 58 return super.matchItem(item) && select(resource); 59 } 60 61 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 } 81 return true; 82 } 83 84 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 [] 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 |