]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateList.java
Handle asserted property statements
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / UpdateList.java
1 package org.simantics.interop.update.model;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashSet;
6 import java.util.List;
7
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.Statement;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.interop.test.GraphChanges;
13 import org.simantics.interop.test.GraphChanges.Modification;
14 import org.simantics.utils.datastructures.Pair;
15
16 public class UpdateList {
17         private HashSet<PropertyChange> changes;
18         
19         public UpdateList() {
20                 changes = new HashSet<>();
21         }
22         
23         public UpdateList(Collection<PropertyChange> changes) {
24                 this.changes = new HashSet<>(changes);
25         }
26         
27         public UpdateList(GraphChanges graphChanges, Collection<Modification> coll) {
28                 this.changes = new HashSet<>();
29                 for (Modification p : coll) {
30                         changes.add(create(graphChanges, p.getLeftSub(),p.getRightSub(),new Pair<Statement, Statement>(p.getLeftStm(), p.getRightStm())));
31                 }
32         }
33         
34         protected PropertyChange create(GraphChanges changes, Resource left, Resource right, Pair<Statement, Statement> change) {
35                 return new PropertyChange(changes, left,right, change);
36         }
37         
38         public Collection<PropertyChange> getChanges() {
39                 return changes;
40         }
41
42         public void addChange(PropertyChange change) {
43                 changes.add(change);
44         }
45         
46         public void removeChange(PropertyChange change) {
47                 changes.remove(change);
48         }
49         
50         public Collection<PropertyChange> getSelected() {
51                 HashSet<PropertyChange> selected = new HashSet<>();
52                 for (PropertyChange c : selected) {
53                         if (c.selected())
54                                 selected.add(c);
55                 }
56                 return selected;
57         }
58         
59         public void clearSelected() {
60                 for (PropertyChange c : changes)
61                         c.select(false);
62         }
63         
64         public PropertyChange getChange(ReadGraph g, Statement s) throws DatabaseException {
65                 for (PropertyChange c : changes) {
66                         if (s.equals(c.pair.first))
67                                 return c;
68                         if (s.equals(c.pair.second))
69                                 return c;
70                 }
71                 return null;
72         }
73         
74         public Collection<PropertyChange> getChanges(ReadGraph g, Resource r) throws DatabaseException{
75                 List<PropertyChange> list = new ArrayList<>();
76                 for (PropertyChange pair : changes) {
77                         if (pair.getFirst() != null) {
78                                 if (pair.getFirst().getSubject().equals(r)) {
79                                         list.add(pair);
80                                         continue;
81                                 }
82                                 if (pair.getFirst().getObject().equals(r)) {
83                                         list.add(pair);
84                                         continue;
85                                 }
86                         }
87                         if (pair.getSecond() != null) {
88                                 if (pair.getSecond().getSubject().equals(r)) {
89                                         list.add(pair);
90                                         continue;
91                                 }
92                                 if (pair.getSecond().getObject().equals(r)) {
93                                         list.add(pair);
94                                         continue;
95                                 }
96                         }
97                 }
98                 return list;
99         }
100 }