1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\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.graphics.Point;
\r
26 import org.eclipse.swt.widgets.Composite;
\r
27 import org.eclipse.swt.widgets.Label;
\r
28 import org.simantics.db.Builtins;
\r
29 import org.simantics.db.ReadGraph;
\r
30 import org.simantics.db.Resource;
\r
31 import org.simantics.db.WriteGraph;
\r
32 import org.simantics.db.common.request.WriteRequest;
\r
33 import org.simantics.db.exception.DatabaseException;
\r
34 import org.simantics.db.request.Read;
\r
35 import org.simantics.sysdyn.SysdynResource;
\r
36 import org.simantics.ui.SimanticsUI;
\r
38 public class WithLookupExpressionViewFactor implements IExpressionViewFactor {
\r
41 private Label expressionLabel;
\r
42 private ExpressionField expression;
\r
43 private Label lookupLabel;
\r
44 private ExpressionField lookup;
\r
45 private ExpressionField lastSelectedText = expression;
\r
48 public void createView(Composite parent, Map<String, Object> data) {
\r
50 final String equation = data.get("equation") != null ? (String)data.get("equation") : "";
\r
51 final String lookupTable = data.get("lookup") != null ? (String)data.get("lookup") : "";
\r
53 GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);
\r
55 expressionLabel = new Label(parent, SWT.NONE);
\r
56 expressionLabel.setFont(FONT);
\r
57 expressionLabel.setText("With\nLookup");
\r
58 GridDataFactory.fillDefaults().applyTo(expressionLabel);
\r
60 expression = new ExpressionField(parent, SWT.BORDER);
\r
61 expression.setFont(FONT);
\r
62 expression.setExpression(equation);
\r
63 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
65 expression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
68 public void focusLost(FocusEvent e) {
\r
69 lastSelectedText = expression;
\r
73 lookupLabel = new Label(parent, SWT.NONE);
\r
74 lookupLabel.setFont(FONT);
\r
75 lookupLabel.setText("Lookup\ntable");
\r
76 GridDataFactory.fillDefaults().applyTo(lookupLabel);
\r
78 lookup = new ExpressionField(parent, SWT.BORDER);
\r
79 lookup.setFont(FONT);
\r
80 lookup.setExpression(lookupTable);
\r
81 GridDataFactory.fillDefaults().grab(true, true).applyTo(lookup);
\r
83 lookup.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
86 public void focusLost(FocusEvent e) {
\r
87 lastSelectedText = lookup;
\r
93 public void readData(final Resource variable, Map<String, Object> data) {
\r
94 String[] results = null;
\r
95 if (variable != null && data.get("equation") == null) {
\r
97 results = SimanticsUI.getSession().syncRequest(new Read<String[]>() {
\r
100 public String[] perform(ReadGraph graph) throws DatabaseException {
\r
101 String[] results = new String[2];
\r
102 SysdynResource sr = SysdynResource.getInstance(graph);
\r
103 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
104 if (expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) {
\r
105 results[0] = graph.getRelatedValue(expression, sr.HasEquation);
\r
106 results[1] = graph.getRelatedValue(expression, sr.HasLookup);
\r
115 } catch (DatabaseException e1) {
\r
116 e1.printStackTrace();
\r
118 data.put("equation", results[0]);
\r
119 data.put("lookup", results[1]);
\r
124 public void writeData(final Resource variable, Map<String, Object> data) {
\r
125 final String currentExpression = expression.getExpression();
\r
126 final String currentLookupTable = lookup.getExpression();
\r
128 if(currentExpression != null && currentLookupTable != null) {
\r
129 data.put("equation", currentExpression);
\r
130 data.put("lookup", currentLookupTable);
\r
131 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
133 public void perform(WriteGraph g)
\r
134 throws DatabaseException {
\r
135 SysdynResource sr = SysdynResource.getInstance(g);
\r
136 Resource expression = g.getPossibleObject(variable, sr.HasExpression);
\r
137 Builtins b = g.getBuiltins();
\r
138 if(expression != null) {
\r
139 g.deny(variable, sr.HasExpression);
\r
141 expression = g.newResource();
\r
142 g.claim(expression, b.InstanceOf, null, sr.WithLookupExpression);
\r
143 g.claim(variable, sr.HasExpression, expression);
\r
144 g.claimValue(expression, sr.HasEquation, currentExpression);
\r
145 g.claimValue(expression, sr.HasLookup, currentLookupTable);
\r
153 public void focus() {
\r
154 if(this.lastSelectedText != null) this.lastSelectedText.focus();
\r
158 public void replaceSelection(String var) {
\r
159 if(lastSelectedText != null) {
\r
160 IDocument doc = lastSelectedText.getDocument();
\r
162 Point selection = lastSelectedText.getSelection();
\r
163 doc.replace(selection.x, selection.y, var);
\r
164 lastSelectedText.setSelection(selection.x + var.length());
\r
165 } catch (BadLocationException e) {
\r
166 e.printStackTrace();
\r
172 public void updateData(Map<String, Object> data) {
\r
173 if(this.expression != null && this.expression.getExpression() != null)
\r
174 data.put("equation", this.expression.getExpression());
\r
175 if(this.lookup != null && this.lookup.getExpression() != null)
\r
176 data.put("lookup", this.lookup.getExpression());
\r
180 public List<ExpressionField> getExpressionFields() {
\r
181 return Arrays.asList(this.expression, this.lookup);
\r