]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/EnumerationVariableModifier.java
Fixed all line endings of the repository
[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.datastructures.Callback;
26 import org.simantics.utils.ui.ErrorLogger;
27
28 /**
29  * @author Tuukka Lehtonen
30  */
31 public class EnumerationVariableModifier implements EnumerationModifier {
32
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;
38
39     private String             initialValue;
40     protected Throwable        modifierFailed;
41
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;
49
50         initializeModifier(processor);
51     }
52
53     protected void initializeModifier(RequestProcessor processor) {
54         try {
55             processor.syncRequest(new ReadRequest() {
56                 @Override
57                 public void run(ReadGraph graph) throws DatabaseException {
58                     initialValue = getInitialValue(graph);
59                 }
60             });
61         } catch (DatabaseException e) {
62             modifierFailed = e;
63         }
64     }
65
66     protected void doModify(final String label) {
67         session.asyncRequest(new VariableWrite(variable, label, null, null),
68                 new Callback<DatabaseException>() {
69             @Override
70             public void run(DatabaseException parameter) {
71                 if (parameter != null)
72                     ErrorLogger.defaultLogError(parameter);
73             }
74         });
75     }
76
77     protected String getInitialValue(ReadGraph graph) throws DatabaseException {
78         Object value = variable.getValue(graph);
79         return value == null ? defaultValue : value.toString();
80     }
81
82     @Override
83     public String getValue() {
84         return initialValue;
85     }
86
87     @Override
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
93         // enumerations.
94         return null;
95     }
96
97     @Override
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());
102         doModify(label);
103     }
104
105     @Override
106     public List<String> getValues() {
107         return allowedValues;
108     }
109
110 };