1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.diagram.adapter;
\r
14 import org.simantics.db.AsyncReadGraph;
\r
15 import org.simantics.db.Resource;
\r
16 import org.simantics.db.common.procedure.guarded.GuardedAsyncProcedureWrapper;
\r
17 import org.simantics.db.procedure.AsyncProcedure;
\r
18 import org.simantics.diagram.stubs.DiagramResource;
\r
19 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
\r
20 import org.simantics.g2d.canvas.ICanvasContext;
\r
21 import org.simantics.g2d.diagram.DiagramHints;
\r
22 import org.simantics.g2d.diagram.IDiagram;
\r
23 import org.simantics.g2d.element.ElementClass;
\r
24 import org.simantics.g2d.element.IElement;
\r
25 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
\r
26 import org.simantics.g2d.elementclass.connection.ConnectionClass;
\r
27 import org.simantics.g2d.routing.IRouter2;
\r
28 import org.simantics.structural.stubs.StructuralResource2;
\r
31 * An element class for single connection entity elements. A connection entity
\r
32 * consists of connection edge segments and branch points as its children.
\r
34 * @author Tuukka Lehtonen
\r
36 public class DefaultConnectionClassFactory extends ElementFactoryAdapter {
\r
38 public static final ElementClass CLASS = ConnectionClass.CLASS;
\r
41 public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, final AsyncProcedure<ElementClass> procedure) {
\r
42 procedure.execute(graph, ConnectionClass.CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));
\r
46 protected Resource getElementClassBaseType(AsyncReadGraph graph) {
\r
47 return graph.getService(DiagramResource.class).Connection;
\r
51 public void load(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, final Resource elementResource,
\r
52 final IElement element, final AsyncProcedure<IElement> procedure) {
\r
53 final GuardedAsyncProcedureWrapper<IElement> guard = new GuardedAsyncProcedureWrapper<IElement>(procedure, 1);
\r
55 // Get custom routing algorithm for connection if necessary.
\r
56 DiagramGraphUtil.getPossibleRouter(graph, elementResource, new AsyncProcedure<IRouter2>() {
\r
58 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
59 guard.exception(graph, throwable);
\r
62 public void execute(AsyncReadGraph graph, IRouter2 router) {
\r
63 if (router != null) {
\r
64 element.setHint(DiagramHints.ROUTE_ALGORITHM, router);
\r
65 procedure.execute(graph, element);
\r
67 loadConnectionTypeDefaultRouting(graph, element, elementResource, guard);
\r
73 protected void loadConnectionTypeDefaultRouting(AsyncReadGraph graph, final IElement element, Resource elementResource, final AsyncProcedure<IElement> guard) {
\r
74 StructuralResource2 STR = graph.getService(StructuralResource2.class);
\r
75 graph.forPossibleObject(elementResource, STR.HasConnectionType, new AsyncProcedure<Resource>() {
\r
77 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
78 guard.exception(graph, throwable);
\r
81 public void execute(AsyncReadGraph graph, Resource connectionType) {
\r
82 DiagramResource DIA = graph.getService(DiagramResource.class);
\r
83 if (connectionType != null) {
\r
84 graph.forPossibleObject(connectionType, DIA.HasDefaultRouting, new AsyncProcedure<Resource>() {
\r
86 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
87 guard.exception(graph, throwable);
\r
90 public void execute(AsyncReadGraph graph, Resource routing) {
\r
91 if (routing != null) {
\r
92 graph.forPossibleAdapted(routing, IRouter2.class, new AsyncProcedure<IRouter2>() {
\r
94 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
95 guard.exception(graph, throwable);
\r
98 public void execute(AsyncReadGraph graph, IRouter2 router) {
\r
100 element.setHint(DiagramHints.ROUTE_ALGORITHM, router);
\r
101 guard.execute(graph, element);
\r
105 guard.execute(graph, element);
\r
110 guard.execute(graph, element);
\r