]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode.java
Moved label generation to separate init() method
[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                 init(g);
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                 init(g);
42         }
43         
44         protected void init(ReadGraph g) throws DatabaseException {
45                 this.label = getLabel(g, r);
46         }
47         
48         public Resource getResource() {
49                 return r;
50         }
51         
52         public Resource getParentResource(ReadGraph g) throws DatabaseException {
53                 Layer0 l0 = Layer0.getInstance(g);
54                 return g.getPossibleObject(r, l0.PartOf);
55         }
56         
57         public void setStatus(UpdateStatus status) {
58                 this.status = status;
59         }
60         
61         public UpdateStatus getStatus() {
62                 return status;
63         }
64         
65         public Collection<UpdateNode> getChildren() {
66                 return children;
67         }
68         
69         public void addChild(UpdateNode node) {
70                 children.add(node);
71                 if (op != null && node.op != null) {
72                         if (!op.getSubOps().contains(node.op)) {
73                                 op.addSubOp(node.op);
74                                 node.op.addParentOp(op);
75                         }
76                 }
77         }
78
79         public ImageDescriptor getImage(ReadGraph graph) throws DatabaseException {
80                 return null;
81         }
82
83         public String getLabel() {
84                 return label;
85         }
86         
87         @Override
88         public String toString() {
89                 return label;
90         }
91         
92         protected String getLabel(ReadGraph graph, Resource r) throws DatabaseException {
93                 String label = NameUtils.getSafeLabel(graph, r);
94                 if (label.length() == 0)
95                         label = NameUtils.getSafeName(graph, r);
96                 
97                 return label;
98         }
99
100
101         public UpdateOp getOp() {
102                 return op;
103         }
104
105 }