Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 123198 | Differences between
and this patch

Collapse All | Expand All

(-)plugin.properties (+11 lines)
Lines 951-956 Link Here
951
ViewCommand.callHierarchy.name= Java Call Hierarchy
951
ViewCommand.callHierarchy.name= Java Call Hierarchy
952
ViewCommand.callHierarchy.description= Show the Call Hierarchy view
952
ViewCommand.callHierarchy.description= Show the Call Hierarchy view
953
953
954
#--- Commands for surfacing java elements
955
command.openElementInEditor.name= Open Java Element
956
command.openElementInEditor.desc= Open a Java element in its editor
957
commandParameter.openElementInEditor.elementRef.name= Java element reference
958
command.showElementInPackageView.name= Show Java Element in Package Explorer
959
command.showElementInPackageView.desc= Select Java element in the Package Explorer view
960
commandParameter.showElementInPackageView.elementRef.name= Java element reference
961
command.showElementInTypeHierarchyView.name= Show Java Element Type Hierarchy
962
command.showElementInTypeHierarchyView.desc= Show a Java element in the Type Hierarchy view
963
commandParameter.showElementInTypeHierarchyView.elementRef.name= Java element reference
964
954
#--- Call Hierarchy
965
#--- Call Hierarchy
955
callHierarchyViewName=Call Hierarchy
966
callHierarchyViewName=Call Hierarchy
956
967
(-)plugin.xml (+45 lines)
Lines 314-319 Link Here
314
		</command>
314
		</command>
315
	</extension>
315
	</extension>
316
	
316
	
317
	<!-- commands to surface java elements given handle id encoded parameter -->
318
	<extension
319
		point="org.eclipse.ui.commands">
320
		<commandParameterType
321
			converter="org.eclipse.jdt.internal.ui.commands.JavaElementReferenceConverter"
322
			id="org.eclipse.jdt.ui.commands.javaElementReference"
323
			type="org.eclipse.jdt.core.IJavaElement"/>
324
		<command
325
			categoryId="org.eclipse.ui.category.navigate"
326
			defaultHandler="org.eclipse.jdt.internal.ui.commands.OpenElementInEditorHandler"
327
			description="%command.openElementInEditor.desc"
328
			id="org.eclipse.jdt.ui.commands.openElementInEditor"
329
			name="%command.openElementInEditor.name">
330
			<commandParameter
331
				id="elementRef"
332
				name="%commandParameter.openElementInEditor.elementRef.name"
333
				optional="false"
334
				typeId="org.eclipse.jdt.ui.commands.javaElementReference"/>
335
		</command>
336
		<command
337
			categoryId="org.eclipse.ui.category.navigate"
338
			defaultHandler="org.eclipse.jdt.internal.ui.commands.ShowElementInPackageViewHandler"
339
			description="%command.showElementInPackageView.desc"
340
			id="org.eclipse.jdt.ui.commands.showElementInPackageView"
341
			name="%command.showElementInPackageView.name">
342
			<commandParameter
343
				id="elementRef"
344
				name="%commandParameter.showElementInPackageView.elementRef.name"
345
				optional="false"
346
				typeId="org.eclipse.jdt.ui.commands.javaElementReference"/>
347
		</command>
348
		<command
349
			categoryId="org.eclipse.ui.category.navigate"
350
			defaultHandler="org.eclipse.jdt.internal.ui.commands.ShowElementInTypeHierarchyViewHandler"
351
			description="%command.showElementInTypeHierarchyView.desc"
352
			id="org.eclipse.jdt.ui.commands.showElementInTypeHierarchyView"
353
			name="%command.showElementInTypeHierarchyView.name">
354
			<commandParameter
355
				id="elementRef"
356
				name="%commandParameter.showElementInTypeHierarchyView.elementRef.name"
357
				optional="false"
358
				typeId="org.eclipse.jdt.ui.commands.javaElementReference"/>
359
		</command>
360
	</extension>
361
	
317
   <!-- Note: Do not change the sequence of those hover contributions -->
362
   <!-- Note: Do not change the sequence of those hover contributions -->
318
   <extension
363
   <extension
319
         point="org.eclipse.jdt.ui.javaEditorTextHovers">
