]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
refs #5093
authorjsimomaa <jsimomaa@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 15 Jul 2014 12:06:11 +0000 (12:06 +0000)
committerjsimomaa <jsimomaa@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 15 Jul 2014 12:06:11 +0000 (12:06 +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@29891 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ExternalFilesTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/FunctionTab.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java
org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/SharedFunctionLibrariesTab.java

index f2a98f76167ad4b727b629e394aa989a7fca7a2b..fb32aa51533e46c0bb0a2aa52bb3bfe2ba1e1609 100644 (file)
@@ -12,6 +12,7 @@
 package org.simantics.sysdyn.ui.properties;\r
 \r
 import java.util.ArrayList;\r
+import java.util.Arrays;\r
 import java.util.List;\r
 \r
 import org.eclipse.jface.dialogs.MessageDialog;\r
@@ -23,6 +24,8 @@ import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.widgets.Composite;\r
 import org.eclipse.swt.widgets.Control;\r
+import org.eclipse.swt.widgets.Event;\r
+import org.eclipse.swt.widgets.Listener;\r
 import org.eclipse.swt.widgets.Shell;\r
 import org.eclipse.swt.widgets.Tree;\r
 import org.eclipse.ui.IWorkbenchSite;\r
@@ -35,17 +38,18 @@ import org.simantics.browsing.ui.swt.widgets.impl.Widget;
 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\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.common.utils.NameUtils;\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.management.ISessionContext;\r
 import org.simantics.jfreechart.chart.properties.LabelPropertyTabContributor;\r
 import org.simantics.sysdyn.SysdynResource;\r
-import org.simantics.sysdyn.ui.handlers.RemoveNodeHandler;\r
 import org.simantics.sysdyn.ui.handlers.exports.ExportExternalFunctionFilesHandler;\r
 import org.simantics.sysdyn.ui.handlers.imports.ImportExternalFunctionFilesHandler;\r
-import org.simantics.ui.SimanticsUI;\r
 import org.simantics.utils.datastructures.ArrayMap;\r
 import org.simantics.utils.datastructures.Pair;\r
+import org.simantics.utils.threads.SWTThread;\r
 \r
 /**\r
  * Tab for properties of a SysdynModelicaFunction containing all external files added to that function \r
@@ -81,8 +85,17 @@ public class ExternalFilesTab extends LabelPropertyTabContributor implements Wid
                                Resource.class));\r
 \r
                externalFilesExplorer.setContextMenuId("#ExternalFunctionFileBrowser");\r
+               \r
+               externalFilesExplorer.addListenerToControl(SWT.Selection, new Listener() {\r
+            \r
+            @Override\r
+            public void handleEvent(Event event) {\r
+                updateButtons(externalFilesExplorer);\r
+            }\r
+        });\r
+               \r
                externalFilesExplorer.finish();\r
-\r
+               \r
                GridDataFactory.fillDefaults().grab(true, true).applyTo(\r
                                externalFilesExplorer);\r
                \r
@@ -100,34 +113,26 @@ public class ExternalFilesTab extends LabelPropertyTabContributor implements Wid
                importButton.setText("Import");\r
                importButton.addSelectionListener(new SelectionListenerImpl<Resource>(context) {\r
 \r
+                   private Pair<String, String[]> importedFiles; \r
+                   \r
+                   @Override\r
+                   public void beforeApply() {\r
+                Shell shell = importButton.getWidget().getShell();\r
+                importedFiles = ImportExternalFunctionFilesHandler.importFiles(shell, "Import files", ImportExternalFunctionFilesHandler.C_EXTENSIONS);                        \r
+                   }\r
+                   \r
                        @Override\r
-                       public void apply(WriteGraph graph, final Resource input)\r
-                                       throws DatabaseException {\r
-                               \r
-                               importButton.getWidget().getDisplay().asyncExec(new Runnable() {\r
-                                       \r
-                                       @Override\r
-                                       public void run() {\r
-                                               Shell shell = importButton.getWidget().getShell();\r
-                                               \r
-                                               final Pair<String, String[]> importedFiles = ImportExternalFunctionFilesHandler.importFiles(shell, "Import files", ImportExternalFunctionFilesHandler.C_EXTENSIONS);\r
-                                               \r
-                                               SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
-\r
-                                                       @Override\r
-                                                       public void perform(WriteGraph graph) throws DatabaseException {\r
-                                                               ImportExternalFunctionFilesHandler.addFilesToFunction(graph, input, importedFiles);\r
-                                                       }\r
-                                               });\r
-                                       }\r
-                               });\r
-\r
-\r
+                       public void apply(WriteGraph graph, final Resource input) throws DatabaseException {\r
+                           graph.markUndoPoint();\r
+                ImportExternalFunctionFilesHandler.addFilesToFunction(graph, input, importedFiles);\r
+                Layer0Utils.addCommentMetadata(graph, "Imported External File(s) " + Arrays.toString(importedFiles.second) + " to " + NameUtils.getSafeName(graph, input));\r
+                updateButtons(externalFilesExplorer);\r
                        }\r
                });\r
 \r
                exportButton = new Button(buttonRow, support, SWT.NONE);\r
                exportButton.setText("Export");\r
+               exportButton.getControl().setEnabled(false);\r
                exportButton.addSelectionListener(new SelectionListenerImpl<Resource>(context) {\r
 \r
                        @Override\r
@@ -145,39 +150,46 @@ public class ExternalFilesTab extends LabelPropertyTabContributor implements Wid
                                                        ExportExternalFunctionFilesHandler.exportFiles(shell, resources);\r
                                        }\r
                                });\r
-\r
-\r
                        }\r
                });\r
 \r
                removeButton = new Button(buttonRow, support, SWT.NONE);\r
                removeButton.setText("Remove");\r
+               removeButton.getControl().setEnabled(false);\r
                removeButton.addSelectionListener(new SelectionListenerImpl<Resource>(context) {\r
 \r
+                   private int delete;\r
+                   private List<Resource> resourceList;\r
+                   \r
+                   @Override\r
+                   public void beforeApply() {\r
+                Shell shell = removeButton.getWidget().getShell();\r
+\r
+                resourceList = getSelectedResources(externalFilesExplorer);\r
+                Resource[] resources = resourceList.toArray(new Resource[resourceList.size()]);\r
+                if(resources.length > 0) {\r
+                    MessageDialog dialog = new MessageDialog(shell, resources.length > 1 ? "Remove selected items" : "Remove selected item" , null, "Are you sure?", 0,\r
+                            new String[] { "OK", "Cancel" }, 0);\r
+                    dialog.create();\r
+                    delete = dialog.open();\r
+                }\r
+                   }\r
+                   \r
                        @Override\r
-                       public void apply(WriteGraph graph, final Resource input)\r
-                       throws DatabaseException {\r
-\r
-                               removeButton.getWidget().getDisplay().asyncExec(new Runnable() {\r
-\r
-                                       @Override\r
-                                       public void run() {\r
-                                               Shell shell = removeButton.getWidget().getShell();\r
-\r
-                                               List<Resource> resourceList = getSelectedResources(externalFilesExplorer);\r
-                                               Resource[] resources = resourceList.toArray(new Resource[resourceList.size()]);\r
-                                               if(resources.length > 0) {\r
-                                                       MessageDialog dialog = new MessageDialog(shell, resources.length > 1 ? "Remove selected items" : "Remove selected item" , null, "Are you sure?", 0,\r
-                                                                       new String[] { "OK", "Cancel" }, 0);\r
-                                                       dialog.create();\r
-                                                       if (dialog.open() == 0) {\r
-                                                               RemoveNodeHandler.deleteItem(resources);\r
-                                                       }\r
-                                               }\r
-                                       }\r
-                               });\r
-\r
-\r
+                       public void apply(WriteGraph graph, final Resource input) throws DatabaseException {\r
+                           if (delete == 0) {\r
+                    Resource[] resources = resourceList.toArray(new Resource[resourceList.size()]);\r
+                    StringBuilder sb = new StringBuilder();\r
+                    sb.append("Removed External File(s) ");\r
+                    for (Resource resource : resources) {\r
+                        RemoverUtil.remove(graph, resource);\r
+                        sb.append(NameUtils.getSafeName(graph, resource) + " ");\r
+                    }\r
+                    sb.append("from " + NameUtils.getSafeName(graph, input));\r
+                    Layer0Utils.addCommentMetadata(graph, sb.toString());\r
+                    \r
+                    updateButtons(externalFilesExplorer);\r
+                           }\r
                        }\r
                });\r
        }\r
@@ -185,6 +197,7 @@ public class ExternalFilesTab extends LabelPropertyTabContributor implements Wid
        @Override\r
        public void setInput(ISessionContext context, Object input) {\r
                externalFilesExplorer.setInput(context, input);\r
+               updateButtons(externalFilesExplorer);\r
        }\r
 \r
        /**\r
@@ -194,7 +207,6 @@ public class ExternalFilesTab extends LabelPropertyTabContributor implements Wid
         */\r
        private List<Resource> getSelectedResources(GraphExplorerComposite explorer) {\r
                List<Resource> result = new ArrayList<Resource>();\r
-               \r
                ISelection selection = ((ISelectionProvider) explorer\r
                                .getAdapter(ISelectionProvider.class)).getSelection();\r
                if (selection == null)\r
@@ -208,5 +220,30 @@ public class ExternalFilesTab extends LabelPropertyTabContributor implements Wid
                }\r
                return result;\r
        }\r
