]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/node/AbstractNode.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / node / AbstractNode.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.browsing.ui.common.node;
13
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.simantics.browsing.ui.common.imagers.ImageURLs;
16 import org.simantics.browsing.ui.content.Imager;
17 import org.simantics.browsing.ui.content.Labeler;
18 import org.simantics.utils.ReflectionUtils;
19
20 public abstract class AbstractNode<T> implements IAdaptable {
21
22     public final T data;
23     public final Class<?> clazz;
24
25     public AbstractNode(T data) {
26         assert(data != null);
27         this.data = data;
28         clazz = ReflectionUtils.getSingleParameterType(getClass());
29     }
30
31     @Override
32     public int hashCode() {
33         return data.hashCode() + getClass().hashCode();
34     }
35
36     @Override
37     public boolean equals(Object obj) {
38         if (this == obj)
39             return true;
40         if (obj == null)
41             return false;
42         if (getClass() != obj.getClass())
43             return false;
44         AbstractNode<?> other = (AbstractNode<?>) obj;
45         return data.equals(other.data);
46     }
47
48     @SuppressWarnings("unchecked")
49     @Override
50     public <A> A getAdapter(Class<A> adapter) {
51         if(clazz == adapter)
52             return (A) data;
53         if(Imager.class == adapter)
54             return (A) getImager();
55         if(ImageURLs.class == adapter)
56             return (A) getImageURLs();
57         if(Labeler.class == adapter)
58             return (A) getLabeler();
59         return null;
60     }
61
62     protected Imager getImager() {
63         return null;
64     }
65
66     protected ImageURLs getImageURLs() {
67         return null;
68     }
69
70     protected Labeler getLabeler() {
71         return null;
72     }
73
74     @Override
75     public String toString() {
76         return getClass().getSimpleName();
77     }
78
79 }