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.equation.expressions;
\r
14 import java.util.Arrays;
\r
15 import java.util.List;
\r
16 import java.util.Map;
\r
18 import org.eclipse.jface.layout.GridDataFactory;
\r
19 import org.eclipse.jface.layout.GridLayoutFactory;
\r
20 import org.eclipse.jface.text.BadLocationException;
\r
21 import org.eclipse.jface.text.IDocument;
\r
22 import org.eclipse.swt.SWT;
\r
23 import org.eclipse.swt.events.FocusAdapter;
\r
24 import org.eclipse.swt.events.FocusEvent;
\r
25 import org.eclipse.swt.events.SelectionAdapter;
\r
26 import org.eclipse.swt.events.SelectionEvent;
\r
27 import org.eclipse.swt.graphics.Point;
\r
28 import org.eclipse.swt.widgets.Button;
\r
29 import org.eclipse.swt.widgets.Composite;
\r
30 import org.eclipse.swt.widgets.Label;
\r
31 import org.simantics.db.ReadGraph;
\r
32 import org.simantics.db.Resource;
\r
33 import org.simantics.db.WriteGraph;
\r
34 import org.simantics.db.common.request.WriteRequest;
\r
35 import org.simantics.db.exception.DatabaseException;
\r
36 import org.simantics.db.request.Read;
\r
37 import org.simantics.layer0.Layer0;
\r
38 import org.simantics.sysdyn.SysdynResource;
\r
39 import org.simantics.ui.SimanticsUI;
\r
41 public class WithLookupExpressionViewFactor implements IExpressionViewFactor {
\r
44 private Label expressionLabel;
\r
45 private ExpressionField expression;
\r
46 private Label lookupLabel;
\r
47 private ExpressionField lookup;
\r
48 private Button asGraph;
\r
49 private ExpressionField lastSelectedText = expression;
\r
50 private Resource variable;
\r
51 private LookupChartInfo chartInfo;
\r
53 public WithLookupExpressionViewFactor(Resource variable) {
\r
55 this.variable = variable;
\r
59 public void createView(Composite parent, final Map<String, Object> data) {
\r
60 String equation = data.get("equation") != null ? (String)data.get("equation") : "";
\r
61 String lookupTable = data.get("lookup") != null ? (String)data.get("lookup") : "";
\r
63 GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);
\r
65 expressionLabel = new Label(parent, SWT.NONE);
\r
66 expressionLabel.setFont(FONT);
\r
67 expressionLabel.setText("With\nLookup");
\r
68 GridDataFactory.fillDefaults().applyTo(expressionLabel);
\r
70 expression = new ExpressionField(parent, SWT.BORDER);
\r
71 expression.setFont(FONT);
\r
72 expression.setExpression(equation);
\r
73 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
75 expression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
78 public void focusLost(FocusEvent e) {
\r
79 lastSelectedText = expression;
\r
83 lookupLabel = new Label(parent, SWT.NONE);
\r
84 lookupLabel.setFont(FONT);
\r
85 lookupLabel.setText("Lookup\ntable");
\r
86 GridDataFactory.fillDefaults().applyTo(lookupLabel);
\r
88 lookup = new ExpressionField(parent, SWT.BORDER);
\r
89 lookup.setFont(FONT);
\r
90 lookup.setExpression(lookupTable);
\r
91 GridDataFactory.fillDefaults().grab(true, true).applyTo(lookup);
\r
93 lookup.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
96 public void focusLost(FocusEvent e) {
\r
97 lastSelectedText = lookup;
\r
101 asGraph = new Button(parent, SWT.None);
\r
102 asGraph.setText("As graph");
\r
103 asGraph.setFont(FONT);
\r
104 asGraph.addSelectionListener(new SelectionAdapter() {
\r
105 public void widgetSelected(SelectionEvent e) {
\r
106 if(e.widget == asGraph) {
\r
107 if(chartInfo == null) chartInfo = new LookupChartInfo(expression.getExpression(), lookup.getExpression(), variable, data);
\r
108 LookupPopup pud = new LookupPopup(asGraph.getParent().getShell(), chartInfo);
\r
109 LookupChartInfo newInfo = pud.open(false);
\r
110 if(pud.getReturnCode() == org.eclipse.jface.window.Window.OK) {
\r
111 chartInfo = newInfo;
\r
112 lookup.setExpression(chartInfo.lookupTable);
\r
121 public void readData(final Resource variable, Map<String, Object> data) {
\r
122 String[] results = null;
\r
123 if (variable != null && data.get("equation") == null) {
\r
125 results = SimanticsUI.getSession().syncRequest(new Read<String[]>() {
\r
128 public String[] perform(ReadGraph graph) throws DatabaseException {
\r
129 String[] results = new String[2];
\r
130 SysdynResource sr = SysdynResource.getInstance(graph);
\r
131 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
132 if (expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) {
\r
133 results[0] = graph.getRelatedValue(expression, sr.HasEquation);
\r
134 results[1] = graph.getRelatedValue(expression, sr.HasLookup);
\r
143 } catch (DatabaseException e1) {
\r
144 e1.printStackTrace();
\r
146 data.put("equation", results[0]);
\r
147 data.put("lookup", results[1]);
\r
152 public void writeData(final Resource variable, Map<String, Object> data) {
\r
153 final String currentExpression = expression.getExpression();
\r
154 final String currentLookupTable = lookup.getExpression();
\r
156 if(currentExpression != null && currentLookupTable != null) {
\r
158 data.put("equation", currentExpression);
\r
159 data.put("lookup", currentLookupTable);
\r
160 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
162 public void perform(WriteGraph g)
\r
163 throws DatabaseException {
\r
164 SysdynResource sr = SysdynResource.getInstance(g);
\r
165 Resource expression = g.getPossibleObject(variable, sr.HasExpression);
\r
166 Layer0 l0 = Layer0.getInstance(g);
\r
167 if(expression != null) {
\r
168 g.deny(variable, sr.HasExpression);
\r
170 expression = g.newResource();
\r
171 g.claim(expression, l0.InstanceOf, null, sr.WithLookupExpression);
\r
172 g.claim(variable, sr.HasExpression, expression);
\r
173 g.claimLiteral(expression, sr.HasEquation, currentExpression);
\r
174 g.claimLiteral(expression, sr.HasLookup, currentLookupTable);
\r
175 g.claimLiteral(expression, sr.HasMinX, chartInfo.minX);
\r
176 g.claimLiteral(expression, sr.HasMaxX, chartInfo.maxX);
\r
177 g.claimLiteral(expression, sr.HasMinY, chartInfo.minY);
\r
178 g.claimLiteral(expression, sr.HasMaxY, chartInfo.maxY);
\r
185 public void focus() {
\r
186 if(this.lastSelectedText != null) this.lastSelectedText.focus();
\r
190 public void replaceSelection(String var) {
\r
191 if(lastSelectedText != null) {
\r
192 IDocument doc = lastSelectedText.getDocument();
\r
194 Point selection = lastSelectedText.getSelection();
\r
195 doc.replace(selection.x, selection.y, var);
\r
196 lastSelectedText.setSelection(selection.x + var.length());
\r
197 } catch (BadLocationException e) {
\r
198 e.printStackTrace();
\r
204 public void updateData(Map<String, Object> data) {
\r
205 if(this.expression != null && this.expression.getExpression() != null)
\r
206 data.put("equation", this.expression.getExpression());
\r
207 if(this.lookup != null && this.lookup.getExpression() != null)
\r
208 data.put("lookup", this.lookup.getExpression());
\r
209 if(this.chartInfo != null) {
\r
210 data.put("minX", chartInfo.minX);
\r
211 data.put("maxX", chartInfo.maxX);
\r
212 data.put("minY", chartInfo.minY);
\r
213 data.put("maxY", chartInfo.maxY);
\r
218 public List<ExpressionField> getExpressionFields() {
\r
219 return Arrays.asList(this.expression, this.lookup);
\r