]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/GraphSynchronizationContext.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / synchronization / graph / GraphSynchronizationContext.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.diagram.synchronization.graph;
13
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.WriteGraph;
16 import org.simantics.diagram.internal.DebugPolicy;
17 import org.simantics.diagram.synchronization.IModifiableSynchronizationContext;
18 import org.simantics.diagram.synchronization.IModificationQueue;
19 import org.simantics.diagram.synchronization.SynchronizationHints;
20 import org.simantics.utils.datastructures.hints.HintContext;
21 import org.simantics.utils.datastructures.hints.IHintContext;
22 import org.simantics.utils.datastructures.hints.IHintContext.Key;
23
24 /**
25  * @author Tuukka Lehtonen
26  */
27 public class GraphSynchronizationContext implements IModifiableSynchronizationContext {
28
29     private final IHintContext ctx = new HintContext();
30
31     public GraphSynchronizationContext(ReadGraph graph, IModificationQueue modificationQueue) {
32         ctx.setHint(GraphSynchronizationHints.SESSION, graph.getSession());
33         ctx.setHint(GraphSynchronizationHints.BASIC_RESOURCES, BasicResources.getInstance(graph));
34         ctx.setHint(SynchronizationHints.MODIFICATION_QUEUE, modificationQueue);
35     }
36
37     public static GraphSynchronizationContext getWriteInstance(WriteGraph graph, IModificationQueue modificationQueue) {
38         GraphSynchronizationContext ctx = new GraphSynchronizationContext(graph, modificationQueue);
39         ctx.ctx.setHint(GraphSynchronizationHints.READ_TRANSACTION, graph);
40         ctx.ctx.setHint(GraphSynchronizationHints.WRITE_TRANSACTION, graph);
41         return ctx;
42     }
43
44     @Override
45     public <T> T get(Key key) {
46         return ctx.getHint(key);
47     }
48
49     @Override
50     public <T> T set(Key key, Object value) {
51         if (DebugPolicy.DEBUG_SYNC_CONTEXT)
52             System.out.println(this + ".set(" + key + "): " + value);
53
54         if (value == null)
55             return ctx.removeHint(key);
56
57         T t = ctx.getHint(key);
58         ctx.setHint(key, value);
59         return t;
60     }
61
62 }