]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/node/AbstractNode.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / node / AbstractNode.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.common.node;\r
13 \r
14 import org.eclipse.core.runtime.IAdaptable;\r
15 import org.simantics.browsing.ui.common.imagers.ImageURLs;\r
16 import org.simantics.browsing.ui.content.Imager;\r
17 import org.simantics.browsing.ui.content.Labeler;\r
18 import org.simantics.utils.ReflectionUtils;\r
19 \r
20 public abstract class AbstractNode<T> implements IAdaptable {\r
21 \r
22     public final T data;\r
23     public final Class<?> clazz;\r
24 \r
25     public AbstractNode(T data) {\r
26         assert(data != null);\r
27         this.data = data;\r
28         clazz = ReflectionUtils.getSingleParameterType(getClass());\r
29     }\r
30 \r
31     @Override\r
32     public int hashCode() {\r
33         return data.hashCode() + getClass().hashCode();\r
34     }\r
35 \r
36     @Override\r
37     public boolean equals(Object obj) {\r
38         if (this == obj)\r
39             return true;\r
40         if (obj == null)\r
41             return false;\r
42         if (getClass() != obj.getClass())\r
43             return false;\r
44         AbstractNode<?> other = (AbstractNode<?>) obj;\r
45         return data.equals(other.data);\r
46     }\r
47 \r
48     @SuppressWarnings({ "rawtypes" })\r
49     @Override\r
50     public Object getAdapter(Class adapter) {\r
51         if(clazz == adapter)\r
52             return data;\r
53         if(Imager.class == adapter)\r
54             return getImager();\r
55         if(ImageURLs.class == adapter)\r
56             return getImageURLs();\r
57         if(Labeler.class == adapter)\r
58             return getLabeler();\r
59         return null;\r
60     }\r
61 \r
62     protected Imager getImager() {\r
63         return null;\r
64     }\r
65 \r
66     protected ImageURLs getImageURLs() {\r
67         return null;\r
68     }\r
69 \r
70     protected Labeler getLabeler() {\r
71         return null;\r
72     }\r
73 \r
74     @Override\r
75     public String toString() {\r
76         return getClass().getSimpleName();\r
77     }\r
78 \r
79 }\r