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.ui.ErrorLogger;
39 * @author Tuukka Lehtonen
41 public class EnumerationVariableModifier2 implements EnumerationModifier {
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;
49 protected Throwable modifierFailed;
51 public EnumerationVariableModifier2(RequestProcessor processor, UndoContext undoContext, Variable variable) {
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);
61 protected String computeDefaultValue(RequestProcessor processor, final Variable variable) {
63 return processor.syncRequest(new Read<String>() {
65 public String perform(ReadGraph graph) throws DatabaseException {
66 return variable.getPossiblePropertyValue(graph, Variables.LABEL);
69 } catch (DatabaseException e) {
74 protected Set<String> computeAllowedValues(RequestProcessor processor, final Variable variable) {
76 return processor.syncRequest(new Read<Set<String>>() {
78 public Set<String> perform(ReadGraph graph) throws DatabaseException {
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);
93 } catch (DatabaseException e) {
94 return Collections.emptySet();
98 public class Write extends WriteRequest {
100 final private Variable variable;
101 final private String label;
103 public Write(Variable variable, String label) {
104 super((VirtualGraph)null);
105 this.variable = variable;
110 public void perform(WriteGraph graph) throws DatabaseException {
111 variable.setValue(graph, label, Bindings.STRING);
116 protected void doModify(final String label) {
117 session.asyncRequest(new Write(variable, label),
119 if (parameter != null)
120 ErrorLogger.defaultLogError(parameter);
125 public String getValue() {
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
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());
148 public List<String> getValues() {
149 ArrayList<String> result = new ArrayList<String>();
150 result.addAll(allowedValues);