]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode.java
Allow UpdateOps to describe parent resource
[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                 if (op != null) {
56                         Resource parent = op.getParentResource(g);
57                         if (parent != null)
58                                 return parent;
59                 }
60                 Layer0 l0 = Layer0.getInstance(g);
61                 return g.getPossibleObject(r, l0.PartOf);
62         }
63         
64         public void setStatus(UpdateStatus status) {
65                 this.status = status;
66         }
67         
68         public UpdateStatus getStatus() {
69                 return status;
70         }
71         
72         public Collection<UpdateNode> getChildren() {
73                 return children;
74         }
75         
76         public void addChild(UpdateNode node) {
77                 children.add(node);
78                 node.parent = this;
79                 if (op != null && node.op != null) {
80                         if (!op.getSubOps().contains(node.op)) {
81                                 op.addSubOp(node.op);
82                                 node.op.addParentOp(op);
83                         }
84                 }
85         }
86
87         public ImageDescriptor getImage(ReadGraph graph) throws DatabaseException {
88                 return null;
89         }
90
91         public String getLabel() {
92                 return label;
93         }
94         
95         @Override
96         public String toString() {
97                 return label;
98         }
99         
100         protected String getLabel(ReadGraph graph, Resource r) throws DatabaseException {
101                 String label = NameUtils.getSafeLabel(graph, r);
102                 if (label.length() == 0)
103                         label = NameUtils.getSafeName(graph, r);
104                 
105                 return label;
106         }
107
108
109         public UpdateOp getOp() {
110                 return op;
111         }
112         
113         public boolean isVisible() {
114                 return visible;
115         }
116         
117         public void setVisible(boolean visible) {
118                 this.visible = visible;
119                 if (op != null)
120                         op.visible = visible;
121                 if (visible) {
122                     if (parent != null && !parent.visible)
123                         parent.setVisible(true);
124                 } else {
125                     for (UpdateNode n : children)
126                         n.setVisible(false);
127                 }
128         }
129         
130         public void setAllVisible(boolean visible) {
131         this.visible = visible;
132         if (op != null)
133             op.visible = visible;
134         for (UpdateNode n : children)
135              n.setAllVisible(visible);
136     }
137
138 }