Commit cec904e6 authored by Tuukka Lehtonen's avatar Tuukka Lehtonen
Browse files

Fix flag label editing and validation regression

Now flag HasLabel property editing in the selection view will again:
* validate that the given label is not used by other flag in the same
  diagram
* set the same label for all other flags joined to the edited flag

closes #745


(cherry picked from commit 03a24f86)
parent 94325ddd
......@@ -14,8 +14,12 @@ DIA.FlagType <T L0.Property
DIA.FlagType.InputFlag : DIA.FlagType
DIA.FlagType.OutputFlag : DIA.FlagType
DIA.FlagLabel <T L0.String
DIA.FlagLabel <T L0.String <T SEL.GenericParameterType
L0.HasDescription "This specialized label type exists because we need to attach a custom StringModifier to it."
@L0.assert SEL.HasDisplayValue
_ : SEL.DisplayValue
L0.valueAccessor DIA.Functions.flagLabelValueAccessor : L0.ExternalValue
L0.Literal.HasInputValidator DIA.Functions.flagLabelValidator : L0.Function
DIA.Flag <T DIA.DefinedElement
L0.HasDescription "Flags are used on diagrams for cutting connections and continuing them elsewhere."
......
......@@ -882,6 +882,7 @@
<externalClass className="org.simantics.db.Resource"/>
<externalClass className="org.simantics.db.ReadGraph"/>
<externalClass className="org.simantics.db.layer0.variable.Variable"/>
<externalClass className="org.simantics.db.layer0.variable.ValueAccessor"/>
<class
className="org.simantics.diagram.function.All">
</class>
......
......@@ -20,10 +20,11 @@ import org.simantics.databoard.Bindings;
import org.simantics.db.ReadGraph;
import org.simantics.db.Resource;
import org.simantics.db.WriteGraph;
import org.simantics.db.common.primitiverequest.OrderedSet;
import org.simantics.db.common.request.ObjectsWithType;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.layer0.adapter.AbstractStringModifier;
import org.simantics.diagram.stubs.DiagramResource;
import org.simantics.diagram.synchronization.graph.BasicResources;
import org.simantics.layer0.Layer0;
import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate;
......@@ -46,21 +47,21 @@ public final class FlagLabelModifier extends AbstractStringModifier {
private Set<String> labelsInUse = Collections.emptySet();
public FlagLabelModifier(ReadGraph graph, Resource flag, Resource relation, Resource label) throws DatabaseException {
super(label);
super(label);
this.ownerFlag = flag;
this.relation = relation;
this.property = label;
initialize(graph);
}
private void initialize(ReadGraph graph) throws DatabaseException {
if (ownerFlag != null) {
diagrams = OrderedSetElementsPredicate.INSTANCE.getSubjects(graph, ownerFlag);
Resource correspondence = FlagUtil.getPossibleCounterpart(graph, ownerFlag);
addPossibleLabel(graph, ownerFlag, originalLabels);
if (correspondence != null)
addPossibleLabel(graph, correspondence, originalLabels);
FlagUtil.forCounterparts(graph, ownerFlag, counterpart -> {
addPossibleLabel(graph, counterpart, originalLabels);
});
}
refreshUsedLabels(graph);
}
......@@ -73,12 +74,10 @@ public final class FlagLabelModifier extends AbstractStringModifier {
private void refreshUsedLabels(ReadGraph graph) throws DatabaseException {
Set<String> used = new HashSet<String>();
DiagramResource DIA = DiagramResource.getInstance(graph);
BasicResources BR = BasicResources.getInstance(graph);
for (Resource diagram : diagrams) {
for (Resource element : graph.syncRequest(new OrderedSet(diagram))) {
if (graph.isInstanceOf(element, DIA.Flag)) {
addPossibleLabel(graph, element, used);
}
for (Resource element : graph.syncRequest(new ObjectsWithType(diagram, BR.L0.ConsistsOf, BR.DIA.Flag))) {
addPossibleLabel(graph, element, used);
}
}
//System.out.println("used labels:" + used);
......@@ -102,12 +101,16 @@ public final class FlagLabelModifier extends AbstractStringModifier {
@Override
final public void modify(WriteGraph graph, String value) throws DatabaseException {
modify(graph, ownerFlag, relation, value);
}
public static void modify(WriteGraph graph, Resource ownerFlag, Resource relation, String value) throws DatabaseException {
DiagramResource DIA = DiagramResource.getInstance(graph);
graph.claimLiteral(ownerFlag, relation, DIA.FlagLabel, value, Bindings.STRING);
Resource counterpart = FlagUtil.getPossibleCounterpart(graph, ownerFlag);
if (counterpart != null)
FlagUtil.forCounterparts(graph, ownerFlag, counterpart -> {
graph.claimLiteral(counterpart, relation, DIA.FlagLabel, value, Bindings.STRING);
});
}
}
......@@ -15,6 +15,7 @@ import org.simantics.Simantics;
import org.simantics.browsing.ui.NodeContext;
import org.simantics.browsing.ui.content.Labeler.DialogModifier;
import org.simantics.databoard.Bindings;
import org.simantics.databoard.binding.Binding;
import org.simantics.db.ReadGraph;
import org.simantics.db.Resource;
import org.simantics.db.WriteGraph;
......@@ -24,9 +25,12 @@ import org.simantics.db.common.request.WriteRequest;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.layer0.exception.MissingVariableException;
import org.simantics.db.layer0.request.PossibleConfiguration;
import org.simantics.db.layer0.variable.ValueAccessor;
import org.simantics.db.layer0.variable.ValueAccessorWithBinding;
import org.simantics.db.layer0.variable.Variable;
import org.simantics.db.layer0.variable.Variables;
import org.simantics.diagram.content.ElementContext;
import org.simantics.diagram.flag.FlagLabelModifier;
import org.simantics.diagram.stubs.DiagramResource;
import org.simantics.diagram.stubs.G2DResource;
import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
......@@ -37,6 +41,7 @@ import org.simantics.modeling.template2d.ontology.Template2dResource;
import org.simantics.scenegraph.loader.SceneGraphContext;
import org.simantics.scenegraph.loader.ScenegraphLoaderUtils;
import org.simantics.scl.reflection.annotations.SCLValue;
import org.simantics.scl.runtime.function.Function1;
import org.simantics.ui.fonts.Fonts;
import org.simantics.utils.ui.AdaptionUtils;
import org.simantics.utils.ui.ErrorLogger;
......@@ -307,5 +312,37 @@ public class All {
};
}
@SCLValue(type = "ReadGraph -> Resource -> Variable -> b")
public static Function1<String, String> flagLabelValidator(ReadGraph graph, Resource r, Variable context) throws DatabaseException {
// context is .../flag#HasLabel#HasDisplayValue#HasInputValidator
Variable hasLabel = context.getParent(graph).getParent(graph);
Resource value = hasLabel.getRepresents(graph);
Resource flag = hasLabel.getParent(graph).getRepresents(graph);
FlagLabelModifier flm = new FlagLabelModifier(graph, flag, Layer0.getInstance(graph).HasLabel, value);
return s -> flm.isValid(s);
}
static class FlagLabelValueAccessor extends ValueAccessorWithBinding {
@Override
public Binding getBinding() {
return Bindings.STRING;
}
@Override
public Object getValue(ReadGraph graph, Variable context) throws DatabaseException {
// context is .../flag#HasLabel#HasDisplayValue
return context.getParent(graph).getValue(graph, Bindings.STRING);
}
@Override
public void setValue(WriteGraph graph, Variable context, Object value) throws DatabaseException {
// context is .../flag#HasLabel#HasDisplayValue
Variable hasLabel = context.getParent(graph);
Resource flag = hasLabel.getParent(graph).getRepresents(graph);
FlagLabelModifier.modify(graph, flag, Layer0.getInstance(graph).HasLabel, (String) value);
}
}
@SCLValue(type = "ValueAccessor")
public static final ValueAccessor flagLabelValueAccessor = new FlagLabelValueAccessor();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment