]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/callhierarchy/CallHierarchyViewer.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / callhierarchy / CallHierarchyViewer.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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  *   Jesper Kamstrup Linnet (eclipse@kamstrup-linnet.dk) - initial API and implementation
10  *                      (report 36180: Callers/Callees view)
11  *******************************************************************************/
12 package org.eclipse.jdt.internal.ui.callhierarchy;
13
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.KeyListener;
16 import org.eclipse.swt.events.TreeEvent;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Menu;
20 import org.eclipse.swt.widgets.Tree;
21
22 import org.eclipse.jface.action.IMenuListener;
23 import org.eclipse.jface.action.MenuManager;
24 import org.eclipse.jface.viewers.ISelectionProvider;
25 import org.eclipse.jface.viewers.StructuredSelection;
26 import org.eclipse.jface.viewers.TreeViewer;
27
28 import org.eclipse.ui.IWorkbenchPartSite;
29
30 import org.eclipse.jdt.internal.corext.callhierarchy.CallerMethodWrapper;
31 import org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper;
32
33 import org.eclipse.jdt.internal.ui.viewsupport.ColoringLabelProvider;
34
35
36 class CallHierarchyViewer extends TreeViewer {
37
38         private final CallHierarchyViewPart fPart;
39         private final CallHierarchyContentProvider fContentProvider;
40
41         private CallerMethodWrapper fConstructorToExpand;
42
43         private TreeRoot fDummyRoot;
44
45     /**
46      * @param parent the parent composite
47      * @param part the call hierarchy view part
48      */
49     CallHierarchyViewer(Composite parent, CallHierarchyViewPart part) {
50         super(new Tree(parent, SWT.MULTI));
51
52         fPart = part;
53
54         getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
55         setUseHashlookup(true);
56         setAutoExpandLevel(2);
57         fContentProvider = new CallHierarchyContentProvider(fPart);
58         setContentProvider(fContentProvider);
59         setLabelProvider(new ColoringLabelProvider(new CallHierarchyLabelProvider()));
60
61
62         clearViewer();
63     }
64
65     void setMethodWrappers(MethodWrapper[] wrappers) {
66         setInput(getTreeRoot(wrappers));
67
68         setFocus();
69         if (wrappers != null && wrappers.length > 0)
70                 setSelection(new StructuredSelection(wrappers[0]), true);
71                 expandConstructorNode();
72     }
73
74     CallHierarchyViewPart getPart() {
75         return fPart;
76     }
77
78     /**
79      *
80      */
81     void setFocus() {
82         getControl().setFocus();
83     }
84
85     boolean isInFocus() {
86         return getControl().isFocusControl();
87     }
88
89     void addKeyListener(KeyListener keyListener) {
90         getControl().addKeyListener(keyListener);
91     }
92
93     /**
94      * Wraps the roots of a MethodWrapper tree in a dummy root in order to show
95      * it in the tree.
96      *
97      * @param roots The visible roots of the MethodWrapper tree.
98      * @return A new TreeRoot which is a dummy root above the specified root.
99      */
100     private TreeRoot getTreeRoot(MethodWrapper[] roots) {
101                 return getTreeRoot(roots, false);
102         }
103
104
105         /**
106          * Wraps the roots of a MethodWrapper tree in a dummy root in order to show it in the tree.
107          * 
108          * @param roots The visible roots of the MethodWrapper tree.
109          * @param addRoots <code>true</code> if the roots need to be added to the existing roots,
110          *            <code>false</code> otherwise
111          * @return a new TreeRoot which is a dummy root above the specified root
112          * @since 3.7
113          */
114         TreeRoot getTreeRoot(MethodWrapper[] roots, boolean addRoots) {
115                 if (fDummyRoot == null || !addRoots)
116                         fDummyRoot= new TreeRoot(roots);
117                 else
118                         fDummyRoot.addRoots(roots);
119
120                 return fDummyRoot;
121         }
122
123     /**
124      * Attaches a context menu listener to the tree
125      * @param menuListener the menu listener
126      * @param viewSite the view site
127      * @param selectionProvider the selection provider
128      */
129     void initContextMenu(IMenuListener menuListener, IWorkbenchPartSite viewSite, ISelectionProvider selectionProvider) {
130         MenuManager menuMgr= new MenuManager();
131         menuMgr.setRemoveAllWhenShown(true);
132         menuMgr.addMenuListener(menuListener);
133         Menu menu= menuMgr.createContextMenu(getTree());
134         getTree().setMenu(menu);
135         viewSite.registerContextMenu(menuMgr, selectionProvider);
136     }
137
138     void clearViewer() {
139         setInput(TreeRoot.EMPTY_ROOT);
140                 fDummyRoot= null;
141     }
142
143     void cancelJobs() {
144         if (fPart == null)
145                 return;
146         fContentProvider.cancelJobs(fPart.getCurrentMethodWrappers());
147     }
148
149         /**
150          * {@inheritDoc}
151          * 
152          * @since 3.7
153          */
154         @Override
155         protected Object[] getSortedChildren(Object parentElementOrTreePath) {
156                 Object[] sortedChildren= super.getSortedChildren(parentElementOrTreePath);
157                 if (parentElementOrTreePath instanceof CallerMethodWrapper) {
158                         CallerMethodWrapper parentWrapper= (CallerMethodWrapper)parentElementOrTreePath;
159                         if (parentWrapper.getExpandWithConstructors() && sortedChildren.length == 2 && sortedChildren[0] instanceof CallerMethodWrapper) {
160                                 setConstructorToExpand((CallerMethodWrapper)sortedChildren[0]);
161                         }
162                 }
163                 return sortedChildren;
164         }
165
166
167         /**
168          * {@inheritDoc}
169          * 
170          * @since 3.7
171          */
172         @Override
173         protected void handleTreeExpand(TreeEvent event) {
174                 super.handleTreeExpand(event);
175                 expandConstructorNode();
176         }
177
178         /**
179          * Sets the constructor node.
180          * 
181          * @param wrapper the constructor caller method wrapper
182          * @since 3.7
183          */
184         private void setConstructorToExpand(CallerMethodWrapper wrapper) {
185                 fConstructorToExpand= wrapper;
186                 
187         }
188
189         /**
190          * Expands the constructor node when in expand with constructors mode.
191          * 
192          * @since 3.7
193          */
194         void expandConstructorNode() {
195                 if (fConstructorToExpand != null) {
196                         setExpandedState(fConstructorToExpand, true);
197                         fConstructorToExpand= null;
198                 }
199         }
200 }