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
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;
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;
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;
28 import org.eclipse.ui.IWorkbenchPartSite;
30 import org.eclipse.jdt.internal.corext.callhierarchy.CallerMethodWrapper;
31 import org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper;
33 import org.eclipse.jdt.internal.ui.viewsupport.ColoringLabelProvider;
36 class CallHierarchyViewer extends TreeViewer {
38 private final CallHierarchyViewPart fPart;
39 private final CallHierarchyContentProvider fContentProvider;
41 private CallerMethodWrapper fConstructorToExpand;
43 private TreeRoot fDummyRoot;
46 * @param parent the parent composite
47 * @param part the call hierarchy view part
49 CallHierarchyViewer(Composite parent, CallHierarchyViewPart part) {
50 super(new Tree(parent, SWT.MULTI));
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()));
65 void setMethodWrappers(MethodWrapper[] wrappers) {
66 setInput(getTreeRoot(wrappers));
69 if (wrappers != null && wrappers.length > 0)
70 setSelection(new StructuredSelection(wrappers[0]), true);
71 expandConstructorNode();
74 CallHierarchyViewPart getPart() {
82 getControl().setFocus();
86 return getControl().isFocusControl();
89 void addKeyListener(KeyListener keyListener) {
90 getControl().addKeyListener(keyListener);
94 * Wraps the roots of a MethodWrapper tree in a dummy root in order to show
97 * @param roots The visible roots of the MethodWrapper tree.
98 * @return A new TreeRoot which is a dummy root above the specified root.
100 private TreeRoot getTreeRoot(MethodWrapper[] roots) {
101 return getTreeRoot(roots, false);
106 * Wraps the roots of a MethodWrapper tree in a dummy root in order to show it in the tree.
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
114 TreeRoot getTreeRoot(MethodWrapper[] roots, boolean addRoots) {
115 if (fDummyRoot == null || !addRoots)
116 fDummyRoot= new TreeRoot(roots);
118 fDummyRoot.addRoots(roots);
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
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);
139 setInput(TreeRoot.EMPTY_ROOT);
146 fContentProvider.cancelJobs(fPart.getCurrentMethodWrappers());
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]);
163 return sortedChildren;
173 protected void handleTreeExpand(TreeEvent event) {
174 super.handleTreeExpand(event);
175 expandConstructorNode();
179 * Sets the constructor node.
181 * @param wrapper the constructor caller method wrapper
184 private void setConstructorToExpand(CallerMethodWrapper wrapper) {
185 fConstructorToExpand= wrapper;
190 * Expands the constructor node when in expand with constructors mode.
194 void expandConstructorNode() {
195 if (fConstructorToExpand != null) {
196 setExpandedState(fConstructorToExpand, true);
197 fConstructorToExpand= null;