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.Arrays;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
20 import org.simantics.databoard.Bindings;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.RequestProcessor;
23 import org.simantics.db.Resource;
24 import org.simantics.db.common.primitiverequest.OrderedSet;
25 import org.simantics.db.common.procedure.wrapper.SetListenerToSingleSetListener;
26 import org.simantics.db.common.request.BinaryRead;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.procedure.SetListener;
29 import org.simantics.diagram.stubs.DiagramResource;
30 import org.simantics.layer0.Layer0;
31 import org.simantics.scenegraph.profile.Group;
34 * @author Tuukka Lehtonen
36 public class TypeGroup implements Group {
38 private final Collection<Resource> types;
39 private final String name;
41 public TypeGroup(String name, Resource type) {
45 public TypeGroup(String name, Resource... types) {
46 this.types = Arrays.asList(types);
51 * Initialize TypeGroup from a DIAGRAM.Group instance.
55 * @throws DatabaseException
57 public TypeGroup(ReadGraph graph, final Resource group) throws DatabaseException {
58 Layer0 L0 = Layer0.getInstance(graph);
59 DiagramResource DIA = DiagramResource.getInstance(graph);
61 this.name = graph.getPossibleRelatedValue(group, L0.HasName, Bindings.STRING);
62 this.types = graph.getObjects(group, DIA.TypeGroup_HasType);
66 public void trackItems(RequestProcessor processor, final Resource runtimeDiagram, final SetListener<Resource> listener) {
67 if (types.isEmpty()) {
68 System.out.println("TypeGroup has no types!");
72 processor.asyncRequest(new BinaryRead<Resource, Collection<Resource>, Collection<Resource>>(runtimeDiagram, types) {
75 public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
76 HashSet<Resource> result = new HashSet<Resource>();
78 Resource realDiagram = graph.getPossibleObject(runtimeDiagram, DiagramResource.getInstance(graph).RuntimeDiagram_HasConfiguration);
79 if (realDiagram == null)
82 // System.out.println("looking for diagram elements of type:");
83 // for (Resource t : types)
84 // System.out.println("\t" + NameUtils.getSafeName(graph, t, true));
86 Collection<Resource> elements = graph.syncRequest(new OrderedSet(realDiagram));
87 for (Resource element : elements) {
88 // System.out.println("checking element " + NameUtils.getSafeName(graph, element, true));
89 Collection<Resource> elementTypes = graph.getTypes(element);
90 if (!Collections.disjoint(types, elementTypes))
97 }, new SetListenerToSingleSetListener<Resource>(listener));
101 public String toString() {
102 return "every '" + name + "'";