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