]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode.java
Added support for filtering changes
[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         private boolean visible = true;
20         
21         
22         private Collection<UpdateNode> children = new ArrayList<UpdateNode>();
23         /**
24          * 
25          * @param resource old Resource if status is DELETED or EXISTS.
26          * @param status
27          * @param changes
28          */
29         public UpdateNode(ReadGraph g, UpdateStatus status, UpdateOp op) throws DatabaseException{
30
31                 this.status = status;
32                 this.op = op;
33                 this.r = op.getResource();
34                 init(g);
35         }
36         
37         public UpdateNode(ReadGraph g, UpdateStatus status, Resource r) throws DatabaseException {
38
39                 this.status = status;
40                 this.op = null;
41                 this.r = r;
42                 init(g);
43         }
44         
45         protected void init(ReadGraph g) throws DatabaseException {
46                 this.label = getLabel(g, r);
47         }
48         
49         public Resource getResource() {
50                 return r;
51         }
52         
53         public Resource getParentResource(ReadGraph g) throws DatabaseException {
54                 Layer0 l0 = Layer0.getInstance(g);
55                 return g.getPossibleObject(r, l0.PartOf);
56         }
57         
58         public void setStatus(UpdateStatus status) {
59                 this.status = status;
60         }
61         
62         public UpdateStatus getStatus() {
63                 return status;
64         }
65         
66         public Collection<UpdateNode> getChildren() {
67                 return children;
68         }
69         
70         public void addChild(UpdateNode node) {
71                 children.add(node);
72                 if (op != null && node.op != null) {
73                         if (!op.getSubOps().contains(node.op)) {
74                                 op.addSubOp(node.op);
75                                 node.op.addParentOp(op);
76                         }
77                 }
78         }
79
80         public ImageDescriptor getImage(ReadGraph graph) throws DatabaseException {
81                 return null;
82         }
83
84         public String getLabel() {
85                 return label;
86         }
87         
88         @Override
89         public String toString() {
90                 return label;
91         }
92         
93         protected String getLabel(ReadGraph graph, Resource r) throws DatabaseException {
94                 String label = NameUtils.getSafeLabel(graph, r);
95                 if (label.length() == 0)
96                         label = NameUtils.getSafeName(graph, r);
97                 
98                 return label;
99         }
100
101
102         public UpdateOp getOp() {
103                 return op;
104         }
105         
106         public boolean isVisible() {
107                 return visible;
108         }
109         
110         public void setVisible(boolean visible) {
111                 this.visible = visible;
112                 if (op != null)
113                         op.visible = visible;
114         }
115
116 }