]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/GraphToDiagramSynchronizer.java
03cedfcb39a66cae558ccd27f0431bffa3dadadf
[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(ReadGraph 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.syncRequest(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.syncRequest(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(ReadGraph 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.syncRequest(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.syncRequest(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(ReadGraph 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.syncRequest(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.syncRequest(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.syncRequest(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             // NOTE: This order is important.
2323             Object task = Timing.BEGIN("processNodesConnections");
2324             //System.out.println("---- PROCESS NODES & CONNECTIONS BEGIN");
2325             if (!changes.elements.isEmpty()) {
2326                 graph.syncRequest(new ReadRequest() {
2327                     @Override
2328                     public void run(ReadGraph graph) throws DatabaseException {
2329                         processNodes(graph);
2330                     }
2331                     @Override
2332                     public String toString() {
2333                         return "processNodes";
2334                     }
2335                 });
2336             }
2337             //System.out.println("---- PROCESS NODES & CONNECTIONS END");
2338
2339             processConnections();
2340
2341             //System.out.println("---- PROCESS BRANCH POINTS BEGIN");
2342             if (!changes.branchPoints.isEmpty()) {
2343                 graph.syncRequest(new ReadRequest() {
2344                     @Override
2345                     public void run(ReadGraph graph) throws DatabaseException {
2346                         processBranchPoints(graph);
2347                     }
2348                     @Override
2349                     public String toString() {
2350                         return "processBranchPoints";
2351                     }
2352                 });
2353             }
2354             //System.out.println("---- PROCESS BRANCH POINTS END");
2355
2356             Timing.END(task);
2357             task = Timing.BEGIN("processConnectionSegments");
2358
2359             //System.out.println("---- PROCESS CONNECTION SEGMENTS BEGIN");
2360             if (!changes.connectionSegments.isEmpty()) {
2361                 graph.syncRequest(new ReadRequest() {
2362                     @Override
2363                     public void run(ReadGraph graph) throws DatabaseException {
2364                         processConnectionSegments(graph);
2365                     }
2366                     @Override
2367                     public String toString() {
2368                         return "processConnectionSegments";
2369                     }
2370                 });
2371             }
2372             //System.out.println("---- PROCESS CONNECTION SEGMENTS END");
2373
2374             Timing.END(task);
2375
2376             task = Timing.BEGIN("processRouteGraphConnections");
2377             if (!changes.routeGraphConnections.isEmpty()) {
2378                 graph.syncRequest(new ReadRequest() {
2379                     @Override
2380                     public void run(ReadGraph graph) throws DatabaseException {
2381                         processRouteGraphConnections(graph);
2382                     }
2383                     @Override
2384                     public String toString() {
2385                         return "processRouteGraphConnections";
2386                     }
2387                 });
2388             }
2389             Timing.END(task);
2390
2391             //System.out.println("---- AFTER LOADING");
2392             //for (IElement e : addedElements)
2393             //    System.out.println("    ADDED ELEMENT: " + e);
2394             //for (IElement e : addedBranchPoints)
2395             //    System.out.println("    ADDED BRANCH POINTS: " + e);
2396
2397             task = Timing.BEGIN("executeDeferredLoaders");
2398             executeDeferredLoaders(graph);
2399             Timing.END(task);
2400         }
2401
2402         public boolean isEmpty() {
2403             return addedElements.isEmpty() && removedElements.isEmpty()
2404             && addedConnectionSegments.isEmpty() && removedConnectionSegments.isEmpty()
2405             && addedBranchPoints.isEmpty() && removedBranchPoints.isEmpty()
2406             && addedConnectionEntities.isEmpty() && removedConnectionEntities.isEmpty()
2407             && addedRouteGraphConnectionMap.isEmpty() && removedRouteGraphConnections.isEmpty()
2408             && !changes.elementOrderChanged;
2409         }
2410
2411         class DefaultConnectionSegmentAdapter implements ConnectionSegmentAdapter {
2412
2413             @Override
2414             public void getClass(AsyncReadGraph graph, EdgeResource edge, ConnectionInfo info, ListenerSupport listenerSupport, ICanvasContext canvas, IDiagram diagram, final AsyncProcedure<ElementClass> procedure) {
2415                 if (info.connectionType != null) {
2416                     NodeClassRequest request = new NodeClassRequest(canvas, diagram, info.connectionType, true);
2417                     graph.asyncRequest(request, new CacheListener<ElementClass>(listenerSupport));
2418                     graph.asyncRequest(request, procedure);
2419                 } else {
2420                     procedure.execute(graph, null);
2421                 }
2422             }
2423
2424             @Override
2425             public void load(AsyncReadGraph graph, final EdgeResource edge, final ConnectionInfo info, ListenerSupport listenerSupport, ICanvasContext canvas, final IDiagram diagram, final IElement element) {
2426                 graph.asyncRequest(new Read<IElement>() {
2427                     @Override
2428                     public IElement perform(ReadGraph graph) throws DatabaseException {
2429                         //ITask task = ThreadLogger.getInstance().begin("LoadSegment");
2430                         syncLoad(graph, edge, info, diagram, element);
2431                         //task.finish();
2432                         return element;
2433                     }
2434                     @Override
2435                     public String toString() {
2436                         return "defaultConnectionSegmentAdapter";
2437                     }
2438                 }, new DisposableListener<IElement>(listenerSupport) {
2439                     
2440                     @Override
2441                     public String toString() {
2442                         return "DefaultConnectionSegmentAdapter listener for " + edge;
2443                     }
2444                     
2445                     @Override
2446                     public void execute(IElement loaded) {
2447                         // Invoked when the element has been loaded.
2448
2449                         if (loaded == null) {
2450                             disposeListener();
2451                             return;
2452                         }
2453
2454                         Object data = loaded.getHint(ElementHints.KEY_OBJECT);
2455
2456                         if (DebugPolicy.DEBUG_EDGE_LISTENER)
2457                             System.out.println("EDGE LoadListener for " + loaded + " " + data);
2458
2459                         if (addedElementMap.containsKey(data)) {
2460                             // This element was just loaded, in
2461                             // which case its hints need to
2462                             // uploaded to the real mapped
2463                             // element immediately.
2464                             IElement mappedElement = getMappedElement(data);
2465                             if (DebugPolicy.DEBUG_EDGE_LISTENER)
2466                                 System.out.println("LOADED ADDED EDGE, currently mapped element: " + mappedElement);
2467                             if (mappedElement != null && (mappedElement instanceof Element)) {
2468                                 if (DebugPolicy.DEBUG_EDGE_LISTENER) {
2469                                     System.out.println("  mapped hints: " + mappedElement.getHints());
2470                                     System.out.println("  loaded hints: " + loaded.getHints());
2471                                 }
2472                                 updateMappedElement((Element) mappedElement, loaded);
2473                             }
2474                         } else {
2475                             // This element was already loaded.
2476                             // Just schedule an update some time
2477                             // in the future.
2478                             if (DebugPolicy.DEBUG_EDGE_LISTENER)
2479                                 System.out.println("PREVIOUSLY LOADED EDGE UPDATED, scheduling update into the future");
2480                             offerGraphUpdate( edgeUpdater(element, loaded) );
2481                         }
2482                     }
2483                 });
2484             }
2485
2486             void syncLoad(ReadGraph graph, EdgeResource connectionSegment, ConnectionInfo info, IDiagram diagram, IElement element) throws DatabaseException {
2487                 // Check that at least some data exists before continuing further.
2488                 if (!graph.hasStatement(connectionSegment.first()) && !graph.hasStatement(connectionSegment.second())) {
2489                     return;
2490                 }
2491
2492                 // Validate that both ends of the segment are
2493                 // part of the same connection before loading.
2494                 // This will happen for connections that are
2495                 // modified through splitting and joining of
2496                 // connection segments.
2497                 Resource c = ConnectionUtil.tryGetConnection(graph, connectionSegment);
2498                 if (c == null) {
2499                     // Ok, this segment is somehow invalid. Just don't load it.
2500                     if (DebugPolicy.DEBUG_CONNECTION_LOAD)
2501                         System.out.println("Skipping edge " + connectionSegment + ". Both segment ends are not part of the same connection.");
2502                     return;
2503                 }
2504
2505                 if (!info.isValid()) {
2506                     // This edge must be somehow invalid, don't proceed with loading.
2507                     if (DebugPolicy.DEBUG_CONNECTION_LOAD)
2508                         warning("Cannot load edge " + connectionSegment + ". ConnectionInfo " + info + " is invalid.", new DebugException("execution trace"));
2509                     return;
2510                 }
2511
2512                 Element edge = (Element) element;
2513                 edge.setHint(ElementHints.KEY_OBJECT, connectionSegment);
2514
2515                 // connectionSegment resources may currently be in a different
2516                 // order than ConnectionInfo.firstEnd/secondEnd. Therefore the
2517                 // segment ends must be resolved here.
2518                 ConnectionSegmentEnd firstEnd = DiagramGraphUtil.resolveConnectionSegmentEnd(graph, connectionSegment.first());
2519                 ConnectionSegmentEnd secondEnd = DiagramGraphUtil.resolveConnectionSegmentEnd(graph, connectionSegment.second());
2520                 if (firstEnd == null || secondEnd == null) {
2521                     if (DebugPolicy.DEBUG_CONNECTION_LOAD)
2522                         warning("End attachments for edge " + connectionSegment + " are unresolved: (" + firstEnd + "," + secondEnd + ")", new DebugException("execution trace"));
2523                     return;
2524                 }
2525
2526                 if (DebugPolicy.DEBUG_CONNECTION_LOAD)
2527                     System.out.println("CONNECTION INFO: " + connectionSegment + " - " + info);
2528                 DesignatedTerminal firstTerminal = DiagramGraphUtil.findDesignatedTerminal(
2529                         graph, diagram, connectionSegment.first(), firstEnd);
2530                 DesignatedTerminal secondTerminal = DiagramGraphUtil.findDesignatedTerminal(
2531                         graph, diagram, connectionSegment.second(), secondEnd);
2532
2533                 // Edges must be connected at both ends in order for edge loading to succeed.
2534                 String err = validateConnectivity(graph, connectionSegment, firstTerminal, secondTerminal);
2535                 if (err != null) {
2536                     // Stop loading edge if the connectivity cannot be completely resolved.
2537                     if (DebugPolicy.DEBUG_CONNECTION_LOAD)
2538                         warning(err, null);
2539                     return;
2540                 }
2541
2542                 graph.syncRequest(new AsyncReadRequest() {
2543                     @Override
2544                     public void run(AsyncReadGraph graph) {
2545                         // NOTICE: Layer information is loaded from the connection entity resource
2546                         // that is shared by all segments of the same connection.
2547                         ElementFactoryUtil.loadLayersForElement(graph, layerManager, diagram, edge, info.connection,
2548                                 new AsyncProcedureAdapter<IElement>() {
2549                             @Override
2550                             public void exception(AsyncReadGraph graph, Throwable t) {
2551                                 error("failed to load layers for connection segment", t);
2552                             }
2553                         });
2554                     }
2555                 });
2556
2557                 edge.setHintWithoutNotification(KEY_CONNECTION_BEGIN_PLACEHOLDER, new PlaceholderConnection(
2558                         EdgeEnd.Begin,
2559                         firstTerminal.element.getHint(ElementHints.KEY_OBJECT),
2560                         firstTerminal.terminal));
2561                 edge.setHintWithoutNotification(KEY_CONNECTION_END_PLACEHOLDER, new PlaceholderConnection(
2562                         EdgeEnd.End,
2563                         secondTerminal.element.getHint(ElementHints.KEY_OBJECT),
2564                         secondTerminal.terminal));
2565
2566                 IModelingRules modelingRules = diagram.getHint(DiagramModelHints.KEY_MODELING_RULES);
2567                 if (modelingRules != null) {
2568                     ConnectionVisualsLoader loader = diagram.getHint(DiagramModelHints.KEY_CONNECTION_VISUALS_LOADER);
2569                     if (loader != null)
2570                         loader.loadConnectionVisuals(graph, modelingRules, info.connection, diagram, edge, firstTerminal, secondTerminal);
2571                     else
2572                         DiagramGraphUtil.loadConnectionVisuals(graph, modelingRules, info.connection, diagram, edge, firstTerminal, secondTerminal);
2573                 }
2574             }
2575
2576             private String validateConnectivity(ReadGraph graph, EdgeResource edge,
2577                     DesignatedTerminal firstTerminal,
2578                     DesignatedTerminal secondTerminal)
2579             throws DatabaseException {
2580                 boolean firstLoose = firstTerminal == null;
2581                 boolean secondLoose = secondTerminal == null;
2582                 boolean stray = firstLoose && secondLoose;
2583                 if (firstTerminal == null || secondTerminal == null) {
2584                     StringBuilder sb = new StringBuilder();
2585                     sb.append("encountered ");
2586                     sb.append(stray ? "stray" : "loose");
2587                     sb.append(" connection segment, ");
2588                     if (firstLoose)
2589                         sb.append("first ");
2590                     if (stray)
2591                         sb.append("and ");
2592                     if (secondLoose)
2593                         sb.append("second ");
2594                     sb.append("end disconnected: ");
2595                     sb.append(edge.toString(graph));
2596                     sb.append(" - ");
2597                     sb.append(edge.toString());
2598                     return sb.toString();
2599                 }
2600                 return null;
2601             }
2602
2603         }
2604
2605         ConnectionSegmentAdapter connectionSegmentAdapter = new DefaultConnectionSegmentAdapter();
2606
2607     }
2608
2609     private static final Double DIAGRAM_UPDATE_DIAGRAM_PRIORITY = 1d;
2610     private static final Double DIAGRAM_UPDATE_NODE_PRIORITY = 2d;
2611     private static final Double DIAGRAM_UPDATE_CONNECTION_PRIORITY = 3d;
2612     private static final Double DIAGRAM_UPDATE_EDGE_PRIORITY = 4d;
2613
2614     interface DiagramUpdater extends Runnable {
2615         Double getPriority();
2616
2617         Comparator<DiagramUpdater> DIAGRAM_UPDATER_COMPARATOR = new Comparator<DiagramUpdater>() {
2618             @Override
2619             public int compare(DiagramUpdater o1, DiagramUpdater o2) {
2620                 return o1.getPriority().compareTo(o2.getPriority());
2621             }
2622         };
2623     }
2624
2625     interface GraphUpdateReactor {
2626         DiagramUpdater graphUpdate(ReadGraph graph) throws DatabaseException;
2627     }
2628
2629     static abstract class AbstractDiagramUpdater implements DiagramUpdater, GraphUpdateReactor {
2630         protected final Double priority;
2631         protected final String runnerName;
2632
2633         public AbstractDiagramUpdater(Double priority, String runnerName) {
2634             if (priority == null)
2635                 throw new NullPointerException("null priority");
2636             if (runnerName == null)
2637                 throw new NullPointerException("null runner name");
2638             this.priority = priority;
2639             this.runnerName = runnerName;
2640         }
2641
2642         @Override
2643         public Double getPriority() {
2644             return priority;
2645         }
2646
2647         @Override
2648         public AbstractDiagramUpdater graphUpdate(ReadGraph graph) {
2649             return this;
2650         }
2651
2652         @Override
2653         public void run() {
2654             Object task = Timing.BEGIN(runnerName);
2655             forDiagram();
2656             Timing.END(task);
2657         }
2658
2659         protected void forDiagram() {
2660         }
2661
2662         @Override
2663         public String toString() {
2664             return runnerName + "@" + System.identityHashCode(this) + " [" + priority + "]";
2665         }
2666     }
2667
2668     /**
2669      * @param content the new contents of the diagram, must not be
2670      *        <code>null</code>.
2671      */
2672     private GraphUpdateReactor diagramGraphUpdater(final DiagramContents content) {
2673         if (content == null)
2674             throw new NullPointerException("null diagram content");
2675
2676         return new GraphUpdateReactor() {
2677             @Override
2678             public String toString() {
2679                 return "DiagramGraphUpdater@" + System.identityHashCode(this);
2680             }
2681
2682             @Override
2683             public DiagramUpdater graphUpdate(ReadGraph graph) throws DatabaseException {
2684                 // Never do anything here if the canvas has already been disposed.
2685                 if (!GraphToDiagramSynchronizer.this.isAlive())
2686                     return null;
2687
2688                 // We must be prepared for the following changes in the diagram graph
2689                 // model:
2690                 // - the diagram has been completely removed
2691                 // - elements have been added
2692                 // - elements have been removed
2693                 //
2694                 // Element-specific changes are handled by the element query listeners:
2695                 // - elements have been modified
2696                 // - element position has changed
2697                 // - element class (e.g. image) has changed
2698
2699                 diagramUpdateLock.lock();
2700                 try {
2701                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE)
2702                         System.out.println("In diagramGraphUpdater:");
2703
2704                     // Find out what has changed since the last query.
2705                     Object task = Timing.BEGIN("diagramContentDifference");
2706                     DiagramContents lastContent = previousContent;
2707                     DiagramContentChanges changes = content.differenceFrom(previousContent);
2708                     previousContent = content;
2709                     Timing.END(task);
2710                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE)
2711                         System.out.println("  changes: " + changes);
2712
2713                     // Bail out if there are no changes to react to.
2714                     if (changes.isEmpty())
2715                         return null;
2716
2717                     // Load everything that needs to be loaded from the graph,
2718                     // but don't update the UI model in this thread yet.
2719                     task = Timing.BEGIN("updater.process");
2720                     GraphToDiagramUpdater updater = new GraphToDiagramUpdater(lastContent, content, changes);
2721                     GraphToDiagramSynchronizer.this.currentUpdater = updater;
2722                     try {
2723                         updater.process(graph);
2724                     } finally {
2725                         GraphToDiagramSynchronizer.this.currentUpdater = null;
2726                     }
2727                     Timing.END(task);
2728
2729                     if (updater.isEmpty())
2730                         return null;
2731
2732                     // Return an updater that will update the UI run-time model.
2733                     return diagramUpdater(updater);
2734                 } finally {
2735                     diagramUpdateLock.unlock();
2736                 }
2737             }
2738         };
2739     }
2740
2741     DiagramUpdater diagramUpdater(final GraphToDiagramUpdater updater) {
2742         return new AbstractDiagramUpdater(DIAGRAM_UPDATE_DIAGRAM_PRIORITY, "updateDiagram") {
2743             @Override
2744             protected void forDiagram() {
2745                 if (DebugPolicy.DEBUG_DIAGRAM_UPDATE)
2746                     System.out.println("running diagram updater: " + this);
2747
2748                 // DiagramUtils.testDiagram(diagram);
2749                 Set<IElement> dirty = new HashSet<IElement>();
2750
2751                 Object task2 = Timing.BEGIN("Preprocess connection changes");
2752                 Map<ConnectionEntity, ConnectionChildren> connectionChangeData = new HashMap<ConnectionEntity, ConnectionChildren>(updater.changedConnectionEntities.size());
2753                 for (ConnectionData cd : updater.changedConnectionEntities.values()) {
2754                     connectionChangeData.put(cd.impl, cd.impl.getConnectionChildren());
2755                 }
2756                 Timing.END(task2);
2757
2758                 task2 = Timing.BEGIN("removeRouteGraphConnections");
2759                 for (IElement removedRouteGraphConnection : updater.removedRouteGraphConnections) {
2760                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2761                         System.out.println("removing route graph connection: " + removedRouteGraphConnection);
2762
2763                     ConnectionEntity ce = removedRouteGraphConnection.getHint(ElementHints.KEY_CONNECTION_ENTITY);
2764                     if (ce == null)
2765                         continue;
2766                     Object connectionData = ElementUtils.getObject(removedRouteGraphConnection);
2767                     tempConnections.clear();
2768                     for (Connection conn : ce.getTerminalConnections(tempConnections)) {
2769                         ((Element) conn.node).removeHintWithoutNotification(new TerminalKeyOf(conn.terminal, connectionData, Connection.class));
2770                         // To be sure the view will be up-to-date, mark the node
2771                         // connected to the removed connection dirty.
2772                         dirty.add(conn.node);
2773                     }
2774                 }
2775                 Timing.END(task2);
2776
2777                 task2 = Timing.BEGIN("removeBranchPoints");
2778                 for (IElement removed : updater.removedBranchPoints) {
2779                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2780                         System.out.println("removing branch point: " + removed);
2781
2782                     unmapElement(removed.getHint(ElementHints.KEY_OBJECT));
2783                     removeNodeTopologyHints((Element) removed);
2784
2785                     IElement connection = ElementUtils.getParent(removed);
2786                     if (connection != null) {
2787                         dirty.add(connection);
2788                     }
2789                 }
2790                 Timing.END(task2);
2791
2792                 task2 = Timing.BEGIN("removeConnectionSegments");
2793                 for (IElement removed : updater.removedConnectionSegments) {
2794                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2795                         System.out.println("removing segment: " + removed);
2796
2797                     unmapElement(removed.getHint(ElementHints.KEY_OBJECT));
2798                     removeEdgeTopologyHints((Element) removed);
2799
2800                     IElement connection = ElementUtils.getParent(removed);
2801                     if (connection != null) {
2802                         dirty.add(connection);
2803                     }
2804                 }
2805                 Timing.END(task2);
2806
2807                 task2 = Timing.BEGIN("removeElements");
2808                 for (IElement removed : updater.removedElements) {
2809                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2810                         System.out.println("removing element: " + removed);
2811
2812                     removed.setHint(KEY_REMOVE_RELATIONSHIPS, Boolean.TRUE);
2813                     if (diagram.containsElement(removed)) {
2814                         diagram.removeElement(removed);
2815                     }
2816                     unmapElement(removed.getHint(ElementHints.KEY_OBJECT));
2817                     removeNodeTopologyHints((Element) removed);
2818
2819                     // No use marking removed elements dirty.
2820                     dirty.remove(removed);
2821                 }
2822                 Timing.END(task2);
2823
2824                 // TODO: get rid of this
2825                 task2 = Timing.BEGIN("removeConnectionEntities");
2826                 for (Resource ce : updater.removedConnectionEntities) {
2827                     unmapConnection(ce);
2828                 }
2829                 Timing.END(task2);
2830
2831                 task2 = Timing.BEGIN("setConnectionData");
2832                 for (ConnectionData cd : updater.changedConnectionEntities.values()) {
2833                     cd.impl.setData(cd.segments, cd.branchPoints);
2834                 }
2835                 Timing.END(task2);
2836
2837                 // TODO: get rid of this
2838                 task2 = Timing.BEGIN("addConnectionEntities");
2839                 for (Map.Entry<Resource, ConnectionEntityImpl> entry : updater.addedConnectionEntities
2840                         .entrySet()) {
2841                     mapConnection(entry.getKey(), entry.getValue());
2842                 }
2843                 Timing.END(task2);
2844
2845                 task2 = Timing.BEGIN("addBranchPoints");
2846                 for (IElement added : updater.addedBranchPoints) {
2847                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2848                         System.out.println("adding branch point: " + added);
2849
2850                     mapElement(ElementUtils.getObject(added), added);
2851
2852                     IElement connection = ElementUtils.getParent(added);
2853                     if (connection != null) {
2854                         dirty.add(connection);
2855                     }
2856                 }
2857                 Timing.END(task2);
2858
2859                 // Add new elements at end of diagram, element order will be synchronized later.
2860                 task2 = Timing.BEGIN("addElements");
2861                 for(Resource r : updater.content.elements) {
2862                     IElement added = updater.addedElementMap.get(r);
2863                     if(added != null) {
2864                         if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2865                             System.out.println("adding element: " + added);
2866
2867                         //Object task3 = BEGIN("mapElement " + added);
2868                         Object task3 = Timing.BEGIN("mapElement");
2869                         mapElement(added.getHint(ElementHints.KEY_OBJECT), added);
2870                         Timing.END(task3);
2871
2872                         //task3 = BEGIN("addElement " + added);
2873                         task3 = Timing.BEGIN("addElement");
2874                         //System.out.println("diagram.addElement: " + added + " - " + diagram);
2875                         diagram.addElement(added);
2876                         dirty.add(added);
2877                         Timing.END(task3);
2878                     }
2879                 }
2880                 Timing.END(task2);
2881
2882                 // We've ensured that all nodes must have been and
2883                 // mapped before reaching this.
2884                 task2 = Timing.BEGIN("addConnectionSegments");
2885                 for (IElement added : updater.addedConnectionSegments) {
2886                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2887                         System.out.println("adding segment: " + added);
2888
2889                     PlaceholderConnection cb = added.removeHint(GraphToDiagramSynchronizer.KEY_CONNECTION_BEGIN_PLACEHOLDER);
2890                     PlaceholderConnection ce = added.removeHint(GraphToDiagramSynchronizer.KEY_CONNECTION_END_PLACEHOLDER);
2891                     if (cb == null || ce == null) {
2892                         if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2893                             warning("ignoring connection segment " + added + ", connectivity was not resolved (begin=" + cb + ", end=" + ce +")", null);
2894                         continue;
2895                     }
2896
2897                     mapElement(ElementUtils.getObject(added), added);
2898
2899                     IElement beginNode = assertMappedElement(cb.node);
2900                     IElement endNode = assertMappedElement(ce.node);
2901
2902                     if (cb != null)
2903                         connect(added, cb.end, beginNode, cb.terminal);
2904                     if (ce != null)
2905                         connect(added, ce.end, endNode, ce.terminal);
2906
2907                     IElement connection = ElementUtils.getParent(added);
2908                     if (connection != null) {
2909                         dirty.add(connection);
2910                     }
2911                 }
2912                 Timing.END(task2);
2913
2914                 // We've ensured that all nodes must have been and
2915                 // mapped before reaching this.
2916
2917                 task2 = Timing.BEGIN("handle dirty RouteGraph connections");
2918                 for (IElement addedRouteGraphConnection : updater.addedRouteGraphConnectionMap.values()) {
2919                     updateDirtyRouteGraphConnection(addedRouteGraphConnection, dirty);
2920                 }
2921                 Timing.END(task2);
2922
2923                 // Prevent memory leaks
2924                 tempConnections.clear();
2925
2926                 // Make sure that the diagram element order matches that of the database.
2927                 final TObjectIntHashMap<IElement> orderMap = new TObjectIntHashMap<IElement>(2 * updater.content.elements.size());
2928                 int i = 1;
2929                 for (Resource r : updater.content.elements) {
2930                     IElement e = getMappedElement(r);
2931                     if (e != null)
2932                         orderMap.put(e, i);
2933                     ++i;
2934                 }
2935                 diagram.sort(new Comparator<IElement>() {
2936                     @Override
2937                     public int compare(IElement e1, IElement e2) {
2938                         int o1 = orderMap.get(e1);
2939                         int o2 = orderMap.get(e2);
2940                         return o1 - o2;
2941                     }
2942                 });
2943
2944                 // TODO: consider removing this. The whole thing should work without it and
2945                 // this "fix" will only be hiding the real problems.
2946                 task2 = Timing.BEGIN("fixChangedConnections");
2947                 for (ConnectionData cd : updater.changedConnectionEntities.values()) {
2948                     cd.impl.fix();
2949                 }
2950                 Timing.END(task2);
2951
2952                 task2 = Timing.BEGIN("validateAndFix");
2953                 DiagramUtils.validateAndFix(diagram, dirty);
2954                 Timing.END(task2);
2955
2956                 // This will fire connection entity change listeners
2957                 task2 = Timing.BEGIN("Postprocess connection changes");
2958                 for (ConnectionData cd : updater.changedConnectionEntities.values()) {
2959                     ConnectionChildren oldChildren = connectionChangeData.get(cd.impl);
2960                     if (oldChildren != null) {
2961                         ConnectionChildren currentChildren = cd.impl.getConnectionChildren();
2962                         cd.impl.fireListener(oldChildren, currentChildren);
2963                     }
2964                 }
2965                 Timing.END(task2);
2966
2967                 task2 = Timing.BEGIN("setDirty");
2968                 for (IElement e : dirty) {
2969                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2970                         System.out.println("MARKING ELEMENT DIRTY: " + e);
2971                     e.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);
2972                 }
2973                 Timing.END(task2);
2974
2975                 // Signal about possible changes in the z-order of diagram elements.
2976                 if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2977                     System.out.println("MARKING DIAGRAM DIRTY: " + diagram);
2978                 diagram.setHint(Hints.KEY_DIRTY, Hints.VALUE_Z_ORDER_CHANGED);
2979
2980                 // Mark the updater as "processed".
2981                 updater.clear();
2982
2983                 // Inform listeners that the diagram has been updated.
2984                 diagram.setHint(DiagramModelHints.KEY_DIAGRAM_CONTENTS_UPDATED, Boolean.TRUE);
2985             }
2986         };
2987     }
2988
2989     /**
2990      * @param connection
2991      * @param dirtySet
2992      */
2993     private void updateDirtyRouteGraphConnection(IElement connection, Set<IElement> dirtySet) {
2994         if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
2995             System.out.println("updating dirty route graph connection: " + connection);
2996
2997         ConnectionEntity ce = connection.getHint(ElementHints.KEY_CONNECTION_ENTITY);
2998         if (ce == null)
2999             return;
3000
3001         tempConnections.clear();
3002         Object connectionData = ElementUtils.getObject(connection);
3003         for (Connection conn : ce.getTerminalConnections(tempConnections)) {
3004             ((Element) conn.node).setHintWithoutNotification(
3005                     new TerminalKeyOf(conn.terminal, connectionData, Connection.class),
3006                     conn);
3007             if (dirtySet != null)
3008                 dirtySet.add(conn.node);
3009         }
3010
3011         // Prevent memory leaks.
3012         tempConnections.clear();
3013     }
3014
3015     abstract class ElementUpdater extends AbstractDiagramUpdater {
3016         private final IElement newElement;
3017
3018         public ElementUpdater(Double priority, String runnerName, IElement newElement) {
3019             super(priority, runnerName);
3020             if (newElement == null)
3021                 throw new NullPointerException("null element");
3022             this.newElement = newElement;
3023         }
3024
3025         @Override
3026         public String toString() {
3027             return super.toString() + "[" + newElement + "]";
3028         }
3029
3030         @Override
3031         public void run() {
3032 //            System.out.println("ElementUpdateRunner new=" + newElement);
3033             Object elementResource = newElement.getHint(ElementHints.KEY_OBJECT);
3034 //            System.out.println("ElementUpdateRunner res=" + elementResource);
3035             final Element mappedElement = (Element) getMappedElement(elementResource);
3036 //            System.out.println("ElementUpdateRunner mapped=" + mappedElement);
3037             if (mappedElement == null) {
3038                 if (DebugPolicy.DEBUG_ELEMENT_LIFECYCLE) {
3039                     System.out.println("SKIP DIAGRAM UPDATE " + this  + " for element resource " + elementResource + ", no mapped element (newElement=" + newElement + ")");
3040                 }
3041                 // Indicates the element has been removed from the graph.
3042                 return;
3043             }
3044
3045             Object task = Timing.BEGIN(runnerName);
3046             forMappedElement(mappedElement);
3047             Timing.END(task);
3048         }
3049
3050         protected abstract void forMappedElement(Element mappedElement);
3051     }
3052
3053     ElementUpdater nodeUpdater(final Resource resource, final IElement newElement) {
3054
3055         return new ElementUpdater(DIAGRAM_UPDATE_NODE_PRIORITY, "updateNode", newElement) {
3056
3057             Collection<Terminal> getTerminals(IElement e) {
3058                 Collection<Terminal> ts = Collections.emptyList();
3059                 TerminalTopology tt = e.getElementClass().getAtMostOneItemOfClass(TerminalTopology.class);
3060                 if (tt != null) {
3061                     ts = new ArrayList<Terminal>();
3062                     tt.getTerminals(newElement, ts);
3063                 }
3064                 return ts;
3065             }
3066
3067             @Override
3068             protected void forMappedElement(final Element mappedElement) {
3069                 if (DebugPolicy.DEBUG_NODE_UPDATE)
3070                     System.out.println("running node updater: " + this + " - new element: " + newElement);
3071
3072                 // Newly loaded node elements NEVER contain topology-related
3073                 // hints, i.e. TerminalKeyOf hints. Instead all connections are
3074                 // actually set into element hints when connection edges are
3075                 // loaded.
3076
3077                 Collection<Terminal> oldTerminals = getTerminals(mappedElement);
3078                 Collection<Terminal> newTerminals = getTerminals(newElement);
3079                 if (!oldTerminals.equals(newTerminals)) {
3080                     // Okay, there are differences in the terminals. Need to fix
3081                     // the TerminalKeyOf hint values to use the new terminal
3082                     // instances when correspondences exist.
3083
3084                     // If there is no correspondence for an old terminal, we
3085                     // are simply forced to remove the hints related to this
3086                     // connection.
3087
3088                     Map<Terminal, Terminal> newTerminalMap = new HashMap<Terminal, Terminal>(newTerminals.size());
3089                     for (Terminal t : newTerminals) {
3090                         newTerminalMap.put(t, t);
3091                     }
3092
3093                     for (Map.Entry<TerminalKeyOf, Object> entry : mappedElement.getHintsOfClass(TerminalKeyOf.class).entrySet()) {
3094                         TerminalKeyOf key = entry.getKey();
3095                         Connection c = (Connection) entry.getValue();
3096                         if (c.node == mappedElement) {
3097                             Terminal newTerminal = newTerminalMap.get(c.terminal);
3098                             if (newTerminal != null) {
3099                                 c = new Connection(c.edge, c.end, c.node, newTerminal);
3100                                 ((Element) c.edge).setHintWithoutNotification(EndKeyOf.get(c.end), c);
3101                             } else {
3102                                 mappedElement.removeHintWithoutNotification(key);
3103                             }
3104                         }
3105                     }
3106                 }
3107
3108                 updateMappedElement(mappedElement, newElement);
3109                 mappedElement.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);
3110             }
3111         };
3112     }
3113
3114     ElementUpdater connectionUpdater(final Object data, final IElement newElement) {
3115         return new ElementUpdater(DIAGRAM_UPDATE_CONNECTION_PRIORITY, "updateConnection", newElement) {
3116             @Override
3117             public void forMappedElement(Element mappedElement) {
3118                 if (DebugPolicy.DEBUG_CONNECTION_UPDATE)
3119                     System.out.println("running connection updater: " + this + " - new element: " + newElement
3120                             + " with data " + data);
3121
3122                 // This is kept up-to-date by GDS, make sure not to overwrite it
3123                 // from the mapped element.
3124                 newElement.removeHint(ElementHints.KEY_CONNECTION_ENTITY);
3125
3126                 updateMappedElement(mappedElement, newElement);
3127                 mappedElement.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);
3128             }
3129         };
3130     }
3131
3132     ElementUpdater edgeUpdater(final Object data, final IElement newElement) {
3133         return new ElementUpdater(DIAGRAM_UPDATE_EDGE_PRIORITY, "updateEdge", newElement) {
3134             @Override
3135             public void forMappedElement(Element mappedElement) {
3136                 if (DebugPolicy.DEBUG_EDGE_UPDATE)
3137                     System.out.println("running edge updater: " + this + " - new element: " + newElement
3138                             + " with data " + data);
3139
3140                 updateMappedElement(mappedElement, newElement);
3141                 mappedElement.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);
3142             }
3143         };
3144     }
3145
3146     ElementUpdater routeGraphConnectionUpdater(final Object data, final IElement newElement, final Set<Object> dirtyNodes) {
3147         return new ElementUpdater(DIAGRAM_UPDATE_CONNECTION_PRIORITY, "updateRouteGraphConnection", newElement) {
3148             @Override
3149             public void forMappedElement(Element mappedElement) {
3150                 if (DebugPolicy.DEBUG_CONNECTION_UPDATE)
3151                     System.out.println("running route graph connection updater: " + this + " - new element: " + newElement
3152                             + " with data " + data);
3153
3154                 // Remove all TerminalKeyOf hints from nodes that were
3155                 // previously connected to the connection (mappedElement)
3156                 // before updating mappedElement with new topology information.
3157
3158                 for (Object dirtyNodeObject : dirtyNodes) {
3159                     Element dirtyNode = (Element) getMappedElement(dirtyNodeObject);
3160                     if (dirtyNode == null)
3161                         continue;
3162                     if (DebugPolicy.DEBUG_DIAGRAM_UPDATE_DETAIL)
3163                         System.out.println("preparing node with dirty connectivity: " + dirtyNode);
3164
3165                     for (Map.Entry<TerminalKeyOf, Object> entry : dirtyNode.getHintsOfClass(TerminalKeyOf.class).entrySet()) {
3166                         Connection conn = (Connection) entry.getValue();
3167                         Object connectionNode = conn.edge.getHint(ElementHints.KEY_OBJECT);
3168                         if (data.equals(connectionNode)) {
3169                             dirtyNode.removeHintWithoutNotification(entry.getKey());
3170                         }
3171                     }
3172                 }
3173
3174                 // Update connection information, including topology
3175                 updateMappedElement(mappedElement, newElement);
3176
3177                 // Reinstall TerminalKeyOf hints into nodes that are now connected
3178                 // to mappedElement to keep diagram run-time model properly in sync
3179                 // with the database.
3180                 updateDirtyRouteGraphConnection(mappedElement, null);
3181
3182                 // TODO: should mark dirty nodes' scene graph dirty ?
3183
3184                 mappedElement.setHint(Hints.KEY_DIRTY, Hints.VALUE_SG_DIRTY);
3185             }
3186         };
3187     }
3188
3189     /**
3190      * Copies hints from <code>newElement</code> to <code>mappedElement</code>
3191      * asserting some validity conditions at the same time.
3192      * 
3193      * @param mappedElement
3194      * @param newElement
3195      */
3196     static void updateMappedElement(Element mappedElement, IElement newElement) {
3197         if (mappedElement == newElement)
3198             // Can't update anything if the two elements are the same.
3199             return;
3200
3201         ElementClass oldClass = mappedElement.getElementClass();
3202         ElementClass newClass = newElement.getElementClass();
3203
3204         Object mappedData = mappedElement.getHint(ElementHints.KEY_OBJECT);
3205         Object newData = newElement.getHint(ElementHints.KEY_OBJECT);
3206
3207         assert mappedData != null;
3208         assert newData != null;
3209         assert mappedData.equals(newData);
3210
3211         if (DebugPolicy.DEBUG_GENERAL_ELEMENT_UPDATE) {
3212             System.out.println("Updating mapped element, setting hints\n  from: " + newElement + "\n  into: " + mappedElement);
3213         }
3214
3215         // TODO: consider if this equals check is a waste of time or does it pay
3216         // off due to having to reinitialize per-class caches for the new
3217         // ElementClass that are constructed on the fly?
3218         if (!newClass.equals(oldClass)) {
3219             if (DebugPolicy.DEBUG_GENERAL_ELEMENT_UPDATE) {
3220                 System.out.println("  old element class: " + oldClass);
3221                 System.out.println("  new element class: " + newClass);
3222             }
3223             mappedElement.setElementClass(newClass);
3224         }
3225
3226         // Tuukka@2010-02-19: replaced with notifications for making
3227         // the graph synchronizer more transparent to the client.
3228
3229         // Hint notifications will not work when this is used.
3230         //mappedElement.setHintsWithoutNotification(newElement.getHints());
3231
3232         Map<DiscardableKey, Object> discardableHints = mappedElement.getHintsOfClass(DiscardableKey.class);
3233
3234         // Set all hints from newElement to mappedElement.
3235         // Leave any hints in mappedElement but not in newElement as is.
3236         Map<Key, Object> hints = newElement.getHints();
3237         Map<Key, Object> newHints = new HashMap<Key, Object>();
3238         for (Map.Entry<Key, Object> entry : hints.entrySet()) {
3239             Key key = entry.getKey();
3240             Object newValue = entry.getValue();
3241             Object oldValue = mappedElement.getHint(key);
3242             if (DebugPolicy.DEBUG_GENERAL_ELEMENT_UPDATE_DETAIL) {
3243                 System.out.println("  hint " + key + " compare values: " + oldValue + "  ->  " + newValue);
3244             }
3245             if (!newValue.equals(oldValue)) {
3246                 newHints.put(key, newValue);
3247                 if (DebugPolicy.DEBUG_GENERAL_ELEMENT_UPDATE) {
3248                     System.out.format("    %-42s : %64s -> %-64s\n", key, oldValue, newValue);
3249                 }
3250             } else {
3251                 // If the hint value has not changed but the hint still exists
3252                 // we don't need to discard it even if it is considered
3253                 // discardable.
3254                 discardableHints.remove(key);
3255             }
3256         }
3257
3258         // Set all hints at once and send notifications after setting the values.
3259         if (!discardableHints.isEmpty()) {
3260             if (DebugPolicy.DEBUG_GENERAL_ELEMENT_UPDATE)
3261                 System.out.println("Discarding " + discardableHints.size() + " discardable hints:\n  " + discardableHints);
3262             mappedElement.removeHints(discardableHints.keySet());
3263         }
3264         if (!newHints.isEmpty()) {
3265             if (DebugPolicy.DEBUG_GENERAL_ELEMENT_UPDATE) {
3266                 System.out.println("Updating mapped element, setting new hints:\n\t"
3267                         + EString.implode(newHints.entrySet(), "\n\t") + "\nto replace old hints\n\t"
3268                         + EString.implode(mappedElement.getHints().entrySet(), "\n\t"));
3269             }
3270             mappedElement.setHints(newHints);
3271         }
3272         if (DebugPolicy.DEBUG_GENERAL_ELEMENT_UPDATE) {
3273             System.out.println("All hints after update:\n\t"
3274                     + EString.implode(mappedElement.getHints().entrySet(), "\n\t"));
3275         }
3276     }
3277
3278     class TransactionListener extends SessionEventListenerAdapter {
3279         long startTime;
3280         @Override
3281         public void writeTransactionStarted() {
3282             startTime = System.nanoTime();
3283             if (DebugPolicy.DEBUG_WRITE_TRANSACTIONS)
3284                 System.out.println(GraphToDiagramSynchronizer.class.getSimpleName() + ".sessionEventListener.writeTransactionStarted");
3285             inWriteTransaction.set(true);
3286         }
3287         @Override
3288         public void writeTransactionFinished() {
3289             long endTime = System.nanoTime();
3290             if (DebugPolicy.DEBUG_WRITE_TRANSACTIONS)
3291                 System.out.println(GraphToDiagramSynchronizer.class.getSimpleName() + ".sessionEventListener.writeTransactionFinished: " + (endTime - startTime)*1e-6 + " ms");
3292             inWriteTransaction.set(false);
3293             scheduleGraphUpdates();
3294         }
3295     };
3296
3297     Object                   graphUpdateLock             = new Object();
3298     TransactionListener      sessionListener             = null;
3299     AtomicBoolean            inWriteTransaction          = new AtomicBoolean(false);
3300     AtomicBoolean            graphUpdateRequestScheduled = new AtomicBoolean(false);
3301     List<GraphUpdateReactor> queuedGraphUpdates          = new ArrayList<GraphUpdateReactor>();
3302
3303     private void offerGraphUpdate(GraphUpdateReactor update) {
3304         if (DebugPolicy.DEBUG_GRAPH_UPDATE)
3305             System.out.println("offerGraphUpdate: " + update);
3306         boolean inWrite = inWriteTransaction.get();
3307         synchronized (graphUpdateLock) {
3308             if (DebugPolicy.DEBUG_GRAPH_UPDATE)
3309                 System.out.println("queueing graph update: " + update);
3310             queuedGraphUpdates.add(update);
3311         }
3312         if (!inWrite) {
3313             if (DebugPolicy.DEBUG_GRAPH_UPDATE)
3314                 System.out.println("scheduling queued graph update immediately: " + update);
3315             scheduleGraphUpdates();
3316         }
3317     }
3318
3319     private Collection<GraphUpdateReactor> scrubGraphUpdates() {
3320         synchronized (graphUpdateLock) {
3321             if (queuedGraphUpdates.isEmpty())
3322                 return Collections.emptyList();
3323             final List<GraphUpdateReactor> updates = queuedGraphUpdates;
3324             queuedGraphUpdates = new ArrayList<GraphUpdateReactor>();
3325             return updates;
3326         }
3327     }
3328
3329     private void scheduleGraphUpdates() {
3330         synchronized (graphUpdateLock) {
3331             if (queuedGraphUpdates.isEmpty())
3332                 return;
3333             if (!graphUpdateRequestScheduled.compareAndSet(false, true))
3334                 return;
3335         }
3336
3337         if (DebugPolicy.DEBUG_GRAPH_UPDATE)
3338             System.out.println("scheduling " + queuedGraphUpdates.size() + " queued graph updates with ");
3339
3340         session.asyncRequest(new ReadRequest() {
3341             @Override
3342             public void run(final ReadGraph graph) throws DatabaseException {
3343                 Collection<GraphUpdateReactor> updates;
3344                 synchronized (graphUpdateLock) {
3345                     graphUpdateRequestScheduled.set(false);
3346                     updates = scrubGraphUpdates();
3347                 }
3348
3349                 if (!GraphToDiagramSynchronizer.this.isAlive())
3350                     return;
3351
3352                 processGraphUpdates(graph, updates);
3353             }
3354         }, new ProcedureAdapter<Object>() {
3355             @Override
3356             public void exception(Throwable t) {
3357                 error(t);
3358             }
3359         });
3360     }
3361
3362     private void processGraphUpdates(ReadGraph graph, final Collection<GraphUpdateReactor> graphUpdates)
3363     throws DatabaseException {
3364         final List<DiagramUpdater> diagramUpdates = new ArrayList<DiagramUpdater>(graphUpdates.size());
3365
3366         // Run GraphUpdaters and gather DiagramUpdaters.
3367         if (DebugPolicy.DEBUG_GRAPH_UPDATE)
3368             System.out.println("Running GRAPH updates: " + graphUpdates);
3369         for (GraphUpdateReactor graphUpdate : graphUpdates) {
3370             DiagramUpdater diagramUpdate = graphUpdate.graphUpdate(graph);
3371             if (diagramUpdate != null) {
3372                 if (DebugPolicy.DEBUG_GRAPH_UPDATE)
3373                     System.out.println(graphUpdate + " => " + diagramUpdate);
3374                 diagramUpdates.add(diagramUpdate);
3375             }
3376         }
3377
3378         if (diagramUpdates.isEmpty())
3379             return;
3380
3381         if (DebugPolicy.DEBUG_DIAGRAM_UPDATE)
3382             System.out.println("Diagram updates: " + diagramUpdates);
3383         Collections.sort(diagramUpdates, DiagramUpdater.DIAGRAM_UPDATER_COMPARATOR);
3384         if (DebugPolicy.DEBUG_DIAGRAM_UPDATE)
3385             System.out.println("Sorted diagram updates: " + diagramUpdates);
3386
3387         ThreadUtils.asyncExec(canvas.getThreadAccess(), new StateRunnable() {
3388             @Override
3389             public void run() {
3390                 if (GraphToDiagramSynchronizer.this.isAlive() && getState() != State.DISPOSED)
3391                     safeRunInState(State.UPDATING_DIAGRAM, this);
3392             }
3393
3394             @Override
3395             public void execute() throws InvocationTargetException {
3396                 // Block out diagram write transactions.
3397                 DiagramUtils.inDiagramTransaction(diagram, TransactionType.READ, new Runnable() {
3398                     @Override
3399                     public void run() {
3400                         if (DebugPolicy.DEBUG_DIAGRAM_UPDATE)
3401                             System.out.println("Running DIAGRAM updates: " + diagramUpdates);
3402                         for (DiagramUpdater update : diagramUpdates) {
3403                             if (DebugPolicy.DEBUG_DIAGRAM_UPDATE)
3404                                 System.out.println("Running DIAGRAM update: " + update);
3405                             update.run();
3406                         }
3407                     }
3408                 });
3409
3410                 setCanvasDirty();
3411             }
3412         });
3413     }
3414
3415     private void attachSessionListener(Session session) {
3416         SessionEventSupport support = session.peekService(SessionEventSupport.class);
3417         if (support != null) {
3418             sessionListener = new TransactionListener();
3419             support.addListener(sessionListener);
3420         }
3421     }
3422
3423     private void detachSessionListener() {
3424         if (sessionListener != null) {
3425             session.getService(SessionEventSupport.class).removeListener(sessionListener);
3426             sessionListener = null;
3427         }
3428     }
3429
3430
3431     // ------------------------------------------------------------------------
3432     // GRAPH TO DIAGRAM SYNCHRONIZATION LOGIC END
3433     // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3434
3435     // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3436     // DIAGRAM CHANGE TRACKING, MAINLY VALIDATION PURPOSES.
3437     // This does not try to synchronize anything back into the graph.
3438     // ------------------------------------------------------------------------
3439
3440     IHintListener elementHintValidator = new HintListenerAdapter() {
3441         @Override
3442         public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
3443             if (!(sender instanceof Element))
3444                 throw new IllegalStateException("invalid sender: " + sender);
3445             Element e = (Element) sender;
3446             if (newValue != null) {
3447                 if (key instanceof TerminalKeyOf) {
3448                     Connection c = (Connection) newValue;
3449                     if (e != c.node)
3450                         throw new IllegalStateException("TerminalKeyOf hint of node " + e + " refers to a different node " + c.node + ". Should be the same.");
3451                     Object edgeObject = ElementUtils.getObject(c.edge);
3452                     if (!(edgeObject instanceof EdgeResource))
3453                         throw new IllegalStateException("EndKeyOf hint of edge " + c.edge + " refers contains an invalid object: " + edgeObject);
3454                 } else if (key instanceof EndKeyOf) {
3455                     Connection c = (Connection) newValue;
3456                     if (e != c.edge)
3457                         throw new IllegalStateException("EndKeyOf hint of edge " + e + " refers to a different edge " + c.edge + ". Should be the same.");
3458                     Object edgeObject = ElementUtils.getObject(c.edge);
3459                     if (!(edgeObject instanceof EdgeResource))
3460                         throw new IllegalStateException("EndKeyOf hint of edge " + e + " refers contains an invalid object: " + edgeObject);
3461                 }
3462             }
3463         }
3464     };
3465
3466     class DiagramListener implements CompositionListener, CompositionVetoListener {
3467         @Override
3468         public boolean beforeElementAdded(IDiagram d, IElement e) {
3469             // Make sure that MutatedElements NEVER get added to the diagram.
3470             if (d == diagram) {
3471                 if (!(e instanceof Element)) {
3472                     // THIS IS NOT GOOD!
3473                     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"));
3474                     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.");
3475                     return false;
3476                 }
3477
3478                 // Perform sanity checks that might veto the element addition.
3479                 boolean pass = true;
3480
3481                 // Check that all elements added to the diagram are adaptable to Resource
3482                 ElementClass ec = e.getElementClass();
3483                 Resource resource = ElementUtils.adapt(ec, Resource.class);
3484                 if (resource == null) {
3485                     pass = false;
3486                     LOGGER.error("", new Exception("Attempted to add an element to the diagram that is not adaptable to Resource: " + e + ", class: " + ec));
3487                 }
3488
3489                 // Sanity check connection hints
3490                 for (Map.Entry<TerminalKeyOf, Object> entry : e.getHintsOfClass(TerminalKeyOf.class).entrySet()) {
3491                     Connection c = (Connection) entry.getValue();
3492                     Object edgeObject = ElementUtils.getObject(c.edge);
3493                     if (e != c.node) {
3494                         System.err.println("Invalid node in TerminalKeyOf hint: " + entry.getKey() + "=" + entry.getValue());
3495                         System.err.println("\tconnection.edge=" + c.edge);
3496                         System.err.println("\tconnection.node=" + c.node);
3497                         System.err.println("\tconnection.end=" + c.end);
3498                         System.err.println("\telement=" + e);
3499                         System.err.println("\telement class=" + e.getElementClass());
3500                         pass = false;
3501                     }
3502                     if (!(edgeObject instanceof EdgeResource)) {
3503                         System.err.println("Invalid object in TerminalKeyOf hint edge: " + entry.getKey() + "=" + entry.getValue());
3504                         System.err.println("\tconnection.edge=" + c.edge);
3505                         System.err.println("\tconnection.node=" + c.node);
3506                         System.err.println("\tconnection.end=" + c.end);
3507                         System.err.println("\telement=" + e);
3508                         System.err.println("\telement class=" + e.getElementClass());
3509                         pass = false;
3510                     }
3511                 }
3512
3513                 return pass;
3514             }
3515             return true;
3516         }
3517
3518         @Override
3519         public boolean beforeElementRemoved(IDiagram d, IElement e) {
3520             // Never veto diagram changes.
3521             return true;
3522         }
3523
3524         @Override
3525         public void onElementAdded(IDiagram d, IElement e) {
3526             if (DebugPolicy.DEBUG_ELEMENT_LIFECYCLE)
3527                 System.out.println("[" + d + "] element added: " + e);
3528
3529             if (USE_ELEMENT_VALIDATING_LISTENERS)
3530                 e.addHintListener(elementHintValidator);
3531         }
3532
3533         @Override
3534         public void onElementRemoved(IDiagram d, IElement e) {
3535             if (DebugPolicy.DEBUG_ELEMENT_LIFECYCLE)
3536                 System.out.println("[" + d + "] element removed: " + e);
3537
3538             if (USE_ELEMENT_VALIDATING_LISTENERS)
3539                 e.removeHintListener(elementHintValidator);
3540
3541             if (e.containsHint(KEY_REMOVE_RELATIONSHIPS))
3542                 relationshipHandler.denyAll(diagram, e);
3543         }
3544     }
3545
3546     DiagramListener diagramListener = new DiagramListener();
3547
3548     static void removeNodeTopologyHints(Element node) {
3549         Set<TerminalKeyOf> terminalKeys = node.getHintsOfClass(TerminalKeyOf.class).keySet();
3550         if (!terminalKeys.isEmpty()) {
3551             removeNodeTopologyHints(node, terminalKeys);
3552         }
3553     }
3554
3555     static void removeNodeTopologyHints(Element node, Collection<TerminalKeyOf> terminalKeys) {
3556         for (TerminalKeyOf key : terminalKeys) {
3557             Connection c = node.removeHintWithoutNotification(key);
3558             if (c != null) {
3559                 removeEdgeTopologyHints((Element) c.edge);
3560             }
3561         }
3562     }
3563
3564     static void removeEdgeTopologyHints(Element edge) {
3565         Object edgeData = edge.getHint(ElementHints.KEY_OBJECT);
3566         for (EndKeyOf key : EndKeyOf.KEYS) {
3567             Connection c = edge.removeHintWithoutNotification(key);
3568             if (c != null) {
3569                 ((Element) c.node).removeHintWithoutNotification(new TerminalKeyOf(c.terminal, edgeData, Connection.class));
3570             }
3571         }
3572     }
3573
3574     // ------------------------------------------------------------------------
3575     // DIAGRAM CHANGE TRACKING, MAINLY VALIDATION PURPOSES.
3576     // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3577
3578     // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3579     // BACKEND TO DIAGRAM LOAD/LISTEN LOGIC BEGIN
3580     // ------------------------------------------------------------------------
3581
3582     void adaptDiagramClass(AsyncReadGraph graph, Resource diagram, final AsyncProcedure<DiagramClass> procedure) {
3583         graph.forAdapted(diagram, DiagramClass.class, new AsyncProcedure<DiagramClass>() {
3584             @Override
3585             public void exception(AsyncReadGraph graph, Throwable throwable) {
3586                 procedure.exception(graph, throwable);
3587             }
3588
3589             @Override
3590             public void execute(AsyncReadGraph graph, DiagramClass dc) {
3591                 // To move TopologyImpl out of here, we need a separate
3592                 // DiagramClassFactory that takes a canvas context as an argument.
3593                 // DataElementMapImpl, ElementFactoryImpl and diagramLifeCycle can
3594                 // safely stay here.
3595                 procedure.execute(graph, dc.newClassWith(
3596                         // This handler takes care of the topology of the diagram model.
3597                         // It sets and fixes element hints related to describing the
3598                         // connectivity of elements.
3599                         diagramTopology,
3600                         // TODO: not quite sure whether this can prove itself useful or not.
3601                         elementFactory,
3602                         // This map provides a bidirectional mapping between
3603                         // IElement and back-end objects.
3604                         dataElementMap,
3605                         // This handler provides a facility to adapt an element class
3606                         // to work properly with a diagram synchronized using this
3607                         // GraphToDiagramSynchronizer.
3608                         substituteElementClass,
3609                         // These handlers provide a way to create simple identified
3610                         // uni- and bidirectional relationships between any diagram
3611                         // objects/elements.
3612                         relationshipHandler));
3613             }
3614         });
3615     }
3616
3617     static Connection connect(IElement edge, EdgeEnd end, IElement element, Terminal terminal) {
3618         Connection c = new Connection(edge, end, element, terminal);
3619
3620         Object edgeData = edge.getHint(ElementHints.KEY_OBJECT);
3621         if (DebugPolicy.DEBUG_CONNECTION) {
3622             System.out.println("[connect](edge=" + edge + ", edgeData=" + edgeData + ", end=" + end + ", element="
3623                     + element + ", terminal=" + terminal + ")");
3624         }
3625
3626         TerminalKeyOf key = new TerminalKeyOf(terminal, edgeData, Connection.class);
3627         element.setHint(key, c);
3628
3629         EndKeyOf key2 = EndKeyOf.get(end);
3630         edge.setHint(key2, c);
3631
3632         return c;
3633     }
3634
3635     static class ElementFactoryImpl implements ElementFactory {
3636         @Override
3637         public IElement spawnNew(ElementClass clazz) {
3638             IElement e = Element.spawnNew(clazz);
3639             return e;
3640         }
3641     }
3642
3643     ElementFactoryImpl elementFactory = new ElementFactoryImpl();
3644
3645     public static final Object FIRST_TIME = new Object() {
3646         @Override
3647         public String toString() {
3648             return "FIRST_TIME";
3649         }
3650     };
3651
3652
3653     /**
3654      * A base for all listeners of graph requests performed internally by
3655      * GraphToDiagramSynchronizer.
3656      *
3657      * @param <T> type of stored data element
3658      * @param <Result> query result type
3659      */
3660     abstract class BaseListener<T, Result> implements AsyncListener<Result> {
3661
3662         protected final T    data;
3663
3664         private Object       oldResult = FIRST_TIME;
3665
3666         protected boolean    disposed  = false;
3667
3668         final ICanvasContext canvas;
3669
3670         public BaseListener(T data) {
3671             this.canvas = GraphToDiagramSynchronizer.this.canvas;
3672             this.data = data;
3673         }
3674
3675         @Override
3676         public void exception(AsyncReadGraph graph, Throwable throwable) {
3677             // Exceptions are always expected to mean that the listener should
3678             // be considered disposed once a query fails.
3679             disposed = true;
3680         }
3681
3682         abstract void execute(AsyncReadGraph graph, Object oldResult, Object newResult);
3683
3684         @Override
3685         public void execute(AsyncReadGraph graph, Result result) {
3686             if (DebugPolicy.DEBUG_LISTENER_BASE)
3687                 System.out.println("BaseListener: " + result);
3688
3689             if (disposed) {
3690                 if (DebugPolicy.DEBUG_LISTENER_BASE)
3691                     System.out.println("BaseListener: execute invoked although listener is disposed!");
3692                 return;
3693             }
3694
3695             // A null result will permanently mark this listener disposed!
3696             if (result == null) {
3697                 disposed = true;
3698                 if (DebugPolicy.DEBUG_LISTENER_BASE)
3699                     System.out.println(this + " null result, listener marked disposed");
3700             }
3701
3702             if (oldResult == FIRST_TIME) {
3703                 oldResult = result;
3704                 if (DebugPolicy.DEBUG_LISTENER_BASE)
3705                     System.out.println(this + " first result computed: " + result);
3706             } else {
3707                 if (DebugPolicy.DEBUG_LISTENER_BASE)
3708                     System.out.println(this + " result changed from '" + oldResult + "' to '" + result + "'");
3709                 try {
3710                     execute(graph, oldResult, result);
3711                 } finally {
3712                     oldResult = result;
3713                 }
3714             }
3715         }
3716
3717         @Override
3718         public boolean isDisposed() {
3719             if (disposed)
3720                 return true;
3721
3722             boolean alive = isAlive();
3723             //System.out.println(getClass().getName() + ": isDisposed(" + resource.getResourceId() + "): canvas=" + canvas + ", isAlive=" + alive);
3724             if (!alive)
3725                 return true;
3726             // If a mapping no longer exists for this element, dispose of this
3727             // listener.
3728             //IElement e = getMappedElement(resource);
3729             //System.out.println(getClass().getName() + ": isDisposed(" + resource.getResourceId() + "): canvas=" + canvas + ", element=" + e);
3730             //return e == null;
3731             return false;
3732         }
3733
3734 //        @Override
3735 //        protected void finalize() throws Throwable {
3736 //            System.out.println("finalize listener: " + this);
3737 //            super.finalize();
3738 //        }
3739     }
3740
3741     class DiagramClassRequest extends BaseRequest2<Resource, DiagramClass> {
3742         public DiagramClassRequest(Resource resource) {
3743             super(GraphToDiagramSynchronizer.this.canvas, resource);
3744         }
3745
3746         @Override
3747         public void perform(AsyncReadGraph graph, AsyncProcedure<DiagramClass> procedure) {
3748             adaptDiagramClass(graph, data, procedure);
3749         }
3750     }
3751
3752     public class DiagramContentListener extends BaseListener<Resource, DiagramContents> {
3753
3754         public DiagramContentListener(Resource resource) {
3755             super(resource);
3756         }
3757
3758         @Override
3759         public void execute(final AsyncReadGraph graph, Object oldResult, Object newResult) {
3760             final DiagramContents newContent = (newResult == null) ? new DiagramContents()
3761             : (DiagramContents) newResult;
3762
3763             // diagramGraphUpdater is called synchronously during
3764             // loading. The first result will not get updated through
3765             // this listener but through loadDiagram.
3766
3767             if (DebugPolicy.DISABLE_DIAGRAM_UPDATES) {
3768                 System.out.println("Skipped diagram content update: " + newResult);
3769                 return;
3770             }
3771
3772             if (DebugPolicy.DEBUG_DIAGRAM_LISTENER)
3773                 System.out.println("diagram contents changed: " + oldResult + " => " + newResult);
3774
3775             offerGraphUpdate( diagramGraphUpdater(newContent) );
3776         }
3777
3778         @Override
3779         public boolean isDisposed() {
3780             return !isAlive();
3781         }
3782
3783         @Override
3784         public void exception(AsyncReadGraph graph, Throwable t) {
3785             super.exception(graph, t);
3786             error("DiagramContentRequest failed", t);
3787         }
3788     }
3789
3790     // ------------------------------------------------------------------------
3791     // BACKEND TO DIAGRAM LOAD/LISTEN LOGIC END
3792     // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3793
3794     // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3795     // GRAPH-CUSTOMIZED DIAGRAM TOPOLOGY HANDLER BEGIN
3796     // ------------------------------------------------------------------------
3797
3798     static class TopologyImpl implements Topology {
3799
3800         @Override
3801         public Connection getConnection(IElement edge, EdgeEnd end) {
3802             Key key = EndKeyOf.get(end);
3803             Connection c = edge.getHint(key);
3804             if (c == null)
3805                 return null;
3806             return c;
3807         }
3808
3809         @Override
3810         public void getConnections(IElement node, Terminal terminal, Collection<Connection> connections) {
3811 //            IDiagram d = ElementUtils.getDiagram(node);
3812             for (Map.Entry<TerminalKeyOf, Object> entry : node.getHintsOfClass(TerminalKeyOf.class).entrySet()) {
3813                 // First check that the terminal matches.
3814                 TerminalKeyOf key = entry.getKey();
3815                 if (!key.getTerminal().equals(terminal))
3816                     continue;
3817
3818                 Connection c = (Connection) entry.getValue();
3819                 if (c != null) {
3820                     connections.add(c);
3821                 }
3822             }
3823         }
3824
3825         @Override
3826         public void connect(IElement edge, EdgeEnd end, IElement node, Terminal terminal) {
3827             if (node != null && terminal != null)
3828                 GraphToDiagramSynchronizer.connect(edge, end, node, terminal);
3829
3830             if (DebugPolicy.DEBUG_CONNECTION) {
3831                 if (end == EdgeEnd.Begin)
3832                     System.out.println("Connection started from: " + edge + ", " + end + ", " + node + ", " + terminal);
3833                 else
3834                     System.out.println("Creating connection to: " + edge + ", " + end + ", " + node + ", " + terminal);
3835             }
3836         }
3837
3838         @Override
3839         public void disconnect(IElement edge, EdgeEnd end, IElement node, Terminal terminal) {
3840             EndKeyOf edgeKey = EndKeyOf.get(end);
3841             Connection c = edge.getHint(edgeKey);
3842             if (c == null)
3843                 throw new UnsupportedOperationException("cannot disconnect, no Connection in edge " + edge);
3844
3845             for (Map.Entry<TerminalKeyOf, Object> entry : node.getHintsOfClass(TerminalKeyOf.class).entrySet()) {
3846                 Connection cc = (Connection) entry.getValue();
3847                 if (c == cc) {
3848                     node.removeHint(entry.getKey());
3849                     edge.removeHint(edgeKey);
3850                     return;
3851                 }
3852             }
3853
3854             throw new UnsupportedOperationException("cannot disconnect, no connection between found between edge "
3855                     + edge + " and node " + node);
3856         }
3857     }
3858
3859     Topology            diagramTopology     = new TopologyImpl();
3860
3861     // ------------------------------------------------------------------------
3862     // GRAPH-CUSTOMIZED DIAGRAM TOPOLOGY HANDLER END
3863     // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
3864
3865     // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
3866     // DIAGRAM OBJECT RELATIONSHIP HANDLER BEGIN
3867     // ------------------------------------------------------------------------
3868
3869     RelationshipHandler relationshipHandler = new RelationshipHandler() {
3870
3871         AssociativeMap map = new AssociativeMap(Associativity.of(true, false, false));
3872
3873         Object getPossibleObjectOrElement(Object o) {
3874             if (o instanceof IElement) {
3875                 IElement e = (IElement) o;
3876                 Object oo = e.getHint(ElementHints.KEY_OBJECT);
3877                 return oo != null ? oo : e;
3878             }
3879             return o;
3880         }
3881
3882         IElement getElement(Object o) {
3883             if (o instanceof IElement)
3884                 return (IElement) o;
3885             return getMappedElement(o);
3886         }
3887
3888         @Override
3889         public void claim(IDiagram diagram, Object subject,
3890                 Relationship predicate, Object object) {
3891             Object sd = getPossibleObjectOrElement(subject);
3892             Object od = getPossibleObjectOrElement(object);
3893
3894             Collection<Tuple> ts = null;
3895             Relationship inverse = predicate.getInverse();
3896             if (inverse != null)
3897                 ts = Arrays.asList(new Tuple(sd, predicate, od), new Tuple(od, inverse, sd));
3898             else
3899                 ts = Collections.singletonList(new Tuple(sd, predicate, od));
3900
3901             synchronized (this) {
3902                 map.add(ts);
3903             }
3904
3905             if (DebugPolicy.DEBUG_RELATIONSHIP) {
3906                 new Exception().printStackTrace();
3907                 System.out.println("Claimed relationships:");
3908                 for (Tuple t : ts)
3909                     System.out.println("\t" + t);
3910             }
3911         }
3912
3913         private void doDeny(IDiagram diagram, Object subject,
3914                 Relationship predicate, Object object) {
3915             Object sd = getPossibleObjectOrElement(subject);
3916             Object od = getPossibleObjectOrElement(object);
3917             if (sd == subject || od == object) {
3918                 System.out
3919                 .println("WARNING: denying relationship '"
3920                         + predicate
3921                         + "' between diagram element(s), not back-end object(s): "
3922                         + sd + " -> " + od);
3923             }
3924
3925             Collection<Tuple> ts = null;
3926             Relationship inverse = predicate.getInverse();
3927             if (inverse != null)
3928                 ts = Arrays.asList(new Tuple(sd, predicate, od), new Tuple(od,
3929                         inverse, sd));
3930             else
3931                 ts = Collections.singleton(new Tuple(sd, predicate, od));
3932
3933             synchronized (this) {
3934                 map.remove(ts);
3935             }
3936
3937             if (DebugPolicy.DEBUG_RELATIONSHIP) {
3938                 new Exception().printStackTrace();
3939                 System.out.println("Denied relationships:");
3940                 for (Tuple t : ts)
3941                     System.out.println("\t" + t);
3942             }
3943         }
3944
3945         @Override
3946         public void deny(IDiagram diagram, Object subject,
3947                 Relationship predicate, Object object) {
3948             synchronized (this) {
3949                 doDeny(diagram, subject, predicate, object);
3950             }
3951         }
3952
3953         @Override
3954         public void deny(IDiagram diagram, Relation relation) {
3955             synchronized (this) {
3956                 doDeny(diagram, relation.getSubject(), relation
3957                         .getRelationship(), relation.getObject());
3958             }
3959         }
3960
3961         @Override
3962         public void denyAll(IDiagram diagram, Object element) {
3963             synchronized (this) {
3964                 for (Relation relation : getRelations(diagram, element, null)) {
3965                     doDeny(diagram, relation.getSubject(), relation
3966                             .getRelationship(), relation.getObject());
3967                 }
3968             }
3969         }
3970
3971         @Override
3972         public Collection<Relation> getRelations(IDiagram diagram,
3973                 Object element, Collection<Relation> result) {
3974             if (DebugPolicy.DEBUG_GET_RELATIONSHIP)
3975                 System.out.println("getRelations(" + element + ")");
3976             Object e = getPossibleObjectOrElement(element);
3977
3978             Collection<Tuple> tuples = null;
3979             synchronized (this) {
3980                 tuples = map.get(new Tuple(e, null, null), null);
3981             }
3982
3983             if (DebugPolicy.DEBUG_GET_RELATIONSHIP) {
3984                 System.out.println("Result size: " + tuples.size());
3985                 for (Tuple t : tuples)
3986                     System.out.println("\t" + t);
3987             }
3988
3989             if (tuples.isEmpty())
3990                 return Collections.emptyList();
3991             if (result == null)
3992                 result = new ArrayList<Relation>(tuples.size());
3993             for (Tuple t : tuples) {
3994                 Object obj = t.getField(2);
3995                 IElement el = getElement(obj);
3996                 Relationship r = (Relationship) t.getField(1);
3997                 result.add(new Relation(element, r, el != null ? el : obj));
3998             }
3999             return result;
4000         }
4001
4002     };
4003
4004     // ------------------------------------------------------------------------
4005     // DIAGRAM ELEMENT RELATIONSHIP HANDLER END
4006     // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
4007
4008 }