--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.diagram.symbolcontribution;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.request.BinaryRead;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.Instances;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.diagram.symbollibrary.ISymbolGroup;\r
+import org.simantics.layer0.Layer0;\r
+\r
+/**\r
+ * \r
+ * @author Antti Villberg\r
+ */\r
+public class IndexRootSymbolProviderFactory implements SymbolProviderFactory {\r
+ \r
+ Resource indexRoot;\r
+ Resource diagram;\r
+\r
+ public IndexRootSymbolProviderFactory(Resource indexRoot, Resource diagram) {\r
+ this.indexRoot = indexRoot;\r
+ this.diagram = diagram;\r
+ }\r
+\r
+ @Override\r
+ public int hashCode() {\r
+ final int prime = 31;\r
+ int result = 1;\r
+ result = prime * result + ((diagram == null) ? 0 : diagram.hashCode());\r
+ result = prime * result + ((indexRoot == null) ? 0 : indexRoot.hashCode());\r
+ return result;\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (this == obj)\r
+ return true;\r
+ if (obj == null)\r
+ return false;\r
+ if (getClass() != obj.getClass())\r
+ return false;\r
+ IndexRootSymbolProviderFactory other = (IndexRootSymbolProviderFactory) obj;\r
+ if (diagram == null) {\r
+ if (other.diagram != null)\r
+ return false;\r
+ } else if (!diagram.equals(other.diagram))\r
+ return false;\r
+ if (indexRoot == null) {\r
+ if (other.indexRoot != null)\r
+ return false;\r
+ } else if (!indexRoot.equals(other.indexRoot))\r
+ return false;\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public ISymbolProvider create(ReadGraph g) throws DatabaseException {\r
+ return g.syncRequest(new LoadRequest(indexRoot, diagram));\r
+ }\r
+\r
+ static class LoadRequest extends BinaryRead<Resource, Resource, ISymbolProvider> {\r
+ public LoadRequest(Resource contribution, Resource diagram) {\r
+ super(contribution, diagram);\r
+ }\r
+ @Override\r
+ public ISymbolProvider perform(ReadGraph graph) throws DatabaseException {\r
+ \r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ DiagramResource dr = DiagramResource.getInstance(graph);\r
+ \r
+ Collection<ISymbolGroup> groups = new ArrayList<ISymbolGroup>();\r
+ \r
+ if (parameter != null) {\r
+ Instances query = graph.adapt(dr.SymbolReferenceLibrary, Instances.class);\r
+ for(Resource library : query.find(graph, parameter)) {\r
+ if (SymbolProviderFactories.accept(graph, dr, library, parameter2)) {\r
+ groups.add(BasicSymbolProviderFactory.createGroup(graph, library, L0.IsRelatedTo, parameter2));\r
+ }\r
+ }\r
+ }\r
+\r
+ return new SymbolProvider(groups);\r
+ }\r
+ }\r
+\r
+ static class SymbolProvider extends AbstractSymbolProvider {\r
+ public SymbolProvider(Collection<ISymbolGroup> groups) {\r
+ super();\r
+ setSymbolGroup(groups);\r
+ lockGroups();\r
+ }\r
+ }\r
+\r
+}\r