]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode.java
UpdateList object for property changes
[simantics/interop.git] / org.simantics.interop.update / src / org / simantics / interop / update / model / UpdateNode.java
1 package org.simantics.interop.update.model;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5
6 import org.eclipse.jface.resource.ImageDescriptor;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.utils.NameUtils;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.layer0.Layer0;
12
13 public class UpdateNode {
14
15         public enum Status {EXIST,DELETED,NEW,CONTAINS};
16         
17
18         private Status status;
19         private UpdateOp op;
20         private Resource r;
21         private String label;
22         
23         
24         private Collection<UpdateNode> children = new ArrayList<UpdateNode>();
25         /**
26          * 
27          * @param resource old Resource if status is DELETED or EXISTS.
28          * @param status
29          * @param changes
30          */
31         public UpdateNode(ReadGraph g, Status status, UpdateOp op) throws DatabaseException{
32
33                 this.status = status;
34                 this.op = op;
35                 this.r = op.getResource();
36                 this.label = getLabel(g, r);
37         }
38         
39         public UpdateNode(ReadGraph g, Status status, Resource r) throws DatabaseException {
40
41                 this.status = status;
42                 this.op = null;
43                 this.r = r;
44                 this.label = getLabel(g, r);
45         }
46         
47         public Resource getResource() {
48                 return r;
49         }
50         
51         public Resource getParentResource(ReadGraph g) throws DatabaseException {
52                 Layer0 l0 = Layer0.getInstance(g);
53                 return g.getPossibleObject(r, l0.PartOf);
54         }
55         
56         public void setStatus(Status status) {
57                 this.status = status;
58         }
59         
60         public Status getStatus() {
61                 return status;
62         }
63         
64         public Collection<UpdateNode> getChildren() {
65                 return children;
66         }
67         
68         public void addChild(UpdateNode node) {
69                 children.add(node);
70                 if (op != null && node.op != null) {
71                         if (!op.getSubOps().contains(node.op)) {
72                                 op.addSubOp(node.op);
73                                 node.op.addParentOp(op);
74                         }
75                 }
76         }
77
78         public ImageDescriptor getImage(ReadGraph graph) throws DatabaseException {
79                 return null;
80         }
81
82         public String getLabel() {
83                 return label;
84         }
85         
86         @Override
87         public String toString() {
88                 return label;
89         }
90         
91         protected String getLabel(ReadGraph graph, Resource r) throws DatabaseException {
92                 String label = NameUtils.getSafeLabel(graph, r);
93                 if (label.length() == 0)
94                         label = NameUtils.getSafeName(graph, r);
95                 
96                 return label;
97         }
98
99
100         public UpdateOp getOp() {
101                 return op;
102         }
103
104 }