]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyTypeAcceptViewpoint.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / LazyTypeAcceptViewpoint.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16
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;
25
26 /**
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
29  * types.
30  * 
31  * <p>
32  * The accepted types must be returned through {@link #getAcceptedTypes()}
33  * </p>
34  * 
35  * <p>
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.
39  * </p>
40  * 
41  * <p>
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
45  * not work properly!
46  * </p>
47  * 
48  * @author Tuukka Lehtonen
49  */
50 public abstract class LazyTypeAcceptViewpoint extends LazyViewpoint {
51     
52     public LazyTypeAcceptViewpoint(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {
53         super(updater, context, key);
54     }
55
56     protected abstract Resource[] getAcceptedTypes(ReadGraph g);
57     
58     protected Collection<Resource> getPotentialChildren(ReadGraph g, Resource r) throws DatabaseException {
59         Layer0 L0 = Layer0.getInstance(g);
60         return g.getObjects(r, L0.ConsistsOf);
61     }
62
63     @Override
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));
73             }
74         }
75         return resultContexts.toArray(new NodeContext[resultContexts.size()]);
76     }
77
78     @Override
79     public Boolean hasChildren(ReadGraph g) throws DatabaseException {
80         return Boolean.valueOf(children(g).length > 0);
81     }
82
83     /*
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;
89         }
90         @Override
91         protected Resource[] getAcceptedTypes(Graph g) {
92             return acceptedTypes;
93         }
94     }
95     */
96 }