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%2FGetEnumeration.java;h=3ae65574cff8893907ac7331765c4ba1570fb8ea;hp=77104d22f168ebbc350b2f0ae3789c9a866d9c30;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/GetEnumeration.java b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/GetEnumeration.java index 77104d22f..3ae65574c 100644 --- a/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/GetEnumeration.java +++ b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/GetEnumeration.java @@ -1,86 +1,86 @@ -/******************************************************************************* - * 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.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 GetEnumeration extends ResourceRead> { - - public GetEnumeration(Resource resource) { - super(resource); - } - - @Override - public Enumeration perform(ReadGraph graph) throws DatabaseException { - return enumerate(graph, resource); - } - - public static Enumeration 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) || types.contains(l0.BooleanArray)) { - List> values = new ArrayList>(2); - values.add(new EnumeratedValue(Boolean.FALSE.toString(), l0.False)); - values.add(new EnumeratedValue(Boolean.TRUE.toString(), l0.True)); - return new Enumeration(values); - } 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)); - } - return new Enumeration(result); - } - } - } - 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); - } - } - } - -} +/******************************************************************************* + * 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.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 GetEnumeration extends ResourceRead> { + + public GetEnumeration(Resource resource) { + super(resource); + } + + @Override + public Enumeration perform(ReadGraph graph) throws DatabaseException { + return enumerate(graph, resource); + } + + public static Enumeration 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) || types.contains(l0.BooleanArray)) { + List> values = new ArrayList>(2); + values.add(new EnumeratedValue(Boolean.FALSE.toString(), l0.False)); + values.add(new EnumeratedValue(Boolean.TRUE.toString(), l0.True)); + return new Enumeration(values); + } 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)); + } + return new Enumeration(result); + } + } + } + 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); + } + } + } + +}