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 org.simantics.browsing.ui.BuiltinKeys;
15 import org.simantics.browsing.ui.DataSource;
16 import org.simantics.browsing.ui.NodeContext;
17 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
18 import org.simantics.browsing.ui.common.viewpoints.ViewpointStub;
19 import org.simantics.browsing.ui.content.Viewpoint;
20 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
21 import org.simantics.db.AsyncReadGraph;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.procedure.Listener;
25 import org.simantics.utils.datastructures.Pair;
28 * Implement {@link #children(ReadGraph)} and {@link #hasChildren(ReadGraph)}.
30 * @author Tuukka Lehtonen
32 public abstract class LazyViewpoint extends ViewpointStub {
35 * Needed for separating childQuery and hasChildQuery from each other in the
38 private static final Object CHILDREN = new Object();
40 final private ResourceQuery<NodeContext[]> childQuery;
42 final protected PrimitiveQueryUpdater updater;
43 final protected NodeContext context;
44 final protected BuiltinKeys.ViewpointKey key;
50 public abstract NodeContext[] children(ReadGraph graph) throws DatabaseException;
56 public abstract Boolean hasChildren(ReadGraph graph) throws DatabaseException;
59 * This identity is used to give the back-end graph requests a
60 * <em>properly unique</em> identity that so that the graph back-end caching
61 * and graph explorer node context caching work together properly.
63 * Consider having two graph explorer instances that have the same
64 * configuration (same evaluators) and are showing the same resource from
65 * the graph database. In this case the requests are actually meant to have
66 * an identical identity and performing the graph request will simply bind a
67 * new listener for the one and same request.
69 * @return an additional identity for graph back-end requests to make them
72 public Object getIdentity() {
76 public LazyViewpoint(final PrimitiveQueryUpdater updater, NodeContext context, BuiltinKeys.ViewpointKey key) {
78 assert updater != null;
79 assert context != null;
82 this.updater = updater;
83 this.context = context;
86 this.childQuery = new ResourceQuery<NodeContext[]>(Pair.make(getIdentity(), CHILDREN), context) {
89 public NodeContext[] perform(ReadGraph graph) throws DatabaseException {
90 return children(graph);
94 public String toString() {
95 return LazyViewpoint.this.toString() + "[CHILDREN]";
102 private Listener<NodeContext[]> createListener() {
104 return new Listener<NodeContext[]>() {
106 boolean executed = false;
107 boolean disposed = false;
110 public void execute(NodeContext[] result) {
111 replaceChildrenResult(result);
116 public boolean isDisposed() {
117 if(disposed) return true;
119 if((updater.isDisposed() || !updater.isShown(context)) && executed) {
120 children = Viewpoint.PENDING_CHILDREN;
129 public void exception(Throwable t) {
130 System.out.print("LazyViewpoint.childQuery failed: ");
135 public String toString() {
136 return "LazyViewpoint[" + System.identityHashCode(LazyViewpoint.this) + "].childProcedure";
143 public NodeContext getContext() {
148 public NodeContext[] getChildren() {
150 if (children == Viewpoint.PENDING_CHILDREN) {
151 DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
152 final Listener<NodeContext[]> childProcedure = createListener();
153 source.schedule(graph -> graph.asyncRequest(childQuery, childProcedure));
161 public Boolean getHasChildren() {
162 return getChildren().length > 0;
165 protected void replaceChildrenResult(NodeContext[] result) {
166 setChildren(updater, result);
167 updater.scheduleReplace(context, key, this);
173 * @return input of the specified class
174 * @throws ClassCastException if the input class does not match the
176 * @throws NullPointerException if the input is null
178 @SuppressWarnings("unchecked")
179 protected <T> T getInput(Class<T> clazz) throws ClassCastException {
180 Object o = context.getConstant(BuiltinKeys.INPUT);
182 throw new NullPointerException("null input");
189 * @return <code>null</code> if input is <code>null</code> or if the class does not match
191 @SuppressWarnings("unchecked")
192 protected <T> T tryGetInput(Class<T> clazz) {
193 Object o = context.getConstant(BuiltinKeys.INPUT);
194 if (o != null && clazz.isInstance(o))