]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/common/ObserverGroupListener.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scenegraph.profile / src / org / simantics / scenegraph / profile / common / ObserverGroupListener.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.scenegraph.profile.common;\r
13 \r
14 import java.util.Map;\r
15 import java.util.Set;\r
16 import java.util.concurrent.ConcurrentHashMap;\r
17 \r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.procedure.SetListener;\r
20 import org.simantics.scenegraph.profile.Group;\r
21 import org.simantics.scenegraph.profile.Observer;\r
22 import org.simantics.scenegraph.profile.Style;\r
23 \r
24 public class ObserverGroupListener implements SetListener<Resource> {\r
25 \r
26     final Map<Object, Object> items = new ConcurrentHashMap<Object, Object>();\r
27     final Map<Resource, Resource> entries = new ConcurrentHashMap<Resource, Resource>();\r
28 \r
29     protected final Style style;\r
30     protected final Group group;\r
31     protected final Observer observer;\r
32     private boolean disposed = false;\r
33 \r
34     public ObserverGroupListener(Style style, Group group, Observer observer) {\r
35         assert(style != null);\r
36         assert(group != null);\r
37         assert(observer != null);\r
38         this.style = style;\r
39         this.group = group;\r
40         this.observer = observer;\r
41     }\r
42 \r
43     @Override\r
44     public void add(Resource item) {\r
45         //System.out.println("Add to group(" + this + "): " + item);\r
46         items.put(item, item);\r
47         observer.update();\r
48     }\r
49 \r
50     @Override\r
51     public void remove(Resource item) {\r
52 //        new Exception().printStackTrace();\r
53         //System.out.println("Remove from group(" + this + "): " + item);\r
54         items.remove(item);\r
55         observer.update();\r
56     }\r
57 \r
58     @Override\r
59     public int hashCode() {\r
60         return observer.hashCode() + 31 * group.hashCode() + 41 * style.hashCode();\r
61     }\r
62 \r
63     @Override\r
64     public boolean equals(Object object) {\r
65         if (this == object)\r
66             return true;\r
67         else if (object == null)\r
68             return false;\r
69         else if (ObserverGroupListener.class != object.getClass())\r
70             return false;\r
71         ObserverGroupListener other = (ObserverGroupListener)object;\r
72         return observer.equals(other.observer) && group.equals(other.group) && style.equals(other.style);\r
73     }\r
74 \r
75     public Set<Object> getItems() {\r
76         return items.keySet();\r
77     }\r
78 \r
79     public void dispose() {\r
80         this.disposed = true;\r
81     }\r
82 \r
83     @Override\r
84     public boolean isDisposed() {\r
85         return disposed || observer.isDisposed();\r
86     }\r
87 \r
88     @Override\r
89     public void exception(Throwable throwable) {\r
90         observer.exception(throwable);\r
91     }\r
92 \r
93     public void addEntry(Resource entry) {\r
94         entries.put(entry, entry);\r
95     }\r
96 \r
97     public void removeEntry(Resource entry) {\r
98         entries.remove(entry);\r
99     }\r
100 \r
101     public boolean hasEntries() {\r
102         return !entries.isEmpty();\r
103     }\r
104 \r
105 }\r