1 package org.simantics.interop.update.model;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
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;
15 public class UpdateNode {
17 private UpdateNode parent;
18 private UpdateStatus status;
22 private boolean visible = true;
25 private List<UpdateNode> children;
28 * @param resource old Resource if status is DELETED or EXISTS.
32 public UpdateNode(ReadGraph g, UpdateStatus status, UpdateOp op) throws DatabaseException{
36 this.r = op.getResource();
40 public UpdateNode(ReadGraph g, UpdateStatus status, Resource r) throws DatabaseException {
48 protected void init(ReadGraph g) throws DatabaseException {
49 this.label = getLabel(g, r);
52 public Resource getResource() {
56 public Resource getParentResource(ReadGraph g) throws DatabaseException {
58 Resource parent = op.getParentResource(g);
62 Layer0 l0 = Layer0.getInstance(g);
63 return g.getPossibleObject(r, l0.PartOf);
66 public void setStatus(UpdateStatus status) {
70 public UpdateStatus getStatus() {
74 @SuppressWarnings("unchecked")
75 public List<UpdateNode> getChildren() {
77 return Collections.EMPTY_LIST;
81 public void addChild(UpdateNode node) {
83 children = new ArrayList<UpdateNode>(2);
86 if (op != null && node.op != null) {
87 if (!op.getSubOps().contains(node.op)) {
89 node.op.addParentOp(op);
97 Comparator<UpdateNode> comparator = new Comparator<UpdateNode>() {
99 public int compare(UpdateNode o1, UpdateNode o2) {
100 return o1.getLabel().compareTo(o2.getLabel());
103 Collections.sort(this.children, comparator );
104 for (UpdateNode n : this.children) {
109 public void sort(Comparator<UpdateNode> comparator ) {
110 if (children == null)
112 Collections.sort(this.children, comparator );
113 for (UpdateNode n : this.children) {
118 public ImageDescriptor getImage(ReadGraph graph) throws DatabaseException {
122 public String getLabel() {
127 public String toString() {
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);
140 public UpdateOp getOp() {
144 public boolean isVisible() {
148 public void setVisible(boolean visible) {
149 this.visible = visible;
151 op.visible = visible;
153 if (parent != null && !parent.visible)
154 parent.setVisible(true);
156 if (children != null)
157 for (UpdateNode n : children)
162 public void setAllVisible(boolean visible) {
163 this.visible = visible;
165 op.visible = visible;
166 if (children != null)
167 for (UpdateNode n : children)
168 n.setAllVisible(visible);