--- /dev/null
+package org.simantics.modeling.ui.diagram.renaming;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.List;\r
+\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.request.Configuration;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.services.ComponentNamingStrategy;\r
+import org.simantics.modeling.services.ComponentNamingUtil;\r
+import org.simantics.modeling.services.NamingException;\r
+import org.simantics.modeling.ui.Activator;\r
+import org.simantics.operation.Layer0X;\r
+import org.simantics.scl.runtime.function.Function1;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+\r
+/**\r
+ * @author Hannu Niemistö\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ComponentsRenamingModel {\r
+ public ArrayList<NameEntry> entries = new ArrayList<NameEntry>();\r
+ public String oldNamePrefix;\r
+ public String newNamePrefix;\r
+ public boolean reset;\r
+ public Function1<String, String> prefixValidator;\r
+\r
+ private Session session;\r
+ private Variable compositeVariable;\r
+ private Resource configuration;\r
+ private ComponentNamingStrategy namingStrategy;\r
+\r
+ public void read(ReadGraph g, Resource composite) throws DatabaseException {\r
+ this.session = g.getSession();\r
+ this.compositeVariable = Variables.getVariable(g, composite);\r
+ this.configuration = g.syncRequest(new Configuration(composite));\r
+\r
+ Layer0 L0 = Layer0.getInstance(g);\r
+ Layer0X L0X = Layer0X.getInstance(g);\r
+ StructuralResource2 STR = StructuralResource2.getInstance(g);\r
+ for(Resource component : g.getObjects(composite, L0.ConsistsOf)) {\r
+ if(!g.isInstanceOf(component, STR.Component))\r
+ continue;\r
+ String name = g.getRelatedValue(component, L0.HasName);\r
+ Resource componentType = g.getPossibleType(component, STR.Component);\r
+ String componentTypePrefix = componentType != null\r
+ ? g.<String>getPossibleRelatedValue(componentType, L0X.HasGeneratedNamePrefix, Bindings.STRING)\r
+ : "";\r
+ entries.add(new NameEntry(component, name, name, componentTypePrefix));\r
+ }\r
+ Collections.sort(entries);\r
+ Variable namePrefixValue = compositeVariable.getProperty(g, L0X.HasGeneratedNamePrefix);\r
+ oldNamePrefix = newNamePrefix = namePrefixValue.getValue(g, Bindings.STRING);\r
+\r
+ Variable displayValue = namePrefixValue.getPossibleProperty(g, Variables.DISPLAY_VALUE);\r
+ if (displayValue != null)\r
+ prefixValidator = displayValue.getPossiblePropertyValue(g, Variables.INPUT_VALIDATOR);\r
+\r
+ this.namingStrategy = ComponentNamingUtil.findNamingStrategy(g, null, composite);\r
+ }\r
+\r
+ public void computeNewNames() {\r
+ final boolean reset = this.reset;\r
+ if (reset) {\r
+ for (NameEntry entry : entries)\r
+ entry.newName = newNamePrefix + entry.namingPrefix;\r
+ } else {\r
+ for (NameEntry entry : entries)\r
+ if (entry.oldName.startsWith(oldNamePrefix))\r
+ entry.newName = newNamePrefix + entry.oldName.substring(oldNamePrefix.length());\r
+ }\r
+\r
+ if (session != null) {\r
+ try {\r
+ session.syncRequest(new ReadRequest() {\r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ validateNewNames(graph, !reset);\r
+ }\r
+ });\r
+ } catch (DatabaseException e) {\r
+ Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "validateNewNames failed, see exception for details", e));\r
+ }\r
+ } else {\r
+ if (reset) {\r
+ int i=0;\r
+ for(NameEntry entry : entries)\r
+ entry.newName = entry.newName + (++i);\r
+ }\r
+ }\r
+ }\r
+\r
+ private void validateNewNames(ReadGraph graph, boolean acceptPropositions) throws DatabaseException {\r
+ try {\r
+ if (namingStrategy != null) {\r
+ List<String> propositions = new ArrayList<String>(entries.size());\r
+ for (NameEntry entry : entries)\r
+ propositions.add(entry.newName);\r
+\r
+ propositions = namingStrategy.validateInstanceNames(graph, configuration, propositions, acceptPropositions, null);\r
+\r
+ for (int i = 0; i < propositions.size(); ++i) {\r
+ NameEntry entry = entries.get(i);\r
+ if (!acceptPropositions || !entry.oldName.equals(entry.newName))\r
+ entry.newName = propositions.get(i);\r
+ }\r
+ }\r
+ } catch (NamingException e) {\r
+ throw new DatabaseException(e);\r
+ }\r
+ }\r
+\r
+ public void write(WriteGraph g) throws DatabaseException {\r
+ Layer0 L0 = Layer0.getInstance(g);\r
+ Layer0X L0X = Layer0X.getInstance(g);\r
+ for(NameEntry entry : entries)\r
+ if(!entry.oldName.equals(entry.newName))\r
+ g.claimLiteral(entry.resource, L0.HasName, entry.newName, Bindings.STRING);\r
+ if(!oldNamePrefix.equals(newNamePrefix))\r
+ compositeVariable.setPropertyValue(g, L0X.HasGeneratedNamePrefix, newNamePrefix, Bindings.STRING);\r
+ }\r
+}\r