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.Collection;
16 import java.util.List;
19 import org.simantics.browsing.ui.common.modifiers.EnumeratedValue;
20 import org.simantics.browsing.ui.common.modifiers.Enumeration;
21 import org.simantics.browsing.ui.common.modifiers.EnumerationValue;
22 import org.simantics.databoard.Bindings;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.Resource;
25 import org.simantics.db.common.request.ResourceRead;
26 import org.simantics.db.common.utils.NameUtils;
27 import org.simantics.db.exception.AdaptionException;
28 import org.simantics.db.exception.DatabaseException;
29 import org.simantics.layer0.Layer0;
32 * @author Tuukka Lehtonen
34 public class GetEnumerationValue extends ResourceRead<EnumerationValue<Resource>> {
36 public GetEnumerationValue(Resource resource) {
41 public EnumerationValue<Resource> perform(ReadGraph graph) throws DatabaseException {
42 return enumerate(graph, resource);
45 public static String getEnumerationValueName(ReadGraph graph, Resource resource) throws DatabaseException {
46 Layer0 L0 = Layer0.getInstance(graph);
47 String label = graph.getPossibleRelatedValue(resource, L0.HasLabel, Bindings.STRING);
50 return safeName(graph, resource);
53 public static EnumerationValue<Resource> enumerate(ReadGraph graph, Resource resource) throws DatabaseException {
54 Layer0 l0 = Layer0.getInstance(graph);
55 Set<Resource> types = graph.getTypes(resource);
57 // FIXME: this is a hack to make boolean values editable through a combo-box like other enumerations
58 if (types.contains(l0.Boolean)) {
59 List<EnumeratedValue<Resource>> values = new ArrayList<EnumeratedValue<Resource>>(2);
60 values.add(new EnumeratedValue<Resource>(Boolean.FALSE.toString(), l0.False));
61 values.add(new EnumeratedValue<Resource>(Boolean.TRUE.toString(), l0.True));
62 Boolean currentValue = graph.getPossibleValue(resource, Bindings.BOOLEAN);
63 Enumeration<Resource> enumeration = new Enumeration<Resource>(values);
64 EnumeratedValue<Resource> current = Boolean.TRUE.equals(currentValue) ? values.get(1) : values.get(0);
65 return new EnumerationValue<Resource>(enumeration, current);
67 for (Resource type : types) {
68 if (graph.hasStatement(type, l0.Enumeration, type)) {
69 Collection<Resource> values = graph.getObjects(type, l0.ConsistsOf);
70 List<EnumeratedValue<Resource>> result = new ArrayList<EnumeratedValue<Resource>>(values.size());
71 for (Resource value : values) {
72 result.add(new EnumeratedValue<Resource>(getEnumerationValueName(graph, value), value));
74 Enumeration<Resource> enumeration = new Enumeration<Resource>(result);
75 return new EnumerationValue<Resource>(enumeration, enumeration.find(resource));
82 private static String safeName(ReadGraph graph, Resource value) throws DatabaseException {
83 return graph.syncRequest(new NameRequest(value));
86 public static class NameRequest extends ResourceRead<String> {
87 public NameRequest(Resource resource) {
91 public String perform(ReadGraph graph) throws DatabaseException {
93 return graph.adapt(resource, String.class);
94 } catch (AdaptionException e) {
95 return NameUtils.getSafeName(graph, resource);