|
Lines 12-19
Link Here
|
| 12 |
|
12 |
|
| 13 |
import java.lang.reflect.InvocationTargetException; |
13 |
import java.lang.reflect.InvocationTargetException; |
| 14 |
import java.util.ArrayList; |
14 |
import java.util.ArrayList; |
|
|
15 |
import java.util.HashMap; |
| 15 |
import java.util.Iterator; |
16 |
import java.util.Iterator; |
| 16 |
import java.util.List; |
17 |
import java.util.List; |
|
|
18 |
import java.util.Map; |
| 17 |
|
19 |
|
| 18 |
import org.eclipse.core.runtime.CoreException; |
20 |
import org.eclipse.core.runtime.CoreException; |
| 19 |
import org.eclipse.core.runtime.IStatus; |
21 |
import org.eclipse.core.runtime.IStatus; |
|
Lines 25-30
Link Here
|
| 25 |
import org.eclipse.jface.dialogs.ErrorDialog; |
27 |
import org.eclipse.jface.dialogs.ErrorDialog; |
| 26 |
import org.eclipse.jface.util.OpenStrategy; |
28 |
import org.eclipse.jface.util.OpenStrategy; |
| 27 |
import org.eclipse.jface.viewers.IStructuredSelection; |
29 |
import org.eclipse.jface.viewers.IStructuredSelection; |
|
|
30 |
import org.eclipse.jface.window.Window; |
| 28 |
|
31 |
|
| 29 |
import org.eclipse.jface.text.ITextSelection; |
32 |
import org.eclipse.jface.text.ITextSelection; |
| 30 |
|
33 |
|
|
Lines 32-47
Link Here
|
| 32 |
import org.eclipse.ui.IWorkbenchSite; |
35 |
import org.eclipse.ui.IWorkbenchSite; |
| 33 |
import org.eclipse.ui.PartInitException; |
36 |
import org.eclipse.ui.PartInitException; |
| 34 |
import org.eclipse.ui.PlatformUI; |
37 |
import org.eclipse.ui.PlatformUI; |
|
|
38 |
import org.eclipse.ui.dialogs.ElementListSelectionDialog; |
| 35 |
import org.eclipse.ui.texteditor.IEditorStatusLine; |
39 |
import org.eclipse.ui.texteditor.IEditorStatusLine; |
| 36 |
|
40 |
|
|
|
41 |
import org.eclipse.jdt.core.Flags; |
| 37 |
import org.eclipse.jdt.core.ICompilationUnit; |
42 |
import org.eclipse.jdt.core.ICompilationUnit; |
| 38 |
import org.eclipse.jdt.core.IJavaElement; |
43 |
import org.eclipse.jdt.core.IJavaElement; |
|
|
44 |
import org.eclipse.jdt.core.IMethod; |
| 39 |
import org.eclipse.jdt.core.ISourceReference; |
45 |
import org.eclipse.jdt.core.ISourceReference; |
|
|
46 |
import org.eclipse.jdt.core.IType; |
| 47 |
import org.eclipse.jdt.core.ITypeHierarchy; |
| 40 |
import org.eclipse.jdt.core.JavaModelException; |
48 |
import org.eclipse.jdt.core.JavaModelException; |
| 41 |
|
49 |
|
| 42 |
import org.eclipse.jdt.internal.corext.util.JavaModelUtil; |
50 |
import org.eclipse.jdt.internal.corext.util.JavaModelUtil; |
| 43 |
import org.eclipse.jdt.internal.corext.util.Messages; |
51 |
import org.eclipse.jdt.internal.corext.util.Messages; |
| 44 |
|
52 |
|
|
|
53 |
import org.eclipse.jdt.ui.JavaElementLabelProvider; |
| 45 |
import org.eclipse.jdt.ui.JavaUI; |
54 |
import org.eclipse.jdt.ui.JavaUI; |
| 46 |
|
55 |
|
| 47 |
import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; |
56 |
import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; |
|
Lines 150-156
Link Here
|
| 150 |
if (element == null) |
159 |
if (element == null) |
| 151 |
return; |
160 |
return; |
| 152 |
} |
161 |
} |
|
|
162 |
|
| 163 |
try { |
| 164 |
|
| 165 |
switch(element.getElementType()) { |
| 153 |
|
166 |
|
|
|
167 |
case IJavaElement.METHOD : |
| 168 |
|
| 169 |
// Check parent is interface |
| 170 |
if(element.getParent().getElementType() == IJavaElement.TYPE && |
| 171 |
((IType) element.getParent()).isInterface()) { |
| 172 |
|
| 173 |
// Grab method & interface |
| 174 |
IMethod method = (IMethod) element; |
| 175 |
IType iface = (IType) element.getParent(); |
| 176 |
|
| 177 |
// Create hierarchy & map to store implementing types |
| 178 |
ITypeHierarchy hierarchy = iface.newTypeHierarchy(null); |
| 179 |
Map impTypes = new HashMap(); |
| 180 |
|
| 181 |
// Get *all* sub types |
| 182 |
IType[] subTypes = hierarchy.getAllSubtypes(iface); |
| 183 |
|
| 184 |
for (int i = 0; i < subTypes.length; i++) { |
| 185 |
|
| 186 |
// Attempt to get method (TODO: Is this utility method accurate enough?) |
| 187 |
IMethod currentMethod = JavaModelUtil.findMethod(method.getElementName(), |
| 188 |
method.getParameterTypes(), |
| 189 |
false, |
| 190 |
subTypes[i]); |
| 191 |
// Additionally, check the return types are the same... |
| 192 |
if(currentMethod != null && |
| 193 |
currentMethod.getReturnType().equals(method.getReturnType())) { |
| 194 |
|
| 195 |
// Check it's not been overriden in a sub interface or |
| 196 |
// redeclared as abstract somewhere |
| 197 |
if(!subTypes[i].isInterface() && |
| 198 |
!Flags.isAbstract(currentMethod.getFlags()) && |
| 199 |
!impTypes.containsKey(subTypes[i])) { |
| 200 |
|
| 201 |
impTypes.put(subTypes[i], currentMethod); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
// Prompt user for choice |
| 207 |
IType[] types = (IType[]) impTypes.keySet().toArray(new IType[impTypes.size()-1]); |
| 208 |
IType selectedImpl = selectImplementingType(types, method.getElementName()); |
| 209 |
|
| 210 |
// Set type to open |
| 211 |
if(selectedImpl != null) { |
| 212 |
element = (IJavaElement) impTypes.get(selectedImpl); |
| 213 |
} |
| 214 |
} |
| 215 |
break; |
| 216 |
|
| 217 |
case IJavaElement.TYPE : |
| 218 |
|
| 219 |
// Check its an interface |
| 220 |
if(((IType) element).isInterface()) { |
| 221 |
|
| 222 |
IType iface = (IType) element; |
| 223 |
|
| 224 |
// Create hierarchy & list for implementing classes |
| 225 |
ITypeHierarchy hierarchy = iface.newTypeHierarchy(null); // TODO : Should use ProgressMonitor |
| 226 |
List implTypes = new ArrayList(); |
| 227 |
|
| 228 |
// Add only direct subtypes which are classes (no extending interfaces) |
| 229 |
IType[] subTypes = hierarchy.getSubtypes(iface); |
| 230 |
for (int i= 0; i < subTypes.length; i++) { |
| 231 |
if(subTypes[i].isClass()) { |
| 232 |
implTypes.add(subTypes[i]); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
// Prompt user for selection |
| 237 |
IType[] types = (IType[]) implTypes.toArray(new IType[implTypes.size()-1]); |
| 238 |
IType selectedImpl = selectImplementingType(types, |
| 239 |
iface.getElementName()); |
| 240 |
|
| 241 |
// Set type to open |
| 242 |
if(selectedImpl != null) { |
| 243 |
element = selectedImpl; |
| 244 |
} |
| 245 |
} |
| 246 |
break; |
| 247 |
} |
| 248 |
} catch(JavaModelException jme) { /*...*/ } |
| 249 |
|
| 154 |
run(new Object[] {element} ); |
250 |
run(new Object[] {element} ); |
| 155 |
} catch (InvocationTargetException e) { |
251 |
} catch (InvocationTargetException e) { |
| 156 |
ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.OpenAction_error_message); |
252 |
ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.OpenAction_error_message); |
|
Lines 158-164
Link Here
|
| 158 |
// ignore |
254 |
// ignore |
| 159 |
} |
255 |
} |
| 160 |
} |
256 |
} |
|
|
257 |
|
| 258 |
private IType selectImplementingType(IType[] types, String elementName) { |
| 161 |
|
259 |
|
|
|
260 |
// Check basic |
| 261 |
if(types.length == 0) return null; |
| 262 |
if(types.length == 1) return types[0]; |
| 263 |
|
| 264 |
// Prompt user to choose |
| 265 |
int flags = JavaElementLabelProvider.SHOW_DEFAULT | JavaElementLabelProvider.SHOW_POST_QUALIFIED; |
| 266 |
ElementListSelectionDialog dialog= new ElementListSelectionDialog(fEditor.getSite().getShell(), |
| 267 |
new JavaElementLabelProvider(flags)); |
| 268 |
dialog.setTitle("Open Implementation"); //$NON-NLS-1$ |
| 269 |
dialog.setMessage("Choose an implementation of: " + elementName); //$NON-NLS-1$ |
| 270 |
dialog.setElements(types); |
| 271 |
|
| 272 |
// Prompt user to select type |
| 273 |
if (dialog.open() == Window.OK) { |
| 274 |
return (IType) dialog.getFirstResult(); |
| 275 |
} |
| 276 |
return null; |
| 277 |
} |
| 162 |
/** |
278 |
/** |
| 163 |
* Selects the openable elements out of the given ones. |
279 |
* Selects the openable elements out of the given ones. |
| 164 |
* |
280 |
* |