]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/PropertyViewpointFactory.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / PropertyViewpointFactory.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 import java.util.HashSet;\r
17 import java.util.Set;\r
18 \r
19 import org.simantics.browsing.ui.BuiltinKeys.ViewpointKey;\r
20 import org.simantics.browsing.ui.NodeContext;\r
21 import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
22 import org.simantics.browsing.ui.common.property.IProperty;\r
23 import org.simantics.browsing.ui.content.Viewpoint;\r
24 import org.simantics.browsing.ui.content.ViewpointFactory;\r
25 import org.simantics.db.ReadGraph;\r
26 import org.simantics.db.Resource;\r
27 import org.simantics.db.Statement;\r
28 import org.simantics.db.common.ResourceArray;\r
29 import org.simantics.db.exception.DatabaseException;\r
30 import org.simantics.db.impl.RelationContextImpl;\r
31 import org.simantics.layer0.Layer0;\r
32 \r
33 /**\r
34  * A basic viewpoint which follows the Has Property relation to show properties\r
35  * of resources.\r
36  * \r
37  * <p>\r
38  * The viewpoint generates {@link ResourceArrayProperty} output for Value instances that contain array values.\r
39  * </p>\r
40  * \r
41  * @see ArrayPropertyValueViewpointFactory viewpoint with IArrayProperty support\r
42  */\r
43 public class PropertyViewpointFactory implements ViewpointFactory {\r
44     @Override\r
45     public Viewpoint create(PrimitiveQueryUpdater provider, final NodeContext context, ViewpointKey key) {\r
46         return new LazyViewpoint(provider, context, key) {\r
47             @Override\r
48             public NodeContext[] children(ReadGraph graph) throws DatabaseException {\r
49                 Object in = getInput(Object.class);\r
50                 Resource input = null;\r
51                 if (in instanceof Resource) {\r
52                     input = (Resource) in;\r
53                 } else if (in instanceof ResourceArray) {\r
54                     ResourceArray ra = (ResourceArray) in;\r
55                     if (ra.isEmpty())\r
56                         return NodeContext.NONE;\r
57                     input = ra.resources[0];\r
58                 } else if (in instanceof Statement) {\r
59                     input = ((Statement) in).getObject();\r
60                 } else {\r
61                     return NodeContext.NONE;\r
62                 }\r
63 \r
64 //                // First see if this is a Value resource.\r
65 //                NodeContext[] valueChildren = GraphPropertyUtil.tryValueGetChildren(graph, input, new ResourcePropertyFactory(IProperty.Type.DIRECT, input));\r
66 //                if (valueChildren != null)\r
67 //                    return valueChildren;\r
68 \r
69                 // If not, then go look for properties, either direct or asserted.\r
70                 Collection<Object> properties = new ArrayList<Object>();\r
71                 Set<Object> usedProperties = new HashSet<Object>();\r
72                 Layer0 L0 = Layer0.getInstance(graph);\r
73                 for (Resource predicate : graph.getPredicates(input)) {\r
74                     if (!graph.isSubrelationOf(predicate, L0.IsRelatedTo))\r
75                         continue;\r
76 \r
77                     for (Statement stm : graph.getStatements(input, predicate)) {\r
78                         Resource object = stm.getObject();\r
79                         //System.out.println("STM: " + NameUtils.toString(graph, stm));\r
80                         if (usedProperties.add(stm.getPredicate())) {\r
81                             if (\r
82                                     // [TLe] Added to keep data that can't be\r
83                                     // visualized anyway out of the basic property view.\r
84                                     graph.isInstanceOf(stm.getObject(), L0.Literal)\r
85                                     && (\r
86                                            graph.isSubrelationOf(stm.getPredicate(), L0.HasProperty)\r
87                                            || graph.syncRequest(new IsEnumeratedValue(object))\r
88                                            )\r
89                                     )\r
90                             {\r
91                                 IProperty.Type type = stm.getSubject().equals(input) ? IProperty.Type.DIRECT : IProperty.Type.ASSERTED;\r
92                                 //System.out.println("\tTYPE: " + type);\r
93                                 IProperty prop = GraphPropertyUtil.tryCreateProperty(graph, object, new ResourcePropertyFactory(type, input, stm.getPredicate(), object, ResourceArray.EMPTY));\r
94                                 //System.out.println("\tPROP: " + prop);\r
95                                 if (prop != null)\r
96                                     properties.add(prop);\r
97                                 else\r
98                                     properties.add(new RelationContextImpl(input, stm));\r
99                             }\r
100                         }\r
101                     }\r
102                 }\r
103 \r
104                 return toContextsWithInput(properties);\r
105             }\r
106 \r
107             @Override\r
108             public Boolean hasChildren(ReadGraph graph) throws DatabaseException {\r
109                 return children(graph).length > 0;\r
110             }\r
111         };\r
112     }\r
113 \r
114     @Override\r
115     public String toString() {\r
116         return "Properties";\r
117     }\r
118 \r
119 }\r