-
Notifications
You must be signed in to change notification settings - Fork 576
Expand file tree
/
Copy pathLegend.java
More file actions
252 lines (228 loc) · 8.43 KB
/
Legend.java
File metadata and controls
252 lines (228 loc) · 8.43 KB
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
/*******************************************************************************
* Copyright (c) 2010 Oak Ridge National Laboratory.
* 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
******************************************************************************/
package org.csstudio.swt.xygraph.figures;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import org.csstudio.swt.xygraph.Preferences;
import org.csstudio.swt.xygraph.figures.Trace.TraceType;
import org.csstudio.swt.xygraph.util.XYGraphMediaFactory;
import scouter.client.util.ColorUtil;
import org.eclipse.draw2d.FigureUtilities;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
/**The legend to indicate the style and size of the trace line and point.
* The border color of the legend is same as the traces' Y-Axis color.
* @author Xihui Chen
*
*/
public class Legend extends RectangleFigure {
private final static int ICON_WIDTH = 15;//Edited by scouter.project@gmail.com 25->15;
private final static int INNER_GAP = 2;
private final static int OUT_GAP = 5;
// private final static Font LEGEND_FONT = XYGraphMediaFactory.getInstance().getFont(
// XYGraphMediaFactory.FONT_ARIAL);
//
// private final Color WHITE_COLOR = XYGraphMediaFactory.getInstance().getColor(
// XYGraphMediaFactory.COLOR_WHITE);
private final Color BLACK_COLOR = XYGraphMediaFactory.getInstance().getColor(
XYGraphMediaFactory.COLOR_BLACK);
private final List<Trace> traceList = new ArrayList<Trace>();
public Legend(XYGraph xyGraph) {
// setFont(LEGEND_FONT);
xyGraph.getPlotArea().addPropertyChangeListener(PlotArea.BACKGROUND_COLOR, new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
setBackgroundColor((Color) evt.getNewValue());
}
});
setBackgroundColor(xyGraph.getPlotArea().getBackgroundColor());
setForegroundColor(ColorUtil.getChartForeground());
setOpaque(false);
setOutline(false); // Edited by scouter.project true -> false
addListenerToBoldline(); // Added by scouter.project
}
/**Add a trace to the axis.
* @param trace the trace to be added.
*/
public void addTrace(Trace trace){
traceList.add(trace);
}
/**Remove a trace from the axis.
* @param trace
* @return true if this axis contained the specified trace
*/
public boolean removeTrace(Trace trace){
return traceList.remove(trace);
}
@Override
protected void outlineShape(Graphics graphics) {
graphics.setForegroundColor(traceList.get(0).getYAxis().getForegroundColor());
super.outlineShape(graphics);
}
@Override
protected void fillShape(Graphics graphics) {
if(!((XYGraph)getParent()).isTransparent())
super.fillShape(graphics);
int hPos = bounds.x +INNER_GAP;
int vPos = bounds.y - INNER_GAP; // up little
int i = 0;
// Added by scouter.project
positionList.clear();
for(Trace trace : traceList){
int hwidth = OUT_GAP + ICON_WIDTH + INNER_GAP +
+ FigureUtilities.getTextExtents(trace.getName(), getFont()).width;
int hEnd = hPos + hwidth;
if(hEnd > (bounds.x + bounds.width) && i>0){
hPos= bounds.x + INNER_GAP;
vPos += ICON_WIDTH + INNER_GAP;
hEnd = hPos + hwidth;
}
// graphics.setForegroundColor(trace.getYAxis().getForegroundColor());
// Rectangle rect = new Rectangle(hPos, vPos-INNER_GAP/2, hwidth - OUT_GAP,ICON_WIDTH-INNER_GAP);
// graphics.fillRectangle(rect);
// graphics.drawRectangle(rect);
drawTraceLagend(trace, graphics, hPos, vPos);
hPos = hEnd;
i++;
}
}
private void drawTraceLagend(Trace trace, Graphics graphics, int hPos, int vPos){
graphics.pushState();
if (Preferences.useAdvancedGraphics())
graphics.setAntialias(SWT.ON);
graphics.setForegroundColor(trace.getTraceColor());
///********************************** Removed by scouter.project
// draw symbol
// switch (trace.getTraceType()) {
// case BAR:
// trace.drawLine(graphics, new Point(hPos + ICON_WIDTH / 2, vPos + trace.getPointSize() / 2), new Point(hPos + ICON_WIDTH / 2, vPos + ICON_WIDTH));
// trace.drawPoint(graphics, new Point(hPos + ICON_WIDTH / 2, vPos + trace.getPointSize() / 2));
// break;
// case AREA:
// graphics.setBackgroundColor(trace.getTraceColor());
// if (Preferences.useAdvancedGraphics())
// graphics.setAlpha(trace.getAreaAlpha());
// graphics.fillPolygon(new int[] { hPos, vPos + ICON_WIDTH / 2, hPos + ICON_WIDTH / 2, vPos + trace.getPointSize() / 2, hPos + ICON_WIDTH, vPos + ICON_WIDTH / 2, hPos + ICON_WIDTH,
// vPos + ICON_WIDTH, hPos, vPos + ICON_WIDTH });
// if (Preferences.useAdvancedGraphics())
// graphics.setAlpha(255);
// trace.drawPoint(graphics, new Point(hPos + ICON_WIDTH / 2, vPos + trace.getPointSize() / 2));
// break;
// default:
// trace.drawLine(graphics, new Point(hPos, vPos + ICON_WIDTH / 2), new Point(hPos + ICON_WIDTH, vPos + ICON_WIDTH / 2));
// trace.drawPoint(graphics, new Point(hPos + ICON_WIDTH / 2, vPos + ICON_WIDTH / 2));
// break;
// }
//**************************************/
// Added by scouter.project
// Draw rectangle and name
graphics.setBackgroundColor(trace.getTraceColor());
int rectangleX = hPos+ INNER_GAP*2;
int rectangleY = vPos+ INNER_GAP;
graphics.fillRectangle(rectangleX, rectangleY ,10 ,10 );
int textX = hPos + ICON_WIDTH + INNER_GAP;
int textY = vPos + ICON_WIDTH / 2 - FigureUtilities.getTextExtents(trace.getName(), getFont()).height / 2;
graphics.drawText(trace.getName(), textX, textY);
int x2 = textX + FigureUtilities.getTextExtents(trace.getName(), getFont()).width;
int y2 = textY + FigureUtilities.getTextExtents(trace.getName(), getFont()).height;
positionList.add(new LegendPosition(rectangleX, rectangleY, x2, y2));
graphics.popState();
}
@Override
public Dimension getPreferredSize(int wHint, int hHint) {
int maxWidth =0;
int hEnd = INNER_GAP;
int height = ICON_WIDTH + INNER_GAP;
// int i=0;
for(Trace trace : traceList){
hEnd = hEnd + OUT_GAP + ICON_WIDTH + INNER_GAP +
+ FigureUtilities.getTextExtents(trace.getName(), getFont()).width;
if(hEnd > wHint){
hEnd= INNER_GAP + OUT_GAP + ICON_WIDTH + INNER_GAP +
+ FigureUtilities.getTextExtents(trace.getName(), getFont()).width;
height += ICON_WIDTH + INNER_GAP;
}
if(maxWidth < hEnd)
maxWidth = hEnd;
// i++;
}
return new Dimension(maxWidth, height);
}
/**
* @return the traceList
*/
public List<Trace> getTraceList() {
return traceList;
}
//Added by scouter.project
private final List<LegendPosition> positionList = new ArrayList<LegendPosition>();
/**
* Added by scouter.project
* When mouse pressed/released event occurs, trace is highlighted/normal
*/
private void addListenerToBoldline() {
addMouseListener(new MouseListener.Stub() {
Trace holdingTrace;
int originalWidth;
public void mousePressed(MouseEvent me) {
mouseReleased(me);
for (int i = 0; i < positionList.size(); i++) {
LegendPosition pos = positionList.get(i);
if (pos.isContained(me.x, me.y)) {
holdingTrace = traceList.get(i);
if (holdingTrace.getTraceType() == TraceType.SOLID_LINE) {
originalWidth = holdingTrace.getLineWidth();
holdingTrace.setLineWidth(originalWidth * 2);
} else {
holdingTrace = null;
}
break;
}
}
}
public void mouseReleased(MouseEvent me) {
if (holdingTrace != null) {
if (originalWidth > 0) {
holdingTrace.setLineWidth(originalWidth);
} else {
holdingTrace.setLineWidth(1);
}
originalWidth = 0;
holdingTrace = null;
}
}
});
}
/**
* Added by scouter.project@gmail.com
* Value object to remember rectangle area of legend.
*/
static class LegendPosition {
int x1, x2, y1, y2;
public LegendPosition (int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public boolean isContained(int x, int y) {
boolean result = false;
if(x1 <= x && x <= x2 && y1 <= y && y <= y2) {
result = true;
}
return result;
}
}
}