]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/typicals/TypicalDiagramTemplateListener.java
Use Consumer interface instead of deprecated Callback interface
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / typicals / TypicalDiagramTemplateListener.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
3  * 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.modeling.typicals;
13
14 import java.util.Map;
15
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;
34
35 import gnu.trove.set.hash.THashSet;
36
37 /**
38  * This listener needs to discover if changes are made to typical diagram
39  * templates.
40  * 
41  * It uses DependencyChange contents to find out if the changes affect any
42  * dependencies of typical diagram templates.
43  * 
44  * @author Tuukka Lehtonen
45  * 
46  * @see SyncTypicalTemplatesToInstances
47  * @deprecated not to be used anymore, will be removed
48  */
49 public class TypicalDiagramTemplateListener extends GenericChangeListener<DependencyChangesRequest, DependencyChanges> {
50
51     private static final boolean   DEBUG = false;
52
53     Layer0                         L0;
54     StructuralResource2            STR;
55     DiagramResource                DIA;
56     ModelingResources              MOD;
57
58     THashSet<Resource>             visited  = new THashSet<Resource>();
59     THashSet<Resource>             typicals = new THashSet<Resource>();
60
61     /**
62      * For optimizing the synchronization visual element properties (transform)
63      */
64     MapSet<Resource, Resource>     changedElementsByDiagram = new MapSet.Hash<Resource, Resource>();
65
66     @Override
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);
72
73         visited.clear();
74         typicals.clear();
75         try {
76             processChanges(graph, event);
77         } finally {
78             // Clear unwanted references
79             visited.clear();
80             typicals.clear();
81             visited.compact();
82         }
83     }
84
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();
88             if (changes == null)
89                 continue;
90
91             if (DEBUG)
92                 for (Change change : changes)
93                     System.out.println("CH: -" + change.toString(graph));
94
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);
109                 }
110             }
111         }
112
113         if (!typicals.isEmpty())
114             scheduleSynchronization(graph, typicals.toArray(Resource.NONE));
115     }
116
117     private void scheduleSynchronization(ReadGraph graph, final Resource[] templates) {
118         MapSet<Resource, Resource> changes = this.changedElementsByDiagram;
119         this.changedElementsByDiagram = new MapSet.Hash<Resource, Resource>();
120
121         graph.asyncRequest(new SyncTypicalTemplatesToInstances(null, templates, changes), e -> {
122             if (e != null)
123                 ErrorLogger.defaultLogError("Typical template diagram synchronization to instances failes, see exception for details.", e);
124         });
125     }
126
127     private void resolveDependentTypicalDiagrams(ReadGraph graph, Resource component, boolean modification) throws DatabaseException {
128         if (visited.contains(component))
129             return;
130
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));
141                     if (modification)
142                         changedElementsByDiagram.add(parent, component);
143                 } else {
144                     Resource diagram = graph.getPossibleObject(parent, MOD.CompositeToDiagram);
145                     if (diagram != null) {
146                         addVisited(diagram, graph.hasStatement(diagram, MOD.DiagramHasInstance));
147                         if (modification) {
148                             Resource element = graph.getPossibleObject(component, MOD.ComponentToElement);
149                             if (element != null)
150                                 changedElementsByDiagram.add(diagram, element);
151                         }
152                     }
153                 }
154             }
155         } else {
156             // handle changes to properties of components/elements ??
157         }
158
159         if (modification) {
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);
168                         if (isTypical)
169                             changedElementsByDiagram.add(parent, connection);
170                     }
171                 }
172             }
173         }
174
175         addVisited(component, false);
176     }
177
178     private void addVisited(Resource r, boolean isTypical) {
179         if (r != null && visited.add(r)) {
180             if (DEBUG)
181                 System.out.println("Visited: " + r);
182             if (isTypical) {
183                 typicals.add(r);
184                 if (DEBUG)
185                     System.out.println("Added typical: " + r);
186             }
187         }
188     }
189
190 }