]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/renaming/ComponentsRenamingModel.java
23d2ffa83d723de2f76642d06d1fdbdb3d3ffb30
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagram / renaming / ComponentsRenamingModel.java
1 package org.simantics.modeling.ui.diagram.renaming;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collections;\r
5 import java.util.List;\r
6 \r
7 import org.eclipse.core.runtime.IStatus;\r
8 import org.eclipse.core.runtime.Status;\r
9 import org.simantics.databoard.Bindings;\r
10 import org.simantics.db.ReadGraph;\r
11 import org.simantics.db.Resource;\r
12 import org.simantics.db.Session;\r
13 import org.simantics.db.WriteGraph;\r
14 import org.simantics.db.common.request.ReadRequest;\r
15 import org.simantics.db.exception.DatabaseException;\r
16 import org.simantics.db.layer0.request.Configuration;\r
17 import org.simantics.db.layer0.variable.Variable;\r
18 import org.simantics.db.layer0.variable.Variables;\r
19 import org.simantics.layer0.Layer0;\r
20 import org.simantics.modeling.services.ComponentNamingStrategy;\r
21 import org.simantics.modeling.services.ComponentNamingUtil;\r
22 import org.simantics.modeling.services.NamingException;\r
23 import org.simantics.modeling.ui.Activator;\r
24 import org.simantics.operation.Layer0X;\r
25 import org.simantics.scl.runtime.function.Function1;\r
26 import org.simantics.structural.stubs.StructuralResource2;\r
27 \r
28 /**\r
29  * @author Hannu Niemistö\r
30  * @author Tuukka Lehtonen\r
31  */\r
32 public class ComponentsRenamingModel {\r
33     public ArrayList<NameEntry> entries = new ArrayList<NameEntry>();\r
34     public String oldNamePrefix;\r
35     public String newNamePrefix;\r
36     public boolean reset;\r
37     public Function1<String, String> prefixValidator;\r
38 \r
39     private Session session;\r
40     private Variable compositeVariable;\r
41     private Resource configuration;\r
42     private ComponentNamingStrategy namingStrategy;\r
43 \r
44     public void read(ReadGraph g, Resource composite) throws DatabaseException {\r
45         this.session = g.getSession();\r
46         this.compositeVariable = Variables.getVariable(g, composite);\r
47         this.configuration = g.syncRequest(new Configuration(composite));\r
48 \r
49         Layer0 L0 = Layer0.getInstance(g);\r
50         Layer0X L0X = Layer0X.getInstance(g);\r
51         StructuralResource2 STR = StructuralResource2.getInstance(g);\r
52         for(Resource component : g.getObjects(composite, L0.ConsistsOf)) {\r
53             if(!g.isInstanceOf(component, STR.Component))\r
54                 continue;\r
55             String name = g.getRelatedValue(component, L0.HasName);\r
56             Resource componentType = g.getPossibleType(component, STR.Component);\r
57             String componentTypePrefix = componentType != null\r
58                     ? g.<String>getPossibleRelatedValue(componentType, L0X.HasGeneratedNamePrefix, Bindings.STRING)\r
59                     : "";\r
60             entries.add(new NameEntry(component, name, name, componentTypePrefix));\r
61         }\r
62         Collections.sort(entries);\r
63         Variable namePrefixValue = compositeVariable.getProperty(g, L0X.HasGeneratedNamePrefix);\r
64         oldNamePrefix = newNamePrefix = namePrefixValue.getValue(g, Bindings.STRING);\r
65 \r
66         Variable displayValue = namePrefixValue.getPossibleProperty(g, Variables.DISPLAY_VALUE);\r
67         if (displayValue != null)\r
68             prefixValidator = displayValue.getPossiblePropertyValue(g, Variables.INPUT_VALIDATOR);\r
69 \r
70         this.namingStrategy = ComponentNamingUtil.findNamingStrategy(g, null, composite);\r
71     }\r
72 \r
73     public void computeNewNames() {\r
74         final boolean reset = this.reset;\r
75         if (reset) {\r
76             for (NameEntry entry : entries)\r
77                 entry.newName = newNamePrefix + entry.namingPrefix;\r
78         } else {\r
79             for (NameEntry entry : entries)\r
80                 if (entry.oldName.startsWith(oldNamePrefix))\r
81                     entry.newName = newNamePrefix + entry.oldName.substring(oldNamePrefix.length());\r
82         }\r
83 \r
84         if (session != null) {\r
85             try {\r
86                 session.syncRequest(new ReadRequest() {\r
87                     @Override\r
88                     public void run(ReadGraph graph) throws DatabaseException {\r
89                         validateNewNames(graph, !reset);\r
90                     }\r
91                 });\r
92             } catch (DatabaseException e) {\r
93                 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "validateNewNames failed, see exception for details", e));\r
94             }\r
95         } else {\r
96             if (reset) {\r
97                 int i=0;\r
98                 for(NameEntry entry : entries)\r
99                     entry.newName = entry.newName + (++i);\r
100             }\r
101         }\r
102     }\r
103 \r
104     private void validateNewNames(ReadGraph graph, boolean acceptPropositions) throws DatabaseException {\r
105         try {\r
106             if (namingStrategy != null) {\r
107                 List<String> propositions = new ArrayList<String>(entries.size());\r
108                 for (NameEntry entry : entries)\r
109                      propositions.add(entry.newName);\r
110 \r
111                 propositions = namingStrategy.validateInstanceNames(graph, configuration, propositions, acceptPropositions, null);\r
112 \r
113                 for (int i = 0; i < propositions.size(); ++i) {\r
114                     NameEntry entry = entries.get(i);\r
115                     if (!acceptPropositions || !entry.oldName.equals(entry.newName))\r
116                         entry.newName = propositions.get(i);\r
117                 }\r
118             }\r
119         } catch (NamingException e) {\r
120             throw new DatabaseException(e);\r
121         }\r
122     }\r
123 \r
124     public void write(WriteGraph g) throws DatabaseException {\r
125         Layer0 L0 = Layer0.getInstance(g);\r
126         Layer0X L0X = Layer0X.getInstance(g);\r
127         for(NameEntry entry : entries)\r
128             if(!entry.oldName.equals(entry.newName))\r
129                 g.claimLiteral(entry.resource, L0.HasName, entry.newName, Bindings.STRING);\r
130         if(!oldNamePrefix.equals(newNamePrefix))\r
131             compositeVariable.setPropertyValue(g, L0X.HasGeneratedNamePrefix, newNamePrefix, Bindings.STRING);\r
132     }\r
133 }\r