]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyParametrizedViewpoint.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / LazyParametrizedViewpoint.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.ParametrizedResourceQuery;\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 LazyParametrizedViewpoint 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         private static final Object                              HAS_CHILDREN = new Object();\r
41 \r
42         private final ParametrizedResourceQuery<NodeContext[]> childQuery;\r
43         private final ParametrizedResourceQuery<Boolean>        hasChildQuery;\r
44 \r
45     private final Listener<NodeContext[]>              childQueryProcedure;\r
46     private final Listener<Boolean>                     hasChildQueryProcedure;\r
47 \r
48     private final PrimitiveQueryUpdater                      updater;\r
49     private final NodeContext                               context;\r
50     private final BuiltinKeys.ViewpointKey                   key;\r
51 \r
52         /**\r
53          * @return\r
54          */\r
55         public abstract NodeContext[] children(ReadGraph graph) throws DatabaseException;\r
56 \r
57         /**\r
58          * @param graph\r
59          * @return\r
60          */\r
61         public abstract Boolean hasChildren(ReadGraph graph) throws DatabaseException;\r
62 \r
63 \r
64         public LazyParametrizedViewpoint(PrimitiveQueryUpdater updater, NodeContext context, BuiltinKeys.ViewpointKey key, Object... parameters) {\r
65                 assert updater != null;\r
66                 assert context != null;\r
67                 assert key != null;\r
68 \r
69                 this.updater = updater;\r
70                 this.context = context;\r
71                 this.key = key;\r
72 \r
73                 this.childQuery = new ParametrizedResourceQuery<NodeContext[]>(Pair.make(getClass(), CHILDREN), context, parameters) {\r
74 \r
75                         @Override\r
76                         public NodeContext[] perform(ReadGraph graph) throws DatabaseException {\r
77                                 return  children(graph);\r
78                         }\r
79 \r
80                 };\r
81 \r
82                 this.childQueryProcedure = new Listener<NodeContext[]>() {\r
83 \r
84                         @Override\r
85                         public void execute(NodeContext[] result) {\r
86                                 replaceChildrenResult(result);\r
87                         }\r
88 \r
89                         @Override\r
90                         public boolean isDisposed() {\r
91                                 return LazyParametrizedViewpoint.this.updater.isDisposed();\r
92                         }\r
93 \r
94                         public void exception(Throwable t) {\r
95                                 System.out.print("LazyParametrizedViewpoint2.childQuery failed: ");\r
96                                 t.printStackTrace();\r
97                         }\r
98 \r
99                 };\r
100 \r
101                 this.hasChildQuery = new ParametrizedResourceQuery<Boolean>(Pair.make(getClass(), HAS_CHILDREN), context, parameters) {\r
102 \r
103                         @Override\r
104                         public Boolean perform(ReadGraph graph) throws DatabaseException {\r
105                                 return hasChildren(graph);\r
106                         }\r
107 \r
108                 };\r
109 \r
110                 this.hasChildQueryProcedure = new Listener<Boolean>() {\r
111 \r
112                         @Override\r
113                         public void execute(Boolean result) {\r
114                                 replaceHasChildrenResult(result);\r
115                         }\r
116 \r
117                         @Override\r
118                         public boolean isDisposed() {\r
119                                 return LazyParametrizedViewpoint.this.updater.isDisposed();\r
120                         }\r
121 \r
122                         public void exception(Throwable t) {\r
123                                 System.out.print("LazyParametrizedViewpoint2.hasChildQuery failed: ");\r
124                                 t.printStackTrace();\r
125                         }\r
126 \r
127                 };\r
128 \r
129         }\r
130 \r
131         public NodeContext getContext() {\r
132                 return context;\r
133         }\r
134 \r
135         @Override\r
136         public NodeContext[] getChildren() {\r
137                 if (children == Viewpoint.PENDING_CHILDREN) {\r
138                         DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);\r
139                         if (source != null) {\r
140                                 source.schedule(graph -> graph.asyncRequest(childQuery, childQueryProcedure));\r
141                         }\r
142                 }\r
143 \r
144                 return children;\r
145         }\r
146 \r
147         @Override\r
148         public Boolean getHasChildren() {\r
149                 if (hasChildren == Viewpoint.PENDING_HAS_CHILDREN) {\r
150                         DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);\r
151                         if (source != null) {\r
152                                 source.schedule(new Consumer<ReadGraph>() {\r
153                                         @Override\r
154                                         public void accept(ReadGraph source) {\r
155                                                 source.asyncRequest(hasChildQuery, hasChildQueryProcedure);\r
156                                         }\r
157                                 });\r
158                         }\r
159                 }\r
160 \r
161                 return hasChildren;\r
162         }\r
163 \r
164         protected void replaceChildrenResult(NodeContext[] result) {\r
165                 setChildren(updater, result);\r
166                 updater.scheduleReplace(context, key, this);\r
167         }\r
168 \r
169         protected void replaceHasChildrenResult(Boolean result) {\r
170                 setHasChildren(result);\r
171                 updater.scheduleReplace(context, key, this);\r
172         }\r
173 \r
174         /**\r
175          * @param <T>\r
176          * @param clazz\r
177          * @return input of the specified class\r
178          * @throws ClassCastException if the input class does not match the\r
179          *         specified class\r
180          * @throws NullPointerException if the input is null\r
181          */\r
182          protected <T> T getInput(Class<T> clazz) throws ClassCastException {\r
183                  Object o = context.getConstant(BuiltinKeys.INPUT);\r
184                  if (o == null)\r
185                          throw new NullPointerException("null input");\r
186                  return clazz.cast(o);\r
187          }\r
188 \r
189          /**\r
190           * @param <T>\r
191           * @param clazz\r
192           * @return <code>null</code> if input is <code>null</code> or if the class does not match\r
193           */\r
194          protected <T> T tryGetInput(Class<T> clazz) {\r
195                  Object o = context.getConstant(BuiltinKeys.INPUT);\r
196                  if (o != null && clazz.isInstance(o))\r
197                          return clazz.cast(o);\r
198                  return null;\r
199          }\r
200 \r
201 }\r