]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/MultiInputPropertyViewpointFactory.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / MultiInputPropertyViewpointFactory.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
17 import org.simantics.browsing.ui.BuiltinKeys;
18 import org.simantics.browsing.ui.NodeContext;
19 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
20 import org.simantics.browsing.ui.BuiltinKeys.ViewpointKey;
21 import org.simantics.browsing.ui.common.NodeContextBuilder;
22 import org.simantics.browsing.ui.content.Viewpoint;
23 import org.simantics.browsing.ui.content.ViewpointFactory;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.Statement;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.layer0.Layer0;
29
30 /**
31  * A first test version of a viewpoint that provides editing of the shared
32  * properties of multiple input resources.
33  */
34 public class MultiInputPropertyViewpointFactory implements ViewpointFactory {
35
36     @Override
37     public Viewpoint create(PrimitiveQueryUpdater provider, final NodeContext context, ViewpointKey key) {
38
39         assert(provider != null);
40         assert(context != null);
41
42         return new LazyViewpoint(provider, context, key) {
43
44             @Override
45             public NodeContext[] children(ReadGraph graph) throws DatabaseException {
46                 Resource[] rs = (Resource[]) context.getConstant(BuiltinKeys.INPUT);
47                 if(rs.length == 0) return new NodeContext[0];
48                 if(rs.length == 1) return new NodeContext[0];
49                 Resource r = rs[0];
50                 Layer0 l0 = Layer0.getInstance(graph);
51                 Resource type = graph.getSingleType(r, l0.Entity);
52                 for(int i=1;i<rs.length;i++) {
53                     Resource type2 = graph.getSingleType(rs[i], l0.Entity);
54                     if(!type.equals(type2)) return new NodeContext[0];
55                 }
56
57                 Collection<Statement> children = graph.getStatements(r, l0.HasProperty);
58
59                 ArrayList<NodeContext> resultContexts = new ArrayList<NodeContext>();
60
61                 for(Statement child : children) {
62
63                     ArrayList<Resource> objects = new ArrayList<Resource>();
64
65                     objects.add(child.getObject());
66
67                     boolean ok = true;
68                     for(int i=1;i<rs.length;i++) {
69                         Collection<Resource> objects2 = graph.getObjects(rs[1], child.getPredicate());
70                         if(objects2.size() != 1) {
71                             ok = false;
72                             break;
73                         }
74                         objects.add(objects2.iterator().next());
75                     }
76                     if(ok) {
77                         resultContexts.add(NodeContextBuilder.buildWithInput(new PropertyArray(child.getPredicate(), objects)));
78                     }
79
80                 }
81
82                 return resultContexts.toArray(new NodeContext[resultContexts.size()]);
83             }
84
85             @Override
86             public Boolean hasChildren(ReadGraph graph) throws DatabaseException {
87                 return Boolean.valueOf(children(graph).length > 0);
88             }
89         };
90     }
91
92     @Override
93     public String toString() {
94         return "Properties";
95     }
96
97 }