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