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;
16 import java.util.HashSet;
19 import org.simantics.browsing.ui.BuiltinKeys.ViewpointKey;
20 import org.simantics.browsing.ui.NodeContext;
21 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
22 import org.simantics.browsing.ui.common.property.IProperty;
23 import org.simantics.browsing.ui.content.Viewpoint;
24 import org.simantics.browsing.ui.content.ViewpointFactory;
25 import org.simantics.db.ReadGraph;
26 import org.simantics.db.Resource;
27 import org.simantics.db.Statement;
28 import org.simantics.db.common.ResourceArray;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.db.impl.RelationContextImpl;
31 import org.simantics.layer0.Layer0;
34 * A basic viewpoint which follows the Has Property relation to show properties
38 * The viewpoint generates {@link ResourceArrayProperty} output for Value instances that contain array values.
41 * @see ArrayPropertyValueViewpointFactory viewpoint with IArrayProperty support
43 public class PropertyViewpointFactory implements ViewpointFactory {
45 public Viewpoint create(PrimitiveQueryUpdater provider, final NodeContext context, ViewpointKey key) {
46 return new LazyViewpoint(provider, context, key) {
48 public NodeContext[] children(ReadGraph graph) throws DatabaseException {
49 Object in = getInput(Object.class);
50 Resource input = null;
51 if (in instanceof Resource) {
52 input = (Resource) in;
53 } else if (in instanceof ResourceArray) {
54 ResourceArray ra = (ResourceArray) in;
56 return NodeContext.NONE;
57 input = ra.resources[0];
58 } else if (in instanceof Statement) {
59 input = ((Statement) in).getObject();
61 return NodeContext.NONE;
64 // // First see if this is a Value resource.
65 // NodeContext[] valueChildren = GraphPropertyUtil.tryValueGetChildren(graph, input, new ResourcePropertyFactory(IProperty.Type.DIRECT, input));
66 // if (valueChildren != null)
67 // return valueChildren;
69 // If not, then go look for properties, either direct or asserted.
70 Collection<Object> properties = new ArrayList<Object>();
71 Set<Object> usedProperties = new HashSet<Object>();
72 Layer0 L0 = Layer0.getInstance(graph);
73 for (Resource predicate : graph.getPredicates(input)) {
74 if (!graph.isSubrelationOf(predicate, L0.IsRelatedTo))
77 for (Statement stm : graph.getStatements(input, predicate)) {
78 Resource object = stm.getObject();
79 //System.out.println("STM: " + NameUtils.toString(graph, stm));
80 if (usedProperties.add(stm.getPredicate())) {
82 // [TLe] Added to keep data that can't be
83 // visualized anyway out of the basic property view.
84 graph.isInstanceOf(stm.getObject(), L0.Literal)
86 graph.isSubrelationOf(stm.getPredicate(), L0.HasProperty)
87 || graph.syncRequest(new IsEnumeratedValue(object))
91 IProperty.Type type = stm.getSubject().equals(input) ? IProperty.Type.DIRECT : IProperty.Type.ASSERTED;
92 //System.out.println("\tTYPE: " + type);
93 IProperty prop = GraphPropertyUtil.tryCreateProperty(graph, object, new ResourcePropertyFactory(type, input, stm.getPredicate(), object, ResourceArray.EMPTY));
94 //System.out.println("\tPROP: " + prop);
98 properties.add(new RelationContextImpl(input, stm));
104 return toContextsWithInput(properties);
108 public Boolean hasChildren(ReadGraph graph) throws DatabaseException {
109 return children(graph).length > 0;
115 public String toString() {