]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyTypeAcceptViewpoint.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / LazyTypeAcceptViewpoint.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.graph.impl;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 \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
25 \r
26 /**\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
29  * types.\r
30  * \r
31  * <p>\r
32  * The accepted types must be returned through {@link #getAcceptedTypes()}\r
33  * </p>\r
34  * \r
35  * <p>\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
39  * </p>\r
40  * \r
41  * <p>\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
46  * </p>\r
47  * \r
48  * @author Tuukka Lehtonen\r
49  */\r
50 public abstract class LazyTypeAcceptViewpoint extends LazyViewpoint {\r
51     \r
52     public LazyTypeAcceptViewpoint(PrimitiveQueryUpdater updater, NodeContext context, ViewpointKey key) {\r
53         super(updater, context, key);\r
54     }\r
55 \r
56     protected abstract Resource[] getAcceptedTypes(ReadGraph g);\r
57     \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
61     }\r
62 \r
63     @Override\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
73             }\r
74         }\r
75         return resultContexts.toArray(new NodeContext[resultContexts.size()]);\r
76     }\r
77 \r
78     @Override\r
79     public Boolean hasChildren(ReadGraph g) throws DatabaseException {\r
80         return Boolean.valueOf(children(g).length > 0);\r
81     }\r
82 \r
83     /*\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
89         }\r
90         @Override\r
91         protected Resource[] getAcceptedTypes(Graph g) {\r
92             return acceptedTypes;\r
93         }\r
94     }\r
95     */\r
96 }\r