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.graph.impl;
14 import java.util.List;
16 import org.simantics.browsing.ui.content.Labeler.EnumerationModifier;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.RequestProcessor;
19 import org.simantics.db.Session;
20 import org.simantics.db.UndoContext;
21 import org.simantics.db.common.request.ReadRequest;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.variable.Variable;
24 import org.simantics.db.layer0.variable.VariableWrite;
25 import org.simantics.utils.datastructures.Callback;
26 import org.simantics.utils.ui.ErrorLogger;
29 * @author Tuukka Lehtonen
31 public class EnumerationVariableModifier implements EnumerationModifier {
33 protected final Session session;
34 protected final UndoContext undoContext;
35 protected final Variable variable;
36 protected final List<String> allowedValues;
37 protected final String defaultValue;
39 private String initialValue;
40 protected Throwable modifierFailed;
42 public EnumerationVariableModifier(RequestProcessor processor, UndoContext undoContext, Variable variable,
43 List<String> allowedValues, String defaultValue) {
44 this.session = processor.getSession();
45 this.undoContext = undoContext;
46 this.variable = variable;
47 this.allowedValues = allowedValues;
48 this.defaultValue = defaultValue;
50 initializeModifier(processor);
53 protected void initializeModifier(RequestProcessor processor) {
55 processor.syncRequest(new ReadRequest() {
57 public void run(ReadGraph graph) throws DatabaseException {
58 initialValue = getInitialValue(graph);
61 } catch (DatabaseException e) {
66 protected void doModify(final String label) {
67 session.asyncRequest(new VariableWrite(variable, label, null, null),
68 new Callback<DatabaseException>() {
70 public void run(DatabaseException parameter) {
71 if (parameter != null)
72 ErrorLogger.defaultLogError(parameter);
77 protected String getInitialValue(ReadGraph graph) throws DatabaseException {
78 Object value = variable.getValue(graph);
79 return value == null ? defaultValue : value.toString();
83 public String getValue() {
88 public String isValid(String label) {
89 if (modifierFailed != null)
90 return "Could not resolve validator for this value, modification denied. Reason: "
91 + modifierFailed.getMessage();
92 // Validity should already be enforced by the editing UI for
98 public final void modify(String label) {
99 if (modifierFailed != null)
100 // Should never end up here, isValid should prevent it.
101 throw new Error("modifier failed: " + modifierFailed.getMessage());
106 public List<String> getValues() {
107 return allowedValues;