1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.requests;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\r
16 import java.util.Collections;
\r
17 import java.util.Comparator;
\r
18 import java.util.List;
\r
20 import org.simantics.db.Resource;
\r
21 import org.simantics.db.common.ResourceArray;
\r
22 import org.simantics.utils.page.PageDesc;
\r
23 import org.simantics.utils.strings.AlphanumComparator;
\r
26 * @author Tuukka Lehtonen
\r
28 public class Node implements Comparable<Node> {
\r
30 public static final Comparator<Node> CASE_INSENSITIVE_COMPARATOR = new Comparator<Node>() {
\r
32 public int compare(Node o1, Node o2) {
\r
33 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(o1.getName(), o2.getName());
\r
37 private final Node parent;
\r
38 private final List<Node> children = new ArrayList<Node>();
\r
41 * May be <code>null</code> if there is no diagram for this node.
\r
43 private final Resource diagram;
\r
44 private final ResourceArray definingResource; // i.e. Composite
\r
45 private final String name;
\r
47 // private String[] partOfGroups = {};
\r
49 private PageDesc pageDesc;
\r
55 * @param diagram may be <code>null</code> if there is no diagram for this node
\r
56 * @param definingResources
\r
58 public Node(Node parent, String name, Resource diagram, Resource... definingResources) {
\r
59 if (definingResources.length == 0)
\r
60 throw new IllegalArgumentException("must provide at least one defining resource");
\r
61 this.parent = parent;
\r
63 this.diagram = diagram;
\r
64 this.definingResource = new ResourceArray(definingResources);
\r
67 parent.addChild(this);
\r
70 public Node getParent() {
\r
75 * @return <code>null</code> if there is no diagram for this node
\r
77 public Resource getDiagramResource() {
\r
81 public ResourceArray getDefiningResources() {
\r
82 return definingResource;
\r
85 public String getName() {
\r
90 public String toString() {
\r
91 return (parent != null ? parent : "") + "/" + name + definingResource;
\r
94 public void addChild(Node child) {
\r
95 children.add(child);
\r
98 public void removeChild(Node child) {
\r
99 children.remove(child);
\r
102 public Collection<Node> getChildren() {
\r
103 return Collections.unmodifiableCollection(children);
\r
106 // public void setPartOfGroups(Collection<String> groups) {
\r
107 // this.partOfGroups = groups.toArray(new String[groups.size()]);
\r
110 // public String[] getPartOfGroups() {
\r
111 // return partOfGroups;
\r
114 public void setPageDesc(PageDesc pageDesc) {
\r
115 this.pageDesc = pageDesc;
\r
118 public void setRVI(String RVI) {
\r
122 public String getRVI() {
\r
126 public PageDesc getPageDesc() {
\r
131 public int compareTo(Node o) {
\r
132 int ret = name.compareTo(o.name);
\r
137 public int hashCode() {
\r
138 final int prime = 31;
\r
140 result = prime * result + ((diagram == null) ? 0 : diagram.hashCode());
\r
141 result = prime * result + ((parent == null) ? 0 : parent.hashCode());
\r
142 result = prime * result + ((definingResource == null) ? 0 : definingResource.hashCode());
\r
147 public boolean equals(Object obj) {
\r
152 if (getClass() != obj.getClass())
\r
154 Node other = (Node) obj;
\r
155 if (diagram == null) {
\r
156 if (other.diagram != null)
\r
158 } else if (!diagram.equals(other.diagram))
\r
160 if (parent == null) {
\r
161 if (other.parent != null)
\r
163 } else if (!parent.equals(other.parent))
\r
165 if (definingResource == null) {
\r
166 if (other.definingResource != null)
\r
168 } else if (!definingResource.equals(other.definingResource))
\r