+/*******************************************************************************\r
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
+ * 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.modeling.typicals;\r
+\r
+import gnu.trove.set.hash.THashSet;\r
+\r
+import java.util.Map;\r
+\r
+import org.simantics.db.MetadataI;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.changeset.GenericChangeListener;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.genericrelation.DependenciesRelation.DependencyChangesRequest;\r
+import org.simantics.db.layer0.genericrelation.DependencyChanges;\r
+import org.simantics.db.layer0.genericrelation.DependencyChanges.Change;\r
+import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentAddition;\r
+import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentModification;\r
+import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentRemoval;\r
+import org.simantics.diagram.content.ConnectionUtil;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.ModelingResources;\r
+import org.simantics.structural.stubs.StructuralResource2;\r
+import org.simantics.utils.datastructures.Callback;\r
+import org.simantics.utils.datastructures.MapSet;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+\r
+/**\r
+ * This listener needs to discover if changes are made to typical diagram\r
+ * templates.\r
+ * \r
+ * It uses DependencyChange contents to find out if the changes affect any\r
+ * dependencies of typical diagram templates.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ * \r
+ * @see SyncTypicalTemplatesToInstances\r
+ * @deprecated not to be used anymore, will be removed\r
+ */\r
+public class TypicalDiagramTemplateListener extends GenericChangeListener<DependencyChangesRequest, DependencyChanges> {\r
+\r
+ private static final boolean DEBUG = false;\r
+\r
+ Layer0 L0;\r
+ StructuralResource2 STR;\r
+ DiagramResource DIA;\r
+ ModelingResources MOD;\r
+\r
+ THashSet<Resource> visited = new THashSet<Resource>();\r
+ THashSet<Resource> typicals = new THashSet<Resource>();\r
+\r
+ /**\r
+ * For optimizing the synchronization visual element properties (transform)\r
+ */\r
+ MapSet<Resource, Resource> changedElementsByDiagram = new MapSet.Hash<Resource, Resource>();\r
+\r
+ @Override\r
+ public void onEvent(ReadGraph graph, MetadataI metadata, DependencyChanges event) throws DatabaseException {\r
+ this.L0 = Layer0.getInstance(graph);\r
+ this.STR = StructuralResource2.getInstance(graph);\r
+ this.DIA = DiagramResource.getInstance(graph);\r
+ this.MOD = ModelingResources.getInstance(graph);\r
+\r
+ visited.clear();\r
+ typicals.clear();\r
+ try {\r
+ processChanges(graph, event);\r
+ } finally {\r
+ // Clear unwanted references\r
+ visited.clear();\r
+ typicals.clear();\r
+ visited.compact();\r
+ }\r
+ }\r
+\r
+ private void processChanges(ReadGraph graph, DependencyChanges event) throws DatabaseException {\r
+ for (Map.Entry<Resource, Change[]> entry : event.modelChanges.entrySet()) {\r
+ Change[] changes = entry.getValue();\r
+ if (changes == null)\r
+ continue;\r
+\r
+ if (DEBUG)\r
+ for (Change change : changes)\r
+ System.out.println("CH: -" + change.toString(graph));\r
+\r
+ for (Change c : changes) {\r
+ if (c instanceof ComponentAddition) {\r
+ // element/module addition\r
+ ComponentAddition add = (ComponentAddition) c;\r
+ resolveDependentTypicalDiagrams(graph, add.component, false);\r
+ } else if (c instanceof ComponentRemoval) {\r
+ // element/module removal\r
+ ComponentRemoval rm = (ComponentRemoval) c;\r
+ resolveDependentTypicalDiagrams(graph, rm.parent, false);\r
+ } else if (c instanceof ComponentModification) {\r
+ // element transform changes\r
+ // module property changes\r
+ ComponentModification mod = (ComponentModification) c;\r
+ resolveDependentTypicalDiagrams(graph, mod.component, true);\r
+ }\r
+ }\r
+ }\r
+\r
+ if (!typicals.isEmpty())\r
+ scheduleSynchronization(graph, typicals.toArray(Resource.NONE));\r
+ }\r
+\r
+ private void scheduleSynchronization(ReadGraph graph, final Resource[] templates) {\r
+ MapSet<Resource, Resource> changes = this.changedElementsByDiagram;\r
+ this.changedElementsByDiagram = new MapSet.Hash<Resource, Resource>();\r
+\r
+ graph.asyncRequest(new SyncTypicalTemplatesToInstances(null, templates, changes), new Callback<DatabaseException>() {\r
+ @Override\r
+ public void run(DatabaseException parameter) {\r
+ if (parameter != null)\r
+ ErrorLogger.defaultLogError("Typical template diagram synchronization to instances failes, see exception for details.", parameter);\r
+ }\r
+ });\r
+ }\r
+\r
+ private void resolveDependentTypicalDiagrams(ReadGraph graph, Resource component, boolean modification) throws DatabaseException {\r
+ if (visited.contains(component))\r
+ return;\r
+\r
+ if (graph.isInstanceOf(component, DIA.Diagram)) {\r
+ addVisited(component, graph.hasStatement(component, MOD.DiagramHasInstance));\r
+ } else if (graph.isInstanceOf(component, STR.Composite)) {\r
+ Resource diagram = graph.getPossibleObject(component, MOD.CompositeToDiagram);\r
+ addVisited(diagram, diagram != null && graph.hasStatement(diagram, MOD.DiagramHasInstance));\r
+ } else if (graph.isInstanceOf(component, STR.Component)) {\r
+ Resource parent = graph.getPossibleObject(component, L0.PartOf);\r
+ if (parent != null) {\r
+ if (graph.isInstanceOf(component, DIA.Element)) {\r
+ addVisited(parent, graph.hasStatement(parent, MOD.DiagramHasInstance));\r
+ if (modification)\r
+ changedElementsByDiagram.add(parent, component);\r
+ } else {\r
+ Resource diagram = graph.getPossibleObject(parent, MOD.CompositeToDiagram);\r
+ if (diagram != null) {\r
+ addVisited(diagram, graph.hasStatement(diagram, MOD.DiagramHasInstance));\r
+ if (modification) {\r
+ Resource element = graph.getPossibleObject(component, MOD.ComponentToElement);\r
+ if (element != null)\r
+ changedElementsByDiagram.add(diagram, element);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ } else {\r
+ // handle changes to properties of components/elements ??\r
+ }\r
+\r
+ if (modification) {\r
+ // Recognize changes in template diagram connections.\r
+ if (graph.isInstanceOf(component, DIA.RouteNode)) {\r
+ Resource connection = ConnectionUtil.tryGetConnection(graph, component);\r
+ if (connection != null) {\r
+ Resource parent = graph.getPossibleObject(connection, L0.PartOf);\r
+ if (parent != null) {\r
+ boolean isTypical = graph.hasStatement(parent, MOD.DiagramHasInstance);\r
+ addVisited(parent, isTypical);\r
+ if (isTypical)\r
+ changedElementsByDiagram.add(parent, connection);\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ addVisited(component, false);\r
+ }\r
+\r
+ private void addVisited(Resource r, boolean isTypical) {\r
+ if (r != null && visited.add(r)) {\r
+ if (DEBUG)\r
+ System.out.println("Visited: " + r);\r
+ if (isTypical) {\r
+ typicals.add(r);\r
+ if (DEBUG)\r
+ System.out.println("Added typical: " + r);\r
+ }\r
+ }\r
+ }\r
+\r
+}\r