]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contributor/labeler/LabelerContributorImpl.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 / contributor / labeler / LabelerContributorImpl.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.contributor.labeler;
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.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;
32
33 abstract public class LabelerContributorImpl<T> implements Contributor<LabelerFactory> {
34
35     abstract public String getLabel(ReadGraph graph, T input) throws DatabaseException;
36
37     public int getCategory(ReadGraph graph, T input) throws DatabaseException {
38         return 0;
39     }
40
41     public Modifier getModifier(ReadGraph graph, T input, String columnKey) throws DatabaseException {
42         return null;
43     }
44
45     final private Class<?> clazz;
46
47     @Override
48     public Tester getNodeContextTester() {
49         return null;
50     }
51
52     public LabelerContributorImpl() {
53         clazz = ReflectionUtils.getSingleParameterType(getClass());
54     }
55
56     public LabelerContributorImpl(Class<?> clazz) {
57         this.clazz = clazz;
58     }
59
60     @Override
61     public LabelerFactory getFactory() {
62
63         return new LabelerFactory() {
64
65             @Override
66             public Labeler create(final PrimitiveQueryUpdater updater, NodeContext context, final LabelerKey key) {
67
68                 return new LabelerContributionImpl(updater, context, key) {
69
70                     @SuppressWarnings("unchecked")
71                     @Override
72                     public Map<String, String> labels(ReadGraph graph, NodeContext context) throws DatabaseException {
73
74                         T input = (T)context.getConstant(BuiltinKeys.INPUT);
75
76                         String value = LabelerContributorImpl.this.getLabel(graph, input);
77
78                         return new ArrayMap<String, String>(ColumnKeys.KEYS_SINGLE,
79                                 new String[] { value });
80
81                     }
82
83                     @SuppressWarnings("unchecked")
84                     @Override
85                     public Modifier getModifier(ReadGraph graph, UndoContext undoContext, NodeContext context, String columnKey) throws DatabaseException {
86
87                         T input = (T)context.getConstant(BuiltinKeys.INPUT);
88
89                         return LabelerContributorImpl.this.getModifier(graph, input, columnKey);
90
91                     }
92
93                     @SuppressWarnings("unchecked")
94                     @Override
95                     public int category(ReadGraph graph, NodeContext context) throws DatabaseException {
96
97                         T input = (T)context.getConstant(BuiltinKeys.INPUT);
98
99                         return LabelerContributorImpl.this.getCategory(graph, input);
100
101                     }
102
103                     @Override
104                     public String toString() {
105                         return LabelerContributorImpl.this.toString();
106                     }
107
108                 };
109
110             }
111
112         };
113
114     }
115
116     @Override
117     public Class<?> getInputClass() {
118         return clazz;
119     }
120
121 }