]> 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 all line endings of the repository
[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
28 /**
29  * @author Tuukka Lehtonen
30  */
31 public class RelationContextLabelerFactory implements LabelerFactory {
32
33     @Override
34     public Labeler create(PrimitiveQueryUpdater updater, final NodeContext context, LabelerKey key) {
35         assert(updater != null);
36         assert(context != null);
37
38         return new LazyGraphLabeler(updater, context, key) {
39             @Override
40             public Map<String, String> labels(ReadGraph graph) throws DatabaseException {
41                 final RelationContext ctx = (RelationContext) context.getConstant(BuiltinKeys.INPUT);
42                 String property = LabelerUtil.safeStringRepresentation(graph, ctx.getStatement().getPredicate());
43                 //System.out.println(getClass().getName() + ": relation context: " + NameUtils.getSafeName(graph, ctx.getSubject()) + " - " + NameUtils.toString(graph, ctx.getStatement()));
44                 String object = graph.getPossibleRelatedAdapter(ctx.getSubject(), ctx.getStatement().getPredicate(), String.class);
45                 if (object == null)
46                     object = LabelerUtil.safeStringRepresentation(graph, ctx.getStatement().getObject());
47                 return ArrayMap.make(ColumnKeys.KEYS_PROPERTY_VALUE, new String[] { property, object });
48             }
49         };
50     }
51
52 }