]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/RelationContextLabelerFactory.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 / RelationContextLabelerFactory.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.browsing.ui.BuiltinKeys;
17 import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;
18 import org.simantics.browsing.ui.NodeContext;
19 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
20 import org.simantics.browsing.ui.common.ColumnKeys;
21 import org.simantics.browsing.ui.content.Labeler;
22 import org.simantics.browsing.ui.content.LabelerFactory;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.RelationContext;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.utils.datastructures.ArrayMap;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * @author Tuukka Lehtonen
32  */
33 public class RelationContextLabelerFactory implements LabelerFactory {
34
35     @Override
36     public Labeler create(PrimitiveQueryUpdater updater, final NodeContext context, LabelerKey key) {
37         assert(updater != null);
38         assert(context != null);
39
40         return new LazyGraphLabeler(updater, context, key) {
41             @Override
42             public Map<String, String> labels(ReadGraph graph) throws DatabaseException {
43                 final RelationContext ctx = (RelationContext) context.getConstant(BuiltinKeys.INPUT);
44                 String property = LabelerUtil.safeStringRepresentation(graph, ctx.getStatement().getPredicate());
45                 //System.out.println(getClass().getName() + ": relation context: " + NameUtils.getSafeName(graph, ctx.getSubject()) + " - " + NameUtils.toString(graph, ctx.getStatement()));
46                 String object = graph.getPossibleRelatedAdapter(ctx.getSubject(), ctx.getStatement().getPredicate(), String.class);
47                 if (object == null)
48                     object = LabelerUtil.safeStringRepresentation(graph, ctx.getStatement().getObject());
49                 return ArrayMap.make(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { property, object });
50             }
51
52             @Override
53             public Logger getLogger() {
54                 return LoggerFactory.getLogger(RelationContextLabelerFactory.class);
55             }
56         };
57     }
58
59 }