1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.model.modifiers;
14 import org.simantics.browsing.ui.common.ErrorLogger;
15 import org.simantics.browsing.ui.content.Labeler.Modifier;
16 import org.simantics.databoard.Bindings;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.RequestProcessor;
19 import org.simantics.db.Session;
20 import org.simantics.db.common.request.ReadRequest;
21 import org.simantics.db.common.utils.Logger;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.variable.InputValidator;
24 import org.simantics.db.layer0.variable.Variable;
25 import org.simantics.db.layer0.variable.VariableWrite;
26 import org.simantics.db.layer0.variable.Variables;
27 import org.simantics.scl.runtime.function.Function1;
30 * @author Tuukka Lehtonen
32 public class VariableModifier2 implements Modifier {
34 final protected Session session;
35 final protected Variable variable;
37 private String initialValue;
38 protected Throwable modifierFailed;
39 protected InputValidator variableInputValidator;
41 public VariableModifier2(RequestProcessor processor, Variable variable) {
42 this.variable = variable;
43 this.session = processor.getSession();
44 initializeModifier(processor);
47 protected void initializeModifier(RequestProcessor processor) {
51 processor.syncRequest(new ReadRequest() {
53 public void run(ReadGraph graph) throws DatabaseException {
54 initialValue = getInitialValue(graph);
55 initializeValidator(graph, variable);
59 } catch (DatabaseException e) {
60 Logger.defaultLogError("Modifier initialization failed, see exception for details.", e);
66 protected void initializeValidator(ReadGraph graph, Variable var) throws DatabaseException {
68 // Resource validator = variable.getPossiblePropertyValue(graph, Variables.INPUT_VALIDATOR);
69 // if(validator != null) {
70 // variableInputValidator = graph.getPossibleContextualAdapter(validator, variable, Variable.class, InputValidator.class);
73 final Function1<String, String> validator = variable.getPossiblePropertyValue(graph, Variables.INPUT_VALIDATOR);
74 if (validator != null)
75 variableInputValidator = new InputValidator() {
78 public String isValid(Object newValue) {
79 return validator.apply((String)newValue);
86 protected void doModify(final String label) {
87 session.asyncRequest(new VariableWrite(variable, label), parameter -> {
88 if (parameter != null)
89 ErrorLogger.defaultLogError(parameter);
95 protected void modifySuccessful() {
98 protected String getInitialValue(ReadGraph graph) throws DatabaseException {
99 return variable.getValue(graph, Bindings.STRING);
103 public String getValue() {
108 public String isValid(String label) {
110 if (modifierFailed != null)
111 return "Could not resolve validator for this value, modification denied. Reason: " + modifierFailed.getMessage();
113 if (variableInputValidator != null)
114 return variableInputValidator.isValid(label);
121 public final void modify(String label) {
122 if (modifierFailed != null)
123 // Should never end up here, isValid should prevent it.
124 throw new Error("modifier failed: " + modifierFailed.getMessage());