]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode.java
Sort UpdateNodes
[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                 Comparator<UpdateNode> comparator = new Comparator<UpdateNode>() {
98                         @Override
99                         public int compare(UpdateNode o1, UpdateNode o2) {
100                                 return o1.getLabel().compareTo(o2.getLabel());
101                         }
102                 };
103                 Collections.sort(this.children, comparator );
104                 for (UpdateNode n : this.children) {
105                         n.sort(comparator);
106                 }
107         }
108         
109         public void sort(Comparator<UpdateNode> comparator ) {
110                 if (children == null)
111                         return;
112                 Collections.sort(this.children, comparator );
113                 for (UpdateNode n : this.children) {
114                         n.sort(comparator);
115                 }
116         }
117
118         public ImageDescriptor getImage(ReadGraph graph) throws DatabaseException {
119                 return null;
120         }
121
122         public String getLabel() {
123                 return label;
124         }
125         
126         @Override
127         public String toString() {
128                 return label;
129         }
130         
131         protected String getLabel(ReadGraph graph, Resource r) throws DatabaseException {
132                 String label = NameUtils.getSafeLabel(graph, r);
133                 if (label.length() == 0)
134                         label = NameUtils.getSafeName(graph, r);
135                 
136                 return label;
137         }
138
139
140         public UpdateOp getOp() {
141                 return op;
142         }
143         
144         public boolean isVisible() {
145                 return visible;
146         }
147         
148         public void setVisible(boolean visible) {
149                 this.visible = visible;
150                 if (op != null)
151                         op.visible = visible;
152                 if (visible) {
153                     if (parent != null && !parent.visible)
154                         parent.setVisible(true);
155                 } else {
156                         if (children != null)
157                                 for (UpdateNode n : children)
158                                         n.setVisible(false);
159                 }
160         }
161         
162         public void setAllVisible(boolean visible) {
163                 this.visible = visible;
164                 if (op != null)
165                         op.visible = visible;
166                 if (children != null)
167                         for (UpdateNode n : children)
168                                 n.setAllVisible(visible);
169         }
170
171 }