]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
d42556ef35a4d8b60c00687b3d661e09d269f211
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 Association for Decentralized Information Management in\r
3  * 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.sysdyn.ui.editor.participant;\r
13 \r
14 import java.util.Map;\r
15 \r
16 import org.simantics.databoard.Bindings;\r
17 import org.simantics.db.ReadGraph;\r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.Statement;\r
20 import org.simantics.db.WriteGraph;\r
21 import org.simantics.db.common.utils.NameUtils;\r
22 import org.simantics.db.exception.DatabaseException;\r
23 import org.simantics.diagram.synchronization.ISynchronizationContext;\r
24 import org.simantics.diagram.synchronization.graph.CopyAdvisorUtil;\r
25 import org.simantics.layer0.Layer0;\r
26 import org.simantics.modeling.ComponentUtils;\r
27 import org.simantics.modeling.mapping.ComponentCopyAdvisor;\r
28 import org.simantics.structural.stubs.StructuralResource2;\r
29 import org.simantics.utils.datastructures.BinaryFunction;\r
30 \r
31 public class SysdynComponentCopyAdvisor extends ComponentCopyAdvisor {\r
32 \r
33     @Override\r
34     public Object copy(ISynchronizationContext context, WriteGraph graph, Resource source, Resource sourceContainer, Resource targetContainer, Map<Object, Object> map) throws DatabaseException {\r
35         \r
36         BinaryFunction<Boolean, ReadGraph, Statement> tester = new BinaryFunction<Boolean, ReadGraph, Statement>() {\r
37 \r
38                         @Override\r
39                         public Boolean call(ReadGraph graph, Statement statement) {\r
40                                 /*\r
41                                 SysdynResource sr = SysdynResource.getInstance(graph);\r
42                                 try {\r
43                                         if(graph.isInstanceOf(statement.getSubject(), sr.IndependentVariable)) {\r
44                                                 if(statement.getPredicate().equals(sr.HasExpressions) ||\r
45                                                                 statement.getPredicate().equals(sr.HasArrayIndexes) )\r
46                                                         return false;\r
47                                         }\r
48                                 } catch (ServiceException e) {\r
49                                         e.printStackTrace();\r
50                                 }\r
51                                 */\r
52                                 return false;\r
53                         }\r
54                 \r
55         };\r
56         \r
57         Resource copy;\r
58         \r
59         StructuralResource2 sr2 = StructuralResource2.getInstance(graph);\r
60         if(graph.isInstanceOf(source, sr2.Connection)) {\r
61                  copy = CopyAdvisorUtil.copy(graph, source, tester, map);\r
62         } else {\r
63             copy = CopyAdvisorUtil.copy4(graph, source);\r
64         }\r
65 \r
66         \r
67         rename(context, graph, source, copy, sourceContainer, targetContainer);\r
68         return copy;\r
69     }\r
70     \r
71 \r
72     public String rename(ISynchronizationContext context, WriteGraph graph, Resource source,\r
73             Resource copy, Resource sourceContainer, Resource targetContainer) throws DatabaseException {\r
74         Layer0 l0 = Layer0.getInstance(graph);\r
75         String copyName = NameUtils.getSafeName(graph, copy);\r
76         Resource configurationRoot = ComponentUtils.getCompositeConfigurationRoot(graph, targetContainer);\r
77         String name =  NameUtils.findFreshName(graph, copyName, configurationRoot, l0.ConsistsOf, "%s%d");\r
78         graph.claimLiteral(copy, l0.HasName, name, Bindings.STRING);\r
79         return name;\r
80     }\r
81 }\r