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