]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/PropertyViewpointFactory.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 / PropertyViewpointFactory.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 import java.util.HashSet;
17 import java.util.Set;
18
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;
32
33 /**
34  * A basic viewpoint which follows the Has Property relation to show properties
35  * of resources.
36  * 
37  * <p>
38  * The viewpoint generates {@link ResourceArrayProperty} output for Value instances that contain array values.
39  * </p>
40  * 
41  * @see ArrayPropertyValueViewpointFactory viewpoint with IArrayProperty support
42  */
43 public class PropertyViewpointFactory implements ViewpointFactory {
44     @Override
45     public Viewpoint create(PrimitiveQueryUpdater provider, final NodeContext context, ViewpointKey key) {
46         return new LazyViewpoint(provider, context, key) {
47             @Override
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;
55                     if (ra.isEmpty())
56                         return NodeContext.NONE;
57                     input = ra.resources[0];
58                 } else if (in instanceof Statement) {
59                     input = ((Statement) in).getObject();
60                 } else {
61                     return NodeContext.NONE;
62                 }
63
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;
68
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))
75                         continue;
76
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())) {
81                             if (
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)
85                                     && (
86                                            graph.isSubrelationOf(stm.getPredicate(), L0.HasProperty)
87                                            || graph.syncRequest(new IsEnumeratedValue(object))
88                                            )
89                                     )
90                             {
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);
95                                 if (prop != null)
96                                     properties.add(prop);
97                                 else
98                                     properties.add(new RelationContextImpl(input, stm));
99                             }
100                         }
101                     }
102                 }
103
104                 return toContextsWithInput(properties);
105             }
106
107             @Override
108             public Boolean hasChildren(ReadGraph graph) throws DatabaseException {
109                 return children(graph).length > 0;
110             }
111         };
112     }
113
114     @Override
115     public String toString() {
116         return "Properties";
117     }
118
119 }