]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/refaktor-before/src/no/uio/ifi/refaktor/handlers/TreeSelectionInfoHandler.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / refaktor-before / src / no / uio / ifi / refaktor / handlers / TreeSelectionInfoHandler.java
1 package no.uio.ifi.refaktor.handlers;
2
3 import java.util.List;
4
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;
15
16 public class TreeSelectionInfoHandler extends AbstractHandler {
17
18         @Override
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";
23
24                 if (!(selection instanceof ITreeSelection)) {
25                         MessageDialog.openInformation(window.getShell(), title, "Selection is not of type " + ITreeSelection.class.getCanonicalName());
26                         return null;
27                 }
28
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());
33
34                 List<?> elementList = treeSelection.toList();
35                 if (elementList.size() > 1)
36                         message += "\n\nElements:";
37
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();
45
46                                 if (element instanceof IType)
47                                         message += "\nElement fully qualified name: " + ((IType) element).getFullyQualifiedName();
48                         }
49                 }
50
51                 MessageDialog.openInformation(window.getShell(), title, message);
52                 return null;
53         }
54
55         private String getAdditionalTypeInfoForElement(Object element) {
56                 String interfaces = "";
57                 for (Class<?> iface: element.getClass().getInterfaces())
58                         interfaces += iface.getCanonicalName() + "\n";
59                 return interfaces;
60         }
61
62 }