1 package org.simantics.sysdyn.ui.properties.widgets.expressions;
\r
3 import java.awt.Point;
\r
4 import java.awt.event.MouseEvent;
\r
5 import java.awt.geom.Point2D;
\r
6 import java.awt.geom.Rectangle2D;
\r
8 import org.jfree.chart.ChartPanel;
\r
9 import org.jfree.chart.ChartRenderingInfo;
\r
10 import org.jfree.chart.JFreeChart;
\r
11 import org.jfree.chart.axis.ValueAxis;
\r
12 import org.jfree.chart.entity.ChartEntity;
\r
13 import org.jfree.chart.entity.PlotEntity;
\r
14 import org.jfree.chart.entity.XYItemEntity;
\r
15 import org.jfree.chart.plot.XYPlot;
\r
16 import org.jfree.data.xy.XYSeries;
\r
17 import org.jfree.data.xy.XYSeriesCollection;
\r
19 @SuppressWarnings("serial")
\r
20 public class LookupChartPanel extends ChartPanel {
\r
22 private XYItemEntity dragPrevEntity;
\r
23 private boolean drawing;
\r
24 private XYSeries series;
\r
25 private JFreeChart chart;
\r
26 private LookupInputOutputTable table;
\r
28 public LookupChartPanel(JFreeChart chart) {
\r
31 XYSeriesCollection collection = (XYSeriesCollection) ((XYPlot)chart.getPlot()).getDataset();
\r
32 series = collection.getSeries(0);
\r
35 public void setTable(LookupInputOutputTable table) {
\r
39 public void mouseDragged(MouseEvent e)
\r
41 if(dragPrevEntity != null) {
\r
43 int item = dragPrevEntity.getItem();
\r
44 XYPlot plot = (XYPlot)chart.getPlot();
\r
45 ValueAxis rangeAxis = plot.getRangeAxis();
\r
46 ValueAxis domainAxis = plot.getDomainAxis();
\r
47 Point2D location = getLocationOnChart(getMouseLocation(e));
\r
48 Number prevX = item == 0 ? null : series.getX(item - 1);
\r
49 Number nextX = item == series.getItemCount() - 1 ? null : series.getX(item + 1);
\r
52 if(series.indexOf(location.getX()) >= 0 && series.indexOf(location.getX()) != item)
\r
54 else if (prevX != null && location.getX() < prevX.doubleValue())
\r
55 location.setLocation(series.getX(item).doubleValue(), location.getY());
\r
56 else if (nextX != null && location.getX() > nextX.doubleValue())
\r
57 location.setLocation(series.getX(item).doubleValue(), location.getY());
\r
58 else if (location.getX() > domainAxis.getUpperBound())
\r
59 location.setLocation(domainAxis.getUpperBound(), location.getY());
\r
60 else if (location.getX() < domainAxis.getLowerBound())
\r
61 location.setLocation(domainAxis.getLowerBound(), location.getY());
\r
63 if (location.getY() > rangeAxis.getUpperBound())
\r
64 location.setLocation(location.getX(), rangeAxis.getUpperBound());
\r
65 else if (location.getY() < rangeAxis.getLowerBound())
\r
66 location.setLocation(location.getX(), rangeAxis.getLowerBound());
\r
68 removeItemFromSeries(dragPrevEntity.getItem());
\r
69 addLocationToSeries(location);
\r
71 ChartEntity currEntity = this.getEntityForPoint(e.getX(),e.getY());
\r
72 if(!drawing && currEntity instanceof XYItemEntity) {
\r
73 dragPrevEntity = ((XYItemEntity)currEntity);
\r
74 } else if (currEntity instanceof PlotEntity){
\r
76 Point2D locationOnChart = getLocationOnChart(getMouseLocation(e));
\r
77 int item = series.indexOf(locationOnChart.getX());
\r
79 Point2D location = new Point2D.Double(series.getX(item).doubleValue(), series.getY(item).doubleValue());
\r
80 Point2D javaLocation = getLocationOnJava2D(location);
\r
81 removeItemFromSeries(item);
\r
82 addLocationToSeries(getLocationOnChart(new Point2D.Double(javaLocation.getX(), e.getY())));
\r
89 public void mouseReleased(MouseEvent e) {
\r
91 dragPrevEntity = null;
\r
95 public void mouseClicked(MouseEvent e)
\r
97 if(e.getButton() == MouseEvent.BUTTON1) {
\r
98 addLocationToSeries(getLocationOnChart(getMouseLocation(e)));
\r
99 } else if (e.getButton() == MouseEvent.BUTTON3) {
\r
100 ChartEntity entity = this.getEntityForPoint(e.getX(),e.getY());
\r
101 if(entity instanceof XYItemEntity) {
\r
102 XYItemEntity xyentity = ((XYItemEntity)entity);
\r
103 removeItemFromSeries(xyentity.getItem());
\r
108 private Point2D getLocationOnChart(Point2D coordinates) {
\r
109 XYPlot plot = (XYPlot)chart.getPlot();
\r
110 ChartRenderingInfo info = getChartRenderingInfo();
\r
111 Rectangle2D dataArea = info.getPlotInfo().getDataArea();
\r
112 double chartX = plot.getDomainAxis().java2DToValue(coordinates.getX(), dataArea,
\r
113 plot.getDomainAxisEdge());
\r
114 double chartY = plot.getRangeAxis().java2DToValue(coordinates.getY(), dataArea,
\r
115 plot.getRangeAxisEdge());
\r
116 return new Point2D.Double(chartX, chartY);
\r
119 private Point2D getLocationOnJava2D(Point2D location) {
\r
120 XYPlot plot = (XYPlot)chart.getPlot();
\r
121 ChartRenderingInfo info = getChartRenderingInfo();
\r
122 Rectangle2D dataArea = info.getPlotInfo().getDataArea();
\r
123 double javaX = plot.getDomainAxis().valueToJava2D(location.getX(), dataArea,
\r
124 plot.getDomainAxisEdge());
\r
125 double javaY = plot.getRangeAxis().valueToJava2D(location.getY(), dataArea,
\r
126 plot.getRangeAxisEdge());
\r
127 return new Point2D.Double(javaX, javaY);
\r
130 public void addLocationToSeries(Point2D location) {
\r
131 if(series.indexOf(location.getX()) < 0) {
\r
132 series.add(location.getX(), location.getY());
\r
133 table.addLocation(location);
\r
137 public void removeItemFromSeries(int item){
\r
138 Point2D location = new Point2D.Double(series.getX(item).doubleValue(),series.getY(item).doubleValue());
\r
139 series.remove(item);
\r
140 table.removeLocation(location);
\r
143 private Point2D getMouseLocation(MouseEvent e) {
\r
144 int mouseX = e.getX();
\r
145 int mouseY = e.getY();
\r
146 Point2D p = translateScreenToJava2D(
\r
147 new Point(mouseX, mouseY));
\r