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