source: trunk/src/org/expeditee/items/widgets/charts/AbstractChart.java@ 278

Last change on this file since 278 was 278, checked in by ra33, 16 years ago

Fixed some bugs...
AbstractCharts can not be copied, resized, etc

File size: 10.4 KB
Line 
1package org.expeditee.items.widgets.charts;
2
3import java.awt.BasicStroke;
4import java.awt.Color;
5import java.awt.Component;
6import java.awt.Font;
7import java.awt.Paint;
8import java.awt.Point;
9import java.awt.event.KeyEvent;
10import java.awt.event.KeyListener;
11import java.awt.event.MouseEvent;
12import java.awt.event.MouseListener;
13import java.util.Collection;
14import java.util.LinkedHashMap;
15import java.util.Map;
16
17import javax.swing.JPanel;
18
19import org.expeditee.gui.ColorUtils;
20import org.expeditee.gui.Frame;
21import org.expeditee.gui.FrameGraphics;
22import org.expeditee.gui.FrameIO;
23import org.expeditee.gui.FrameKeyboardActions;
24import org.expeditee.gui.FrameMouseActions;
25import org.expeditee.gui.FunctionKey;
26import org.expeditee.items.Item;
27import org.expeditee.items.Text;
28import org.expeditee.items.widgets.DataFrameWidget;
29import org.jfree.chart.ChartPanel;
30import org.jfree.chart.JFreeChart;
31import org.jfree.chart.block.BlockBorder;
32import org.jfree.chart.plot.Plot;
33import org.jfree.chart.title.LegendTitle;
34import org.jfree.chart.title.TextTitle;
35import org.jfree.chart.title.Title;
36import org.jfree.ui.RectangleInsets;
37
38public abstract class AbstractChart extends DataFrameWidget {
39
40 protected static final String DEFAULT_TITLE = "";
41
42 protected static final String DEFAULT_XAXIS = "X";
43
44 protected static final String DEFAULT_YAXIS = "Y";
45
46 private JFreeChart _chart = null;
47
48 private LegendTitle _legend = null;
49
50 protected Map<String, Paint> _paints;
51
52 protected JFreeChart getChart() {
53 return _chart;
54 }
55
56 public AbstractChart(Text source, String[] args) {
57 super(source, new JPanel(), 100, 300, -1, 100, 300, -1);
58 _paints = new LinkedHashMap<String, Paint>();
59 init();
60 refresh();
61 }
62
63 @Override
64 protected String[] getArgs() {
65 return null;
66 }
67
68 protected void init() {
69 // create a chart...
70 _chart = createNewChart();
71 _chart.getPlot().setNoDataMessage("Add link to data frame");
72 _legend = _chart.getLegend();
73 ChartPanel cp = new ChartPanel(_chart);
74 cp.setPopupMenu(null);
75 cp.addKeyListener(new KeyListener() {
76 public void keyPressed(KeyEvent e) {
77 int index = e.getKeyCode() - KeyEvent.VK_F1 + 1;
78 // Make sure the key is within range
79 if (index < 0 || index >= FunctionKey.values().length) {
80 return;
81 }
82
83 FunctionKey key = FunctionKey.values()[index];
84 switch (key) {
85 case SizeUp:
86 invalidateLink();
87 FrameKeyboardActions.SetSize(getFirstCorner(), 1, false, true);
88 invalidateSelf();
89 FrameGraphics.refresh(true);
90 //FrameGraphics.requestRefresh(true);
91 break;
92 case SizeDown:
93 invalidateLink();
94 FrameKeyboardActions.SetSize(getFirstCorner(), -1, false, true);
95 invalidateSelf();
96 FrameGraphics.refresh(true);
97 //FrameGraphics.ForceRepaint();
98 //FrameGraphics.refresh(true);
99 //FrameGraphics.requestRefresh(true);
100 break;
101 case ChangeColor:
102 if (e.isControlDown()) {
103
104 if (e.isShiftDown()) {
105 setFillColor(null);
106 } else {
107 Color newColor = ColorUtils.getNextColor(
108 getSource().getFillColor(),
109 Frame.COLOR_WHEEL, null);
110 setFillColor(newColor);
111 }
112 } else {
113 if (e.isShiftDown()) {
114 setBackgroundColor(null);
115 } else {
116 Color newColor = ColorUtils.getNextColor(
117 getSource().getColor(), Item.COLOR_WHEEL,
118 null);
119 setColor(newColor);
120 }
121 }
122 break;
123 }
124 }
125
126 public void keyReleased(KeyEvent e) {
127 }
128
129 public void keyTyped(KeyEvent e) {
130 FrameKeyboardActions.processChar(e.getKeyChar(), e
131 .isShiftDown());
132 }
133 });
134 cp.addMouseListener(new MouseListener() {
135
136 public void mouseClicked(MouseEvent e) {
137 FrameMouseActions.getInstance().mouseClicked(translateEvent(e));
138 }
139
140 public void mouseEntered(MouseEvent e) {
141 // FrameMouseActions.getInstance().mouseEntered(translateEvent(e));
142 }
143
144 public void mouseExited(MouseEvent e) {
145 // FrameMouseActions.getInstance().mouseExited(translateEvent(e));
146 }
147
148 public void mousePressed(MouseEvent e) {
149 FrameMouseActions.getInstance().mousePressed(translateEvent(e));
150 }
151
152 private MouseEvent translateEvent(MouseEvent e) {
153 Point origin = getOrigin();
154 return new MouseEvent((Component) e.getSource(), e.getID(), e
155 .getWhen(), e.getModifiers(), origin.x + e.getX(),
156 origin.y + e.getY(), e.getXOnScreen(),
157 e.getYOnScreen(), e.getClickCount(), false, e
158 .getButton());
159 }
160
161 public void mouseReleased(MouseEvent e) {
162 FrameMouseActions.getInstance()
163 .mouseReleased(translateEvent(e));
164 }
165
166 });
167 super._swingComponent = cp;
168
169 setColor(getSource().getColor());
170 setBorderColor(getSource().getBorderColor());
171 setFillColor(getSource().getFillColor());
172 setBackgroundColor(getSource().getBackgroundColor());
173 setThickness(getSource().getThickness(), false);
174 }
175
176 protected abstract JFreeChart createNewChart();
177
178 @Override
179 public final void refresh() {
180 super.refresh();
181 clearData();
182 // Get the data from the linked frame
183 Frame dataFrame = getDataFrame();
184 _paints.clear();
185 _chart.clearSubtitles();
186
187 if (dataFrame != null) {
188 _chart.setTitle(dataFrame.getTitle());
189
190 refreshData(dataFrame);
191
192 if (dataFrame.hasAnnotation("legend")) {
193 _chart.addLegend(_legend);
194 }
195 if (dataFrame.hasAnnotation("subtitle")) {
196 getChart().addSubtitle(
197 new TextTitle(dataFrame.getAnnotationValue("subtitle"),
198 JFreeChart.DEFAULT_TITLE_FONT.deriveFont(
199 Font.ITALIC,
200 JFreeChart.DEFAULT_TITLE_FONT
201 .getSize2D() * .7F)));
202 }
203
204 refreshPlot(dataFrame, _chart.getPlot());
205
206 } else {
207 _chart.setTitle(this.getClass().getSimpleName() + " Chart");
208 }
209 _chart.getPlot().setDataPaints(_paints);
210 super._swingComponent.invalidate();
211 }
212
213 /**
214 * @param dataFrame
215 */
216 protected abstract void refreshPlot(Frame dataFrame, Plot simplePlot);
217
218 /**
219 * @param dataFrame
220 */
221 protected void refreshData(Frame dataFrame) {
222 clearSubjects();
223 boolean swap = false;
224 if (dataFrame.hasAnnotation("swap")) {
225 swap = true;
226 }
227 boolean foundData = false;
228 // First do a pass to see if there are any line ends
229 Collection<Text> textItems = dataFrame.getNonAnnotationText(true);
230 textItems.remove(dataFrame.getTitleItem());
231
232 // Search for line ends
233 for (Text text : textItems) {
234 if (!text.isLineEnd()) {
235 continue;
236 }
237 foundData |= addCategoryData(text.getText(), text
238 .getEnclosedNonAnnotationText(), swap);
239 }
240
241 // As a second option look for linked items on the frame
242 if (!foundData) {
243 for (Text category : textItems) {
244 if (category.isLineEnd() || category.getLink() == null)
245 continue;
246 Frame linkFrame = FrameIO.LoadFrame(category.getAbsoluteLink());
247 if (linkFrame == null)
248 continue;
249
250 String categoryName = category.getText();
251 if (foundData |= addCategoryData(categoryName, linkFrame
252 .getNonAnnotationText(true), swap)) {
253 Color backgroundColor = category.getBackgroundColor();
254 if (backgroundColor != null)
255 _paints.put(categoryName, backgroundColor);
256 addSubject(linkFrame);
257 }
258 }
259 }
260
261 // If there were no groupings in boxes then just get all the values on
262 // the frame
263 if (!foundData) {
264 foundData |= addCategoryData("", textItems, swap);
265 }
266 /* if (foundData) */{
267 addSubject(dataFrame);
268 }
269 }
270
271 protected boolean addCategoryData(String categoryName,
272 Collection<Text> items, boolean swap) {
273 return true;
274 }
275
276 @Override
277 public float getMinimumBorderThickness() {
278 return 1.0F;
279 }
280
281 protected abstract void clearData();
282
283 @Override
284 public void setBackgroundColor(Color c) {
285 super.setBackgroundColor(c);
286 if (_chart == null)
287 return;
288 if (c == null) {
289 _chart.setBackgroundPaint(JFreeChart.DEFAULT_BACKGROUND_PAINT);
290 } else {
291 _chart.setBackgroundPaint(c);
292 }
293 }
294
295 @Override
296 public void setColor(Color c) {
297 super.setColor(c);
298 if (_chart == null)
299 return;
300 if (c == null) {
301 _chart.getTitle().setPaint(TextTitle.DEFAULT_TEXT_PAINT);
302 } else {
303 _chart.getTitle().setPaint(c);
304 }
305 for (Title t : _chart.getSubtitles()) {
306 if (c == null) {
307 t.setPaint(TextTitle.DEFAULT_TEXT_PAINT);
308 } else {
309 t.setPaint(c);
310 }
311 }
312 LegendTitle legend = _chart.getLegend();
313 if (legend != null) {
314 if (c == null) {
315 legend.setFrame(new BlockBorder(Color.black));
316 } else {
317 legend.setFrame(new BlockBorder(c));
318 }
319 }
320 }
321
322 @Override
323 public void setBorderColor(Color c) {
324 super.setBorderColor(c);
325 if (_chart == null)
326 return;
327 if (c == null) {
328 _chart.getPlot().setOutlinePaint(Plot.DEFAULT_OUTLINE_PAINT);
329 } else {
330 _chart.getPlot().setOutlinePaint(c);
331 }
332 }
333
334 @Override
335 public void setFillColor(Color c) {
336 super.setFillColor(c);
337 if (_chart == null)
338 return;
339 LegendTitle legend = _chart.getLegend();
340 if (c == null) {
341 _chart.getPlot().setBackgroundPaint(Plot.DEFAULT_BACKGROUND_PAINT);
342 if (legend != null)
343 legend.setBackgroundPaint(null);
344 } else {
345 _chart.getPlot().setBackgroundPaint(c);
346 if (legend != null)
347 legend.setBackgroundPaint(c);
348 }
349 }
350
351 @Override
352 public void setThickness(float newThickness, boolean setConnected) {
353 super.setThickness(newThickness, setConnected);
354 if (_chart == null)
355 return;
356 _chart.setBorderStroke(new BasicStroke(newThickness));
357 _chart.getPlot().setOutlineStroke(new BasicStroke(newThickness));
358
359 float fontSize = getFontSize(newThickness, TextTitle.DEFAULT_FONT
360 .getSize2D());
361 LegendTitle legend = _chart.getLegend();
362 if (legend != null) {
363 legend.setFrame(new BlockBorder(newThickness, newThickness,
364 newThickness, newThickness, getSource().getColor()));
365 legend.setItemFont(legend.getItemFont().deriveFont(fontSize));
366 float pad = newThickness + 1;
367 float pad2 = pad;
368 legend.setItemLabelPadding(new RectangleInsets(pad, pad, pad, pad));
369 legend.setLegendItemGraphicPadding(new RectangleInsets(pad2, pad2,
370 pad2, pad2));
371 legend.setMargin(newThickness, newThickness, newThickness,
372 newThickness);
373 }
374 float pad0 = newThickness - 1;
375 _chart.setPadding(new RectangleInsets(pad0, pad0, pad0, pad0));
376 TextTitle title = _chart.getTitle();
377 title.setFont(title.getFont().deriveFont(
378 getFontSize(newThickness, JFreeChart.DEFAULT_TITLE_FONT
379 .getSize2D())));
380
381 for (Title t : _chart.getSubtitles()) {
382 if (t instanceof TextTitle) {
383 ((TextTitle) t).setFont(legend.getItemFont().deriveFont(
384 fontSize));
385 }
386 t.setPadding(pad0, pad0, pad0, pad0);
387 }
388 }
389
390 static protected float getFontSize(float newThickness, float oldFontSize) {
391 return (newThickness + 5) * oldFontSize / 6;
392 }
393}
Note: See TracBrowser for help on using the repository browser.