]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/handlers/e4/SyncCurrentTypicalTemplateToInstances.java
Prevent unnecessary read transaction for synch master typical handle
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / handlers / e4 / SyncCurrentTypicalTemplateToInstances.java
index f262cd56e53a3868e3ef4c86683769513dad9100..bbafb03e0ce201b530166609745ac243fcdd3fd8 100644 (file)
-package org.simantics.modeling.ui.handlers.e4;\r
-\r
-import javax.inject.Named;\r
-\r
-import org.eclipse.e4.core.di.annotations.CanExecute;\r
-import org.eclipse.e4.core.di.annotations.Execute;\r
-import org.eclipse.e4.ui.model.application.ui.basic.MPart;\r
-import org.eclipse.e4.ui.services.IServiceConstants;\r
-import org.eclipse.jface.dialogs.MessageDialog;\r
-import org.eclipse.swt.widgets.Shell;\r
-import org.eclipse.ui.IEditorPart;\r
-import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;\r
-import org.simantics.Simantics;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.Session;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.modeling.typicals.SyncTypicalTemplatesToInstances;\r
-import org.simantics.modeling.ui.documents.OpenPlainTextDocumentAdapter;\r
-import org.simantics.modeling.ui.property.TypicalPropertyTester;\r
-import org.simantics.modeling.ui.typicals.RuleChooserDialog;\r
-import org.simantics.ui.workbench.IResourceEditorInput;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class SyncCurrentTypicalTemplateToInstances {\r
-\r
-    @CanExecute\r
-    public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditor) throws DatabaseException, InterruptedException {\r
-        // TODO: Fix this when we get rid of CompatibilityEditors\r
-        IEditorPart activeEditor = null;\r
-        if (mActiveEditor != null && mActiveEditor.getObject()instanceof CompatibilityEditor) {\r
-            CompatibilityEditor compEditor = (CompatibilityEditor) mActiveEditor.getObject();\r
-            activeEditor = compEditor.getEditor();\r
-        } else {\r
-            // TODO: This is not good practice with E4 but an OK fallback for now\r
-            activeEditor = WorkbenchUtils.getActiveEditor();\r
-        }\r
-        if (activeEditor == null)\r
-            return false;\r
-        IResourceEditorInput input = (IResourceEditorInput) activeEditor.getEditorInput();\r
-        return TypicalPropertyTester.isTypicalMasterEditor(Simantics.getSession(), input.getResource());\r
-    }\r
-    \r
-    @Execute\r
-    public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell, @Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditor) {\r
-        // TODO: Fix this when we get rid of CompatibilityEditors\r
-        IEditorPart activeEditor = null;\r
-        Object editor = mActiveEditor.getObject();\r
-        if (editor instanceof CompatibilityEditor) {\r
-            CompatibilityEditor compEditor = (CompatibilityEditor) editor;\r
-            activeEditor = compEditor.getEditor();\r
-        } else {\r
-            // TODO: This is not good practice with E4 but an OK fallback for now\r
-            activeEditor = WorkbenchUtils.getActiveEditor();\r
-        }\r
-        if (activeEditor == null)\r
-            return;\r
-\r
-        IResourceEditorInput input = (IResourceEditorInput) activeEditor.getEditorInput();\r
-        Session session = Simantics.getSession();\r
-\r
-//        if (!MessageDialog\r
-//                .openConfirm(shell,\r
-//                        "Synchronize Typical Template With Instances",\r
-//                        "Are you sure you want to synchronize all instances of this typical template with the template?"))\r
-//            return null;\r
-\r
-        try {\r
-            if (!TypicalPropertyTester.isTypicalMasterEditor(session, input.getResource())) {\r
-                MessageDialog.openInformation(shell, "Not Synchronizing", "Currently active editor is not a typical diagram template editor.");\r
-                return;\r
-            }\r
-\r
-            RuleChooserDialog.RuleResult result = RuleChooserDialog.choose(shell, "Synchronizing typical template to all its instances.", new Resource[] { input.getResource() });\r
-            if(result == null) return;\r
-\r
-            SyncTypicalTemplatesToInstances req = new SyncTypicalTemplatesToInstances(result.selectedRules, input.getResource()).logging(result.logging); \r
-            session.syncRequest(req);\r
-            if (result.logging) {\r
-                for(Resource log : req.logs)\r
-                    new OpenPlainTextDocumentAdapter().openEditor(log);\r
-            }\r
-        } catch (Exception e) {\r
-            ErrorLogger.defaultLogError(e);\r
-        }\r
-        return;\r
-    }\r
-\r
-}\r
+package org.simantics.modeling.ui.handlers.e4;
+
+import javax.inject.Named;
+
+import org.eclipse.e4.core.di.annotations.CanExecute;
+import org.eclipse.e4.core.di.annotations.Execute;
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;
+import org.eclipse.e4.ui.services.IServiceConstants;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
+import org.simantics.DatabaseJob;
+import org.simantics.Simantics;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.modeling.typicals.SyncTypicalTemplatesToInstances;
+import org.simantics.modeling.ui.diagramEditor.DiagramEditor;
+import org.simantics.modeling.ui.documents.OpenPlainTextDocumentAdapter;
+import org.simantics.modeling.ui.property.TypicalPropertyTester;
+import org.simantics.modeling.ui.typicals.RuleChooserDialog;
+import org.simantics.ui.workbench.IResourceEditorInput;
+import org.simantics.utils.ui.ErrorLogger;
+import org.simantics.utils.ui.workbench.WorkbenchUtils;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class SyncCurrentTypicalTemplateToInstances {
+
+    @CanExecute
+    public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditor) throws DatabaseException, InterruptedException {
+        // TODO: Fix this when we get rid of CompatibilityEditors
+        IEditorPart activeEditor = null;
+        if (mActiveEditor != null && mActiveEditor.getObject()instanceof CompatibilityEditor) {
+            CompatibilityEditor compEditor = (CompatibilityEditor) mActiveEditor.getObject();
+            activeEditor = compEditor.getEditor();
+        } else {
+            // TODO: This is not good practice with E4 but an OK fallback for now
+            activeEditor = WorkbenchUtils.getActiveEditor();
+        }
+        if (activeEditor == null || (!(activeEditor instanceof DiagramEditor)))
+            return false;
+        if (DatabaseJob.inProgress())
+            return false;
+        if (activeEditor.getEditorInput() instanceof IResourceEditorInput) {
+            IResourceEditorInput input = (IResourceEditorInput) activeEditor.getEditorInput();
+            return TypicalPropertyTester.isTypicalMasterEditor(Simantics.getSession(), input.getResource());
+        } else {
+            return false;
+        }
+    }
+    
+    @Execute
+    public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell, @Named(IServiceConstants.ACTIVE_PART) MPart mActiveEditor) {
+        // TODO: Fix this when we get rid of CompatibilityEditors
+        IEditorPart activeEditor = null;
+        Object editor = mActiveEditor.getObject();
+        if (editor instanceof CompatibilityEditor) {
+            CompatibilityEditor compEditor = (CompatibilityEditor) editor;
+            activeEditor = compEditor.getEditor();
+        } else {
+            // TODO: This is not good practice with E4 but an OK fallback for now
+            activeEditor = WorkbenchUtils.getActiveEditor();
+        }
+        if (activeEditor == null)
+            return;
+
+        IResourceEditorInput input = (IResourceEditorInput) activeEditor.getEditorInput();
+        Session session = Simantics.getSession();
+
+//        if (!MessageDialog
+//                .openConfirm(shell,
+//                        "Synchronize Typical Template With Instances",
+//                        "Are you sure you want to synchronize all instances of this typical template with the template?"))
+//            return null;
+
+        try {
+            if (!TypicalPropertyTester.isTypicalMasterEditor(session, input.getResource())) {
+                MessageDialog.openInformation(shell, "Not Synchronizing", "Currently active editor is not a typical diagram template editor.");
+                return;
+            }
+
+            RuleChooserDialog.RuleResult result = RuleChooserDialog.choose(shell, "Synchronizing typical template to all its instances.", new Resource[] { input.getResource() });
+            if(result == null) return;
+
+            session.markUndoPoint();
+            SyncTypicalTemplatesToInstances req = new SyncTypicalTemplatesToInstances(result.selectedRules, input.getResource()).logging(result.logging); 
+            session.syncRequest(req);
+            if (result.logging) {
+                for(Resource log : req.logs)
+                    new OpenPlainTextDocumentAdapter().openEditor(log);
+            }
+        } catch (Exception e) {
+            ErrorLogger.defaultLogError(e);
+        }
+        return;
+    }
+
+}