]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode.java
f68f887dca33c98e925862c203dab27926ae5480
[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.Collections;
5 import java.util.Comparator;
6 import java.util.List;
7
8 import org.eclipse.jface.resource.ImageDescriptor;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.utils.NameUtils;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.layer0.Layer0;
14
15 public class UpdateNode {
16
17     private UpdateNode parent;
18         private UpdateStatus status;
19         private UpdateOp op;
20         private Resource r;
21         private String label;
22         private boolean visible = true;
23         
24         
25         private List<UpdateNode> children;
26         /**
27          * 
28          * @param resource old Resource if status is DELETED or EXISTS.
29          * @param status
30          * @param changes
31          */
32         public UpdateNode(ReadGraph g, UpdateStatus status, UpdateOp op) throws DatabaseException{
33
34                 this.status = status;
35                 this.op = op;
36                 this.r = op.getResource();
37                 init(g);
38         }
39         
40         public UpdateNode(ReadGraph g, UpdateStatus status, Resource r) throws DatabaseException {
41
42                 this.status = status;
43                 this.op = null;
44                 this.r = r;
45                 init(g);
46         }
47         
48         protected void init(ReadGraph g) throws DatabaseException {
49                 this.label = getLabel(g, r);
50         }
51         
52         public Resource getResource() {
53                 return r;
54         }
55         
56         public Resource getParentResource(ReadGraph g) throws DatabaseException {
57                 if (op != null) {
58                         Resource parent = op.getParentResource(g);
59                         if (parent != null)
60                                 return parent;
61                 }
62                 Layer0 l0 = Layer0.getInstance(g);
63                 return g.getPossibleObject(r, l0.PartOf);
64         }
65         
66         public void setStatus(UpdateStatus status) {
67                 this.status = status;
68         }
69         
70         public UpdateStatus getStatus() {
71                 return status;
72         }
73         
74         @SuppressWarnings("unchecked")
75         public List<UpdateNode> getChildren() {
76                 if (children == null)
77                         return Collections.EMPTY_LIST;
78                 return children;
79         }
80         
81         public void addChild(UpdateNode node) {
82                 if (children == null)
83                         children = new ArrayList<UpdateNode>(2);
84                 children.add(node);
85                 node.parent = this;
86                 if (op != null && node.op != null) {
87                         if (!op.getSubOps().contains(node.op)) {
88                                 op.addSubOp(node.op);
89                                 node.op.addParentOp(op);
90                         }
91                 }
92         }
93         
94         public void sort() {
95                 if (children == null)
96                         return;
97                 Collections.sort(this.children, new Comparator<UpdateNode>() {
98                         @Override
99                         public int compare(UpdateNode o1, UpdateNode o2) {
100                                 return o1.getLabel().compareTo(o2.getLabel());
101                         }
102                 });
103         }
104
105         public ImageDescriptor getImage(ReadGraph graph) throws DatabaseException {
106                 return null;
107         }
108
109         public String getLabel() {
110                 return label;
111         }
112         
113         @Override
114         public String toString() {
115                 return label;
116         }
117         
118         protected String getLabel(ReadGraph graph, Resource r) throws DatabaseException {
119                 String label = NameUtils.getSafeLabel(graph, r);
120                 if (label.length() == 0)
121                         label = NameUtils.getSafeName(graph, r);
122                 
123                 return label;
124         }
125
126
127         public UpdateOp getOp() {
128                 return op;
129         }
130         
131         public boolean isVisible() {
132                 return visible;
133         }
134         
135         public void setVisible(boolean visible) {
136                 this.visible = visible;
137                 if (op != null)
138                         op.visible = visible;
139                 if (visible) {
140                     if (parent != null && !parent.visible)
141                         parent.setVisible(true);
142                 } else {
143                         if (children != null)
144                                 for (UpdateNode n : children)
145                                         n.setVisible(false);
146                 }
147         }
148         
149         public void setAllVisible(boolean visible) {
150                 this.visible = visible;
151                 if (op != null)
152                         op.visible = visible;
153                 if (children != null)
154                         for (UpdateNode n : children)
155                                 n.setAllVisible(visible);
156         }
157
158 }