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.diagram.adapter;
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;
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;
37 * @author Tuukka Lehtonen
39 public class MappedTypeGroup implements Group {
41 private final List<Resource> types;
42 private final String name;
44 public MappedTypeGroup(String name, Resource type) {
48 public MappedTypeGroup(String name, Resource... types) {
49 this.types = Arrays.asList(types);
54 * Initialize TypeGroup from a DIAGRAM.Group instance.
58 * @throws DatabaseException
60 public MappedTypeGroup(ReadGraph graph, final Resource group) throws DatabaseException {
61 Layer0 L0 = Layer0.getInstance(graph);
62 DiagramResource DIA = DiagramResource.getInstance(graph);
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));
71 public void trackItems(RequestProcessor processor, final Resource runtimeDiagram, final SetListener<Resource> listener) {
72 if (types.isEmpty()) {
73 System.out.println("MappedTypeGroup has no types!");
77 processor.asyncRequest(new BinaryRead<Resource, Collection<Resource>, Collection<Resource>>(runtimeDiagram, types) {
80 public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
81 HashSet<Resource> result = new HashSet<Resource>();
83 Resource realDiagram = graph.getPossibleObject(runtimeDiagram, DiagramResource.getInstance(graph).RuntimeDiagram_HasConfiguration);
84 if (realDiagram == null)
87 ModelingResources MOD = ModelingResources.getInstance(graph);
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));
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));
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))
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)) {
111 for (Resource referencedType : referencedTypes) {
112 Set<Resource> allTypes = graph.getSupertypes(referencedType);
113 if (!Collections.disjoint(types, allTypes))
124 }, new SetListenerToSingleSetListener<Resource>(listener));
128 public int hashCode() {
129 return types.hashCode();
133 public boolean equals(Object object) {
136 else if (object == null)
138 else if (MappedTypeGroup.class != object.getClass())
140 MappedTypeGroup other = (MappedTypeGroup)object;
141 return types.equals(other.types);
145 public String toString() {
146 return "every '" + name + "'";