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 / CheckedStateContributorImpl.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 org.simantics.browsing.ui.BuiltinKeys;
15 import org.simantics.browsing.ui.CheckedState;
16 import org.simantics.browsing.ui.NodeContext;
17 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
18 import org.simantics.browsing.ui.Tester;
19 import org.simantics.browsing.ui.BuiltinKeys.CheckedStateKey;
20 import org.simantics.browsing.ui.content.CheckedStateFactory;
21 import org.simantics.browsing.ui.content.Contributor;
22 import org.simantics.browsing.ui.graph.impl.contribution.CheckedStateContributionImpl;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.utils.ReflectionUtils;
26
27 abstract public class CheckedStateContributorImpl<T> implements Contributor<CheckedStateFactory> {
28
29     abstract public CheckedState getState(ReadGraph graph, T input) throws DatabaseException;
30
31     final private Class<?> clazz;
32
33     @Override
34     public Tester getNodeContextTester() {
35         return null;
36     }
37
38     public CheckedStateContributorImpl() {
39         clazz = ReflectionUtils.getSingleParameterType(getClass());
40     }
41
42     public CheckedStateContributorImpl(Class<?> clazz) {
43         this.clazz = clazz;
44     }
45
46     @Override
47     public CheckedStateFactory getFactory() {
48
49         return new CheckedStateFactory() {
50
51             @Override
52             public CheckedState create(final PrimitiveQueryUpdater updater, NodeContext context, final CheckedStateKey key) {
53
54                 CheckedStateContributionImpl contrib = new CheckedStateContributionImpl(updater, context, key) {
55
56                     @SuppressWarnings("unchecked")
57                                         @Override
58                                         public CheckedState getState(ReadGraph graph, NodeContext context) throws DatabaseException {
59                         T input = (T)context.getConstant(BuiltinKeys.INPUT);
60                                                 return CheckedStateContributorImpl.this.getState(graph, input);
61                                         }
62
63                 };
64                 
65                 return contrib.getState();
66
67             }
68
69         };
70
71     }
72
73     @Override
74     public Class<?> getInputClass() {
75         return clazz;
76     }
77
78 }