Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 178b169d4e8870c091a326a09a97c97483ac9a30 (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
/*******************************************************************************
 * Copyright (c) 2006 IBM Corporation.
 * 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:
 *     IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
 *******************************************************************************/

package org.eclipse.linuxtools.systemtap.ui.graphingapi.ui.widgets;

import org.eclipse.linuxtools.systemtap.ui.graphingapi.ui.internal.Localization;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;



/**
 * A legend primitive for the Graphing system. Used to display a list of
 * all the series that are on this graph, and to change the current axis on a
 * normalized multi-series graph.
 * @author Jeff Briggs
 * @author Ryan Morse
 */
public class GraphLegend implements IGraphPrimitive {
	public GraphLegend(GraphCanvas graph, String[] keysPassed, Color[] colorsPassed) {
		this.graph = graph;
		colors = colorsPassed;
		keys = keysPassed;
		bounds = new Rectangle[keys.length];
		
		width = 0;
		height = 0;
	}
	
	public boolean isVisible() {
		return true;
	}
	
	public void calculateBounds() {
		x = graph.getSize().x - width;
		y = 0;
	}
	
	private void getSize(GC gc) {
		textHeight = gc.getFontMetrics().getHeight();
		height = textHeight * (keys.length + 1) + (BORDER<<1);

		for (int i=0; i<TITLE.length(); i++)
			width += gc.getCharWidth(TITLE.charAt(i));
		
		int currWidth;
		for(int i=0; i<keys.length; i++) {
			currWidth = 0;
			for (int j=0; j<keys[i].length(); j++)
				currWidth += gc.getCharWidth(keys[i].charAt(j));

			if (currWidth > width)
				width = currWidth;
		}
		width += BOX_SIZE + 3*BORDER;
	}
	
	public boolean isUnder(Point loc) {
		if(loc.x >=x && loc.y >= y && loc.x <= x+width && loc.y <= y+height)
			return true;
		return false;
	}
	
	public void paint(GC gc) {
		if(width == 0 || height ==0)
			getSize(gc);
		calculateBounds();
		
		Color temp = gc.getForeground();
		gc.setForeground(graph.axisColor); 
		gc.drawRectangle(x, y, width, height);
		gc.fillRectangle(x+1, y+1, width-1, height-1);
		gc.drawText(TITLE, x+BORDER, y+BORDER);
		
		for (int i=0; i<keys.length; i++) {
			gc.setForeground(colors[i]); 

			bounds[i] = new Rectangle(x+BORDER, y+BORDER+((i+1)*textHeight), BOX_SIZE, BOX_SIZE);
			gc.fillGradientRectangle(bounds[i].x, bounds[i].y, BOX_SIZE, BOX_SIZE, true);
			
			gc.setForeground(graph.axisColor);
			gc.drawText(keys[i], x+(BORDER<<1)+BOX_SIZE, bounds[i].y);
		}
		
		gc.setForeground(temp);
	}

	private final GraphCanvas graph;
	private int x, y, width, height, textHeight;
	
	private String[] keys;
	private Color[] colors;
	private Rectangle[] bounds;
	
	private static final int BORDER = 5;
	private static final int BOX_SIZE = 15;
	private static final String TITLE = Localization.getString("GraphLegend.Legend");
}

Back to the top