package org.simantics.interop.update.model;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
+import java.util.List;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
import org.simantics.db.Statement;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.interop.test.GraphChanges;
+import org.simantics.interop.test.GraphChanges.Modification;
import org.simantics.utils.datastructures.Pair;
public class UpdateList {
- private HashSet<Pair<Statement, Statement>> changes;
+ private List<PropertyChange> changes;
public UpdateList() {
- changes = new HashSet<>();
+ changes = new ArrayList<>();
}
- public UpdateList(Collection<Pair<Statement, Statement>> changes) {
- this.changes = new HashSet<>(changes);
+ public UpdateList(Collection<PropertyChange> changes) {
+ this.changes = new ArrayList<>(changes);
}
- public Collection<Pair<Statement, Statement>> getChanges() {
- return changes;
+ public UpdateList(GraphChanges graphChanges, Collection<Modification> coll) {
+ this.changes = new ArrayList<>();
+ for (Modification p : coll) {
+ addChange(create(graphChanges, p.getLeftSub(),p.getRightSub(),new Pair<Statement, Statement>(p.getLeftStm(), p.getRightStm())));
+ }
+ }
+
+ protected PropertyChange create(GraphChanges changes, Resource left, Resource right, Pair<Statement, Statement> change) {
+ return new PropertyChange(changes, left,right, change);
}
- public void addChange(Pair<Statement, Statement> change) {
- changes.add(change);
+ public Collection<PropertyChange> getChanges() {
+ return changes;
+ }
+
+ public void addChange(PropertyChange change) {
+ if (!changes.contains(change))
+ changes.add(change);
}
- public void removeChange(Pair<Statement, Statement> change) {
+ public void removeChange(PropertyChange change) {
changes.remove(change);
}
- public void clear() {
- changes.clear();
+ public Collection<PropertyChange> getSelected() {
+ HashSet<PropertyChange> selected = new HashSet<>();
+ for (PropertyChange c : selected) {
+ if (c.selected())
+ selected.add(c);
+ }
+ return selected;
+ }
+
+ public void clearSelected() {
+ for (PropertyChange c : changes)
+ c.select(false);
+ }
+
+ public PropertyChange getChange(ReadGraph g, Statement s) throws DatabaseException {
+ for (PropertyChange c : changes) {
+ if (s.equals(c.pair.first))
+ return c;
+ if (s.equals(c.pair.second))
+ return c;
+ }
+ return null;
+ }
+
+ public Collection<PropertyChange> getChanges(ReadGraph g, Resource r) throws DatabaseException{
+ List<PropertyChange> list = new ArrayList<>();
+ for (PropertyChange pair : changes) {
+ if (pair.getFirst() != null) {
+ if (pair.getFirst().getSubject().equals(r)) {
+ list.add(pair);
+ continue;
+ }
+ if (pair.getFirst().getObject().equals(r)) {
+ list.add(pair);
+ continue;
+ }
+ }
+ if (pair.getSecond() != null) {
+ if (pair.getSecond().getSubject().equals(r)) {
+ list.add(pair);
+ continue;
+ }
+ if (pair.getSecond().getObject().equals(r)) {
+ list.add(pair);
+ continue;
+ }
+ }
+ }
+ return list;
}
}