]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
NameComposite renames variables (the new name doesn't yet go to the visual representa...
authorlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 25 Jan 2010 17:00:45 +0000 (17:00 +0000)
committerlempinen <lempinen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 25 Jan 2010 17:00:45 +0000 (17:00 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13637 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/NameComposite.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldKeyListener.java [deleted file]

index b39f0044fde52472c88fd600cc6a1a419d69dd4c..6d1fd01f988288d60ab013c8b21fa7d5f731c74e 100644 (file)
@@ -12,6 +12,8 @@ import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.events.FocusEvent;\r
 import org.eclipse.swt.events.FocusListener;\r
+import org.eclipse.swt.events.KeyEvent;\r
+import org.eclipse.swt.events.KeyListener;\r
 import org.eclipse.swt.events.ModifyEvent;\r
 import org.eclipse.swt.events.ModifyListener;\r
 import org.eclipse.swt.events.MouseEvent;\r
@@ -52,6 +54,7 @@ public class EquationView extends ViewPart implements ISelectionListener {
     boolean disposed = false;\r
     org.eclipse.ui.IPartListener2 focusLostListener;   \r
 \r
+    NameComposite nameComposite;\r
     ShortcutTabs shortcutTabs;\r
     ExpressionTypeSelector expressionController;    \r
     Composite unitComposite;\r
@@ -59,7 +62,6 @@ public class EquationView extends ViewPart implements ISelectionListener {
     ExpressionComposite expressionComposite;      \r
 \r
     // Dummies\r
-    Text nameText;\r
     Composite unitsAndRange;  \r
     Composite emptyComposite;\r
 \r
@@ -86,6 +88,10 @@ public class EquationView extends ViewPart implements ISelectionListener {
                     if(unitSelector != null) {\r
                         unitSelector.save();                       \r
                     }\r
+                    \r
+                    if(nameComposite != null) {\r
+                        nameComposite.save(variable);\r
+                    }\r
                 }\r
 \r
             }\r
@@ -100,9 +106,8 @@ public class EquationView extends ViewPart implements ISelectionListener {
 \r
 \r
         // Upper part of the view: Name (and renaming)\r
-        nameText = new Text(parent, SWT.SINGLE | SWT.BORDER);\r
-        nameText.setFont(FONT);\r
-        GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText);\r
+        nameComposite = new NameComposite(parent, SWT.NONE);\r
+        GridDataFactory.fillDefaults().grab(true, false).applyTo(nameComposite);\r
 \r
 \r
         // Upper part of the view: Expression type\r
@@ -218,7 +223,7 @@ public class EquationView extends ViewPart implements ISelectionListener {
                                             expressionController.setExpressionTypes(expressionComposite.getExpressionTypes());  \r
                                             expressionController.select(expressionType);\r
 \r
-                                            nameText.setText(result.name);\r
+                                            nameComposite.setName(result.name);\r
                                         }\r
                                     });      \r
 \r
@@ -313,6 +318,7 @@ public class EquationView extends ViewPart implements ISelectionListener {
     private void addExpressionFieldListeners() {\r
         if(expressionComposite.getExpressionViewFactor() != null)\r
             for(Text text : expressionComposite.getExpressionViewFactor().getExpressionFields()) {\r
+                final String expression = text.getText();\r
                 text.addModifyListener(new ModifyListener() {\r
 \r
                     @Override\r
@@ -320,6 +326,20 @@ public class EquationView extends ViewPart implements ISelectionListener {
                         validateExpressionFields();\r
                     }\r
                 });\r
+                text.addKeyListener(new KeyListener() {\r
+                    \r
+                    @Override\r
+                    public void keyReleased(KeyEvent e) {\r
+                    }\r
+                    \r
+                    @Override\r
+                    public void keyPressed(KeyEvent e) {\r
+                        if(e.keyCode == SWT.ESC && expression != null) {\r
+                            ((Text)e.widget).setText(expression);\r
+                            ((Text)e.widget).setSelection(expression.length());\r
+                        }   \r
+                    }\r
+                });\r
             }\r
     }\r
 \r
@@ -357,7 +377,6 @@ public class EquationView extends ViewPart implements ISelectionListener {
             for(String s : variables) {\r
                 no_connections = no_connections + s + " ";\r
             }\r
-            System.out.println(no_connections);\r
         }\r
     }\r
 \r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/NameComposite.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/NameComposite.java
new file mode 100644 (file)
index 0000000..48e5d76
--- /dev/null
@@ -0,0 +1,73 @@
+package org.simantics.sysdyn.ui.equation;\r
+\r
+import org.eclipse.jface.layout.GridDataFactory;\r
+import org.eclipse.jface.layout.GridLayoutFactory;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.KeyEvent;\r
+import org.eclipse.swt.events.KeyListener;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.simantics.db.Builtins;\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.ui.SimanticsUI;\r
+\r
+public class NameComposite extends Composite {\r
+\r
+    Text nameText;\r
+    String originalName;\r
+\r
+\r
+    public NameComposite(Composite parent, int style) {\r
+        super(parent, style);\r
+        GridLayoutFactory.fillDefaults().spacing(3, 3).applyTo(this);\r
+\r
+        nameText = new Text(this, SWT.SINGLE | SWT.BORDER);\r
+        nameText.setFont(EquationView.FONT);\r
+        GridDataFactory.fillDefaults().grab(true, false).applyTo(nameText);\r
+\r
+        nameText.addKeyListener(new KeyListener() {\r
+\r
+            @Override\r
+            public void keyReleased(KeyEvent e) {\r
+\r
+            }\r
+\r
+            @Override\r
+            public void keyPressed(KeyEvent e) {\r
+                if(e.keyCode == SWT.ESC && originalName != null) {\r
+                    ((Text)e.widget).setText(originalName);\r
+                    ((Text)e.widget).setSelection(originalName.length());\r
+                }                \r
+            }\r
+        });\r
+    }\r
+\r
+    public void setName(String name) {\r
+        this.originalName = name;\r
+        this.nameText.setText(name);\r
+    }\r
+\r
+    public void save(final Resource variable) {\r
+        final String nameNew = this.nameText.getText();\r
+\r
+        if(nameNew != null && !nameNew.equals(this.originalName) && variable != null) {\r
+            try {\r
+                SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
+\r
+                    @Override\r
+                    public void perform(WriteGraph g)\r
+                    throws DatabaseException {\r
+                        Builtins b = g.getBuiltins();\r
+                        g.claimValue(variable, b.HasName, nameNew);\r
+                    }\r
+                });\r
+            } catch (DatabaseException e) {\r
+                e.printStackTrace();\r
+            }\r
+        }\r
+    }\r
+\r
+}\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldKeyListener.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/expressions/ExpressionFieldKeyListener.java
deleted file mode 100644 (file)
index e945278..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-package org.simantics.sysdyn.ui.equation.expressions;\r
-\r
-import java.io.StringReader;\r
-\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.events.KeyEvent;\r
-import org.eclipse.swt.events.KeyListener;\r
-import org.eclipse.swt.graphics.Color;\r
-import org.eclipse.swt.widgets.Text;\r
-import org.simantics.sysdyn.expressionParser.ExpressionParser;\r
-import org.simantics.sysdyn.expressionParser.ParseException;\r
-\r
-public class ExpressionFieldKeyListener implements KeyListener {\r
-\r
-    private String originalText; \r
-    private ExpressionParser parser;\r
-    \r
-    public ExpressionFieldKeyListener(ExpressionParser parser, Text text) {\r
-        this.originalText = text.getText();        \r
-        this.parser = parser;\r
-        this.handleKeyReleased(text);        \r
-    }\r
-    @Override\r
-    public void keyPressed(KeyEvent e) {   \r
-        if(e.keyCode == SWT.ESC && e.widget instanceof Text) {\r
-            ((Text)e.widget).setText(originalText);\r
-            ((Text)e.widget).setSelection(originalText.length());\r
-        }\r
-    }\r
-\r
-    @Override\r
-    public void keyReleased(KeyEvent e) {     \r
-        if(e.widget instanceof Text) {\r
-            Text text = (Text)e.widget;\r
-            this.handleKeyReleased(text);\r
-        }\r
-    }\r
-    \r
-    private void handleKeyReleased(Text text) {\r
-        String textString = text.getText();\r
-        this.parser.ReInit(new StringReader(textString));\r
-        \r
-        try {\r
-            parser.expr();\r
-            text.setBackground(new Color(text.getDisplay(), 255, 255, 255));\r
-        } catch (ParseException e1) {\r
-            text.setBackground(new Color(text.getDisplay(), 255, 230, 230));\r
-        }\r
-    }\r
-}\r