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.symbolcontribution;
14 import java.util.ArrayList;
15 import java.util.Collection;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.request.BinaryRead;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.adapter.Instances;
22 import org.simantics.diagram.stubs.DiagramResource;
23 import org.simantics.diagram.symbollibrary.ISymbolGroup;
24 import org.simantics.layer0.Layer0;
28 * @author Antti Villberg
30 public class IndexRootSymbolProviderFactory implements SymbolProviderFactory {
35 public IndexRootSymbolProviderFactory(Resource indexRoot, Resource diagram) {
36 this.indexRoot = indexRoot;
37 this.diagram = diagram;
41 public int hashCode() {
44 result = prime * result + ((diagram == null) ? 0 : diagram.hashCode());
45 result = prime * result + ((indexRoot == null) ? 0 : indexRoot.hashCode());
50 public boolean equals(Object obj) {
55 if (getClass() != obj.getClass())
57 IndexRootSymbolProviderFactory other = (IndexRootSymbolProviderFactory) obj;
58 if (diagram == null) {
59 if (other.diagram != null)
61 } else if (!diagram.equals(other.diagram))
63 if (indexRoot == null) {
64 if (other.indexRoot != null)
66 } else if (!indexRoot.equals(other.indexRoot))
72 public ISymbolProvider create(ReadGraph g) throws DatabaseException {
73 return g.syncRequest(new LoadRequest(indexRoot, diagram));
76 static class LoadRequest extends BinaryRead<Resource, Resource, ISymbolProvider> {
77 public LoadRequest(Resource contribution, Resource diagram) {
78 super(contribution, diagram);
81 public ISymbolProvider perform(ReadGraph graph) throws DatabaseException {
83 Layer0 L0 = Layer0.getInstance(graph);
84 DiagramResource dr = DiagramResource.getInstance(graph);
86 Collection<ISymbolGroup> groups = new ArrayList<ISymbolGroup>();
88 if (parameter != null) {
89 Instances query = graph.adapt(dr.SymbolReferenceLibrary, Instances.class);
90 for(Resource library : query.find(graph, parameter)) {
91 if (SymbolProviderFactories.accept(graph, dr, library, parameter2)) {
92 groups.add(BasicSymbolProviderFactory.createGroup(graph, library, L0.IsRelatedTo, parameter2));
97 return new SymbolProvider(groups);
101 static class SymbolProvider extends AbstractSymbolProvider {
102 public SymbolProvider(Collection<ISymbolGroup> groups) {
104 setSymbolGroup(groups);