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