]> gerrit.simantics Code Review - simantics/platform.git/blob
f514fa48c9aeee4daeda1684e2c1e158f47a35cd
[simantics/platform.git] /
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.contribution;
13
14 import java.util.Collection;
15
16 import org.simantics.browsing.ui.BuiltinKeys;
17 import org.simantics.browsing.ui.DataSource;
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.ViewpointContribution;
22 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
23 import org.simantics.db.AsyncReadGraph;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.procedure.Listener;
27 import org.simantics.db.procedure.Procedure;
28 import org.simantics.utils.ui.ErrorLogger;
29
30 /**
31  * Implement {@link #children(AsyncReadGraph)} and {@link #hasChildren(AsyncReadGraph)}.
32  * 
33  * @author Tuukka Lehtonen
34  */
35 abstract public class FinalViewpointContributionImpl extends ContributionStub implements GraphContribution {
36
37     final private ResourceQuery<Collection<NodeContext>> childQuery;
38     final private Procedure<Collection<NodeContext>>     childProcedure;
39
40     final protected PrimitiveQueryUpdater       updater;
41     final private NodeContext                context;
42     final private BuiltinKeys.ViewpointContributionKey    key;
43
44     /**
45      * This identity is used to give the back-end graph requests a
46      * <em>properly unique</em> identity that so that the graph back-end caching
47      * and graph explorer node context caching work together properly.
48      * 
49      * Consider having two graph explorer instances that have the same
50      * configuration (same evaluators) and are showing the same resource from
51      * the graph database. In this case the requests are actually meant to have
52      * an identical identity and performing the graph request will simply bind a
53      * new listener for the one and same request.
54      * 
55      * @return an additional identity for graph back-end requests to make them
56      *         properly unique
57      */
58     public Object getIdentity() {
59         return key;
60     }
61
62     public FinalViewpointContributionImpl(final PrimitiveQueryUpdater updater, NodeContext context, BuiltinKeys.ViewpointContributionKey key) {
63
64         assert updater != null;
65         assert context != null;
66         assert key != null;
67
68         this.updater = updater;
69         this.context = context;
70         this.key = key;
71
72         this.childQuery = new ResourceQuery<Collection<NodeContext>>(getIdentity(), context) {
73
74             @Override
75             public Collection<NodeContext> perform(ReadGraph graph) throws DatabaseException {
76                 try {
77                     // Make sure that null is not returned.
78                     Collection<NodeContext> result = children(graph, context);
79                     if (result == null)
80                         throw new NullPointerException("LazyContributionImpl.children is not allowed to return null, but " + FinalViewpointContributionImpl.this.getClass() + " just did it");
81                     return result;
82                 } catch (DatabaseException e) {
83                     throw e;
84                 } catch (Throwable t) {
85                     ErrorLogger.defaultLogError("LazyContributionImpl.childQuery produced unexpected exception.", t);
86                     return ViewpointContribution.NO_CONTRIBUTION;
87                 }
88             }
89
90             @Override
91             public String toString() {
92                 return "LazyContributionImpl[" + System.identityHashCode(FinalViewpointContributionImpl.this) + "].childQuery";
93             }
94
95         };
96
97         childProcedure = createProcedure();
98         
99     }
100     
101     protected Procedure<Collection<NodeContext>> createProcedure() {
102
103         return new Procedure<Collection<NodeContext>>() {
104
105             @Override
106             public void execute(Collection<NodeContext> result) {
107                 replaceChildrenResult(result);
108             }
109
110             public void exception(Throwable t) {
111                 ErrorLogger.defaultLogError("LazyContributionImpl.childQuery failed, see exception for details.", t);
112             }
113
114             @Override
115             public String toString() {
116                 return "LazyContributionImpl[" + System.identityHashCode(FinalViewpointContributionImpl.this) + "].childProcedure";
117             }
118
119         };
120         
121     }
122
123     public NodeContext getContext() {
124         return context;
125     }
126
127     @Override
128     public Collection<NodeContext> getContribution() {
129
130         //System.out.println("LazyViewpoint2@" + System.identityHashCode(this) + " getChildren() = " + children.length);
131
132         if (children == org.simantics.browsing.ui.content.ViewpointContribution.PENDING_CONTRIBUTION) {
133             DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
134             if (source != null) {
135                 source.schedule(graph -> {
136                     if(childProcedure instanceof Listener<?>)
137                         graph.asyncRequest(childQuery, (Listener<Collection<NodeContext>>)childProcedure);
138                     else 
139                         graph.asyncRequest(childQuery, childProcedure);
140                 });
141             }
142         }
143
144         //System.out.println("LazyViewpoint.getChildren returns " + children);
145
146         return children;
147
148     }
149
150     protected void replaceChildrenResult(Collection<NodeContext> result) {
151         setChildren(updater, result);
152         updater.scheduleReplace(context, key, this);
153     }
154
155     /**
156      * @param <T>
157      * @param clazz
158      * @return input of the specified class
159      * @throws ClassCastException if the input class does not match the
160      *         specified class
161      * @throws NullPointerException if the input is null
162      */
163     @SuppressWarnings("unchecked")
164     protected <T> T getInput(Class<T> clazz) throws ClassCastException {
165         Object o = context.getConstant(BuiltinKeys.INPUT);
166         if (o == null)
167             throw new NullPointerException("null input");
168 //        return clazz.cast(o);
169         return (T) o;
170     }
171
172     /**
173      * @param <T>
174      * @param clazz
175      * @return <code>null</code> if input is <code>null</code> or if the class does not match
176      */
177     @SuppressWarnings("unchecked")
178     protected <T> T tryGetInput(Class<T> clazz) {
179         Object o = context.getConstant(BuiltinKeys.INPUT);
180         if (o != null && clazz.isInstance(o))
181 //            return clazz.cast(o);
182             return (T) o;
183         return null;
184     }
185
186     @Override
187     public Tester getNodeContextTester() {
188         return null;
189     }
190
191 }