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