]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
refs #5093
authorjsimomaa <jsimomaa@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 15 Jul 2014 06:55:25 +0000 (06:55 +0000)
committerjsimomaa <jsimomaa@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 15 Jul 2014 06:55:25 +0000 (06:55 +0000)
Fixing undo problems in Sysdyn by adding graph.markUndoPoint() calls in write transactions

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

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ArrayIndexesTab.java

index cd1f9971cb5192be98e9a2460a08ac100a11d1d8..741cfcc20a92ea7311409075a37a6e3090f2d767 100644 (file)
@@ -29,13 +29,16 @@ import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl;
 import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;\r
+import org.simantics.databoard.Bindings;\r
 import org.simantics.db.ReadGraph;\r
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.common.ResourceArray;\r
+import org.simantics.db.common.request.ReadRequest;\r
 import org.simantics.db.common.utils.ListUtils;\r
 import org.simantics.db.common.utils.NameUtils;\r
 import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
 import org.simantics.db.management.ISessionContext;\r
 import org.simantics.jfreechart.chart.properties.LabelPropertyTabContributor;\r
 import org.simantics.layer0.Layer0;\r
@@ -43,6 +46,7 @@ import org.simantics.sysdyn.SysdynResource;
 import org.simantics.sysdyn.ui.properties.widgets.ColumnKeys;\r
 import org.simantics.utils.RunnableWithObject;\r
 import org.simantics.utils.datastructures.ArrayMap;\r
+import org.simantics.utils.threads.SWTThread;\r
 \r
 public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widget{\r
 \r
@@ -50,6 +54,9 @@ public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widg
        GraphExplorerComposite usedEnumerationsExplorer;\r
        private WidgetSupportImpl buttonSupport;\r
        private org.simantics.browsing.ui.swt.widgets.Label usedEnumerationsLabel;\r
+    private Button down;\r
+    private Button up;\r
+    private Button remove;\r
        \r
        public ArrayIndexesTab(Object variables) {\r
         super(variables);\r
@@ -98,7 +105,7 @@ public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widg
             List<Resource> enumerationResources;\r
 \r
             @Override\r
-            public void beforeApply() {\r
+            public void beforeApply() { \r
                 enumerationResources = getSelectedResources(availableEnumerationsExplorer);\r
             }\r
 \r
@@ -108,16 +115,27 @@ public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widg
                                SysdynResource sr = SysdynResource.getInstance(graph);\r
                                for(Resource r : input.resources) {\r
                                    Resource arrayIndexes = graph.getPossibleObject(r, sr.Variable_arrayIndexesList);\r
+                                   StringBuilder sb = new StringBuilder();\r
+                                   sb.append("Added new enumeration(s) ");\r
                                    if(arrayIndexes == null) {\r
-                                       graph.claim(r, sr.Variable_arrayIndexesList, ListUtils.create(graph, enumerationResources));\r
+                                       arrayIndexes = ListUtils.create(graph, enumerationResources);\r
+                                       graph.claim(r, sr.Variable_arrayIndexesList, arrayIndexes);\r
+                                       sb.append(enumerationResources.toString() + " ");\r
                                    } else {\r
                                        ArrayList<Resource> filtered = new ArrayList<Resource>();\r
                                        for(Resource enumeration : enumerationResources) {\r
-                                           if(ListUtils.getNode(graph, arrayIndexes, enumeration) == null)\r
+                                           if(ListUtils.getNode(graph, arrayIndexes, enumeration) == null) {\r
                                                filtered.add(enumeration);\r
+                                               sb.append(graph.getPossibleRelatedValue2(enumeration, Layer0.getInstance(graph).HasName, Bindings.STRING) + " ");\r
+                                           }\r
                                        }\r
+                                       if (filtered.isEmpty())\r
+                                           return;\r
                                        ListUtils.insertBack(graph, arrayIndexes, filtered);\r
                                    }\r
+                    sb.append("to " + graph.getPossibleRelatedValue2(r, Layer0.getInstance(graph).HasName, Bindings.STRING));\r
+                    Layer0Utils.addCommentMetadata(graph, sb.toString());\r
+                    updateUsedButtons(graph, arrayIndexes);\r
                                }\r
                        }\r
                });\r
