]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
refs #5096
authorjsimomaa <jsimomaa@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 14 Jul 2014 05:56:52 +0000 (05:56 +0000)
committerjsimomaa <jsimomaa@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 14 Jul 2014 05:56:52 +0000 (05:56 +0000)
Add undo support in Sysdyns property view that allows the user to undo last typed text

git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29867 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/plugin.xml
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/ExpressionFieldUndoHandler.java [new file with mode: 0644]
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/BasicExpression.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/DelayExpression.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/EmptyExpression.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/IExpression.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/LookupExpression.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/StockExpression.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/expressions/WithLookupExpression.java

index ee0afef0f083552eb0e557341bfd5ecafee3a695..6e8d49e6b0dee37eafd66a250a5cbd4dcba0b83f 100644 (file)
                </or>\r
             </with>\r
          </activeWhen>\r
-      </handler>      \r
+      </handler>\r
+            <handler\r
+            class="org.simantics.sysdyn.ui.ExpressionFieldUndoHandler"\r
+            commandId="org.eclipse.ui.edit.undo">\r
+         <activeWhen>\r
+            <reference\r
+                  definitionId="org.simantics.sysdyn.ui.inExpressionField">\r
+            </reference>\r
+         </activeWhen>\r
+      </handler>\r
+      <handler\r
+            class="org.simantics.sysdyn.ui.ExpressionFieldUndoHandler"\r
+            commandId="org.eclipse.ui.edit.redo">\r
+         <activeWhen>\r
+            <reference\r
+                  definitionId="org.simantics.sysdyn.ui.inExpressionField">\r
+            </reference>\r
+         </activeWhen>\r
+      </handler>\r
+      \r
    </extension>\r
    \r
    <extension\r
             class="org.simantics.sysdyn.ui.preferences.SysdynDiagramPreferencesInitializer">\r
       </initializer>      \r
    </extension>\r
+   \r
+   <extension point="org.eclipse.core.expressions.definitions">\r
+      <definition\r
+            id="org.simantics.sysdyn.ui.inExpressionField">\r
+         <with variable="activePartId">\r
+            <equals\r
+                  value="org.simantics.browsing.ui.graph.propertyView">\r
+            </equals>\r
+         </with>\r
+      </definition>\r
+   </extension>\r
+   \r
 </plugin>\r
diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/ExpressionFieldUndoHandler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/ExpressionFieldUndoHandler.java
new file mode 100644 (file)
index 0000000..3f33ea9
--- /dev/null
@@ -0,0 +1,40 @@
+package org.simantics.sysdyn.ui;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.swt.custom.StyledText;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.Event;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
+import org.simantics.sysdyn.ui.properties.widgets.expressions.ExpressionField;\r
+\r
+public class ExpressionFieldUndoHandler extends AbstractHandler {\r
+\r
+    @Override\r
+    public Object execute(ExecutionEvent event) throws ExecutionException {\r
+        String id = event.getCommand().getId();\r
+        \r
+        Composite c = ((Control)((Event)event.getTrigger()).widget).getParent();\r
+        if (c instanceof ExpressionField) {\r
+            ExpressionField f = (ExpressionField)c;\r
+            if(id.equals("org.eclipse.ui.edit.undo")) {\r
+                f.getSourceViewer().getUndoManager().undo();\r
+            }\r
+            else {\r
+                f.getSourceViewer().getUndoManager().redo();\r
+            }\r
+        } else {\r
+            try {\r
+                Layer0Utils.undo();\r
+            } catch (DatabaseException e) {\r
+                // TODO Auto-generated catch block\r
+                e.printStackTrace();\r
+            }\r
+        }\r
+        return null;\r
+    }\r
+\r
+}\r
index 12ad3c80a5a20901cbb3bdf9c8d992de6941d0d9..a1c654953e8056e6951790ebf254a8797e3b9869 100644 (file)
@@ -19,6 +19,7 @@ import org.eclipse.jface.layout.GridDataFactory;
 import org.eclipse.jface.layout.GridLayoutFactory;\r
 import org.eclipse.jface.text.BadLocationException;\r
 import org.eclipse.jface.text.IDocument;\r
