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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.diagram.symbolcontribution;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\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
28 * @author Antti Villberg
\r
30 public class IndexRootSymbolProviderFactory implements SymbolProviderFactory {
\r
35 public IndexRootSymbolProviderFactory(Resource indexRoot, Resource diagram) {
\r
36 this.indexRoot = indexRoot;
\r
37 this.diagram = diagram;
\r
41 public int hashCode() {
\r
42 final int prime = 31;
\r
44 result = prime * result + ((diagram == null) ? 0 : diagram.hashCode());
\r
45 result = prime * result + ((indexRoot == null) ? 0 : indexRoot.hashCode());
\r
50 public boolean equals(Object obj) {
\r
55 if (getClass() != obj.getClass())
\r
57 IndexRootSymbolProviderFactory other = (IndexRootSymbolProviderFactory) obj;
\r
58 if (diagram == null) {
\r
59 if (other.diagram != null)
\r
61 } else if (!diagram.equals(other.diagram))
\r
63 if (indexRoot == null) {
\r
64 if (other.indexRoot != null)
\r
66 } else if (!indexRoot.equals(other.indexRoot))
\r
72 public ISymbolProvider create(ReadGraph g) throws DatabaseException {
\r
73 return g.syncRequest(new LoadRequest(indexRoot, diagram));
\r
76 static class LoadRequest extends BinaryRead<Resource, Resource, ISymbolProvider> {
\r
77 public LoadRequest(Resource contribution, Resource diagram) {
\r
78 super(contribution, diagram);
\r
81 public ISymbolProvider perform(ReadGraph graph) throws DatabaseException {
\r
83 Layer0 L0 = Layer0.getInstance(graph);
\r
84 DiagramResource dr = DiagramResource.getInstance(graph);
\r
86 Collection<ISymbolGroup> groups = new ArrayList<ISymbolGroup>();
\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
97 return new SymbolProvider(groups);
\r
101 static class SymbolProvider extends AbstractSymbolProvider {
\r
102 public SymbolProvider(Collection<ISymbolGroup> groups) {
\r
104 setSymbolGroup(groups);
\r