X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.graph.impl%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fgraph%2Fimpl%2FEnumerationVariableModifier3.java;fp=bundles%2Forg.simantics.browsing.ui.graph.impl%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fgraph%2Fimpl%2FEnumerationVariableModifier3.java;h=e20557353698699c79affc282749cf23de202dae;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/EnumerationVariableModifier3.java b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/EnumerationVariableModifier3.java new file mode 100644 index 000000000..e20557353 --- /dev/null +++ b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/EnumerationVariableModifier3.java @@ -0,0 +1,124 @@ +/******************************************************************************* + * 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.browsing.ui.graph.impl; + +import java.util.List; + +import org.simantics.browsing.ui.content.Labeler.EnumerationModifier; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.RequestProcessor; +import org.simantics.db.Session; +import org.simantics.db.VirtualGraph; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; +import org.simantics.db.request.Read; +import org.simantics.utils.datastructures.Callback; +import org.simantics.utils.ui.ErrorLogger; + +/** + * @author Tuukka Lehtonen + */ +public class EnumerationVariableModifier3 implements EnumerationModifier { + + protected final Session session; + protected final Variable variable; + protected final List allowedValues; + protected final String defaultValue; + + protected Throwable modifierFailed; + + public EnumerationVariableModifier3(RequestProcessor processor, Variable variable, List allowedValues) { + + this.session = processor.getSession(); + this.variable = variable; + this.allowedValues = allowedValues; + this.defaultValue = computeDefaultValue(processor, variable); + //System.err.println(this.defaultValue); + + } + + protected String computeDefaultValue(RequestProcessor processor, final Variable variable) { + try { + return processor.syncRequest(new Read() { + @Override + public String perform(ReadGraph graph) throws DatabaseException { + // System.err.println(variable.getURI(graph)); + return variable.getValue(graph);//variable.getPossiblePropertyValue(graph, Variables.LABEL); + } + }); + } catch (DatabaseException e) { + return ""; + } + } + + public class Write extends WriteRequest { + + final private Variable variable; + final private String label; + + public Write(Variable variable, String label) { + super((VirtualGraph)null); + this.variable = variable; + this.label = label; + } + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + variable.setValue(graph, label, Bindings.STRING); + } + + } + + protected void doModify(final String label) { + session.asyncRequest(new Write(variable, label), + new Callback() { + @Override + public void run(DatabaseException parameter) { + if (parameter != null) + ErrorLogger.defaultLogError(parameter); + } + }); + } + + @Override + public String getValue() { + return defaultValue; + } + + @Override + public String isValid(String label) { + if (modifierFailed != null) + return "Could not resolve validator for this value, modification denied. Reason: " + + modifierFailed.getMessage(); + // Validity should already be enforced by the editing UI for + // enumerations. + return null; + } + + @Override + public final void modify(String label) { + if (modifierFailed != null) + // Should never end up here, isValid should prevent it. + throw new Error("modifier failed: " + modifierFailed.getMessage()); + doModify(label); + } + + @Override + public List getValues() { + return allowedValues; + } + +};