+import org.eclipse.jface.text.IUndoManager;\r
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.custom.VerifyKeyListener;\r
 import org.eclipse.swt.events.FocusListener;\r
@@ -236,4 +237,10 @@ public class BasicExpression implements IExpression {
        public void addVerifyKeyListener(VerifyKeyListener listener) {\r
                this.expression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
        }\r
+\r
+    @Override\r
+    public IUndoManager getUndoManager() {\r
+        // TODO Auto-generated method stub\r
+        return null;\r
+    }\r
 }\r
index e6b1962641bb1f6efa6979dfd0359f66b435568a..5e778c926986ca0c95076deb0f9061335813117a 100644 (file)
@@ -19,6 +19,7 @@ import org.eclipse.jface.layout.GridDataFactory;
 import org.eclipse.jface.layout.GridLayoutFactory;\r
 import org.eclipse.jface.text.BadLocationException;\r
 import org.eclipse.jface.text.IDocument;\r
+import org.eclipse.jface.text.IUndoManager;\r
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.custom.VerifyKeyListener;\r
 import org.eclipse.swt.events.FocusAdapter;\r
@@ -382,4 +383,10 @@ public class DelayExpression implements IExpression {
         this.initialValue.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
     }\r
 \r
+    @Override\r
+    public IUndoManager getUndoManager() {\r
+        // TODO Auto-generated method stub\r
+        return null;\r
+    }\r
+\r
 }\r
index f36fac969ddd6c15999b7127893636b6e7924e39..8a382746ad91d2b02873da773e80aaabefb2ab2b 100644 (file)
@@ -14,6 +14,7 @@ package org.simantics.sysdyn.ui.properties.widgets.expressions;
 import java.util.List;\r
 import java.util.Map;\r
 \r
+import org.eclipse.jface.text.IUndoManager;\r
 import org.eclipse.swt.custom.VerifyKeyListener;\r
 import org.eclipse.swt.events.FocusListener;\r
 import org.eclipse.swt.events.KeyListener;\r
@@ -90,4 +91,10 @@ public class EmptyExpression implements IExpression {
                \r
        }\r
 \r
+    @Override\r
+    public IUndoManager getUndoManager() {\r
+        // TODO Auto-generated method stub\r
+        return null;\r
+    }\r
+\r
 }\r
index c2c6eff032a4d4dfc3d850477ed88e62dea368cc..bd0b0b71d0c67c7e6e0f68cef76cc6f67c01dddd 100644 (file)
@@ -14,6 +14,7 @@ package org.simantics.sysdyn.ui.properties.widgets.expressions;
 import java.util.List;\r
 import java.util.Map;\r
 \r
+import org.eclipse.jface.text.IUndoManager;\r
 import org.eclipse.swt.custom.VerifyKeyListener;\r
 import org.eclipse.swt.events.FocusListener;\r
 import org.eclipse.swt.events.KeyListener;\r
@@ -45,4 +46,6 @@ public interface IExpression {
     public void addVerifyKeyListener(VerifyKeyListener listener);\r
 \r
     public void addFocusListener(FocusListener listener);\r
+    \r
+    public IUndoManager getUndoManager();\r
 }\r
index cf0f2304215d8cd5d00e198aacda42812ca2bc2a..db7311b382f86c7a8072c9c48fb6240eff03ad9e 100644 (file)
@@ -14,6 +14,7 @@ package org.simantics.sysdyn.ui.properties.widgets.expressions;
 import java.util.List;\r
 import java.util.Map;\r
 \r