@@ -171,7 +189,7 @@ public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widg
                Composite buttons = new Composite(body, SWT.None);\r
                GridLayoutFactory.fillDefaults().applyTo(buttons);\r
                \r
-               Button up = new Button(buttons, buttonSupport, SWT.NONE);\r
+               up = new Button(buttons, buttonSupport, SWT.NONE);\r
                up.setText("Up");\r
                up.addSelectionListener(new SelectionListenerImpl<ResourceArray>(context) {\r
 \r
@@ -194,6 +212,8 @@ public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widg
                     if(arrayIndexes != null) {\r
                         List<Resource> enumerations = ListUtils.toList(graph, arrayIndexes);\r
                         List<Resource> toBeMoved = new ArrayList<Resource>();\r
+                        StringBuilder sb = new StringBuilder();\r
+                        sb.append("Moved enumeration(s) ");\r
                         for(Integer index : selectedIndexes) {\r
                             if(index < enumerations.size()) {\r
                                 Resource enumeration = enumerations.get(index);\r
@@ -201,14 +221,20 @@ public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widg
                             }\r
 \r
                         }\r
-\r
+                        boolean moved = false;\r
                         for(Resource enumeration : toBeMoved) {\r
                             Resource node = ListUtils.getNode(graph, arrayIndexes, enumeration);\r
                             Resource prev = graph.getSingleObject(node, L0.List_Previous);\r
                             if(!arrayIndexes.equals(prev) && !toBeMoved.contains(graph.getPossibleObject(prev, L0.List_Element))) {\r
-                                ListUtils.swapWithPrevious(graph, arrayIndexes, enumeration);\r
+                                sb.append(graph.getPossibleRelatedValue2(enumeration, Layer0.getInstance(graph).HasName, Bindings.STRING) + " ");\r
+                                moved = ListUtils.swapWithPrevious(graph, arrayIndexes, enumeration);\r
                             }\r
                         }\r
+                        if (moved) {\r
+                            sb.append("up");\r
+                            Layer0Utils.addCommentMetadata(graph, sb.toString());\r
+                        }\r
+\r
                     }\r
                 }\r
                 \r
@@ -240,7 +266,7 @@ public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widg
                    }\r
                });\r
 \r
-               Button down = new Button(buttons, buttonSupport, SWT.NONE);\r
+               down = new Button(buttons, buttonSupport, SWT.NONE);\r
                down.setText("Down");\r
                down.addSelectionListener(new SelectionListenerImpl<ResourceArray>(context) {\r
 \r
@@ -264,21 +290,28 @@ public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widg
                            if(arrayIndexes != null) {\r
                                List<Resource> enumerations = ListUtils.toList(graph, arrayIndexes);\r
                                List<Resource> toBeMoved = new ArrayList<Resource>();\r
+                               StringBuilder sb = new StringBuilder();\r
+                               sb.append("Moved enumeration(s) ");\r
                                for(Integer index : selectedIndexes) {\r
                                    if(index < enumerations.size()) {\r
                                        Resource enumeration = enumerations.get(index);\r
                                        toBeMoved.add(0, enumeration); // Make an inverted list. \r
                                    }\r
-\r
                                }\r
-\r
+                               boolean moved = false;\r
                                for(Resource enumeration : toBeMoved) {\r
                             Resource node = ListUtils.getNode(graph, arrayIndexes, enumeration);\r
                             Resource next = graph.getSingleObject(node, L0.List_Next);\r
+                            \r
                             if(!arrayIndexes.equals(next) && !toBeMoved.contains(graph.getPossibleObject(next, L0.List_Element))) {\r
-                                ListUtils.swapWithNext(graph, arrayIndexes, enumeration);\r
+                                sb.append(graph.getPossibleRelatedValue2(enumeration, Layer0.getInstance(graph).HasName, Bindings.STRING) + " ");\r
+                                moved = ListUtils.swapWithNext(graph, arrayIndexes, enumeration);\r
                                    }\r
                                }\r
+                        if (moved) {\r
+                            sb.append("down");\r
+                            Layer0Utils.addCommentMetadata(graph, sb.toString());\r
+                        }\r
                            }\r
                        }\r
                        \r
@@ -312,7 +345,7 @@ public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widg
                });\r
 \r
                \r
