1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl;
14 import java.util.function.Consumer;
16 import org.simantics.browsing.ui.BuiltinKeys;
17 import org.simantics.browsing.ui.DataSource;
18 import org.simantics.browsing.ui.NodeContext;
19 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
20 import org.simantics.browsing.ui.common.viewpoints.ViewpointStub;
21 import org.simantics.browsing.ui.content.Viewpoint;
22 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.procedure.Listener;
26 import org.simantics.utils.datastructures.Pair;
29 * Implement {@link #children(ReadGraph)} and {@link #hasChildren(ReadGraph)}.
31 * @author Tuukka Lehtonen
33 public abstract class LazyViewpoint extends ViewpointStub {
36 * Needed for separating childQuery and hasChildQuery from each other in the
39 private static final Object CHILDREN = new Object();
41 final private ResourceQuery<NodeContext[]> childQuery;
43 final protected PrimitiveQueryUpdater updater;
44 final protected NodeContext context;
45 final protected BuiltinKeys.ViewpointKey key;
51 public abstract NodeContext[] children(ReadGraph graph) throws DatabaseException;
57 public abstract Boolean hasChildren(ReadGraph graph) throws DatabaseException;
60 * This identity is used to give the back-end graph requests a
61 * <em>properly unique</em> identity that so that the graph back-end caching
62 * and graph explorer node context caching work together properly.
64 * Consider having two graph explorer instances that have the same
65 * configuration (same evaluators) and are showing the same resource from
66 * the graph database. In this case the requests are actually meant to have
67 * an identical identity and performing the graph request will simply bind a
68 * new listener for the one and same request.
70 * @return an additional identity for graph back-end requests to make them
73 public Object getIdentity() {
77 public LazyViewpoint(final PrimitiveQueryUpdater updater, NodeContext context, BuiltinKeys.ViewpointKey key) {
79 assert updater != null;
80 assert context != null;
83 this.updater = updater;
84 this.context = context;
87 this.childQuery = new ResourceQuery<NodeContext[]>(Pair.make(getIdentity(), CHILDREN), context) {
90 public NodeContext[] perform(ReadGraph graph) throws DatabaseException {
91 return children(graph);
95 public String toString() {
96 return LazyViewpoint.this.toString() + "[CHILDREN]";
103 private Listener<NodeContext[]> createListener() {
105 return new Listener<NodeContext[]>() {
107 boolean executed = false;
108 boolean disposed = false;
111 public void execute(NodeContext[] result) {
112 replaceChildrenResult(result);
117 public boolean isDisposed() {
118 if(disposed) return true;
120 if((updater.isDisposed() || !updater.isShown(context)) && executed) {
121 children = Viewpoint.PENDING_CHILDREN;
130 public void exception(Throwable t) {
131 System.out.print("LazyViewpoint.childQuery failed: ");
136 public String toString() {
137 return "LazyViewpoint[" + System.identityHashCode(LazyViewpoint.this) + "].childProcedure";
144 public NodeContext getContext() {
149 public NodeContext[] getChildren() {
151 if (children == Viewpoint.PENDING_CHILDREN) {
152 DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
153 final Listener<NodeContext[]> childProcedure = createListener();
154 source.schedule(new Consumer<ReadGraph>() {
156 public void accept(ReadGraph source) {
157 source.asyncRequest(childQuery, childProcedure);
167 public Boolean getHasChildren() {
168 return getChildren().length > 0;
171 protected void replaceChildrenResult(NodeContext[] result) {
172 setChildren(updater, result);
173 updater.scheduleReplace(context, key, this);
179 * @return input of the specified class
180 * @throws ClassCastException if the input class does not match the
182 * @throws NullPointerException if the input is null
184 @SuppressWarnings("unchecked")
185 protected <T> T getInput(Class<T> clazz) throws ClassCastException {
186 Object o = context.getConstant(BuiltinKeys.INPUT);
188 throw new NullPointerException("null input");
195 * @return <code>null</code> if input is <code>null</code> or if the class does not match
197 @SuppressWarnings("unchecked")
198 protected <T> T tryGetInput(Class<T> clazz) {
199 Object o = context.getConstant(BuiltinKeys.INPUT);
200 if (o != null && clazz.isInstance(o))