-\r
+       \r
+       protected void updateButtons(final GraphExplorerComposite explorer) {\r
+           if (SWTThread.getThreadAccess().currentThreadAccess()) {\r
+               runnable.run();\r
+           } else {\r
+               SWTThread.getThreadAccess().asyncExec(runnable);\r
+           }\r
+       }\r
+       \r
+       Runnable runnable = new Runnable() {\r
+        \r
+        @Override\r
+        public void run() {\r
+            ISelection selection = ((ISelectionProvider) externalFilesExplorer.getAdapter(ISelectionProvider.class)).getSelection();\r
+            \r
+            IStructuredSelection iss = (IStructuredSelection) selection;\r
+            @SuppressWarnings("unchecked")\r
+            List<AdaptableHintContext> selections = iss.toList();\r
+            \r
+            boolean enabled = false;\r
+            if (!selections.isEmpty()) \r
+                enabled = true;\r
+            removeButton.getControl().setEnabled(enabled);\r
+            exportButton.getControl().setEnabled(enabled);\r
+        }\r
+    };\r
 }\r
index c0944c03dda770532dd89a2328f721c2db70b1b0..c4564eaf2bfb3f03738da6a10054244eb9f44841 100644 (file)
@@ -17,7 +17,6 @@ import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Composite;\r
 import org.eclipse.swt.widgets.Group;\r
 import org.eclipse.ui.IWorkbenchSite;\r
