Sync git svn branch with SVN repository r33366.
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / wizard / MigrateWizard.java
index f0eee752920fad488de5d51018a11b96f2e089e4..533a4303483689340b26e1878fb885377c1d8743 100644 (file)
  *******************************************************************************/\r
 package org.simantics.modeling.ui.wizard;\r
 \r
-import gnu.trove.map.hash.THashMap;\r
-import gnu.trove.set.hash.THashSet;\r
-import gnu.trove.set.hash.TIntHashSet;\r
-\r
+import java.lang.reflect.InvocationTargetException;\r
 import java.util.ArrayList;\r
 import java.util.Collection;\r
 import java.util.Collections;\r
@@ -22,6 +19,10 @@ import java.util.Comparator;
 import java.util.Map;\r
 import java.util.Set;\r
 \r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.OperationCanceledException;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.core.runtime.SubMonitor;\r
 import org.eclipse.core.runtime.preferences.IEclipsePreferences;\r
 import org.eclipse.core.runtime.preferences.InstanceScope;\r
 import org.eclipse.jface.dialogs.IDialogSettings;\r
@@ -44,7 +45,6 @@ import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Button;\r
 import org.eclipse.swt.widgets.Composite;\r
 import org.eclipse.swt.widgets.Control;\r
-import org.eclipse.swt.widgets.Display;\r
 import org.eclipse.swt.widgets.Event;\r
 import org.eclipse.swt.widgets.Label;\r
 import org.eclipse.swt.widgets.List;\r
@@ -64,6 +64,7 @@ import org.simantics.db.common.request.UniqueRead;
 import org.simantics.db.common.request.WriteResultRequest;\r
 import org.simantics.db.common.utils.Logger;\r
 import org.simantics.db.common.utils.Versions;\r
+import org.simantics.db.exception.CancelTransactionException;\r
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.diagram.stubs.DiagramResource;\r
 import org.simantics.layer0.Layer0;\r
@@ -78,6 +79,10 @@ import org.simantics.utils.datastructures.Pair;
 import org.simantics.utils.datastructures.Triple;\r
 import org.simantics.utils.strings.AlphanumComparator;\r
 \r
+import gnu.trove.map.hash.THashMap;\r
+import gnu.trove.set.hash.THashSet;\r
+import gnu.trove.set.hash.TIntHashSet;\r
+\r
 /**\r
  * @author Antti Villberg\r
  */\r
