]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contributor/labeler/ColumnLabelerContributorImpl.java
Fixed multiple issues causing dangling references to discarded queries
[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.HashMap;
16 import java.util.Map;
17
18 import org.simantics.browsing.ui.BuiltinKeys;
19 import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;
20 import org.simantics.browsing.ui.NodeContext;
21 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
22 import org.simantics.browsing.ui.Tester;
23 import org.simantics.browsing.ui.content.Contributor;
24 import org.simantics.browsing.ui.content.Labeler;
25 import org.simantics.browsing.ui.content.Labeler.Modifier;
26 import org.simantics.browsing.ui.content.LabelerFactory;
27 import org.simantics.browsing.ui.graph.impl.contribution.LabelerContributionImpl;
28 import org.simantics.db.ReadGraph;
29 import org.simantics.db.UndoContext;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.utils.ReflectionUtils;
32
33 abstract public class ColumnLabelerContributorImpl<T> implements Contributor<LabelerFactory> {
34
35     abstract public Map<String, 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, UndoContext context, 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 ColumnLabelerContributorImpl() {
53         clazz = ReflectionUtils.getSingleParameterType(getClass());
54     }
55
56     public ColumnLabelerContributorImpl(Class<?> clazz) {
57         this.clazz = clazz;
58     }
59
60     public boolean shouldCreateToolTip(Object event, T input, Map<Object, Object> auxiliary) {
61         return false;
62     }
63
64     public Object createToolTipContentArea(Object event, Object parent, T input, Map<Object, Object> auxiliary) {
65         return null;
66     }
67
68     @Override
69     public LabelerFactory getFactory() {
70
71         return new LabelerFactory() {
72
73             @Override
74             public Labeler create(final PrimitiveQueryUpdater updater, NodeContext context, final LabelerKey key) {
75
76                 return new LabelerContributionImpl(updater, context, key) {
77
78                     @SuppressWarnings("unchecked")
79                     @Override
80                     public Map<String, String> labels(ReadGraph graph, NodeContext context) throws DatabaseException {
81
82                         T input = (T)context.getConstant(BuiltinKeys.INPUT);
83
84                         return ColumnLabelerContributorImpl.this.getLabel(graph, input);
85
86                     }
87
88                     @SuppressWarnings("unchecked")
89                     @Override
90                     public int category(ReadGraph graph, NodeContext context) throws DatabaseException {
91
92                         T input = (T)context.getConstant(BuiltinKeys.INPUT);
93
94                         return ColumnLabelerContributorImpl.this.getCategory(graph, input);
95
96                     }
97
98                     @SuppressWarnings("unchecked")
99                     @Override
100                     public Modifier getModifier(ReadGraph graph, UndoContext undoContext, NodeContext context, String columnKey) throws DatabaseException {
101
102                         T input = (T)context.getConstant(BuiltinKeys.INPUT);
103
104                         return ColumnLabelerContributorImpl.this.getModifier(graph, undoContext, input, columnKey);
105
106                     }
107
108                     @Override
109                     public String toString() {
110                         return ColumnLabelerContributorImpl.this.toString();
111                     }
112                     
113                     private Map<Object, Object> auxiliary = new HashMap<>();
114
115                     @Override
116                     public boolean createToolTip(Object event, NodeContext nodeContext) {
117                         T input = (T)context.getConstant(BuiltinKeys.INPUT);
118                         return ColumnLabelerContributorImpl.this.shouldCreateToolTip(event, input, auxiliary);
119                     }
120
121                     @Override
122                     public Object createToolTipContent(Object event, Object parent, NodeContext nodeContext) {
123                         T input = (T)context.getConstant(BuiltinKeys.INPUT);
124                         try {
125                             return ColumnLabelerContributorImpl.this.createToolTipContentArea(event, parent, input, auxiliary);
126                         } finally {
127                             auxiliary.clear();
128                         }
129                     }
130
131                 };
132
133             }
134
135         };
136
137     }
138
139     @Override
140     public Class<?> getInputClass() {
141         return clazz;
142     }
143
144 }