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.HashMap;
18 import java.util.function.Consumer;
20 import org.simantics.browsing.ui.BuiltinKeys;
21 import org.simantics.browsing.ui.BuiltinKeys.ViewpointKey;
22 import org.simantics.browsing.ui.DataSource;
23 import org.simantics.browsing.ui.NodeContext;
24 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
25 import org.simantics.browsing.ui.common.NodeContextBuilder;
26 import org.simantics.browsing.ui.common.viewpoints.LazyContainerViewpoint;
27 import org.simantics.browsing.ui.content.Viewpoint;
28 import org.simantics.browsing.ui.content.ViewpointFactory;
29 import org.simantics.db.ReadGraph;
30 import org.simantics.db.Resource;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.layer0.Layer0;
34 public class TypeIndexViewpointFactory implements ViewpointFactory {
37 public Viewpoint create(final PrimitiveQueryUpdater provider, final NodeContext context, final ViewpointKey key) {
38 assert(provider != null);
39 assert(context != null);
41 final LazyContainerViewpoint result = new LazyContainerViewpoint();
43 DataSource<ReadGraph> source = provider.getDataSource(ReadGraph.class);
47 final Resource inputResource = (Resource) context.getConstant(BuiltinKeys.INPUT);
48 assert(inputResource != null);
50 source.schedule(new Consumer<ReadGraph>() {
53 public void accept(ReadGraph source) {
57 HashMap<Resource, Collection<Resource>> index = new HashMap<Resource, Collection<Resource>>();
59 Layer0 L0 = Layer0.getInstance(source);
60 for(Resource child : source.getObjects(inputResource, L0.ConsistsOf)) {
62 for(Resource type : source.getObjects(child, L0.InstanceOf)) {
63 Collection<Resource> coll = index.get(type);
65 coll = new ArrayList<Resource>();
66 index.put(type, coll);
73 NodeContext[] resultContexts = new NodeContext[index.size()];
75 for(Map.Entry<Resource, Collection<Resource>> e : index.entrySet()) {
76 Resource type = e.getKey();
77 String rep = source.adapt(type, String.class);
78 resultContexts[index2++] = NodeContextBuilder.buildWithInput(new TypeIndex(type, e.getValue(), rep));
80 result.setChildren(provider, resultContexts);
81 result.setHasChildren(resultContexts.length > 0);
83 provider.scheduleReplace(context, key, result);
85 } catch (DatabaseException e) {
97 public String toString() {
98 return "Structure by types";