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;
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;
31 * A first test version of a viewpoint that provides editing of the shared
32 * properties of multiple input resources.
34 public class MultiInputPropertyViewpointFactory implements ViewpointFactory {
37 public Viewpoint create(PrimitiveQueryUpdater provider, final NodeContext context, ViewpointKey key) {
39 assert(provider != null);
40 assert(context != null);
42 return new LazyViewpoint(provider, context, key) {
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];
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];
57 Collection<Statement> children = graph.getStatements(r, l0.HasProperty);
59 ArrayList<NodeContext> resultContexts = new ArrayList<NodeContext>();
61 for(Statement child : children) {
63 ArrayList<Resource> objects = new ArrayList<Resource>();
65 objects.add(child.getObject());
68 for(int i=1;i<rs.length;i++) {
69 Collection<Resource> objects2 = graph.getObjects(rs[1], child.getPredicate());
70 if(objects2.size() != 1) {
74 objects.add(objects2.iterator().next());
77 resultContexts.add(NodeContextBuilder.buildWithInput(new PropertyArray(child.getPredicate(), objects)));
82 return resultContexts.toArray(new NodeContext[resultContexts.size()]);
86 public Boolean hasChildren(ReadGraph graph) throws DatabaseException {
87 return Boolean.valueOf(children(graph).length > 0);
93 public String toString() {