1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 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.functions;
\r
14 import java.awt.event.ActionEvent;
\r
15 import java.awt.event.ActionListener;
\r
16 import java.io.StringReader;
\r
18 import javax.swing.Timer;
\r
20 import org.eclipse.jface.layout.GridDataFactory;
\r
21 import org.eclipse.swt.SWT;
\r
22 import org.eclipse.swt.custom.StyledText;
\r
23 import org.eclipse.swt.custom.VerifyKeyListener;
\r
24 import org.eclipse.swt.events.FocusAdapter;
\r
25 import org.eclipse.swt.events.FocusEvent;
\r
26 import org.eclipse.swt.events.ModifyEvent;
\r
27 import org.eclipse.swt.events.ModifyListener;
\r
28 import org.eclipse.swt.events.VerifyEvent;
\r
29 import org.eclipse.swt.widgets.Composite;
\r
30 import org.eclipse.swt.widgets.Listener;
\r
31 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
\r
32 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
\r
33 import org.simantics.db.ReadGraph;
\r
34 import org.simantics.db.Resource;
\r
35 import org.simantics.db.WriteGraph;
\r
36 import org.simantics.db.common.request.WriteRequest;
\r
37 import org.simantics.db.exception.DatabaseException;
\r
38 import org.simantics.db.management.ISessionContext;
\r
39 import org.simantics.db.request.Read;
\r
40 import org.simantics.sysdyn.SysdynResource;
\r
41 import org.simantics.sysdyn.modelParser.ModelParser;
\r
42 import org.simantics.sysdyn.modelParser.ParseException;
\r
43 import org.simantics.sysdyn.modelParser.Token;
\r
44 import org.simantics.sysdyn.modelParser.TokenMgrError;
\r
45 import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionField;
\r
46 import org.simantics.sysdyn.ui.utils.SyntaxError;
\r
47 import org.simantics.ui.SimanticsUI;
\r
48 import org.simantics.ui.utils.AdaptionUtils;
\r
50 public class FunctionCodeWidget implements Widget {
\r
52 private ExpressionField modelicaCode;
\r
53 private Resource function;
\r
55 private Timer updateChartTimer;
\r
56 private static int VALIDATION_DELAY_TIME = 500;
\r
58 public FunctionCodeWidget(Composite parent, WidgetSupport support, int style) {
\r
59 support.register(this);
\r
60 modelicaCode = new ExpressionField(parent, SWT.BORDER, null, false);
\r
61 GridDataFactory.fillDefaults().grab(true, true).applyTo(modelicaCode);
\r
64 // Support shift+enter for line change
\r
65 modelicaCode.getSourceViewer().getTextWidget().addVerifyKeyListener(new VerifyKeyListener() {
\r
68 public void verifyKey(VerifyEvent event) {
\r
69 if(event.keyCode == SWT.CR || event.keyCode == SWT.KEYPAD_CR) {
\r
70 if((event.stateMask & SWT.SHIFT) == 0) {
\r
72 Listener[] listeners = modelicaCode.getSourceViewer().getTextWidget().getListeners(SWT.FocusOut);
\r
73 for(Listener l : listeners) {
\r
74 modelicaCode.getSourceViewer().getTextWidget().removeListener(SWT.FocusOut, l);
\r
76 ((StyledText)event.widget).getParent().forceFocus();
\r
78 for(Listener l : listeners) {
\r
79 modelicaCode.getSourceViewer().getTextWidget().addListener(SWT.FocusOut, l);
\r
88 modelicaCode.getSourceViewer().getTextWidget().addModifyListener(new ModifyListener() {
\r
91 public void modifyText(ModifyEvent e) {
\r
92 validateFieldsTimed();
\r
96 modelicaCode.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {
\r
98 public void focusLost(FocusEvent e) {
\r
104 updateChartTimer = new Timer(VALIDATION_DELAY_TIME, new ActionListener() {
\r
107 public void actionPerformed(ActionEvent e) {
\r
108 if(modelicaCode == null || modelicaCode.isDisposed())
\r
110 modelicaCode.getDisplay().asyncExec(new Runnable() {
\r
113 public void run() {
\r
119 updateChartTimer.setRepeats(false);
\r
123 public void setInput(ISessionContext context, Object input) {
\r
124 function = AdaptionUtils.adaptToSingle(input, Resource.class);
\r
127 String code = context.getSession().syncRequest(new Read<String>() {
\r
130 public String perform(ReadGraph graph) throws DatabaseException {
\r
131 String code = graph.getPossibleRelatedValue(
\r
132 function, SysdynResource.getInstance(graph).SysdynModelicaFunction_modelicaFunctionCode);
\r
133 if (code == null) {
\r
134 code = graph.getPossibleRelatedValue(
\r
135 function, SysdynResource.getInstance(graph).SysdynModelicaFunction_modelicaFunctionInterface);
\r
136 if (code == null) {
\r
137 code = new String("");
\r
145 modelicaCode.setExpression(code);
\r
148 } catch (DatabaseException e) {
\r
149 e.printStackTrace();
\r
154 public void validateFieldsTimed() {
\r
155 validateFieldsTimed(VALIDATION_DELAY_TIME);
\r
158 public void validateFieldsTimed(int delay) {
\r
159 updateChartTimer.setDelay(delay);
\r
160 if(!updateChartTimer.isRunning())
\r
161 updateChartTimer.start();
\r
163 updateChartTimer.restart();
\r
166 public void validate() {
\r
167 modelicaCode.resetAnnotations();
\r
168 String code = modelicaCode.getExpression();
\r
169 StringReader sr = new StringReader(code);
\r
170 ModelParser modelParser = new ModelParser(sr);
\r
172 modelParser.parse_composition();
\r
173 } catch (ParseException e1) {
\r
174 Token token = e1.currentToken;
\r
175 modelicaCode.setSyntaxError(new SyntaxError(token.image, "Syntax error", token.beginLine, token.beginColumn, token.endLine, token.endColumn));
\r
176 } catch (TokenMgrError err) {
\r
177 String message = err.getMessage();
\r
178 String line = message.substring(0, message.indexOf(","));
\r
179 line = line.substring(line.lastIndexOf(" ") + 1);
\r
180 String column = message.substring(message.indexOf(",") + 1, message.indexOf("."));
\r
181 column = column.substring(column.lastIndexOf(" ") + 1);
\r
183 Integer endLine = Integer.parseInt(line);
\r
184 Integer endColumn = Integer.parseInt(column);
\r
185 Token token = modelParser.token;
\r
186 modelicaCode.setSyntaxError(new SyntaxError(token.image, "Syntax error", token.endLine, token.endColumn, endLine, endColumn));
\r
187 } catch (NumberFormatException e) {
\r
194 private void save() {
\r
195 final String code = modelicaCode.getExpression();
\r
196 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
199 public void perform(WriteGraph graph) throws DatabaseException {
\r
200 graph.claimLiteral(
\r
202 SysdynResource.getInstance(graph).SysdynModelicaFunction_modelicaFunctionCode,
\r