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.contributor.labeler;
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.Tester;
21 import org.simantics.browsing.ui.common.ColumnKeys;
22 import org.simantics.browsing.ui.content.Contributor;
23 import org.simantics.browsing.ui.content.Labeler;
24 import org.simantics.browsing.ui.content.Labeler.Modifier;
25 import org.simantics.browsing.ui.content.LabelerFactory;
26 import org.simantics.browsing.ui.graph.impl.contribution.LabelerContributionImpl;
27 import org.simantics.db.ReadGraph;
28 import org.simantics.db.UndoContext;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.utils.ReflectionUtils;
31 import org.simantics.utils.datastructures.ArrayMap;
33 abstract public class LabelerContributorImpl<T> implements Contributor<LabelerFactory> {
35 abstract public String getLabel(ReadGraph graph, T input) throws DatabaseException;
37 public int getCategory(ReadGraph graph, T input) throws DatabaseException {
41 public Modifier getModifier(ReadGraph graph, T input, String columnKey) throws DatabaseException {
45 final private Class<?> clazz;
48 public Tester getNodeContextTester() {
52 public LabelerContributorImpl() {
53 clazz = ReflectionUtils.getSingleParameterType(getClass());
56 public LabelerContributorImpl(Class<?> clazz) {
61 public LabelerFactory getFactory() {
63 return new LabelerFactory() {
66 public Labeler create(final PrimitiveQueryUpdater updater, NodeContext context, final LabelerKey key) {
68 return new LabelerContributionImpl(updater, context, key) {
70 @SuppressWarnings("unchecked")
72 public Map<String, String> labels(ReadGraph graph, NodeContext context) throws DatabaseException {
74 T input = (T)context.getConstant(BuiltinKeys.INPUT);
76 String value = LabelerContributorImpl.this.getLabel(graph, input);
78 return new ArrayMap<String, String>(ColumnKeys.KEYS_SINGLE,
79 new String[] { value });
83 @SuppressWarnings("unchecked")
85 public Modifier getModifier(ReadGraph graph, UndoContext undoContext, NodeContext context, String columnKey) throws DatabaseException {
87 T input = (T)context.getConstant(BuiltinKeys.INPUT);
89 return LabelerContributorImpl.this.getModifier(graph, input, columnKey);
93 @SuppressWarnings("unchecked")
95 public int category(ReadGraph graph, NodeContext context) throws DatabaseException {
97 T input = (T)context.getConstant(BuiltinKeys.INPUT);
99 return LabelerContributorImpl.this.getCategory(graph, input);
104 public String toString() {
105 return LabelerContributorImpl.this.toString();
117 public Class<?> getInputClass() {