]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/symbolcontribution/IndexRootSymbolProviderFactory.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / symbolcontribution / IndexRootSymbolProviderFactory.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram.symbolcontribution;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 \r
17 import org.simantics.db.ReadGraph;\r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.common.request.BinaryRead;\r
20 import org.simantics.db.exception.DatabaseException;\r
21 import org.simantics.db.layer0.adapter.Instances;\r
22 import org.simantics.diagram.stubs.DiagramResource;\r
23 import org.simantics.diagram.symbollibrary.ISymbolGroup;\r
24 import org.simantics.layer0.Layer0;\r
25 \r
26 /**\r
27  * \r
28  * @author Antti Villberg\r
29  */\r
30 public class IndexRootSymbolProviderFactory implements SymbolProviderFactory {\r
31                 \r
32     Resource indexRoot;\r
33     Resource diagram;\r
34 \r
35     public IndexRootSymbolProviderFactory(Resource indexRoot, Resource diagram) {\r
36         this.indexRoot = indexRoot;\r
37         this.diagram = diagram;\r
38     }\r
39 \r
40         @Override\r
41         public int hashCode() {\r
42                 final int prime = 31;\r
43                 int result = 1;\r
44                 result = prime * result + ((diagram == null) ? 0 : diagram.hashCode());\r
45                 result = prime * result + ((indexRoot == null) ? 0 : indexRoot.hashCode());\r
46                 return result;\r
47         }\r
48 \r
49         @Override\r
50         public boolean equals(Object obj) {\r
51                 if (this == obj)\r
52                         return true;\r
53                 if (obj == null)\r
54                         return false;\r
55                 if (getClass() != obj.getClass())\r
56                         return false;\r
57                 IndexRootSymbolProviderFactory other = (IndexRootSymbolProviderFactory) obj;\r
58                 if (diagram == null) {\r
59                         if (other.diagram != null)\r
60                                 return false;\r
61                 } else if (!diagram.equals(other.diagram))\r
62                         return false;\r
63                 if (indexRoot == null) {\r
64                         if (other.indexRoot != null)\r
65                                 return false;\r
66                 } else if (!indexRoot.equals(other.indexRoot))\r
67                         return false;\r
68                 return true;\r
69         }\r
70 \r
71     @Override\r
72     public ISymbolProvider create(ReadGraph g) throws DatabaseException {\r
73         return g.syncRequest(new LoadRequest(indexRoot, diagram));\r
74     }\r
75 \r
76     static class LoadRequest extends BinaryRead<Resource, Resource, ISymbolProvider> {\r
77         public LoadRequest(Resource contribution, Resource diagram) {\r
78             super(contribution, diagram);\r
79         }\r
80         @Override\r
81         public ISymbolProvider perform(ReadGraph graph) throws DatabaseException {\r
82                 \r
83                 Layer0 L0 = Layer0.getInstance(graph);\r
84             DiagramResource dr = DiagramResource.getInstance(graph);\r
85             \r
86             Collection<ISymbolGroup> groups = new ArrayList<ISymbolGroup>();\r
87             \r
88             if (parameter != null) {\r
89                 Instances query = graph.adapt(dr.SymbolReferenceLibrary, Instances.class);\r
90                 for(Resource library : query.find(graph, parameter)) {\r
91                     if (SymbolProviderFactories.accept(graph, dr, library, parameter2)) {\r
92                                 groups.add(BasicSymbolProviderFactory.createGroup(graph, library, L0.IsRelatedTo, parameter2));\r
93                     }\r
94                 }\r
95             }\r
96 \r
97             return new SymbolProvider(groups);\r
98         }\r
99     }\r
100 \r
101     static class SymbolProvider extends AbstractSymbolProvider {\r
102         public SymbolProvider(Collection<ISymbolGroup> groups) {\r
103             super();\r
104             setSymbolGroup(groups);\r
105             lockGroups();\r
106         }\r
107     }\r
108 \r
109 }\r