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=ea14039eecb0bbe3fc1c65f5c45cdf8a6c225d60;hb=HEAD;hp=4c172997bd1068f490dd267b86f125760e3af021;hpb=98caf73c954a9d8d5b80b13f3ca4e02fee92d87d;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 4c17299..ea14039 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,7 +4,6 @@ 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; @@ -41,6 +40,7 @@ import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Text; +import org.simantics.Simantics; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; @@ -48,19 +48,18 @@ import org.simantics.db.Statement; import org.simantics.db.WriteGraph; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.util.Layer0Utils; import org.simantics.db.request.Read; import org.simantics.interop.test.GraphChanges; -import org.simantics.interop.test.GraphComparator; import org.simantics.interop.update.Activator; +import org.simantics.interop.update.model.ModelUpdate; +import org.simantics.interop.update.model.ModelUpdate.WarningListener; +import org.simantics.interop.update.model.PropertyChange; import org.simantics.interop.update.model.UpdateList; import org.simantics.interop.update.model.UpdateNode; -import org.simantics.interop.update.model.UpdateNode.Status; import org.simantics.interop.update.model.UpdateOp; +import org.simantics.interop.update.model.UpdateStatus; import org.simantics.interop.update.model.UpdateTree; import org.simantics.interop.utils.TableUtils; -import org.simantics.ui.SimanticsUI; -import org.simantics.utils.datastructures.Callback; import org.simantics.utils.datastructures.Pair; import org.simantics.utils.ui.ExceptionUtils; @@ -71,22 +70,13 @@ import org.simantics.utils.ui.ExceptionUtils; * @author Marko Luukkainen * */ -public abstract class ModelUpdateEditor extends Composite{ +public abstract class ModelUpdateEditor extends Composite implements WarningListener{ private Composite errorComposite; private CheckboxTreeViewer changeBrowser; private TableViewer changeViewer; - private GraphChanges changes; - private UpdateTree updateTree; - private UpdateList updateList; - - private GraphChanges changes2; - private GraphChanges changes3; - private UpdateTree updateTree2; - private UpdateList updateList2; - private Button updateAllButton; private Button updateSelectedButton; @@ -99,22 +89,25 @@ public abstract class ModelUpdateEditor extends Composite{ private Color containsColor; private Color deletedColor; private Color addedColor; + private Color disabledColor; + private ModelUpdate update; - private HashSet selectedStructure = new HashSet(); - private List filters = new ArrayList(); + private HashSet selectedStructure = new HashSet(); public ModelUpdateEditor(Composite parent) { super(parent,SWT.NONE); checked = manager.createImage(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/tick.png")); unchecked = manager.createImage(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/cross.png")); + unchecked = manager.createImage(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/cross.png")); warning = manager.createImage(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/error.png")); containsColor = new Color(parent.getDisplay(), new RGB(255,255,220)); deletedColor = new Color(parent.getDisplay(), new RGB(255,220,220)); addedColor = new Color(parent.getDisplay(), new RGB(220,255,220)); + disabledColor = new Color(parent.getDisplay(), new RGB(128,128,128)); this.setLayout(new GridLayout(1,false)); @@ -205,29 +198,26 @@ public abstract class ModelUpdateEditor extends Composite{ changeViewer.setUseHashlookup(true); - TableViewerColumn selection = TableUtils.addColumn(changeViewer, getColumntTitle(0), false, false, 20); - TableViewerColumn diagram = TableUtils.addColumn(changeViewer, getColumntTitle(1), true, true, 100); - TableViewerColumn symbol = TableUtils.addColumn(changeViewer, getColumntTitle(2), true, true, 100); - TableViewerColumn property = TableUtils.addColumn(changeViewer, getColumntTitle(3), true, true, 100); - TableViewerColumn oldValue = TableUtils.addColumn(changeViewer, getColumntTitle(4), true, true, 100); - TableViewerColumn newValue = TableUtils.addColumn(changeViewer, getColumntTitle(5), true, true, 100); - - diagram.setLabelProvider(getLabelProvider(1)); - symbol.setLabelProvider(getLabelProvider(2)); - property.setLabelProvider(getLabelProvider(3)); - oldValue.setLabelProvider(getLabelProvider(4)); - newValue.setLabelProvider(getLabelProvider(5)); + TableViewerColumn cols[] = new TableViewerColumn[getChangeListColumnCount()]; + TableViewerColumn selection = TableUtils.addColumn(changeViewer, getColumntTitle(0), false, false, getChangeListColumnWidth(0)); + cols[0] = selection; + for (int i = 1 ; i < getChangeListColumnCount(); i++) { + TableViewerColumn column = TableUtils.addColumn(changeViewer, getColumntTitle(i), true, getChangeListColumnSortable(i), getChangeListColumnWidth(i)); + cols[i] = column; + column.setLabelProvider(getLabelProvider(i)); + configureChangeListColumn(i, column); + } selection.setLabelProvider(new SelectionLabelProvider()); selection.getColumn().addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - if (updateList.getChanges().size() > 0) { - if (updateList.getSelected().size() > 0) { - updateList.clearSelected(); + if (update.getUpdateList().getChanges().size() > 0) { + if (update.getUpdateList().getSelected().size() > 0) { + update.getUpdateList().clearSelected(); } else { - for (Pair nr : updateList.getChanges()) - updateList.addSelected(nr); + for (PropertyChange nr : update.getUpdateList().getChanges()) + nr.select(true); } changeViewer.refresh(); } @@ -274,8 +264,28 @@ public abstract class ModelUpdateEditor extends Composite{ }); } + protected int getChangeListColumnCount() { + return 6; + } + + protected int getChangeListColumnWidth(int col) { + if (col == 0) + return 20; + return 100; + } + + protected boolean getChangeListColumnSortable(int col) { + if (col == 0) + return false; + return true; + } + + protected void configureChangeListColumn(int col, TableViewerColumn column) { + + } + protected Session getSession() { - return SimanticsUI.getSession(); + return Simantics.getSession(); } protected String getColumntTitle(int i) { @@ -299,26 +309,17 @@ public abstract class ModelUpdateEditor extends Composite{ } protected abstract ColumnLabelProvider getLabelProvider(int i); - 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()); - } - - protected void addFilters(List filters) { - - } public GraphChanges getChanges() { - return changes; + return update.getChanges(); } public UpdateTree getUpdateTree() { - return updateTree; + return update.getUpdateTree(); } public UpdateList getUpdateList() { - return updateList; + return update.getUpdateList(); } public CheckboxTreeViewer getChangeBrowser() { @@ -329,7 +330,7 @@ public abstract class ModelUpdateEditor extends Composite{ return changeViewer; } - private void showWarning(String text) { + public void showWarning(ModelUpdate update, String text) { errorComposite.setVisible(true); Label label = new Label(errorComposite, SWT.NONE); @@ -354,12 +355,12 @@ public abstract class ModelUpdateEditor extends Composite{ public void refreshChecked() { Stack nodeStack = new Stack(); - nodeStack.push((UpdateNode)updateTree.getRootNode()); + nodeStack.push((UpdateNode)update.getUpdateTree().getRootNode()); while (!nodeStack.isEmpty()) { UpdateNode n = nodeStack.pop(); if (n.getOp() != null) { UpdateOp op = n.getOp(); - if (!op.isAdd() && !op.isDelete()) { + if (!op.isChange()) { changeBrowser.setGrayed(n, true); changeBrowser.setChecked(n, true); } else { @@ -377,7 +378,7 @@ public abstract class ModelUpdateEditor extends Composite{ selectedStructure.remove(n); } changeBrowser.setChecked(n, sel); - changeBrowser.setGrayed(n, false); + changeBrowser.setGrayed(n, !op.enabled()); } } } else { @@ -396,88 +397,22 @@ public abstract class ModelUpdateEditor extends Composite{ changeViewer.refresh(); } + protected abstract ModelUpdate createUpdate(); + public void load(UpdateEditorInput uei) { - - addFilters(filters); 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 - + boolean newDistinct = uei.isNewDistinct(); try { - - 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 != 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(); - } - + if (update != null) + update.removeListener(this); + update = createUpdate(); + update.addListener(this); + update.setInput(oldModel, newModel, originalModel, newDistinct); } catch (DatabaseException e) { Text text = new Text(this, SWT.MULTI); text.setText(e.getMessage()); @@ -491,8 +426,10 @@ public abstract class ModelUpdateEditor extends Composite{ } protected void setInputs() { - changeViewer.setInput(updateList.getChanges()); - changeBrowser.setInput(updateTree); + changeViewer.setInput(update.getUpdateList().getChanges()); + changeBrowser.setInput(update.getUpdateTree()); + updateAllButton.setEnabled(true); + updateSelectedButton.setEnabled(true); } private void applyAll() { @@ -501,14 +438,7 @@ public abstract class ModelUpdateEditor extends Composite{ getSession().asyncRequest(new WriteRequest(){ @Override public void perform(WriteGraph graph) throws DatabaseException { - Layer0Utils.addCommentMetadata(graph, "Apply all model updates"); - graph.markUndoPoint(); - for (Pair mod : updateList.getChanges()) { - applyLiteralChange(graph, mod); - } - updateList.clear(); - - updateTree.getUpdateOps().applyAll(graph); + update.applyAll(graph); Display.getDefault().asyncExec(new Runnable() { @@ -524,47 +454,19 @@ public abstract class ModelUpdateEditor extends Composite{ } - }, new Callback() { - @Override - public void run(DatabaseException parameter) { - if (parameter != null) - ExceptionUtils.logAndShowError("Cannot update model", parameter); - } + }, e -> { + if (e != null) + ExceptionUtils.logAndShowError("Cannot update model", e); }); } - protected void applyLiteralChange(WriteGraph graph, Pair mod) throws DatabaseException { - if (mod.second == null) { - graph.deny(mod.first); - return; - } - Resource s = changes.getComparable().getLeft(mod.second.getSubject()); - Resource pred = mod.second.getPredicate(); - if (graph.hasValue(mod.second.getObject())) { - Object value = graph.getValue(mod.second.getObject()); - graph.claimLiteral(s, pred, value); - } else { - graph.deny(s,pred); - } - - - } - private void applySelected() { updateAllButton.setEnabled(false); updateSelectedButton.setEnabled(false); getSession().asyncRequest(new WriteRequest(){ @Override public void perform(WriteGraph graph) throws DatabaseException { - Layer0Utils.addCommentMetadata(graph, "Apply selected model updates"); - graph.markUndoPoint(); - HashSet> changes = new HashSet<>(updateList.getSelected()); - for (Pair mod : changes) { - updateList.removeChange(mod); - applyLiteralChange(graph, mod); - } - - updateTree.getUpdateOps().applySelected(graph); + update.applySelected(graph); Display.getDefault().asyncExec(new Runnable() { @@ -578,142 +480,7 @@ 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); - } - } - } - - } - - - /** - * Filters changes: - * 1. Changes that are not essential for model update (changes that can be found when the models are axcatly the same) - * 2. Runs custom filters for value changes. - * - * @param g - * @param changes - * @return - * @throws DatabaseException - */ - protected GraphChanges filterChanges(ReadGraph g, GraphChanges changes) throws DatabaseException - { - - List> modifications = new ArrayList>(); - - for (Pair mod : changes.getModifications()) { - - boolean accept = true; - for (ChangeFilter filter : filters) { - if (!filter.accept(g, mod)) { - accept = false; - break; - } - } - if (accept) - modifications.add(mod); - } - GraphChanges newChanges = new GraphChanges(changes.getResource1(),changes.getResource2(),changes.getDeletions(), changes.getAdditions(), modifications, changes.getComparable()); - return newChanges; - } - - public interface ChangeFilter { - public boolean accept(ReadGraph g, Pair change) throws DatabaseException; - } - - - /** - * - * Filters floating point value changes (default filter is set filter when the change is less than 1%) - * - */ - protected class FPValueFilter implements ChangeFilter { - - private double percentage = 0.01; - - public FPValueFilter() { - - } - - public FPValueFilter(double percentage) { - if (percentage < 0.0 || percentage > 1.0) - throw new IllegalArgumentException("Percentage must be between 0.0 and 1.0."); - this.percentage = percentage; - } - - @Override - public boolean accept(ReadGraph g, Pair change) throws DatabaseException { - //filter floating point values that have less than 1% difference. - if (!g.hasValue(change.first.getObject()) || !g.hasValue(change.second.getObject())) - return true; - Object v1 = g.getValue(change.first.getObject()); - Object v2 = g.getValue(change.second.getObject()); - - if (v1 instanceof Double && v2 instanceof Double) { - double d1 = (Double)v1; - double d2 = (Double)v2; - if (Math.abs(d1-d2) / Math.max(Math.abs(d1), Math.abs(d2)) < percentage) - return false; - } else if (v1 instanceof Float && v2 instanceof Float) { - float d1 = (Float)v1; - float d2 = (Float)v2; - if (Math.abs(d1-d2) / Math.max(Math.abs(d1), Math.abs(d2)) < percentage) - return false; - } - - return true; - } - } - - - private class FilterChangesRead implements Read { - private GraphChanges changes; - public FilterChangesRead(GraphChanges changes) { - this.changes = changes; - } - - @Override - public GraphChanges perform(ReadGraph graph) throws DatabaseException { - return filterChanges(graph, changes); - } - } - + } private class ModificationListContentProvider implements IStructuredContentProvider { @@ -789,13 +556,39 @@ public abstract class ModelUpdateEditor extends Composite{ @Override public Image getImage(Object element) { - if (updateList == null) + if (update == null || !update.isInit()) return null; - if (updateList.isSelected((Pair) element)) + PropertyChange pc = (PropertyChange)element; + if (pc.applied()) + return null; + if (pc.selected()) return checked; else return unchecked; } + + @Override + public Color getForeground(Object element) { + PropertyChange pc = (PropertyChange)element; + if (!pc.enabled()) + return disabledColor; + return null; + } + } + + protected abstract class PropertyChangeLabelProvider extends ColumnLabelProvider { + + public PropertyChangeLabelProvider() { + + } + + @Override + public Color getForeground(Object element) { + PropertyChange pc = (PropertyChange)element; + if (!pc.enabled()) + return disabledColor; + return null; + } } private class UpdateNodeLabelProvider extends ColumnLabelProvider { @@ -845,24 +638,30 @@ public abstract class ModelUpdateEditor extends Composite{ @Override public Color getBackground(Object element) { final UpdateNode node = (UpdateNode)element; - Status status = node.getStatus(); - if (status == Status.CONTAINS) + UpdateStatus status = node.getStatus(); + if (status == UpdateStatus.CONTAINS) return containsColor; - if (status == Status.DELETED) + if (status == UpdateStatus.DELETED) return deletedColor; - if (status == Status.NEW) + if (status == UpdateStatus.NEW) return addedColor; return null; } + + @Override + public Color getForeground(Object element) { + final UpdateNode node = (UpdateNode)element; + if (node.getOp() != null && !node.getOp().enabled()) + return disabledColor; + return null; + } } private class SelectionEditingSupport extends EditingSupport { - @SuppressWarnings("rawtypes") public SelectionEditingSupport(ColumnViewer viewer) { super(viewer); - } @Override @@ -877,20 +676,21 @@ public abstract class ModelUpdateEditor extends Composite{ @Override protected Object getValue(Object element) { - if (updateList == null) + if (update == null || !update.isInit()) return false; - return updateList.isSelected((Pair) element); + PropertyChange pc = (PropertyChange)element; + return pc.selected(); } - @SuppressWarnings("unchecked") @Override protected void setValue(Object element, Object value) { - if (updateList == null) + if (update == null || !update.isInit()) return; + PropertyChange pc = (PropertyChange)element; if (Boolean.TRUE.equals(value)) - updateList.addSelected((Pair) element); + pc.select(true); else - updateList.removeSelected((Pair) element); + pc.select(false); getViewer().refresh(element); }