]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/EnumerationVariableModifier2.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 / EnumerationVariableModifier2.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.ArrayList;
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
19
20 import org.simantics.browsing.ui.content.Labeler.EnumerationModifier;
21 import org.simantics.databoard.Bindings;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.RequestProcessor;
24 import org.simantics.db.Resource;
25 import org.simantics.db.Session;
26 import org.simantics.db.UndoContext;
27 import org.simantics.db.VirtualGraph;
28 import org.simantics.db.WriteGraph;
29 import org.simantics.db.common.request.ObjectsWithType;
30 import org.simantics.db.common.request.WriteRequest;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.db.layer0.variable.Variable;
33 import org.simantics.db.layer0.variable.Variables;
34 import org.simantics.db.request.Read;
35 import org.simantics.layer0.Layer0;
36 import org.simantics.utils.ui.ErrorLogger;
37
38 /**
39  * @author Tuukka Lehtonen
40  */
41 public class EnumerationVariableModifier2 implements EnumerationModifier {
42
43     protected final Session      session;
44     protected final UndoContext  undoContext;
45     protected final Variable     variable;
46     protected final Set<String> allowedValues;
47     protected final String       defaultValue;
48
49     protected Throwable        modifierFailed;
50
51     public EnumerationVariableModifier2(RequestProcessor processor, UndoContext undoContext, Variable variable) {
52
53         this.session = processor.getSession();
54         this.undoContext = undoContext;
55         this.variable = variable;
56         this.allowedValues = computeAllowedValues(processor, variable);
57         this.defaultValue = computeDefaultValue(processor, variable);
58         
59     }
60
61     protected String computeDefaultValue(RequestProcessor processor, final Variable variable) {
62         try {
63            return processor.syncRequest(new Read<String>() {
64                 @Override
65                 public String perform(ReadGraph graph) throws DatabaseException {
66                         return variable.getPossiblePropertyValue(graph, Variables.LABEL);
67                 }
68             });
69         } catch (DatabaseException e) {
70             return "";
71         }
72     }
73     
74     protected Set<String> computeAllowedValues(RequestProcessor processor, final Variable variable) {
75         try {
76            return processor.syncRequest(new Read<Set<String>>() {
77                 @Override
78                 public Set<String> perform(ReadGraph graph) throws DatabaseException {
79
80                         Layer0 L0 = Layer0.getInstance(graph);
81                         Set<String> result = new HashSet<String>();
82                         Resource literal = variable.getPossibleRepresents(graph);
83                         Resource type = graph.getSingleObject(literal, L0.PartOf);
84                         for(Resource lit : graph.syncRequest(new ObjectsWithType(type, L0.ConsistsOf, type))) {
85                                 String label = graph.getPossibleRelatedValue(lit, L0.HasLabel);
86                                 if(label == null) label = graph.getRelatedValue(lit, L0.HasName);
87                                 result.add(label);
88                         }
89                         return result;
90                         
91                 }
92             });
93         } catch (DatabaseException e) {
94             return Collections.emptySet();
95         }
96     }
97
98     public class Write extends WriteRequest {
99
100         final private Variable variable;
101         final private String label;
102
103         public Write(Variable variable, String label) {
104             super((VirtualGraph)null);
105             this.variable = variable;
106             this.label = label;
107         }
108
109         @Override
110         public void perform(WriteGraph graph) throws DatabaseException {
111                 variable.setValue(graph, label, Bindings.STRING);
112         }
113
114     }
115     
116     protected void doModify(final String label) {
117         session.asyncRequest(new Write(variable, label),
118                 parameter -> {
119                     if (parameter != null)
120                         ErrorLogger.defaultLogError(parameter);
121         });
122     }
123
124     @Override
125     public String getValue() {
126         return defaultValue;
127     }
128
129     @Override
130     public String isValid(String label) {
131         if (modifierFailed != null)
132             return "Could not resolve validator for this value, modification denied. Reason: "
133             + modifierFailed.getMessage();
134         // Validity should already be enforced by the editing UI for
135         // enumerations.
136         return null;
137     }
138
139     @Override
140     public final void modify(String label) {
141         if (modifierFailed != null)
142             // Should never end up here, isValid should prevent it.
143             throw new Error("modifier failed: " + modifierFailed.getMessage());
144         doModify(label);
145     }
146
147     @Override
148     public List<String> getValues() {
149         ArrayList<String> result = new ArrayList<String>();
150         result.addAll(allowedValues);
151         return result;
152     }
153
154 };