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