]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/BranchPointClassFactory.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / BranchPointClassFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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
15 import org.simantics.db.AsyncReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.common.procedure.adapter.AsyncProcedureAdapter;
18 import org.simantics.db.common.procedure.guarded.GuardedAsyncProcedureWrapper;
19 import org.simantics.db.procedure.AsyncProcedure;
20 import org.simantics.diagram.stubs.DiagramResource;
21 import org.simantics.diagram.synchronization.SynchronizationHints;
22 import org.simantics.diagram.synchronization.graph.TransformSynchronizer;
23 import org.simantics.g2d.canvas.ICanvasContext;
24 import org.simantics.g2d.diagram.IDiagram;
25 import org.simantics.g2d.element.ElementClass;
26 import org.simantics.g2d.element.IElement;
27 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
28 import org.simantics.g2d.elementclass.BranchPoint;
29 import org.simantics.g2d.elementclass.BranchPoint.Direction;
30 import org.simantics.g2d.elementclass.BranchPointClass;
31
32 /**
33  * @author Tuukka Lehtonen
34  */
35 public class BranchPointClassFactory extends ElementFactoryAdapter {
36
37     public static ElementClass create(Resource elementClass) {
38         return BranchPointClass.CLASS.newClassWith(false, new StaticObjectAdapter(elementClass));
39     }
40
41     @Override
42     public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, AsyncProcedure<ElementClass> procedure) {
43         procedure.execute(graph, BranchPointClass.CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));
44     }
45
46     @Override
47     public void load(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, final Resource element, final IElement e, final AsyncProcedure<IElement> procedure) {
48         e.setHint(SynchronizationHints.HINT_SYNCHRONIZER, TransformSynchronizer.INSTANCE);
49
50         final AsyncProcedure<IElement> guard = new GuardedAsyncProcedureWrapper<IElement>(procedure, 2);
51
52         final BranchPoint bp = e.getElementClass().getAtMostOneItemOfClass(BranchPoint.class);
53         if (bp != null) {
54             final DiagramResource dr = graph.getService(DiagramResource.class);
55             graph.forHasStatement(element, dr.Horizontal, element, new AsyncProcedureAdapter<Boolean>() {
56                 @Override
57                 public void exception(AsyncReadGraph graph, Throwable throwable) {
58                     guard.exception(graph, throwable);
59                 }
60                 @Override
61                 public void execute(AsyncReadGraph graph, final Boolean horizontal) {
62                     graph.forHasStatement(element, dr.Vertical, element, new AsyncProcedure<Boolean>() {
63                         @Override
64                         public void exception(AsyncReadGraph graph, Throwable throwable) {
65                             guard.exception(graph, throwable);
66                         }
67                         @Override
68                         public void execute(AsyncReadGraph graph, Boolean vertical) {
69                             Direction direction = Direction.Any;
70                             if (horizontal ^ vertical)
71                                 direction = horizontal ? Direction.Horizontal : Direction.Vertical;
72                             bp.setDirectionPreference(e, direction);
73
74                             guard.execute(graph, e);
75                         }
76                     });
77                 }
78             });
79         }
80
81         ElementFactoryUtil.readTransform(graph, element, e, guard);
82     }
83
84 }