-
Notifications
You must be signed in to change notification settings - Fork 82
/
GotoResourceAction.java
124 lines (106 loc) · 4.09 KB
/
GotoResourceAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*******************************************************************************
* Copyright (c) 2000, 2011 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.ui.packageview;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.FilteredResourcesSelectionDialog;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaModel;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
public class GotoResourceAction extends Action {
private PackageExplorerPart fPackageExplorer;
private static class GotoResourceDialog extends FilteredResourcesSelectionDialog {
private IJavaModel fJavaModel;
public GotoResourceDialog(Shell parentShell, IContainer container) {
super(parentShell, false, container, IResource.FILE | IResource.FOLDER | IResource.PROJECT);
fJavaModel= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
setTitle(PackagesMessages.GotoResource_dialog_title);
PlatformUI.getWorkbench().getHelpSystem().setHelp(parentShell, IJavaHelpContextIds.GOTO_RESOURCE_DIALOG);
}
@Override
protected ItemsFilter createFilter() {
return new GotoResourceFilter();
}
private class GotoResourceFilter extends ResourceFilter {
@Override
public boolean matchItem(Object item) {
IResource resource = (IResource) item;
return super.matchItem(item) && select(resource);
}
/**
* This is the orignal <code>select</code> method. Since
* <code>GotoResourceDialog</code> needs to extend
* <code>FilteredResourcesSelectionDialog</code> result of this
* method must be combined with the <code>matchItem</code> method
* from super class (<code>ResourceFilter</code>).
*
* @param resource
* A resource
* @return <code>true</code> if item matches against given
* conditions <code>false</code> otherwise
*/
private boolean select(IResource resource) {
IProject project= resource.getProject();
try {
if (project.getNature(JavaCore.NATURE_ID) != null)
return fJavaModel.contains(resource);
} catch (CoreException e) {
// do nothing. Consider resource;
}
return true;
}
@Override
public boolean equalsFilter(ItemsFilter filter) {
if (!super.equalsFilter(filter)) {
return false;
}
if (!(filter instanceof GotoResourceFilter)) {
return false;
}
return true;
}
}
}
public GotoResourceAction(PackageExplorerPart explorer) {
setText(PackagesMessages.GotoResource_action_label);
PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.GOTO_RESOURCE_ACTION);
fPackageExplorer= explorer;
}
@Override
public void run() {
TreeViewer viewer= fPackageExplorer.getTreeViewer();
GotoResourceDialog dialog= new GotoResourceDialog(fPackageExplorer.getSite().getShell(),
ResourcesPlugin.getWorkspace().getRoot());
dialog.open();
Object[] result = dialog.getResult();
if (result == null || result.length == 0 || !(result[0] instanceof IResource))
return;
StructuredSelection selection= null;
IJavaElement element = JavaCore.create((IResource)result[0]);
if (element != null && element.exists())
selection= new StructuredSelection(element);
else
selection= new StructuredSelection(result[0]);
viewer.setSelection(selection, true);
}
}