1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
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.modeling.typicals;
16 import org.simantics.db.MetadataI;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.changeset.GenericChangeListener;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.genericrelation.DependenciesRelation.DependencyChangesRequest;
22 import org.simantics.db.layer0.genericrelation.DependencyChanges;
23 import org.simantics.db.layer0.genericrelation.DependencyChanges.Change;
24 import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentAddition;
25 import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentModification;
26 import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentRemoval;
27 import org.simantics.diagram.content.ConnectionUtil;
28 import org.simantics.diagram.stubs.DiagramResource;
29 import org.simantics.layer0.Layer0;
30 import org.simantics.modeling.ModelingResources;
31 import org.simantics.structural.stubs.StructuralResource2;
32 import org.simantics.utils.datastructures.MapSet;
33 import org.simantics.utils.ui.ErrorLogger;
35 import gnu.trove.set.hash.THashSet;
38 * This listener needs to discover if changes are made to typical diagram
41 * It uses DependencyChange contents to find out if the changes affect any
42 * dependencies of typical diagram templates.
44 * @author Tuukka Lehtonen
46 * @see SyncTypicalTemplatesToInstances
47 * @deprecated not to be used anymore, will be removed
49 public class TypicalDiagramTemplateListener extends GenericChangeListener<DependencyChangesRequest, DependencyChanges> {
51 private static final boolean DEBUG = false;
54 StructuralResource2 STR;
56 ModelingResources MOD;
58 THashSet<Resource> visited = new THashSet<Resource>();
59 THashSet<Resource> typicals = new THashSet<Resource>();
62 * For optimizing the synchronization visual element properties (transform)
64 MapSet<Resource, Resource> changedElementsByDiagram = new MapSet.Hash<Resource, Resource>();
67 public void onEvent(ReadGraph graph, MetadataI metadata, DependencyChanges event) throws DatabaseException {
68 this.L0 = Layer0.getInstance(graph);
69 this.STR = StructuralResource2.getInstance(graph);
70 this.DIA = DiagramResource.getInstance(graph);
71 this.MOD = ModelingResources.getInstance(graph);
76 processChanges(graph, event);
78 // Clear unwanted references
85 private void processChanges(ReadGraph graph, DependencyChanges event) throws DatabaseException {
86 for (Map.Entry<Resource, Change[]> entry : event.modelChanges.entrySet()) {
87 Change[] changes = entry.getValue();
92 for (Change change : changes)
93 System.out.println("CH: -" + change.toString(graph));
95 for (Change c : changes) {
96 if (c instanceof ComponentAddition) {
97 // element/module addition
98 ComponentAddition add = (ComponentAddition) c;
99 resolveDependentTypicalDiagrams(graph, add.component, false);
100 } else if (c instanceof ComponentRemoval) {
101 // element/module removal
102 ComponentRemoval rm = (ComponentRemoval) c;
103 resolveDependentTypicalDiagrams(graph, rm.parent, false);
104 } else if (c instanceof ComponentModification) {
105 // element transform changes
106 // module property changes
107 ComponentModification mod = (ComponentModification) c;
108 resolveDependentTypicalDiagrams(graph, mod.component, true);
113 if (!typicals.isEmpty())
114 scheduleSynchronization(graph, typicals.toArray(Resource.NONE));
117 private void scheduleSynchronization(ReadGraph graph, final Resource[] templates) {
118 MapSet<Resource, Resource> changes = this.changedElementsByDiagram;
119 this.changedElementsByDiagram = new MapSet.Hash<Resource, Resource>();
121 graph.asyncRequest(new SyncTypicalTemplatesToInstances(null, templates, changes), e -> {
123 ErrorLogger.defaultLogError("Typical template diagram synchronization to instances failes, see exception for details.", e);
127 private void resolveDependentTypicalDiagrams(ReadGraph graph, Resource component, boolean modification) throws DatabaseException {
128 if (visited.contains(component))
131 if (graph.isInstanceOf(component, DIA.Diagram)) {
132 addVisited(component, graph.hasStatement(component, MOD.DiagramHasInstance));
133 } else if (graph.isInstanceOf(component, STR.Composite)) {
134 Resource diagram = graph.getPossibleObject(component, MOD.CompositeToDiagram);
135 addVisited(diagram, diagram != null && graph.hasStatement(diagram, MOD.DiagramHasInstance));
136 } else if (graph.isInstanceOf(component, STR.Component)) {
137 Resource parent = graph.getPossibleObject(component, L0.PartOf);
138 if (parent != null) {
139 if (graph.isInstanceOf(component, DIA.Element)) {
140 addVisited(parent, graph.hasStatement(parent, MOD.DiagramHasInstance));
142 changedElementsByDiagram.add(parent, component);
144 Resource diagram = graph.getPossibleObject(parent, MOD.CompositeToDiagram);
145 if (diagram != null) {
146 addVisited(diagram, graph.hasStatement(diagram, MOD.DiagramHasInstance));
148 Resource element = graph.getPossibleObject(component, MOD.ComponentToElement);
150 changedElementsByDiagram.add(diagram, element);
156 // handle changes to properties of components/elements ??
160 // Recognize changes in template diagram connections.
161 if (graph.isInstanceOf(component, DIA.RouteNode)) {
162 Resource connection = ConnectionUtil.tryGetConnection(graph, component);
163 if (connection != null) {
164 Resource parent = graph.getPossibleObject(connection, L0.PartOf);
165 if (parent != null) {
166 boolean isTypical = graph.hasStatement(parent, MOD.DiagramHasInstance);
167 addVisited(parent, isTypical);
169 changedElementsByDiagram.add(parent, connection);
175 addVisited(component, false);
178 private void addVisited(Resource r, boolean isTypical) {
179 if (r != null && visited.add(r)) {
181 System.out.println("Visited: " + r);
185 System.out.println("Added typical: " + r);