]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/common/ObserverGroupListener.java b/bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/common/ObserverGroupListener.java
new file mode 100644 (file)
index 0000000..d9151fb
--- /dev/null
@@ -0,0 +1,105 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.scenegraph.profile.common;\r
+\r
+import java.util.Map;\r
+import java.util.Set;\r
+import java.util.concurrent.ConcurrentHashMap;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.procedure.SetListener;\r
+import org.simantics.scenegraph.profile.Group;\r
+import org.simantics.scenegraph.profile.Observer;\r
+import org.simantics.scenegraph.profile.Style;\r
+\r
+public class ObserverGroupListener implements SetListener<Resource> {\r
+\r
+    final Map<Object, Object> items = new ConcurrentHashMap<Object, Object>();\r
+    final Map<Resource, Resource> entries = new ConcurrentHashMap<Resource, Resource>();\r
+\r
+    protected final Style style;\r
+    protected final Group group;\r
+    protected final Observer observer;\r
+    private boolean disposed = false;\r
+\r
+    public ObserverGroupListener(Style style, Group group, Observer observer) {\r
+        assert(style != null);\r
+        assert(group != null);\r
+        assert(observer != null);\r
+        this.style = style;\r
+        this.group = group;\r
+        this.observer = observer;\r
+    }\r
+\r
+    @Override\r
+    public void add(Resource item) {\r
+        //System.out.println("Add to group(" + this + "): " + item);\r
+        items.put(item, item);\r
+        observer.update();\r
+    }\r
+\r
+    @Override\r
+    public void remove(Resource item) {\r
+//        new Exception().printStackTrace();\r
+        //System.out.println("Remove from group(" + this + "): " + item);\r
+        items.remove(item);\r
+        observer.update();\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return observer.hashCode() + 31 * group.hashCode() + 41 * style.hashCode();\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object object) {\r
+        if (this == object)\r
+            return true;\r
+        else if (object == null)\r
+            return false;\r
+        else if (ObserverGroupListener.class != object.getClass())\r
+            return false;\r
+        ObserverGroupListener other = (ObserverGroupListener)object;\r
+        return observer.equals(other.observer) && group.equals(other.group) && style.equals(other.style);\r
+    }\r
+\r
+    public Set<Object> getItems() {\r
+        return items.keySet();\r
+    }\r
+\r
+    public void dispose() {\r
+        this.disposed = true;\r
+    }\r
+\r
+    @Override\r
+    public boolean isDisposed() {\r
+        return disposed || observer.isDisposed();\r
+    }\r
+\r
+    @Override\r
+    public void exception(Throwable throwable) {\r
+        observer.exception(throwable);\r
+    }\r
+\r
+    public void addEntry(Resource entry) {\r
+        entries.put(entry, entry);\r
+    }\r
+\r
+    public void removeEntry(Resource entry) {\r
+        entries.remove(entry);\r
+    }\r
+\r
+    public boolean hasEntries() {\r
+        return !entries.isEmpty();\r
+    }\r
+\r
+}\r