-import org.lobobrowser.util.ID;\r
 import org.simantics.browsing.ui.swt.widgets.Label;\r
 import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory;\r
 import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier;\r
@@ -25,17 +24,16 @@ import org.simantics.browsing.ui.swt.widgets.TrackedText;
 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.common.request.IsParent;\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.jfreechart.chart.properties.AdjustableTab;\r
 import org.simantics.layer0.Layer0;\r
-import org.simantics.sysdyn.SysdynResource;\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.factories.FunctionNameInputValidator;\r
 import org.simantics.sysdyn.ui.properties.widgets.functions.FunctionCodeWidget;\r
+import org.simantics.sysdyn.utils.SelectionUtils;\r
 \r
 public class FunctionTab extends AdjustableTab {\r
        \r
@@ -100,8 +98,7 @@ public class FunctionTab extends AdjustableTab {
 \r
                     @Override\r
                     public Boolean perform(ReadGraph graph) throws DatabaseException {\r
-                        boolean editable = new IsParent(SysdynResource.getInstance(graph).Built$in_Functions, r).perform(graph);\r
-                        return !editable;\r
+                        return SelectionUtils.canEdit(graph, r);\r
                     }\r
                 });\r
                 information.setEditable(editable);\r
index 11ef3069e7faf981908ec6cd5b6a3ee76f721db4..ab129cfb2a160126fb4d5fdefb20720215460fca 100644 (file)
@@ -44,6 +44,7 @@ import org.simantics.sysdyn.JFreeChartResource;
 import org.simantics.sysdyn.SysdynResource;\r
 import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionsFolder;\r
 import org.simantics.sysdyn.ui.trend.SensitivityChartAxisAndVariablesTab;\r
