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;
16 import org.simantics.Simantics;
17 import org.simantics.browsing.ui.BuiltinKeys;
18 import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;
19 import org.simantics.browsing.ui.GraphExplorer.ModificationContext;
20 import org.simantics.browsing.ui.NodeContext;
21 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
22 import org.simantics.browsing.ui.common.ColumnKeys;
23 import org.simantics.browsing.ui.common.modifiers.EnumerationValue;
24 import org.simantics.browsing.ui.common.property.IProperty;
25 import org.simantics.browsing.ui.content.Labeler;
26 import org.simantics.browsing.ui.content.LabelerFactory;
27 import org.simantics.db.ReadGraph;
28 import org.simantics.db.Resource;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.db.management.ISessionContext;
31 import org.simantics.utils.datastructures.ArrayMap;
34 * Requires {@link IProperty} as input.
36 * @author Tuukka Lehtonen
38 public class PropertyValueRepresentationLabelerFactory implements LabelerFactory {
41 public Labeler create(PrimitiveQueryUpdater manager, final NodeContext context, LabelerKey key) {
42 assert(manager != null);
43 assert(context != null);
45 return new LazyGraphLabeler(manager, context, key) {
47 public Map<String, String> labels(ReadGraph graph) throws DatabaseException {
48 IProperty prop = (IProperty) context.getConstant(BuiltinKeys.INPUT);
49 Resource[] r = prop.adapt(Resource[].class);
51 String property = LabelerUtil.safeStringRepresentation(graph, r[1]);
52 String value = LabelerUtil.safeStringRepresentation(graph, r[2]);
54 return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { property, value });
58 public Modifier getModifier(ModificationContext modificationContext, String key) {
59 ISessionContext session = Simantics.getSessionContext();
63 IProperty prop = (IProperty) context.getConstant(BuiltinKeys.INPUT);
64 Resource[] r = prop.adapt(Resource[].class);
66 if (ColumnKeys.VALUE.equals(key)) {
68 EnumerationValue<Resource> enu = session.getSession().syncRequest(new GetEnumerationValue(r[2]));
70 return new GraphEnumerationModifier(session.getSession(), r[0], r[1], enu.getEnumeration(), enu.getEnumeratedValue());
72 return new GraphFactoryStringModifier(r[0], r[1], r[2], session.getSession());
74 } catch (DatabaseException e) {
75 throw new RuntimeException(e);