X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Fdiagram%2Frenaming%2FComponentsRenamingModel.java;h=da861d816b5eff552c50c635da1d7c88c3d88ef5;hb=95144e3b879f0a10715927027bb9a7aa8e05bdeb;hp=23d2ffa83d723de2f76642d06d1fdbdb3d3ffb30;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/renaming/ComponentsRenamingModel.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/renaming/ComponentsRenamingModel.java index 23d2ffa83..da861d816 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/renaming/ComponentsRenamingModel.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/renaming/ComponentsRenamingModel.java @@ -1,133 +1,141 @@ -package org.simantics.modeling.ui.diagram.renaming; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -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 entries = new ArrayList(); - public String oldNamePrefix; - public String newNamePrefix; - public boolean reset; - public Function1 prefixValidator; - - private Session session; - private Variable compositeVariable; - private Resource configuration; - private ComponentNamingStrategy namingStrategy; - - public void 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.getPossibleRelatedValue(componentType, L0X.HasGeneratedNamePrefix, Bindings.STRING) - : ""; - 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); - } - - 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)); - } - } 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 propositions = new ArrayList(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)) - g.claimLiteral(entry.resource, L0.HasName, entry.newName, Bindings.STRING); - if(!oldNamePrefix.equals(newNamePrefix)) - compositeVariable.setPropertyValue(g, L0X.HasGeneratedNamePrefix, newNamePrefix, Bindings.STRING); - } -} +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 entries = new ArrayList(); + public Set selectedEntries = new HashSet<>(); + public String oldNamePrefix; + public String newNamePrefix; + public boolean reset; + public Function1 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.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 propositions = new ArrayList(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); + } +}