]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/symbolcontribution/IndexRootSymbolProviderFactory.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / symbolcontribution / IndexRootSymbolProviderFactory.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.symbolcontribution;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16
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;
25
26 /**
27  * 
28  * @author Antti Villberg
29  */
30 public class IndexRootSymbolProviderFactory implements SymbolProviderFactory {
31                 
32     Resource indexRoot;
33     Resource diagram;
34
35     public IndexRootSymbolProviderFactory(Resource indexRoot, Resource diagram) {
36         this.indexRoot = indexRoot;
37         this.diagram = diagram;
38     }
39
40         @Override
41         public int hashCode() {
42                 final int prime = 31;
43                 int result = 1;
44                 result = prime * result + ((diagram == null) ? 0 : diagram.hashCode());
45                 result = prime * result + ((indexRoot == null) ? 0 : indexRoot.hashCode());
46                 return result;
47         }
48
49         @Override
50         public boolean equals(Object obj) {
51                 if (this == obj)
52                         return true;
53                 if (obj == null)
54                         return false;
55                 if (getClass() != obj.getClass())
56                         return false;
57                 IndexRootSymbolProviderFactory other = (IndexRootSymbolProviderFactory) obj;
58                 if (diagram == null) {
59                         if (other.diagram != null)
60                                 return false;
61                 } else if (!diagram.equals(other.diagram))
62                         return false;
63                 if (indexRoot == null) {
64                         if (other.indexRoot != null)
65                                 return false;
66                 } else if (!indexRoot.equals(other.indexRoot))
67                         return false;
68                 return true;
69         }
70
71     @Override
72     public ISymbolProvider create(ReadGraph g) throws DatabaseException {
73         return g.syncRequest(new LoadRequest(indexRoot, diagram));
74     }
75
76     static class LoadRequest extends BinaryRead<Resource, Resource, ISymbolProvider> {
77         public LoadRequest(Resource contribution, Resource diagram) {
78             super(contribution, diagram);
79         }
80         @Override
81         public ISymbolProvider perform(ReadGraph graph) throws DatabaseException {
82                 
83                 Layer0 L0 = Layer0.getInstance(graph);
84             DiagramResource dr = DiagramResource.getInstance(graph);
85             
86             Collection<ISymbolGroup> groups = new ArrayList<ISymbolGroup>();
87             
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));
93                     }
94                 }
95             }
96
97             return new SymbolProvider(groups);
98         }
99     }
100
101     static class SymbolProvider extends AbstractSymbolProvider {
102         public SymbolProvider(Collection<ISymbolGroup> groups) {
103             super();
104             setSymbolGroup(groups);
105             lockGroups();
106         }
107     }
108
109 }