From: Tuukka Lehtonen Date: Mon, 7 Nov 2016 19:42:56 +0000 (+0200) Subject: Sync git svn branch with SVN repository r33366. X-Git-Tag: v1.25.0~62^2 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=a2a42428426818a7498d5a1705603fb8d3a8a95a;p=simantics%2Fplatform.git Sync git svn branch with SVN repository r33366. refs #6793 --- diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/wizard/MigrateWizard.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/wizard/MigrateWizard.java index f0eee7529..533a43034 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/wizard/MigrateWizard.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/wizard/MigrateWizard.java @@ -11,10 +11,7 @@ *******************************************************************************/ package org.simantics.modeling.ui.wizard; -import gnu.trove.map.hash.THashMap; -import gnu.trove.set.hash.THashSet; -import gnu.trove.set.hash.TIntHashSet; - +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -22,6 +19,10 @@ import java.util.Comparator; import java.util.Map; import java.util.Set; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.OperationCanceledException; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.SubMonitor; import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.jface.dialogs.IDialogSettings; @@ -44,7 +45,6 @@ import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; -import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.List; @@ -64,6 +64,7 @@ import org.simantics.db.common.request.UniqueRead; import org.simantics.db.common.request.WriteResultRequest; import org.simantics.db.common.utils.Logger; import org.simantics.db.common.utils.Versions; +import org.simantics.db.exception.CancelTransactionException; import org.simantics.db.exception.DatabaseException; import org.simantics.diagram.stubs.DiagramResource; import org.simantics.layer0.Layer0; @@ -78,6 +79,10 @@ import org.simantics.utils.datastructures.Pair; import org.simantics.utils.datastructures.Triple; import org.simantics.utils.strings.AlphanumComparator; +import gnu.trove.map.hash.THashMap; +import gnu.trove.set.hash.THashSet; +import gnu.trove.set.hash.TIntHashSet; + /** * @author Antti Villberg */ @@ -129,7 +134,7 @@ public class MigrateWizard extends Wizard { Collection ops = migratePage.model.sortedShownInstances; int index = 0; - final ArrayList result = new ArrayList(); + final ArrayList result = new ArrayList<>(); for(MigrationOperation op : ops) { if(sels.contains(index)) result.add(op); index++; @@ -138,58 +143,80 @@ public class MigrateWizard extends Wizard { if(result.isEmpty()) return true; try { - - final String report = Simantics.getSession().syncRequest(new WriteResultRequest() { - @Override - public String perform(WriteGraph graph) throws DatabaseException { - graph.markUndoPoint(); - String report = UserComponentMigration.doMigration(graph, result); - UserComponentMigration.doPostMigration(graph, result); - return report; + String[] report = { null }; + getContainer().run(true, true, monitor -> { + SubMonitor mon = SubMonitor.convert(monitor, 1000); + try { + report[0] = Simantics.getSession().syncRequest(new WriteResultRequest() { + @Override + public String perform(WriteGraph graph) throws DatabaseException { + graph.markUndoPoint(); + String report = UserComponentMigration.doMigration(mon.newChild(500, SubMonitor.SUPPRESS_NONE), graph, result); + UserComponentMigration.doPostMigration(mon.newChild(500, SubMonitor.SUPPRESS_NONE), graph, result); + mon.setTaskName("Committing Changes"); + mon.subTask(""); + return report; + } + }); + } catch (DatabaseException e) { + throw new InvocationTargetException(e); + } finally { + monitor.done(); } }); - class InfoMessageDialog extends MessageDialog { - - public InfoMessageDialog(Shell shell) { - super(shell, - "Migration report", null, - "", - MessageDialog.INFORMATION, new String[] { "Continue" }, 0); - } - - @Override - protected boolean isResizable() { - return true; - } - - @Override - protected Point getInitialSize() { - return new Point(800, 500); - } - - @Override - protected Control createCustomArea(Composite composite) { - - GridLayoutFactory.fillDefaults().applyTo(composite); - Text text = new Text(composite, SWT.MULTI | SWT.V_SCROLL | SWT.READ_ONLY | SWT.BORDER); - text.setText(report); - GridDataFactory.fillDefaults().grab(true, true).applyTo(text); - return composite; - - } - - } - - InfoMessageDialog md = new InfoMessageDialog(Display.getCurrent().getActiveShell()); - md.open(); - - - } catch (DatabaseException e) { - Logger.defaultLogError(e); - } - - return true; + + ReportDialog md = new ReportDialog(getShell(), report[0], 800, 500); + md.open(); + + return true; + } catch (InvocationTargetException e) { + // Don't show user cancellations as errors. + Throwable cause = e.getCause(); + if (!(cause instanceof CancelTransactionException || cause instanceof OperationCanceledException)) { + Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "Migration failed, see Error Log for details.", e.getCause())); + } + } catch (InterruptedException e) { + Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + "Migration interrupted, see Error Log for details.", e)); + } + return false; + } + + static class ReportDialog extends MessageDialog { + private final String report; + private final int initialWidth; + private final int initialHeight; + + public ReportDialog(Shell shell, String report, int width, int height) { + super(shell, + "Migration report", null, + "", + MessageDialog.INFORMATION, new String[] { "Continue" }, 0); + this.report = report; + this.initialWidth = width; + this.initialHeight = height; + } + + @Override + protected boolean isResizable() { + return true; + } + + @Override + protected Point getInitialSize() { + return new Point(initialWidth, initialHeight); + } + + @Override + protected Control createCustomArea(Composite composite) { + GridLayoutFactory.fillDefaults().applyTo(composite); + Text text = new Text(composite, SWT.MULTI | SWT.V_SCROLL | SWT.READ_ONLY | SWT.BORDER); + text.setText(report); + GridDataFactory.fillDefaults().grab(true, true).applyTo(text); + return composite; + } } abstract static class SelectionAdapter implements SelectionListener { @@ -414,7 +441,7 @@ public class MigrateWizard extends Wizard { result.add(new NamedResource(name, symbol)); } - return new ArrayList(result); + return new ArrayList<>(result); } @@ -556,7 +583,7 @@ public class MigrateWizard extends Wizard { int locationIndex = locations.getSelectionIndex(); if(locationIndex == -1) return; - model.sortedShownInstances = new ArrayList(); + model.sortedShownInstances = new ArrayList<>(); for(MigrationOperation o : model.instances.get(locationIndex).third) model.sortedShownInstances.add(o); Collections.sort(model.sortedShownInstances, MIGRATION_OP_COMPARATOR); diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/MigrateModel.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/MigrateModel.java index 8e08d8ce6..fe385a6a1 100644 --- a/bundles/org.simantics.modeling/src/org/simantics/modeling/MigrateModel.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/MigrateModel.java @@ -20,6 +20,7 @@ import java.util.List; import java.util.Set; import org.simantics.databoard.Bindings; +import org.simantics.databoard.util.URIStringUtils; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Statement; @@ -63,10 +64,10 @@ public class MigrateModel { sourceURI = sourceURI.replace("http://Projects/Development%20Project/", ""); if(targetSymbol != null) { String targetURI = graph.getURI(targetSymbol.getResource()); - return sourceURI + " into " + targetURI; + return URIStringUtils.unescape(sourceURI) + " into " + URIStringUtils.unescape(targetURI); } else { String targetURI = graph.getURI(targetType.getResource()); - return sourceURI + " into " + targetURI; + return URIStringUtils.unescape(sourceURI) + " into " + URIStringUtils.unescape(targetURI); } } diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/UserComponentMigration.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/UserComponentMigration.java index 6cb08ddfd..9f7f486c8 100644 --- a/bundles/org.simantics.modeling/src/org/simantics/modeling/UserComponentMigration.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/UserComponentMigration.java @@ -19,6 +19,8 @@ import java.util.Collections; import java.util.Comparator; import java.util.Set; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; import org.simantics.Simantics; import org.simantics.databoard.Bindings; import org.simantics.databoard.util.URIStringUtils; @@ -31,6 +33,7 @@ import org.simantics.db.common.request.PossibleIndexRoot; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.common.utils.VersionMap; import org.simantics.db.common.utils.Versions; +import org.simantics.db.exception.CancelTransactionException; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.adapter.Instances; import org.simantics.db.layer0.request.ActiveModels; @@ -65,15 +68,21 @@ public class UserComponentMigration { } } if (!result.isEmpty()) - doMigration(graph, result); + doMigration(new NullProgressMonitor(), graph, result); } - public static String doMigration(WriteGraph graph, final ArrayList result) throws DatabaseException { + public static String doMigration(IProgressMonitor monitor, WriteGraph graph, final ArrayList operations) throws DatabaseException { graph.markUndoPoint(); StringBuilder b = new StringBuilder(); int success = 0; int problem = 0; - for(MigrationOperation op : result) { + int no = 1; + int count = operations.size(); + monitor.setTaskName("Migrating " + count + " User Components"); + for(MigrationOperation op : operations) { + if (monitor.isCanceled()) + throw new CancelTransactionException(); + monitor.subTask("(" + (no++) + "/" + count + ") " + op.toString()); String problems = op.perform(graph); if(problems != null) { b.insert(0, problems); @@ -98,17 +107,19 @@ public class UserComponentMigration { return b.toString(); } - public static void doPostMigration(WriteGraph graph, ArrayList result) throws DatabaseException { + public static void doPostMigration(IProgressMonitor monitor, WriteGraph graph, ArrayList result) throws DatabaseException { THashSet roots = new THashSet<>(); for(MigrationOperation op : result) { Resource root = graph.syncRequest(new PossibleIndexRoot(op.instanceToMigrate.getResource())); if(root != null) roots.add(root); } + if (monitor.isCanceled()) + throw new CancelTransactionException(); for(Resource root : roots) { UserComponentPostMigrationAction action = graph.getPossibleAdapter(root, UserComponentPostMigrationAction.class); if(action != null) - action.perform(graph); + action.perform(monitor, graph); } } diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/migration/UserComponentPostMigrationAction.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/migration/UserComponentPostMigrationAction.java index 5166968bb..e817049fd 100644 --- a/bundles/org.simantics.modeling/src/org/simantics/modeling/migration/UserComponentPostMigrationAction.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/migration/UserComponentPostMigrationAction.java @@ -1,8 +1,9 @@ package org.simantics.modeling.migration; +import org.eclipse.core.runtime.IProgressMonitor; import org.simantics.db.WriteGraph; import org.simantics.db.exception.DatabaseException; public interface UserComponentPostMigrationAction { - public void perform(WriteGraph graph) throws DatabaseException; + public void perform(IProgressMonitor monitor, WriteGraph graph) throws DatabaseException; } diff --git a/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/UpdateComponentUids.java b/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/UpdateComponentUids.java index 840d9ec4c..5ace8a2c3 100644 --- a/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/UpdateComponentUids.java +++ b/bundles/org.simantics.structural.synchronization.client/src/org/simantics/structural/synchronization/base/UpdateComponentUids.java @@ -1,6 +1,8 @@ package org.simantics.structural.synchronization.base; +import org.eclipse.core.runtime.IProgressMonitor; import org.simantics.db.ReadGraph; +import org.simantics.db.exception.CancelTransactionException; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.Variable; @@ -11,17 +13,67 @@ import org.simantics.db.layer0.variable.Variable; * and resource ids have been changed. * * @author Hannu Niemistö + * @author Tuukka Lehtonen */ -public class UpdateComponentUids { - - public static > void update(ReadGraph graph, T component, Variable variable) throws DatabaseException { +public class UpdateComponentUids> { + + public static interface ComponentUpdateProgressMonitor { + void componentsDone(int components, int componentCount); + } + + private IProgressMonitor monitor; + private ComponentUpdateProgressMonitor componentMonitor; + private ReadGraph graph; + private int componentCount; + private int counter; + + private UpdateComponentUids(IProgressMonitor monitor, ReadGraph graph, int componentCount) { + this.monitor = monitor; + this.componentMonitor = monitor instanceof ComponentUpdateProgressMonitor ? (ComponentUpdateProgressMonitor) monitor : null; + this.graph = graph; + this.componentCount = componentCount; + } + + private void update(T component, Variable variable) throws DatabaseException { component.uid = variable.getRVI(graph).toString(); + + // Handle progress monitoring and cancellation + counter++; + if (monitor != null) { + if (componentMonitor != null) + componentMonitor.componentsDone(counter, componentCount); + if (monitor.isCanceled()) + throw new CancelTransactionException(); + } + for(Variable childVariable : variable.getChildren(graph)) { String childName = childVariable.getName(graph); T childComponent = component.getChild(childName); if(childComponent != null) - update(graph, childComponent, childVariable); + update(childComponent, childVariable); } } - + + public static > void update(ReadGraph graph, T component, Variable variable) throws DatabaseException { + update(null, graph, component, variable); + } + + public static > void update(IProgressMonitor monitor, ReadGraph graph, T component, Variable variable) throws DatabaseException { + new UpdateComponentUids(monitor, graph, countComponents(component)).update(component, variable); + } + + public static > int countComponents(T component) { + return new Counter().count(component); + } + + private static class Counter> { + int counter; + int count(T component) { + ++counter; + for (T child : component.getChildren()) + count(child); + return counter; + } + } + }