X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.graph.impl%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fgraph%2Fimpl%2FGetEnumerationValue.java;fp=bundles%2Forg.simantics.browsing.ui.graph.impl%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fgraph%2Fimpl%2FGetEnumerationValue.java;h=d9c729cc6aec777f22585cfead15367906e73aa4;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/GetEnumerationValue.java b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/GetEnumerationValue.java new file mode 100644 index 000000000..d9c729cc6 --- /dev/null +++ b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/GetEnumerationValue.java @@ -0,0 +1,92 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.browsing.ui.graph.impl; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Set; + +import org.simantics.browsing.ui.common.modifiers.EnumeratedValue; +import org.simantics.browsing.ui.common.modifiers.Enumeration; +import org.simantics.browsing.ui.common.modifiers.EnumerationValue; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ResourceRead; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.AdaptionException; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; + +/** + * @author Tuukka Lehtonen + */ +public class GetEnumerationValue extends ResourceRead> { + + public GetEnumerationValue(Resource resource) { + super(resource); + } + + @Override + public EnumerationValue perform(ReadGraph graph) throws DatabaseException { + return enumerate(graph, resource); + } + + public static EnumerationValue enumerate(ReadGraph graph, Resource resource) throws DatabaseException { + Layer0 l0 = Layer0.getInstance(graph); + Set types = graph.getTypes(resource); + + // FIXME: this is a hack to make boolean values editable through a combo-box like other enumerations + if (types.contains(l0.Boolean)) { + List> values = new ArrayList>(2); + values.add(new EnumeratedValue(Boolean.FALSE.toString(), l0.False)); + values.add(new EnumeratedValue(Boolean.TRUE.toString(), l0.True)); + Boolean currentValue = graph.getPossibleValue(resource, Bindings.BOOLEAN); + Enumeration enumeration = new Enumeration(values); + EnumeratedValue current = Boolean.TRUE.equals(currentValue) ? values.get(1) : values.get(0); + return new EnumerationValue(enumeration, current); + } else { + for (Resource type : types) { + if (graph.hasStatement(type, l0.Enumeration, type)) { + Collection values = graph.getObjects(type, l0.ConsistsOf); + List> result = new ArrayList>(values.size()); + for (Resource value : values) { + result.add(new EnumeratedValue(safeName(graph, value), value)); + } + Enumeration enumeration = new Enumeration(result); + return new EnumerationValue(enumeration, enumeration.find(resource)); + } + } + } + return null; + } + + private static String safeName(ReadGraph graph, Resource value) throws DatabaseException { + return graph.syncRequest(new NameRequest(value)); + } + + public static class NameRequest extends ResourceRead { + public NameRequest(Resource resource) { + super(resource); + } + @Override + public String perform(ReadGraph graph) throws DatabaseException { + try { + return graph.adapt(resource, String.class); + } catch (AdaptionException e) { + return NameUtils.getSafeName(graph, resource); + } + } + } + +}