]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/handler/impl/LockingTransactionContext.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / diagram / handler / impl / LockingTransactionContext.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/handler/impl/LockingTransactionContext.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/diagram/handler/impl/LockingTransactionContext.java
new file mode 100644 (file)
index 0000000..a42fdf0
--- /dev/null
@@ -0,0 +1,133 @@
+/*******************************************************************************\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.g2d.diagram.handler.impl;\r
+\r
+import java.util.Collection;\r
+import java.util.concurrent.locks.ReadWriteLock;\r
+import java.util.concurrent.locks.ReentrantReadWriteLock;\r
+\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.diagram.handler.LifeCycle;\r
+import org.simantics.g2d.diagram.handler.TransactionContext;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.utils.datastructures.ListenerList;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class LockingTransactionContext implements TransactionContext, LifeCycle {\r
+\r
+    public static final LockingTransactionContext INSTANCE = new LockingTransactionContext();\r
+\r
+    private static final Key TRANSACTION_LISTENERS = new KeyOf(ListenerList.class, "TRANSACTION LISTENERS");\r
+    private static final Key TRANSACTION_LOCK      = new KeyOf(ReadWriteLock.class, "TRANSACTION LOCK");\r
+\r
+    @Override\r
+    public void onDiagramCreated(IDiagram diagram) {\r
+        diagram.setHint(TRANSACTION_LISTENERS, new ListenerList<TransactionListener>(TransactionListener.class));\r
+        diagram.setHint(TRANSACTION_LOCK, new ReentrantReadWriteLock(true));\r
+    }\r
+\r
+    @Override\r
+    public void onDiagramLoaded(IDiagram diagram, Collection<IElement> initialElements) {\r
+    }\r
+\r
+    @Override\r
+    public void onDiagramDestroyed(IDiagram diagram) {\r
+    }\r
+\r
+    @Override\r
+    public void onDiagramDisposed(IDiagram diagram) {\r
+        diagram.removeHint(TRANSACTION_LISTENERS);\r
+        diagram.removeHint(TRANSACTION_LOCK);\r
+    }\r
+\r
+    @Override\r
+    public void addTransactionListener(IDiagram d, TransactionListener l) {\r
+        ListenerList<TransactionListener> ll;\r
+        synchronized (d) {\r
+            ll = d.getHint(TRANSACTION_LISTENERS);\r
+            if (ll == null) {\r
+                ll = new ListenerList<TransactionListener>(TransactionListener.class);\r
+                d.setHint(TRANSACTION_LISTENERS, ll);\r
+            }\r
+        }\r
+        ll.add(l);\r
+    }\r
+\r
+    @Override\r
+    public void removeTransactionListener(IDiagram d, TransactionListener l) {\r
+        synchronized (d) {\r
+            ListenerList<TransactionListener> ll = d.getHint(TRANSACTION_LISTENERS);\r
+            if (ll == null)\r
+                return;\r
+            ll.remove(l);\r
+        }\r
+    }\r
+\r
+    void lock(ReadWriteLock lock, TransactionType type) {\r
+        switch (type) {\r
+            case READ:\r
+                lock.readLock().lock();\r
+                return;\r
+            case WRITE:\r
+                lock.writeLock().lock();\r
+                return;\r
+        }\r
+    }\r
+\r
+    void unlock(ReadWriteLock lock, TransactionType type) {\r
+        switch (type) {\r
+            case READ:\r
+                lock.readLock().unlock();\r
+                return;\r
+            case WRITE:\r
+                lock.writeLock().unlock();\r
+                return;\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public Transaction startTransaction(IDiagram d, TransactionType type) {\r
+        ReadWriteLock lock = d.getHint(TRANSACTION_LOCK);\r
+        assert lock != null;\r
+        lock(lock, type);\r
+        try {\r
+            Transaction t = new Transaction.Impl(type);\r
+            ListenerList<TransactionListener> ll = d.getHint(TRANSACTION_LISTENERS);\r
+            if (ll != null)\r
+                for (TransactionListener l : ll.getListeners())\r
+                    l.transactionStarted(d, t);\r
+            return t;\r
+        } catch (RuntimeException e) {\r
+            unlock(lock, type);\r
+            throw e;\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public void finishTransaction(IDiagram d, Transaction t) {\r
+        ReadWriteLock lock = d.getHint(TRANSACTION_LOCK);\r
+        assert lock != null;\r
+        try {\r
+            ListenerList<TransactionListener> ll = d.getHint(TRANSACTION_LISTENERS);\r
+            if (ll != null)\r
+                for (TransactionListener l : ll.getListeners())\r
+                    l.transactionFinished(d, t);\r
+        } finally {\r
+            unlock(lock, t.getType());\r
+        }\r
+    }\r
+\r
+}\r