1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.mapping;
16 import org.simantics.Simantics;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.Resource;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.util.Layer0Utils;
22 import org.simantics.diagram.synchronization.ISynchronizationContext;
23 import org.simantics.diagram.synchronization.SynchronizationException;
24 import org.simantics.diagram.synchronization.SynchronizationHints;
25 import org.simantics.diagram.synchronization.graph.BasicResources;
26 import org.simantics.diagram.synchronization.graph.CopyAdvisorUtil;
27 import org.simantics.diagram.synchronization.graph.GraphCopyAdvisor;
28 import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
29 import org.simantics.layer0.Layer0;
30 import org.simantics.modeling.ComponentUtils;
31 import org.simantics.modeling.ModelingUtils;
32 import org.simantics.modeling.services.ComponentNamingUtil;
33 import org.simantics.modeling.services.NamingException;
34 import org.simantics.project.IProject;
35 import org.simantics.structural.stubs.StructuralResource2;
37 import gnu.trove.map.hash.THashMap;
40 * @author Tuukka Lehtonen
42 public class ComponentCopyAdvisor extends GraphCopyAdvisor {
45 public Evaluation canCopy(ISynchronizationContext context, WriteGraph graph, Resource source,
46 Resource sourceContainer, Resource targetContainer) throws DatabaseException {
47 BasicResources br = context.get(GraphSynchronizationHints.BASIC_RESOURCES);
48 return (graph.isInstanceOf(source, br.STR.Component) || graph.isInstanceOf(source, br.STR.Connection))
49 ? Evaluation.SUPPORTED : Evaluation.NOT_SUPPORTED;
53 public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
54 Resource targetContainer) throws DatabaseException {
55 return copy(context, graph, source, sourceContainer, targetContainer, new THashMap<Object, Object>());
59 public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
60 Resource targetContainer, Map<Object, Object> map) throws DatabaseException {
61 StructuralResource2 STR = StructuralResource2.getInstance(graph);
62 Resource copy = CopyAdvisorUtil.copy2(graph, source, null, map);
64 Layer0 L0 = Layer0.getInstance(graph);
65 if (graph.hasStatement(sourceContainer, L0.ConsistsOf, source))
66 graph.claim(targetContainer, L0.ConsistsOf, copy);
68 if (ModelingUtils.needsIdentifier(graph, source))
69 Layer0Utils.claimNewIdentifier(graph, copy, false);
70 if (context.get(SynchronizationHints.NO_RENAME) == null)
71 renameComponent(context, graph, source, copy, sourceContainer, targetContainer);
75 public static String renameComponent(ISynchronizationContext context, WriteGraph graph, Resource source,
76 Resource copy, Resource sourceContainer, Resource targetContainer) throws DatabaseException {
77 return renameComponent(context, graph, source, copy, sourceContainer, targetContainer,
78 Layer0.getInstance(graph).HasName);
81 public static String renameComponent(ISynchronizationContext context, WriteGraph graph, Resource source,
82 Resource copy, Resource sourceContainer, Resource targetContainer, Resource nameProperty)
83 throws DatabaseException {
84 BasicResources br = context.get(GraphSynchronizationHints.BASIC_RESOURCES);
86 Resource sourceComponentType = graph.getPossibleType(source, br.STR.Component);
87 if (sourceComponentType == null)
90 // Try to obtain a ComponentNamingStrategy for renaming the copied object.
91 IProject project = context.get(SynchronizationHints.PROJECT);
93 project = Simantics.getProject();
95 // Try to give a valid or at least unique name for the new component.
96 //Resource configurationRoot = ComponentUtils.getComponentConfigurationRoot(graph, copy);
97 //Resource composite = ComponentUtils.tryGetComponentContainer(graph, copy);
98 Resource configurationRoot = ComponentUtils.getCompositeConfigurationRoot(graph, targetContainer);
99 Resource composite = targetContainer;
101 if (configurationRoot != null) {
103 String name = ComponentNamingUtil.findFreshInstanceName(graph, project, configurationRoot, composite, sourceComponentType);
104 graph.claimLiteral(copy, nameProperty, name, Bindings.STRING);
106 } catch (NamingException e) {
107 // This will produce duplicate names in the model,
108 // should not happen if we don't run out of names.
109 throw new SynchronizationException(e);
116 public Object cut(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer,
117 Resource targetContainer) throws DatabaseException {
118 Layer0 L0 = Layer0.getInstance(graph);
120 if (sourceContainer.equals(targetContainer))
123 // This handles e.g. connections, which are not part of container
124 if(graph.hasStatement(sourceContainer, L0.ConsistsOf, source)) {
125 graph.deny(sourceContainer, L0.ConsistsOf, source);
126 graph.claim(targetContainer, L0.ConsistsOf, source);