]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/GraphViewpointFactory.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / GraphViewpointFactory.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.Collection;
15
16 import org.simantics.browsing.ui.BuiltinKeys;
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.content.Viewpoint;
21 import org.simantics.browsing.ui.content.ViewpointFactory;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.exception.DatabaseException;
25
26 public abstract class GraphViewpointFactory implements ViewpointFactory {
27
28     @Override
29     public Viewpoint create(PrimitiveQueryUpdater updater,
30             NodeContext context, ViewpointKey key) {
31         assert(updater != null);
32         assert(context != null);
33
34         return new LazyViewpoint(updater, context, key) {
35             @Override
36             public NodeContext[] children(ReadGraph graph) throws DatabaseException {
37                 return toContextsWithInput(GraphViewpointFactory.this.
38                         getChildren(graph, (Resource) context.getConstant(BuiltinKeys.INPUT)));
39             }
40
41             @Override
42             public Boolean hasChildren(ReadGraph graph) throws DatabaseException {
43                 return children(graph).length > 0;
44             }
45
46             @Override
47             public String toString() {
48                 return GraphViewpointFactory.this.toString();
49             }
50
51             @Override
52             public Object getIdentity() {
53                 return GraphViewpointFactory.this.getClass();
54             }
55
56         };
57     }
58
59     abstract protected Collection<?> getChildren(ReadGraph g, Resource r) throws DatabaseException;
60     protected boolean hasChildren(ReadGraph g, Resource r) throws DatabaseException {
61         return !getChildren(g, r).isEmpty();
62     }
63
64 }