]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/EnumerationVariableModifier.java
Use Consumer interface instead of deprecated Callback interface
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / EnumerationVariableModifier.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl;
13
14 import java.util.List;
15
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.ui.ErrorLogger;
26
27 /**
28  * @author Tuukka Lehtonen
29  */
30 public class EnumerationVariableModifier implements EnumerationModifier {
31
32     protected final Session      session;
33     protected final UndoContext  undoContext;
34     protected final Variable     variable;
35     protected final List<String> allowedValues;
36     protected final String       defaultValue;
37
38     private String             initialValue;
39     protected Throwable        modifierFailed;
40
41     public EnumerationVariableModifier(RequestProcessor processor, UndoContext undoContext, Variable variable,
42             List<String> allowedValues, String defaultValue) {
43         this.session = processor.getSession();
44         this.undoContext = undoContext;
45         this.variable = variable;
46         this.allowedValues = allowedValues;
47         this.defaultValue = defaultValue;
48
49         initializeModifier(processor);
50     }
51
52     protected void initializeModifier(RequestProcessor processor) {
53         try {
54             processor.syncRequest(new ReadRequest() {
55                 @Override
56                 public void run(ReadGraph graph) throws DatabaseException {
57                     initialValue = getInitialValue(graph);
58                 }
59             });
60         } catch (DatabaseException e) {
61             modifierFailed = e;
62         }
63     }
64
65     protected void doModify(final String label) {
66         session.asyncRequest(new VariableWrite(variable, label, null, null),
67                 parameter -> {
68                     if (parameter != null)
69                         ErrorLogger.defaultLogError(parameter);
70         });
71     }
72
73     protected String getInitialValue(ReadGraph graph) throws DatabaseException {
74         Object value = variable.getValue(graph);
75         return value == null ? defaultValue : value.toString();
76     }
77
78     @Override
79     public String getValue() {
80         return initialValue;
81     }
82
83     @Override
84     public String isValid(String label) {
85         if (modifierFailed != null)
86             return "Could not resolve validator for this value, modification denied. Reason: "
87             + modifierFailed.getMessage();
88         // Validity should already be enforced by the editing UI for
89         // enumerations.
90         return null;
91     }
92
93     @Override
94     public final void modify(String label) {
95         if (modifierFailed != null)
96             // Should never end up here, isValid should prevent it.
97             throw new Error("modifier failed: " + modifierFailed.getMessage());
98         doModify(label);
99     }
100
101     @Override
102     public List<String> getValues() {
103         return allowedValues;
104     }
105
106 };