]> gerrit.simantics Code Review - simantics/platform.git/blob
2b63c1a4f4de372e586ced9ba047ee88a532acda
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl;
13
14 import java.util.Map;
15
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;
32
33 /**
34  * Requires {@link IProperty} as input.
35  * 
36  * @author Tuukka Lehtonen
37  */
38 public class PropertyValueRepresentationLabelerFactory implements LabelerFactory {
39
40     @Override
41     public Labeler create(PrimitiveQueryUpdater manager, final NodeContext context, LabelerKey key) {
42         assert(manager != null);
43         assert(context != null);
44
45         return new LazyGraphLabeler(manager, context, key) {
46             @Override
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);
50
51                 String property = LabelerUtil.safeStringRepresentation(graph, r[1]);
52                 String value = LabelerUtil.safeStringRepresentation(graph, r[2]);
53
54                 return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { property, value });
55             }
56
57             @Override
58             public Modifier getModifier(ModificationContext modificationContext, String key) {
59                 ISessionContext session = Simantics.getSessionContext();
60                 if (session == null)
61                     return null;
62
63                 IProperty prop = (IProperty) context.getConstant(BuiltinKeys.INPUT);
64                 Resource[] r = prop.adapt(Resource[].class);
65
66                 if (ColumnKeys.VALUE.equals(key)) {
67                     try {
68                         EnumerationValue<Resource> enu = session.getSession().syncRequest(new GetEnumerationValue(r[2]));
69                         if (enu != null) {
70                             return new GraphEnumerationModifier(session.getSession(), r[0], r[1], enu.getEnumeration(), enu.getEnumeratedValue());
71                         } else {
72                             return new GraphFactoryStringModifier(r[0], r[1], r[2], session.getSession());
73                         }
74                     } catch (DatabaseException e) {
75                         throw new RuntimeException(e);
76                     }
77                 }
78                 return null;
79             }
80         };
81     }
82
83 }