]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/typicals/rules/NameRule.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / typicals / rules / NameRule.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012 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.modeling.typicals.rules;\r
13 \r
14 import org.simantics.db.ReadGraph;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.WriteGraph;\r
17 import org.simantics.db.exception.DatabaseException;\r
18 import org.simantics.modeling.ModelingResources;\r
19 import org.simantics.modeling.typicals.ITypicalSynchronizationRule;\r
20 import org.simantics.modeling.typicals.TypicalInfo;\r
21 import org.simantics.modeling.typicals.TypicalUtil;\r
22 import org.simantics.scl.runtime.function.Function4;\r
23 \r
24 /**\r
25  * Re-evaluates the name of the typical instance component corresponding to the\r
26  * specified element. If the template name is not parameterized or the evaluated\r
27  * name already matches the instance component name, the rule does nothing.\r
28  * Otherwise it resets the instance component name.\r
29  * \r
30  * @author Tuukka Lehtonen\r
31  */\r
32 public enum NameRule implements ITypicalSynchronizationRule {\r
33 \r
34     INSTANCE;\r
35 \r
36     public static NameRule getInstance() {\r
37         return INSTANCE;\r
38     }\r
39 \r
40     @Override\r
41     public boolean synchronize(WriteGraph graph, Resource templateElement, Resource instanceElement, TypicalInfo info) throws DatabaseException {\r
42         \r
43         Function4<ReadGraph, Resource, Resource, String, String> namingFunction = info.bean.getAux(AuxKeys.KEY_TYPICAL_NAMING_FUNCTION);\r
44         if (namingFunction == null)\r
45             return false;\r
46 \r
47         ModelingResources MOD = ModelingResources.getInstance(graph);\r
48         Resource instanceComponent = graph.getPossibleObject(instanceElement, MOD.ElementToComponent);\r
49         if (instanceComponent == null)\r
50             return false;\r
51 \r
52         boolean result = TypicalUtil.applyTypicalModuleName(graph, instanceComponent, namingFunction, null); \r
53         if(result) info.messageLog.add("\t\t\tname");\r
54         \r
55         return result;\r
56         \r
57     }\r
58 \r
59 }\r