]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
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.g2d.diagram.handler.impl;\r
13 \r
14 import java.util.Collection;\r
15 import java.util.concurrent.locks.ReadWriteLock;\r
16 import java.util.concurrent.locks.ReentrantReadWriteLock;\r
17 \r
18 import org.simantics.g2d.diagram.IDiagram;\r
19 import org.simantics.g2d.diagram.handler.LifeCycle;\r
20 import org.simantics.g2d.diagram.handler.TransactionContext;\r
21 import org.simantics.g2d.element.IElement;\r
22 import org.simantics.utils.datastructures.ListenerList;\r
23 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
24 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
25 \r
26 /**\r
27  * @author Tuukka Lehtonen\r
28  */\r
29 public class LockingTransactionContext implements TransactionContext, LifeCycle {\r
30 \r
31     public static final LockingTransactionContext INSTANCE = new LockingTransactionContext();\r
32 \r
33     private static final Key TRANSACTION_LISTENERS = new KeyOf(ListenerList.class, "TRANSACTION LISTENERS");\r
34     private static final Key TRANSACTION_LOCK      = new KeyOf(ReadWriteLock.class, "TRANSACTION LOCK");\r
35 \r
36     @Override\r
37     public void onDiagramCreated(IDiagram diagram) {\r
38         diagram.setHint(TRANSACTION_LISTENERS, new ListenerList<TransactionListener>(TransactionListener.class));\r
39         diagram.setHint(TRANSACTION_LOCK, new ReentrantReadWriteLock(true));\r
40     }\r
41 \r
42     @Override\r
43     public void onDiagramLoaded(IDiagram diagram, Collection<IElement> initialElements) {\r
44     }\r
45 \r
46     @Override\r
47     public void onDiagramDestroyed(IDiagram diagram) {\r
48     }\r
49 \r
50     @Override\r
51     public void onDiagramDisposed(IDiagram diagram) {\r
52         diagram.removeHint(TRANSACTION_LISTENERS);\r
53         diagram.removeHint(TRANSACTION_LOCK);\r
54     }\r
55 \r
56     @Override\r
57     public void addTransactionListener(IDiagram d, TransactionListener l) {\r
58         ListenerList<TransactionListener> ll;\r
59         synchronized (d) {\r
60             ll = d.getHint(TRANSACTION_LISTENERS);\r
61             if (ll == null) {\r
62                 ll = new ListenerList<TransactionListener>(TransactionListener.class);\r
63                 d.setHint(TRANSACTION_LISTENERS, ll);\r
64             }\r
65         }\r
66         ll.add(l);\r
67     }\r
68 \r
69     @Override\r
70     public void removeTransactionListener(IDiagram d, TransactionListener l) {\r
71         synchronized (d) {\r
72             ListenerList<TransactionListener> ll = d.getHint(TRANSACTION_LISTENERS);\r
73             if (ll == null)\r
74                 return;\r
75             ll.remove(l);\r
76         }\r
77     }\r
78 \r
79     void lock(ReadWriteLock lock, TransactionType type) {\r
80         switch (type) {\r
81             case READ:\r
82                 lock.readLock().lock();\r
83                 return;\r
84             case WRITE:\r
85                 lock.writeLock().lock();\r
86                 return;\r
87         }\r
88     }\r
89 \r
90     void unlock(ReadWriteLock lock, TransactionType type) {\r
91         switch (type) {\r
92             case READ:\r
93                 lock.readLock().unlock();\r
94                 return;\r
95             case WRITE:\r
96                 lock.writeLock().unlock();\r
97                 return;\r
98         }\r
99     }\r
100 \r
101     @Override\r
102     public Transaction startTransaction(IDiagram d, TransactionType type) {\r
103         ReadWriteLock lock = d.getHint(TRANSACTION_LOCK);\r
104         assert lock != null;\r
105         lock(lock, type);\r
106         try {\r
107             Transaction t = new Transaction.Impl(type);\r
108             ListenerList<TransactionListener> ll = d.getHint(TRANSACTION_LISTENERS);\r
109             if (ll != null)\r
110                 for (TransactionListener l : ll.getListeners())\r
111                     l.transactionStarted(d, t);\r
112             return t;\r
113         } catch (RuntimeException e) {\r
114             unlock(lock, type);\r
115             throw e;\r
116         }\r
117     }\r
118 \r
119     @Override\r
120     public void finishTransaction(IDiagram d, Transaction t) {\r
121         ReadWriteLock lock = d.getHint(TRANSACTION_LOCK);\r
122         assert lock != null;\r
123         try {\r
124             ListenerList<TransactionListener> ll = d.getHint(TRANSACTION_LISTENERS);\r
125             if (ll != null)\r
126                 for (TransactionListener l : ll.getListeners())\r
127                     l.transactionFinished(d, t);\r
128         } finally {\r
129             unlock(lock, t.getType());\r
130         }\r
131     }\r
132 \r
133 }\r