Allow column specific tooltips in the GraphExplorer
[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, 2018 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  *     Semantum Oy - gitlab #146 - tooltip support
12  *******************************************************************************/
13 package org.simantics.browsing.ui.graph.impl.contributor.labeler;
14
15 import java.util.Map;
16
17 import org.simantics.browsing.ui.BuiltinKeys;
18 import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;
19 import org.simantics.browsing.ui.NodeContext;
20 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
21 import org.simantics.browsing.ui.Tester;
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
32 abstract public class ColumnLabelerContributorImpl<T> implements Contributor<LabelerFactory> {
33
34     abstract public Map<String, String> getLabel(ReadGraph graph, T input) throws DatabaseException;
35
36     public int getCategory(ReadGraph graph, T input) throws DatabaseException {
37         return 0;
38     }
39
40     public Modifier getModifier(ReadGraph graph, UndoContext context, T input, String columnKey) throws DatabaseException {
41         return null;
42     }
43
44     final private Class<?> clazz;
45
46     @Override
47     public Tester getNodeContextTester() {
48         return null;
49     }
50
51     public ColumnLabelerContributorImpl() {
52         clazz = ReflectionUtils.getSingleParameterType(getClass());
53     }
54
55     public ColumnLabelerContributorImpl(Class<?> clazz) {
56         this.clazz = clazz;
57     }
58
59     public boolean shouldCreateToolTip(Object event, T input) {
60         return false;
61     }
62
63     public Object createToolTipContentArea(Object event, Object parent, T input) {
64         return null;
65     }
66
67     @Override
68     public LabelerFactory getFactory() {
69
70         return new LabelerFactory() {
71
72             @Override
73             public Labeler create(final PrimitiveQueryUpdater updater, NodeContext context, final LabelerKey key) {
74
75                 return new LabelerContributionImpl(updater, context, key) {
76
77                     @SuppressWarnings("unchecked")
78                     @Override
79                     public Map<String, String> labels(ReadGraph graph, NodeContext context) throws DatabaseException {
80
81                         T input = (T)context.getConstant(BuiltinKeys.INPUT);
82
83                         return ColumnLabelerContributorImpl.this.getLabel(graph, input);
84
85                     }
86
87                     @SuppressWarnings("unchecked")
88                     @Override
89                     public int category(ReadGraph graph, NodeContext context) throws DatabaseException {
90
91                         T input = (T)context.getConstant(BuiltinKeys.INPUT);
92
93                         return ColumnLabelerContributorImpl.this.getCategory(graph, input);
94
95                     }
96
97                     @SuppressWarnings("unchecked")
98                     @Override
99                     public Modifier getModifier(ReadGraph graph, UndoContext undoContext, NodeContext context, String columnKey) throws DatabaseException {
100
101                         T input = (T)context.getConstant(BuiltinKeys.INPUT);
102
103                         return ColumnLabelerContributorImpl.this.getModifier(graph, undoContext, input, columnKey);
104
105                     }
106
107                     @Override
108                     public String toString() {
109                         return ColumnLabelerContributorImpl.this.toString();
110                     }
111
112                     @Override
113                     public boolean createToolTip(Object event, NodeContext nodeContext) {
114                         T input = (T)context.getConstant(BuiltinKeys.INPUT);
115                         return ColumnLabelerContributorImpl.this.shouldCreateToolTip(event, input);
116                     }
117
118                     @Override
119                     public Object createToolTipContent(Object event, Object parent, NodeContext nodeContext) {
120                         T input = (T)context.getConstant(BuiltinKeys.INPUT);
121                         return ColumnLabelerContributorImpl.this.createToolTipContentArea(event, parent, input);
122                     }
123
124                 };
125
126             }
127
128         };
129
130     }
131
132     @Override
133     public Class<?> getInputClass() {
134         return clazz;
135     }
136
137 }