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=919fe4b68238d05311533d363553bdad6de0bc17;hb=refs%2Fheads%2Frelease%2F1.32.1;hp=a3b60332ce9e1552bbe68f9e14bdb182f65a86d1;hpb=2a37041c6da8864656e7217dbcc3d6dae8cd65be;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 a3b6033..919fe4b 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 @@ -40,30 +40,26 @@ 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.eclipse.ui.IEditorInput; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.Session; 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.exception.DoesNotContainValueException; -import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; -import org.simantics.db.exception.NoSingleResultException; -import org.simantics.db.exception.ServiceException; import org.simantics.db.request.Read; import org.simantics.interop.test.GraphChanges; -import org.simantics.interop.test.GraphComparator; -import org.simantics.interop.test.NameComparator; 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.layer0.Layer0; -import org.simantics.ui.workbench.ResourceEditorPart2; -import org.simantics.utils.datastructures.BijectionMap; -import org.simantics.utils.datastructures.Callback; +import org.simantics.ui.SimanticsUI; import org.simantics.utils.datastructures.Pair; import org.simantics.utils.ui.ExceptionUtils; @@ -74,18 +70,13 @@ import org.simantics.utils.ui.ExceptionUtils; * @author Marko Luukkainen * */ -public abstract class ModelUpdateEditor extends ResourceEditorPart2 { +public abstract class ModelUpdateEditor extends Composite implements WarningListener{ - - private Composite composite; private Composite errorComposite; private CheckboxTreeViewer changeBrowser; private TableViewer changeViewer; - private GraphChanges changes; - private UpdateTree updateTree; - private Button updateAllButton; private Button updateSelectedButton; @@ -98,30 +89,26 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 { private Color containsColor; private Color deletedColor; private Color addedColor; + + private ModelUpdate update; - private HashSet> selected = new HashSet>(); private HashSet selectedStructure = new HashSet(); - private List filters = new ArrayList(); - - public ModelUpdateEditor() { + 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")); warning = manager.createImage(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/error.png")); - } - - @Override - public void createPartControl(Composite parent) { + 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)); - composite = new Composite(parent, SWT.NONE); - composite.setLayout(new GridLayout(1,false)); + this.setLayout(new GridLayout(1,false)); - errorComposite = new Composite(composite, SWT.BORDER); + errorComposite = new Composite(this, SWT.BORDER); GridData data = new GridData(); data.grabExcessHorizontalSpace = true; data.grabExcessVerticalSpace = false; @@ -132,14 +119,14 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 { errorComposite.setVisible(false); - IEditorInput input = getEditorInput(); - if (!(input instanceof UpdateEditorInput)) { - Label label = new Label(composite, SWT.NONE); - label.setText("Unknown input."); - return; - } - - Composite fillComposite = new Composite(composite, SWT.NONE); +// IEditorInput input = getEditorInput(); +// if (!(input instanceof UpdateEditorInput)) { +// Label label = new Label(composite, SWT.NONE); +// label.setText("Unknown input."); +// return; +// } + + Composite fillComposite = new Composite(this, SWT.NONE); data = new GridData(); data.grabExcessHorizontalSpace = true; data.grabExcessVerticalSpace = true; @@ -160,7 +147,7 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 { TreeViewerColumn dataColumn = TableUtils.addColumn(changeBrowser, "Data", true, 600); - dataColumn.setLabelProvider(new UpdateNodeLabelProvicer()); + dataColumn.setLabelProvider(new UpdateNodeLabelProvider()); changeBrowser.addCheckStateListener(new ICheckStateListener() { @@ -171,7 +158,7 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 { node.getOp().select(Boolean.TRUE.equals(event.getChecked())); } - updateSelection(); + refreshChecked(); } }); @@ -183,7 +170,8 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 { @Override public void run() { - updateSelection(); + // TreeViewer uses lazy load, checked states must be updated when the tree is expanded. + refreshChecked(); } }); @@ -220,26 +208,25 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 { 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 (changes.getModifications().size() > 0) { - if (selected.contains(changes.getModifications().get(0))) { - for (Pair nr : changes.getModifications()) - selected.remove(nr); + if (update.getUpdateList().getChanges().size() > 0) { + if (update.getUpdateList().getSelected().size() > 0) { + update.getUpdateList().clearSelected(); } else { - for (Pair nr : changes.getModifications()) - selected.add(nr); + for (PropertyChange nr : update.getUpdateList().getChanges()) + nr.select(true); } changeViewer.refresh(); } } }); - selection.setEditingSupport(new SelectionEditingSupport(changeViewer, selected)); + selection.setEditingSupport(new SelectionEditingSupport(changeViewer)); } - Composite buttonComposite = new Composite(composite, SWT.NONE); + Composite buttonComposite = new Composite(this, SWT.NONE); data = new GridData(); data.grabExcessHorizontalSpace = true; @@ -264,7 +251,7 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 { updateAllButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - updateAll(); + applyAll(); } }); updateSelectedButton = new Button(buttonComposite, SWT.PUSH); @@ -272,12 +259,13 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 { updateSelectedButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - updateSelected(); + applySelected(); } }); - - load(); - + } + + protected Session getSession() { + return SimanticsUI.getSession(); } protected String getColumntTitle(int i) { @@ -301,18 +289,28 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 { } 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 void addFilters(List filters) { - + public GraphChanges getChanges() { + return update.getChanges(); } - public GraphChanges getChanges() { - return changes; + public UpdateTree getUpdateTree() { + return update.getUpdateTree(); + } + + public UpdateList getUpdateList() { + return update.getUpdateList(); } - private void showWarning(String text) { + public CheckboxTreeViewer getChangeBrowser() { + return changeBrowser; + } + + public TableViewer getChangeViewer() { + return changeViewer; + } + + public void showWarning(ModelUpdate update, String text) { errorComposite.setVisible(true); Label label = new Label(errorComposite, SWT.NONE); @@ -320,31 +318,48 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 { label = new Label(errorComposite, SWT.NONE); label.setText(text); //this.setStatusMessage("Update contains structural changes (new or deleted symbols), please create a new model."); - composite.layout(true); + this.layout(true); + } + + private List checkStateListeners = new ArrayList<>(); + + + public void addCheckStateListener(ICheckStateListener listener) { + checkStateListeners.add(listener); + } + + public void removeCheckStateListener(ICheckStateListener listener) { + checkStateListeners.remove(listener); } - private void updateSelection() { + 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) { - boolean applied = n.getOp().applied(); - if (applied) { - changeBrowser.setChecked(n, true); + UpdateOp op = n.getOp(); + if (!op.isChange()) { changeBrowser.setGrayed(n, true); - selectedStructure.remove(n); + changeBrowser.setChecked(n, true); } else { - boolean sel = n.getOp().selected(); - if (sel) { - selectedStructure.add(n); - - } else { + boolean applied = op.applied(); + if (applied) { + changeBrowser.setChecked(n, true); + changeBrowser.setGrayed(n, true); selectedStructure.remove(n); + } else { + boolean sel = op.selected(); + if (sel) { + selectedStructure.add(n); + + } else { + selectedStructure.remove(n); + } + changeBrowser.setChecked(n, sel); + changeBrowser.setGrayed(n, false); } - changeBrowser.setChecked(n, sel); - changeBrowser.setGrayed(n, false); } } else { changeBrowser.setGrayed(n, true); @@ -356,58 +371,52 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 { } changeBrowser.refresh(); + for (ICheckStateListener l : checkStateListeners) { + l.checkStateChanged(new CheckStateChangedEvent(changeBrowser, null, false)); + } + changeViewer.refresh(); } + protected abstract ModelUpdate createUpdate(); - private void load() { - - addFilters(filters); - - UpdateEditorInput uei = (UpdateEditorInput)getEditorInput(); - Resource r1 = uei.getR1(); - Resource r2 = uei.getR2(); - + + public void load(UpdateEditorInput uei) { + 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 { - - Pair result = getChanges(r1,r2); - 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."); - comparator.test(getSession()); - changes = comparator.getChanges(); - changes = getSession().syncRequest(new FilterChangesRead(changes)); - updateTree = getUpdateTree(changes); + if (update != null) + update.removeListener(this); + update = createUpdate(); + update.addListener(this); + update.setInput(oldModel, newModel, originalModel, newDistinct); } catch (DatabaseException e) { - Text text = new Text(composite, SWT.MULTI); + Text text = new Text(this, SWT.MULTI); text.setText(e.getMessage()); e.printStackTrace(); return; } - - - changeViewer.setInput(changes.getModifications()); - changeBrowser.setInput(updateTree); - updateSelection(); + setInputs(); + + refreshChecked(); } + protected void setInputs() { + changeViewer.setInput(update.getUpdateList().getChanges()); + changeBrowser.setInput(update.getUpdateTree()); + } - - private void updateAll() { + private void applyAll() { updateAllButton.setEnabled(false); updateSelectedButton.setEnabled(false); getSession().asyncRequest(new WriteRequest(){ @Override public void perform(WriteGraph graph) throws DatabaseException { - for (Pair mod : changes.getModifications()) { - applyLiteralChange(graph, mod); - } - selected.clear(); - changes.getModifications().clear(); - - updateTree.getUpdateOps().applyAll(graph); + update.applyAll(graph); Display.getDefault().asyncExec(new Runnable() { @@ -416,49 +425,26 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 { updateAllButton.setEnabled(true); updateSelectedButton.setEnabled(true); - updateSelection(); + refreshChecked(); changeViewer.refresh(); } }); } - }, 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); }); } - private void applyLiteralChange(WriteGraph graph, Pair mod) throws DoesNotContainValueException, ServiceException, ManyObjectsForFunctionalRelationException { - - Resource s = changes.getComparable().getLeft(mod.second.getSubject()); - Resource pred = mod.first.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 updateSelected() { + private void applySelected() { updateAllButton.setEnabled(false); updateSelectedButton.setEnabled(false); getSession().asyncRequest(new WriteRequest(){ @Override public void perform(WriteGraph graph) throws DatabaseException { - for (Pair mod : selected) { - changes.getModifications().remove(mod); - applyLiteralChange(graph, mod); - } - selected.clear(); - - updateTree.getUpdateOps().applySelected(graph); + update.applySelected(graph); Display.getDefault().asyncExec(new Runnable() { @@ -467,113 +453,12 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 { changeViewer.refresh(); updateAllButton.setEnabled(true); updateSelectedButton.setEnabled(true); - updateSelection(); + refreshChecked(); } }); } }); - } - - @Override - public void setFocus() { - composite.setFocus(); - - } - - /** - * 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 { @@ -639,9 +524,8 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 { private class SelectionLabelProvider extends ColumnLabelProvider { - Collection selected; - public SelectionLabelProvider(Collection selected) { - this.selected = selected; + public SelectionLabelProvider() { + } @Override public String getText(Object element) { @@ -650,28 +534,22 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 { @Override public Image getImage(Object element) { - if (selected.contains(element)) + if (update == null || !update.isInit()) + return null; + PropertyChange pc = (PropertyChange)element; + if (pc.selected()) return checked; else return unchecked; } } - private class UpdateNodeLabelProvicer extends ColumnLabelProvider { + private class UpdateNodeLabelProvider extends ColumnLabelProvider { @Override public String getText(Object element) { final UpdateNode node = (UpdateNode)element; - try { - return getSession().syncRequest(new Read() { - @Override - public String perform(ReadGraph graph) throws DatabaseException { - return node.getLabel(graph); - } - }); - } catch (Exception e) { - return e.getMessage(); - } + return node.getLabel(); } @Override @@ -713,12 +591,12 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 { @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; } @@ -726,15 +604,9 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 { 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; - } @Override @@ -749,16 +621,21 @@ public abstract class ModelUpdateEditor extends ResourceEditorPart2 { @Override protected Object getValue(Object element) { - return selected.contains(element); + if (update == null || !update.isInit()) + return false; + PropertyChange pc = (PropertyChange)element; + return pc.selected(); } - @SuppressWarnings("unchecked") @Override protected void setValue(Object element, Object value) { + if (update == null || !update.isInit()) + return; + PropertyChange pc = (PropertyChange)element; if (Boolean.TRUE.equals(value)) - selected.add(element); + pc.select(true); else - selected.remove(element); + pc.select(false); getViewer().refresh(element); }