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