Skip to main content
summaryrefslogtreecommitdiffstats
blob: a7ecf346c6052967f52892f83d27ba57173059d6 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*******************************************************************************
 * 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.callgraph.graphlisteners;

import java.util.List;

import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.linuxtools.callgraph.StapGraph;
import org.eclipse.linuxtools.callgraph.StapNode;
import org.eclipse.linuxtools.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;

@SuppressWarnings("unused")
public class StapGraphMouseListener implements MouseListener {
	private int x;
	private int y;
	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();
		}
		
		
		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.getData(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);
			graph.getTreeViewer().expandToLevel(node.getData(), 0);
			graph.getTreeViewer().setSelection(new StructuredSelection(node.getData()));
			// 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) {
//		MP.println("You clicked: " + e.x + ", " + e.y); //$NON-NLS-1$ //$NON-NLS-2$
//		MP.println("Convert to control: " + graph.toControl(e.x, e.y).x + ", " //$NON-NLS-1$ //$NON-NLS-2$
//				+ graph.toControl(e.x, e.y).y);
//		MP.println("Convert to display: " + graph.toDisplay(e.x, e.y).x + ", " //$NON-NLS-1$ //$NON-NLS-2$
//				+ graph.toDisplay(e.x, e.y).y);
//		MP.println("Bounds: " + graph.getBounds().width + ", " //$NON-NLS-1$ //$NON-NLS-2$
//				+ graph.getBounds().height);
		mouseDownEvent(e.x, e.y);
	}

	@Override
	public void mouseUp(MouseEvent e) {
			mouseUpEvent();
	}

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

		if (graph.isCollapseMode())
			callees = graph.getData(id).collapsedCallees;
		else
			callees = graph.getData(id).callees;
		for (int subID : callees) {
			if (graph.getNode(subID) != null)
				graph.getNode(subID).unhighlight();
		}

		if (graph.getParentNode(id) != null) {
			graph.getParentNode(id).unhighlight();
		}
		n.unhighlight();
	}
	
	
	@SuppressWarnings("unchecked")
	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;
	}
	
	@SuppressWarnings("unchecked")
	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) instanceof GraphNode) {
			node = (GraphNode) graphNodeList.remove(0);
		} else {
			graph.setSelection(null);
			return null;
		}
		return node;
	}
	
	public String controlDoubleClick() {
		String output = null;
		if (graph.getDrawMode() == StapGraph.CONSTANT_DRAWMODE_AGGREGATE) {
			GraphNode node = getAggregateNodeFromSelection();
			
			if (node == null)
				return null;
			
			String functionName = (String) node.getData("AGGREGATE_NAME"); //$NON-NLS-1$
			output= FileFinderOpener.findAndOpen(graph.getProject(), functionName);
		} else {
			StapNode node = getNodeFromSelection();
			
			if (node == null)
				return null;

			int caller = node.getData().caller;

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

		graph.setSelection(null);
		return output;
	}
	
	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);
		}
	}
	
	@SuppressWarnings("unchecked")
	public void mouseUpEvent() {

		listener.setStop(true);
		graph.removeMouseMoveListener(listener);
		graph.removeListener(SWT.MouseExit, exitListener);

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

		// ------------Debug information
		if (list.size() == 1) {
			int id;
			if (list.get(0) instanceof StapNode)
				id = list.get(0).id;
			else {
				graph.setSelection(null);
				return;
			}
			graph.setSelection(null);
//			MP.println("Clicked node " + graph.getData(id).name + " with id " //$NON-NLS-1$ //$NON-NLS-2$
//					+ id);
//			MP.println("    level: " + graph.getData(id).levelOfRecursion); //$NON-NLS-1$
//			MP.println("    called: " + graph.getData(id).called); //$NON-NLS-1$
//			MP.println("    caller: " + graph.getData(id).caller); //$NON-NLS-1$
//			MP.println("    copllapsedCaller: " //$NON-NLS-1$
//					+ graph.getData(id).collapsedCaller);
//			MP.println("    callees: " + graph.getData(id).callees.size()); //$NON-NLS-1$
//			MP.println("    position: " + graph.getNode(id).getLocation().x //$NON-NLS-1$
//					+ ", " + graph.getNode(id).getLocation().y); //$NON-NLS-1$

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

				List<Integer> callees = null;

				if (graph.isCollapseMode())
					callees = graph.getData(id).collapsedCallees;
				else
					callees = graph.getData(id).callees;

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

				if (graph.getParentNode(id) != null) {
					graph.getParentNode(id).highlight();
				}
//				graph.setSelection(null);
				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);
			}

		}

//		else {
//			for (StapNode n : list) {
//				unhighlightall(n);
//			}
//		}

//		graph.setSelection(null);
	}
};

Back to the top