X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fflag%2FFlagLabelModifier.java;fp=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fflag%2FFlagLabelModifier.java;h=778a1eb1481d33f36a6c889557459dc849ef4dcd;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=1d5c7a669f89091ed7e821be2d9b3b9835eb6fa4;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagLabelModifier.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagLabelModifier.java index 1d5c7a669..778a1eb14 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagLabelModifier.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/flag/FlagLabelModifier.java @@ -1,113 +1,113 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.diagram.flag; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; - -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.exception.DatabaseException; -import org.simantics.db.layer0.adapter.AbstractStringModifier; -import org.simantics.diagram.stubs.DiagramResource; -import org.simantics.layer0.Layer0; -import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate; - -/** - * Makes sure that two things apply: - *
    - *
  1. If a flag is joined with another flag, both flags must have the same label property
  2. - *
  3. A flag's label is unique within its own diagram
  4. - *
- * @author Tuukka Lehtonen - */ -public final class FlagLabelModifier extends AbstractStringModifier { - - private Resource ownerFlag; - private Resource relation; - @SuppressWarnings("unused") - private Resource property; - private Collection diagrams = Collections.emptySet(); - private Set originalLabels = new HashSet(); - private Set labelsInUse = Collections.emptySet(); - - public FlagLabelModifier(ReadGraph graph, Resource flag, Resource relation, Resource label) throws DatabaseException { - 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); - } - refreshUsedLabels(graph); - } - - private void addPossibleLabel(ReadGraph graph, Resource flag, Set result) throws DatabaseException { - String label = graph.getPossibleRelatedValue(flag, Layer0.getInstance(graph).HasLabel, Bindings.STRING); - if (label != null && !label.isEmpty()) - result.add(label); - } - - private void refreshUsedLabels(ReadGraph graph) throws DatabaseException { - Set used = new HashSet(); - DiagramResource DIA = DiagramResource.getInstance(graph); - for (Resource diagram : diagrams) { - for (Resource element : graph.syncRequest(new OrderedSet(diagram))) { - if (graph.isInstanceOf(element, DIA.Flag)) { - addPossibleLabel(graph, element, used); - } - } - } - //System.out.println("used labels:" + used); - //System.out.println("original labels:" + originalLabels); - used.removeAll(originalLabels); - - this.labelsInUse = used; - //System.out.println("final used labels:" + used); - } - - @Override - public String isValid(String value) { - if (diagrams.isEmpty() || ownerFlag == null) - // No diagram or flag for property, cannot verify, therefore don't care. - return null; - - if (labelsInUse.contains(value)) - return "Label is already in use on the same diagram"; - return null; - } - - @Override - final public void modify(WriteGraph graph, 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) - graph.claimLiteral(counterpart, relation, DIA.FlagLabel, value, Bindings.STRING); - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.diagram.flag; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +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.exception.DatabaseException; +import org.simantics.db.layer0.adapter.AbstractStringModifier; +import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.layer0.Layer0; +import org.simantics.layer0.utils.binaryPredicates.OrderedSetElementsPredicate; + +/** + * Makes sure that two things apply: + *
    + *
  1. If a flag is joined with another flag, both flags must have the same label property
  2. + *
  3. A flag's label is unique within its own diagram
  4. + *
+ * @author Tuukka Lehtonen + */ +public final class FlagLabelModifier extends AbstractStringModifier { + + private Resource ownerFlag; + private Resource relation; + @SuppressWarnings("unused") + private Resource property; + private Collection diagrams = Collections.emptySet(); + private Set originalLabels = new HashSet(); + private Set labelsInUse = Collections.emptySet(); + + public FlagLabelModifier(ReadGraph graph, Resource flag, Resource relation, Resource label) throws DatabaseException { + 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); + } + refreshUsedLabels(graph); + } + + private void addPossibleLabel(ReadGraph graph, Resource flag, Set result) throws DatabaseException { + String label = graph.getPossibleRelatedValue(flag, Layer0.getInstance(graph).HasLabel, Bindings.STRING); + if (label != null && !label.isEmpty()) + result.add(label); + } + + private void refreshUsedLabels(ReadGraph graph) throws DatabaseException { + Set used = new HashSet(); + DiagramResource DIA = DiagramResource.getInstance(graph); + for (Resource diagram : diagrams) { + for (Resource element : graph.syncRequest(new OrderedSet(diagram))) { + if (graph.isInstanceOf(element, DIA.Flag)) { + addPossibleLabel(graph, element, used); + } + } + } + //System.out.println("used labels:" + used); + //System.out.println("original labels:" + originalLabels); + used.removeAll(originalLabels); + + this.labelsInUse = used; + //System.out.println("final used labels:" + used); + } + + @Override + public String isValid(String value) { + if (diagrams.isEmpty() || ownerFlag == null) + // No diagram or flag for property, cannot verify, therefore don't care. + return null; + + if (labelsInUse.contains(value)) + return "Label is already in use on the same diagram"; + return null; + } + + @Override + final public void modify(WriteGraph graph, 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) + graph.claimLiteral(counterpart, relation, DIA.FlagLabel, value, Bindings.STRING); + } + +}