]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
21224331eea1e5c2ea6b39c58102ff3777cfc5ee
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.properties.widgets.expressions;\r
2 \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
7 \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
18 \r
19 @SuppressWarnings("serial")\r
20 public class LookupChartPanel extends ChartPanel {\r
21 \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
27 \r
28     public LookupChartPanel(JFreeChart chart) {\r
29         super(chart);\r
30         this.chart = chart;\r
31         XYSeriesCollection collection = (XYSeriesCollection) ((XYPlot)chart.getPlot()).getDataset();\r
32         series = collection.getSeries(0); \r
33     }\r
34 \r
35     public void setTable(LookupInputOutputTable table) {\r
36         this.table = table;\r
37     }\r
38 \r
39     public void mouseDragged(MouseEvent e)\r
40     {\r
41         if(dragPrevEntity != null) {\r
42 \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
50 \r
51 \r
52             if(series.indexOf(location.getX()) >= 0 && series.indexOf(location.getX()) != item)\r
53                 return;\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
62 \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
67 \r
68             removeItemFromSeries(dragPrevEntity.getItem());\r
69             addLocationToSeries(location);\r
70         } else {\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
75                 drawing = true;\r
76                 Point2D locationOnChart = getLocationOnChart(getMouseLocation(e));\r
77                 int item = series.indexOf(locationOnChart.getX());\r
78                 if (item >= 0) {\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
83                 }\r
84             }\r
85         }\r
86 \r
87     }\r
88 \r
89     public void mouseReleased(MouseEvent e) {\r
90 \r
91         dragPrevEntity = null;\r
92         drawing = false;\r
93     }\r
94 \r
95     public void mouseClicked(MouseEvent e)\r
96     {\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
104             }\r
105         }\r
106     }         \r
107 \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
117     }\r
118 \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
128     }\r
129 \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
134         }\r
135     }\r
136 \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
141     }\r
142 \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
148         return p;\r
149     }\r
150 \r
151 \r
152 }\r