]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/GraphToDiagramSynchronizer.java
226f47daa76b62ea90f572d561aa75e6bc830295
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / GraphToDiagramSynchronizer.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2018 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.adapter;
13
14 import java.awt.geom.AffineTransform;
15 import java.lang.reflect.InvocationTargetException;
16 import java.util.ArrayDeque;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.Deque;
23 import java.util.EnumSet;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Queue;
29 import java.util.Set;
30 import java.util.concurrent.ConcurrentHashMap;
31 import java.util.concurrent.ConcurrentMap;
32 import java.util.concurrent.atomic.AtomicBoolean;
33 import java.util.concurrent.locks.Condition;
34 import java.util.concurrent.locks.ReentrantLock;
35
36 import org.eclipse.core.runtime.IProgressMonitor;
37 import org.eclipse.core.runtime.SubMonitor;
38 import org.simantics.db.AsyncReadGraph;
39 import org.simantics.db.ReadGraph;
40 import org.simantics.db.RequestProcessor;
41 import org.simantics.db.Resource;
42 import org.simantics.db.Session;
43 import org.simantics.db.common.ResourceArray;
44 import org.simantics.db.common.exception.DebugException;
45 import org.simantics.db.common.procedure.adapter.AsyncProcedureAdapter;
46 import org.simantics.db.common.procedure.adapter.CacheListener;
47 import org.simantics.db.common.procedure.adapter.ListenerSupport;
48 import org.simantics.db.common.procedure.adapter.ProcedureAdapter;
49 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
50 import org.simantics.db.common.request.AsyncReadRequest;
51 import org.simantics.db.common.request.ReadRequest;
52 import org.simantics.db.common.session.SessionEventListenerAdapter;
53 import org.simantics.db.common.utils.NameUtils;
54 import org.simantics.db.exception.CancelTransactionException;
55 import org.simantics.db.exception.DatabaseException;
56 import org.simantics.db.exception.NoSingleResultException;
57 import org.simantics.db.exception.ServiceException;
58 import org.simantics.db.procedure.AsyncListener;
59 import org.simantics.db.procedure.AsyncProcedure;
60 import org.simantics.db.procedure.Listener;
61 import org.simantics.db.procedure.Procedure;
62 import org.simantics.db.request.Read;
63 import org.simantics.db.service.SessionEventSupport;
64 import org.simantics.diagram.connection.ConnectionSegmentEnd;
65 import org.simantics.diagram.content.Change;
66 import org.simantics.diagram.content.ConnectionUtil;
67 import org.simantics.diagram.content.DesignatedTerminal;
68 import org.simantics.diagram.content.DiagramContentChanges;
69 import org.simantics.diagram.content.DiagramContents;
70 import org.simantics.diagram.content.EdgeResource;
71 import org.simantics.diagram.content.ResourceTerminal;
72 import org.simantics.diagram.internal.DebugPolicy;
73 import org.simantics.diagram.internal.timing.GTask;
74 import org.simantics.diagram.internal.timing.Timing;
75 import org.simantics.diagram.profile.ProfileKeys;
76 import org.simantics.diagram.synchronization.CollectingModificationQueue;
77 import org.simantics.diagram.synchronization.CompositeModification;
78 import org.simantics.diagram.synchronization.CopyAdvisor;
79 import org.simantics.diagram.synchronization.ErrorHandler;
80 import org.simantics.diagram.synchronization.IHintSynchronizer;
81 import org.simantics.diagram.synchronization.IModifiableSynchronizationContext;
82 import org.simantics.diagram.synchronization.IModification;
83 import org.simantics.diagram.synchronization.LogErrorHandler;
84 import org.simantics.diagram.synchronization.ModificationAdapter;
85 import org.simantics.diagram.synchronization.SynchronizationHints;
86 import org.simantics.diagram.synchronization.graph.AddElement;
87 import org.simantics.diagram.synchronization.graph.BasicResources;
88 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
89 import org.simantics.diagram.synchronization.graph.ElementLoader;
90 import org.simantics.diagram.synchronization.graph.ElementReorder;
91 import org.simantics.diagram.synchronization.graph.ElementWriter;
92 import org.simantics.diagram.synchronization.graph.GraphSynchronizationContext;
93 import org.simantics.diagram.synchronization.graph.GraphSynchronizationHints;
94 import org.simantics.diagram.synchronization.graph.ModificationQueue;
95 import org.simantics.diagram.synchronization.graph.TagChange;
96 import org.simantics.diagram.synchronization.graph.TransformElement;
97 import org.simantics.diagram.synchronization.graph.layer.GraphLayer;
98 import org.simantics.diagram.synchronization.graph.layer.GraphLayerManager;
99 import org.simantics.diagram.ui.DiagramModelHints;
100 import org.simantics.g2d.canvas.Hints;
101 import org.simantics.g2d.canvas.ICanvasContext;
102 import org.simantics.g2d.connection.ConnectionEntity;
103 import org.simantics.g2d.connection.EndKeyOf;
104 import org.simantics.g2d.connection.TerminalKeyOf;
105 import org.simantics.g2d.diagram.DiagramClass;
106 import org.simantics.g2d.diagram.DiagramHints;
107 import org.simantics.g2d.diagram.DiagramMutator;
108 import org.simantics.g2d.diagram.DiagramUtils;
109 import org.simantics.g2d.diagram.IDiagram;
110 import org.simantics.g2d.diagram.IDiagram.CompositionListener;
111 import org.simantics.g2d.diagram.IDiagram.CompositionVetoListener;
112 import org.simantics.g2d.diagram.handler.DataElementMap;
113 import org.simantics.g2d.diagram.handler.ElementFactory;
114 import org.simantics.g2d.diagram.handler.Relationship;
115 import org.simantics.g2d.diagram.handler.RelationshipHandler;
116 import org.simantics.g2d.diagram.handler.SubstituteElementClass;
117 import org.simantics.g2d.diagram.handler.Topology;
118 import org.simantics.g2d.diagram.handler.Topology.Connection;
119 import org.simantics.g2d.diagram.handler.Topology.Terminal;
120 import org.simantics.g2d.diagram.handler.TransactionContext.TransactionType;
121 import org.simantics.g2d.diagram.impl.Diagram;
122 import org.simantics.g2d.diagram.participant.ElementPainter;
123 import org.simantics.g2d.element.ElementClass;
124 import org.simantics.g2d.element.ElementHints;
125 import org.simantics.g2d.element.ElementHints.DiscardableKey;
126 import org.simantics.g2d.element.ElementUtils;
127 import org.simantics.g2d.element.IElement;
128 import org.simantics.g2d.element.IElementClassProvider;
129 import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd;
130 import org.simantics.g2d.element.handler.ElementHandler;
131 import org.simantics.g2d.element.handler.ElementLayerListener;
132 import org.simantics.g2d.element.handler.TerminalTopology;
133 import org.simantics.g2d.element.impl.Element;
134 import org.simantics.g2d.layers.ILayer;
135 import org.simantics.g2d.layers.ILayersEditor;
136 import org.simantics.g2d.routing.RouterFactory;
137 import org.simantics.scenegraph.INode;
138 import org.simantics.scenegraph.profile.DataNodeConstants;
139 import org.simantics.scenegraph.profile.DataNodeMap;
140 import org.simantics.scenegraph.profile.common.ProfileObserver;
141 import org.simantics.structural2.modelingRules.IModelingRules;
142 import org.simantics.utils.datastructures.ArrayMap;
143 import org.simantics.utils.datastructures.MapSet;
144 import org.simantics.utils.datastructures.Pair;
145 import org.simantics.utils.datastructures.disposable.AbstractDisposable;
146 import org.simantics.utils.datastructures.hints.HintListenerAdapter;
147 import org.simantics.utils.datastructures.hints.IHintContext.Key;
148 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
149 import org.simantics.utils.datastructures.hints.IHintListener;
150 import org.simantics.utils.datastructures.hints.IHintObservable;
151 import org.simantics.utils.datastructures.map.AssociativeMap;
152 import org.simantics.utils.datastructures.map.Associativity;
153 import org.simantics.utils.datastructures.map.Tuple;
154 import org.simantics.utils.strings.EString;
155 import org.simantics.utils.threads.ThreadUtils;
156 import org.simantics.utils.threads.logger.ITask;
157 import org.simantics.utils.threads.logger.ThreadLogger;
158 import org.slf4j.Logger;
159 import org.slf4j.LoggerFactory;
160
161 import gnu.trove.map.hash.TObjectIntHashMap;
162 import gnu.trove.set.hash.THashSet;
163
164 /**
165  * This class loads a diagram contained in the graph database into the runtime
166  * diagram model and synchronizes changes in the graph into the run-time
167  * diagram. Any modifications to the graph model will be reflected to the
168  * run-time diagram model. Hence the name GraphToDiagramSynchronizer.
169  * 
170  * <p>
171  * This class does not in itself support modification of the graph diagram
172  * model. This manipulation is meant to be performed through a
173  * {@link DiagramMutator} implementation that is installed into the diagram
174  * using the {@link DiagramHints#KEY_MUTATOR} hint key.
175  * 
176  * This implementations is built to only support diagrams defined in the graph
177  * model as indicated in <a
178  * href="https://www.simantics.org/wiki/index.php/Org.simantics.diagram" >this
179  * </a> document.
180  * 
181  * <p>
182  * The synchronizer in itself is an {@link IDiagramLoader} which means that it
183  * can be used for loading a diagram from the graph. In order for the
184  * synchronizer to keep tracking the graph diagram model for changes the diagram
185  * must be loaded with it. The tracking is implemented using graph database
186  * queries. If you just want to load the diagram but detach it from the
187  * synchronizer's tracking mechanisms, all you need to do is to dispose the
188  * synchronizer after loading the diagram.
189  * 
190  * <p>
191  * This class guarantees that a single diagram element (IElement) representing a
192  * single back-end object ({@link ElementHints#KEY_OBJECT}) will stay the same
193  * object for the same back-end object.
194  * 
195  * <p>
196  * TODO: Currently it just happens that {@link GraphToDiagramSynchronizer}
197  * contains {@link DefaultDiagramMutator} which depends on some internal details
198  * of {@link GraphToDiagramSynchronizer} but it should be moved out of here by
199  * introducing new interfaces.
200  * 
201  * <h2>Basic usage example</h2>
202  * <p>
203  * This example shows how to initialize {@link GraphToDiagramSynchronizer} for a
204  * specified {@link ICanvasContext} and load a diagram from a specified diagram
205  * resource in the graph.
206  * 
207  * <pre>
208  * IDiagram loadDiagram(final ICanvasContext canvasContext, RequestProcessor processor, Resource diagramResource,
209  *         ResourceArray structuralPath) throws DatabaseException {
210  *     GraphToDiagramSynchronizer synchronizer = processor.syncRequest(new Read&lt;GraphToDiagramSynchronizer&gt;() {
211  *         public GraphToDiagramSynchronizer perform(ReadGraph graph) throws DatabaseException {
212  *             return new GraphToDiagramSynchronizer(graph, canvasContext, createElementClassProvider(graph));
213  *         }
214  *     });
215  *     IDiagram d = requestProcessor
216  *             .syncRequest(new DiagramLoadQuery(diagramResource, structuralPath, synchronizer, null));
217  *     return d;
218  * }
219  * 
220  * protected IElementClassProvider createElementClassProvider(ReadGraph graph) {
221  *     DiagramResource dr = DiagramResource.getInstance(graph);
222  *     return ElementClassProviders.mappedProvider(ElementClasses.CONNECTION, DefaultConnectionClassFactory.CLASS
223  *             .newClassWith(new ResourceAdapterImpl(dr.Connection)), ElementClasses.FLAG, FlagClassFactory
224  *             .createFlagClass(dr.Flag));
225  * }
226  * </pre>
227  * 
228  * <p>
229  * TODO: make GraphToDiagramSynchronizer a canvas participant to make it more
230  * uniform with the rest of the canvas system. This does not mean that G2DS must
231  * be attached to an ICanvasContext in order to be used, rather that it can be
232  * attached to one.
233  * 
234  * <p>
235  * TODO: test that detaching the synchronizer via {@link #dispose()} actually
236  * works.
237  * <p>
238  * TODO: remove {@link DefaultDiagramMutator} and all {@link DiagramMutator}
239  * stuff altogether
240  * 
241  * <p>
242  * TODO: diagram connection loading has no listener
243  * 
244  * @author Tuukka Lehtonen
245  * 
246  * @see GraphElementClassFactory
247  * @see GraphElementFactory
248  * @see ElementLoader
249  * @see ElementWriter
250  * @see IHintSynchronizer
251  * @see CopyAdvisor
252  */
253 public class GraphToDiagramSynchronizer extends AbstractDisposable implements IDiagramLoader, IModifiableSynchronizationContext {
254
255     private static final Logger LOGGER = LoggerFactory.getLogger(GraphToDiagramSynchronizer.class);
256
257     /**
258      * Controls whether the class adds hint listeners to each diagram element
259      * that try to perform basic sanity checks on changes happening in element
260      * hints. Having this will immediately inform you of bugs that corrupt the
261      * diagram model within the element hints in some way.
262      */
263     private static final boolean USE_ELEMENT_VALIDATING_LISTENERS = false;
264
265     /**
266      * These keys are used to hang on to Connection instances of edges that will
267      * be later installed as EndKeyOf/TerminalKeyOf hints into the loaded
268      * element during the "graph to diagram update transaction".
269      */
270     private static final Key     KEY_CONNECTION_BEGIN_PLACEHOLDER = new KeyOf(PlaceholderConnection.class, "CONNECTION_BEGIN_PLACEHOLDER");
271     private static final Key     KEY_CONNECTION_END_PLACEHOLDER   = new KeyOf(PlaceholderConnection.class, "CONNECTION_END_PLACEHOLDER");
272
273     /**
274      * Stored into an edge node during connection edge requests using the
275      * KEY_CONNECTION_BEGIN_PLACEHOLDER and KEY_CONNECTION_END_PLACEHOLDER keys.
276      */
277     static class PlaceholderConnection {
278         public final EdgeEnd end;
279         public final Object node;
280         public final Terminal terminal;
281         public PlaceholderConnection(EdgeEnd end, Object node, Terminal terminal) {
282             this.end = end;
283             this.node = node;
284             this.terminal = terminal;
285         }
286     }
287
288     /**
289      * Indicates to the diagram CompositionListener of this synchronizer that is
290      * should deny all relationships for the element this hint is attached to.
291      */
292     private static final Key        KEY_REMOVE_RELATIONSHIPS = new KeyOf(Boolean.class, "REMOVE_RELATIONSHIPS");
293
294     static ErrorHandler             errorHandler             = LogErrorHandler.INSTANCE;
295
296     /**
297      * The canvas context which is being synchronized with the graph. Received
298      * during construction.
299      * 
300      * <p>
301      * Is not nulled during disposal of this class since internal listener's
302      * life-cycles depend on canvas.isDisposed.
303      */
304     ICanvasContext                  canvas;
305
306     /**
307      * The session used by this synchronizer. Received during construction.
308      */
309     Session                         session;
310
311     /**
312      * Locked while updating diagram contents from the graph.
313      */
314     ReentrantLock                   diagramUpdateLock        = new ReentrantLock();
315
316     // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
317     // BI-DIRECTIONAL DIAGRAM ELEMENT <-> BACKEND OBJECT MAPPING BEGIN
318     // ------------------------------------------------------------------------
319
320     /**
321      * Holds a GraphToDiagramUpdater instance while a diagram content update is
322      * currently in progress.
323      *
324      * <p>
325      * Basically this is a hack solution to the problem of properly finding
326      * newly added Resource<->IElement mappings while loading the diagram
327      * contents. See {@link DataElementMapImpl} for why this is necessary.
328      */
329     GraphToDiagramUpdater                   currentUpdater                   = null;
330
331     /**
332      * A map from data objects to elements. Elements should already contain the
333      * data objects as {@link ElementHints#KEY_OBJECT} hints.
334      */
335     ConcurrentMap<Object, IElement>         dataElement                      = new ConcurrentHashMap<Object, IElement>();
336
337     /**
338      * Temporary structure for single-threaded use in #{@link DiagramUpdater}.
339      */
340     Collection<Connection>                  tempConnections = new ArrayList<Connection>();
341
342     /**
343      * A dummy class of which an instance will be given to each new edge element
344      * to make {@link TerminalKeyOf} keys unique for each edge.
345      */
346     static class TransientElementObject {
347         @Override
348         public String toString() {
349             return "MUTATOR GENERATED (hash=" + System.identityHashCode(this) + ")";
350         }
351     }
352
353     private static class ConnectionChildren {
354         public Set<IElement> branchPoints;
355         public Set<IElement> segments;
356
357         public ConnectionChildren(Set<IElement> branchPoints, Set<IElement> segments) {
358             this.branchPoints = branchPoints;
359             this.segments = segments;
360         }
361     }
362
363     ListenerSupport canvasListenerSupport = new ListenerSupport() {
364         @Override
365         public void exception(Throwable t) {
366             error(t);
367         }
368
369         @Override
370         public boolean isDisposed() {
371             return !isAlive() || canvas.isDisposed();
372         }
373     };
374
375     /**
376      * @see ElementHints#KEY_CONNECTION_ENTITY
377      */
378     class ConnectionEntityImpl implements ConnectionEntity {
379
380         /**
381          * The connection instance resource in the graph backend.
382          *
383          * May be <code>null</code> if the connection has not been synchronized
384          * yet.
385          */
386         Resource                 connection;
387
388         /**
389          * The connection type resource in the graph backend.
390          *
391          * May be <code>null</code> if the connection has not been synchronized
392          * yet.
393          */
394         Resource                 connectionType;
395
396         /**
397          * The connection entity element which is a part of the diagram.
398          */
399         IElement                 connectionElement;
400
401         /**
402          * List of backend-synchronized branch points that are part of this
403          * connection.
404          */
405         Collection<Resource>     branchPoints        = Collections.emptyList();
406
407         /**
408          * List of backend-synchronized edges that are part of this connection.
409          */
410         Collection<EdgeResource> segments            = Collections.emptyList();
411
412         Set<Object>              removedBranchPoints = new HashSet<Object>(4);
413
414         Set<Object>              removedSegments     = new HashSet<Object>(4);
415
416         /**
417          * List of non-backend-synchronized branch point element that are part
418          * of this connection.
419          */
420         List<IElement>           branchPointElements = new ArrayList<IElement>(1);
421
422         /**
423          * List of non-backend-synchronized edge element that are part of this
424          * connection.
425          */
426         List<IElement>           segmentElements     = new ArrayList<IElement>(2);
427
428         ConnectionListener       listener;
429
430         ConnectionEntityImpl(Resource connection, Resource connectionType, IElement connectionElement) {
431             this.connection = connection;
432             this.connectionType = connectionType;
433             this.connectionElement = connectionElement;
434         }
435
436         ConnectionEntityImpl(Resource connectionType, IElement connectionElement) {
437             this.connectionType = connectionType;
438             this.connectionElement = connectionElement;
439         }
440
441         ConnectionEntityImpl(ReadGraph graph, Resource connection, IElement connectionElement)
442         throws NoSingleResultException, ServiceException {
443             this.connection = connection;
444             this.connectionType = graph.getSingleType(connection, br.DIA.Connection);
445             this.connectionElement = connectionElement;
446         }
447
448         @Override
449         public IElement getConnection() {
450             return connectionElement;
451         }
452
453         public Object getConnectionObject() {
454             return connection;
455         }
456
457         public IElement getConnectionElement() {
458             if (connectionElement == null)
459                 return getMappedConnectionElement();
460             return connectionElement;
461         }
462
463         private IElement getMappedConnectionElement() {
464             IElement ce = null;
465             if (connection != null)
466                 ce = getMappedElement(connection);
467             return ce == null ? connectionElement : ce;
468         }
469
470         void fix() {
471             Collection<IElement> segments = getSegments(null);
472
473             // Remove all TerminalKeyOf hints from branch points that do not
474             // match
475             ArrayList<TerminalKeyOf> pruned = null;
476             for (IElement bp : getBranchPoints(null)) {
477                 if (pruned == null)
478                     pruned = new ArrayList<TerminalKeyOf>(4);
479                 pruned.clear();
480                 for (Map.Entry<TerminalKeyOf, Object> entry : bp.getHintsOfClass(TerminalKeyOf.class).entrySet()) {
481                     // First check that the terminal matches.
482                     Connection c = (Connection) entry.getValue();
483                     if (!segments.contains(c.edge))
484                         pruned.add(entry.getKey());
485                 }
486                 removeNodeTopologyHints((Element) bp, pruned);
487             }
488         }
489
490         public ConnectionChildren getConnectionChildren() {
491             Set<IElement> bps = Collections.emptySet();
492             Set<IElement> segs = Collections.emptySet();
493             if (!branchPoints.isEmpty()) {
494                 bps = new HashSet<IElement>(branchPoints.size());
495                 for (Resource bp : branchPoints) {
496                     IElement e = getMappedElement(bp);
497                     if (e != null)
498                         bps.add(e);
499                 }
500             }
501             if (!segments.isEmpty()) {
502                 segs = new HashSet<IElement>(segments.size());
503                 for (EdgeResource seg : segments) {
504                     IElement e = getMappedElement(seg);
505                     if (e != null)
506                         segs.add(e);
507                 }
508             }
509             return new ConnectionChildren(bps, segs);
510         }
511
512         public void setData(Collection<EdgeResource> segments, Collection<Resource> branchPoints) {
513             // System.out.println("setData " + segments.size());
514             this.branchPoints = branchPoints;
515             this.segments = segments;
516
517             // Reset the added/removed state of segments and branchpoints.
518             this.removedBranchPoints = new HashSet<Object>(4);
519             this.removedSegments = new HashSet<Object>(4);
520             this.branchPointElements = new ArrayList<IElement>(4);
521             this.segmentElements = new ArrayList<IElement>(4);
522         }
523
524         public void fireListener(ConnectionChildren old, ConnectionChildren current) {
525             if (listener != null) {
526                 List<IElement> removed = new ArrayList<IElement>();
527                 List<IElement> added = new ArrayList<IElement>();
528
529                 for (IElement oldBp : old.branchPoints)
530                     if (!current.branchPoints.contains(oldBp))
531                         removed.add(oldBp);
532                 for (IElement oldSeg : old.segments)
533                     if (!current.segments.contains(oldSeg))
534                         removed.add(oldSeg);
535
536                 for (IElement bp : current.branchPoints)
537                     if (!old.branchPoints.contains(bp))
538                         added.add(bp);
539                 for (IElement seg : current.segments)
540                     if (!old.segments.contains(seg))
541                         added.add(seg);
542
543                 if (!removed.isEmpty() || !added.isEmpty()) {
544                     listener.connectionChanged(new ConnectionEvent(this.connectionElement, removed, added));
545                 }
546             }
547         }
548
549         @Override
550         public Collection<IElement> getBranchPoints(Collection<IElement> result) {
551             if (result == null)
552                 result = new ArrayList<IElement>(branchPoints.size());
553             for (Resource bp : branchPoints) {
554                 if (!removedBranchPoints.contains(bp)) {
555                     IElement e = getMappedElement(bp);
556                     if (e != null)
557                         result.add(e);
558                 }
559             }
560             result.addAll(branchPointElements);
561             return result;
562         }
563
564         @Override
565         public Collection<IElement> getSegments(Collection<IElement> result) {
566             if (result == null)
567                 result = new ArrayList<IElement>(segments.size());
568             for (EdgeResource seg : segments) {
569                 if (!removedSegments.contains(seg)) {
570                     IElement e = getMappedElement(seg);
571                     if (e != null)
572                         result.add(e);
573                 }
574             }
575             result.addAll(segmentElements);
576             return result;
577         }
578
579         @Override
580         public Collection<Connection> getTerminalConnections(Collection<Connection> result) {
581             if (result == null)
582                 result = new ArrayList<Connection>(segments.size() * 2);
583             Set<org.simantics.utils.datastructures.Pair<IElement, Terminal>> processed = new HashSet<org.simantics.utils.datastructures.Pair<IElement, Terminal>>();
584             for (EdgeResource seg : segments) {
585                 IElement edge = getMappedElement(seg);
586                 if (edge != null) {
587                     for (EndKeyOf key : EndKeyOf.KEYS) {
588                         Connection c = edge.getHint(key);
589                         if (c != null && (c.terminal instanceof ResourceTerminal) && processed.add(Pair.make(c.node, c.terminal)))
590                             result.add(c);
591                     }
592                 }
593             }
594             return result;
595         }
596
597         @Override
598         public void setListener(ConnectionListener listener) {
599             this.listener = listener;
600         }
601
602         @Override
603         public String toString() {
604             return getClass().getSimpleName() + "[resource=" + connection + ", branch points=" + branchPoints
605             + ", segments=" + segments + ", connectionElement=" + connectionElement
606             + ", branch point elements=" + branchPointElements + ", segment elements=" + segmentElements
607             + ", removed branch points=" + removedBranchPoints + ", removed segments=" + removedSegments + "]";
608         }
609
610     }
611
612     /**
613      * A map from connection data objects to connection entities. The connection
614      * part elements should already contain the data objects as
615      * {@link ElementHints#KEY_OBJECT} hints.
616      */
617     ConcurrentMap<Object, ConnectionEntityImpl> dataConnection = new ConcurrentHashMap<Object, ConnectionEntityImpl>();
618
619
620     void mapElementIfNew(final Object data, final IElement element) {
621         IElement mapped = getMappedElement(data);
622         if(mapped == null) {
623             mapElement(data, element);
624             currentUpdater.addedElements.add(element);
625             currentUpdater.addedElementMap.put(data, element);
626         }
627     }
628
629     
630     /**
631      * @param data
632      * @param element
633      */
634     void mapElement(final Object data, final IElement element) {
635         if (!(element instanceof Element)) {
636             throw new IllegalArgumentException("mapElement: expected instance of Element, got " + element + " with data " + data);
637         }
638         assert data != null;
639         assert element != null;
640         if (DebugPolicy.DEBUG_MAPPING)
641             new Exception(Thread.currentThread() + " MAPPING: " + data + " -> " + element).printStackTrace();
642         dataElement.put(data, element);
643     }
644
645     /**
646      * @param data
647      * @return
648      */
649     IElement getMappedElement(final Object data) {
650         assert (data != null);
651         IElement element = dataElement.get(data);
652         return element;
653     }
654
655     IElement getMappedElementByElementObject(IElement e) {
656         if (e == null)
657             return null;
658         Object o = e.getHint(ElementHints.KEY_OBJECT);
659         if (o == null)
660             return null;
661         return getMappedElement(o);
662     }
663
664     /**
665      * @param data
666      * @return
667      */
668     IElement assertMappedElement(final Object data) {
669         IElement element = dataElement.get(data);
670         assert element != null;
671         return element;
672     }
673
674     /**
675      * @param data
676      * @return
677      */
678     IElement unmapElement(final Object data) {
679         IElement element = dataElement.remove(data);
680         if (DebugPolicy.DEBUG_MAPPING)
681             new Exception(Thread.currentThread() + " UN-MAPPED: " + data + " -> " + element).printStackTrace();
682         return element;
683     }
684
685     /**
686      * @param data
687      * @param element
688      */
689     void mapConnection(final Object data, final ConnectionEntityImpl connection) {
690         assert data != null;
691         assert connection != null;
692         if (DebugPolicy.DEBUG_MAPPING)
693             System.out.println(Thread.currentThread() + " MAPPING CONNECTION: " + data + " -> " + connection);
694         dataConnection.put(data, connection);
695     }
696
697     /**
698      * @param data
699      * @return
700      */
701     ConnectionEntityImpl getMappedConnection(final Object data) {
702         ConnectionEntityImpl connection = dataConnection.get(data);
703         return connection;
704     }
705
706     /**
707      * @param data
708      * @return
709      */
710     ConnectionEntityImpl assertMappedConnection(final Object data) {
711         ConnectionEntityImpl connection = getMappedConnection(data);
712         assert connection != null;
713         return connection;
714     }
715
716     /**
717      * @param data
718      * @return
719      */
720     ConnectionEntityImpl unmapConnection(final Object data) {
721         ConnectionEntityImpl connection = dataConnection.remove(data);
722         if (DebugPolicy.DEBUG_MAPPING)
723             System.out.println(Thread.currentThread() + " UN-MAPPED CONNECTION: " + data + " -> " + connection);
724         return connection;
725     }
726
727     class DataElementMapImpl implements DataElementMap {
728         @Override
729         public Object getData(IDiagram d, IElement element) {
730             if (d == null)
731                 throw new NullPointerException("null diagram");
732             if (element == null)
733                 throw new NullPointerException("null element");
734
735             assert ElementUtils.getDiagram(element) == d;
736             return element.getHint(ElementHints.KEY_OBJECT);
737         }
738
739         @Override
740         public IElement getElement(IDiagram d, Object data) {
741             if (d == null)
742                 throw new NullPointerException("null diagram");
743             if (data == null)
744                 throw new NullPointerException("null data");
745
746             GraphToDiagramUpdater updater = currentUpdater;
747             if (updater != null) {
748                 // This HACK is for allowing GraphElementFactory implementations
749                 // to find the IElements they are related to.
750                 IElement e = updater.addedElementMap.get(data);
751                 if (e != null)
752                     return e;
753             }
754
755             IElement e = getMappedElement(data);
756             if (e != null)
757                 return e;
758             return null;
759         }
760     }
761
762     class SubstituteElementClassImpl implements SubstituteElementClass {
763         @Override
764         public ElementClass substitute(IDiagram d, ElementClass ec) {
765             if (d != diagram)
766                 throw new IllegalArgumentException("specified diagram does not have this SubstituteElementClass handler");
767
768             // If the element class is our own, there's no point in creating
769             // a copy of it.
770             if (ec.contains(elementLayerListener))
771                 return ec;
772
773             List<ElementHandler> all = ec.getAll();
774             List<ElementHandler> result = new ArrayList<ElementHandler>(all.size());
775             for (ElementHandler eh : all) {
776                 if (eh instanceof ElementLayerListenerImpl)
777                     result.add(elementLayerListener);
778                 else
779                     result.add(eh);
780             }
781             return ElementClass.compile(result, false).setId(ec.getId());
782         }
783     }
784
785     final DataElementMapImpl         dataElementMap         = new DataElementMapImpl();
786
787     final SubstituteElementClassImpl substituteElementClass = new SubstituteElementClassImpl();
788
789     // ------------------------------------------------------------------------
790     // BI-DIRECTIONAL DIAGRAM ELEMENT <-> BACKEND OBJECT MAPPING END
791     // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
792
793     void warning(String message, Exception e) {
794         errorHandler.warning(message, e);
795     }
796
797     void warning(Exception e) {
798         errorHandler.warning(e.getMessage(), e);
799     }
800
801     void error(String message, Throwable e) {
802         errorHandler.error(message, e);
803     }
804
805     void error(Throwable e) {
806         errorHandler.error(e.getMessage(), e);
807     }
808
809     // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
810     // GRAPH MODIFICATION QUEUE BEGIN
811     // ------------------------------------------------------------------------
812
813     ModificationQueue                 modificationQueue;
814     IModifiableSynchronizationContext synchronizationContext;
815
816     @Override
817     public <T> T set(Key key, Object value) {
818         if (synchronizationContext == null)
819             return null;
820         return synchronizationContext.set(key, value);
821     }
822
823     @Override
824     public <T> T get(Key key) {
825         if (synchronizationContext == null)
826             return null;
827         return synchronizationContext.get(key);
828     }
829
830     // ------------------------------------------------------------------------
831     // GRAPH MODIFICATION QUEUE END
832     // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
833
834     /**
835      * The previously loaded version of the diagram content. This is needed to
836      * calculate the difference between new and old content on each
837      * {@link #diagramGraphUpdater(DiagramContents)} invocation.
838      */
839     DiagramContents       previousContent;
840
841     /**
842      * The diagram instance that this synchronizer is synchronizing with the
843      * graph.
844      */
845     IDiagram              diagram;
846
847     /**
848      * An observer for diagram profile entries. Has a life-cycle that must be
849      * bound to the life-cycle of this GraphToDiagramSynchronizer instance.
850      * Disposed if synchronizer is detached in {@link #doDispose()} or finally
851      * when the canvas is disposed.
852      */
853     ProfileObserver       profileObserver;
854
855     IElementClassProvider elementClassProvider;
856
857     BasicResources        br;
858
859     // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
860     // Internal state machine handling BEGIN
861     // ------------------------------------------------------------------------
862
863     /**
864      * An indicator for the current state of this synchronizer. This is a simple
865      * state machine with the following possible state transitions:
866      *
867      * <ul>
868      * <li>INITIAL -> LOADING, DISPOSED</li>
869      * <li>LOADING -> IDLE</li>
870      * <li>IDLE -> UPDATING_DIAGRAM, DISPOSED</li>
871      * <li>UPDATING_DIAGRAM -> IDLE</li>
872      * </ul>
873      * 
874      * Start states: INITIAL
875      * End states: DISPOSED
876      */
877     static enum State {
878         /**
879          * The initial state of the synchronizer.
880          */
881         INITIAL,
882         /**
883          * The synchronizer is performing load-time initialization. During this
884          * time no canvas refreshes should be forced.
885          */
886         LOADING,
887         /**
888          * The synchronizer is performing updates to the diagram model. This
889          * process goes on in the canvas context thread.
890          */
891         UPDATING_DIAGRAM,
892         /**
893          * The synchronizer is doing nothing.
894          */
895         IDLE,
896         /**
897          * The synchronized diagram is being disposed, which means that this
898          * synchronizer should not accept any further actions.
899          */
900         DISPOSED,
901     }
902
903     public static final EnumSet<State> FROM_INITIAL          = EnumSet.of(State.LOADING, State.DISPOSED);
904     public static final EnumSet<State> FROM_LOADING          = EnumSet.of(State.IDLE);
905     public static final EnumSet<State> FROM_UPDATING_DIAGRAM = EnumSet.of(State.IDLE);
906     public static final EnumSet<State> FROM_IDLE             = EnumSet.of(State.UPDATING_DIAGRAM, State.DISPOSED);
907     public static final EnumSet<State> NO_STATES             = EnumSet.noneOf(State.class);
908
909     private EnumSet<State> validTargetStates(State start) {
910         switch (start) {
911             case INITIAL: return FROM_INITIAL;
912             case LOADING: return FROM_LOADING;
913             case UPDATING_DIAGRAM: return FROM_UPDATING_DIAGRAM;
914             case IDLE: return FROM_IDLE;
915             case DISPOSED: return NO_STATES;
916         }
917         throw new IllegalArgumentException("unrecognized state " + start);
918     }
919
920     private String validateStateChange(State start, State end) {
921         EnumSet<State> validTargets = validTargetStates(start);
922         if (!validTargets.contains(end))
923             return "Cannot transition from " + start + " state to " + end + ".";
924         return null;
925     }
926
927     /**
928      * The current state of the synchronizer. At start it is
929      * {@link State#INITIAL} and after loading it is {@link State#IDLE}.
930      */
931     State                              synchronizerState     = State.INITIAL;
932
933     /**
934      * A condition variable used to synchronize synchronizer state changes.
935      */
936     ReentrantLock                      stateLock             = new ReentrantLock();
937
938     /**
939      * A condition that is signaled when the synchronizer state changes to IDLE.
940      */
941     Condition                          idleCondition         = stateLock.newCondition();
942
943     State getState() {
944         return synchronizerState;
945     }
946
947     /**
948      * Activates the desired state after making sure that the synchronizer has
949      * been IDLE in between its current state and this invocation.
950      *
951      * @param newState the new state to activate
952      * @throws InterruptedException if waiting for IDLE state gets interrupted
953      * @throws IllegalStateException if the requested transition from the
954      *         current state to the desired state would be illegal.
955      */
956     void activateState(State newState, boolean waitForIdle) throws InterruptedException {
957         stateLock.lock();
958         try {
959             // Wait until the state of the synchronizer IDLEs if necessary.
960             if (waitForIdle && synchronizerState != State.IDLE) {
961                 String error = validateStateChange(synchronizerState, State.IDLE);
962                 if (error != null)
963                     throw new IllegalStateException(error);
964
965                 while (synchronizerState != State.IDLE) {
966                     if (DebugPolicy.DEBUG_STATE)
967                         System.out.println(Thread.currentThread() + " waiting for IDLE state, current="
968                                 + synchronizerState);
969                     idleCondition.await();
970                 }
971             }
972
973             String error = validateStateChange(synchronizerState, newState);
974             if (error != null)
975                 throw new IllegalStateException(error);
976
977             if (DebugPolicy.DEBUG_STATE)
978                 System.out.println(Thread.currentThread() + " activated state " + newState);
979             this.synchronizerState = newState;
980
981             if (newState == State.IDLE)
982                 idleCondition.signalAll();
983         } finally {
984             stateLock.unlock();
985         }
986     }
987
988     void idle() throws IllegalStateException, InterruptedException {
989         activateState(State.IDLE, false);
990     }
991
992     static interface StateRunnable extends Runnable {
993         void execute() throws InvocationTargetException;
994
995         public abstract class Stub implements StateRunnable {
996             @Override
997             public void run() {
998             }
999
1000             @Override
1001             public final void execute() throws InvocationTargetException {
1002                 try {
1003                     perform();
1004                 } catch (Exception e) {
1005                     throw new InvocationTargetException(e);
1006                 } catch (LinkageError e) {
1007                     throw new InvocationTargetException(e);
1008                 }
1009             }
1010
1011             protected abstract void perform() throws Exception;
1012         }
1013     }
1014
1015     protected void runInState(State state, StateRunnable runnable) throws InvocationTargetException {
1016         try {
1017             activateState(state, true);
1018             try {
1019                 runnable.execute();
1020             } finally {
1021                 idle();
1022             }
1023         } catch (IllegalStateException e) {
1024             throw new InvocationTargetException(e);
1025         } catch (InterruptedException e) {
1026             throw new InvocationTargetException(e);
1027         }
1028     }
1029
1030     protected void safeRunInState(State state, StateRunnable runnable) {
1031         try {
1032             runInState(state, runnable);
1033         } catch (InvocationTargetException e) {
1034             error("Failed to run runnable " + runnable + " in state " + state + ". See exception for details.", e
1035                     .getCause());
1036         }
1037     }
1038
1039     // ------------------------------------------------------------------------
1040     // Internal state machine handling END
1041     // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1042
1043     /**
1044      * @param processor
1045      * @param canvas
1046      * @param elementClassProvider
1047      * @throws DatabaseException
1048      */
1049     public GraphToDiagramSynchronizer(RequestProcessor processor, ICanvasContext canvas, IElementClassProvider elementClassProvider) throws DatabaseException {
1050         if (processor == null)
1051             throw new IllegalArgumentException("null processor");
1052         if (canvas == null)
1053             throw new IllegalArgumentException("null canvas");
1054         if (elementClassProvider == null)
1055             throw new IllegalArgumentException("null element class provider");
1056
1057         this.session = processor.getSession();
1058         this.canvas = canvas;
1059         this.modificationQueue = new ModificationQueue(session, errorHandler);
1060
1061         processor.syncRequest(new ReadRequest() {
1062             @Override
1063             public void run(ReadGraph graph) throws DatabaseException {
1064                 initializeResources(graph);
1065             }
1066         });
1067
1068         this.elementClassProvider = elementClassProvider;
1069         synchronizationContext.set(SynchronizationHints.ELEMENT_CLASS_PROVIDER, elementClassProvider);
1070
1071         attachSessionListener(processor.getSession());
1072     }
1073
1074     /**
1075      * @return
1076      */
1077     public IElementClassProvider getElementClassProvider() {
1078         return elementClassProvider;
1079     }
1080
1081     public Session getSession() {
1082         return session;
1083     }
1084
1085     public ICanvasContext getCanvasContext() {
1086         return canvas;
1087     }
1088
1089     public IDiagram getDiagram() {
1090         return diagram;
1091     }
1092
1093     void setCanvasDirty() {
1094         ICanvasContext c = canvas;
1095         if (synchronizerState != State.LOADING && c != null && !c.isDisposed()) {
1096             // TODO: Consider adding an invocation limiter here, to prevent
1097             // calling setDirty too often if enough time hasn't passed yet since
1098             // the last invocation.
1099             c.getContentContext().setDirty();
1100         }
1101     }
1102
1103     /**
1104      * @param elementType
1105      * @return
1106      * @throws DatabaseException if ElementClass cannot be retrieved
1107      */
1108     public ElementClass getNodeClass(Resource elementType) throws DatabaseException {
1109         return getNodeClass(session, elementType);
1110     }
1111
1112     public ElementClass getNodeClass(RequestProcessor processor, Resource elementType) throws DatabaseException {
1113         ElementClass ec = processor.syncRequest(new NodeClassRequest(canvas, diagram, elementType, true));
1114         return ec;
1115     }
1116
1117     @Override
1118     protected void doDispose() {
1119         try {
1120             try {
1121                 stateLock.lock();
1122                 boolean isInitial = getState() == State.INITIAL;
1123                 activateState(State.DISPOSED, !isInitial);
1124             } finally {
1125                 stateLock.unlock();
1126             }
1127         } catch (InterruptedException e) {
1128             // Shouldn't happen.
1129             LOGGER.error("Dispose interrupted!", e);
1130         } finally {
1131             detachSessionListener();
1132
1133             if (profileObserver != null) {
1134                 profileObserver.dispose();
1135                 profileObserver = null;
1136             }
1137
1138             if (diagram != null) {
1139                 diagram.removeCompositionListener(diagramListener);
1140                 diagram.removeCompositionVetoListener(diagramListener);
1141             }
1142
1143             // TODO: we should probably leave the dataElement map as is since DataElementMap needs it even after the synchronizer has been disposed.
1144             // Currently the diagram's DataElementMap will be broken after disposal.
1145 //            dataElement.clear();
1146 //            dataConnection.clear();
1147
1148             if (layerManager != null) {
1149                 layerManager.dispose();
1150             }
1151
1152             // Let GC work.
1153             modificationQueue.dispose();
1154         }
1155     }
1156
1157     void initializeResources(ReadGraph graph) {
1158         this.br = new BasicResources(graph);
1159
1160         // Initialize synchronization context
1161         synchronizationContext = new GraphSynchronizationContext(graph, modificationQueue);
1162         synchronizationContext.set(SynchronizationHints.ERROR_HANDLER, errorHandler);
1163     }
1164
1165     // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1166     // LAYERS BEGIN
1167     // ------------------------------------------------------------------------
1168
1169     GraphLayerManager layerManager;
1170
1171     /**
1172      * A common handler for all elements that is used to listen to changes in
1173      * element visibility and focusability on diagram layers.
1174      */
1175     class ElementLayerListenerImpl implements ElementLayerListener {
1176         private static final long serialVersionUID = -3410052116598828129L;
1177
1178         @Override
1179         public void visibilityChanged(IElement e, ILayer layer, boolean visible) {
1180             if (!isAlive())
1181                 return;
1182             if (DebugPolicy.DEBUG_LAYERS)
1183                 System.out.println("visibility changed: " + e + ", " + layer + ", " + visible);
1184             GraphLayer gl = layerManager.getGraphLayer(layer.getName());
1185             if (gl != null) {
1186                 changeTag(e, gl.getVisible(), visible);
1187             }
1188         }
1189
1190         @Override
1191         public void focusabilityChanged(IElement e, ILayer layer, boolean focusable) {
1192             if (!isAlive())
1193                 return;
1194             if (DebugPolicy.DEBUG_LAYERS)
1195                 System.out.println("focusability changed: " + e + ", " + layer + ", " + focusable);
1196             GraphLayer gl = layerManager.getGraphLayer(layer.getName());
1197             if (gl != null) {
1198                 changeTag(e, gl.getFocusable(), focusable);
1199             }
1200         }
1201
1202         void changeTag(IElement e, Resource tag, boolean set) {
1203             Object object = e.getHint(ElementHints.KEY_OBJECT);
1204             Resource tagged = null;
1205             if (object instanceof Resource) {
1206                 tagged = (Resource) object;
1207             } else if (object instanceof EdgeResource) {
1208                 ConnectionEntity ce = e.getHint(ElementHints.KEY_CONNECTION_ENTITY);
1209                 if (ce instanceof ConnectionEntityImpl) {
1210                     tagged = ((ConnectionEntityImpl) ce).connection;
1211                 }
1212             }
1213             if (tagged == null)
1214                 return;
1215
1216             modificationQueue.async(new TagChange(tagged, tag, set), null);
1217         }
1218     };
1219
1220     ElementLayerListenerImpl elementLayerListener = new ElementLayerListenerImpl();
1221
1222     // ------------------------------------------------------------------------
1223     // LAYERS END
1224     // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1225
1226     @Override
1227     public IDiagram loadDiagram(IProgressMonitor progressMonitor, ReadGraph g, final String modelURI, final Resource diagram, final Resource runtime, final ResourceArray structuralPath,
1228             IHintObservable initialHints) throws DatabaseException {
1229         if (DebugPolicy.DEBUG_LOAD)
1230             System.out.println(Thread.currentThread() + " loadDiagram: " + NameUtils.getSafeName(g, diagram));
1231
1232         SubMonitor monitor = SubMonitor.convert(progressMonitor, "Load Diagram", 100);
1233
1234         Object loadTask = Timing.BEGIN("GDS.loadDiagram");
1235         try {
1236             try {
1237                 activateState(State.LOADING, false);
1238             } catch (IllegalStateException e) {
1239                 // Disposed already before loading even began.
1240                 this.diagram = Diagram.spawnNew(DiagramClass.DEFAULT);
1241                 return this.diagram;
1242             }
1243             try {
1244                 // Query for diagram class
1245                 Resource diagramClassResource = g.getPossibleType(diagram, br.DIA.Composite);
1246                 if (diagramClassResource != null) {
1247                     // Spawn new diagram
1248                     Object task = Timing.BEGIN("GDS.DiagramClassRequest");
1249                     final DiagramClass diagramClass = g.syncRequest(new DiagramClassRequest(diagram));
1250                     Timing.END(task);
1251                     final IDiagram d = Diagram.spawnNew(diagramClass);
1252                     {
1253                         d.setHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE, diagram);
1254                         if (runtime != null)
1255                             d.setHint(DiagramModelHints.KEY_DIAGRAM_RUNTIME_RESOURCE, runtime);
1256                         if (modelURI != null)
1257                             d.setHint(DiagramModelHints.KEY_DIAGRAM_MODEL_URI, modelURI);
1258                         d.setHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE_ARRAY, structuralPath);
1259
1260                         // Set dumb default routing when DiagramClass does not
1261                         // predefine the default connection routing for the diagram.
1262                         if (!d.containsHint(DiagramHints.ROUTE_ALGORITHM))
1263                             d.setHint(DiagramHints.ROUTE_ALGORITHM, RouterFactory.create(true, false));
1264
1265                         d.setHint(SynchronizationHints.CONTEXT, this);
1266
1267                         // Initialize hints with hints from initialHints if given
1268                         if (initialHints != null) {
1269                             d.setHints(initialHints.getHints());
1270                         }
1271                     }
1272
1273                     // ITask task2 = ThreadLogger.getInstance().begin("loadLayers");
1274                     monitor.subTask("Layers");
1275                     {
1276                         this.layerManager = new GraphLayerManager(g, modificationQueue, diagram);
1277                         synchronizationContext.set(GraphSynchronizationHints.GRAPH_LAYER_MANAGER, this.layerManager);
1278                         ILayersEditor layers = layerManager.loadLayers(d, g, diagram);
1279                         // task2.finish();
1280
1281                         d.setHint(DiagramHints.KEY_LAYERS, layers);
1282                         d.setHint(DiagramHints.KEY_LAYERS_EDITOR, layers);
1283
1284                         d.addCompositionVetoListener(diagramListener);
1285                         d.addCompositionListener(diagramListener);
1286
1287                         this.diagram = d;
1288
1289                         d.setHint(DiagramHints.KEY_MUTATOR, new DefaultDiagramMutator(d, diagram, synchronizationContext));
1290
1291                         // Add default layer if no layers exist.
1292                         // NOTE: this must be done after this.diagram has been set
1293                         // as it will trigger a graph modification which needs the
1294                         // diagram resource.
1295                         // ITask task3 = ThreadLogger.getInstance().begin("addDefaultLayer");
1296 //                        if (layers.getLayers().isEmpty()) {
1297 //                            if (DebugPolicy.DEBUG_LAYERS)
1298 //                                System.out.println("No layers, creating default layer '"
1299 //                                        + DiagramConstants.DEFAULT_LAYER_NAME + "'");
1300 //                            SimpleLayer defaultLayer = new SimpleLayer(DiagramConstants.DEFAULT_LAYER_NAME);
1301 //                            layers.addLayer(defaultLayer);
1302 //                            layers.activate(defaultLayer);
1303 //                        }
1304 //                        // task3.finish();
1305                     }
1306                     monitor.worked(10);
1307
1308                     monitor.subTask("Contents");
1309                     // Discover the plain resources that form the content of the
1310                     // diagram through a separate query. This allows us to
1311                     // separately
1312                     // track changes to the diagram structure itself, not the
1313                     // substructures contained by the structure elements.
1314                     ITask task4 = ThreadLogger.getInstance().begin("DiagramContentRequest1");
1315                     DiagramContentRequest query = new DiagramContentRequest(canvas, diagram, errorHandler);
1316                     g.syncRequest(query, new DiagramContentListener(diagram));
1317                     task4.finish();
1318                     // ITask task5 = ThreadLogger.getInstance().begin("DiagramContentRequest2");
1319                     ITask task42 = ThreadLogger.getInstance().begin("DiagramContentRequest2");
1320                     DiagramContents contents = g.syncRequest(query, TransientCacheAsyncListener.instance());
1321                     //System.err.println("contents: " + contents);
1322                     task42.finish();
1323                     // task5.finish();
1324                     monitor.worked(10);
1325
1326                     monitor.subTask("Graphical elements");
1327                     {
1328                         Object applyDiagramContents = Timing.BEGIN("GDS.applyDiagramContents");
1329                         ITask task6 = ThreadLogger.getInstance().begin("applyDiagramContents");
1330                         processGraphUpdates(g, Collections.singleton(diagramGraphUpdater(contents)));
1331                         task6.finish();
1332                         Timing.END(applyDiagramContents);
1333                     }
1334                     monitor.worked(80);
1335
1336                     DataNodeMap dn = new DataNodeMap() {
1337                         @Override
1338                         public INode getNode(Object data) {
1339                             if (DataNodeConstants.CANVAS_ROOT == data)
1340                                 return canvas.getCanvasNode();
1341                             if (DataNodeConstants.DIAGRAM_ELEMENT_PARENT == data) {
1342                                 ElementPainter ep = canvas.getAtMostOneItemOfClass(ElementPainter.class);
1343                                 return ep != null ? ep.getDiagramElementParentNode() : null;
1344                             }
1345
1346                             DataElementMap emap = GraphToDiagramSynchronizer.this.diagram.getDiagramClass().getSingleItem(DataElementMap.class);
1347                             IElement element = emap.getElement(GraphToDiagramSynchronizer.this.diagram, data);
1348                             if(element == null) return null;
1349                             return element.getHint(ElementHints.KEY_SG_NODE);
1350                         }
1351                     };
1352
1353                     profileObserver = new ProfileObserver(g.getSession(), runtime,
1354                             canvas.getThreadAccess(), canvas, canvas.getSceneGraph(), diagram, 
1355                             ArrayMap.keys(ProfileKeys.DIAGRAM, ProfileKeys.CANVAS, ProfileKeys.NODE_MAP).values(GraphToDiagramSynchronizer.this.diagram, canvas, dn),
1356                             new CanvasNotification(canvas));
1357
1358                     g.getSession().asyncRequest(new AsyncReadRequest() {
1359                         @Override
1360                         public void run(AsyncReadGraph graph) {
1361                           ProfileObserver po = profileObserver;
1362                             if (po != null)
1363                                 po.listen(graph, GraphToDiagramSynchronizer.this);
1364                             else
1365                                 LOGGER.info("profileObserver has been disposed already!");
1366                         }
1367                     });
1368
1369                     return d;
1370
1371                 }
1372
1373                 this.diagram = Diagram.spawnNew(DiagramClass.DEFAULT);
1374                 return this.diagram;
1375
1376             } finally {
1377                 idle();
1378             }
1379         } catch (InterruptedException e) {
1380             throw new RuntimeException(e);
1381         } catch (IllegalStateException e) {
1382             // If the synchronizer was disposed ahead of time, it was done
1383             // for a reason, such as the user having closed the owner editor.
1384             if (!isAlive())
1385                 throw new CancelTransactionException(e);
1386             throw new RuntimeException(e);
1387         } finally {
1388             Timing.END(loadTask);
1389         }
1390     }
1391
1392     static class CanvasNotification implements Runnable {
1393
1394         final private ICanvasContext canvas;
1395
1396         public CanvasNotification(ICanvasContext canvas) {
1397             this.canvas = canvas;
1398         }
1399
1400         public void run() {
1401             canvas.getContentContext().setDirty();
1402         }
1403
1404     }
1405
1406     ArrayList<IModification>        pendingModifications = new ArrayList<IModification>();
1407     MapSet<IElement, IModification> modificationIndex    = new MapSet.Hash<IElement, IModification>();
1408
1409     void addModification(IElement element, IModification modification) {
1410         pendingModifications.add(modification);
1411         if (element != null)
1412             modificationIndex.add(element, modification);
1413
1414     }
1415     class DefaultDiagramMutator implements DiagramMutator {
1416
1417         Map<IElement, Resource> creation = new HashMap<IElement, Resource>();
1418
1419         IDiagram d;
1420         Resource diagram;
1421
1422         IModifiableSynchronizationContext synchronizationContext;
1423
1424         public DefaultDiagramMutator(IDiagram d, Resource diagram, IModifiableSynchronizationContext synchronizationContext) {
1425             this.d = d;
1426             this.diagram = diagram;
1427             this.synchronizationContext = synchronizationContext;
1428
1429             if (synchronizationContext.get(SynchronizationHints.ELEMENT_CLASS_PROVIDER) == null)
1430                 throw new IllegalArgumentException("SynchronizationHints.ELEMENT_CLASS_PROVIDER not available");
1431         }
1432
1433         void assertNotDisposed() {
1434             if (!isAlive())
1435                 throw new IllegalStateException(getClass().getSimpleName() + " is disposed");
1436         }
1437
1438         @Override
1439         public IElement newElement(ElementClass clazz) {
1440             assertNotDisposed();
1441             ElementFactory ef = d.getDiagramClass().getAtMostOneItemOfClass(ElementFactory.class);
1442             IElement element = null;
1443             if (ef != null)
1444                 element = ef.spawnNew(clazz);
1445             else
1446                 element = Element.spawnNew(clazz);
1447
1448             element.setHint(ElementHints.KEY_OBJECT, new TransientElementObject());
1449
1450             addModification(element, new AddElement(synchronizationContext, d, element));
1451
1452             return element;
1453         }
1454
1455         @Override
1456         public void commit() {
1457             assertNotDisposed();
1458             if (DebugPolicy.DEBUG_MUTATOR_COMMIT) {
1459                 System.out.println("DiagramMutator is about to commit changes:");
1460                 for (IModification mod : pendingModifications)
1461                     System.out.println("\t- " + mod);
1462             }
1463
1464             Collections.sort(pendingModifications);
1465
1466             if (DebugPolicy.DEBUG_MUTATOR_COMMIT) {
1467                 if (pendingModifications.size() > 1) {
1468                     System.out.println("* changes were re-ordered to:");
1469                     for (IModification mod : pendingModifications)
1470                         System.out.println("\t" + mod);
1471                 }
1472             }
1473
1474             Timing.safeTimed(errorHandler, "QUEUE AND WAIT FOR MODIFICATIONS TO FINISH", new GTask() {
1475                 @Override
1476                 public void run() throws DatabaseException {
1477                     // Performs a separate write request and query result update
1478                     // for each modification
1479 //                    for (IModification mod : pendingModifications) {
1480 //                        try {
1481 //                            modificationQueue.sync(mod);
1482 //                        } catch (InterruptedException e) {
1483 //                            error("Pending diagram modification " + mod
1484 //                                    + " was interrupted. See exception for details.", e);
1485 //                        }
1486 //                    }
1487
1488                     // NOTE: this is still under testing, the author is not
1489                     // truly certain that it should work in all cases ATM.
1490
1491                     // Performs all modifications with in a single write request
1492                     for (IModification mod : pendingModifications) {
1493                         modificationQueue.offer(mod, null);
1494                     }
1495                     try {
1496                         // Perform the modifications in a single request.
1497                         modificationQueue.finish();
1498                     } catch (InterruptedException e) {
1499                         errorHandler.error("Diagram modification finishing was interrupted. See exception for details.", e);
1500                     }
1501                 }
1502             });
1503             pendingModifications.clear();
1504             modificationIndex.clear();
1505             creation.clear();
1506             if (DebugPolicy.DEBUG_MUTATOR_COMMIT)
1507                 System.out.println("DiagramMutator has committed");
1508         }
1509
1510         @Override
1511         public void clear() {
1512             assertNotDisposed();
1513             pendingModifications.clear();
1514             modificationIndex.clear();
1515             creation.clear();
1516             if (DebugPolicy.DEBUG_MUTATOR)
1517                 System.out.println("DiagramMutator has been cleared");
1518         }
1519
1520         @Override
1521         public void modifyTransform(IElement element) {
1522             assertNotDisposed();
1523             Resource resource = backendObject(element);
1524             AffineTransform tr = element.getHint(ElementHints.KEY_TRANSFORM);
1525             if (resource != null && tr != null) {
1526                 addModification(element, new TransformElement(resource, tr));
1527             }
1528         }
1529
1530         @Override
1531         public void synchronizeHintsToBackend(IElement element) {
1532             assertNotDisposed();
1533             IHintSynchronizer synchronizer = element.getHint(SynchronizationHints.HINT_SYNCHRONIZER);
1534             if (synchronizer != null) {
1535                 CollectingModificationQueue queue = new CollectingModificationQueue();
1536                 synchronizer.synchronize(synchronizationContext, element);
1537                 addModification(element, new CompositeModification(ModificationAdapter.LOW_PRIORITY, queue.getQueue()));
1538             }
1539         }
1540
1541         @Override
1542         public void synchronizeElementOrder() {
1543             assertNotDisposed();
1544             List<IElement> snapshot = d.getSnapshot();
1545             addModification(null, new ElementReorder(d, snapshot));
1546         }
1547
1548         @Override
1549         public void register(IElement element, Object object) {
1550             creation.put(element, (Resource) object);
1551         }
1552
1553         @SuppressWarnings("unchecked")
1554         @Override
1555         public <T> T backendObject(IElement element) {
1556             Object object = ElementUtils.getObject(element);
1557             if (object instanceof Resource)
1558                 return (T) object;
1559             else
1560                 return (T) creation.get(element);
1561         }
1562
1563     }
1564
1565     // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1566     // GRAPH TO DIAGRAM SYCHRONIZATION LOGIC BEGIN
1567     // ------------------------------------------------------------------------
1568
1569     static class ConnectionData {
1570         ConnectionEntityImpl impl;
1571         List<Resource>       branchPoints = new ArrayList<Resource>();
1572         List<EdgeResource>   segments     = new ArrayList<EdgeResource>();
1573
1574         ConnectionData(ConnectionEntityImpl ce) {
1575             this.impl = ce;
1576         }
1577
1578         void addBranchPoint(Resource bp) {
1579             branchPoints.add(bp);
1580         }
1581
1582         void addSegment(EdgeResource seg) {
1583             segments.add(seg);
1584         }
1585     }
1586
1587     class GraphToDiagramUpdater {
1588         DiagramContents                                 lastContent;
1589         DiagramContents                                 content;
1590         DiagramContentChanges                           changes;
1591
1592         final List<IElement>                            addedElements;
1593         final List<IElement>                            removedElements;
1594
1595         final List<IElement>                            addedConnectionSegments;
1596         final List<IElement>                            removedConnectionSegments;
1597
1598         final List<IElement>                            addedBranchPoints;
1599         final List<IElement>                            removedBranchPoints;
1600
1601         final Map<Object, IElement>                     addedElementMap;
1602         final Map<Resource, IElement>                   addedConnectionMap;
1603         final Map<Resource, ConnectionEntityImpl>       addedConnectionEntities;
1604         final List<Resource>                            removedConnectionEntities;
1605         final Map<ConnectionEntityImpl, ConnectionData> changedConnectionEntities;
1606
1607         final Map<Resource, IElement>                   addedRouteGraphConnectionMap;
1608         final List<IElement>                            removedRouteGraphConnections;
1609
1610
1611         GraphToDiagramUpdater(DiagramContents lastContent, DiagramContents content, DiagramContentChanges changes) {
1612             this.lastContent = lastContent;
1613             this.content = content;
1614             this.changes = changes;
1615
1616             this.addedElements = new ArrayList<IElement>(changes.elements.size() + changes.branchPoints.size());
1617             this.removedElements = new ArrayList<IElement>(changes.elements.size() + changes.branchPoints.size());
1618             this.addedConnectionSegments = new ArrayList<IElement>(content.connectionSegments.size());
1619             this.removedConnectionSegments = new ArrayList<IElement>(content.connectionSegments.size());
1620             this.addedBranchPoints = new ArrayList<IElement>(content.branchPoints.size());
1621             this.removedBranchPoints = new ArrayList<IElement>(content.branchPoints.size());
1622             this.addedElementMap = new HashMap<Object, IElement>();
1623             this.addedConnectionMap = new HashMap<Resource, IElement>();
1624             this.addedConnectionEntities = new HashMap<Resource, ConnectionEntityImpl>();
1625             this.removedConnectionEntities = new ArrayList<Resource>(changes.connections.size());
1626             this.changedConnectionEntities = new HashMap<ConnectionEntityImpl, ConnectionData>();
1627             this.addedRouteGraphConnectionMap = new HashMap<Resource, IElement>();
1628             this.removedRouteGraphConnections = new ArrayList<IElement>(changes.routeGraphConnections.size());
1629         }
1630
1631         public void clear() {
1632             // Prevent DiagramContents leakage through DisposableListeners.
1633             lastContent = null;
1634             content = null;
1635             changes = null;
1636
1637             this.addedElements.clear();
1638             this.removedElements.clear();
1639             this.addedConnectionSegments.clear();
1640             this.removedConnectionSegments.clear();
1641             this.addedBranchPoints.clear();
1642             this.removedBranchPoints.clear();
1643             this.addedElementMap.clear();
1644             this.addedConnectionMap.clear();
1645             this.addedConnectionEntities.clear();
1646             this.removedConnectionEntities.clear();
1647             this.changedConnectionEntities.clear();
1648             this.addedRouteGraphConnectionMap.clear();
1649             this.removedRouteGraphConnections.clear();
1650         }
1651
1652         void processNodes(AsyncReadGraph graph) throws DatabaseException {
1653
1654             for (Map.Entry<Resource, Change> entry : changes.elements.entrySet()) {
1655
1656                 final Resource element = entry.getKey();
1657                 Change change = entry.getValue();
1658
1659                 switch (change) {
1660                     case ADDED: {
1661                         IElement mappedElement = getMappedElement(element);
1662                         if (mappedElement == null) {
1663                             if (DebugPolicy.DEBUG_NODE_LOAD)
1664                                 graph.syncRequest(new ReadRequest() {
1665                                     @Override
1666                                     public void run(ReadGraph graph) throws DatabaseException {
1667                                         System.out.println("    EXTERNALLY ADDED ELEMENT: "
1668                                                 + NameUtils.getSafeName(graph, element) + " ("
1669                                                 + element.getResourceId() + ")");
1670                                     }
1671                                 });
1672
1673                             if (content.connectionSet.contains(element)) {
1674
1675                                 // TODO: Connection loading has no listening, changes :Connection will not be noticed by this code!
1676                                 Listener<IElement> loadListener = new DisposableListener<IElement>(canvasListenerSupport) {
1677
1678                                     boolean firstTime = true;
1679
1680                                     @Override
1681                                     public String toString() {
1682                                         return "Connection load listener for " + element;
1683                                     }
1684                                     @Override
1685                                     public void execute(IElement loaded) {
1686                                         // Invoked when the element has been loaded.
1687                                         if (DebugPolicy.DEBUG_CONNECTION_LISTENER)
1688                                             System.out.println("CONNECTION LoadListener for " + loaded);
1689
1690                                         if (loaded == null) {
1691                                             disposeListener();
1692                                             return;
1693                                         }
1694
1695                                         if (firstTime) {
1696
1697                                             mapElement(element, loaded);
1698                                             synchronized (GraphToDiagramUpdater.this) {
1699                                                 addedElements.add(loaded);
1700                                                 addedElementMap.put(element, loaded);
1701                                                 addedConnectionMap.put(element, loaded);
1702                                             }
1703
1704                                             firstTime = false;
1705
1706                                         }
1707
1708                                         Object data = loaded.getHint(ElementHints.KEY_OBJECT);
1709
1710                                         // Logic for disposing listener
1711                                         if (!previousContent.connectionSet.contains(data)) {
1712                                             if (DebugPolicy.DEBUG_CONNECTION_LISTENER)
1713                                                 System.out.println("CONNECTION LoadListener, connection not in current content: " + data + ". Disposing.");
1714                                             disposeListener();
1715                                             return;
1716                                         }
1717
1718                                         if (addedElementMap.containsKey(data)) {
1719                                             // This element was just loaded, in
1720                                             // which case its hints need to
1721                                             // uploaded to the real mapped
1722                                             // element immediately.
1723                                             IElement mappedElement = getMappedElement(data);
1724                                             if (DebugPolicy.DEBUG_CONNECTION_LISTENER)
1725                                                 System.out.println("LOADED ADDED CONNECTION, currently mapped connection: " + mappedElement);
1726                                             if (mappedElement != null && (mappedElement instanceof Element)) {
1727                                                 if (DebugPolicy.DEBUG_CONNECTION_LISTENER) {
1728                                                     System.out.println("  mapped hints: " + mappedElement.getHints());
1729                                                     System.out.println("  loaded hints: " + loaded.getHints());
1730                                                 }
1731                                                 updateMappedElement((Element) mappedElement, loaded);
1732                                             }
1733                                         } else {
1734                                             // This element was already loaded.
1735                                             // Just schedule an update some time
1736                                             // in the future.
1737                                             if (DebugPolicy.DEBUG_CONNECTION_LISTENER)
1738                                                 System.out.println("PREVIOUSLY LOADED CONNECTION UPDATED, scheduling update into the future");
1739                                             offerGraphUpdate( connectionUpdater(element, loaded) );
1740                                         }
1741                                     }
1742                                 };
1743
1744                                 graph.asyncRequest(new ConnectionRequest(canvas, diagram, element, errorHandler, loadListener), new AsyncProcedure<IElement>() {
1745                                     @Override
1746                                     public void execute(AsyncReadGraph graph, final IElement e) {
1747
1748                                         // Read connection type
1749                                         graph.forSingleType(element, br.DIA.Connection, new Procedure<Resource>() {
1750                                             @Override
1751                                             public void exception(Throwable t) {
1752                                                 error(t);
1753                                             }
1754
1755                                             @Override
1756                                             public void execute(Resource connectionType) {
1757                                                 synchronized (GraphToDiagramUpdater.this) {
1758                                                     IElement mapped = getMappedElement(element);
1759                                                     assert(mapped != null);
1760                                                     
1761                                                     if (DebugPolicy.DEBUG_CONNECTION_LISTENER)
1762                                                         System.out.println("CONNECTION ENTITY CREATED " + e + " " + element);
1763
1764                                                     //System.out.println("new connection entity " + e);
1765                                                     ConnectionEntityImpl entity = new ConnectionEntityImpl(element, connectionType, mapped);
1766                                                     mapped.setHint(ElementHints.KEY_CONNECTION_ENTITY, entity);
1767                                                     addedConnectionEntities.put(element, entity);
1768                                                 }
1769                                             }
1770                                         });
1771
1772                                     }
1773
1774                                     @Override
1775                                     public void exception(AsyncReadGraph graph, Throwable throwable) {
1776                                         error(throwable);
1777                                     }
1778                                 });
1779                             } else if (content.nodeSet.contains(element)) {
1780
1781                                 Listener<IElement> loadListener = new DisposableListener<IElement>(canvasListenerSupport) {
1782
1783                                     boolean firstTime = true;
1784
1785                                     @Override
1786                                     public String toString() {
1787                                         return "Node load listener for " + element;
1788                                     }
1789                                     @Override
1790                                     public void execute(IElement loaded) {
1791                                         // Invoked when the element has been loaded.
1792                                         if (DebugPolicy.DEBUG_NODE_LISTENER)
1793                                             System.out.println("NODE LoadListener for " + loaded);
1794
1795                                         if (loaded == null) {
1796                                             disposeListener();
1797                                             return;
1798                                         }
1799
1800                                         if (firstTime) {
1801
1802                                             // This is invoked before the element is actually loaded.
1803                                             //System.out.println("NodeRequestProcedure " + e);
1804                                             if (DebugPolicy.DEBUG_NODE_LOAD)
1805                                                 System.out.println("MAPPING ADDED NODE: " + element + " -> " + loaded);
1806                                             mapElement(element, loaded);
1807                                             synchronized (GraphToDiagramUpdater.this) {
1808                                                 addedElements.add(loaded);
1809                                                 addedElementMap.put(element, loaded);
1810                                             }
1811
1812                                             firstTime = false;
1813
1814                                         }
1815
1816                                         Object data = loaded.getHint(ElementHints.KEY_OBJECT);
1817
1818                                         // Logic for disposing listener
1819                                         if (!previousContent.nodeSet.contains(data)) {
1820                                             if (DebugPolicy.DEBUG_NODE_LISTENER)
1821                                                 System.out.println("NODE LoadListener, node not in current content: " + data + ". Disposing.");
1822                                             disposeListener();
1823                                             return;
1824                                         }
1825
1826                                         if (addedElementMap.containsKey(data)) {
1827                                             // This element was just loaded, in
1828                                             // which case its hints need to
1829                                             // uploaded to the real mapped
1830                                             // element immediately.
1831                                             IElement mappedElement = getMappedElement(data);
1832                                             if (DebugPolicy.DEBUG_NODE_LISTENER)
1833                                                 System.out.println("LOADED ADDED ELEMENT, currently mapped element: " + mappedElement);
1834                                             if (mappedElement != null && (mappedElement instanceof Element)) {
1835                                                 if (DebugPolicy.DEBUG_NODE_LISTENER) {
1836                                                     System.out.println("  mapped hints: " + mappedElement.getHints());
1837                                                     System.out.println("  loaded hints: " + loaded.getHints());
1838                                                 }
1839                                                 updateMappedElement((Element) mappedElement, loaded);
1840                                             }
1841                                         } else {
1842                                             // This element was already loaded.
1843                                             // Just schedule an update some time
1844                                             // in the future.
1845                                             if (DebugPolicy.DEBUG_NODE_LISTENER)
1846                                                 System.out.println("PREVIOUSLY LOADED NODE UPDATED, scheduling update into the future");
1847                                             offerGraphUpdate( nodeUpdater(element, loaded) );
1848                                         }
1849                                     }
1850                                 };
1851
1852                                 //System.out.println("NODE REQUEST: " + element);
1853                                 graph.asyncRequest(new NodeRequest(canvas, diagram, element, loadListener), new AsyncProcedure<IElement>() {
1854                                     @Override
1855                                     public void execute(AsyncReadGraph graph, IElement e) {
1856                                     }
1857
1858                                     @Override
1859                                     public void exception(AsyncReadGraph graph, Throwable throwable) {
1860                                         error(throwable);
1861                                     }
1862                                 });
1863
1864                             } else {
1865 //                                warning("Diagram elements must be either elements or connections, "
1866 //                                        + NameUtils.getSafeName(g, element) + " is neither",
1867 //                                        new AssumptionException(""));
1868                             }
1869                         }
1870                         break;
1871                     }
1872                     case REMOVED: {
1873                         IElement e = getMappedElement(element);
1874                         if (DebugPolicy.DEBUG_NODE_LOAD)
1875                             graph.syncRequest(new ReadRequest() {
1876                                 @Override
1877                                 public void run(ReadGraph graph) throws DatabaseException {
1878                                     System.out.println("    EXTERNALLY REMOVED ELEMENT: "
1879                                             + NameUtils.getSafeName(graph, element) + " ("
1880                                             + element.getResourceId() + ")");
1881                                 }
1882                             });
1883                         if (e != null) {
1884                             removedElements.add(e);
1885                         }
1886                         break;
1887                     }
1888                     default:
1889                 }
1890             }
1891         }
1892
1893         void gatherChangedConnectionParts(Map<?, Change> changes) {
1894             for (Map.Entry<?, Change> entry : changes.entrySet()) {
1895                 Object part = entry.getKey();
1896                 Change change = entry.getValue();
1897
1898                 switch (change) {
1899                     case ADDED: {
1900                         synchronized (GraphToDiagramUpdater.this) {
1901                             Resource connection = content.partToConnection.get(part);
1902                             assert connection != null;
1903
1904                             IElement ce = getMappedElement(connection);
1905                             if (ce == null)
1906                                 ce = addedElementMap.get(connection);
1907
1908                             if (ce != null)
1909                                 markConnectionChanged(ce);
1910                             break;
1911                         }
1912                     }
1913                     case REMOVED: {
1914                         if (lastContent == null)
1915                             break;
1916                         Resource connection = lastContent.partToConnection.get(part);
1917                         if (connection != null && content.connectionSet.contains(connection)) {
1918                             markConnectionChanged(connection);
1919                         }
1920                         break;
1921                     }
1922                     default:
1923                 }
1924             }
1925         }
1926
1927         void markConnectionChanged(Resource connection) {
1928 //            System.out.println("markConnectionChanged");
1929             ConnectionEntityImpl ce = getMappedConnection(connection);
1930             if (ce != null) {
1931                 markConnectionChanged(ce);
1932                 return;
1933             }
1934             error("WARNING: marking connection entity " + connection
1935                     + " changed, but the connection was not previously mapped",
1936                     new Exception("created exception to get a stack trace"));
1937         }
1938
1939         void markConnectionChanged(IElement connection) {
1940             ConnectionEntityImpl entity = connection.getHint(ElementHints.KEY_CONNECTION_ENTITY);
1941             if (entity != null)
1942                 markConnectionChanged(entity);
1943         }
1944
1945         void markConnectionChanged(ConnectionEntityImpl ce) {
1946             if (!changedConnectionEntities.containsKey(ce)) {
1947                 changedConnectionEntities.put(ce, new ConnectionData(ce));
1948             }
1949         }
1950
1951         void processConnections() {
1952             // Find added/removed connection segments/branch points
1953             // in order to find all changed connection entities.
1954             gatherChangedConnectionParts(changes.connectionSegments);
1955             gatherChangedConnectionParts(changes.branchPoints);
1956
1957             // Find removed connection entities
1958             for (Map.Entry<Resource, Change> entry : changes.connections.entrySet()) {
1959                 Resource ce = entry.getKey();
1960                 Change change = entry.getValue();
1961
1962                 switch (change) {
1963                     case REMOVED: {
1964                         removedConnectionEntities.add(ce);
1965                     }
1966                     default:
1967                 }
1968             }
1969
1970             // Generate update data of changed connection entities.
1971             // This ConnectionData will be applied in the canvas thread
1972             // diagram updater.
1973             for (ConnectionData cd : changedConnectionEntities.values()) {
1974                 for (Object part : content.connectionToParts.getValuesUnsafe(cd.impl.connection)) {
1975                     if (part instanceof Resource) {
1976                         cd.branchPoints.add((Resource) part);
1977                     } else if (part instanceof EdgeResource) {
1978                         cd.segments.add((EdgeResource) part);
1979                     }
1980                 }
1981             }
1982         }
1983
1984         void processRouteGraphConnections(ReadGraph graph) throws DatabaseException {
1985             for (Map.Entry<Resource, Change> entry : changes.routeGraphConnections.entrySet()) {
1986                 final Resource connection = entry.getKey();
1987
1988                 Change change = entry.getValue();
1989                 switch (change) {
1990                     case ADDED: {
1991                         IElement mappedElement = getMappedElement(connection);
1992                         if (mappedElement != null)
1993                             continue;
1994
1995                         Listener<IElement> loadListener = new DisposableListener<IElement>(canvasListenerSupport) {
1996
1997                             boolean firstTime = true;
1998
1999                             @Override
2000                             public String toString() {
2001                                 return "processRouteGraphConnections " + connection;
2002                             }
2003                             @Override
2004                             public void execute(IElement loaded) {
2005                                 // Invoked when the element has been loaded.
2006                                 if (DebugPolicy.DEBUG_CONNECTION_LISTENER)
2007                                     System.out.println("ROUTE GRAPH CONNECTION LoadListener for " + loaded);
2008
2009                                 if (loaded == null) {
2010                                     disposeListener();
2011                                     return;
2012                                 }
2013
2014                                 if(firstTime) {
2015                                     if (DebugPolicy.DEBUG_NODE_LOAD)
2016                                         System.out.println("MAPPING ADDED ROUTE GRAPH CONNECTION: " + connection + " -> " + loaded);
2017                                     mapElement(connection, loaded);
2018                                     synchronized (GraphToDiagramUpdater.this) {
2019                                         addedElements.add(loaded);
2020                                         addedElementMap.put(connection, loaded);
2021                                         addedRouteGraphConnectionMap.put(connection, loaded);
2022                                     }
2023                                     firstTime = false;
2024                                 }
2025
2026                                 Object data = loaded.getHint(ElementHints.KEY_OBJECT);
2027
2028                                 // Logic for disposing listener
2029                                 if (!previousContent.routeGraphConnectionSet.contains(data)) {
2030                                     if (DebugPolicy.DEBUG_CONNECTION_LISTENER)
2031                                         System.out.println("ROUTE GRAPH CONNECTION LoadListener, connection not in current content: " + data + ". Disposing.");
2032                                     disposeListener();
2033                                     return;
2034                                 }
2035
2036                                 if (addedElementMap.containsKey(data)) {
2037                                     // This element was just loaded, in
2038                                     // which case its hints need to
2039                                     // uploaded to the real mapped
2040                                     // element immediately.
2041                                     IElement mappedElement = getMappedElement(data);
2042                                     if (DebugPolicy.DEBUG_CONNECTION_LISTENER)
2043                                         System.out.println("LOADED ADDED ROUTE GRAPH CONNECTION, currently mapped connection: " + mappedElement);
2044                                     if (mappedElement instanceof Element) {
2045                                         if (DebugPolicy.DEBUG_CONNECTION_LISTENER) {
2046                                             System.out.println("  mapped hints: " + mappedElement.getHints());
2047                                             System.out.println("  loaded hints: " + loaded.getHints());
2048                                         }
2049                                         updateMappedElement((Element) mappedElement, loaded);
2050                                     }
2051                                 } else {
2052                                     // This element was already loaded.
2053                                     // Just schedule an update some time
2054                                     // in the future.
2055                                     if (DebugPolicy.DEBUG_CONNECTION_LISTENER)
2056                                         System.out.println("PREVIOUSLY LOADED ROUTE GRAPH CONNECTION UPDATED, scheduling update into the future: " + connection);
2057
2058                                     Set<Object> dirtyNodes = new THashSet<Object>(4);
2059                                     IElement mappedElement = getMappedElement(connection);
2060                                     ConnectionEntity ce = mappedElement.getHint(ElementHints.KEY_CONNECTION_ENTITY);
2061                                     if (ce != null) {
2062                                         for (Connection conn : ce.getTerminalConnections(null)) {
2063                                             Object o = conn.node.getHint(ElementHints.KEY_OBJECT);
2064                                             if (o != null) {
2065                                                 dirtyNodes.add(o);
2066                                                 if (DebugPolicy.DEBUG_CONNECTION_LISTENER)
2067                                                     System.out.println("Marked connectivity dirty for node: " + conn.node);
2068                                             }
2069                                         }
2070                                     }
2071
2072                                     offerGraphUpdate( routeGraphConnectionUpdater(connection, loaded, dirtyNodes) );
2073                                 }
2074                             }
2075                         };
2076
2077                         graph.syncRequest(new ConnectionRequest(canvas, diagram, connection, errorHandler, loadListener), new Procedure<IElement>() {
2078                             @Override
2079                             public void execute(final IElement e) {
2080                             }
2081                             @Override
2082                             public void exception(Throwable throwable) {
2083                                 error(throwable);
2084                             }
2085                         });
2086                         break;
2087                     }
2088                     case REMOVED: {
2089                         IElement e = getMappedElement(connection);
2090                         if (e != null)
2091                             removedRouteGraphConnections.add(e);
2092                         break;
2093                     }
2094                     default:
2095                 }
2096             }
2097         }
2098
2099         ConnectionEntityImpl getConnectionEntity(Object connectionPart) {
2100             Resource connection = content.partToConnection.get(connectionPart);
2101             assert connection != null;
2102             ConnectionEntityImpl ce = addedConnectionEntities.get(connection);
2103             if (ce != null)
2104                 return ce;
2105             return assertMappedConnection(connection);
2106         }
2107
2108         void processBranchPoints(AsyncReadGraph graph) throws DatabaseException {
2109             for (Map.Entry<Resource, Change> entry : changes.branchPoints.entrySet()) {
2110
2111                 final Resource element = entry.getKey();
2112                 Change change = entry.getValue();
2113
2114                 switch (change) {
2115                     case ADDED: {
2116                         IElement mappedElement = getMappedElement(element);
2117                         if (mappedElement == null) {
2118                             if (DebugPolicy.DEBUG_NODE_LOAD)
2119                                 graph.asyncRequest(new ReadRequest() {
2120                                     @Override
2121                                     public void run(ReadGraph graph) throws DatabaseException {
2122                                         System.out.println("    EXTERNALLY ADDED BRANCH POINT: "
2123                                                 + NameUtils.getSafeName(graph, element) + " ("
2124                                                 + element.getResourceId() + ")");
2125                                     }
2126                                 });
2127
2128                             Listener<IElement> loadListener = new DisposableListener<IElement>(canvasListenerSupport) {
2129
2130                                 boolean firstTime = true;
2131
2132                                 @Override
2133                                 public String toString() {
2134                                     return "processBranchPoints for " + element;
2135                                 }
2136                                 @Override
2137                                 public void execute(IElement loaded) {
2138                                     // Invoked when the element has been loaded.
2139                                     if (DebugPolicy.DEBUG_NODE_LISTENER)
2140                                         System.out.println("BRANCH POINT LoadListener for " + loaded);
2141
2142                                     if (loaded == null) {
2143                                         disposeListener();
2144                                         return;
2145                                     }
2146
2147                                     if (firstTime) {
2148
2149                                         mapElement(element, loaded);
2150                                         synchronized (GraphToDiagramUpdater.this) {
2151                                             addedBranchPoints.add(loaded);
2152                                             addedElementMap.put(element, loaded);
2153                                             ConnectionEntityImpl ce = getConnectionEntity(element);
2154                                             loaded.setHint(ElementHints.KEY_CONNECTION_ENTITY, ce);
2155                                             loaded.setHint(ElementHints.KEY_PARENT_ELEMENT, ce.getConnectionElement());
2156                                         }
2157
2158                                         firstTime = false;
2159
2160                                     }
2161
2162                                     Object data = loaded.getHint(ElementHints.KEY_OBJECT);
2163                                     if (addedElementMap.containsKey(data)) {
2164                                         // This element was just loaded, in
2165                                         // which case its hints need to
2166                                         // uploaded to the real mapped
2167                                         // element immediately.
2168                                         IElement mappedElement = getMappedElement(data);
2169                                         if (DebugPolicy.DEBUG_NODE_LISTENER)
2170                                             System.out.println("LOADED ADDED BRANCH POINT, currently mapped element: " + mappedElement);
2171                                         if (mappedElement != null && (mappedElement instanceof Element)) {
2172                                             if (DebugPolicy.DEBUG_NODE_LISTENER) {
2173                                                 System.out.println("  mapped hints: " + mappedElement.getHints());
2174                                                 System.out.println("  loaded hints: " + loaded.getHints());
2175                                             }
2176                                             updateMappedElement((Element) mappedElement, loaded);
2177                                         }
2178                                     } else {
2179                                         // This element was already loaded.
2180                                         // Just schedule an update some time
2181                                         // in the future.
2182                                         if (DebugPolicy.DEBUG_NODE_LISTENER)
2183                                             System.out.println("PREVIOUSLY LOADED BRANCH POINT UPDATED, scheduling update into the future");
2184                                         offerGraphUpdate( nodeUpdater(element, loaded) );
2185                                     }
2186                                 }
2187                             };
2188
2189                             graph.asyncRequest(new NodeRequest(canvas, diagram, element, loadListener), new AsyncProcedure<IElement>() {
2190                                 @Override
2191                                 public void execute(AsyncReadGraph graph, IElement e) {
2192                                 }
2193
2194                                 @Override
2195                                 public void exception(AsyncReadGraph graph, Throwable throwable) {
2196                                     error(throwable);
2197                                 }
2198                             });
2199                         }
2200                         break;
2201                     }
2202                     case REMOVED: {
2203                         IElement e = getMappedElement(element);
2204                         if (DebugPolicy.DEBUG_NODE_LOAD)
2205                             graph.syncRequest(new ReadRequest() {
2206                                 @Override
2207                                 public void run(ReadGraph graph) throws DatabaseException {
2208                                     System.out.println("    EXTERNALLY REMOVED BRANCH POINT: "
2209                                             + NameUtils.getSafeName(graph, element) + " ("
2210                                             + element.getResourceId() + ")");
2211                                 }
2212                             });
2213                         if (e != null) {
2214                             removedBranchPoints.add(e);
2215                         }
2216                         break;
2217                     }
2218                     default:
2219                 }
2220             }
2221         }
2222
2223         void processConnectionSegments(AsyncReadGraph graph) throws DatabaseException {
2224             ConnectionSegmentAdapter adapter = connectionSegmentAdapter;
2225
2226             for (Map.Entry<EdgeResource, Change> entry : changes.connectionSegments.entrySet()) {
2227                 final EdgeResource seg = entry.getKey();
2228                 Change change = entry.getValue();
2229
2230                 switch (change) {
2231                     case ADDED: {
2232                         IElement mappedElement = getMappedElement(seg);
2233                         if (mappedElement == null) {
2234                             if (DebugPolicy.DEBUG_EDGE_LOAD)
2235                                 graph.asyncRequest(new ReadRequest() {
2236                                     @Override
2237                                     public void run(ReadGraph graph) throws DatabaseException {
2238                                         System.out.println("    EXTERNALLY ADDED CONNECTION SEGMENT: " + seg.toString()
2239                                                 + " - " + seg.toString(graph));
2240                                     }
2241                                 });
2242
2243                             graph.asyncRequest(new EdgeRequest(GraphToDiagramSynchronizer.this, canvas, errorHandler, canvasListenerSupport, diagram, adapter, seg), new AsyncProcedure<IElement>() {
2244                                 @Override
2245                                 public void execute(AsyncReadGraph graph, IElement e) {
2246                                     if (DebugPolicy.DEBUG_EDGE_LOAD)
2247                                         System.out.println("ADDED EDGE LOADED: " + e + " " + seg);
2248
2249                                     if (e != null) {
2250                                         synchronized (GraphToDiagramUpdater.this) {
2251                                             addedConnectionSegments.add(e);
2252                                             addedElementMap.put(seg, e);
2253                                             ConnectionEntityImpl ce = getConnectionEntity(seg);
2254                                             e.setHint(ElementHints.KEY_CONNECTION_ENTITY, ce);
2255                                             e.setHint(ElementHints.KEY_PARENT_ELEMENT, ce.getConnectionElement());
2256                                         }
2257                                     }
2258                                 }
2259
2260                                 @Override
2261                                 public void exception(AsyncReadGraph graph, Throwable throwable) {
2262                                     error(throwable);
2263                                 }
2264                             });
2265                         }
2266                         break;
2267                     }
2268                     case REMOVED: {
2269                         final IElement e = getMappedElement(seg);
2270                         if (DebugPolicy.DEBUG_EDGE_LOAD)
2271                             graph.asyncRequest(new ReadRequest() {
2272                                 @Override
2273                                 public void run(ReadGraph graph) throws DatabaseException {
2274                                     System.out.println("    EXTERNALLY REMOVED CONNECTION SEGMENT: " + seg.toString() + " - "
2275                                             + seg.toString(graph) + " -> " + e);
2276                                 }
2277                             });
2278                         if (e != null) {
2279                             removedConnectionSegments.add(e);
2280                         }
2281                         break;
2282                     }
2283                     default:
2284                 }
2285             }
2286         }
2287
2288         void executeDeferredLoaders(ReadGraph graph) throws DatabaseException {
2289             // The rest of the diagram loading passes
2290             Deque<IElement> q1 = new ArrayDeque<IElement>();
2291             Deque<IElement> q2 = new ArrayDeque<IElement>();
2292             collectElementLoaders(q1, addedElements);
2293             while (!q1.isEmpty()) {
2294                 //System.out.println("DEFFERED LOADERS: " + q1);
2295                 for (IElement e : q1) {
2296                     ElementLoader loader = e.removeHint(DiagramModelHints.KEY_ELEMENT_LOADER);
2297                     //System.out.println("EXECUTING DEFFERED LOADER: " + loader);
2298                     loader.load(graph, diagram, e);
2299                 }
2300
2301                 collectElementLoaders(q2, q1);
2302                 Deque<IElement> qt = q1;
2303                 q1 = q2;
2304                 q2 = qt;
2305                 q2.clear();
2306             }
2307         }
2308
2309         private void collectElementLoaders(Queue<IElement> queue, Collection<IElement> cs) {
2310             for (IElement e : cs) {
2311                 ElementLoader loader = e.getHint(DiagramModelHints.KEY_ELEMENT_LOADER);
2312                 if (loader != null)
2313                     queue.add(e);
2314             }
2315         }
2316
2317         public void process(ReadGraph graph) throws DatabaseException {
2318             // No changes? Do nothing.
2319             if (changes.isEmpty())
2320                 return;
2321
2322             ITask threadLog = ThreadLogger.task("processNodes");
2323             
2324             // NOTE: This order is important.
2325             Object task = Timing.BEGIN("processNodesConnections");
2326             //System.out.println("---- PROCESS NODES & CONNECTIONS BEGIN");
2327             if (!changes.elements.isEmpty()) {
2328                 graph.syncRequest(new AsyncReadRequest() {
2329                     @Override
2330                     public void run(AsyncReadGraph graph) throws DatabaseException {
2331                         processNodes(graph);
2332                     }
2333                     @Override
2334                     public String toString() {
2335                         return "processNodes";
2336                     }
2337                 });
2338             }
2339             //System.out.println("---- PROCESS NODES & CONNECTIONS END");
2340
2341             threadLog.finish();
2342             
2343             threadLog = ThreadLogger.task("processConnections");
2344
2345             processConnections();
2346
2347             threadLog.finish();
2348
2349             threadLog = ThreadLogger.task("processBranchPoints");
2350
2351             //System.out.println("---- PROCESS BRANCH POINTS BEGIN");
2352             if (!changes.branchPoints.isEmpty()) {
2353                 graph.syncRequest(new AsyncReadRequest() {
2354                     @Override
2355                     public void run(AsyncReadGraph graph) throws DatabaseException {
2356                         processBranchPoints(graph);
2357                     }
2358                     @Override
2359                     public String toString() {
2360                         return "processBranchPoints";
2361                     }
2362                 });
2363             }
2364             //System.out.println("---- PROCESS BRANCH POINTS END");
2365
2366             threadLog.finish();
2367
2368             Timing.END(task);
2369
2370             threadLog = ThreadLogger.task("processConnectionSegments");
2371
2372             task = Timing.BEGIN("processConnectionSegments");
2373
2374             //System.out.println("---- PROCESS CONNECTION SEGMENTS BEGIN");
2375             if (!changes.connectionSegments.isEmpty()) {
2376                 graph.syncRequest(new AsyncReadRequest() {
2377                     @Override
2378                     public void run(AsyncReadGraph graph) throws DatabaseException {
2379                         processConnectionSegments(graph);
2380                     }
2381                     @Override
2382                     public String toString() {
2383                         return "processConnectionSegments";
2384                     }
2385                 });
2386             }
2387             //System.out.println("---- PROCESS CONNECTION SEGMENTS END");
2388
2389             threadLog.finish();
2390
2391             Timing.END(task);
2392
2393             threadLog = ThreadLogger.task("processRouteGraphConnections");
2394
2395             task = Timing.BEGIN("processRouteGraphConnections");
2396             if (!changes.routeGraphConnections.isEmpty()) {
2397                 graph.syncRequest(new ReadRequest() {
2398                     @Override
2399                     public void run(ReadGraph graph) throws DatabaseException {
2400                         processRouteGraphConnections(graph);
2401                     }
2402                     @Override
2403                     public String toString() {
2404                         return "processRouteGraphConnections";
2405                     }
2406                 });
2407             }
2408             Timing.END(task);
2409
2410             threadLog.finish();
2411
2412             //System.out.println("---- AFTER LOADING");
2413             //for (IElement e : addedElements)
2414             //    System.out.println("    ADDED ELEMENT: " + e);
2415             //for (IElement e : addedBranchPoints)
2416             //    System.out.println("    ADDED BRANCH POINTS: " + e);
2417
2418             task = Timing.BEGIN("executeDeferredLoaders");
2419             threadLog = ThreadLogger.task("executeDeferredLoaders");
2420             
2421             executeDeferredLoaders(graph);
2422
2423             threadLog.finish();
2424
2425             Timing.END(task);
2426         }
2427
2428         public boolean isEmpty() {
2429             return addedElements.isEmpty() && removedElements.isEmpty()
2430             && addedConnectionSegments.isEmpty() && removedConnectionSegments.isEmpty()
2431             && addedBranchPoints.isEmpty() && removedBranchPoints.isEmpty()
2432             && addedConnectionEntities.isEmpty() && removedConnectionEntities.isEmpty()
2433             && addedRouteGraphConnectionMap.isEmpty() && removedRouteGraphConnections.isEmpty()
2434             && !changes.elementOrderChanged;
2435         }
2436
2437         class DefaultConnectionSegmentAdapter implements ConnectionSegmentAdapter {
2438
2439             @Override
2440             public void getClass(AsyncReadGraph graph, EdgeResource edge, ConnectionInfo info, ListenerSupport listenerSupport, ICanvasContext canvas, IDiagram diagram, final AsyncProcedure<ElementClass> procedure) {
2441                 if (info.connectionType != null) {
2442                     NodeClassRequest request = new NodeClassRequest(canvas, diagram, info.connectionType, true);
2443                     graph.asyncRequest(request, new CacheListener<ElementClass>(listenerSupport));
2444                     graph.asyncRequest(request, procedure);
2445                 } else {
2446                     procedure.execute(graph, null);
2447                 }
2448             }
2449
2450             @Override
2451             public void load(AsyncReadGraph graph, final EdgeResource edge, final ConnectionInfo info, ListenerSupport listenerSupport, ICanvasContext canvas, final IDiagram diagram, final IElement element) {
2452                 graph.asyncRequest(new Read<IElement>() {
2453                     @Override
2454                     public IElement perform(ReadGraph graph) throws DatabaseException {
2455                         //ITask task = ThreadLogger.getInstance().begin("LoadSegment");
2456                         syncLoad(graph, edge, info, diagram, element);
2457                         //task.finish();
2458                         return element;
2459                     }
2460                     @Override
2461                     public String toString() {
2462                         return "defaultConnectionSegmentAdapter";
2463                     }
2464                 }, new DisposableListener<IElement>(listenerSupport) {
2465                     
2466                     @Override
2467                     public String toString() {
2468                         return "DefaultConnectionSegmentAdapter listener for " + edge;
2469                     }
2470                     
2471                     @Override
2472                     public void execute(IElement loaded) {
2473                         // Invoked when the element has been loaded.
2474
2475                         if (loaded == null) {
2476                             disposeListener();
2477                             return;
2478                         }
2479
2480                         Object data = loaded.getHint(ElementHints.KEY_OBJECT);
2481
2482                         if (DebugPolicy.DEBUG_EDGE_LISTENER)
2483                             System.out.println("EDGE LoadListener for " + loaded + " " + data);
2484
2485                         if (addedElementMap.containsKey(data)) {
2486                             // This element was just loaded, in
2487                             // which case its hints need to
2488                             // uploaded to the real mapped
2489                             // element immediately.
2490                             IElement mappedElement = getMappedElement(data);
2491                             if (DebugPolicy.DEBUG_EDGE_LISTENER)
2492                                 System.out.println("LOADED ADDED EDGE, currently mapped element: " + mappedElement);
2493                             if (mappedElement != null && (mappedElement instanceof Element)) {
2494                                 if (DebugPolicy.DEBUG_EDGE_LISTENER) {
2495                                     System.out.println("  mapped hints: " + mappedElement.getHints());
2496                                     System.out.println("  loaded hints: " + loaded.getHints());
2497                                 }
2498                                 updateMappedElement((Element) mappedElement, loaded);
2499                             }
2500                         } else {
2501                             // This element was already loaded.
2502                             // Just schedule an update some time
2503                             // in the future.
2504                             if (DebugPolicy.DEBUG_EDGE_LISTENER)
2505                                 System.out.println("PREVIOUSLY LOADED EDGE UPDATED, scheduling update into the future");
2506                             offerGraphUpdate( edgeUpdater(element, loaded) );
2507                         }
2508                     }
2509                 });
2510             }
2511
2512             void syncLoad(ReadGraph graph, EdgeResource connectionSegment, ConnectionInfo info, IDiagram diagram, IElement element) throws DatabaseException {
2513                 // Check that at least some data exists before continuing further.
2514                 if (!graph.hasStatement(connectionSegment.first()) && !graph.hasStatement(connectionSegment.second())) {
2515                     return;
2516                 }
2517
2518                 // Validate that both ends of the segment are
2519                 // part of the same connection before loading.
2520                 // This will happen for connections that are
2521                 // modified through splitting and joining of
2522                 // connection segments.
2523                 Resource c = ConnectionUtil.tryGetConnection(graph, connectionSegment);
2524                 if (c == null) {
2525                     // Ok, this segment is somehow invalid. Just don't load it.
2526                     if (DebugPolicy.DEBUG_CONNECTION_LOAD)
2527                         System.out.println("Skipping edge " + connectionSegment + ". Both segment ends are not part of the same connection.");
2528                     return;
2529                 }
2530
2531                 if (!info.isValid()) {
2532                     // This edge must be somehow invalid, don't proceed with loading.
2533                     if (DebugPolicy.DEBUG_CONNECTION_LOAD)
2534                         warning("Cannot load edge " + connectionSegment + ". ConnectionInfo " + info + " is invalid.", new DebugException("execution trace"));
2535                     return;
2536                 }
2537
2538                 Element edge = (Element) element;
2539                 edge.setHint(ElementHints.KEY_OBJECT, connectionSegment);
2540
2541                 // connectionSegment resources may currently be in a different
2542                 // order than ConnectionInfo.firstEnd/secondEnd. Therefore the
2543                 // segment ends must be resolved here.
2544                 ConnectionSegmentEnd firstEnd = DiagramGraphUtil.resolveConnectionSegmentEnd(graph, connectionSegment.first());
2545                 ConnectionSegmentEnd secondEnd = DiagramGraphUtil.resolveConnectionSegmentEnd(graph, connectionSegment.second());
2546                 if (firstEnd == null || secondEnd == null) {
2547                     if (DebugPolicy.DEBUG_CONNECTION_LOAD)
2548                         warning("End attachments for edge " + connectionSegment + " are unresolved: (" + firstEnd + "," + secondEnd + ")", new DebugException("execution trace"));
2549                     return;
2550                 }
2551
2552                 if (DebugPolicy.DEBUG_CONNECTION_LOAD)
2553                     System.out.println("CONNECTION INFO: " + connectionSegment + " - " + info);
2554                 DesignatedTerminal firstTerminal = DiagramGraphUtil.findDesignatedTerminal(
2555                         graph, diagram, connectionSegment.first(), firstEnd);
2556                 DesignatedTerminal secondTerminal = DiagramGraphUtil.findDesignatedTerminal(
2557                         graph, diagram, connectionSegment.second(), secondEnd);
2558
2559                 // Edges must be connected at both ends in order for edge loading to succeed.
2560                 String err = validateConnectivity(graph, connectionSegment, firstTerminal, secondTerminal);
2561                 if (err != null) {
2562                     // Stop loading edge if the connectivity cannot be completely resolved.
2563                     if (DebugPolicy.DEBUG_CONNECTION_LOAD)
2564                         warning(err, null);
2565                     return;
2566                 }
2567
2568                 graph.syncRequest(new AsyncReadRequest() {
2569                     @Override
2570                     public void run(AsyncReadGraph graph) {
2571                         // NOTICE: Layer information is loaded from the connection entity resource
2572                         // that is shared by all segments of the same connection.
2573                         ElementFactoryUtil.loadLayersForElement(graph, layerManager, diagram, edge, info.connection,
2574                                 new AsyncProcedureAdapter<IElement>() {
2575                             @Override
2576                             public void exception(AsyncReadGraph graph, Throwable t) {
2577                                 error("failed to load layers for connection segment", t);
2578                             }
2579                         });
2580                     }
2581                 });
2582
2583                 edge.setHintWithoutNotification(KEY_CONNECTION_BEGIN_PLACEHOLDER, new PlaceholderConnection(
2584                         EdgeEnd.Begin,
2585                         firstTerminal.element.getHint(ElementHints.KEY_OBJECT),
2586                         firstTerminal.terminal));
2587                 edge.setHintWithoutNotification(KEY_CONNECTION_END_PLACEHOLDER, new PlaceholderConnection(
2588                         EdgeEnd.End,
2589                         secondTerminal.element.getHint(ElementHints.KEY_OBJECT),
2590                         secondTerminal.terminal));
2591
2592                 IModelingRules modelingRules = diagram.getHint(DiagramModelHints.KEY_MODELING_RULES);
2593                 if (modelingRules != null) {
2594                     ConnectionVisualsLoader loader = diagram.getHint(DiagramModelHints.KEY_CONNECTION_VISUALS_LOADER);
2595                     if (loader != null)
2596                         loader.loadConnectionVisuals(graph, modelingRules, info.connection, diagram, edge, firstTerminal, secondTerminal);
2597                     else
2598                         DiagramGraphUtil.loadConnectionVisuals(graph, modelingRules, info.connection, diagram, edge, firstTerminal, secondTerminal);
2599                 }
2600             }
2601
2602             private String validateConnectivity(ReadGraph graph, EdgeResource edge,
2603                     DesignatedTerminal firstTerminal,
2604                     DesignatedTerminal secondTerminal)
2605             throws DatabaseException {
2606                 boolean firstLoose = firstTerminal == null;
2607                 boolean secondLoose = secondTerminal == null;
2608                 boolean stray = firstLoose && secondLoose;
2609                 if (firstTerminal == null || secondTerminal == null) {
2610                     StringBuilder sb = new StringBuilder();
2611                     sb.append("encountered ");
2612                     sb.append(stray ? "stray" : "loose");
2613                     sb.append(" connection segment, ");
2614                     if (firstLoose)
2615                         sb.append("first ");
2616                     if (stray)
2617                         sb.append("and ");
2618                     if (secondLoose)
2619                         sb.append("second ");
2620                     sb.append("end disconnected: ");
2621                     sb.append(edge.toString(graph));
2622                     sb.append(" - ");
2623                     sb.append(edge.toString());
2624                     return sb.toString();
2625                 }
2626                 return null;
2627             }
2628
2629         }
2630
2631         ConnectionSegmentAdapter connectionSegmentAdapter = new DefaultConnectionSegmentAdapter();
2632
2633     }
2634
2635     private static final Double DIAGRAM_UPDATE_DIAGRAM_PRIORITY = 1d;
2636     private static final Double DIAGRAM_UPDATE_NODE_PRIORITY = 2d;
2637     private static final Double DIAGRAM_UPDATE_CONNECTION_PRIORITY = 3d;
2638     private static final Double DIAGRAM_UPDATE_EDGE_PRIORITY = 4d;
2639
2640     interface DiagramUpdater extends Runnable {
2641         Double getPriority();
2642
2643         Comparator<DiagramUpdater> DIAGRAM_UPDATER_COMPARATOR = new Comparator<DiagramUpdater>() {
2644             @Override
2645             public int compare(DiagramUpdater o1, DiagramUpdater o2) {
2646                 return o1.getPriority().compareTo(o2.getPriority());
2647             }
2648         };
2649     }
2650
2651     interface GraphUpdateReactor {
2652         DiagramUpdater graphUpdate(ReadGraph graph) throws DatabaseException;
2653     }
2654
2655     static abstract class AbstractDiagramUpdater implements DiagramUpdater, GraphUpdateReactor {
2656         protected final Double priority;
2657         protected final String runnerName;
2658
2659         public AbstractDiagramUpdater(Double priority, String runnerName) {
2660             if (priority == null)
2661                 throw new NullPointerException("null priority");
2662             if (runnerName == null)
2663                 throw new NullPointerException("null runner name");
2664             this.priority = priority;
2665             this.runnerName = runnerName;
2666         }
2667
2668         @Override
2669         public Double getPriority() {
2670             return priority;
2671         }
2672
2673         @Override
2674         public AbstractDiagramUpdater graphUpdate(ReadGraph graph) {
2675             return this;
2676         }
2677
2678         @Override
2679         public void run() {
2680             Object task = Timing.BEGIN(runnerName);
2681             forDiagram();
2682             Timing.END(task);
2683         }
2684
2685         protected void forDiagram() {
2686         }
2687
2688         @Override
2689         public String toString() {
2690             return runnerName + "@" + System.identityHashCode(this) + " [" + priority + "]";
2691         }
2692     }
2693
2694     /**
2695      * @param content the new contents of the diagram, must not be
2696      *        <code>null</code>.
2697      */
2698     private GraphUpdateReactor diagramGraphUpdater(final DiagramContents content) {
2699         if (content == null)
2700             throw new NullPointerException("null diagram content");
2701
2702         return new GraphUpdateReactor() {
2703             @Override
2704             public String toString() {
2705                 return "DiagramGraphUpdater@" + System.identityHashCode(this);
2706             }
2707
2708             @Override
2709             public DiagramUpdater graphUpdate(ReadGraph graph) throws DatabaseException {
2710                 // Never do anything here if the canvas has already been disposed.
2711                 if (!GraphToDiagramSynchronizer.this.isAlive())
2712                     return null;
2713
2714                 // We must be prepared for the following changes in the diagram graph
2715                 // model:
2716                 // - the diagram has been completely removed
2717                 // - elements have been added
2718                 // - elements have been removed
2719                 //
2720                 // Element-specific changes are handled by the element query listeners:
2721                 // - elements have been modified
2722                 // - element position has changed
2723                 // - element class (e.g. image) has changed
2724
2725                 diagramUpdateLock.lock();
2726                 try {
2727                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE)
2728                         System.out.println("In diagramGraphUpdater:");
2729
2730                     // Find out what has changed since the last query.
2731                     Object task = Timing.BEGIN("diagramContentDifference");
2732                     DiagramContents lastContent = previousContent;
2733                     DiagramContentChanges changes = content.differenceFrom(previousContent);
2734                     previousContent = content;
2735                     Timing.END(task);
2736                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE)
2737                         System.out.println("  changes: " + changes);
2738
2739                     // Bail out if there are no changes to react to.
2740                     if (changes.isEmpty())
2741                         return null;
2742
2743                     // Load everything that needs to be loaded from the graph,
2744                     // but don't update the UI model in this thread yet.
2745                     task = Timing.BEGIN("updater.process");
2746                     GraphToDiagramUpdater updater = new GraphToDiagramUpdater(lastContent, content, changes);
2747                     GraphToDiagramSynchronizer.this.currentUpdater = updater;
2748                     try {
2749                         updater.process(graph);
2750                     } finally {
2751                         GraphToDiagramSynchronizer.this.currentUpdater = null;
2752                     }
2753                     Timing.END(task);
2754
2755                     if (updater.isEmpty())
2756                         return null;
2757
2758                     // Return an updater that will update the UI run-time model.
2759                     return diagramUpdater(updater);
2760                 } finally {
2761                     diagramUpdateLock.unlock();
2762                 }
2763             }
2764         };
2765     }
2766
2767     DiagramUpdater diagramUpdater(final GraphToDiagramUpdater updater) {
2768         return new AbstractDiagramUpdater(DIAGRAM_UPDATE_DIAGRAM_PRIORITY, "updateDiagram") {
2769             @Override
2770             protected void forDiagram() {
2771                 if (DebugPolicy.DEBUG_DIAGRAM_UPDATE)
2772                     System.out.println("running diagram updater: " + this);
2773
2774                 // DiagramUtils.testDiagram(diagram);
2775                 Set<IElement> dirty = new HashSet<IElement>();
2776
2777                 Object task2 = Timing.BEGIN("Preprocess connection changes");
2778                 Map<ConnectionEntity, ConnectionChildren> connectionChangeData = new HashMap<ConnectionEntity, ConnectionChildren>(updater.changedConnectionEntities.size());
2779                 for (ConnectionData cd : updater.changedConnectionEntities.values()) {
2780                     connectionChangeData.put(cd.impl, cd.impl.getConnectionChildren());
2781                 }
2782                 Timing.END(task2);
2783
2784                 task2 = Timing.BEGIN("removeRouteGraphConnections");
2785                 for (IElement removedRouteGraphConnection : updater.removedRouteGraphConnections) {
2786                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2787                         System.out.println("removing route graph connection: " + removedRouteGraphConnection);
2788
2789                     ConnectionEntity ce = removedRouteGraphConnection.getHint(ElementHints.KEY_CONNECTION_ENTITY);
2790                     if (ce == null)
2791                         continue;
2792                     Object connectionData = ElementUtils.getObject(removedRouteGraphConnection);
2793                     tempConnections.clear();
2794                     for (Connection conn : ce.getTerminalConnections(tempConnections)) {
2795                         ((Element) conn.node).removeHintWithoutNotification(new TerminalKeyOf(conn.terminal, connectionData, Connection.class));
2796                         // To be sure the view will be up-to-date, mark the node
2797                         // connected to the removed connection dirty.
2798                         dirty.add(conn.node);
2799                     }
2800                 }
2801                 Timing.END(task2);
2802
2803                 task2 = Timing.BEGIN("removeBranchPoints");
2804                 for (IElement removed : updater.removedBranchPoints) {
2805                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2806                         System.out.println("removing branch point: " + removed);
2807
2808                     unmapElement(removed.getHint(ElementHints.KEY_OBJECT));
2809                     removeNodeTopologyHints((Element) removed);
2810
2811                     IElement connection = ElementUtils.getParent(removed);
2812                     if (connection != null) {
2813                         dirty.add(connection);
2814                     }
2815                 }
2816                 Timing.END(task2);
2817
2818                 task2 = Timing.BEGIN("removeConnectionSegments");
2819                 for (IElement removed : updater.removedConnectionSegments) {
2820                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2821                         System.out.println("removing segment: " + removed);
2822
2823                     unmapElement(removed.getHint(ElementHints.KEY_OBJECT));
2824                     removeEdgeTopologyHints((Element) removed);
2825
2826                     IElement connection = ElementUtils.getParent(removed);
2827                     if (connection != null) {
2828                         dirty.add(connection);
2829                     }
2830                 }
2831                 Timing.END(task2);
2832
2833                 task2 = Timing.BEGIN("removeElements");
2834                 for (IElement removed : updater.removedElements) {
2835                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2836                         System.out.println("removing element: " + removed);
2837
2838                     removed.setHint(KEY_REMOVE_RELATIONSHIPS, Boolean.TRUE);
2839                     if (diagram.containsElement(removed)) {
2840                         diagram.removeElement(removed);
2841                     }
2842                     unmapElement(removed.getHint(ElementHints.KEY_OBJECT));
2843                     removeNodeTopologyHints((Element) removed);
2844
2845                     // No use marking removed elements dirty.
2846                     dirty.remove(removed);
2847                 }
2848                 Timing.END(task2);
2849
2850                 // TODO: get rid of this
2851                 task2 = Timing.BEGIN("removeConnectionEntities");
2852                 for (Resource ce : updater.removedConnectionEntities) {
2853                     unmapConnection(ce);
2854                 }
2855                 Timing.END(task2);
2856
2857                 task2 = Timing.BEGIN("setConnectionData");
2858                 for (ConnectionData cd : updater.changedConnectionEntities.values()) {
2859                     cd.impl.setData(cd.segments, cd.branchPoints);
2860                 }
2861                 Timing.END(task2);
2862
2863                 // TODO: get rid of this
2864                 task2 = Timing.BEGIN("addConnectionEntities");
2865                 for (Map.Entry<Resource, ConnectionEntityImpl> entry : updater.addedConnectionEntities
2866                         .entrySet()) {
2867                     mapConnection(entry.getKey(), entry.getValue());
2868                 }
2869                 Timing.END(task2);
2870
2871                 task2 = Timing.BEGIN("addBranchPoints");
2872                 for (IElement added : updater.addedBranchPoints) {
2873                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2874                         System.out.println("adding branch point: " + added);
2875
2876                     mapElement(ElementUtils.getObject(added), added);
2877
2878                     IElement connection = ElementUtils.getParent(added);
2879                     if (connection != null) {
2880                         dirty.add(connection);
2881                     }
2882                 }
2883                 Timing.END(task2);
2884
2885                 // Add new elements at end of diagram, element order will be synchronized later.
2886                 task2 = Timing.BEGIN("addElements");
2887                 for(Resource r : updater.content.elements) {
2888                     IElement added = updater.addedElementMap.get(r);
2889                     if(added != null) {
2890                         if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2891                             System.out.println("adding element: " + added);
2892
2893                         //Object task3 = BEGIN("mapElement " + added);
2894                         Object task3 = Timing.BEGIN("mapElement");
2895                         mapElement(added.getHint(ElementHints.KEY_OBJECT), added);
2896                         Timing.END(task3);
2897
2898                         //task3 = BEGIN("addElement " + added);
2899                         task3 = Timing.BEGIN("addElement");
2900                         //System.out.println("diagram.addElement: " + added + " - " + diagram);
2901                         diagram.addElement(added);
2902                         dirty.add(added);
2903                         Timing.END(task3);
2904                     }
2905                 }
2906                 Timing.END(task2);
2907
2908                 // We've ensured that all nodes must have been and
2909                 // mapped before reaching this.
2910                 task2 = Timing.BEGIN("addConnectionSegments");
2911                 for (IElement added : updater.addedConnectionSegments) {
2912                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2913                         System.out.println("adding segment: " + added);
2914
2915                     PlaceholderConnection cb = added.removeHint(GraphToDiagramSynchronizer.KEY_CONNECTION_BEGIN_PLACEHOLDER);
2916                     PlaceholderConnection ce = added.removeHint(GraphToDiagramSynchronizer.KEY_CONNECTION_END_PLACEHOLDER);
2917                     if (cb == null || ce == null) {
2918                         if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2919                             warning("ignoring connection segment " + added + ", connectivity was not resolved (begin=" + cb + ", end=" + ce +")", null);
2920                         continue;
2921                     }
2922
2923                     mapElement(ElementUtils.getObject(added), added);
2924
2925                     IElement beginNode = assertMappedElement(cb.node);
2926                     IElement endNode = assertMappedElement(ce.node);
2927
2928                     if (cb != null)
2929                         connect(added, cb.end, beginNode, cb.terminal);
2930                     if (ce != null)
2931                         connect(added, ce.end, endNode, ce.terminal);
2932
2933                     IElement connection = ElementUtils.getParent(added);
2934                     if (connection != null) {
2935                         dirty.add(connection);
2936                     }
2937                 }
2938                 Timing.END(task2);
2939
2940                 // We've ensured that all nodes must have been and
2941                 // mapped before reaching this.
2942
2943                 task2 = Timing.BEGIN("handle dirty RouteGraph connections");
2944                 for (IElement addedRouteGraphConnection : updater.addedRouteGraphConnectionMap.values()) {
2945                     updateDirtyRouteGraphConnection(addedRouteGraphConnection, dirty);
2946                 }
2947                 Timing.END(task2);
2948
2949                 // Prevent memory leaks
2950                 tempConnections.clear();
2951
2952                 // Make sure that the diagram element order matches that of the database.
2953                 final TObjectIntHashMap<IElement> orderMap = new TObjectIntHashMap<IElement>(2 * updater.content.elements.size());
2954                 int i = 1;
2955                 for (Resource r : updater.content.elements) {
2956                     IElement e = getMappedElement(r);
2957                     if (e != null)
2958                         orderMap.put(e, i);
2959                     ++i;
2960                 }
2961                 diagram.sort(new Comparator<IElement>() {
2962                     @Override
2963                     public int compare(IElement e1, IElement e2) {
2964                         int o1 = orderMap.get(e1);
2965                         int o2 = orderMap.get(e2);
2966                         return o1 - o2;
2967                     }
2968                 });
2969
2970                 // TODO: consider removing this. The whole thing should work without it and
2971                 // this "fix" will only be hiding the real problems.
2972                 task2 = Timing.BEGIN("fixChangedConnections");
2973                 for (ConnectionData cd : updater.changedConnectionEntities.values()) {
2974                     cd.impl.fix();
2975                 }
2976                 Timing.END(task2);
2977
2978                 task2 = Timing.BEGIN("validateAndFix");
2979                 DiagramUtils.validateAndFix(diagram, dirty);
2980                 Timing.END(task2);
2981
2982                 // This will fire connection entity change listeners
2983                 task2 = Timing.BEGIN("Postprocess connection changes");
2984                 for (ConnectionData cd : updater.changedConnectionEntities.values()) {
2985                     ConnectionChildren oldChildren = connectionChangeData.get(cd.impl);
2986                     if (oldChildren != null) {
2987                         ConnectionChildren currentChildren = cd.impl.getConnectionChildren();
2988                         cd.impl.fireListener(oldChildren, currentChildren);
2989                     }
2990                 }
2991                 Timing.END(task2);
2992
2993                 task2 = Timing.BEGIN("setDirty");
2994                 for (IElement e : dirty) {
2995                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2996                         System.out.println("MARKING ELEMENT DIRTY: " + e);
2997                     e.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);
2998                 }
2999                 Timing.END(task2);
3000
3001                 // Signal about possible changes in the z-order of diagram elements.
3002                 if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
3003                     System.out.println("MARKING DIAGRAM DIRTY: " + diagram);
3004                 diagram.setHint(Hints.KEY_DIRTY, Hints.VALUE_Z_ORDER_CHANGED);
3005
3006                 // Mark the updater as "processed".
3007                 updater.clear();
3008
3009                 // Inform listeners that the diagram has been updated.
3010                 diagram.setHint(DiagramModelHints.KEY_DIAGRAM_CONTENTS_UPDATED, Boolean.TRUE);
3011             }
3012         };
3013     }
3014
3015     /**
3016      * @param connection
3017      * @param dirtySet
3018      */
3019     private void updateDirtyRouteGraphConnection(IElement connection, Set<IElement> dirtySet) {
3020         if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
3021             System.out.println("updating dirty route graph connection: " + connection);
3022
3023         ConnectionEntity ce = connection.getHint(ElementHints.KEY_CONNECTION_ENTITY);
3024         if (ce == null)
3025             return;
3026
3027         tempConnections.clear();
3028         Object connectionData = ElementUtils.getObject(connection);
3029         for (Connection conn : ce.getTerminalConnections(tempConnections)) {
3030             ((Element) conn.node).setHintWithoutNotification(
3031                     new TerminalKeyOf(conn.terminal, connectionData, Connection.class),
3032                     conn);
3033             if (dirtySet != null)
3034                 dirtySet.add(conn.node);
3035         }
3036
3037         // Prevent memory leaks.
3038         tempConnections.clear();
3039     }
3040
3041     abstract class ElementUpdater extends AbstractDiagramUpdater {
3042         private final IElement newElement;
3043
3044         public ElementUpdater(Double priority, String runnerName, IElement newElement) {
3045             super(priority, runnerName);
3046             if (newElement == null)
3047                 throw new NullPointerException("null element");
3048             this.newElement = newElement;
3049         }
3050
3051         @Override
3052         public String toString() {
3053             return super.toString() + "[" + newElement + "]";
3054         }
3055
3056         @Override
3057         public void run() {
3058 //            System.out.println("ElementUpdateRunner new=" + newElement);
3059             Object elementResource = newElement.getHint(ElementHints.KEY_OBJECT);
3060 //            System.out.println("ElementUpdateRunner res=" + elementResource);
3061             final Element mappedElement = (Element) getMappedElement(elementResource);
3062 //            System.out.println("ElementUpdateRunner mapped=" + mappedElement);
3063             if (mappedElement == null) {
3064                 if (DebugPolicy.DEBUG_ELEMENT_LIFECYCLE) {
3065                     System.out.println("SKIP DIAGRAM UPDATE " + this  + " for element resource " + elementResource + ", no mapped element (newElement=" + newElement + ")");
3066                 }
3067                 // Indicates the element has been removed from the graph.
3068                 return;
3069             }
3070
3071             Object task = Timing.BEGIN(runnerName);
3072             forMappedElement(mappedElement);
3073             Timing.END(task);
3074         }
3075
3076         protected abstract void forMappedElement(Element mappedElement);
3077     }
3078
3079     ElementUpdater nodeUpdater(final Resource resource, final IElement newElement) {
3080
3081         return new ElementUpdater(DIAGRAM_UPDATE_NODE_PRIORITY, "updateNode", newElement) {
3082
3083             Collection<Terminal> getTerminals(IElement e) {
3084                 Collection<Terminal> ts = Collections.emptyList();
3085                 TerminalTopology tt = e.getElementClass().getAtMostOneItemOfClass(TerminalTopology.class);
3086                 if (tt != null) {
3087                     ts = new ArrayList<Terminal>();
3088                     tt.getTerminals(newElement, ts);
3089                 }
3090                 return ts;
3091             }
3092
3093             @Override
3094             protected void forMappedElement(final Element mappedElement) {
3095                 if (DebugPolicy.DEBUG_NODE_UPDATE)
3096                     System.out.println("running node updater: " + this + " - new element: " + newElement);
3097
3098                 // Newly loaded node elements NEVER contain topology-related
3099                 // hints, i.e. TerminalKeyOf hints. Instead all connections are
3100                 // actually set into element hints when connection edges are
3101                 // loaded.
3102
3103                 Collection<Terminal> oldTerminals = getTerminals(mappedElement);
3104                 Collection<Terminal> newTerminals = getTerminals(newElement);
3105                 if (!oldTerminals.equals(newTerminals)) {
3106                     // Okay, there are differences in the terminals. Need to fix
3107                     // the TerminalKeyOf hint values to use the new terminal
3108                     // instances when correspondences exist.
3109
3110                     // If there is no correspondence for an old terminal, we
3111                     // are simply forced to remove the hints related to this
3112                     // connection.
3113
3114                     Map<Terminal, Terminal> newTerminalMap = new HashMap<Terminal, Terminal>(newTerminals.size());
3115                     for (Terminal t : newTerminals) {
3116                         newTerminalMap.put(t, t);
3117                     }
3118
3119                     for (Map.Entry<TerminalKeyOf, Object> entry : mappedElement.getHintsOfClass(TerminalKeyOf.class).entrySet()) {
3120                         TerminalKeyOf key = entry.getKey();
3121                         Connection c = (Connection) entry.getValue();
3122                         if (c.node == mappedElement) {
3123                             Terminal newTerminal = newTerminalMap.get(c.terminal);
3124                             if (newTerminal != null) {
3125                                 c = new Connection(c.edge, c.end, c.node, newTerminal);
3126                                 ((Element) c.edge).setHintWithoutNotification(EndKeyOf.get(c.end), c);
3127                             } else {
3128                                 mappedElement.removeHintWithoutNotification(key);
3129                             }
3130                         }
3131                     }
3132                 }
3133
3134                 updateMappedElement(mappedElement, newElement);
3135                 mappedElement.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);
3136             }
3137         };
3138     }
3139
3140     ElementUpdater connectionUpdater(final Object data, final IElement newElement) {
3141         return new ElementUpdater(DIAGRAM_UPDATE_CONNECTION_PRIORITY, "updateConnection", newElement) {
3142             @Override
3143             public void forMappedElement(Element mappedElement) {
3144                 if (DebugPolicy.DEBUG_CONNECTION_UPDATE)
3145                     System.out.println("running connection updater: " + this + " - new element: " + newElement
3146                             + " with data " + data);
3147
3148                 // This is kept up-to-date by GDS, make sure not to overwrite it
3149                 // from the mapped element.
3150                 newElement.removeHint(ElementHints.KEY_CONNECTION_ENTITY);
3151
3152                 updateMappedElement(mappedElement, newElement);
3153                 mappedElement.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);
3154             }
3155         };
3156     }
3157
3158     ElementUpdater edgeUpdater(final Object data, final IElement newElement) {
3159         return new ElementUpdater(DIAGRAM_UPDATE_EDGE_PRIORITY, "updateEdge", newElement) {
3160             @Override
3161             public void forMappedElement(Element mappedElement) {
3162                 if (DebugPolicy.DEBUG_EDGE_UPDATE)
3163                     System.out.println("running edge updater: " + this + " - new element: " + newElement
3164                             + " with data " + data);
3165
3166                 updateMappedElement(mappedElement, newElement);
3167                 mappedElement.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);
3168             }
3169         };
3170     }
3171
3172     ElementUpdater routeGraphConnectionUpdater(final Object data, final IElement newElement, final Set<Object> dirtyNodes) {
3173         return new ElementUpdater(DIAGRAM_UPDATE_CONNECTION_PRIORITY, "updateRouteGraphConnection", newElement) {
3174             @Override
3175             public void forMappedElement(Element mappedElement) {
3176                 if (DebugPolicy.DEBUG_CONNECTION_UPDATE)
3177                     System.out.println("running route graph connection updater: " + this + " - new element: " + newElement
3178                             + " with data " + data);
3179
3180                 // Remove all TerminalKeyOf hints from nodes that were
3181                 // previously connected to the connection (mappedElement)
3182                 // before updating mappedElement with new topology information.
3183
3184                 for (Object dirtyNodeObject : dirtyNodes) {
3185                     Element dirtyNode = (Element) getMappedElement(dirtyNodeObject);
3186                     if (dirtyNode == null)
3187                         continue;
3188                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
3189                         System.out.println("preparing node with dirty connectivity: " + dirtyNode);
3190
3191                     for (Map.Entry<TerminalKeyOf, Object> entry : dirtyNode.getHintsOfClass(TerminalKeyOf.class).entrySet()) {
3192                         Connection conn = (Connection) entry.getValue();
3193                         Object connectionNode = conn.edge.getHint(ElementHints.KEY_OBJECT);
3194                         if (data.equals(connectionNode)) {
3195                             dirtyNode.removeHintWithoutNotification(entry.getKey());
3196                         }
3197                     }
3198                 }
3199
3200                 // Update connection information, including topology
3201                 updateMappedElement(mappedElement, newElement);
3202
3203                 // Reinstall TerminalKeyOf hints into nodes that are now connected
3204                 // to mappedElement to keep diagram run-time model properly in sync
3205                 // with the database.
3206                 updateDirtyRouteGraphConnection(mappedElement, null);
3207
3208                 // TODO: should mark dirty nodes' scene graph dirty ?
3209
3210                 mappedElement.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);
3211             }
3212         };
3213     }
3214
3215     /**
3216      * Copies hints from <code>newElement</code> to <code>mappedElement</code>
3217      * asserting some validity conditions at the same time.
3218      * 
3219      * @param mappedElement
3220      * @param newElement
3221      */
3222     static void updateMappedElement(Element mappedElement, IElement newElement) {
3223         if (mappedElement == newElement)
3224             // Can't update anything if the two elements are the same.
3225             return;
3226
3227         ElementClass oldClass = mappedElement.getElementClass();
3228         ElementClass newClass = newElement.getElementClass();
3229
3230         Object mappedData = mappedElement.getHint(ElementHints.KEY_OBJECT);
3231         Object newData = newElement.getHint(ElementHints.KEY_OBJECT);
3232
3233         assert mappedData != null;
3234         assert newData != null;
3235         assert mappedData.equals(newData);
3236
3237         if (DebugPolicy.DEBUG_GENERAL_ELEMENT_UPDATE) {
3238             System.out.println("Updating mapped element, setting hints\n  from: " + newElement + "\n  into: " + mappedElement);
3239         }
3240
3241         // TODO: consider if this equals check is a waste of time or does it pay
3242         // off due to having to reinitialize per-class caches for the new
3243         // ElementClass that are constructed on the fly?
3244         if (!newClass.equals(oldClass)) {
3245             if (DebugPolicy.DEBUG_GENERAL_ELEMENT_UPDATE) {
3246                 System.out.println("  old element class: " + oldClass);
3247                 System.out.println("  new element class: " + newClass);
3248             }
3249             mappedElement.setElementClass(newClass);
3250         }
3251
3252         // Tuukka@2010-02-19: replaced with notifications for making
3253         // the graph synchronizer more transparent to the client.
3254
3255         // Hint notifications will not work when this is used.
3256         //mappedElement.setHintsWithoutNotification(newElement.getHints());
3257
3258         Map<DiscardableKey, Object> discardableHints = mappedElement.getHintsOfClass(DiscardableKey.class);
3259
3260         // Set all hints from newElement to mappedElement.
3261         // Leave any hints in mappedElement but not in newElement as is.
3262         Map<Key, Object> hints = newElement.getHints();
3263         Map<Key, Object> newHints = new HashMap<Key, Object>();
3264         for (Map.Entry<Key, Object> entry : hints.entrySet()) {
3265             Key key = entry.getKey();
3266             Object newValue = entry.getValue();
3267             Object oldValue = mappedElement.getHint(key);
3268             if (DebugPolicy.DEBUG_GENERAL_ELEMENT_UPDATE_DETAIL) {
3269                 System.out.println("  hint " + key + " compare values: " + oldValue + "  ->  " + newValue);
3270             }
3271             if (!newValue.equals(oldValue)) {
3272                 newHints.put(key, newValue);
3273                 if (DebugPolicy.DEBUG_GENERAL_ELEMENT_UPDATE) {
3274                     System.out.format("    %-42s : %64s -> %-64s\n", key, oldValue, newValue);
3275                 }
3276             } else {
3277                 // If the hint value has not changed but the hint still exists
3278                 // we don't need to discard it even if it is considered
3279                 // discardable.
3280                 discardableHints.remove(key);
3281             }
3282         }
3283
3284         // Set all hints at once and send notifications after setting the values.
3285         if (!discardableHints.isEmpty()) {
3286             if (DebugPolicy.DEBUG_GENERAL_ELEMENT_UPDATE)
3287                 System.out.println("Discarding " + discardableHints.size() + " discardable hints:\n  " + discardableHints);
3288             mappedElement.removeHints(discardableHints.keySet());
3289         }
3290         if (!newHints.isEmpty()) {
3291             if (DebugPolicy.DEBUG_GENERAL_ELEMENT_UPDATE) {
3292                 System.out.println("Updating mapped element, setting new hints:\n\t"
3293                         + EString.implode(newHints.entrySet(), "\n\t") + "\nto replace old hints\n\t"
3294                         + EString.implode(mappedElement.getHints().entrySet(), "\n\t"));
3295             }
3296             mappedElement.setHints(newHints);
3297         }
3298         if (DebugPolicy.DEBUG_GENERAL_ELEMENT_UPDATE) {
3299             System.out.println("All hints after update:\n\t"
3300                     + EString.implode(mappedElement.getHints().entrySet(), "\n\t"));
3301         }
3302     }
3303
3304     class TransactionListener extends SessionEventListenerAdapter {
3305         long startTime;
3306         @Override
3307         public void writeTransactionStarted() {
3308             startTime = System.nanoTime();
3309             if (DebugPolicy.DEBUG_WRITE_TRANSACTIONS)
3310                 System.out.println(GraphToDiagramSynchronizer.class.getSimpleName() + ".sessionEventListener.writeTransactionStarted");
3311             inWriteTransaction.set(true);
3312         }
3313         @Override
3314         public void writeTransactionFinished() {
3315             long endTime = System.nanoTime();
3316             if (DebugPolicy.DEBUG_WRITE_TRANSACTIONS)
3317                 System.out.println(GraphToDiagramSynchronizer.class.getSimpleName() + ".sessionEventListener.writeTransactionFinished: " + (endTime - startTime)*1e-6 + " ms");
3318             inWriteTransaction.set(false);
3319             scheduleGraphUpdates();
3320         }
3321     };
3322
3323     Object                   graphUpdateLock             = new Object();
3324     TransactionListener      sessionListener             = null;
3325     AtomicBoolean            inWriteTransaction          = new AtomicBoolean(false);
3326     AtomicBoolean            graphUpdateRequestScheduled = new AtomicBoolean(false);
3327     List<GraphUpdateReactor> queuedGraphUpdates          = new ArrayList<GraphUpdateReactor>();
3328
3329     private void offerGraphUpdate(GraphUpdateReactor update) {
3330         if (DebugPolicy.DEBUG_GRAPH_UPDATE)
3331             System.out.println("offerGraphUpdate: " + update);
3332         boolean inWrite = inWriteTransaction.get();
3333         synchronized (graphUpdateLock) {
3334             if (DebugPolicy.DEBUG_GRAPH_UPDATE)
3335                 System.out.println("queueing graph update: " + update);
3336             queuedGraphUpdates.add(update);
3337         }
3338         if (!inWrite) {
3339             if (DebugPolicy.DEBUG_GRAPH_UPDATE)
3340                 System.out.println("scheduling queued graph update immediately: " + update);
3341             scheduleGraphUpdates();
3342         }
3343     }
3344
3345     private Collection<GraphUpdateReactor> scrubGraphUpdates() {
3346         synchronized (graphUpdateLock) {
3347             if (queuedGraphUpdates.isEmpty())
3348                 return Collections.emptyList();
3349             final List<GraphUpdateReactor> updates = queuedGraphUpdates;
3350             queuedGraphUpdates = new ArrayList<GraphUpdateReactor>();
3351             return updates;
3352         }
3353     }
3354
3355     private void scheduleGraphUpdates() {
3356         synchronized (graphUpdateLock) {
3357             if (queuedGraphUpdates.isEmpty())
3358                 return;
3359             if (!graphUpdateRequestScheduled.compareAndSet(false, true))
3360                 return;
3361         }
3362
3363         if (DebugPolicy.DEBUG_GRAPH_UPDATE)
3364             System.out.println("scheduling " + queuedGraphUpdates.size() + " queued graph updates with ");
3365
3366         session.asyncRequest(new ReadRequest() {
3367             @Override
3368             public void run(final ReadGraph graph) throws DatabaseException {
3369                 Collection<GraphUpdateReactor> updates;
3370                 synchronized (graphUpdateLock) {
3371                     graphUpdateRequestScheduled.set(false);
3372                     updates = scrubGraphUpdates();
3373                 }
3374
3375                 if (!GraphToDiagramSynchronizer.this.isAlive())
3376                     return;
3377
3378                 processGraphUpdates(graph, updates);
3379             }
3380         }, new ProcedureAdapter<Object>() {
3381             @Override
3382             public void exception(Throwable t) {
3383                 error(t);
3384             }
3385         });
3386     }
3387
3388     private void processGraphUpdates(ReadGraph graph, final Collection<GraphUpdateReactor> graphUpdates)
3389     throws DatabaseException {
3390         final List<DiagramUpdater> diagramUpdates = new ArrayList<DiagramUpdater>(graphUpdates.size());
3391
3392         // Run GraphUpdaters and gather DiagramUpdaters.
3393         if (DebugPolicy.DEBUG_GRAPH_UPDATE)
3394             System.out.println("Running GRAPH updates: " + graphUpdates);
3395         for (GraphUpdateReactor graphUpdate : graphUpdates) {
3396             DiagramUpdater diagramUpdate = graphUpdate.graphUpdate(graph);
3397             if (diagramUpdate != null) {
3398                 if (DebugPolicy.DEBUG_GRAPH_UPDATE)
3399                     System.out.println(graphUpdate + " => " + diagramUpdate);
3400                 diagramUpdates.add(diagramUpdate);
3401             }
3402         }
3403
3404         if (diagramUpdates.isEmpty())
3405             return;
3406
3407         if (DebugPolicy.DEBUG_DIAGRAM_UPDATE)
3408             System.out.println("Diagram updates: " + diagramUpdates);
3409         Collections.sort(diagramUpdates, DiagramUpdater.DIAGRAM_UPDATER_COMPARATOR);
3410         if (DebugPolicy.DEBUG_DIAGRAM_UPDATE)
3411             System.out.println("Sorted diagram updates: " + diagramUpdates);
3412
3413         ThreadUtils.asyncExec(canvas.getThreadAccess(), new StateRunnable() {
3414             @Override
3415             public void run() {
3416                 if (GraphToDiagramSynchronizer.this.isAlive() && getState() != State.DISPOSED)
3417                     safeRunInState(State.UPDATING_DIAGRAM, this);
3418             }
3419
3420             @Override
3421             public void execute() throws InvocationTargetException {
3422                 // Block out diagram write transactions.
3423                 DiagramUtils.inDiagramTransaction(diagram, TransactionType.READ, new Runnable() {
3424                     @Override
3425                     public void run() {
3426                         if (DebugPolicy.DEBUG_DIAGRAM_UPDATE)
3427                             System.out.println("Running DIAGRAM updates: " + diagramUpdates);
3428                         for (DiagramUpdater update : diagramUpdates) {
3429                             if (DebugPolicy.DEBUG_DIAGRAM_UPDATE)
3430                                 System.out.println("Running DIAGRAM update: " + update);
3431                             update.run();
3432                         }
3433                     }
3434                 });
3435
3436                 setCanvasDirty();
3437             }
3438         });
3439     }
3440
3441     private void attachSessionListener(Session session) {
3442         SessionEventSupport support = session.peekService(SessionEventSupport.class);
3443         if (support != null) {
3444             sessionListener = new TransactionListener();
3445             support.addListener(sessionListener);
3446         }
3447     }
3448
3449     private void detachSessionListener() {
3450         if (sessionListener != null) {
3451             session.getService(SessionEventSupport.class).removeListener(sessionListener);
3452             sessionListener = null;
3453         }
3454     }
3455
3456
3457     // ------------------------------------------------------------------------
3458     // GRAPH TO DIAGRAM SYNCHRONIZATION LOGIC END
3459     // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3460
3461     // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3462     // DIAGRAM CHANGE TRACKING, MAINLY VALIDATION PURPOSES.
3463     // This does not try to synchronize anything back into the graph.
3464     // ------------------------------------------------------------------------
3465
3466     IHintListener elementHintValidator = new HintListenerAdapter() {
3467         @Override
3468         public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
3469             if (!(sender instanceof Element))
3470                 throw new IllegalStateException("invalid sender: " + sender);
3471             Element e = (Element) sender;
3472             if (newValue != null) {
3473                 if (key instanceof TerminalKeyOf) {
3474                     Connection c = (Connection) newValue;
3475                     if (e != c.node)
3476                         throw new IllegalStateException("TerminalKeyOf hint of node " + e + " refers to a different node " + c.node + ". Should be the same.");
3477                     Object edgeObject = ElementUtils.getObject(c.edge);
3478                     if (!(edgeObject instanceof EdgeResource))
3479                         throw new IllegalStateException("EndKeyOf hint of edge " + c.edge + " refers contains an invalid object: " + edgeObject);
3480                 } else if (key instanceof EndKeyOf) {
3481                     Connection c = (Connection) newValue;
3482                     if (e != c.edge)
3483                         throw new IllegalStateException("EndKeyOf hint of edge " + e + " refers to a different edge " + c.edge + ". Should be the same.");
3484                     Object edgeObject = ElementUtils.getObject(c.edge);
3485                     if (!(edgeObject instanceof EdgeResource))
3486                         throw new IllegalStateException("EndKeyOf hint of edge " + e + " refers contains an invalid object: " + edgeObject);
3487                 }
3488             }
3489         }
3490     };
3491
3492     class DiagramListener implements CompositionListener, CompositionVetoListener {
3493         @Override
3494         public boolean beforeElementAdded(IDiagram d, IElement e) {
3495             // Make sure that MutatedElements NEVER get added to the diagram.
3496             if (d == diagram) {
3497                 if (!(e instanceof Element)) {
3498                     // THIS IS NOT GOOD!
3499                     error("Attempting to add another implementation of IElement besides Element (=" + e.getElementClass().getClass().getName() + ") to the synchronized diagram which means that there is a bug somewhere! See stack trace to find out who is doing this!", new Exception("stacktrace"));
3500                     System.err.println("Attempting to add another implementation of IElement besides Element (=" + e.getElementClass().getClass().getName() + ") to the synchronized diagram which means that there is a bug somewhere! See Error Log.");
3501                     return false;
3502                 }
3503
3504                 // Perform sanity checks that might veto the element addition.
3505                 boolean pass = true;
3506
3507                 // Check that all elements added to the diagram are adaptable to Resource
3508                 ElementClass ec = e.getElementClass();
3509                 Resource resource = ElementUtils.adapt(ec, Resource.class);
3510                 if (resource == null) {
3511                     pass = false;
3512                     LOGGER.error("", new Exception("Attempted to add an element to the diagram that is not adaptable to Resource: " + e + ", class: " + ec));
3513                 }
3514
3515                 // Sanity check connection hints
3516                 for (Map.Entry<TerminalKeyOf, Object> entry : e.getHintsOfClass(TerminalKeyOf.class).entrySet()) {
3517                     Connection c = (Connection) entry.getValue();
3518                     Object edgeObject = ElementUtils.getObject(c.edge);
3519                     if (e != c.node) {
3520                         System.err.println("Invalid node in TerminalKeyOf hint: " + entry.getKey() + "=" + entry.getValue());
3521                         System.err.println("\tconnection.edge=" + c.edge);
3522                         System.err.println("\tconnection.node=" + c.node);
3523                         System.err.println("\tconnection.end=" + c.end);
3524                         System.err.println("\telement=" + e);
3525                         System.err.println("\telement class=" + e.getElementClass());
3526                         pass = false;
3527                     }
3528                     if (!(edgeObject instanceof EdgeResource)) {
3529                         System.err.println("Invalid object in TerminalKeyOf hint edge: " + entry.getKey() + "=" + entry.getValue());
3530                         System.err.println("\tconnection.edge=" + c.edge);
3531                         System.err.println("\tconnection.node=" + c.node);
3532                         System.err.println("\tconnection.end=" + c.end);
3533                         System.err.println("\telement=" + e);
3534                         System.err.println("\telement class=" + e.getElementClass());
3535                         pass = false;
3536                     }
3537                 }
3538
3539                 return pass;
3540             }
3541             return true;
3542         }
3543
3544         @Override
3545         public boolean beforeElementRemoved(IDiagram d, IElement e) {
3546             // Never veto diagram changes.
3547             return true;
3548         }
3549
3550         @Override
3551         public void onElementAdded(IDiagram d, IElement e) {
3552             if (DebugPolicy.DEBUG_ELEMENT_LIFECYCLE)
3553                 System.out.println("[" + d + "] element added: " + e);
3554
3555             if (USE_ELEMENT_VALIDATING_LISTENERS)
3556                 e.addHintListener(elementHintValidator);
3557         }
3558
3559         @Override
3560         public void onElementRemoved(IDiagram d, IElement e) {
3561             if (DebugPolicy.DEBUG_ELEMENT_LIFECYCLE)
3562                 System.out.println("[" + d + "] element removed: " + e);
3563
3564             if (USE_ELEMENT_VALIDATING_LISTENERS)
3565                 e.removeHintListener(elementHintValidator);
3566
3567             if (e.containsHint(KEY_REMOVE_RELATIONSHIPS))
3568                 relationshipHandler.denyAll(diagram, e);
3569         }
3570     }
3571
3572     DiagramListener diagramListener = new DiagramListener();
3573
3574     static void removeNodeTopologyHints(Element node) {
3575         Set<TerminalKeyOf> terminalKeys = node.getHintsOfClass(TerminalKeyOf.class).keySet();
3576         if (!terminalKeys.isEmpty()) {
3577             removeNodeTopologyHints(node, terminalKeys);
3578         }
3579     }
3580
3581     static void removeNodeTopologyHints(Element node, Collection<TerminalKeyOf> terminalKeys) {
3582         for (TerminalKeyOf key : terminalKeys) {
3583             Connection c = node.removeHintWithoutNotification(key);
3584             if (c != null) {
3585                 removeEdgeTopologyHints((Element) c.edge);
3586             }
3587         }
3588     }
3589
3590     static void removeEdgeTopologyHints(Element edge) {
3591         Object edgeData = edge.getHint(ElementHints.KEY_OBJECT);
3592         for (EndKeyOf key : EndKeyOf.KEYS) {
3593             Connection c = edge.removeHintWithoutNotification(key);
3594             if (c != null) {
3595                 ((Element) c.node).removeHintWithoutNotification(new TerminalKeyOf(c.terminal, edgeData, Connection.class));
3596             }
3597         }
3598     }
3599
3600     // ------------------------------------------------------------------------
3601     // DIAGRAM CHANGE TRACKING, MAINLY VALIDATION PURPOSES.
3602     // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3603
3604     // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3605     // BACKEND TO DIAGRAM LOAD/LISTEN LOGIC BEGIN
3606     // ------------------------------------------------------------------------
3607
3608     void adaptDiagramClass(AsyncReadGraph graph, Resource diagram, final AsyncProcedure<DiagramClass> procedure) {
3609         graph.forAdapted(diagram, DiagramClass.class, new AsyncProcedure<DiagramClass>() {
3610             @Override
3611             public void exception(AsyncReadGraph graph, Throwable throwable) {
3612                 procedure.exception(graph, throwable);
3613             }
3614
3615             @Override
3616             public void execute(AsyncReadGraph graph, DiagramClass dc) {
3617                 // To move TopologyImpl out of here, we need a separate
3618                 // DiagramClassFactory that takes a canvas context as an argument.
3619                 // DataElementMapImpl, ElementFactoryImpl and diagramLifeCycle can
3620                 // safely stay here.
3621                 procedure.execute(graph, dc.newClassWith(
3622                         // This handler takes care of the topology of the diagram model.
3623                         // It sets and fixes element hints related to describing the
3624                         // connectivity of elements.
3625                         diagramTopology,
3626                         // TODO: not quite sure whether this can prove itself useful or not.
3627                         elementFactory,
3628                         // This map provides a bidirectional mapping between
3629                         // IElement and back-end objects.
3630                         dataElementMap,
3631                         // This handler provides a facility to adapt an element class
3632                         // to work properly with a diagram synchronized using this
3633                         // GraphToDiagramSynchronizer.
3634                         substituteElementClass,
3635                         // These handlers provide a way to create simple identified
3636                         // uni- and bidirectional relationships between any diagram
3637                         // objects/elements.
3638                         relationshipHandler));
3639             }
3640         });
3641     }
3642
3643     static Connection connect(IElement edge, EdgeEnd end, IElement element, Terminal terminal) {
3644         Connection c = new Connection(edge, end, element, terminal);
3645
3646         Object edgeData = edge.getHint(ElementHints.KEY_OBJECT);
3647         if (DebugPolicy.DEBUG_CONNECTION) {
3648             System.out.println("[connect](edge=" + edge + ", edgeData=" + edgeData + ", end=" + end + ", element="
3649                     + element + ", terminal=" + terminal + ")");
3650         }
3651
3652         TerminalKeyOf key = new TerminalKeyOf(terminal, edgeData, Connection.class);
3653         element.setHint(key, c);
3654
3655         EndKeyOf key2 = EndKeyOf.get(end);
3656         edge.setHint(key2, c);
3657
3658         return c;
3659     }
3660
3661     static class ElementFactoryImpl implements ElementFactory {
3662         @Override
3663         public IElement spawnNew(ElementClass clazz) {
3664             IElement e = Element.spawnNew(clazz);
3665             return e;
3666         }
3667     }
3668
3669     ElementFactoryImpl elementFactory = new ElementFactoryImpl();
3670
3671     public static final Object FIRST_TIME = new Object() {
3672         @Override
3673         public String toString() {
3674             return "FIRST_TIME";
3675         }
3676     };
3677
3678
3679     /**
3680      * A base for all listeners of graph requests performed internally by
3681      * GraphToDiagramSynchronizer.
3682      *
3683      * @param <T> type of stored data element
3684      * @param <Result> query result type
3685      */
3686     abstract class BaseListener<T, Result> implements AsyncListener<Result> {
3687
3688         protected final T    data;
3689
3690         private Object       oldResult = FIRST_TIME;
3691
3692         protected boolean    disposed  = false;
3693
3694         final ICanvasContext canvas;
3695
3696         public BaseListener(T data) {
3697             this.canvas = GraphToDiagramSynchronizer.this.canvas;
3698             this.data = data;
3699         }
3700
3701         @Override
3702         public void exception(AsyncReadGraph graph, Throwable throwable) {
3703             // Exceptions are always expected to mean that the listener should
3704             // be considered disposed once a query fails.
3705             disposed = true;
3706         }
3707
3708         abstract void execute(AsyncReadGraph graph, Object oldResult, Object newResult);
3709
3710         @Override
3711         public void execute(AsyncReadGraph graph, Result result) {
3712             if (DebugPolicy.DEBUG_LISTENER_BASE)
3713                 System.out.println("BaseListener: " + result);
3714
3715             if (disposed) {
3716                 if (DebugPolicy.DEBUG_LISTENER_BASE)
3717                     System.out.println("BaseListener: execute invoked although listener is disposed!");
3718                 return;
3719             }
3720
3721             // A null result will permanently mark this listener disposed!
3722             if (result == null) {
3723                 disposed = true;
3724                 if (DebugPolicy.DEBUG_LISTENER_BASE)
3725                     System.out.println(this + " null result, listener marked disposed");
3726             }
3727
3728             if (oldResult == FIRST_TIME) {
3729                 oldResult = result;
3730                 if (DebugPolicy.DEBUG_LISTENER_BASE)
3731                     System.out.println(this + " first result computed: " + result);
3732             } else {
3733                 if (DebugPolicy.DEBUG_LISTENER_BASE)
3734                     System.out.println(this + " result changed from '" + oldResult + "' to '" + result + "'");
3735                 try {
3736                     execute(graph, oldResult, result);
3737                 } finally {
3738                     oldResult = result;
3739                 }
3740             }
3741         }
3742
3743         @Override
3744         public boolean isDisposed() {
3745             if (disposed)
3746                 return true;
3747
3748             boolean alive = isAlive();
3749             //System.out.println(getClass().getName() + ": isDisposed(" + resource.getResourceId() + "): canvas=" + canvas + ", isAlive=" + alive);
3750             if (!alive)
3751                 return true;
3752             // If a mapping no longer exists for this element, dispose of this
3753             // listener.
3754             //IElement e = getMappedElement(resource);
3755             //System.out.println(getClass().getName() + ": isDisposed(" + resource.getResourceId() + "): canvas=" + canvas + ", element=" + e);
3756             //return e == null;
3757             return false;
3758         }
3759
3760 //        @Override
3761 //        protected void finalize() throws Throwable {
3762 //            System.out.println("finalize listener: " + this);
3763 //            super.finalize();
3764 //        }
3765     }
3766
3767     class DiagramClassRequest extends BaseRequest2<Resource, DiagramClass> {
3768         public DiagramClassRequest(Resource resource) {
3769             super(GraphToDiagramSynchronizer.this.canvas, resource);
3770         }
3771
3772         @Override
3773         public void perform(AsyncReadGraph graph, AsyncProcedure<DiagramClass> procedure) {
3774             adaptDiagramClass(graph, data, procedure);
3775         }
3776     }
3777
3778     public class DiagramContentListener extends BaseListener<Resource, DiagramContents> {
3779
3780         public DiagramContentListener(Resource resource) {
3781             super(resource);
3782         }
3783
3784         @Override
3785         public void execute(final AsyncReadGraph graph, Object oldResult, Object newResult) {
3786             final DiagramContents newContent = (newResult == null) ? new DiagramContents()
3787             : (DiagramContents) newResult;
3788
3789             // diagramGraphUpdater is called synchronously during
3790             // loading. The first result will not get updated through
3791             // this listener but through loadDiagram.
3792
3793             if (DebugPolicy.DISABLE_DIAGRAM_UPDATES) {
3794                 System.out.println("Skipped diagram content update: " + newResult);
3795                 return;
3796             }
3797
3798             if (DebugPolicy.DEBUG_DIAGRAM_LISTENER)
3799                 System.out.println("diagram contents changed: " + oldResult + " => " + newResult);
3800
3801             offerGraphUpdate( diagramGraphUpdater(newContent) );
3802         }
3803
3804         @Override
3805         public boolean isDisposed() {
3806             return !isAlive();
3807         }
3808
3809         @Override
3810         public void exception(AsyncReadGraph graph, Throwable t) {
3811             super.exception(graph, t);
3812             error("DiagramContentRequest failed", t);
3813         }
3814     }
3815
3816     // ------------------------------------------------------------------------
3817     // BACKEND TO DIAGRAM LOAD/LISTEN LOGIC END
3818     // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3819
3820     // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3821     // GRAPH-CUSTOMIZED DIAGRAM TOPOLOGY HANDLER BEGIN
3822     // ------------------------------------------------------------------------
3823
3824     static class TopologyImpl implements Topology {
3825
3826         @Override
3827         public Connection getConnection(IElement edge, EdgeEnd end) {
3828             Key key = EndKeyOf.get(end);
3829             Connection c = edge.getHint(key);
3830             if (c == null)
3831                 return null;
3832             return c;
3833         }
3834
3835         @Override
3836         public void getConnections(IElement node, Terminal terminal, Collection<Connection> connections) {
3837 //            IDiagram d = ElementUtils.getDiagram(node);
3838             for (Map.Entry<TerminalKeyOf, Object> entry : node.getHintsOfClass(TerminalKeyOf.class).entrySet()) {
3839                 // First check that the terminal matches.
3840                 TerminalKeyOf key = entry.getKey();
3841                 if (!key.getTerminal().equals(terminal))
3842                     continue;
3843
3844                 Connection c = (Connection) entry.getValue();
3845                 if (c != null) {
3846                     connections.add(c);
3847                 }
3848             }
3849         }
3850
3851         @Override
3852         public void connect(IElement edge, EdgeEnd end, IElement node, Terminal terminal) {
3853             if (node != null && terminal != null)
3854                 GraphToDiagramSynchronizer.connect(edge, end, node, terminal);
3855
3856             if (DebugPolicy.DEBUG_CONNECTION) {
3857                 if (end == EdgeEnd.Begin)
3858                     System.out.println("Connection started from: " + edge + ", " + end + ", " + node + ", " + terminal);
3859                 else
3860                     System.out.println("Creating connection to: " + edge + ", " + end + ", " + node + ", " + terminal);
3861             }
3862         }
3863
3864         @Override
3865         public void disconnect(IElement edge, EdgeEnd end, IElement node, Terminal terminal) {
3866             EndKeyOf edgeKey = EndKeyOf.get(end);
3867             Connection c = edge.getHint(edgeKey);
3868             if (c == null)
3869                 throw new UnsupportedOperationException("cannot disconnect, no Connection in edge " + edge);
3870
3871             for (Map.Entry<TerminalKeyOf, Object> entry : node.getHintsOfClass(TerminalKeyOf.class).entrySet()) {
3872                 Connection cc = (Connection) entry.getValue();
3873                 if (c == cc) {
3874                     node.removeHint(entry.getKey());
3875                     edge.removeHint(edgeKey);
3876                     return;
3877                 }
3878             }
3879
3880             throw new UnsupportedOperationException("cannot disconnect, no connection between found between edge "
3881                     + edge + " and node " + node);
3882         }
3883     }
3884
3885     Topology            diagramTopology     = new TopologyImpl();
3886
3887     // ------------------------------------------------------------------------
3888     // GRAPH-CUSTOMIZED DIAGRAM TOPOLOGY HANDLER END
3889     // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3890
3891     // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3892     // DIAGRAM OBJECT RELATIONSHIP HANDLER BEGIN
3893     // ------------------------------------------------------------------------
3894
3895     RelationshipHandler relationshipHandler = new RelationshipHandler() {
3896
3897         AssociativeMap map = new AssociativeMap(Associativity.of(true, false, false));
3898
3899         Object getPossibleObjectOrElement(Object o) {
3900             if (o instanceof IElement) {
3901                 IElement e = (IElement) o;
3902                 Object oo = e.getHint(ElementHints.KEY_OBJECT);
3903                 return oo != null ? oo : e;
3904             }
3905             return o;
3906         }
3907
3908         IElement getElement(Object o) {
3909             if (o instanceof IElement)
3910                 return (IElement) o;
3911             return getMappedElement(o);
3912         }
3913
3914         @Override
3915         public void claim(IDiagram diagram, Object subject,
3916                 Relationship predicate, Object object) {
3917             Object sd = getPossibleObjectOrElement(subject);
3918             Object od = getPossibleObjectOrElement(object);
3919
3920             Collection<Tuple> ts = null;
3921             Relationship inverse = predicate.getInverse();
3922             if (inverse != null)
3923                 ts = Arrays.asList(new Tuple(sd, predicate, od), new Tuple(od, inverse, sd));
3924             else
3925                 ts = Collections.singletonList(new Tuple(sd, predicate, od));
3926
3927             synchronized (this) {
3928                 map.add(ts);
3929             }
3930
3931             if (DebugPolicy.DEBUG_RELATIONSHIP) {
3932                 new Exception().printStackTrace();
3933                 System.out.println("Claimed relationships:");
3934                 for (Tuple t : ts)
3935                     System.out.println("\t" + t);
3936             }
3937         }
3938
3939         private void doDeny(IDiagram diagram, Object subject,
3940                 Relationship predicate, Object object) {
3941             Object sd = getPossibleObjectOrElement(subject);
3942             Object od = getPossibleObjectOrElement(object);
3943             if (sd == subject || od == object) {
3944                 System.out
3945                 .println("WARNING: denying relationship '"
3946                         + predicate
3947                         + "' between diagram element(s), not back-end object(s): "
3948                         + sd + " -> " + od);
3949             }
3950
3951             Collection<Tuple> ts = null;
3952             Relationship inverse = predicate.getInverse();
3953             if (inverse != null)
3954                 ts = Arrays.asList(new Tuple(sd, predicate, od), new Tuple(od,
3955                         inverse, sd));
3956             else
3957                 ts = Collections.singleton(new Tuple(sd, predicate, od));
3958
3959             synchronized (this) {
3960                 map.remove(ts);
3961             }
3962
3963             if (DebugPolicy.DEBUG_RELATIONSHIP) {
3964                 new Exception().printStackTrace();
3965                 System.out.println("Denied relationships:");
3966                 for (Tuple t : ts)
3967                     System.out.println("\t" + t);
3968             }
3969         }
3970
3971         @Override
3972         public void deny(IDiagram diagram, Object subject,
3973                 Relationship predicate, Object object) {
3974             synchronized (this) {
3975                 doDeny(diagram, subject, predicate, object);
3976             }
3977         }
3978
3979         @Override
3980         public void deny(IDiagram diagram, Relation relation) {
3981             synchronized (this) {
3982                 doDeny(diagram, relation.getSubject(), relation
3983                         .getRelationship(), relation.getObject());
3984             }
3985         }
3986
3987         @Override
3988         public void denyAll(IDiagram diagram, Object element) {
3989             synchronized (this) {
3990                 for (Relation relation : getRelations(diagram, element, null)) {
3991                     doDeny(diagram, relation.getSubject(), relation
3992                             .getRelationship(), relation.getObject());
3993                 }
3994             }
3995         }
3996
3997         @Override
3998         public Collection<Relation> getRelations(IDiagram diagram,
3999                 Object element, Collection<Relation> result) {
4000             if (DebugPolicy.DEBUG_GET_RELATIONSHIP)
4001                 System.out.println("getRelations(" + element + ")");
4002             Object e = getPossibleObjectOrElement(element);
4003
4004             Collection<Tuple> tuples = null;
4005             synchronized (this) {
4006                 tuples = map.get(new Tuple(e, null, null), null);
4007             }
4008
4009             if (DebugPolicy.DEBUG_GET_RELATIONSHIP) {
4010                 System.out.println("Result size: " + tuples.size());
4011                 for (Tuple t : tuples)
4012                     System.out.println("\t" + t);
4013             }
4014
4015             if (tuples.isEmpty())
4016                 return Collections.emptyList();
4017             if (result == null)
4018                 result = new ArrayList<Relation>(tuples.size());
4019             for (Tuple t : tuples) {
4020                 Object obj = t.getField(2);
4021                 IElement el = getElement(obj);
4022                 Relationship r = (Relationship) t.getField(1);
4023                 result.add(new Relation(element, r, el != null ? el : obj));
4024             }
4025             return result;
4026         }
4027
4028     };
4029
4030     // ------------------------------------------------------------------------
4031     // DIAGRAM ELEMENT RELATIONSHIP HANDLER END
4032     // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
4033
4034 }