1 package org.simantics.modeling.ui.diagram.renaming;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashSet;
9 import org.eclipse.core.runtime.IStatus;
10 import org.eclipse.core.runtime.Status;
11 import org.simantics.databoard.Bindings;
12 import org.simantics.db.ReadGraph;
13 import org.simantics.db.Resource;
14 import org.simantics.db.Session;
15 import org.simantics.db.WriteGraph;
16 import org.simantics.db.common.request.ReadRequest;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.request.Configuration;
19 import org.simantics.db.layer0.variable.Variable;
20 import org.simantics.db.layer0.variable.Variables;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.modeling.services.ComponentNamingStrategy;
23 import org.simantics.modeling.services.ComponentNamingUtil;
24 import org.simantics.modeling.services.NamingException;
25 import org.simantics.modeling.ui.Activator;
26 import org.simantics.operation.Layer0X;
27 import org.simantics.scl.runtime.function.Function1;
28 import org.simantics.structural.stubs.StructuralResource2;
31 * @author Hannu Niemistö
32 * @author Tuukka Lehtonen
34 public class ComponentsRenamingModel {
35 public ArrayList<NameEntry> entries = new ArrayList<NameEntry>();
36 public Set<NameEntry> selectedEntries = new HashSet<>();
37 public String oldNamePrefix;
38 public String newNamePrefix;
40 public Function1<String, String> prefixValidator;
42 private Session session;
43 private Variable compositeVariable;
44 private Resource configuration;
45 private ComponentNamingStrategy namingStrategy;
47 public ComponentsRenamingModel read(ReadGraph g, Resource composite) throws DatabaseException {
48 this.session = g.getSession();
49 this.compositeVariable = Variables.getVariable(g, composite);
50 this.configuration = g.syncRequest(new Configuration(composite));
52 Layer0 L0 = Layer0.getInstance(g);
53 Layer0X L0X = Layer0X.getInstance(g);
54 StructuralResource2 STR = StructuralResource2.getInstance(g);
55 for(Resource component : g.getObjects(composite, L0.ConsistsOf)) {
56 if(!g.isInstanceOf(component, STR.Component))
58 String name = g.getRelatedValue(component, L0.HasName);
59 Resource componentType = g.getPossibleType(component, STR.Component);
60 String componentTypePrefix = componentType != null
61 ? g.<String>getPossibleRelatedValue(componentType, L0X.HasGeneratedNamePrefix, Bindings.STRING)
63 entries.add(new NameEntry(component, name, name, componentTypePrefix));
65 Collections.sort(entries);
66 Variable namePrefixValue = compositeVariable.getProperty(g, L0X.HasGeneratedNamePrefix);
67 oldNamePrefix = newNamePrefix = namePrefixValue.getValue(g, Bindings.STRING);
69 Variable displayValue = namePrefixValue.getPossibleProperty(g, Variables.DISPLAY_VALUE);
70 if (displayValue != null)
71 prefixValidator = displayValue.getPossiblePropertyValue(g, Variables.INPUT_VALIDATOR);
73 this.namingStrategy = ComponentNamingUtil.findNamingStrategy(g, null, composite);
75 // By default, select all entries.
76 this.selectedEntries.addAll(entries);
81 public void computeNewNames() {
82 final boolean reset = this.reset;
84 for (NameEntry entry : entries)
85 entry.newName = newNamePrefix + entry.namingPrefix;
87 for (NameEntry entry : entries)
88 if (entry.oldName.startsWith(oldNamePrefix))
89 entry.newName = newNamePrefix + entry.oldName.substring(oldNamePrefix.length());
92 if (session != null) {
94 session.syncRequest(new ReadRequest() {
96 public void run(ReadGraph graph) throws DatabaseException {
97 validateNewNames(graph, !reset);
100 } catch (DatabaseException e) {
101 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "validateNewNames failed, see exception for details", e));
106 for(NameEntry entry : entries)
107 entry.newName = entry.newName + (++i);
112 private void validateNewNames(ReadGraph graph, boolean acceptPropositions) throws DatabaseException {
114 if (namingStrategy != null) {
115 List<String> propositions = new ArrayList<String>(entries.size());
116 for (NameEntry entry : entries)
117 propositions.add(entry.newName);
119 propositions = namingStrategy.validateInstanceNames(graph, configuration, propositions, acceptPropositions, null);
121 for (int i = 0; i < propositions.size(); ++i) {
122 NameEntry entry = entries.get(i);
123 if (!acceptPropositions || !entry.oldName.equals(entry.newName))
124 entry.newName = propositions.get(i);
127 } catch (NamingException e) {
128 throw new DatabaseException(e);
132 public void write(WriteGraph g) throws DatabaseException {
133 Layer0 L0 = Layer0.getInstance(g);
134 Layer0X L0X = Layer0X.getInstance(g);
135 for(NameEntry entry : entries)
136 if(!entry.oldName.equals(entry.newName) && selectedEntries.contains(entry))
137 g.claimLiteral(entry.resource, L0.HasName, entry.newName, Bindings.STRING);
138 if(!oldNamePrefix.equals(newNamePrefix))
139 compositeVariable.setPropertyValue(g, L0X.HasGeneratedNamePrefix, newNamePrefix, Bindings.STRING);