1 package org.simantics.sysdyn.ui.properties.widgets.expressions;
\r
3 import java.awt.BasicStroke;
\r
4 import java.awt.Frame;
\r
5 import java.awt.event.ActionEvent;
\r
6 import java.awt.event.ActionListener;
\r
7 import java.awt.geom.Point2D;
\r
8 import java.io.StringReader;
\r
9 import java.util.ArrayList;
\r
10 import java.util.Arrays;
\r
11 import java.util.List;
\r
12 import java.util.Map;
\r
14 import javax.swing.Timer;
\r
16 import org.eclipse.jface.layout.GridDataFactory;
\r
17 import org.eclipse.jface.layout.GridLayoutFactory;
\r
18 import org.eclipse.jface.text.BadLocationException;
\r
19 import org.eclipse.jface.text.IDocument;
\r
20 import org.eclipse.swt.SWT;
\r
21 import org.eclipse.swt.awt.SWT_AWT;
\r
22 import org.eclipse.swt.events.FocusAdapter;
\r
23 import org.eclipse.swt.events.FocusEvent;
\r
24 import org.eclipse.swt.events.FocusListener;
\r
25 import org.eclipse.swt.events.KeyListener;
\r
26 import org.eclipse.swt.events.ModifyEvent;
\r
27 import org.eclipse.swt.events.ModifyListener;
\r
28 import org.eclipse.swt.graphics.Point;
\r
29 import org.eclipse.swt.widgets.Composite;
\r
30 import org.eclipse.swt.widgets.Label;
\r
31 import org.jfree.chart.ChartFactory;
\r
32 import org.jfree.chart.ChartPanel;
\r
33 import org.jfree.chart.JFreeChart;
\r
34 import org.jfree.chart.plot.PlotOrientation;
\r
35 import org.jfree.data.xy.XYDataset;
\r
36 import org.jfree.data.xy.XYSeries;
\r
37 import org.jfree.data.xy.XYSeriesCollection;
\r
38 import org.simantics.db.ReadGraph;
\r
39 import org.simantics.db.Resource;
\r
40 import org.simantics.db.WriteGraph;
\r
41 import org.simantics.db.common.request.WriteRequest;
\r
42 import org.simantics.db.exception.DatabaseException;
\r
43 import org.simantics.db.procedure.Listener;
\r
44 import org.simantics.db.request.Read;
\r
45 import org.simantics.layer0.Layer0;
\r
46 import org.simantics.sysdyn.SysdynResource;
\r
47 import org.simantics.sysdyn.representation.Auxiliary;
\r
48 import org.simantics.sysdyn.tableParser.ParseException;
\r
49 import org.simantics.sysdyn.tableParser.TableParser;
\r
50 import org.simantics.sysdyn.tableParser.Token;
\r
51 import org.simantics.ui.SimanticsUI;
\r
53 public class WithLookupExpression implements IExpression {
\r
55 private ExpressionField expression;
\r
56 private ExpressionField lookup;
\r
57 private ExpressionField lastSelectedText = expression;
\r
58 private Timer updateChartTimer;
\r
60 private ChartPanel smallPanel;
\r
61 private Frame smallFrame;
\r
63 private Resource variable;
\r
65 public WithLookupExpression(Resource variable) {
\r
66 this.variable = variable;
\r
70 public void createExpressionFields(Composite parent, final Map<String, Object> data) {
\r
71 GridLayoutFactory.fillDefaults().numColumns(3).applyTo(parent);
\r
73 updateChartTimer = new Timer(1000, new ActionListener() {
\r
76 public void actionPerformed(ActionEvent e) {
\r
80 updateChartTimer.setRepeats(false);
\r
82 String equation = data.get("equation") != null ? (String)data.get("equation") : "";
\r
83 String lookupTable = data.get("lookup") != null ? (String)data.get("lookup") : "";
\r
85 Label l = new Label(parent, SWT.NONE);
\r
86 l.setText("With\nLookup");
\r
88 expression = new ExpressionField(parent, SWT.BORDER);
\r
89 expression.setExpression(equation);
\r
90 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
92 expression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
95 public void focusLost(FocusEvent e) {
\r
96 lastSelectedText = expression;
\r
100 Composite chartContainer = new Composite(parent, SWT.NONE);
\r
101 createChart(chartContainer, data);
\r
104 l = new Label(parent, SWT.NONE);
\r
105 l.setText("Lookup\ntable");
\r
107 lookup = new ExpressionField(parent, SWT.BORDER);
\r
108 lookup.setExpression(lookupTable);
\r
109 GridDataFactory.fillDefaults().grab(true, true).applyTo(lookup);
\r
111 lookup.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
114 public void focusLost(FocusEvent e) {
\r
115 lastSelectedText = lookup;
\r
119 lookup.getSourceViewer().getTextWidget().addModifyListener(new ModifyListener() {
\r
122 public void modifyText(ModifyEvent e) {
\r
123 if(!updateChartTimer.isRunning())
\r
124 updateChartTimer.start();
\r
126 updateChartTimer.restart();
\r
131 SimanticsUI.getSession().asyncRequest(new Read<String>() {
\r
134 public String perform(ReadGraph graph) throws DatabaseException {
\r
135 SysdynResource sr = SysdynResource.getInstance(graph);
\r
136 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
137 String result = "";
\r
138 if (expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) {
\r
139 result = graph.getRelatedValue(expression, sr.HasLookup);
\r
143 }, new Listener<String>() {
\r
146 public void exception(Throwable t) {
\r
147 t.printStackTrace();
\r
151 public void execute(final String result) {
\r
153 lookup.getDisplay().asyncExec(new Runnable() {
\r
156 public void run() {
\r
157 lookup.setExpression(result);
\r
164 public boolean isDisposed() {
\r
165 if(lookup != null && !lookup.isDisposed()) {
\r
176 public void focus() {
\r
177 if(this.lastSelectedText != null) this.lastSelectedText.focus();
\r
181 public List<ExpressionField> getExpressionFields() {
\r
182 return Arrays.asList(this.expression, this.lookup);
\r
186 public void readData(final Resource variable, Map<String, Object> data) {
\r
189 String equation, lookup;
\r
192 Auxiliary results = null;
\r
194 if (variable != null && data.get("equation") == null) {
\r
196 results = SimanticsUI.getSession().syncRequest(new Read<Auxiliary>() {
\r
199 public Auxiliary perform(ReadGraph graph) throws DatabaseException {
\r
200 Auxiliary results = new Auxiliary();
\r
201 SysdynResource sr = SysdynResource.getInstance(graph);
\r
202 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
203 if (expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) {
\r
204 results.equation = graph.getRelatedValue(expression, sr.HasEquation);
\r
205 results.lookup = graph.getRelatedValue(expression, sr.HasLookup);
\r
207 results.equation = "";
\r
208 results.lookup = "";
\r
213 } catch (DatabaseException e1) {
\r
214 e1.printStackTrace();
\r
216 data.put("equation", results.equation);
\r
217 data.put("lookup", results.lookup);
\r
223 public void replaceSelection(String var) {
\r
224 if(lastSelectedText != null) {
\r
225 IDocument doc = lastSelectedText.getDocument();
\r
227 Point selection = lastSelectedText.getSelection();
\r
228 doc.replace(selection.x, selection.y, var);
\r
229 lastSelectedText.setSelection(selection.x + var.length());
\r
230 } catch (BadLocationException e) {
\r
231 e.printStackTrace();
\r
237 public void save(final Resource variable, Map<String, Object> data) {
\r
238 final String currentExpression = expression.getExpression();
\r
239 final String currentLookupTable = lookup.getExpression();
\r
240 String oldExpression = (String)data.get("equation");
\r
241 String oldLookupTable = (String)data.get("lookup");
\r
243 if(oldExpression == null || oldLookupTable == null ||
\r
244 (currentExpression != null && currentLookupTable != null
\r
245 && (!currentExpression.equals(oldExpression) ||
\r
246 !currentLookupTable.equals(oldLookupTable)))) {
\r
248 data.put("equation", currentExpression);
\r
249 data.put("lookup", currentLookupTable);
\r
250 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
252 public void perform(WriteGraph g)
\r
253 throws DatabaseException {
\r
254 SysdynResource sr = SysdynResource.getInstance(g);
\r
255 Resource expression = g.getPossibleObject(variable, sr.HasExpression);
\r
256 Layer0 l0 = Layer0.getInstance(g);
\r
257 if(expression != null && !g.isInstanceOf(expression, sr.WithLookupExpression)) {
\r
258 g.deny(variable, sr.HasExpression);
\r
261 if(expression == null) {
\r
262 expression = g.newResource();
\r
263 g.claim(expression, l0.InstanceOf, null, sr.WithLookupExpression);
\r
264 g.claim(variable, sr.HasExpression, expression);
\r
265 g.claimLiteral(expression, sr.HasMinX, 0.0);
\r
266 g.claimLiteral(expression, sr.HasMaxX, 10.0);
\r
267 g.claimLiteral(expression, sr.HasMinY, 0.0);
\r
268 g.claimLiteral(expression, sr.HasMaxY, 10.0);
\r
271 g.claimLiteral(expression, sr.HasEquation, currentExpression);
\r
272 g.claimLiteral(expression, sr.HasLookup, currentLookupTable);
\r
280 public void updateData(Map<String, Object> data) {
\r
281 if(this.expression != null && this.expression.getExpression() != null)
\r
282 data.put("equation", this.expression.getExpression());
\r
283 if(this.lookup != null && this.lookup.getExpression() != null)
\r
284 data.put("lookup", this.lookup.getExpression());
\r
288 public void addKeyListener(KeyListener listener) {
\r
289 this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
290 this.lookup.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
294 public void addModifyListener(ModifyListener listener) {
\r
295 this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
296 this.lookup.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
300 public void addFocusListener(FocusListener listener) {
\r
301 this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
302 this.lookup.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
305 private void createChart(Composite composite, final Map<String, Object> data) {
\r
306 GridLayoutFactory.fillDefaults().applyTo(composite);
\r
307 GridDataFactory.fillDefaults().span(1, 2).hint(150, SWT.DEFAULT).applyTo(composite);
\r
308 final Composite chartComposite = new Composite(composite,
\r
309 SWT.NO_BACKGROUND | SWT.EMBEDDED);
\r
310 GridDataFactory.fillDefaults().grab(true, true).applyTo(chartComposite);
\r
311 smallFrame = SWT_AWT.new_Frame(chartComposite);
\r
313 XYDataset dataset = new XYSeriesCollection(new XYSeries("Lookup Table"));
\r
314 JFreeChart chart = createChart(dataset);
\r
315 smallPanel = new ChartPanel(chart);
\r
316 smallFrame.add(smallPanel);
\r
320 private static JFreeChart createChart(XYDataset dataset) {
\r
321 JFreeChart chart = ChartFactory.createXYLineChart(
\r
326 PlotOrientation.VERTICAL,
\r
331 chart.removeLegend();
\r
332 chart.getXYPlot().getDomainAxis().setTickLabelsVisible(true);
\r
333 chart.getXYPlot().getDomainAxis().setAxisLineVisible(false);
\r
334 chart.getXYPlot().getDomainAxis().setTickMarksVisible(true);
\r
335 chart.getXYPlot().getRangeAxis().setTickLabelsVisible(true);
\r
336 chart.getXYPlot().getRangeAxis().setAxisLineVisible(false);
\r
337 chart.getXYPlot().getRangeAxis().setTickMarksVisible(true);
\r
338 chart.getXYPlot().getRenderer().setSeriesStroke(0, new BasicStroke(3.0f));
\r
342 private void updateChart() {
\r
343 ArrayList<Point2D> dataPoints = new ArrayList<Point2D>();
\r
344 TableParser parser = new TableParser(new StringReader(""));
\r
345 parser.ReInit(new StringReader(lookup.getExpression()));
\r
348 ArrayList<Token> xTokens = parser.getXTokens();
\r
349 ArrayList<Token> yTokens = parser.getYTokens();
\r
350 for(int i = 0; i < xTokens.size(); i++) {
\r
351 dataPoints.add(new Point2D.Double(
\r
352 Double.parseDouble(xTokens.get(i).image),
\r
353 Double.parseDouble(yTokens.get(i).image)));
\r
355 } catch (ParseException e1) {
\r
356 // e1.printStackTrace();
\r
360 XYSeries series = new XYSeries("Lookup Table");
\r
361 for(Point2D point : dataPoints) {
\r
362 series.add(point.getX(), point.getY());
\r
364 XYSeriesCollection dataset = new XYSeriesCollection(series);
\r
365 smallPanel.getChart().getXYPlot().setDataset(dataset);
\r