]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/AssociativeCopyAdvisor.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / synchronization / graph / AssociativeCopyAdvisor.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 java.util.Map;
15
16 import org.simantics.db.Resource;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.diagram.synchronization.CopyAdvisor;
20 import org.simantics.diagram.synchronization.ISynchronizationContext;
21 import org.simantics.layer0.utils.direct.GraphUtils.ResourceTester;
22
23 /**
24  * @author Tuukka Lehtonen
25  */
26 public class AssociativeCopyAdvisor extends GraphCopyAdvisor {
27
28     Map<ResourceTester, CopyAdvisor> advisors;
29
30     public AssociativeCopyAdvisor(Map<ResourceTester, CopyAdvisor> advisors) {
31         this.advisors = advisors;
32     }
33
34     @Override
35     public Evaluation canCopy(ISynchronizationContext context, WriteGraph graph, Resource source,
36             Resource sourceContainer, Resource targetContainer) throws DatabaseException {
37         for (Map.Entry<ResourceTester, CopyAdvisor> entry : advisors.entrySet()) {
38             if (!entry.getKey().test(graph, source))
39                 continue;
40             Evaluation eval = entry.getValue().canCopy(context, source, sourceContainer, targetContainer);
41             if (eval == Evaluation.SUPPORTED)
42                 return eval;
43         }
44         return Evaluation.NOT_SUPPORTED;
45     }
46
47     @Override
48     public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
49             Resource targetContainer) throws DatabaseException {
50         for (Map.Entry<ResourceTester, CopyAdvisor> entry : advisors.entrySet()) {
51             if (!entry.getKey().test(graph, source))
52                 continue;
53             Object copy = entry.getValue().copy(context, source, sourceContainer, targetContainer);
54             if (copy != null)
55                 return copy;
56         }
57         return null;
58     }
59
60     @Override
61     public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
62             Resource targetContainer, Map<Object, Object> map) throws DatabaseException {
63         for (Map.Entry<ResourceTester, CopyAdvisor> entry : advisors.entrySet()) {
64             if (!entry.getKey().test(graph, source))
65                 continue;
66             Object copy = entry.getValue().copy(context, source, sourceContainer, targetContainer, map);
67             if (copy != null)
68                 return copy;
69         }
70         return null;
71     }
72
73     @Override
74     public Object cut(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
75             Resource targetContainer) throws DatabaseException {
76         for (Map.Entry<ResourceTester, CopyAdvisor> entry : advisors.entrySet()) {
77             if (!entry.getKey().test(graph, source))
78                 continue;
79             Object cut = entry.getValue().cut(context, source, sourceContainer, targetContainer);
80             if (cut != null)
81                 return cut;
82         }
83         return null;
84     }
85
86 }