Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ee2802593f4c1f38c2633a7e56b9ae41e0fd6c55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*******************************************************************************
 * Copyright (c) 2009 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.internal.callgraph.graphlisteners;

import java.util.List;

import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.linuxtools.internal.callgraph.StapGraph;
import org.eclipse.linuxtools.internal.callgraph.StapNode;
import org.eclipse.linuxtools.internal.callgraph.core.FileFinderOpener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.zest.core.widgets.GraphNode;

public class StapGraphMouseListener implements MouseListener {
	private StapGraph graph;
	private StapGraphMouseMoveListener listener;
	private StapGraphFocusListener focus;
	private StapGraphMouseExitListener exitListener;

	public StapGraphMouseListener(StapGraph g) {
		this.graph = g;
		listener = new StapGraphMouseMoveListener(graph);
		focus = new StapGraphFocusListener(listener);
		exitListener = new StapGraphMouseExitListener(listener);
	}

	@Override
	public void mouseDoubleClick(MouseEvent e) {
		if (e.stateMask == SWT.CONTROL) {
			controlDoubleClick();
			return;
		}

		if (graph.getDrawMode() == StapGraph.CONSTANT_DRAWMODE_RADIAL) {
			StapNode node = getNodeFromSelection();
			if (node == null) {
				return;
			}

			graph.getTreeViewer().collapseToLevel(node.getData(), 0);
			graph.getTreeViewer().expandToLevel(node.getData(), 0);
			graph.getTreeViewer().setSelection(new StructuredSelection(node.getData()));

			int id = node.getData().id;

			graph.scale = 1;
			// Redraw in the current mode with the new id as the center
			// The x,y parameters to draw() are irrelevant for radial mode
			graph.draw(id);

			// Unhighlight the center node and give it a normal colour
			node = graph.getNode(id);
			node.unhighlight();
			if (graph.getNodeData(id).isMarked()) {
				node.setBackgroundColor(StapGraph.CONSTANT_MARKED);
			} else {
				node.setBackgroundColor(graph.DEFAULT_NODE_COLOR);
			}
			return;
		} else {
			StapNode node = getNodeFromSelection();
			if (node == null) {
				return;
			}

			unhighlightall(node);
			graph.setSelection(null);

			// Draw in current modes with 'id' at the top
			int id = node.getData().id;
			graph.draw(id);
		}

		graph.setSelection(null);
	}


	@Override
	public void mouseDown(MouseEvent e) {
		if (graph.getProjectionist() != null) {
			graph.getProjectionist().pause();
		}
		mouseDownEvent(e.x, e.y);
	}

	@Override
	public void mouseUp(MouseEvent e) {
		listener.setStop(true);
		graph.removeMouseMoveListener(listener);
		graph.removeListener(SWT.MouseExit, exitListener);

		List<StapNode> list = graph.getSelection();

		if (list.size() == 1) {
			int id;
			if (list.get(0) != null) {
				id = list.get(0).id;
			} else {
				graph.setSelection(null);
				return;
			}
			graph.setSelection(null);

			// ------------Highlighting
			if (graph.getDrawMode() == StapGraph.CONSTANT_DRAWMODE_TREE
					|| graph.getDrawMode() == StapGraph.CONSTANT_DRAWMODE_LEVEL) {
				for (StapNode n : (List<StapNode>) graph.getNodes()) {
					unhighlightall(n);
				}

				List<Integer> callees = null;

				if (graph.isCollapseMode()) {
					callees = graph.getNodeData(id).collapsedChildren;
				} else {
					callees = graph.getNodeData(id).children;
				}

				for (int subID : callees) {
					if (graph.getNode(subID) != null) {
						graph.getNode(subID).highlight();
					}
				}

				if (graph.getParentNode(id) != null) {
					graph.getParentNode(id).highlight();
				}
				graph.getNode(id).highlight();
				return;
			}

		} else if (list.size() == 0 && ! (graph.getDrawMode() == StapGraph.CONSTANT_DRAWMODE_AGGREGATE)) {
			for (StapNode n : (List<StapNode>) graph.getNodes()) {
				unhighlightall(n);
			}

		}
	}

	private void unhighlightall(StapNode n) {
		int id = n.id;
		List<Integer> callees = null;

		if (graph.isCollapseMode()) {
			callees = graph.getNodeData(id).collapsedChildren;
		} else {
			callees = graph.getNodeData(id).children;
		}
		if (callees == null) {
			return;
		}

		for (int subID : callees) {
			if (graph.getNode(subID) != null) {
				graph.getNode(subID).unhighlight();
			}
		}

		if (graph.getParentNode(id) != null) {
			graph.getParentNode(id).unhighlight();
		}
		n.unhighlight();
	}


	private StapNode getNodeFromSelection() {
		List<GraphNode> stapNodeList = graph.getSelection();
		if (stapNodeList.isEmpty() || stapNodeList.size() != 1) {
			graph.setSelection(null);
			return null;
		}

		StapNode node = null;
		if (stapNodeList.get(0) instanceof StapNode) {
			node = (StapNode) stapNodeList.remove(0);
		} else {
			graph.setSelection(null);
			return null;
		}
		return node;
	}

	private GraphNode getAggregateNodeFromSelection() {
		List<GraphNode> graphNodeList = graph.getSelection();
		if (graphNodeList.isEmpty() || graphNodeList.size() != 1) {
			graph.setSelection(null);
			return null;
		}

		GraphNode node = null;
		if (graphNodeList.get(0) != null) {
			node = graphNodeList.remove(0);
		} else {
			graph.setSelection(null);
			return null;
		}
		return node;
	}

	public void controlDoubleClick() {
		if (graph.getDrawMode() == StapGraph.CONSTANT_DRAWMODE_AGGREGATE) {
			GraphNode node = getAggregateNodeFromSelection();

			if (node == null) {
				return;
			}

			String functionName = (String) node.getData("AGGREGATE_NAME"); //$NON-NLS-1$
			FileFinderOpener.findAndOpen(graph.getProject(), functionName);
			node.unhighlight();
		} else {
			StapNode node = getNodeFromSelection();

			if (node == null) {
				return;
			}

			int caller = node.getData().id;

			if (caller < graph.getFirstUsefulNode()) {
				// The only node that satisfies this condition should be main
				caller = graph.getFirstUsefulNode();
			}
			FileFinderOpener.findAndOpen(graph.getProject(), graph.getNodeData(caller).name);
			node.unhighlight();
		}

		graph.setSelection(null);
	}

	public void mouseDownEvent(int x, int y) {
		List<?> list = graph.getSelection();
		if (list.size() < 1) {
			listener.setPoint(x, y);
			listener.setStop(false);
			graph.addMouseMoveListener(listener);
			graph.addListener(SWT.MouseExit, exitListener);
		}
	}

}

Back to the top