]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contributor/imager/ImagerContributorImpl.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 / imager / ImagerContributorImpl.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.imager;
13
14 import java.util.Map;
15
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.simantics.browsing.ui.BuiltinKeys;
18 import org.simantics.browsing.ui.BuiltinKeys.ImagerKey;
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.common.ColumnKeys;
23 import org.simantics.browsing.ui.content.Contributor;
24 import org.simantics.browsing.ui.content.Imager;
25 import org.simantics.browsing.ui.content.ImagerFactory;
26 import org.simantics.browsing.ui.graph.impl.contribution.ImagerContributionImpl;
27 import org.simantics.db.ReadGraph;
28 import org.simantics.db.exception.DatabaseException;
29 import org.simantics.utils.ReflectionUtils;
30 import org.simantics.utils.datastructures.ArrayMap;
31
32 abstract public class ImagerContributorImpl<T> implements Contributor<ImagerFactory> {
33
34     abstract public ImageDescriptor getDescriptor(ReadGraph graph, T input) throws DatabaseException;
35
36     final private Class<?> clazz;
37
38     @Override
39     public String toString() {
40         return getClass().getName();
41     }
42
43     @Override
44     public Tester getNodeContextTester() {
45         return null;
46     }
47
48     public ImagerContributorImpl() {
49         clazz = ReflectionUtils.getSingleParameterType(getClass());
50     }
51
52     public ImagerContributorImpl(Class<?> clazz) {
53         this.clazz = clazz;
54     }
55
56     @Override
57     public ImagerFactory getFactory() {
58
59         return new ImagerFactory() {
60
61             @Override
62             public String toString() {
63                 return ImagerContributorImpl.this.toString();
64             }
65
66             @SuppressWarnings("unchecked")
67             @Override
68             public Imager create(final PrimitiveQueryUpdater updater, NodeContext context, final ImagerKey key) {
69
70                 return new ImagerContributionImpl(updater, context, key) {
71
72                     @Override
73                     public Map<String, ImageDescriptor> getDescriptors(ReadGraph graph, NodeContext context) throws DatabaseException {
74
75                         T input = (T)context.getConstant(BuiltinKeys.INPUT);
76
77                         ImageDescriptor value = ImagerContributorImpl.this.getDescriptor(graph, input);
78
79                         return new ArrayMap<String, ImageDescriptor>(ColumnKeys.KEYS_SINGLE,
80                                 new ImageDescriptor[] { value });
81
82                     }
83
84                 };
85
86             }
87
88         };
89
90     }
91
92     @Override
93     public Class<?> getInputClass() {
94         return clazz;
95     }
96
97 }