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