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