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