1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.List;
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;
40 * @author Tuukka Lehtonen
42 public class EnumerationVariableModifier2 implements EnumerationModifier {
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;
50 protected Throwable modifierFailed;
52 public EnumerationVariableModifier2(RequestProcessor processor, UndoContext undoContext, Variable variable) {
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);
62 protected String computeDefaultValue(RequestProcessor processor, final Variable variable) {
64 return processor.syncRequest(new Read<String>() {
66 public String perform(ReadGraph graph) throws DatabaseException {
67 return variable.getPossiblePropertyValue(graph, Variables.LABEL);
70 } catch (DatabaseException e) {
75 protected Set<String> computeAllowedValues(RequestProcessor processor, final Variable variable) {
77 return processor.syncRequest(new Read<Set<String>>() {
79 public Set<String> perform(ReadGraph graph) throws DatabaseException {
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);
94 } catch (DatabaseException e) {
95 return Collections.emptySet();
99 public class Write extends WriteRequest {
101 final private Variable variable;
102 final private String label;
104 public Write(Variable variable, String label) {
105 super((VirtualGraph)null);
106 this.variable = variable;
111 public void perform(WriteGraph graph) throws DatabaseException {
112 variable.setValue(graph, label, Bindings.STRING);
117 protected void doModify(final String label) {
118 session.asyncRequest(new Write(variable, label),
119 new Callback<DatabaseException>() {
121 public void run(DatabaseException parameter) {
122 if (parameter != null)
123 ErrorLogger.defaultLogError(parameter);
129 public String getValue() {
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
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());
152 public List<String> getValues() {
153 ArrayList<String> result = new ArrayList<String>();
154 result.addAll(allowedValues);