]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode.java
b786586c9a3daef1c2b562707e1b07779d107a54
[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 UpdateNode parent;
16         private UpdateStatus status;
17         private UpdateOp op;
18         private Resource r;
19         private String label;
20         private boolean visible = true;
21         
22         
23         private Collection<UpdateNode> children = new ArrayList<UpdateNode>();
24         /**
25          * 
26          * @param resource old Resource if status is DELETED or EXISTS.
27          * @param status
28          * @param changes
29          */
30         public UpdateNode(ReadGraph g, UpdateStatus status, UpdateOp op) throws DatabaseException{
31
32                 this.status = status;
33                 this.op = op;
34                 this.r = op.getResource();
35                 init(g);
36         }
37         
38         public UpdateNode(ReadGraph g, UpdateStatus status, Resource r) throws DatabaseException {
39
40                 this.status = status;
41                 this.op = null;
42                 this.r = r;
43                 init(g);
44         }
45         
46         protected void init(ReadGraph g) throws DatabaseException {
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(UpdateStatus status) {
60                 this.status = status;
61         }
62         
63         public UpdateStatus 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                 node.parent = this;
74                 if (op != null && node.op != null) {
75                         if (!op.getSubOps().contains(node.op)) {
76                                 op.addSubOp(node.op);
77                                 node.op.addParentOp(op);
78                         }
79                 }
80         }
81
82         public ImageDescriptor getImage(ReadGraph graph) throws DatabaseException {
83                 return null;
84         }
85
86         public String getLabel() {
87                 return label;
88         }
89         
90         @Override
91         public String toString() {
92                 return label;
93         }
94         
95         protected String getLabel(ReadGraph graph, Resource r) throws DatabaseException {
96                 String label = NameUtils.getSafeLabel(graph, r);
97                 if (label.length() == 0)
98                         label = NameUtils.getSafeName(graph, r);
99                 
100                 return label;
101         }
102
103
104         public UpdateOp getOp() {
105                 return op;
106         }
107         
108         public boolean isVisible() {
109                 return visible;
110         }
111         
112         public void setVisible(boolean visible) {
113                 this.visible = visible;
114                 if (op != null)
115                         op.visible = visible;
116                 if (visible) {
117                     if (parent != null && !parent.visible)
118                         parent.setVisible(true);
119                 } else {
120                     for (UpdateNode n : children)
121                         n.setVisible(false);
122                 }
123         }
124         
125         public void setAllVisible(boolean visible) {
126         this.visible = visible;
127         if (op != null)
128             op.visible = visible;
129         for (UpdateNode n : children)
130              n.setAllVisible(visible);
131     }
132
133 }