]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.update/src/org/simantics/interop/update/model/UpdateNode.java
Base classes for model updates
[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.db.exception.NoSingleResultException;
12 import org.simantics.db.exception.ServiceException;
13 import org.simantics.db.exception.ValidationException;
14 import org.simantics.layer0.Layer0;
15
16 public abstract class UpdateNode {
17
18         public enum Status {EXIST,DELETED,NEW,CONTAINS};
19         
20
21         private Status status;
22         private UpdateOp op;
23         private Resource r;
24         
25         
26         private Collection<UpdateNode> children = new ArrayList<UpdateNode>();
27         /**
28          * 
29          * @param resource old Resource if status is DELETED or EXISTS.
30          * @param status
31          * @param changes
32          */
33         public UpdateNode(Status status, UpdateOp op) {
34
35                 this.status = status;
36                 this.op = op;
37                 this.r = op.getResource();
38         }
39         
40         public UpdateNode(Status status, Resource r) {
41
42                 this.status = status;
43                 this.op = null;
44                 this.r = r;
45         }
46         
47         public Resource getResource() {
48                 return r;
49         }
50         
51         public Resource getParentResource(ReadGraph g) throws DatabaseException {
52                 Layer0 l0 = Layer0.getInstance(g);
53                 return g.getPossibleObject(r, l0.PartOf);
54         }
55         
56         public void setStatus(Status status) {
57                 this.status = status;
58         }
59         
60         public Status getStatus() {
61                 return status;
62         }
63         
64         public Collection<UpdateNode> getChildren() {
65                 return children;
66         }
67         
68         public void addChild(UpdateNode node) {
69                 children.add(node);
70         }
71
72         public abstract ImageDescriptor getImage(ReadGraph graph) throws DatabaseException;
73
74         public String getLabel(ReadGraph graph) throws DatabaseException {
75                 return getLabel(graph,r);
76         }
77         
78         protected String getLabel(ReadGraph graph, Resource r) throws ValidationException, ServiceException, NoSingleResultException {
79                 String label = NameUtils.getSafeLabel(graph, r);
80                 if (label.length() == 0)
81                         label = NameUtils.getSafeName(graph, r);
82                 
83                 return label;
84         }
85
86
87         public UpdateOp getOp() {
88                 return op;
89         }
90
91 }