364
         point="org.eclipse.jdt.ui.javaEditorTextHovers">
(-)ui/org/eclipse/jdt/internal/ui/commands/ShowElementInPackageViewHandler.java (+42 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005 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.commands;
12
13
import org.eclipse.core.commands.AbstractHandler;
14
import org.eclipse.core.commands.ExecutionEvent;
15
import org.eclipse.core.commands.ExecutionException;
16
17
import org.eclipse.jdt.core.IJavaElement;
18
19
import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart;
20
21
/**
22
 * A command handler to show a java element in the package view.
23
 * 
24
 * @since 3.2
25
 */
26
public class ShowElementInPackageViewHandler extends AbstractHandler {
27
28
	private static final String PARAM_ID_ELEMENT_REF = "elementRef"; //$NON-NLS-1$
29
30
	public Object execute(ExecutionEvent event) throws ExecutionException {
31
32
		IJavaElement javaElement = (IJavaElement) event
33
				.getObjectParameterForExecution(PARAM_ID_ELEMENT_REF);
34
35
		PackageExplorerPart view = PackageExplorerPart
36
				.openInActivePerspective();
37
		view.tryToReveal(javaElement);
38
39
		return null;
40
	}
41
42
}
(-)ui/org/eclipse/jdt/internal/ui/commands/JavaElementReferenceConverter.java (+211 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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.jdt.internal.ui.commands;
12
13
import org.eclipse.core.commands.AbstractParameterValueConverter;
14
import org.eclipse.core.commands.ParameterValueConversionException;
15
16
import org.eclipse.core.resources.ResourcesPlugin;
17
18
import org.eclipse.jdt.core.IField;
19
import org.eclipse.jdt.core.IJavaElement;
20
import org.eclipse.jdt.core.IJavaModel;
21
import org.eclipse.jdt.core.IJavaProject;
22
import org.eclipse.jdt.core.IMethod;
23
import org.eclipse.jdt.core.IType;
24
import org.eclipse.jdt.core.JavaCore;
25
import org.eclipse.jdt.core.JavaModelException;
26
import org.eclipse.jdt.core.Signature;
27
28
/**
29
 * A command parameter value converter to convert between Java elements and
30
 * String references that identify them.
31
 * <p>
32
 * References can be made to Java types, methods and fields. The reference
33
 * identifies the project to use as a search scope as well as the java element
34
 * information. Note that non-source elements may be referenced (such as
35
 * java.lang.Object), but they must be resolved within the scope of some
36
 * project.
37
 * </p>
38
 * <p>
39
 * References take the form:
40
 * 
41
 * <pre>
42
 *        elementRef := typeRef | fieldRef | methodRef
43
 *        typeRef := projectName '/' fullyQualifiedTypeName
44
 *        fieldRef := typeRef '#' fieldName
45
 *        methodRef := typeRef '#' methodName '(' parameterSignatures ')'
46
 * </pre>
47
 * 
48
 * where <code>parameterSignatures</code> uses the signature format documented
49
 * in the {@link org.eclipse.jdt.core.Signature Signature} class.
50
 * </p>
51
 * 
52
 * @since 3.2
53
 */
54
public class JavaElementReferenceConverter extends
55
		AbstractParameterValueConverter {
56
57
	private static final char PROJECT_END_CHAR = '/';
58
59
	private static final char TYPE_END_CHAR = '#';
60
61
	private static final char PARAM_START_CHAR = Signature.C_PARAM_START;
62
63
	private static final char PARAM_END_CHAR = Signature.C_PARAM_END;
64
65
	public Object convertToObject(String parameterValue)
66
			throws ParameterValueConversionException {
67
68
		assertWellFormed(parameterValue != null);
69
70
		final int projectEndPosition = parameterValue.indexOf(PROJECT_END_CHAR);
71
		assertWellFormed(projectEndPosition != -1);
72
73
		String projectName = parameterValue.substring(0, projectEndPosition);
74
		String javaElementRef = parameterValue
75
				.substring(projectEndPosition + 1);
76
77
		IJavaModel javaModel = JavaCore.create(ResourcesPlugin.getWorkspace()
78
				.getRoot());
79
		assertExists(javaModel);
80
81
		IJavaProject javaProject = javaModel.getJavaProject(projectName);
82
		assertExists(javaProject);
83
84
		final int typeEndPosition = javaElementRef.indexOf(TYPE_END_CHAR);
85
		String typeName;
86
		if (typeEndPosition == -1) {
87
			typeName = javaElementRef;
88
		} else {
89
			typeName = javaElementRef.substring(0, typeEndPosition);
90
		}
91
92
		IType type = null;
93
		try {
94
			type = javaProject.findType(typeName);
95
		} catch (JavaModelException ex) {
96
			// type == null
97
		}
98
		assertExists(type);
99
100
		if (typeEndPosition == -1) {
101
			return type;
102
		}
103
104
		String memberRef = javaElementRef.substring(typeEndPosition + 1);
105
106
		final int paramStartPosition = memberRef.indexOf(PARAM_START_CHAR);
107
		if (paramStartPosition == -1) {
108
			IField field = type.getField(memberRef);
109
			assertExists(field);
110
			return field;
111
		}
112
		String methodName = memberRef.substring(0, paramStartPosition);
113
		String signature = memberRef.substring(paramStartPosition);
114
		String[] parameterTypes = null;
115
		try {
116
			parameterTypes = Signature.getParameterTypes(signature);
117
		} catch (IllegalArgumentException ex) {
118
			// parameterTypes == null
119
		}
120
		assertWellFormed(parameterTypes != null);
121
		IMethod method = type.getMethod(methodName, parameterTypes);
122
		assertExists(method);
123
		return method;
124
	}
125
126
	/**
127
	 * Throws a <code>ParameterValueConversionException</code> if the java
128
	 * element reference string does not meet some well-formedness condition.
129
	 * 
130
	 * @param assertion
131
	 *            a boolean check for well-formedness
132
	 * @throws ParameterValueConversionException
133
	 */
134
	private void assertWellFormed(boolean assertion)
135
			throws ParameterValueConversionException {
136
		if (!assertion) {
137
			throw new ParameterValueConversionException(
138
					"Malformed parameterValue"); //$NON-NLS-1$
139
		}
140
	}
141
142
	/**
143
	 * Throws a <code>ParameterValueConversionException</code> if the java
144
	 * element reference string identifes an element that does not exist.
145
	 * 
146
	 * @param javaElement
147
	 *            an element to check for existence
148
	 * @throws ParameterValueConversionException
149
	 */
150
	private void assertExists(IJavaElement javaElement)
151
			throws ParameterValueConversionException {
152
		if ((javaElement == null) || (!javaElement.exists())) {
153
			throw new ParameterValueConversionException(
154
					"parameterValue must reference an existing IJavaElement"); //$NON-NLS-1$
155
		}
156
	}
157
158
	public String convertToString(Object parameterValue)
159
			throws ParameterValueConversionException {
160
161
		if (!(parameterValue instanceof IJavaElement)) {
162
			throw new ParameterValueConversionException(
163
					"parameterValue must be an IJavaElement"); //$NON-NLS-1$
164
		}
165
166
		IJavaElement javaElement = (IJavaElement) parameterValue;
167
168
		IJavaProject javaProject = javaElement.getJavaProject();
169
		if (javaProject == null) {
170
			throw new ParameterValueConversionException(
171
					"Could not get IJavaProject for element"); //$NON-NLS-1$
172
		}
173
174
		StringBuffer buffer;
175
176
		if (javaElement instanceof IType) {
177
			IType type = (IType) javaElement;
178
			buffer = composeTypeReference(type);
179
		} else if (javaElement instanceof IMethod) {
180
			IMethod method = (IMethod) javaElement;
181
			buffer = composeTypeReference(method.getDeclaringType());
182
			buffer.append(TYPE_END_CHAR);
183
			buffer.append(method.getElementName());
184
			String[] parameterTypes = method.getParameterTypes();
185
			buffer.append(PARAM_START_CHAR);
186
			for (int i = 0; i < parameterTypes.length; i++) {
187
				buffer.append(parameterTypes[i]);
188
			}
189
			buffer.append(PARAM_END_CHAR);
190
		} else if (javaElement instanceof IField) {
191
			IField field = (IField) javaElement;
192
			buffer = composeTypeReference(field.getDeclaringType());
193
			buffer.append(TYPE_END_CHAR);
194
			buffer.append(field.getElementName());
195
		} else {
196
			throw new ParameterValueConversionException(
197
					"Unsupported IJavaElement type"); //$NON-NLS-1$
198
		}
199
200
		return buffer.toString();
201
	}
202
203
	private StringBuffer composeTypeReference(IType type) {
204
		StringBuffer buffer = new StringBuffer();
205
		buffer.append(type.getJavaProject().getElementName());
206
		buffer.append(PROJECT_END_CHAR);
207
		buffer.append(type.getFullyQualifiedName());
208
		return buffer;
209
	}
210
211
}
(-)ui/org/eclipse/jdt/internal/ui/commands/OpenElementInEditorHandler.java (+53 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005 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.commands;
12
13
import org.eclipse.core.commands.AbstractHandler;
14
import org.eclipse.core.commands.ExecutionEvent;
15
import org.eclipse.core.commands.ExecutionException;
16
17
import org.eclipse.ui.IEditorPart;
18
import org.eclipse.ui.PartInitException;
19
20
import org.eclipse.jdt.core.IJavaElement;
21
import org.eclipse.jdt.core.JavaModelException;
22
23
import org.eclipse.jdt.ui.JavaUI;
24
25
/**
26
 * A command handler to open a java element in its editor.
27
 * 
28
 * @since 3.2
29
 */
30
public class OpenElementInEditorHandler extends AbstractHandler {
31
32
	private static final String PARAM_ID_ELEMENT_REF = "elementRef"; //$NON-NLS-1$
33
34
	public Object execute(ExecutionEvent event) throws ExecutionException {
35
36
		IJavaElement javaElement = (IJavaElement) event
37
				.getObjectParameterForExecution(PARAM_ID_ELEMENT_REF);
38
39
		try {
40
			IEditorPart editorPart = JavaUI.openInEditor(javaElement);
41
			JavaUI.revealInEditor(editorPart, javaElement);
42
		} catch (JavaModelException ex) {
43
			throw new ExecutionException(
44
					"Error opening java element in editor", ex); //$NON-NLS-1$
45
		} catch (PartInitException ex) {
46
			throw new ExecutionException(
47
					"Error opening java element in editor", ex); //$NON-NLS-1$
48
		}
49
50
		return null;
51
	}
52
53
}
(-)ui/org/eclipse/jdt/internal/ui/commands/ShowElementInTypeHierarchyViewHandler.java (+54 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2005 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.commands;
12
13
import org.eclipse.core.commands.AbstractHandler;
14
import org.eclipse.core.commands.ExecutionEvent;
15
import org.eclipse.core.commands.ExecutionException;
16
17
import org.eclipse.ui.IWorkbench;
18
import org.eclipse.ui.IWorkbenchWindow;
19
import org.eclipse.ui.PlatformUI;
20
21
import org.eclipse.jdt.core.IJavaElement;
22
23
import org.eclipse.jdt.internal.ui.util.OpenTypeHierarchyUtil;
24
25
/**
26
 * A command handler to show a java element in the type hierarchy view.
27
 * 
28
 * @since 3.2
29
 */
30
public class ShowElementInTypeHierarchyViewHandler extends AbstractHandler {
31
32
	private static final String PARAM_ID_ELEMENT_REF = "elementRef"; //$NON-NLS-1$
33
34
	public Object execute(ExecutionEvent event) throws ExecutionException {
35
36
		IWorkbenchWindow window = getWorkbenchWindow();
37
		if (window == null)
38
			return null;
39
40
		IJavaElement javaElement = (IJavaElement) event
41
				.getObjectParameterForExecution(PARAM_ID_ELEMENT_REF);
42
43
		OpenTypeHierarchyUtil.open(javaElement, window);
44
45
		return null;
46
	}
47
48
	private IWorkbenchWindow getWorkbenchWindow() {
49
		IWorkbench workbench = PlatformUI.getWorkbench();
50
		IWorkbenchWindow activeWindow = workbench.getActiveWorkbenchWindow();
51
		return activeWindow;
52
	}
53
54
}

Return to bug 123198