]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/TypeGroup.java
Fix diagram profiles to work with latest DB changes
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / TypeGroup.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.Arrays;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.Set;
19
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;
32
33 /**
34  * @author Tuukka Lehtonen
35  */
36 public class TypeGroup implements Group {
37
38     private final Collection<Resource> types;
39     private final String               name;
40
41     public TypeGroup(String name, Resource type) {
42         this(name);
43     }
44
45     public TypeGroup(String name, Resource... types) {
46         this.types = Arrays.asList(types);
47         this.name = name;
48     }
49
50     @Override
51     public int hashCode() {
52         final int prime = 31;
53         int result = 1;
54         result = prime * result + ((types == null) ? 0 : types.hashCode());
55         return result;
56     }
57
58     @Override
59     public boolean equals(Object obj) {
60         if (this == obj)
61             return true;
62         if (obj == null)
63             return false;
64         if (getClass() != obj.getClass())
65             return false;
66         TypeGroup other = (TypeGroup) obj;
67         if (types == null) {
68             if (other.types != null)
69                 return false;
70         } else if (!types.equals(other.types))
71             return false;
72         return true;
73     }
74
75     /**
76      * Initialize TypeGroup from a DIAGRAM.Group instance.
77      * 
78      * @param graph
79      * @param group
80      * @throws DatabaseException
81      */
82     public TypeGroup(ReadGraph graph, final Resource group) throws DatabaseException {
83         Layer0 L0 = Layer0.getInstance(graph);
84         DiagramResource DIA = DiagramResource.getInstance(graph);
85
86         this.name = graph.getPossibleRelatedValue(group, L0.HasName, Bindings.STRING);
87         this.types = graph.getObjects(group, DIA.TypeGroup_HasType);
88     }
89
90     @Override
91     public void trackItems(RequestProcessor processor, final Resource runtimeDiagram, final SetListener<Resource> listener) throws DatabaseException {
92         if (types.isEmpty()) {
93             System.out.println("TypeGroup has no types!");
94             return;
95         }
96
97         processor.syncRequest(new BinaryRead<Resource, Collection<Resource>, Collection<Resource>>(runtimeDiagram, types) {
98
99             @Override
100             public Set<Resource> perform(ReadGraph graph) throws DatabaseException {
101                 HashSet<Resource> result = new HashSet<Resource>();
102
103                 Resource realDiagram = graph.getPossibleObject(runtimeDiagram, DiagramResource.getInstance(graph).RuntimeDiagram_HasConfiguration);
104                 if (realDiagram == null)
105                     return result;
106
107 //                System.out.println("looking for diagram elements of type:");
108 //                for (Resource t : types)
109 //                    System.out.println("\t" + NameUtils.getSafeName(graph, t, true));
110
111                 Collection<Resource> elements = graph.syncRequest(new OrderedSet(realDiagram));
112                 for (Resource element : elements) {
113 //                    System.out.println("checking element " + NameUtils.getSafeName(graph, element, true));
114                     Collection<Resource> elementTypes = graph.getTypes(element);
115                     if (!Collections.disjoint(types, elementTypes))
116                         result.add(element);
117                 }
118
119                 return result;
120             }
121
122         }, new SetListenerToSingleSetListener<Resource>(listener));
123     }
124
125     @Override
126     public String toString() {
127         return "every '" + name + "'";
128     }
129
130 }