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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.graph.impl;
\r
14 import java.util.function.Consumer;
\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
29 * Implement {@link #children(ReadGraph)} and {@link #hasChildren(ReadGraph)}.
\r
31 * @author Tuukka Lehtonen
\r
33 public abstract class LazyParametrizedViewpoint extends ViewpointStub {
\r
36 * Needed for separating childQuery and hasChildQuery from each other in the
\r
39 private static final Object CHILDREN = new Object();
\r
40 private static final Object HAS_CHILDREN = new Object();
\r
42 private final ParametrizedResourceQuery<NodeContext[]> childQuery;
\r
43 private final ParametrizedResourceQuery<Boolean> hasChildQuery;
\r
45 private final Listener<NodeContext[]> childQueryProcedure;
\r
46 private final Listener<Boolean> hasChildQueryProcedure;
\r
48 private final PrimitiveQueryUpdater updater;
\r
49 private final NodeContext context;
\r
50 private final BuiltinKeys.ViewpointKey key;
\r
55 public abstract NodeContext[] children(ReadGraph graph) throws DatabaseException;
\r
61 public abstract Boolean hasChildren(ReadGraph graph) throws DatabaseException;
\r
64 public LazyParametrizedViewpoint(PrimitiveQueryUpdater updater, NodeContext context, BuiltinKeys.ViewpointKey key, Object... parameters) {
\r
65 assert updater != null;
\r
66 assert context != null;
\r
69 this.updater = updater;
\r
70 this.context = context;
\r
73 this.childQuery = new ParametrizedResourceQuery<NodeContext[]>(Pair.make(getClass(), CHILDREN), context, parameters) {
\r
76 public NodeContext[] perform(ReadGraph graph) throws DatabaseException {
\r
77 return children(graph);
\r
82 this.childQueryProcedure = new Listener<NodeContext[]>() {
\r
85 public void execute(NodeContext[] result) {
\r
86 replaceChildrenResult(result);
\r
90 public boolean isDisposed() {
\r
91 return LazyParametrizedViewpoint.this.updater.isDisposed();
\r
94 public void exception(Throwable t) {
\r
95 System.out.print("LazyParametrizedViewpoint2.childQuery failed: ");
\r
96 t.printStackTrace();
\r
101 this.hasChildQuery = new ParametrizedResourceQuery<Boolean>(Pair.make(getClass(), HAS_CHILDREN), context, parameters) {
\r
104 public Boolean perform(ReadGraph graph) throws DatabaseException {
\r
105 return hasChildren(graph);
\r
110 this.hasChildQueryProcedure = new Listener<Boolean>() {
\r
113 public void execute(Boolean result) {
\r
114 replaceHasChildrenResult(result);
\r
118 public boolean isDisposed() {
\r
119 return LazyParametrizedViewpoint.this.updater.isDisposed();
\r
122 public void exception(Throwable t) {
\r
123 System.out.print("LazyParametrizedViewpoint2.hasChildQuery failed: ");
\r
124 t.printStackTrace();
\r
131 public NodeContext getContext() {
\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
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
154 public void accept(ReadGraph source) {
\r
155 source.asyncRequest(hasChildQuery, hasChildQueryProcedure);
\r
161 return hasChildren;
\r
164 protected void replaceChildrenResult(NodeContext[] result) {
\r
165 setChildren(updater, result);
\r
166 updater.scheduleReplace(context, key, this);
\r
169 protected void replaceHasChildrenResult(Boolean result) {
\r
170 setHasChildren(result);
\r
171 updater.scheduleReplace(context, key, this);
\r
177 * @return input of the specified class
\r
178 * @throws ClassCastException if the input class does not match the
\r
180 * @throws NullPointerException if the input is null
\r
182 protected <T> T getInput(Class<T> clazz) throws ClassCastException {
\r
183 Object o = context.getConstant(BuiltinKeys.INPUT);
\r
185 throw new NullPointerException("null input");
\r
186 return clazz.cast(o);
\r
192 * @return <code>null</code> if input is <code>null</code> or if the class does not match
\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