]> gerrit.simantics Code Review - simantics/platform.git/blob
3bd3e7b91217ef5233388158c88f86e8bb24abdc
[simantics/platform.git] /
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.common.modifiers.EnumerationValue;
17 import org.simantics.browsing.ui.content.Labeler.EnumerationModifier;
18 import org.simantics.databoard.Bindings;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.RequestProcessor;
21 import org.simantics.db.Resource;
22 import org.simantics.db.Session;
23 import org.simantics.db.VirtualGraph;
24 import org.simantics.db.WriteGraph;
25 import org.simantics.db.common.request.WriteRequest;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.variable.Variable;
28 import org.simantics.db.request.Read;
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                     EnumerationValue<Resource> ev = graph.syncRequest(new GetEnumerationValue(variable.getParent(graph).getRepresents(graph)));
59                     if(ev != null) {
60                             return ev.getEnumeratedValue().getName();
61                     }
62           //            System.err.println(variable.getURI(graph));
63                         return variable.getValue(graph);//variable.getPossiblePropertyValue(graph, Variables.LABEL);
64                 }
65             });
66         } catch (DatabaseException e) {
67             return "";
68         }
69     }
70
71     public class Write extends WriteRequest {
72
73         final private Variable variable;
74         final private String label;
75
76         public Write(Variable variable, String label) {
77             super((VirtualGraph)null);
78             this.variable = variable;
79             this.label = label;
80         }
81
82         @Override
83         public void perform(WriteGraph graph) throws DatabaseException {
84                 variable.setValue(graph, label, Bindings.STRING);
85         }
86
87     }
88     
89     protected void doModify(final String label) {
90         session.asyncRequest(new Write(variable, label),
91                 parameter -> {
92                     if (parameter != null)
93                         ErrorLogger.defaultLogError(parameter);
94         });
95     }
96
97     @Override
98     public String getValue() {
99         return defaultValue;
100     }
101
102     @Override
103     public String isValid(String label) {
104         if (modifierFailed != null)
105             return "Could not resolve validator for this value, modification denied. Reason: "
106             + modifierFailed.getMessage();
107         // Validity should already be enforced by the editing UI for
108         // enumerations.
109         return null;
110     }
111
112     @Override
113     public final void modify(String label) {
114         if (modifierFailed != null)
115             // Should never end up here, isValid should prevent it.
116             throw new Error("modifier failed: " + modifierFailed.getMessage());
117         doModify(label);
118     }
119
120     @Override
121     public List<String> getValues() {
122         return allowedValues;
123     }
124
125 };