+import org.eclipse.jface.text.IUndoManager;\r
 import org.eclipse.swt.custom.VerifyKeyListener;\r
 import org.eclipse.swt.events.FocusListener;\r
 import org.eclipse.swt.events.KeyListener;\r
@@ -90,4 +91,10 @@ public class LookupExpression implements IExpression {
                \r
        }\r
 \r
+    @Override\r
+    public IUndoManager getUndoManager() {\r
+        // TODO Auto-generated method stub\r
+        return null;\r
+    }\r
+\r
 }\r
index d92c9a1a96ebb6fe7ae2ca8a6ecca3b74f6e9dfa..14f87e966c5bbb535a650126c394ea38df230d1e 100644 (file)
@@ -19,6 +19,8 @@ import org.eclipse.jface.layout.GridDataFactory;
 import org.eclipse.jface.layout.GridLayoutFactory;\r
 import org.eclipse.jface.text.BadLocationException;\r
 import org.eclipse.jface.text.IDocument;\r
+import org.eclipse.jface.text.IUndoManager;\r
+import org.eclipse.jface.text.TextViewerUndoManager;\r
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.custom.VerifyKeyListener;\r
 import org.eclipse.swt.events.FocusAdapter;\r
@@ -33,6 +35,7 @@ import org.eclipse.swt.widgets.Event;
 import org.eclipse.swt.widgets.Label;\r
 import org.eclipse.swt.widgets.Listener;\r
 import org.eclipse.swt.widgets.Table;\r
+import org.simantics.Simantics;\r
 import org.simantics.databoard.Bindings;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
@@ -42,6 +45,7 @@ import org.simantics.db.common.request.ReadRequest;
 import org.simantics.db.common.request.WriteRequest;\r
 import org.simantics.db.common.utils.ListUtils;\r
 import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
 import org.simantics.db.layer0.util.RemoverUtil;\r
 import org.simantics.db.request.Read;\r
 import org.simantics.layer0.Layer0;\r
@@ -50,7 +54,6 @@ import org.simantics.sysdyn.manager.SysdynModel;
 import org.simantics.sysdyn.manager.SysdynModelManager;\r
 import org.simantics.sysdyn.representation.Stock;\r
 import org.simantics.sysdyn.representation.Valve;\r
