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.ArrayList;
\r
15 import java.util.Collection;
\r
17 import org.simantics.browsing.ui.NodeContext;
\r
18 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
\r
19 import org.simantics.browsing.ui.BuiltinKeys.ViewpointKey;
\r
20 import org.simantics.browsing.ui.common.NodeContextBuilder;
\r
21 import org.simantics.db.ReadGraph;
\r
22 import org.simantics.db.Resource;
\r
23 import org.simantics.db.exception.DatabaseException;
\r
24 import org.simantics.layer0.Layer0;
\r
27 * A very simple viewpoint that by default browses Consists Of relations and
\r
28 * accepts as objects any children that are of any of the specified accepted
\r
32 * The accepted types must be returned through {@link #getAcceptedTypes()}
\r
36 * The children production can be customized by overriding
\r
37 * {@link #getPotentialChildren(ReadGraph, Resource)}. Whatever is produced by this
\r
38 * method will be filtered through the list of accepted resource types.
\r
42 * IMPORTANT: it is vital to actually implement this class and override the
\r
43 * {@link #getAcceptedTypes(ReadGraph)} method in the process. Otherwise the
\r
44 * equality comparisons of the resource queries used within LazyViewpoint will
\r
45 * not work properly!
\r
48 * @author Tuukka Lehtonen
\r
50 public abstract class LazyTypeAcceptViewpoint extends LazyViewpoint {
\r
52 public LazyTypeAcceptViewpoint(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
\r
53 super(updater, context, key);
\r
56 protected abstract Resource[] getAcceptedTypes(ReadGraph g);
\r
58 protected Collection<Resource> getPotentialChildren(ReadGraph g, Resource r) throws DatabaseException {
\r
59 Layer0 L0 = Layer0.getInstance(g);
\r
60 return g.getObjects(r, L0.ConsistsOf);
\r
64 public NodeContext[] children(ReadGraph g) throws DatabaseException {
\r
65 Resource input = getInput(Resource.class);
\r
66 Resource[] accepted = getAcceptedTypes(g);
\r
67 Collection<Resource> children = getPotentialChildren(g, input);
\r
68 ArrayList<NodeContext> resultContexts = new ArrayList<NodeContext>();
\r
69 for (Resource child : children) {
\r
70 for (Resource acc : accepted) {
\r
71 if (g.isInstanceOf(child, acc))
\r
72 resultContexts.add(NodeContextBuilder.buildWithInput(child));
\r
75 return resultContexts.toArray(new NodeContext[resultContexts.size()]);
\r
79 public Boolean hasChildren(ReadGraph g) throws DatabaseException {
\r
80 return Boolean.valueOf(children(g).length > 0);
\r
84 public static class Stub extends LazyTypeAcceptViewpoint {
\r
85 Resource[] acceptedTypes;
\r
86 public Stub(PrimitiveQueryUpdater updater, INodeContext context, ViewpointKey key, Resource[] acceptedTypes) {
\r
87 super(updater, context, key);
\r
88 this.acceptedTypes = acceptedTypes;
\r
91 protected Resource[] getAcceptedTypes(Graph g) {
\r
92 return acceptedTypes;
\r