1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.properties.widgets.expressions;
\r
14 import java.awt.BasicStroke;
\r
15 import java.awt.Frame;
\r
16 import java.awt.event.ActionEvent;
\r
17 import java.awt.event.ActionListener;
\r
18 import java.awt.geom.Point2D;
\r
19 import java.io.StringReader;
\r
20 import java.util.ArrayList;
\r
21 import java.util.Arrays;
\r
22 import java.util.List;
\r
23 import java.util.Map;
\r
25 import javax.swing.Timer;
\r
27 import org.eclipse.jface.layout.GridDataFactory;
\r
28 import org.eclipse.jface.layout.GridLayoutFactory;
\r
29 import org.eclipse.jface.text.BadLocationException;
\r
30 import org.eclipse.jface.text.IDocument;
\r
31 import org.eclipse.swt.SWT;
\r
32 import org.eclipse.swt.awt.SWT_AWT;
\r
33 import org.eclipse.swt.custom.VerifyKeyListener;
\r
34 import org.eclipse.swt.events.FocusAdapter;
\r
35 import org.eclipse.swt.events.FocusEvent;
\r
36 import org.eclipse.swt.events.FocusListener;
\r
37 import org.eclipse.swt.events.KeyListener;
\r
38 import org.eclipse.swt.events.ModifyEvent;
\r
39 import org.eclipse.swt.events.ModifyListener;
\r
40 import org.eclipse.swt.graphics.Point;
\r
41 import org.eclipse.swt.widgets.Composite;
\r
42 import org.eclipse.swt.widgets.Label;
\r
43 import org.eclipse.swt.widgets.Table;
\r
44 import org.jfree.chart.ChartFactory;
\r
45 import org.jfree.chart.ChartPanel;
\r
46 import org.jfree.chart.JFreeChart;
\r
47 import org.jfree.chart.plot.PlotOrientation;
\r
48 import org.jfree.data.xy.XYDataset;
\r
49 import org.jfree.data.xy.XYSeries;
\r
50 import org.jfree.data.xy.XYSeriesCollection;
\r
51 import org.simantics.databoard.Bindings;
\r
52 import org.simantics.db.ReadGraph;
\r
53 import org.simantics.db.Resource;
\r
54 import org.simantics.db.VirtualGraph;
\r
55 import org.simantics.db.WriteGraph;
\r
56 import org.simantics.db.common.request.WriteRequest;
\r
57 import org.simantics.db.common.utils.OrderedSetUtils;
\r
58 import org.simantics.db.exception.DatabaseException;
\r
59 import org.simantics.db.procedure.Listener;
\r
60 import org.simantics.db.request.Read;
\r
61 import org.simantics.layer0.Layer0;
\r
62 import org.simantics.layer0.utils.direct.GraphUtils;
\r
63 import org.simantics.sysdyn.SysdynResource;
\r
64 import org.simantics.sysdyn.tableParser.ParseException;
\r
65 import org.simantics.sysdyn.tableParser.TableParser;
\r
66 import org.simantics.sysdyn.tableParser.Token;
\r
67 import org.simantics.ui.SimanticsUI;
\r
69 public class WithLookupExpression implements IExpression {
\r
71 private ExpressionField expression;
\r
72 private ExpressionField lookup;
\r
73 private ExpressionField lastSelectedText = expression;
\r
74 private Timer updateChartTimer;
\r
76 private ChartPanel smallPanel;
\r
77 private Frame smallFrame;
\r
79 private Resource expr;
\r
81 public WithLookupExpression(Resource expression) {
\r
82 this.expr = expression;
\r
86 public void createExpressionFields(Composite parent, final Map<String, Object> data, Table allowedVariables) {
\r
87 GridLayoutFactory.fillDefaults().numColumns(3).applyTo(parent);
\r
89 updateChartTimer = new Timer(1000, new ActionListener() {
\r
92 public void actionPerformed(ActionEvent e) {
\r
96 updateChartTimer.setRepeats(false);
\r
98 String equation = data.get("equation") != null ? (String)data.get("equation") : "";
\r
99 String lookupTable = data.get("lookup") != null ? (String)data.get("lookup") : "";
\r
101 Label l = new Label(parent, SWT.NONE);
\r
102 l.setText("With\nLookup");
\r
104 expression = new ExpressionField(parent, SWT.BORDER, allowedVariables, true);
\r
105 expression.setExpression(equation);
\r
106 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
108 expression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
111 public void focusLost(FocusEvent e) {
\r
112 lastSelectedText = expression;
\r
116 Composite chartContainer = new Composite(parent, SWT.NONE);
\r
117 createChart(chartContainer, data);
\r
120 l = new Label(parent, SWT.NONE);
\r
121 l.setText("Lookup\ntable");
\r
123 lookup = new ExpressionField(parent, SWT.BORDER, null, false);
\r
124 lookup.setExpression(lookupTable);
\r
125 GridDataFactory.fillDefaults().grab(true, true).applyTo(lookup);
\r
127 lookup.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
130 public void focusLost(FocusEvent e) {
\r
131 lastSelectedText = lookup;
\r
136 lookup.getSourceViewer().getTextWidget().addModifyListener(new ModifyListener() {
\r
139 public void modifyText(ModifyEvent e) {
\r
140 if(!updateChartTimer.isRunning())
\r
141 updateChartTimer.start();
\r
143 updateChartTimer.restart();
\r
148 SimanticsUI.getSession().asyncRequest(new Read<String>() {
\r
151 public String perform(ReadGraph graph) throws DatabaseException {
\r
152 SysdynResource sr = SysdynResource.getInstance(graph);
\r
153 String result = "";
\r
154 if (expr != null && graph.isInstanceOf(expr, sr.WithLookupExpression)) {
\r
155 result = graph.getPossibleRelatedValue(expr, sr.WithLookupExpression_lookup);
\r
159 }, new Listener<String>() {
\r
162 public void exception(Throwable t) {
\r
163 t.printStackTrace();
\r
167 public void execute(final String result) {
\r
169 lookup.getDisplay().asyncExec(new Runnable() {
\r
172 public void run() {
\r
173 lookup.setExpression(result);
\r
180 public boolean isDisposed() {
\r
181 if(lookup != null && !lookup.isDisposed()) {
\r
192 public void focus() {
\r
193 if(this.lastSelectedText != null) this.lastSelectedText.focus();
\r
197 public List<ExpressionField> getExpressionFields() {
\r
198 return Arrays.asList(this.expression, this.lookup);
\r
202 public void readData(final Resource expression, Map<String, Object> data) {
\r
205 String equation, lookup;
\r
208 Auxiliary results = null;
\r
210 if (data.get("equation") == null) {
\r
212 results = SimanticsUI.getSession().syncRequest(new Read<Auxiliary>() {
\r
215 public Auxiliary perform(ReadGraph graph) throws DatabaseException {
\r
216 Auxiliary results = new Auxiliary();
\r
217 SysdynResource sr = SysdynResource.getInstance(graph);
\r
218 if (expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) {
\r
219 results.equation = graph.getPossibleRelatedValue(expression, sr.WithLookupExpression_expression);
\r
220 results.lookup = graph.getPossibleRelatedValue(expression, sr.WithLookupExpression_lookup);
\r
222 results.equation = "";
\r
223 results.lookup = "";
\r
228 } catch (DatabaseException e1) {
\r
229 e1.printStackTrace();
\r
231 data.put("equation", results.equation == null ? "" : results.equation);
\r
232 data.put("lookup", results.lookup == null ? "" : results.lookup);
\r
238 public void replaceSelection(String var) {
\r
239 if(lastSelectedText != null) {
\r
240 IDocument doc = lastSelectedText.getDocument();
\r
242 Point selection = lastSelectedText.getSelection();
\r
243 doc.replace(selection.x, selection.y, var);
\r
244 lastSelectedText.setSelection(selection.x + var.length());
\r
245 } catch (BadLocationException e) {
\r
246 e.printStackTrace();
\r
252 public void save(final Resource expression, Map<String, Object> data) {
\r
253 final String currentExpression = this.expression.getExpression();
\r
254 final String currentLookupTable = lookup.getExpression();
\r
255 String oldExpression = (String)data.get("equation");
\r
256 String oldLookupTable = (String)data.get("lookup");
\r
258 if(oldExpression == null || oldLookupTable == null ||
\r
259 (currentExpression != null && currentLookupTable != null
\r
260 && (!currentExpression.equals(oldExpression) ||
\r
261 !currentLookupTable.equals(oldLookupTable)))) {
\r
263 data.put("equation", currentExpression);
\r
264 data.put("lookup", currentLookupTable);
\r
265 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
267 public void perform(WriteGraph g)
\r
268 throws DatabaseException {
\r
269 SysdynResource sr = SysdynResource.getInstance(g);
\r
270 Layer0 l0 = Layer0.getInstance(g);
\r
272 if(!g.isInstanceOf(expr, sr.WithLookupExpression)) {
\r
275 Resource ownerList = OrderedSetUtils.getSingleOwnerList(g, expression);
\r
276 final Resource newExpression = GraphUtils.create2(g, sr.WithLookupExpression,
\r
277 sr.WithLookupExpression_minX, 0.0,
\r
278 sr.WithLookupExpression_maxX, 10.0,
\r
279 sr.WithLookupExpression_minY, 0.0,
\r
280 sr.WithLookupExpression_maxY, 10.0);
\r
281 String arrayRange = g.getPossibleRelatedValue(expression, sr.Expression_arrayRange, Bindings.STRING);
\r
282 if(arrayRange != null)
\r
283 g.claimLiteral(newExpression, sr.Expression_arrayRange, arrayRange);
\r
285 final Resource variable = g.getSingleObject(ownerList, sr.Variable_expressions_Inverse);
\r
286 OrderedSetUtils.replace(g, ownerList, expression, newExpression);
\r
287 g.deny(expression, l0.PartOf);
\r
288 g.claim(newExpression, l0.PartOf, variable);
\r
290 VirtualGraph runtime = g.getService(VirtualGraph.class);
\r
291 g.syncRequest(new WriteRequest(runtime) {
\r
293 public void perform(WriteGraph graph) throws DatabaseException {
\r
294 SysdynResource sr = SysdynResource.getInstance(graph);
\r
295 if(graph.hasStatement(variable, sr.IndependentVariable_activeExpression))
\r
296 graph.deny(variable, sr.IndependentVariable_activeExpression);
\r
297 graph.claim(variable, sr.IndependentVariable_activeExpression, newExpression);
\r
301 expr = newExpression;
\r
304 g.claimLiteral(expr, sr.WithLookupExpression_expression, currentExpression);
\r
305 g.claimLiteral(expr, sr.WithLookupExpression_lookup, currentLookupTable);
\r
313 public void updateData(Map<String, Object> data) {
\r
314 if(this.expression != null && this.expression.getExpression() != null)
\r
315 data.put("equation", this.expression.getExpression());
\r
316 if(this.lookup != null && this.lookup.getExpression() != null)
\r
317 data.put("lookup", this.lookup.getExpression());
\r
321 public void addKeyListener(KeyListener listener) {
\r
322 this.expression.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
323 this.lookup.getSourceViewer().getTextWidget().addKeyListener(listener);
\r
327 public void addVerifyKeyListener(VerifyKeyListener listener) {
\r
328 this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r
329 this.lookup.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);
\r
333 public void addModifyListener(ModifyListener listener) {
\r
334 this.expression.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
335 this.lookup.getSourceViewer().getTextWidget().addModifyListener(listener);
\r
339 public void addFocusListener(FocusListener listener) {
\r
340 this.expression.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
341 this.lookup.getSourceViewer().getTextWidget().addFocusListener(listener);
\r
344 private void createChart(Composite composite, final Map<String, Object> data) {
\r
345 GridLayoutFactory.fillDefaults().applyTo(composite);
\r
346 GridDataFactory.fillDefaults().span(1, 2).hint(150, SWT.DEFAULT).applyTo(composite);
\r
347 final Composite chartComposite = new Composite(composite,
\r
348 SWT.NO_BACKGROUND | SWT.EMBEDDED);
\r
349 GridDataFactory.fillDefaults().grab(true, true).applyTo(chartComposite);
\r
350 smallFrame = SWT_AWT.new_Frame(chartComposite);
\r
352 XYDataset dataset = new XYSeriesCollection(new XYSeries("Lookup Table"));
\r
353 JFreeChart chart = createChart(dataset);
\r
354 smallPanel = new ChartPanel(chart);
\r
355 smallFrame.add(smallPanel);
\r
359 private static JFreeChart createChart(XYDataset dataset) {
\r
360 JFreeChart chart = ChartFactory.createXYLineChart(
\r
365 PlotOrientation.VERTICAL,
\r
370 chart.removeLegend();
\r
371 chart.getXYPlot().getDomainAxis().setTickLabelsVisible(true);
\r
372 chart.getXYPlot().getDomainAxis().setAxisLineVisible(false);
\r
373 chart.getXYPlot().getDomainAxis().setTickMarksVisible(true);
\r
374 chart.getXYPlot().getRangeAxis().setTickLabelsVisible(true);
\r
375 chart.getXYPlot().getRangeAxis().setAxisLineVisible(false);
\r
376 chart.getXYPlot().getRangeAxis().setTickMarksVisible(true);
\r
377 chart.getXYPlot().getRenderer().setSeriesStroke(0, new BasicStroke(3.0f));
\r
381 private void updateChart() {
\r
382 ArrayList<Point2D> dataPoints = new ArrayList<Point2D>();
\r
383 TableParser parser = new TableParser(new StringReader(""));
\r
384 parser.ReInit(new StringReader(lookup.getExpression()));
\r
387 ArrayList<Token> xTokens = parser.getXTokens();
\r
388 ArrayList<Token> yTokens = parser.getYTokens();
\r
389 for(int i = 0; i < xTokens.size(); i++) {
\r
390 dataPoints.add(new Point2D.Double(
\r
391 Double.parseDouble(xTokens.get(i).image),
\r
392 Double.parseDouble(yTokens.get(i).image)));
\r
394 } catch (ParseException e1) {
\r
395 this.lookup.setSyntaxError(e1.currentToken, "Syntax Error");
\r
396 System.out.println("MESSAGE: " + e1.getMessage());
\r
400 XYSeries series = new XYSeries("Lookup Table");
\r
401 for(Point2D point : dataPoints) {
\r
402 series.add(point.getX(), point.getY());
\r
404 XYSeriesCollection dataset = new XYSeriesCollection(series);
\r
405 smallPanel.getChart().getXYPlot().setDataset(dataset);
\r