]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/EnumerationVariableModifier3.java
32bd28c17f789f1658a271a5e064807c4932f833
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / EnumerationVariableModifier3.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.databoard.Bindings;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.RequestProcessor;
20 import org.simantics.db.Session;
21 import org.simantics.db.VirtualGraph;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.common.request.WriteRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.variable.Variable;
26 import org.simantics.db.request.Read;
27 import org.simantics.utils.ui.ErrorLogger;
28
29 /**
30  * @author Tuukka Lehtonen
31  */
32 public class EnumerationVariableModifier3 implements EnumerationModifier {
33
34     protected final Session      session;
35     protected final Variable     variable;
36     protected final List<String> allowedValues;
37     protected final String       defaultValue;
38
39     protected Throwable        modifierFailed;
40
41     public EnumerationVariableModifier3(RequestProcessor processor, Variable variable, List<String> allowedValues) {
42
43         this.session = processor.getSession();
44         this.variable = variable;
45         this.allowedValues = allowedValues;
46         this.defaultValue = computeDefaultValue(processor, variable);
47         //System.err.println(this.defaultValue);
48         
49     }
50
51     protected String computeDefaultValue(RequestProcessor processor, final Variable variable) {
52         try {
53            return processor.syncRequest(new Read<String>() {
54                 @Override
55                 public String perform(ReadGraph graph) throws DatabaseException {
56           //            System.err.println(variable.getURI(graph));
57                         return variable.getValue(graph);//variable.getPossiblePropertyValue(graph, Variables.LABEL);
58                 }
59             });
60         } catch (DatabaseException e) {
61             return "";
62         }
63     }
64
65     public class Write extends WriteRequest {
66
67         final private Variable variable;
68         final private String label;
69
70         public Write(Variable variable, String label) {
71             super((VirtualGraph)null);
72             this.variable = variable;
73             this.label = label;
74         }
75
76         @Override
77         public void perform(WriteGraph graph) throws DatabaseException {
78                 variable.setValue(graph, label, Bindings.STRING);
79         }
80
81     }
82     
83     protected void doModify(final String label) {
84         session.asyncRequest(new Write(variable, label),
85                 parameter -> {
86                     if (parameter != null)
87                         ErrorLogger.defaultLogError(parameter);
88         });
89     }
90
91     @Override
92     public String getValue() {
93         return defaultValue;
94     }
95
96     @Override
97     public String isValid(String label) {
98         if (modifierFailed != null)
99             return "Could not resolve validator for this value, modification denied. Reason: "
100             + modifierFailed.getMessage();
101         // Validity should already be enforced by the editing UI for
102         // enumerations.
103         return null;
104     }
105
106     @Override
107     public final void modify(String label) {
108         if (modifierFailed != null)
109             // Should never end up here, isValid should prevent it.
110             throw new Error("modifier failed: " + modifierFailed.getMessage());
111         doModify(label);
112     }
113
114     @Override
115     public List<String> getValues() {
116         return allowedValues;
117     }
118
119 };