]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/TypeIndexViewpointFactory.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 / TypeIndexViewpointFactory.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.HashMap;
17 import java.util.Map;
18 import java.util.function.Consumer;
19
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;
33
34 public class TypeIndexViewpointFactory implements ViewpointFactory {
35
36     @Override
37     public Viewpoint create(final PrimitiveQueryUpdater provider, final NodeContext context, final ViewpointKey key) {
38         assert(provider != null);
39         assert(context != null);
40         
41         final LazyContainerViewpoint result = new LazyContainerViewpoint();
42         
43         DataSource<ReadGraph> source = provider.getDataSource(ReadGraph.class);
44         if (source == null)
45             return result;
46         
47         final Resource inputResource = (Resource) context.getConstant(BuiltinKeys.INPUT);
48         assert(inputResource != null);
49         
50         source.schedule(new Consumer<ReadGraph>() {
51
52             @Override
53             public void accept(ReadGraph source) {
54
55                 try {
56                 
57                         HashMap<Resource, Collection<Resource>> index = new HashMap<Resource, Collection<Resource>>(); 
58                         
59                         Layer0 L0 = Layer0.getInstance(source);
60                         for(Resource child : source.getObjects(inputResource, L0.ConsistsOf)) {
61                             
62                             for(Resource type : source.getObjects(child, L0.InstanceOf)) {
63                                 Collection<Resource> coll = index.get(type);
64                                 if(coll == null) {
65                                     coll = new ArrayList<Resource>();
66                                     index.put(type, coll);
67                                 }
68                                 coll.add(child);
69                             }
70                             
71                         }
72         
73                         NodeContext[] resultContexts = new NodeContext[index.size()];
74                         int index2 = 0;
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));
79                         }
80                         result.setChildren(provider, resultContexts);
81                         result.setHasChildren(resultContexts.length > 0);
82         
83                         provider.scheduleReplace(context, key, result);
84
85                 } catch (DatabaseException e) {
86                         e.printStackTrace();
87                 }
88                         
89             }
90             
91         });
92         
93         return result;
94     }
95     
96     @Override
97     public String toString() {
98         return "Structure by types";
99     }
100     
101 }