+import org.simantics.sysdyn.utils.SelectionUtils;\r
 import org.simantics.ui.selection.AnyVariable;\r
 import org.simantics.ui.selection.WorkbenchSelectionElement;\r
 import org.simantics.utils.ui.AdaptionUtils;\r
@@ -454,11 +455,13 @@ public class ResourceSelectionProcessor implements SelectionProcessor<Object, Re
                         2,\r
                         r,\r
                         "Function"));\r
-                tabs.add(new ComparableTabContributor(\r
+                if (SelectionUtils.canEdit(backend, r)) {\r
+                    tabs.add(new ComparableTabContributor(\r
                         new ExternalFilesTab(r),\r
                         1,\r
                         r,\r
                         "External files"));\r
+                }\r
                 return tabs;\r
             }\r
 \r
index b8b08e41ae965dbaf77ceb2f01ab5641de7ca65c..a130a91c2e0f21f642c3a9d0fc88481600bd4e5a 100644 (file)
@@ -36,7 +36,9 @@ import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
 import org.simantics.db.Resource;\r
 import org.simantics.db.WriteGraph;\r
 import org.simantics.db.common.request.WriteRequest;\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
@@ -130,9 +132,14 @@ public class SharedFunctionLibrariesTab extends LabelPropertyTabContributor impl
                            throws DatabaseException {\r
                        if(selectedLibraries != null) {\r
                            Layer0 l0 = Layer0.getInstance(graph);\r
-                           for(Resource library : selectedLibraries) {\r
-                               graph.claim(input, l0.IsLinkedTo, library);\r
-                           }\r
+                    StringBuilder sb = new StringBuilder();\r
+                    sb.append("Added Shared Function Library ");\r
+                    for(Resource library : selectedLibraries) {\r
+                        graph.claim(input, l0.IsLinkedTo, library);\r
+                        sb.append(NameUtils.getSafeName(graph, library) + " ");\r
+                    }\r
+                    sb.append("to " + NameUtils.getSafeName(graph, input));\r
+                    Layer0Utils.addCommentMetadata(graph,  sb.toString());\r
                        }\r
                    }\r
                });\r
@@ -153,9 +160,14 @@ public class SharedFunctionLibrariesTab extends LabelPropertyTabContributor impl
                                throws DatabaseException {\r
                            if(selectedLibraries != null) {\r
                                Layer0 l0 = Layer0.getInstance(graph);\r
+                               StringBuilder sb = new StringBuilder();\r
+                               sb.append("Removed Shared Function Library ");\r
                                for(Resource library : selectedLibraries) {\r
                                    graph.deny(input, l0.IsLinkedTo, library);\r
+                                   sb.append(NameUtils.getSafeName(graph, library) + " ");\r
                                }\r
+                               sb.append("from " + NameUtils.getSafeName(graph, input));\r
+                               Layer0Utils.addCommentMetadata(graph,  sb.toString());\r
                            }\r
                        }\r
                });\r