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;
28 import org.simantics.utils.datastructures.Callback;
31 * @author Tuukka Lehtonen
33 public class VariableModifier2 implements Modifier {
35 final protected Session session;
36 final protected Variable variable;
38 private String initialValue;
39 protected Throwable modifierFailed;
40 protected InputValidator variableInputValidator;
42 public VariableModifier2(RequestProcessor processor, Variable variable) {
43 this.variable = variable;
44 this.session = processor.getSession();
45 initializeModifier(processor);
48 protected void initializeModifier(RequestProcessor processor) {
52 processor.syncRequest(new ReadRequest() {
54 public void run(ReadGraph graph) throws DatabaseException {
55 initialValue = getInitialValue(graph);
56 initializeValidator(graph, variable);
60 } catch (DatabaseException e) {
61 Logger.defaultLogError("Modifier initialization failed, see exception for details.", e);
67 protected void initializeValidator(ReadGraph graph, Variable var) throws DatabaseException {
69 // Resource validator = variable.getPossiblePropertyValue(graph, Variables.INPUT_VALIDATOR);
70 // if(validator != null) {
71 // variableInputValidator = graph.getPossibleContextualAdapter(validator, variable, Variable.class, InputValidator.class);
74 final Function1<String, String> validator = variable.getPossiblePropertyValue(graph, Variables.INPUT_VALIDATOR);
75 if (validator != null)
76 variableInputValidator = new InputValidator() {
79 public String isValid(Object newValue) {
80 return validator.apply((String)newValue);
87 protected void doModify(final String label) {
88 session.asyncRequest(new VariableWrite(variable, label), new Callback<DatabaseException>() {
90 public void run(DatabaseException parameter) {
91 if (parameter != null)
92 ErrorLogger.defaultLogError(parameter);
99 protected void modifySuccessful() {
102 protected String getInitialValue(ReadGraph graph) throws DatabaseException {
103 return variable.getValue(graph, Bindings.STRING);
107 public String getValue() {
112 public String isValid(String label) {
114 if (modifierFailed != null)
115 return "Could not resolve validator for this value, modification denied. Reason: " + modifierFailed.getMessage();
117 if (variableInputValidator != null)
118 return variableInputValidator.isValid(label);
125 public final void modify(String label) {
126 if (modifierFailed != null)
127 // Should never end up here, isValid should prevent it.
128 throw new Error("modifier failed: " + modifierFailed.getMessage());