]> gerrit.simantics Code Review - simantics/platform.git/blob
452be9b333501c561fa4cf6b66935c017f745a2c
[simantics/platform.git] /
1 package org.simantics.modeling.ui.diagram.renaming;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Set;
8
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;
29
30 /**
31  * @author Hannu Niemistö
32  * @author Tuukka Lehtonen
33  */
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;
39     public boolean reset;
40     public Function1<String, String> prefixValidator;
41
42     private Session session;
43     private Variable compositeVariable;
44     private Resource configuration;
45     private ComponentNamingStrategy namingStrategy;
46
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));
51
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))
57                 continue;
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)
62                     : "";
63             entries.add(new NameEntry(component, name, name, componentTypePrefix));
64         }
65         Collections.sort(entries);
66         Variable namePrefixValue = compositeVariable.getProperty(g, L0X.HasGeneratedNamePrefix);
67         oldNamePrefix = newNamePrefix = namePrefixValue.getValue(g, Bindings.STRING);
68
69         Variable displayValue = namePrefixValue.getPossibleProperty(g, Variables.DISPLAY_VALUE);
70         if (displayValue != null)
71             prefixValidator = displayValue.getPossiblePropertyValue(g, Variables.INPUT_VALIDATOR);
72
73         this.namingStrategy = ComponentNamingUtil.findNamingStrategy(g, null, composite);
74
75         // By default, select all entries.
76         this.selectedEntries.addAll(entries);
77
78         return this;
79     }
80
81     public void computeNewNames() {
82         final boolean reset = this.reset;
83         if (reset) {
84             for (NameEntry entry : entries)
85                 entry.newName = newNamePrefix + entry.namingPrefix;
86         } else {
87             for (NameEntry entry : entries)
88                 if (entry.oldName.startsWith(oldNamePrefix))
89                     entry.newName = newNamePrefix + entry.oldName.substring(oldNamePrefix.length());
90         }
91
92         if (session != null) {
93             try {
94                 session.syncRequest(new ReadRequest() {
95                     @Override
96                     public void run(ReadGraph graph) throws DatabaseException {
97                         validateNewNames(graph, !reset);
98                     }
99                 });
100             } catch (DatabaseException e) {
101                 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "validateNewNames failed, see exception for details", e));
102             }
103         } else {
104             if (reset) {
105                 int i=0;
106                 for(NameEntry entry : entries)
107                     entry.newName = entry.newName + (++i);
108             }
109         }
110     }
111
112     private void validateNewNames(ReadGraph graph, boolean acceptPropositions) throws DatabaseException {
113         try {
114             if (namingStrategy != null) {
115                 List<String> propositions = new ArrayList<String>(entries.size());
116                 for (NameEntry entry : entries)
117                      propositions.add(entry.newName);
118
119                 propositions = namingStrategy.validateInstanceNames(graph, configuration, propositions, acceptPropositions, null);
120
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);
125                 }
126             }
127         } catch (NamingException e) {
128             throw new DatabaseException(e);
129         }
130     }
131
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);
140     }
141 }