]> gerrit.simantics Code Review - simantics/interop.git/blobdiff - org.simantics.interop.update/src/org/simantics/interop/update/editor/ModelUpdateEditor.java
PropertyChange class (instead of Pair)
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / editor / ModelUpdateEditor.java
index 18dad814361946b46442952f8172a93e79d88152..1779b8f6bbdaf188b1556a2c61d69669a3035374 100644 (file)
@@ -47,15 +47,16 @@ 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.PropertyChange;
+import org.simantics.interop.update.model.ModelUpdate.WarningListener;
 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;
@@ -70,17 +71,13 @@ import org.simantics.utils.ui.ExceptionUtils;
  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
  *
  */
-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 Button updateAllButton;
        private Button updateSelectedButton;
        
@@ -93,13 +90,12 @@ public abstract class ModelUpdateEditor extends Composite{
        private Color containsColor;
        private Color deletedColor;
        private Color addedColor;
+
+       private ModelUpdate update;
        
-       private HashSet<Pair<Statement, Statement>> selected = new HashSet<Pair<Statement,Statement>>();
        
        private HashSet<UpdateNode> selectedStructure = new HashSet<UpdateNode>();
        
-       private List<ChangeFilter> filters = new ArrayList<ChangeFilter>();
-       
        public ModelUpdateEditor(Composite parent) {
                super(parent,SWT.NONE);
                checked = manager.createImage(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/tick.png"));
@@ -213,23 +209,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<Statement, Statement> nr : updateList.getChanges())
-                                                               selected.remove(nr);
+                                       if (update.getUpdateList().getChanges().size() > 0) {
+                                               if (update.getUpdateList().getSelected().size() > 0) {
+                                                       update.getUpdateList().clearSelected();
                                                } else {
-                                                       for (Pair<Statement, Statement> nr : updateList.getChanges())
-                                                               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(this, SWT.NONE);
@@ -295,26 +290,17 @@ public abstract class ModelUpdateEditor extends Composite{
        }
        
        protected abstract ColumnLabelProvider getLabelProvider(int i);
-       protected abstract Pair<GraphComparator,Boolean> 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<ChangeFilter> 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() {
@@ -325,7 +311,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);
@@ -350,12 +336,12 @@ public abstract class ModelUpdateEditor extends Composite{
        
        public void refreshChecked() {
                Stack<UpdateNode> nodeStack = new Stack<UpdateNode>();
-               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 {
@@ -389,29 +375,25 @@ public abstract class ModelUpdateEditor extends Composite{
                for (ICheckStateListener l : checkStateListeners) {
                        l.checkStateChanged(new CheckStateChangedEvent(changeBrowser, null, false));
                }
+               changeViewer.refresh();
        }
        
+       protected abstract ModelUpdate createUpdate();
+       
        
        
        public void load(UpdateEditorInput uei) {
-               
-               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 
+               boolean newDistinct = uei.isNewDistinct();
                try {
-                       
-                       Pair<GraphComparator,Boolean> 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);
-                       updateList = getUpdateList(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(this, SWT.MULTI);
                        text.setText(e.getMessage());
@@ -425,8 +407,8 @@ 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());
        }
        
        private void applyAll() {
@@ -435,15 +417,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<Statement, Statement> mod : updateList.getChanges()) {
-                                       applyLiteralChange(graph, mod);
-                               }
-                               selected.clear();
-                               updateList.clear();
-                               
-                               updateTree.getUpdateOps().applyAll(graph);
+                               update.applyAll(graph);
                                
                                Display.getDefault().asyncExec(new Runnable() {
                                        
@@ -468,38 +442,13 @@ public abstract class ModelUpdateEditor extends Composite{
                });
        }
        
-       protected void applyLiteralChange(WriteGraph graph, Pair<Statement, Statement> 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();
-                               for (Pair<Statement, Statement> mod : selected) {
-                                       updateList.removeChange(mod);
-                                       applyLiteralChange(graph, mod);
-                               }
-                               selected.clear();
-                               
-                               updateTree.getUpdateOps().applySelected(graph);
+                               update.applySelected(graph);
                                
                                Display.getDefault().asyncExec(new Runnable() {
                                        
@@ -513,103 +462,7 @@ public abstract class ModelUpdateEditor extends Composite{
                                });
                        }
                });
-       }
-
-       
-       /**
-        * 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<Pair<Statement,Statement>> modifications = new ArrayList<Pair<Statement,Statement>>();
-               
-       for (Pair<Statement, Statement> 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<Statement, Statement> 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<Statement, Statement> 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<GraphChanges> {
-               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 {
                
@@ -675,9 +528,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 +538,10 @@ public abstract class ModelUpdateEditor extends Composite{
                
                @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;
@@ -740,12 +595,12 @@ 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;            
                }
@@ -753,14 +608,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 +627,22 @@ public abstract class ModelUpdateEditor extends Composite{
                
                @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);
                }