]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph.ui/src/org/simantics/scenegraph/ui/NodeProxy.java
Merge "Documented difference of RuntimeEnvironmentRequest and Runti...quest2"
[simantics/platform.git] / bundles / org.simantics.scenegraph.ui / src / org / simantics / scenegraph / ui / NodeProxy.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.scenegraph.ui;
13
14 import java.lang.ref.WeakReference;
15
16 import org.simantics.scenegraph.INode;
17
18 /**
19  * @author Tuukka Lehtonen
20  */
21 class NodeProxy {
22
23     WeakReference<INode> ref;
24     String               id;
25
26     NodeProxy(INode n) {
27         this(n, null);
28     }
29
30     NodeProxy(INode n, String id) {
31         this.ref = new WeakReference<INode>(n);
32         this.id = id;
33     }
34
35     INode getNode() {
36         return ref.get();
37     }
38
39     @Override
40     public String toString() {
41         StringBuilder sb = new StringBuilder();
42         INode n = getNode();
43         if (id != null) {
44             sb.append("[");
45             sb.append(id);
46             sb.append("] ");
47         }
48         if (n == null) {
49             sb.append("(collected)");
50         } else {
51             sb.append(n.getSimpleClassName());
52         }
53         return sb.toString();
54     }
55
56     public String getTypeName() {
57         INode n = getNode();
58         return n == null ? "-" : n.getSimpleClassName();
59     }
60
61     public String getInternalId() {
62         INode n = getNode();
63         if (n == null)
64             return "-";
65         return Long.toString(n.getId());
66     }
67
68     public String getId() {
69         return id;
70     }
71
72     @Override
73     public int hashCode() {
74         final int prime = 31;
75         int result = 1;
76         result = prime * result + ((id == null) ? 0 : id.hashCode());
77         INode node = getNode();
78         result = prime * result + ((node == null) ? 0 : node.hashCode());
79         return result;
80     }
81
82     @Override
83     public boolean equals(Object obj) {
84         if (this == obj)
85             return true;
86         if (obj == null)
87             return false;
88         if (getClass() != obj.getClass())
89             return false;
90         NodeProxy other = (NodeProxy) obj;
91         if (id == null) {
92             if (other.id != null)
93                 return false;
94         } else if (!id.equals(other.id))
95             return false;
96
97         INode node = getNode();
98         INode otherNode = other.getNode();
99         if (node == null || otherNode == null)
100             // Collected nodes are never equal to any other node
101             return false;
102
103         return node.equals(otherNode);
104     }
105
106 }