X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.interop.update%2Fsrc%2Forg%2Fsimantics%2Finterop%2Fupdate%2Feditor%2FModelUpdateEditor.java;h=4c172997bd1068f490dd267b86f125760e3af021;hb=98caf73c954a9d8d5b80b13f3ca4e02fee92d87d;hp=18dad814361946b46442952f8172a93e79d88152;hpb=e2f57d7e61d6c52b6341f793f6988c123daa1647;p=simantics%2Finterop.git diff --git a/org.simantics.interop.update/src/org/simantics/interop/update/editor/ModelUpdateEditor.java b/org.simantics.interop.update/src/org/simantics/interop/update/editor/ModelUpdateEditor.java index 18dad81..4c17299 100644 --- a/org.simantics.interop.update/src/org/simantics/interop/update/editor/ModelUpdateEditor.java +++ b/org.simantics.interop.update/src/org/simantics/interop/update/editor/ModelUpdateEditor.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; +import java.util.Map.Entry; import java.util.Stack; import org.eclipse.jface.resource.ImageDescriptor; @@ -81,6 +82,11 @@ public abstract class ModelUpdateEditor extends Composite{ private UpdateTree updateTree; private UpdateList updateList; + private GraphChanges changes2; + private GraphChanges changes3; + private UpdateTree updateTree2; + private UpdateList updateList2; + private Button updateAllButton; private Button updateSelectedButton; @@ -93,8 +99,7 @@ public abstract class ModelUpdateEditor extends Composite{ private Color containsColor; private Color deletedColor; private Color addedColor; - - private HashSet> selected = new HashSet>(); + private HashSet selectedStructure = new HashSet(); @@ -213,23 +218,22 @@ public abstract class ModelUpdateEditor extends Composite{ oldValue.setLabelProvider(getLabelProvider(4)); newValue.setLabelProvider(getLabelProvider(5)); - selection.setLabelProvider(new SelectionLabelProvider(selected)); + selection.setLabelProvider(new SelectionLabelProvider()); selection.getColumn().addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { if (updateList.getChanges().size() > 0) { - if (selected.contains(updateList.getChanges().iterator().next())) { - for (Pair nr : updateList.getChanges()) - selected.remove(nr); + if (updateList.getSelected().size() > 0) { + updateList.clearSelected(); } else { for (Pair nr : updateList.getChanges()) - selected.add(nr); + updateList.addSelected(nr); } changeViewer.refresh(); } } }); - selection.setEditingSupport(new SelectionEditingSupport(changeViewer, selected)); + selection.setEditingSupport(new SelectionEditingSupport(changeViewer)); } Composite buttonComposite = new Composite(this, SWT.NONE); @@ -295,7 +299,7 @@ public abstract class ModelUpdateEditor extends Composite{ } protected abstract ColumnLabelProvider getLabelProvider(int i); - protected abstract Pair getChanges(Resource r1, Resource r2) throws DatabaseException; + protected abstract Pair getChanges(Resource r1, Resource r2) throws DatabaseException; protected abstract UpdateTree getUpdateTree(GraphChanges changes) throws DatabaseException; protected UpdateList getUpdateList(GraphChanges changes) throws DatabaseException { return new UpdateList(changes.getModifications()); @@ -389,6 +393,7 @@ public abstract class ModelUpdateEditor extends Composite{ for (ICheckStateListener l : checkStateListeners) { l.checkStateChanged(new CheckStateChangedEvent(changeBrowser, null, false)); } + changeViewer.refresh(); } @@ -397,21 +402,82 @@ public abstract class ModelUpdateEditor extends Composite{ addFilters(filters); - Resource r1 = uei.getR1(); - Resource r2 = uei.getR2(); - + Resource oldModel = uei.getR1(); // old model that is being updated, contains user made changes + Resource newModel = uei.getR2(); // new model, + Resource originalModel = uei.getR3(); // original old model without user made changes try { - Pair result = getChanges(r1,r2); + if (originalModel != null) { + // tree way comparison + // compare the original and the old model + Pair result2 = getChanges(originalModel, oldModel); + GraphComparator comparator2 = result2.first; + if (result2.second != null) + showWarning(result2.second); + comparator2.test(getSession()); + changes2 = comparator2.getChanges(); + changes2 = getSession().syncRequest(new FilterChangesRead(changes2)); + updateTree2 = getUpdateTree(changes2); + updateList2 = getUpdateList(changes2); + + // compare the original and the new model + Pair result3 = getChanges(originalModel,newModel); + GraphComparator comparator3 = result3.first; + if (result3.second != null) + showWarning(result3.second); + comparator3.test(getSession()); + changes3 = comparator3.getChanges(); + changes3 = getSession().syncRequest(new FilterChangesRead(changes3)); + } + + Pair result = getChanges(oldModel,newModel); GraphComparator comparator = result.first; - if (result.second) - showWarning("Structural symbols have been changed. Model update is not able to update these, please create a new model."); + if (result.second != null) + showWarning(result.second); + if (originalModel != null) { + // three-way comparison: use change information to configure + // the comparison between the old and the new model. + + // 1. map comparable resources + for (Entry origToOld : changes2.getComparable().getEntries()) { + Resource oldR = origToOld.getValue(); + Resource newR = changes3.getComparable().getRight(origToOld.getKey()); + if (newR != null) { + comparator.addComparableResources(oldR, newR); + } + } + // 2. mark removed resources as distinct, so that comparison does not pair them + for (Statement s : changes2.getDeletions()) { + if (changes3.getComparable().containsLeft(s.getObject())) + comparator.addNonMatchedRight(changes3.getComparable().getRight(s.getObject())); + } + + for (Statement s : changes3.getDeletions()) { + if (changes2.getComparable().containsLeft(s.getObject())) + comparator.addNonMatchedLeft(changes2.getComparable().getRight(s.getObject())); + } + if (uei.isNewDistinct()) { + // 3. mark added resources as distinct, so that comparison does not pair them + for (Statement s : changes2.getAdditions()) { + comparator.addNonMatchedLeft(s.getObject()); + } + + for (Statement s : changes3.getAdditions()) { + comparator.addNonMatchedRight(s.getObject()); + } + } + } comparator.test(getSession()); changes = comparator.getChanges(); changes = getSession().syncRequest(new FilterChangesRead(changes)); updateTree = getUpdateTree(changes); updateList = getUpdateList(changes); + + if (originalModel != null) { + createDefaultSelections(); + } + } catch (DatabaseException e) { Text text = new Text(this, SWT.MULTI); text.setText(e.getMessage()); @@ -440,7 +506,6 @@ public abstract class ModelUpdateEditor extends Composite{ for (Pair mod : updateList.getChanges()) { applyLiteralChange(graph, mod); } - selected.clear(); updateList.clear(); updateTree.getUpdateOps().applyAll(graph); @@ -493,11 +558,11 @@ public abstract class ModelUpdateEditor extends Composite{ public void perform(WriteGraph graph) throws DatabaseException { Layer0Utils.addCommentMetadata(graph, "Apply selected model updates"); graph.markUndoPoint(); - for (Pair mod : selected) { + HashSet> changes = new HashSet<>(updateList.getSelected()); + for (Pair mod : changes) { updateList.removeChange(mod); applyLiteralChange(graph, mod); } - selected.clear(); updateTree.getUpdateOps().applySelected(graph); @@ -514,6 +579,47 @@ public abstract class ModelUpdateEditor extends Composite{ } }); } + + protected void createDefaultSelections() { + // select all changes + for (Entry op : updateTree.getUpdateOps().getResourceMap().entrySet()) { + op.getValue().select(true); + } + + + for (Pair pair : updateList.getChanges()) { + updateList.addSelected(pair); + } + + // preserve user-made changes (by removing selections) + for (Entry op : updateTree.getUpdateOps().getResourceMap().entrySet()) { + UpdateOp op2 = updateTree2.getUpdateOps().getUpdateOp(op.getKey()); + if (op2 == null) { + if (changes3.getComparable().containsRight(op.getKey())){ + op2 = updateTree2.getUpdateOps().getUpdateOp(changes3.getComparable().getLeft(op.getKey())); + } + } + if (op2 != null && op.getValue().getClass() == op2.getClass()) { + op.getValue().select(false); + } + } + + for (Pair pair : updateList.getChanges()) { + if (pair.first != null) { + boolean found = false; + for (Pair pair2 : updateList2.getChanges()) { + if (pair.first.equals(pair2.first)) { + found = true; + break; + } + } + if (found) { + updateList.removeSelected(pair); + } + } + } + + } /** @@ -550,9 +656,7 @@ public abstract class ModelUpdateEditor extends Composite{ public interface ChangeFilter { public boolean accept(ReadGraph g, Pair change) throws DatabaseException; } - - - + /** * @@ -675,9 +779,8 @@ public abstract class ModelUpdateEditor extends Composite{ private class SelectionLabelProvider extends ColumnLabelProvider { - Collection selected; - public SelectionLabelProvider(Collection selected) { - this.selected = selected; + public SelectionLabelProvider() { + } @Override public String getText(Object element) { @@ -686,7 +789,9 @@ public abstract class ModelUpdateEditor extends Composite{ @Override public Image getImage(Object element) { - if (selected.contains(element)) + if (updateList == null) + return null; + if (updateList.isSelected((Pair) element)) return checked; else return unchecked; @@ -753,14 +858,10 @@ public abstract class ModelUpdateEditor extends Composite{ private class SelectionEditingSupport extends EditingSupport { - @SuppressWarnings("rawtypes") - Collection selected; - @SuppressWarnings("rawtypes") - public SelectionEditingSupport(ColumnViewer viewer, Collection selected) { + public SelectionEditingSupport(ColumnViewer viewer) { super(viewer); - this.selected = selected; } @@ -776,16 +877,20 @@ public abstract class ModelUpdateEditor extends Composite{ @Override protected Object getValue(Object element) { - return selected.contains(element); + if (updateList == null) + return false; + return updateList.isSelected((Pair) element); } @SuppressWarnings("unchecked") @Override protected void setValue(Object element, Object value) { + if (updateList == null) + return; if (Boolean.TRUE.equals(value)) - selected.add(element); + updateList.addSelected((Pair) element); else - selected.remove(element); + updateList.removeSelected((Pair) element); getViewer().refresh(element); }