@@ -129,7 +134,7 @@ public class MigrateWizard extends Wizard {
 \r
         Collection<MigrationOperation> ops = migratePage.model.sortedShownInstances;\r
         int index = 0;\r
-        final ArrayList<MigrationOperation> result = new ArrayList<MigrationOperation>();\r
+        final ArrayList<MigrationOperation> result = new ArrayList<>();\r
         for(MigrationOperation op : ops) {\r
                if(sels.contains(index)) result.add(op);\r
                index++;\r
@@ -138,58 +143,80 @@ public class MigrateWizard extends Wizard {
         if(result.isEmpty()) return true;\r
         \r
         try {\r
-            \r
-            final String report = Simantics.getSession().syncRequest(new WriteResultRequest<String>() {\r
-                @Override\r
-                public String perform(WriteGraph graph) throws DatabaseException {\r
-                    graph.markUndoPoint();\r
-                    String report = UserComponentMigration.doMigration(graph, result);\r
-                    UserComponentMigration.doPostMigration(graph, result);\r
-                    return report;\r
+            String[] report = { null };\r
+            getContainer().run(true, true, monitor -> {\r
+                SubMonitor mon = SubMonitor.convert(monitor, 1000);\r
+                try {\r
+                    report[0] = Simantics.getSession().syncRequest(new WriteResultRequest<String>() {\r
+                        @Override\r
+                        public String perform(WriteGraph graph) throws DatabaseException {\r
+                            graph.markUndoPoint();\r
+                            String report = UserComponentMigration.doMigration(mon.newChild(500, SubMonitor.SUPPRESS_NONE), graph, result);\r
+                            UserComponentMigration.doPostMigration(mon.newChild(500, SubMonitor.SUPPRESS_NONE), graph, result);\r
+                            mon.setTaskName("Committing Changes");\r
+                            mon.subTask("");\r
+                            return report;\r
+                        }\r
+                    });\r
+                } catch (DatabaseException e) {\r
+                    throw new InvocationTargetException(e);\r
+                } finally {\r
+                    monitor.done();\r
                 }\r
             });\r
 \r
-                       class InfoMessageDialog extends MessageDialog {\r
-\r
-                               public InfoMessageDialog(Shell shell) {\r
-                                       super(shell, \r
-                                                       "Migration report", null, \r
-                                                       "", \r
-                                                       MessageDialog.INFORMATION, new String[] { "Continue" }, 0);\r
-                               }\r
-                               \r
-                               @Override\r
-                               protected boolean isResizable() {\r
-                                       return true;\r
-                               }\r
-                               \r
-                               @Override\r
-                               protected Point getInitialSize() {\r
-                           return new Point(800, 500);\r
-                               }\r
-\r
-                               @Override\r
-                               protected Control createCustomArea(Composite composite) {\r
-                                       \r
-                                       GridLayoutFactory.fillDefaults().applyTo(composite);\r
-                                       Text text = new Text(composite, SWT.MULTI | SWT.V_SCROLL | SWT.READ_ONLY | SWT.BORDER);\r
-                                       text.setText(report);\r
-                                       GridDataFactory.fillDefaults().grab(true, true).applyTo(text);\r
-                                       return composite;\r
-                                       \r
-                               }\r
-\r
-                       }\r
-\r
-                       InfoMessageDialog md = new InfoMessageDialog(Display.getCurrent().getActiveShell());\r
-                       md.open();\r
-                       \r
-                       \r
-               } catch (DatabaseException e) {\r
-                       Logger.defaultLogError(e);\r
-               }\r
-       \r
-        return true;\r
+\r
+            ReportDialog md = new ReportDialog(getShell(), report[0], 800, 500);\r
+            md.open();\r
+\r
+            return true;\r
+        } catch (InvocationTargetException e) {\r
+            // Don't show user cancellations as errors.\r
+            Throwable cause = e.getCause();\r
+            if (!(cause instanceof CancelTransactionException || cause instanceof OperationCanceledException)) {\r
+                Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,\r
+                        "Migration failed, see Error Log for details.", e.getCause()));\r
+            }\r
+        } catch (InterruptedException e) {\r
+            Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,\r
+                    "Migration interrupted, see Error Log for details.", e));\r
+        }\r
+        return false;\r
+    }\r
+\r
+    static class ReportDialog extends MessageDialog {\r
+        private final String report;\r
+        private final int initialWidth;\r
+        private final int initialHeight;\r
+\r
+        public ReportDialog(Shell shell, String report, int width, int height) {\r
+            super(shell, \r
+                    "Migration report", null, \r
+                    "", \r
+                    MessageDialog.INFORMATION, new String[] { "Continue" }, 0);\r
+            this.report = report;\r
+            this.initialWidth = width;\r
+            this.initialHeight = height;\r
+        }\r
+\r
+        @Override\r
+        protected boolean isResizable() {\r
+            return true;\r
+        }\r
+\r
+        @Override\r
+        protected Point getInitialSize() {\r
+            return new Point(initialWidth, initialHeight);\r
+        }\r
+\r
+        @Override\r
+        protected Control createCustomArea(Composite composite) {\r
+            GridLayoutFactory.fillDefaults().applyTo(composite);\r
+            Text text = new Text(composite, SWT.MULTI | SWT.V_SCROLL | SWT.READ_ONLY | SWT.BORDER);\r
+            text.setText(report);\r
+            GridDataFactory.fillDefaults().grab(true, true).applyTo(text);\r
+            return composite;\r
+        }\r
     }\r
 \r
     abstract static class SelectionAdapter implements SelectionListener {\r
@@ -414,7 +441,7 @@ public class MigrateWizard extends Wizard {
                                 result.add(new NamedResource(name, symbol));\r
                         }\r
 \r
-                        return new ArrayList<NamedResource>(result);\r
+                        return new ArrayList<>(result);\r
 \r
                     }\r
 \r
@@ -556,7 +583,7 @@ public class MigrateWizard extends Wizard {
                 int locationIndex = locations.getSelectionIndex();\r
                 if(locationIndex == -1) return;\r
 \r
-                model.sortedShownInstances = new ArrayList<MigrationOperation>();\r
+                model.sortedShownInstances = new ArrayList<>();\r
                 for(MigrationOperation o : model.instances.get(locationIndex).third)\r
                     model.sortedShownInstances.add(o);\r
                 Collections.sort(model.sortedShownInstances, MIGRATION_OP_COMPARATOR);\r