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.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.Builtins;
\r
32 import org.simantics.db.ReadGraph;
\r
33 import org.simantics.db.Resource;
\r
34 import org.simantics.db.WriteGraph;
\r
35 import org.simantics.db.common.request.WriteRequest;
\r
36 import org.simantics.db.exception.DatabaseException;
\r
37 import org.simantics.db.request.Read;
\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
53 public void createView(Composite parent, Map<String, Object> data) {
\r
55 final String equation = data.get("equation") != null ? (String)data.get("equation") : "";
\r
56 final String lookupTable = data.get("lookup") != null ? (String)data.get("lookup") : "";
\r
58 GridLayoutFactory.fillDefaults().numColumns(2).spacing(3, 3).applyTo(parent);
\r
60 expressionLabel = new Label(parent, SWT.NONE);
\r
61 expressionLabel.setFont(FONT);
\r
62 expressionLabel.setText("With\nLookup");
\r
63 GridDataFactory.fillDefaults().applyTo(expressionLabel);
\r
65 expression = new ExpressionField(parent, SWT.BORDER);
\r
66 expression.setFont(FONT);
\r
67 expression.setExpression(equation);
\r
68 GridDataFactory.fillDefaults().grab(true, true).applyTo(expression);
\r
70 expression.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
73 public void focusLost(FocusEvent e) {
\r
74 lastSelectedText = expression;
\r
78 lookupLabel = new Label(parent, SWT.NONE);
\r
79 lookupLabel.setFont(FONT);
\r
80 lookupLabel.setText("Lookup\ntable");
\r
81 GridDataFactory.fillDefaults().applyTo(lookupLabel);
\r
83 lookup = new ExpressionField(parent, SWT.BORDER);
\r
84 lookup.setFont(FONT);
\r
85 lookup.setExpression(lookupTable);
\r
86 GridDataFactory.fillDefaults().grab(true, true).applyTo(lookup);
\r
88 lookup.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
91 public void focusLost(FocusEvent e) {
\r
92 lastSelectedText = lookup;
\r
96 asGraph = new Button(parent, SWT.None);
\r
97 asGraph.setText("As graph");
\r
98 asGraph.setFont(FONT);
\r
99 asGraph.addSelectionListener(new SelectionAdapter() {
\r
100 public void widgetSelected(SelectionEvent e) {
\r
101 if(e.widget == asGraph) {
\r
102 LookupPopup pud = new LookupPopup(asGraph.getParent().getShell(), variable, lookup.getExpression());
\r
103 lookup.setExpression(pud.open(false));
\r
110 public void readData(final Resource variable, Map<String, Object> data) {
\r
111 String[] results = null;
\r
112 if (variable != null && data.get("equation") == null) {
\r
114 results = SimanticsUI.getSession().syncRequest(new Read<String[]>() {
\r
117 public String[] perform(ReadGraph graph) throws DatabaseException {
\r
118 String[] results = new String[2];
\r
119 SysdynResource sr = SysdynResource.getInstance(graph);
\r
120 Resource expression = graph.getPossibleObject(variable, sr.HasExpression);
\r
121 if (expression != null && graph.isInstanceOf(expression, sr.WithLookupExpression)) {
\r
122 results[0] = graph.getRelatedValue(expression, sr.HasEquation);
\r
123 results[1] = graph.getRelatedValue(expression, sr.HasLookup);
\r
132 } catch (DatabaseException e1) {
\r
133 e1.printStackTrace();
\r
135 data.put("equation", results[0]);
\r
136 data.put("lookup", results[1]);
\r
137 this.variable = variable;
\r
142 public void writeData(final Resource variable, Map<String, Object> data) {
\r
143 final String currentExpression = expression.getExpression();
\r
144 final String currentLookupTable = lookup.getExpression();
\r
146 if(currentExpression != null && currentLookupTable != null) {
\r
147 data.put("equation", currentExpression);
\r
148 data.put("lookup", currentLookupTable);
\r
149 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
151 public void perform(WriteGraph g)
\r
152 throws DatabaseException {
\r
153 SysdynResource sr = SysdynResource.getInstance(g);
\r
154 Resource expression = g.getPossibleObject(variable, sr.HasExpression);
\r
155 Builtins b = g.getBuiltins();
\r
156 if(expression != null) {
\r
157 g.deny(variable, sr.HasExpression);
\r
159 expression = g.newResource();
\r
160 g.claim(expression, b.InstanceOf, null, sr.WithLookupExpression);
\r
161 g.claim(variable, sr.HasExpression, expression);
\r
162 g.claimValue(expression, sr.HasEquation, currentExpression);
\r
163 g.claimValue(expression, sr.HasLookup, currentLookupTable);
\r
171 public void focus() {
\r
172 if(this.lastSelectedText != null) this.lastSelectedText.focus();
\r
176 public void replaceSelection(String var) {
\r
177 if(lastSelectedText != null) {
\r
178 IDocument doc = lastSelectedText.getDocument();
\r
180 Point selection = lastSelectedText.getSelection();
\r
181 doc.replace(selection.x, selection.y, var);
\r
182 lastSelectedText.setSelection(selection.x + var.length());
\r
183 } catch (BadLocationException e) {
\r
184 e.printStackTrace();
\r
190 public void updateData(Map<String, Object> data) {
\r
191 if(this.expression != null && this.expression.getExpression() != null)
\r
192 data.put("equation", this.expression.getExpression());
\r
193 if(this.lookup != null && this.lookup.getExpression() != null)
\r
194 data.put("lookup", this.lookup.getExpression());
\r
198 public List<ExpressionField> getExpressionFields() {
\r
199 return Arrays.asList(this.expression, this.lookup);
\r