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.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
29 * Implement {@link #children(ReadGraph)} and {@link #hasChildren(ReadGraph)}.
\r
31 * @author Tuukka Lehtonen
\r
33 public abstract class LazyViewpoint 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
41 final private ResourceQuery<NodeContext[]> childQuery;
\r
43 final protected PrimitiveQueryUpdater updater;
\r
44 final protected NodeContext context;
\r
45 final protected BuiltinKeys.ViewpointKey key;
\r
51 public abstract NodeContext[] children(ReadGraph graph) throws DatabaseException;
\r
57 public abstract Boolean hasChildren(ReadGraph graph) throws DatabaseException;
\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
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
70 * @return an additional identity for graph back-end requests to make them
\r
73 public Object getIdentity() {
\r
77 public LazyViewpoint(final PrimitiveQueryUpdater updater, NodeContext context, BuiltinKeys.ViewpointKey key) {
\r
79 assert updater != null;
\r
80 assert context != null;
\r
83 this.updater = updater;
\r
84 this.context = context;
\r
87 this.childQuery = new ResourceQuery<NodeContext[]>(Pair.make(getIdentity(), CHILDREN), context) {
\r
90 public NodeContext[] perform(ReadGraph graph) throws DatabaseException {
\r
91 return children(graph);
\r
95 public String toString() {
\r
96 return LazyViewpoint.this.toString() + "[CHILDREN]";
\r
103 private Listener<NodeContext[]> createListener() {
\r
105 return new Listener<NodeContext[]>() {
\r
107 boolean executed = false;
\r
108 boolean disposed = false;
\r
111 public void execute(NodeContext[] result) {
\r
112 replaceChildrenResult(result);
\r
117 public boolean isDisposed() {
\r
118 if(disposed) return true;
\r
120 if((updater.isDisposed() || !updater.isShown(context)) && executed) {
\r
121 children = Viewpoint.PENDING_CHILDREN;
\r
130 public void exception(Throwable t) {
\r
131 System.out.print("LazyViewpoint.childQuery failed: ");
\r
132 t.printStackTrace();
\r
136 public String toString() {
\r
137 return "LazyViewpoint[" + System.identityHashCode(LazyViewpoint.this) + "].childProcedure";
\r
144 public NodeContext getContext() {
\r
149 public NodeContext[] getChildren() {
\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
156 public void accept(ReadGraph source) {
\r
157 source.asyncRequest(childQuery, childProcedure);
\r
167 public Boolean getHasChildren() {
\r
168 return getChildren().length > 0;
\r
171 protected void replaceChildrenResult(NodeContext[] result) {
\r
172 setChildren(updater, result);
\r
173 updater.scheduleReplace(context, key, this);
\r
179 * @return input of the specified class
\r
180 * @throws ClassCastException if the input class does not match the
\r
182 * @throws NullPointerException if the input is null
\r
184 @SuppressWarnings("unchecked")
\r
185 protected <T> T getInput(Class<T> clazz) throws ClassCastException {
\r
186 Object o = context.getConstant(BuiltinKeys.INPUT);
\r
188 throw new NullPointerException("null input");
\r
195 * @return <code>null</code> if input is <code>null</code> or if the class does not match
\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