1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.requests;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.Comparator;
18 import java.util.List;
20 import org.simantics.db.Resource;
21 import org.simantics.db.common.ResourceArray;
22 import org.simantics.utils.page.PageDesc;
23 import org.simantics.utils.strings.AlphanumComparator;
26 * @author Tuukka Lehtonen
28 public class Node implements Comparable<Node> {
30 public static final Comparator<Node> CASE_INSENSITIVE_COMPARATOR =
31 (o1, o2) -> AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.getName(), o2.getName());
33 private final Node parent;
34 private final List<Node> children = new ArrayList<Node>();
35 private final List<Node> unmodifiableChildren = Collections.unmodifiableList(children);
38 * May be <code>null</code> if there is no diagram for this node.
40 private final Resource diagram;
41 private final ResourceArray definingResource; // i.e. Composite
42 private final String name;
44 private PageDesc pageDesc;
50 * @param diagram may be <code>null</code> if there is no diagram for this node
51 * @param definingResources
53 public Node(Node parent, String name, Resource diagram, Resource... definingResources) {
54 if (definingResources.length == 0)
55 throw new IllegalArgumentException("must provide at least one defining resource");
58 this.diagram = diagram;
59 this.definingResource = new ResourceArray(definingResources);
62 parent.addChild(this);
65 public Node cloneWithoutChildren(Node parent) {
66 Node clone = new Node(parent, name, diagram, definingResource.resources);
68 clone.setPageDesc(pageDesc);
72 public Node getParent() {
77 * @return <code>null</code> if there is no diagram for this node
79 public Resource getDiagramResource() {
83 public ResourceArray getDefiningResources() {
84 return definingResource;
87 public String getName() {
92 public String toString() {
93 return (parent != null ? parent : "") + "/" + name + definingResource;
96 public void addChild(Node child) {
100 public void removeChild(Node child) {
101 children.remove(child);
104 public Collection<Node> getChildren() {
105 return unmodifiableChildren;
108 public void setPageDesc(PageDesc pageDesc) {
109 this.pageDesc = pageDesc;
112 public void setRVI(String rvi) {
116 public String getRVI() {
120 public PageDesc getPageDesc() {
125 public int compareTo(Node o) {
126 int ret = name.compareTo(o.name);
131 public int hashCode() {
132 final int prime = 31;
134 result = prime * result + ((diagram == null) ? 0 : diagram.hashCode());
135 result = prime * result + ((parent == null) ? 0 : parent.hashCode());
136 result = prime * result + ((definingResource == null) ? 0 : definingResource.hashCode());
141 public boolean equals(Object obj) {
146 if (getClass() != obj.getClass())
148 Node other = (Node) obj;
149 if (diagram == null) {
150 if (other.diagram != null)
152 } else if (!diagram.equals(other.diagram))
154 if (parent == null) {
155 if (other.parent != null)
157 } else if (!parent.equals(other.parent))
159 if (definingResource == null) {
160 if (other.definingResource != null)
162 } else if (!definingResource.equals(other.definingResource))