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