-               Button remove = new Button(buttons, buttonSupport, SWT.NONE);\r
+               remove = new Button(buttons, buttonSupport, SWT.NONE);\r
                remove.setText("Remove");\r
                remove.addSelectionListener(new SelectionListenerImpl<ResourceArray>(context) {\r
                    \r
@@ -340,9 +373,15 @@ public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widg
                                }\r
 \r
                                }\r
-                               \r
-                               for(Resource enumeration : toBeRemoved)\r
+                               StringBuilder sb = new StringBuilder();\r
+                               sb.append("Removed enumeration(s) ");\r
+                               for(Resource enumeration : toBeRemoved) {\r
                                    ListUtils.removeElement(graph, arrayIndexes, enumeration);\r
+                                   sb.append(graph.getPossibleRelatedValue2(enumeration, Layer0.getInstance(graph).HasName, Bindings.STRING) + " ");\r
+                               }\r
+                               sb.append("from " + graph.getPossibleRelatedValue2(variable, Layer0.getInstance(graph).HasName, Bindings.STRING));\r
+                               Layer0Utils.addCommentMetadata(graph, sb.toString());\r
+                               updateUsedButtons(graph, arrayIndexes);\r
                            }\r
                        }\r
                    }\r
@@ -350,6 +389,33 @@ public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widg
        }\r
 \r
        \r
+    protected void updateUsedButtons(ReadGraph graph, Resource arrayIndexes) {\r
+        boolean enabled = false;\r
+        if (arrayIndexes == null)\r
+            enabled = false;\r
+        else {\r
+            try {\r
+                List<Resource> currEnumerations = ListUtils.toList(graph, arrayIndexes);\r
+                if (!currEnumerations.isEmpty()) {\r
+                    enabled = true;\r
+                }\r
+            } catch (DatabaseException e) {\r
+                e.printStackTrace();\r
+            }\r
+        }\r
+        final boolean finalEnabled = enabled;\r
+        SWTThread.getThreadAccess().asyncExec(new Runnable() {\r
+            \r
+            @Override\r
+            public void run() {\r
+                up.getControl().setEnabled(finalEnabled);\r
+                down.getControl().setEnabled(finalEnabled);\r
+                remove.getControl().setEnabled(finalEnabled);     \r
+            }\r
+        });\r
+    }\r
+\r
+\r
        private List<Resource> getSelectedResources(GraphExplorerComposite explorer) {\r
                List<Resource> result = new ArrayList<Resource>();\r
                \r
@@ -400,15 +466,27 @@ public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widg
                 resourceArray = new ResourceArray((Resource)first);\r
             else if( first instanceof Collection<?>)\r
                 resourceArray = new ResourceArray(((Collection<?>)first).toArray(new Resource[((Collection<?>)first).size()]));\r
-            \r
+            final ResourceArray finalResourceArray = resourceArray;\r
             if(resourceArray != null) {\r
-                StructuredSelection selection = new StructuredSelection(resourceArray);\r
+                StructuredSelection selection = new StructuredSelection(finalResourceArray);\r
                 availableEnumerationsExplorer.setInput(context, selection);\r
                 usedEnumerationsExplorer.setInput(context, selection);    \r
                 buttonSupport.fireInput(context, selection);\r
+                \r
+                try {\r
+                    context.getSession().syncRequest(new ReadRequest() {\r
+                        \r
+                        @Override\r
+                        public void run(ReadGraph graph) throws DatabaseException {\r
+                            Resource r = finalResourceArray.resources[0];\r
+                            Resource arrayIndexes = graph.getPossibleObject(r, SysdynResource.getInstance(graph).Variable_arrayIndexesList);\r
+                            updateUsedButtons(graph, arrayIndexes);\r
+                        }\r
+                    });\r
+                } catch (DatabaseException e) {\r
+                    e.printStackTrace();\r
+                }\r
             }\r
         }\r
-        \r
-\r
        }\r
 }\r