]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateTree.java
Generinc UpdateEditor
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / UpdateTree.java
1 package  org.simantics.interop.update.model;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.interop.test.GraphChanges;
10 import org.simantics.interop.update.model.UpdateNode.Status;
11
12
13 public class UpdateTree {
14         
15         private UpdateNode rootNode;
16         private Map<Resource,UpdateNode> nodes;
17         private GraphChanges changes;
18         private UpdateOperations updateOps;
19         
20         
21         public UpdateTree(ReadGraph g, GraphChanges changes, UpdateOperations updateOps) throws DatabaseException {
22                 this.changes = changes;
23                 this.nodes = new HashMap<Resource, UpdateNode>();
24                 this.rootNode = createNode(Status.EXIST, changes.getResource1());
25                 nodes.put(changes.getResource1(), rootNode);
26                 nodes.put(changes.getResource2(), rootNode);
27                 this.updateOps = updateOps;
28                 this.updateOps.populate(g);
29                 populate(g);
30         }
31         
32         public UpdateOperations getUpdateOps() {
33                 return updateOps;
34         }
35         
36         public UpdateNode getRootNode() {
37                 return rootNode;
38         }
39         
40         protected UpdateNode createNode(Status status, Resource r) {
41                 return new UpdateNode(status, r);
42         }
43         
44         protected UpdateNode createNode(Status status, UpdateOp op) {
45                 return new UpdateNode(status, op);
46         }
47         
48         private UpdateNode createNode(Resource r1, Resource r2) {
49                 UpdateNode node = null;
50                 if (r1 != null && r2 != null) {
51                         node =  createNode(Status.EXIST, r1);
52                         nodes.put(r1, node);
53                         nodes.put(r2, node);
54                 } else if (r1 != null) {
55                         node = createNode(Status.DELETED ,updateOps.getUpdateOp(r1));
56                         nodes.put(r1, node);
57                 } else if (r2 != null) {
58                         node = createNode(Status.NEW, updateOps.getUpdateOp(r2));
59                         nodes.put(r2, node);
60                 }
61                 return node;
62         }
63         
64         public UpdateNode addNode(ReadGraph g, Resource r1, Resource r2) throws DatabaseException {
65                 if (r1 != null && r2 != null) {
66                         return null;
67                 }
68                 if (nodes.containsKey(r1))
69                         return nodes.get(r1);
70                 if (nodes.containsKey(r2))
71                         return nodes.get(r2);
72                 
73                 UpdateNode node = createNode(r1, r2);
74                 connectParent(g,node);
75                 return node;
76         }
77         
78         protected boolean connectParent(ReadGraph g, UpdateNode node) throws DatabaseException {
79                 UpdateNode parent = null;
80                 while (true) {
81                         Resource parentResource = node.getParentResource(g);
82                         parent = nodes.get(parentResource);
83                         if (parent == null) {
84                                 if (changes.getComparable().containsLeft(parentResource)) {
85                                         parent = createNode(parentResource, changes.getComparable().getRight(parentResource));
86                                 } else if (changes.getComparable().containsRight(parentResource)) {
87                                         parent = createNode(changes.getComparable().getLeft(parentResource) ,parentResource);
88                                 } else {
89                                         return false;
90                                 }
91                                 //parent.setStatus(Status.CONTAINS);
92                                 parent.addChild(node);
93                                 node = parent;
94                                 parent = null;
95                         } else {
96                                 parent.addChild(node);
97                                 return true;
98                         }
99                         
100                 }
101         }
102         
103         protected boolean handleCustom(ReadGraph g, UpdateOp op) throws DatabaseException {
104                 return false;
105         }
106         
107         private void populate(ReadGraph g) throws DatabaseException{
108
109                 for (UpdateOp op : updateOps.getOperations()) {
110                         if (!handleCustom(g, op)) {
111                                 if (op.isAdd()) {
112                                         addNode(g, null,op.getResource());
113                                 } else {
114                                         addNode(g, op.getResource(), null);
115                                 }
116                         }
117                 }
118                 
119         }
120
121 }