-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.browsing.ui.graph.impl;\r
-\r
-import java.util.function.Consumer;\r
-\r
-import org.simantics.browsing.ui.BuiltinKeys;\r
-import org.simantics.browsing.ui.DataSource;\r
-import org.simantics.browsing.ui.NodeContext;\r
-import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
-import org.simantics.browsing.ui.common.viewpoints.ViewpointStub;\r
-import org.simantics.browsing.ui.content.Viewpoint;\r
-import org.simantics.browsing.ui.graph.impl.request.ParametrizedResourceQuery;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.procedure.Listener;\r
-import org.simantics.utils.datastructures.Pair;\r
-\r
-/**\r
- * Implement {@link #children(ReadGraph)} and {@link #hasChildren(ReadGraph)}.\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-public abstract class LazyParametrizedViewpoint extends ViewpointStub {\r
-\r
- /**\r
- * Needed for separating childQuery and hasChildQuery from each other in the\r
- * equals sense.\r
- */\r
- private static final Object CHILDREN = new Object();\r
- private static final Object HAS_CHILDREN = new Object();\r
-\r
- private final ParametrizedResourceQuery<NodeContext[]> childQuery;\r
- private final ParametrizedResourceQuery<Boolean> hasChildQuery;\r
-\r
- private final Listener<NodeContext[]> childQueryProcedure;\r
- private final Listener<Boolean> hasChildQueryProcedure;\r
-\r
- private final PrimitiveQueryUpdater updater;\r
- private final NodeContext context;\r
- private final BuiltinKeys.ViewpointKey key;\r
-\r
- /**\r
- * @return\r
- */\r
- public abstract NodeContext[] children(ReadGraph graph) throws DatabaseException;\r
-\r
- /**\r
- * @param graph\r
- * @return\r
- */\r
- public abstract Boolean hasChildren(ReadGraph graph) throws DatabaseException;\r
-\r
-\r
- public LazyParametrizedViewpoint(PrimitiveQueryUpdater updater, NodeContext context, BuiltinKeys.ViewpointKey key, Object... parameters) {\r
- assert updater != null;\r
- assert context != null;\r
- assert key != null;\r
-\r
- this.updater = updater;\r
- this.context = context;\r
- this.key = key;\r
-\r
- this.childQuery = new ParametrizedResourceQuery<NodeContext[]>(Pair.make(getClass(), CHILDREN), context, parameters) {\r
-\r
- @Override\r
- public NodeContext[] perform(ReadGraph graph) throws DatabaseException {\r
- return children(graph);\r
- }\r
-\r
- };\r
-\r
- this.childQueryProcedure = new Listener<NodeContext[]>() {\r
-\r
- @Override\r
- public void execute(NodeContext[] result) {\r
- replaceChildrenResult(result);\r
- }\r
-\r
- @Override\r
- public boolean isDisposed() {\r
- return LazyParametrizedViewpoint.this.updater.isDisposed();\r
- }\r
-\r
- public void exception(Throwable t) {\r
- System.out.print("LazyParametrizedViewpoint2.childQuery failed: ");\r
- t.printStackTrace();\r
- }\r
-\r
- };\r
-\r
- this.hasChildQuery = new ParametrizedResourceQuery<Boolean>(Pair.make(getClass(), HAS_CHILDREN), context, parameters) {\r
-\r
- @Override\r
- public Boolean perform(ReadGraph graph) throws DatabaseException {\r
- return hasChildren(graph);\r
- }\r
-\r
- };\r
-\r
- this.hasChildQueryProcedure = new Listener<Boolean>() {\r
-\r
- @Override\r
- public void execute(Boolean result) {\r
- replaceHasChildrenResult(result);\r
- }\r
-\r
- @Override\r
- public boolean isDisposed() {\r
- return LazyParametrizedViewpoint.this.updater.isDisposed();\r
- }\r
-\r
- public void exception(Throwable t) {\r
- System.out.print("LazyParametrizedViewpoint2.hasChildQuery failed: ");\r
- t.printStackTrace();\r
- }\r
-\r
- };\r
-\r
- }\r
-\r
- public NodeContext getContext() {\r
- return context;\r
- }\r
-\r
- @Override\r
- public NodeContext[] getChildren() {\r
- if (children == Viewpoint.PENDING_CHILDREN) {\r
- DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);\r
- if (source != null) {\r
- source.schedule(graph -> graph.asyncRequest(childQuery, childQueryProcedure));\r
- }\r
- }\r
-\r
- return children;\r
- }\r
-\r
- @Override\r
- public Boolean getHasChildren() {\r
- if (hasChildren == Viewpoint.PENDING_HAS_CHILDREN) {\r
- DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);\r
- if (source != null) {\r
- source.schedule(new Consumer<ReadGraph>() {\r
- @Override\r
- public void accept(ReadGraph source) {\r
- source.asyncRequest(hasChildQuery, hasChildQueryProcedure);\r
- }\r
- });\r
- }\r
- }\r
-\r
- return hasChildren;\r
- }\r
-\r
- protected void replaceChildrenResult(NodeContext[] result) {\r
- setChildren(updater, result);\r
- updater.scheduleReplace(context, key, this);\r
- }\r
-\r
- protected void replaceHasChildrenResult(Boolean result) {\r
- setHasChildren(result);\r
- updater.scheduleReplace(context, key, this);\r
- }\r
-\r
- /**\r
- * @param <T>\r
- * @param clazz\r
- * @return input of the specified class\r
- * @throws ClassCastException if the input class does not match the\r
- * specified class\r
- * @throws NullPointerException if the input is null\r
- */\r
- protected <T> T getInput(Class<T> clazz) throws ClassCastException {\r
- Object o = context.getConstant(BuiltinKeys.INPUT);\r
- if (o == null)\r
- throw new NullPointerException("null input");\r
- return clazz.cast(o);\r
- }\r
-\r
- /**\r
- * @param <T>\r
- * @param clazz\r
- * @return <code>null</code> if input is <code>null</code> or if the class does not match\r
- */\r
- protected <T> T tryGetInput(Class<T> clazz) {\r
- Object o = context.getConstant(BuiltinKeys.INPUT);\r
- if (o != null && clazz.isInstance(o))\r
- return clazz.cast(o);\r
- return null;\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.browsing.ui.graph.impl;
+
+import java.util.function.Consumer;
+
+import org.simantics.browsing.ui.BuiltinKeys;
+import org.simantics.browsing.ui.DataSource;
+import org.simantics.browsing.ui.NodeContext;
+import org.simantics.browsing.ui.PrimitiveQueryUpdater;
+import org.simantics.browsing.ui.common.viewpoints.ViewpointStub;
+import org.simantics.browsing.ui.content.Viewpoint;
+import org.simantics.browsing.ui.graph.impl.request.ParametrizedResourceQuery;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.procedure.Listener;
+import org.simantics.utils.datastructures.Pair;
+
+/**
+ * Implement {@link #children(ReadGraph)} and {@link #hasChildren(ReadGraph)}.
+ *
+ * @author Tuukka Lehtonen
+ */
+public abstract class LazyParametrizedViewpoint extends ViewpointStub {
+
+ /**
+ * Needed for separating childQuery and hasChildQuery from each other in the
+ * equals sense.
+ */
+ private static final Object CHILDREN = new Object();
+ private static final Object HAS_CHILDREN = new Object();
+
+ private final ParametrizedResourceQuery<NodeContext[]> childQuery;
+ private final ParametrizedResourceQuery<Boolean> hasChildQuery;
+
+ private final Listener<NodeContext[]> childQueryProcedure;
+ private final Listener<Boolean> hasChildQueryProcedure;
+
+ private final PrimitiveQueryUpdater updater;
+ private final NodeContext context;
+ private final BuiltinKeys.ViewpointKey key;
+
+ /**
+ * @return
+ */
+ public abstract NodeContext[] children(ReadGraph graph) throws DatabaseException;
+
+ /**
+ * @param graph
+ * @return
+ */
+ public abstract Boolean hasChildren(ReadGraph graph) throws DatabaseException;
+
+
+ public LazyParametrizedViewpoint(PrimitiveQueryUpdater updater, NodeContext context, BuiltinKeys.ViewpointKey key, Object... parameters) {
+ assert updater != null;
+ assert context != null;
+ assert key != null;
+
+ this.updater = updater;
+ this.context = context;
+ this.key = key;
+
+ this.childQuery = new ParametrizedResourceQuery<NodeContext[]>(Pair.make(getClass(), CHILDREN), context, parameters) {
+
+ @Override
+ public NodeContext[] perform(ReadGraph graph) throws DatabaseException {
+ return children(graph);
+ }
+
+ };
+
+ this.childQueryProcedure = new Listener<NodeContext[]>() {
+
+ @Override
+ public void execute(NodeContext[] result) {
+ replaceChildrenResult(result);
+ }
+
+ @Override
+ public boolean isDisposed() {
+ return LazyParametrizedViewpoint.this.updater.isDisposed();
+ }
+
+ public void exception(Throwable t) {
+ System.out.print("LazyParametrizedViewpoint2.childQuery failed: ");
+ t.printStackTrace();
+ }
+
+ };
+
+ this.hasChildQuery = new ParametrizedResourceQuery<Boolean>(Pair.make(getClass(), HAS_CHILDREN), context, parameters) {
+
+ @Override
+ public Boolean perform(ReadGraph graph) throws DatabaseException {
+ return hasChildren(graph);
+ }
+
+ };
+
+ this.hasChildQueryProcedure = new Listener<Boolean>() {
+
+ @Override
+ public void execute(Boolean result) {
+ replaceHasChildrenResult(result);
+ }
+
+ @Override
+ public boolean isDisposed() {
+ return LazyParametrizedViewpoint.this.updater.isDisposed();
+ }
+
+ public void exception(Throwable t) {
+ System.out.print("LazyParametrizedViewpoint2.hasChildQuery failed: ");
+ t.printStackTrace();
+ }
+
+ };
+
+ }
+
+ public NodeContext getContext() {
+ return context;
+ }
+
+ @Override
+ public NodeContext[] getChildren() {
+ if (children == Viewpoint.PENDING_CHILDREN) {
+ DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
+ if (source != null) {
+ source.schedule(graph -> graph.asyncRequest(childQuery, childQueryProcedure));
+ }
+ }
+
+ return children;
+ }
+
+ @Override
+ public Boolean getHasChildren() {
+ if (hasChildren == Viewpoint.PENDING_HAS_CHILDREN) {
+ DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
+ if (source != null) {
+ source.schedule(new Consumer<ReadGraph>() {
+ @Override
+ public void accept(ReadGraph source) {
+ source.asyncRequest(hasChildQuery, hasChildQueryProcedure);
+ }
+ });
+ }
+ }
+
+ return hasChildren;
+ }
+
+ protected void replaceChildrenResult(NodeContext[] result) {
+ setChildren(updater, result);
+ updater.scheduleReplace(context, key, this);
+ }
+
+ protected void replaceHasChildrenResult(Boolean result) {
+ setHasChildren(result);
+ updater.scheduleReplace(context, key, this);
+ }
+
+ /**
+ * @param <T>
+ * @param clazz
+ * @return input of the specified class
+ * @throws ClassCastException if the input class does not match the
+ * specified class
+ * @throws NullPointerException if the input is null
+ */
+ protected <T> T getInput(Class<T> clazz) throws ClassCastException {
+ Object o = context.getConstant(BuiltinKeys.INPUT);
+ if (o == null)
+ throw new NullPointerException("null input");
+ return clazz.cast(o);
+ }
+
+ /**
+ * @param <T>
+ * @param clazz
+ * @return <code>null</code> if input is <code>null</code> or if the class does not match
+ */
+ protected <T> T tryGetInput(Class<T> clazz) {
+ Object o = context.getConstant(BuiltinKeys.INPUT);
+ if (o != null && clazz.isInstance(o))
+ return clazz.cast(o);
+ return null;
+ }
+
+}