]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyViewpoint.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / LazyViewpoint.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;\r
13 \r
14 import java.util.function.Consumer;\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.common.viewpoints.ViewpointStub;\r
21 import org.simantics.browsing.ui.content.Viewpoint;\r
22 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;\r
23 import org.simantics.db.ReadGraph;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.db.procedure.Listener;\r
26 import org.simantics.utils.datastructures.Pair;\r
27 \r
28 /**\r
29  * Implement {@link #children(ReadGraph)} and {@link #hasChildren(ReadGraph)}.\r
30  * \r
31  * @author Tuukka Lehtonen\r
32  */\r
33 public abstract class LazyViewpoint extends ViewpointStub {\r
34 \r
35     /**\r
36      * Needed for separating childQuery and hasChildQuery from each other in the\r
37      * equals sense.\r
38      */\r
39     private static final Object                 CHILDREN     = new Object();\r
40 \r
41     final private ResourceQuery<NodeContext[]> childQuery;\r
42 \r
43     final protected PrimitiveQueryUpdater       updater;\r
44     final protected NodeContext                context;\r
45     final protected BuiltinKeys.ViewpointKey    key;\r
46 \r
47     /**\r
48      * @param graph\r
49      * @return\r
50      */\r
51     public abstract NodeContext[] children(ReadGraph graph) throws DatabaseException;\r
52 \r
53     /**\r
54      * @param graph\r
55      * @return\r
56      */\r
57     public abstract Boolean hasChildren(ReadGraph graph) throws DatabaseException;\r
58 \r
59     /**\r
60      * This identity is used to give the back-end graph requests a\r
61      * <em>properly unique</em> identity that so that the graph back-end caching\r
62      * and graph explorer node context caching work together properly.\r
63      * \r
64      * Consider having two graph explorer instances that have the same\r
65      * configuration (same evaluators) and are showing the same resource from\r
66      * the graph database. In this case the requests are actually meant to have\r
67      * an identical identity and performing the graph request will simply bind a\r
68      * new listener for the one and same request.\r
69      * \r
70      * @return an additional identity for graph back-end requests to make them\r
71      *         properly unique\r
72      */\r
73     public Object getIdentity() {\r
74         return key;\r
75     }\r
76 \r
77     public LazyViewpoint(final PrimitiveQueryUpdater updater, NodeContext context, BuiltinKeys.ViewpointKey key) {\r
78 \r
79         assert updater != null;\r
80         assert context != null;\r
81         assert key != null;\r
82 \r
83         this.updater = updater;\r
84         this.context = context;\r
85         this.key = key;\r
86 \r
87         this.childQuery = new ResourceQuery<NodeContext[]>(Pair.make(getIdentity(), CHILDREN), context) {\r
88 \r
89             @Override\r
90             public NodeContext[] perform(ReadGraph graph) throws DatabaseException {\r
91                 return children(graph);\r
92             }\r
93 \r
94             @Override\r
95             public String toString() {\r
96                 return LazyViewpoint.this.toString() + "[CHILDREN]";\r
97             }\r
98 \r
99         };\r
100 \r
101     }\r
102 \r
103     private Listener<NodeContext[]> createListener() {\r
104         \r
105         return new Listener<NodeContext[]>() {\r
106 \r
107                 boolean executed = false;\r
108                 boolean disposed = false;\r
109                 \r
110             @Override\r
111             public void execute(NodeContext[] result) {\r
112                 replaceChildrenResult(result);\r
113                 executed = true;\r
114             }\r
115 \r
116             @Override\r
117             public boolean isDisposed() {\r
118                 if(disposed) return true;\r
119 \r
120                 if((updater.isDisposed() || !updater.isShown(context)) && executed) {\r
121                         children = Viewpoint.PENDING_CHILDREN;\r
122                         disposed = true;\r
123                         return true;\r
124                 } else {\r
125                         return false;\r
126                 }\r
127                 \r
128             }\r
129 \r
130             public void exception(Throwable t) {\r
131                 System.out.print("LazyViewpoint.childQuery failed: ");\r
132                 t.printStackTrace();\r
133             }\r
134 \r
135             @Override\r
136             public String toString() {\r
137                 return "LazyViewpoint[" + System.identityHashCode(LazyViewpoint.this) + "].childProcedure";\r
138             }\r
139 \r
140         };\r
141         \r
142     }\r
143     \r
144     public NodeContext getContext() {\r
145         return context;\r
146     }\r
147 \r
148     @Override\r
149     public NodeContext[] getChildren() {\r
150 \r
151         if (children == Viewpoint.PENDING_CHILDREN) {\r
152             DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);\r
153             final Listener<NodeContext[]> childProcedure = createListener();\r
154             source.schedule(new Consumer<ReadGraph>() {\r
155                 @Override\r
156                 public void accept(ReadGraph source) {\r
157                     source.asyncRequest(childQuery, childProcedure);\r
158                 }\r
159             });\r
160         }\r
161 \r
162         return children;\r
163 \r
164     }\r
165 \r
166     @Override\r
167     public Boolean getHasChildren() {\r
168         return getChildren().length > 0;\r
169     }\r
170 \r
171     protected void replaceChildrenResult(NodeContext[] result) {\r
172         setChildren(updater, result);\r
173         updater.scheduleReplace(context, key, this);\r
174     }\r
175 \r
176     /**\r
177      * @param <T>\r
178      * @param clazz\r
179      * @return input of the specified class\r
180      * @throws ClassCastException if the input class does not match the\r
181      *         specified class\r
182      * @throws NullPointerException if the input is null\r
183      */\r
184     @SuppressWarnings("unchecked")\r
185     protected <T> T getInput(Class<T> clazz) throws ClassCastException {\r
186         Object o = context.getConstant(BuiltinKeys.INPUT);\r
187         if (o == null)\r
188             throw new NullPointerException("null input");\r
189         return (T) o;\r
190     }\r
191 \r
192     /**\r
193      * @param <T>\r
194      * @param clazz\r
195      * @return <code>null</code> if input is <code>null</code> or if the class does not match\r
196      */\r
197     @SuppressWarnings("unchecked")\r
198     protected <T> T tryGetInput(Class<T> clazz) {\r
199         Object o = context.getConstant(BuiltinKeys.INPUT);\r
200         if (o != null && clazz.isInstance(o))\r
201             return (T) o;\r
202         return null;\r
203     }\r
204 \r
205 }\r