]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/MappedTypeGroup.java
6e7d368b58d61629015f080237a07e5f3205ec17
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / MappedTypeGroup.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.diagram.adapter;
13
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Set;
21
22 import org.simantics.databoard.Bindings;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.RequestProcessor;
25 import org.simantics.db.Resource;
26 import org.simantics.db.common.primitiverequest.OrderedSet;
27 import org.simantics.db.common.procedure.wrapper.SetListenerToSingleSetListener;
28 import org.simantics.db.common.request.BinaryRead;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.db.procedure.SetListener;
31 import org.simantics.diagram.stubs.DiagramResource;
32 import org.simantics.layer0.Layer0;
33 import org.simantics.modeling.ModelingResources;
34 import org.simantics.scenegraph.profile.Group;
35
36 /**
37  * @author Tuukka Lehtonen
38  */
39 public class MappedTypeGroup implements Group {
40
41     private final List<Resource> types;
42     private final String         name;
43
44     public MappedTypeGroup(String name, Resource type) {
45         this(name);
46     }
47
48     public MappedTypeGroup(String name, Resource... types) {
49         this.types = Arrays.asList(types);
50         this.name = name;
51     }
52
53     /**
54      * Initialize TypeGroup from a DIAGRAM.Group instance.
55      * 
56      * @param graph
57      * @param group
58      * @throws DatabaseException
59      */
60     public MappedTypeGroup(ReadGraph graph, final Resource group) throws DatabaseException {
61         Layer0 L0 = Layer0.getInstance(graph);
62         DiagramResource DIA = DiagramResource.getInstance(graph);
63
64         this.name = graph.getPossibleRelatedValue(group, L0.HasName, Bindings.STRING);
65         this.types = new ArrayList<Resource>();
66         this.types.addAll(graph.getObjects(group, DIA.TypeGroup_HasType));
67         
68     }
69
70     @Override
71     public void trackItems(RequestProcessor processor, final Resource runtimeDiagram, final SetListener<Resource> listener) throws DatabaseException {
72         if (types.isEmpty()) {
73             System.out.println("MappedTypeGroup has no types!");
74             return;
75         }
76
77         processor.syncRequest(new BinaryRead<Resource, Collection<Resource>, Collection<Resource>>(runtimeDiagram, types) {
78
79             @Override
80             public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
81                 HashSet<Resource> result = new HashSet<Resource>();
82
83                 Resource realDiagram = graph.getPossibleObject(runtimeDiagram, DiagramResource.getInstance(graph).RuntimeDiagram_HasConfiguration);
84                 if (realDiagram == null)
85                     return result;
86
87                 ModelingResources MOD = ModelingResources.getInstance(graph);
88
89 //                System.out.println("looking for diagram elements of type:");
90 //                for (Resource t : types)
91 //                    System.out.println("\t" + NameUtils.getSafeName(graph, t, true));
92
93                 Collection<Resource> elements = graph.syncRequest(new OrderedSet(realDiagram));
94                 for (Resource element : elements) {
95 //                    System.out.println("checking element " + NameUtils.getSafeName(graph, element, true));
96                     Resource mapped = graph.getPossibleObject(element, MOD.ElementToComponent);
97 //                    System.out.println("checking mapped component " + NameUtils.getSafeName(graph, mapped, true));
98                     if(mapped != null) {
99                         Collection<Resource> mappedTypes = graph.getTypes(mapped);
100 //                        for (Resource t : mappedTypes)
101 //                            System.out.println("\t" + NameUtils.getSafeName(graph, t, true));
102                         if (!Collections.disjoint(types, mappedTypes))
103                             result.add(element);
104                     } else {
105                         Resource referenceRelation = graph.getPossibleObject(element, MOD.HasReferenceRelation);
106                         if (referenceRelation != null) {
107                             Collection<Resource> referencedTypes = graph.getObjects(referenceRelation, MOD.HasReferencedType);
108                             if (!Collections.disjoint(types, referencedTypes)) {
109                                 result.add(element);
110                             } else {
111                                 for (Resource referencedType : referencedTypes) {
112                                     Set<Resource> allTypes = graph.getSupertypes(referencedType);
113                                     if (!Collections.disjoint(types, allTypes))
114                                         result.add(element);
115                                 }
116                             }
117                         }
118                     }
119                 }
120
121                 return result;
122             }
123
124         }, new SetListenerToSingleSetListener<Resource>(listener));
125     }
126
127     @Override
128     public int hashCode() {
129         return types.hashCode();
130     }
131
132     @Override
133     public boolean equals(Object object) {
134         if (this == object)
135             return true;
136         else if (object == null)
137             return false;
138         else if (MappedTypeGroup.class != object.getClass())
139             return false;
140         MappedTypeGroup other = (MappedTypeGroup)object;
141         return types.equals(other.types);
142     }
143
144     @Override
145     public String toString() {
146         return "every '" + name + "'";
147     }
148
149 }