-import org.simantics.ui.SimanticsUI;\r
 import org.simantics.utils.datastructures.Pair;\r
 \r
 public class StockExpression implements IExpression {\r
@@ -58,6 +61,7 @@ public class StockExpression implements IExpression {
     private final ExpressionWidgetInput input;\r
     private ExpressionField integralExpression, initialExpression;\r
     private ExpressionField lastSelectedText;\r
+    private TextViewerUndoManager undoManager;\r
     \r
     public StockExpression(ExpressionWidgetInput input) {\r
         this.input = input;\r
@@ -88,6 +92,10 @@ public class StockExpression implements IExpression {
             }\r
         });\r
         \r
+        undoManager = new TextViewerUndoManager(100);\r
+        integralExpression.getSourceViewer().setUndoManager(undoManager);\r
+        undoManager.connect(integralExpression.getSourceViewer());\r
+        \r
         Button defaultButton = new Button(integralComposite, SWT.PUSH);\r
         defaultButton.setText("Use default");\r
         defaultButton.addListener(SWT.Selection, new Listener() {\r
@@ -97,7 +105,7 @@ public class StockExpression implements IExpression {
                        switch (event.type) {\r
                        case SWT.Selection:\r
                                StockExpression.this.integralExpression.setExpression(getDefaultIntegral(input.expression));\r
-                               SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+                               Simantics.getSession().asyncRequest(new WriteRequest() {\r
 \r
                                        @Override\r
                                        public void perform(WriteGraph graph)\r
@@ -152,7 +160,7 @@ public class StockExpression implements IExpression {
         Pair<String, String> equations = null;\r
         if (expression != null && data.get("initialEquation") == null) {\r
                try {\r
-                   equations = SimanticsUI.getSession().syncRequest(new Read<Pair<String, String>>() {\r
+                   equations = Simantics.getSession().syncRequest(new Read<Pair<String, String>>() {\r
        \r
                        @Override\r
                        public Pair<String, String> perform(ReadGraph graph) throws DatabaseException {\r
@@ -176,7 +184,7 @@ public class StockExpression implements IExpression {
 \r
         if (equations.first == null) {\r
                try {\r
-                               SimanticsUI.getSession().syncRequest(new WriteRequest() {\r
+                               Simantics.getSession().syncRequest(new WriteRequest() {\r
 \r
                                        @Override\r
                                        public void perform(WriteGraph graph) throws DatabaseException {\r
@@ -197,7 +205,7 @@ public class StockExpression implements IExpression {
         } else {\r
                try {\r
                        final String integral = equations.first;\r
-                               SimanticsUI.getSession().syncRequest(new ReadRequest() {\r
+                               Simantics.getSession().syncRequest(new ReadRequest() {\r
 \r
                                        @Override\r
                                        public void run(ReadGraph graph) throws DatabaseException {\r
@@ -242,7 +250,7 @@ public class StockExpression implements IExpression {
         if(!currentInitial.equals(data.get("initialEquation"))\r
                        || currentIntegral == null\r
                        || !currentIntegral.equals(data.get("integral"))) {\r
-            SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
+            Simantics.getSession().asyncRequest(new WriteRequest() {\r
 \r
                 @Override\r
                 public void perform(WriteGraph g)\r
@@ -275,10 +283,11 @@ public class StockExpression implements IExpression {
                     }\r
                     g.claimLiteral(expression, sr.StockExpression_integralEquation, currentIntegral);\r
                     \r
+                    updateData(data);\r
+                    \r
                     g.markUndoPoint();\r
-                               CommentMetadata cm = g.getMetadata(CommentMetadata.class);\r
-                               g.addMetadata(cm.add("Save"));\r
-\r
+                    \r
+                    Layer0Utils.addCommentMetadata(g, "Saved Stock Expression " + currentIntegral + " " + expression.toString() + " with Initial Value " + currentInitial);\r
                 }\r
 \r
             });\r
@@ -336,7 +345,7 @@ public class StockExpression implements IExpression {
         if(expression == null)\r
                return integral;\r
         try {\r
-            integral = SimanticsUI.getSession().syncRequest(new Read<String>() {\r
+            integral = Simantics.getSession().syncRequest(new Read<String>() {\r
 \r
                 @Override\r
                 public String perform(ReadGraph graph) throws DatabaseException {\r
@@ -376,4 +385,9 @@ public class StockExpression implements IExpression {
                this.integralExpression.getSourceViewer().getTextWidget().addVerifyKeyListener(listener);\r
        }\r
 \r
+    @Override\r
+    public IUndoManager getUndoManager() {\r
+        return undoManager;\r
+    }\r
+\r
 }\r
index 66132ab91a2088480ca0eb9d5ef86519d5d6fc76..5051f5c1d72456829a2f3d21e034fd73745d22ee 100644 (file)
@@ -28,6 +28,7 @@ import org.eclipse.jface.layout.GridDataFactory;
 import org.eclipse.jface.layout.GridLayoutFactory;\r
 import org.eclipse.jface.text.BadLocationException;\r
 import org.eclipse.jface.text.IDocument;\r
+import org.eclipse.jface.text.IUndoManager;\r
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.awt.SWT_AWT;\r
 import org.eclipse.swt.custom.VerifyKeyListener;\r
@@ -459,4 +460,10 @@ public class WithLookupExpression implements IExpression {
         smallPanel.getChart().getXYPlot().setDataset(dataset);\r
     }\r
 \r
+    @Override\r
+    public IUndoManager getUndoManager() {\r
+        // TODO Auto-generated method stub\r
+        return null;\r
+    }\r
+\r
 }\r