]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
changed the function editor from ordinary text field to ExpressionField
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 3 May 2011 13:35:31 +0000 (13:35 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 3 May 2011 13:35:31 +0000 (13:35 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@20748 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/FunctionTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/ExpressionFieldConfiguration.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/functions/FunctionCodeWidget.java [new file with mode: 0644]
org.simantics.sysdyn/META-INF/MANIFEST.MF

index 2d7e2c22e42e9ff719ff763642a7eecff8637267..9d9eec4d3dd53df1f477ec785ff59756e579ae57 100644 (file)
@@ -28,11 +28,14 @@ import org.simantics.db.WriteGraph;
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.db.management.ISessionContext;\r
 import org.simantics.layer0.Layer0;\r
-import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.manager.FunctionUtils;\r
 import org.simantics.sysdyn.ui.properties.widgets.FunctionLabelFactory;\r
+import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionField;\r
+import org.simantics.sysdyn.ui.properties.widgets.functions.FunctionCodeWidget;\r
 \r
 public class FunctionTab extends LabelPropertyTabContributor {\r
+       \r
+       ExpressionField modelicaCode;\r
 \r
        @Override\r
        public void createControls(Composite body, IWorkbenchSite site,\r
@@ -64,6 +67,9 @@ public class FunctionTab extends LabelPropertyTabContributor {
         \r
         Label startLabel = new Label(modelicaGroup, support, SWT.NONE);\r
         startLabel.setTextFactory(new FunctionLabelFactory(Layer0.URIs.HasName, false));\r
+        \r
+        new FunctionCodeWidget(modelicaGroup, support, SWT.BORDER);\r
+        /*\r
         TrackedText modelicaCode = new TrackedText(modelicaGroup, support, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.WRAP);\r
         modelicaCode.setTextFactory(new StringPropertyFactory(SysdynResource.URIs.HasModelicaFunctionCode));\r
         modelicaCode.addModifyListener(new StringPropertyModifier(context, SysdynResource.URIs.HasModelicaFunctionCode));\r
@@ -78,6 +84,7 @@ public class FunctionTab extends LabelPropertyTabContributor {
                });\r
         \r
         GridDataFactory.fillDefaults().grab(true, true).applyTo(modelicaCode.getWidget());\r
+        */\r
         Label endLabel = new Label(modelicaGroup, support, SWT.NONE);\r
         endLabel.setTextFactory(new FunctionLabelFactory(Layer0.URIs.HasName, true));\r
 \r
index 000db7bec563e7c9cc14bd2c254704b5be1fe28a..ba5d08be37fe211f291891e5bfe0fa698e4c7dbf 100644 (file)
@@ -94,7 +94,8 @@ public class ExpressionFieldConfiguration extends SourceViewerConfiguration {
 \r
                IRule[] rules = new IRule[] {\r
                                reservedWord,\r
-                               new MultiLineRule("/*", "*/", comment)\r
+                               new MultiLineRule("/*", "*/", comment),\r
+                               new MultiLineRule("\"", "\"", comment)\r
                };\r
                scanner.setRules(rules);\r
 \r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/functions/FunctionCodeWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/functions/FunctionCodeWidget.java
new file mode 100644 (file)
index 0000000..ac26d7c
--- /dev/null
@@ -0,0 +1,194 @@
+package org.simantics.sysdyn.ui.properties.widgets.functions;\r
+\r
+import java.awt.event.ActionEvent;\r
+import java.awt.event.ActionListener;\r
+import java.io.StringReader;\r
+\r
+import javax.swing.Timer;\r
+\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.custom.StyledText;\r
+import org.eclipse.swt.custom.VerifyKeyListener;\r
+import org.eclipse.swt.events.FocusAdapter;\r
+import org.eclipse.swt.events.FocusEvent;\r
+import org.eclipse.swt.events.ModifyEvent;\r
+import org.eclipse.swt.events.ModifyListener;\r
+import org.eclipse.swt.events.VerifyEvent;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Listener;\r
+import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.sysdyn.SysdynResource;\r
+import org.simantics.sysdyn.modelParser.ModelParser;\r
+import org.simantics.sysdyn.modelParser.ParseException;\r
+import org.simantics.sysdyn.modelParser.Token;\r
+import org.simantics.sysdyn.modelParser.TokenMgrError;\r
+import org.simantics.sysdyn.manager.FunctionUtils;\r
+import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionField;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.AdaptionUtils;\r
+\r
+public class FunctionCodeWidget implements Widget {\r
+       \r
+       private ExpressionField modelicaCode;\r
+       private Resource function;\r
+       \r
+    private Timer updateChartTimer;\r
+    private static int VALIDATION_DELAY_TIME = 500;\r
+       \r
+       public FunctionCodeWidget(Composite parent, WidgetSupport support, int style) {\r
+               support.register(this);\r
+               \r
+        modelicaCode = new ExpressionField(parent, SWT.BORDER);\r
+        GridDataFactory.fillDefaults().grab(true, true).applyTo(modelicaCode);\r
+        \r
+\r
+        // Support shift+enter for line change\r
+        modelicaCode.getSourceViewer().getTextWidget().addVerifyKeyListener(new VerifyKeyListener() {\r
+\r
+               @Override\r
+               public void verifyKey(VerifyEvent event) {\r
+                       if(event.keyCode == SWT.CR || event.keyCode == SWT.KEYPAD_CR) {\r
+                               if((event.stateMask & SWT.SHIFT) == 0) {\r
+                                       event.doit = false;\r
+                                       Listener[] listeners = modelicaCode.getSourceViewer().getTextWidget().getListeners(SWT.FocusOut);\r
+                                       for(Listener l : listeners) {\r
+                                               modelicaCode.getSourceViewer().getTextWidget().removeListener(SWT.FocusOut, l);\r
+                                       }\r
+                                       ((StyledText)event.widget).getParent().forceFocus();\r
+                                       save();\r
+                                       for(Listener l : listeners) {\r
+                                               modelicaCode.getSourceViewer().getTextWidget().addListener(SWT.FocusOut, l);\r
+                                       }\r
+                               \r
+                               }\r
+                       }\r
+               }\r
+        });\r
+        \r
+        \r
+        modelicaCode.getSourceViewer().getTextWidget().addModifyListener(new ModifyListener() {\r
+\r
+                       @Override\r
+                       public void modifyText(ModifyEvent e) {\r
+                               validateFieldsTimed();\r
+                       }\r
+               });\r
+        \r
+        modelicaCode.getSourceViewer().getTextWidget().addFocusListener(new FocusAdapter() {\r
+                       @Override\r
+                       public void focusLost(FocusEvent e) {\r
+                               save();\r
+                       }\r
+               });\r
+               \r
+        \r
+        updateChartTimer = new Timer(VALIDATION_DELAY_TIME, new ActionListener() {\r
+\r
+            @Override\r
+            public void actionPerformed(ActionEvent e) {\r
+               if(modelicaCode == null || modelicaCode.isDisposed())\r
+                       return;\r
+               modelicaCode.getDisplay().asyncExec(new Runnable() {\r
+                               \r
+                               @Override\r
+                               public void run() {\r
+                                       validate();\r
+                               }\r
+                       });\r
+            }\r
+        });\r
+        updateChartTimer.setRepeats(false);\r
+       }\r
+\r
+       @Override\r
+       public void setInput(ISessionContext context, Object input) {\r
+               function = AdaptionUtils.adaptToSingle(input, Resource.class);\r
+               \r
+               try {\r
+                       String code = context.getSession().syncRequest(new Read<String>() {\r
+\r
+                               @Override\r
+                               public String perform(ReadGraph graph) throws DatabaseException {\r
+                                       String code = graph.getPossibleRelatedValue(\r
+                                                       function, SysdynResource.getInstance(graph).HasModelicaFunctionCode);\r
+                                       return code;\r
+                               }\r
+                               \r
+                       });\r
+                       if(code != null) {\r
+                               modelicaCode.setExpression(code);\r
+                               validate();\r
+                       }\r
+               } catch (DatabaseException e) {\r
+                       e.printStackTrace();\r
+               }\r
+       }\r
+       \r
+       \r
+    public void validateFieldsTimed() {        \r
+       validateFieldsTimed(VALIDATION_DELAY_TIME);\r
+    }\r
+    \r
+    public void validateFieldsTimed(int delay) {\r
+       updateChartTimer.setDelay(delay);\r
+        if(!updateChartTimer.isRunning())\r
+            updateChartTimer.start();\r
+        else\r
+            updateChartTimer.restart();\r
+    }\r
+    \r
+    public void validate() {\r
+       modelicaCode.resetAnnotations();\r
+       String code = modelicaCode.getExpression();\r
+       StringReader sr = new StringReader(code);\r
+       ModelParser modelParser = new ModelParser(sr);\r
+       try {\r
+                       modelParser.parse_composition();\r
+               } catch (ParseException e1) {\r
+                       Token token = e1.currentToken;\r
+                       modelicaCode.setSyntaxError(token.image, "Syntax error", token.beginLine, token.beginColumn, token.endLine, token.endColumn);\r
+               } catch (TokenMgrError err) {\r
+                       String message = err.getMessage();\r
+                       String line = message.substring(0, message.indexOf(","));\r
+                       line = line.substring(line.lastIndexOf(" ") + 1);\r
+                       String column = message.substring(message.indexOf(",") + 1, message.indexOf("."));\r
+                       column = column.substring(column.lastIndexOf(" ") + 1);\r
+                       try {\r
+                               Integer endLine = Integer.parseInt(line);\r
+                               Integer endColumn = Integer.parseInt(column);\r
+                               Token token = modelParser.token;\r
+                               modelicaCode.setSyntaxError(token.image, "Syntax error", token.endLine, token.endColumn, endLine, endColumn);\r
+                       } catch (NumberFormatException e) {\r
+                               \r
+                       }\r
+                       \r
+               }\r
+       }\r
+    \r
+    private void save() {\r
+       final String code = modelicaCode.getExpression();\r
+       SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+                       \r
+                       @Override\r
+                       public void perform(WriteGraph graph) throws DatabaseException {\r
+                               graph.claimLiteral(\r
+                                               function, \r
+                                               SysdynResource.getInstance(graph).HasModelicaFunctionCode, \r
+                                               code);\r
+                               Resource library = graph.getSingleObject(function, Layer0.getInstance(graph).PartOf);\r
+                               FunctionUtils.updateFunctionFileForLibrary(graph, library);\r
+                       }\r
+               });\r
+    }\r
+\r
+}\r
index ce7c53a5eeb22780664a2499722071c4ed6c8592..f901e786b98eea95ef57dac5d2e0e5b51f16ce29 100644 (file)
@@ -24,16 +24,17 @@ Require-Bundle: org.simantics.objmap;bundle-version="0.1.0",
  org.simantics.scl.runtime;bundle-version="0.1.3",
  org.simantics.db.layer0;bundle-version="1.1.0"
 Export-Package: org.simantics.sysdyn,
+ org.simantics.sysdyn.adapter,
  org.simantics.sysdyn.expressionParser,
  org.simantics.sysdyn.manager,
  org.simantics.sysdyn.mdlImport,
  org.simantics.sysdyn.mdlImport.mdlElements,
+ org.simantics.sysdyn.modelParser,
  org.simantics.sysdyn.modelica,
  org.simantics.sysdyn.representation,
  org.simantics.sysdyn.representation.visitors,
  org.simantics.sysdyn.simulation,
- org.simantics.sysdyn.tableParser,
- org.simantics.sysdyn.adapter
+ org.simantics.sysdyn.tableParser
 Bundle-Activator: org.simantics.sysdyn.Activator
 Bundle-ActivationPolicy: lazy
 Import-Package: org.eclipse.ui