]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/PropertyValueRepresentationLabelerFactory.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / PropertyValueRepresentationLabelerFactory.java
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 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Requires {@link IProperty} as input.
37  * 
38  * @author Tuukka Lehtonen
39  */
40 public class PropertyValueRepresentationLabelerFactory implements LabelerFactory {
41
42     @Override
43     public Labeler create(PrimitiveQueryUpdater manager, final NodeContext context, LabelerKey key) {
44         assert(manager != null);
45         assert(context != null);
46
47         return new LazyGraphLabeler(manager, context, key) {
48             @Override
49             public Map<String, String> labels(ReadGraph graph) throws DatabaseException {
50                 IProperty prop = (IProperty) context.getConstant(BuiltinKeys.INPUT);
51                 Resource[] r = prop.adapt(Resource[].class);
52
53                 String property = LabelerUtil.safeStringRepresentation(graph, r[1]);
54                 String value = LabelerUtil.safeStringRepresentation(graph, r[2]);
55
56                 return new ArrayMap<String, String>(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { property, value });
57             }
58
59             @Override
60             public Modifier getModifier(ModificationContext modificationContext, String key) {
61                 ISessionContext session = Simantics.getSessionContext();
62                 if (session == null)
63                     return null;
64
65                 IProperty prop = (IProperty) context.getConstant(BuiltinKeys.INPUT);
66                 Resource[] r = prop.adapt(Resource[].class);
67
68                 if (ColumnKeys.VALUE.equals(key)) {
69                     try {
70                         EnumerationValue<Resource> enu = session.getSession().syncRequest(new GetEnumerationValue(r[2]));
71                         if (enu != null) {
72                             return new GraphEnumerationModifier(session.getSession(), r[0], r[1], enu.getEnumeration(), enu.getEnumeratedValue());
73                         } else {
74                             return new GraphFactoryStringModifier(r[0], r[1], r[2], session.getSession());
75                         }
76                     } catch (DatabaseException e) {
77                         throw new RuntimeException(e);
78                     }
79                 }
80                 return null;
81             }
82             
83             @Override
84             public Logger getLogger() {
85                 return LoggerFactory.getLogger(getClass());
86             }
87
88         };
89     }
90
91 }