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