1 package no.uio.ifi.refaktor.handlers;
5 import org.eclipse.core.commands.AbstractHandler;
6 import org.eclipse.core.commands.ExecutionEvent;
7 import org.eclipse.core.commands.ExecutionException;
8 import org.eclipse.jdt.core.IMember;
9 import org.eclipse.jdt.core.IType;
10 import org.eclipse.jface.dialogs.MessageDialog;
11 import org.eclipse.jface.viewers.ISelection;
12 import org.eclipse.jface.viewers.ITreeSelection;
13 import org.eclipse.ui.IWorkbenchWindow;
14 import org.eclipse.ui.handlers.HandlerUtil;
16 public class TreeSelectionInfoHandler extends AbstractHandler {
19 public Object execute(ExecutionEvent event) throws ExecutionException {
20 ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
21 IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
22 String title = "Tree Selection Info";
24 if (!(selection instanceof ITreeSelection)) {
25 MessageDialog.openInformation(window.getShell(), title, "Selection is not of type " + ITreeSelection.class.getCanonicalName());
29 ITreeSelection treeSelection = (ITreeSelection) selection;
30 String message = "Selection type: " + treeSelection.getClass().getCanonicalName()
31 + "\n\nType of selected element: " + treeSelection.getFirstElement().getClass().getCanonicalName()
32 + "\nInterface type(s) for element:\n" + getAdditionalTypeInfoForElement(treeSelection.getFirstElement());
34 List<?> elementList = treeSelection.toList();
35 if (elementList.size() > 1)
36 message += "\n\nElements:";
38 for (Object element: elementList) {
39 message += "\n\nElement: " + element;
40 if (element instanceof IMember) {
41 message += "\nElement name: '" + ((IMember) element).getElementName() + "'";
42 IType declaringType = ((IMember) element).getDeclaringType();
43 if (declaringType != null)
44 message += "\nElement declared in type: " + declaringType.getFullyQualifiedName();
46 if (element instanceof IType)
47 message += "\nElement fully qualified name: " + ((IType) element).getFullyQualifiedName();
51 MessageDialog.openInformation(window.getShell(), title, message);
55 private String getAdditionalTypeInfoForElement(Object element) {
56 String interfaces = "";
57 for (Class<?> iface: element.getClass().getInterfaces())
58 interfaces += iface.getCanonicalName() + "\n";