-/*******************************************************************************\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
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g2d.diagram.handler.impl;
+
+import java.util.Collection;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+import org.simantics.g2d.diagram.IDiagram;
+import org.simantics.g2d.diagram.handler.LifeCycle;
+import org.simantics.g2d.diagram.handler.TransactionContext;
+import org.simantics.g2d.element.IElement;
+import org.simantics.utils.datastructures.ListenerList;
+import org.simantics.utils.datastructures.hints.IHintContext.Key;
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class LockingTransactionContext implements TransactionContext, LifeCycle {
+
+ public static final LockingTransactionContext INSTANCE = new LockingTransactionContext();
+
+ private static final Key TRANSACTION_LISTENERS = new KeyOf(ListenerList.class, "TRANSACTION LISTENERS");
+ private static final Key TRANSACTION_LOCK = new KeyOf(ReadWriteLock.class, "TRANSACTION LOCK");
+
+ @Override
+ public void onDiagramCreated(IDiagram diagram) {
+ diagram.setHint(TRANSACTION_LISTENERS, new ListenerList<TransactionListener>(TransactionListener.class));
+ diagram.setHint(TRANSACTION_LOCK, new ReentrantReadWriteLock(true));
+ }
+
+ @Override
+ public void onDiagramLoaded(IDiagram diagram, Collection<IElement> initialElements) {
+ }
+
+ @Override
+ public void onDiagramDestroyed(IDiagram diagram) {
+ }
+
+ @Override
+ public void onDiagramDisposed(IDiagram diagram) {
+ diagram.removeHint(TRANSACTION_LISTENERS);
+ diagram.removeHint(TRANSACTION_LOCK);
+ }
+
+ @Override
+ public void addTransactionListener(IDiagram d, TransactionListener l) {
+ ListenerList<TransactionListener> ll;
+ synchronized (d) {
+ ll = d.getHint(TRANSACTION_LISTENERS);
+ if (ll == null) {
+ ll = new ListenerList<TransactionListener>(TransactionListener.class);
+ d.setHint(TRANSACTION_LISTENERS, ll);
+ }
+ }
+ ll.add(l);
+ }
+
+ @Override
+ public void removeTransactionListener(IDiagram d, TransactionListener l) {
+ synchronized (d) {
+ ListenerList<TransactionListener> ll = d.getHint(TRANSACTION_LISTENERS);
+ if (ll == null)
+ return;
+ ll.remove(l);
+ }
+ }
+
+ void lock(ReadWriteLock lock, TransactionType type) {
+ switch (type) {
+ case READ:
+ lock.readLock().lock();
+ return;
+ case WRITE:
+ lock.writeLock().lock();
+ return;
+ }
+ }
+
+ void unlock(ReadWriteLock lock, TransactionType type) {
+ switch (type) {
+ case READ:
+ lock.readLock().unlock();
+ return;
+ case WRITE:
+ lock.writeLock().unlock();
+ return;
+ }
+ }
+
+ @Override
+ public Transaction startTransaction(IDiagram d, TransactionType type) {
+ ReadWriteLock lock = d.getHint(TRANSACTION_LOCK);
+ assert lock != null;
+ lock(lock, type);
+ try {
+ Transaction t = new Transaction.Impl(type);
+ ListenerList<TransactionListener> ll = d.getHint(TRANSACTION_LISTENERS);
+ if (ll != null)
+ for (TransactionListener l : ll.getListeners())
+ l.transactionStarted(d, t);
+ return t;
+ } catch (RuntimeException e) {
+ unlock(lock, type);
+ throw e;
+ }
+ }
+
+ @Override
+ public void finishTransaction(IDiagram d, Transaction t) {
+ ReadWriteLock lock = d.getHint(TRANSACTION_LOCK);
+ assert lock != null;
+ try {
+ ListenerList<TransactionListener> ll = d.getHint(TRANSACTION_LISTENERS);
+ if (ll != null)
+ for (TransactionListener l : ll.getListeners())
+ l.transactionFinished(d, t);
+ } finally {
+ unlock(lock, t.getType());
+ }
+ }
+
+}