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