1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\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.sysdyn.ui.elements2.connections;
\r
14 import java.util.HashMap;
\r
15 import java.util.concurrent.ConcurrentSkipListMap;
\r
16 import java.util.concurrent.atomic.AtomicInteger;
\r
18 import org.simantics.databoard.Bindings;
\r
19 import org.simantics.db.AsyncReadGraph;
\r
20 import org.simantics.db.Resource;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.db.procedure.AsyncMultiProcedure;
\r
23 import org.simantics.db.procedure.AsyncProcedure;
\r
24 import org.simantics.diagram.adapter.ElementFactoryAdapter;
\r
25 import org.simantics.diagram.stubs.DiagramResource;
\r
26 import org.simantics.g2d.canvas.ICanvasContext;
\r
27 import org.simantics.g2d.diagram.DiagramHints;
\r
28 import org.simantics.g2d.diagram.IDiagram;
\r
29 import org.simantics.g2d.element.ElementClass;
\r
30 import org.simantics.g2d.element.IElement;
\r
31 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
\r
32 import org.simantics.layer0.Layer0;
\r
33 import org.simantics.sysdyn.ui.editor.routing.DependencyRouter;
\r
34 import org.simantics.utils.datastructures.Pair;
\r
37 * An element class for single connection entity elements. A connection entity
\r
38 * consists of connection edge segments and branch points as its children.
\r
40 * @author Tuukka Lehtonen
\r
42 public class DependencyConnectionFactory extends ElementFactoryAdapter {
\r
44 public static final ElementClass CLASS = SysdynConnectionClass.CLASS;
\r
47 public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, final AsyncProcedure<ElementClass> procedure) {
\r
48 DiagramResource dr = graph.getService(DiagramResource.class);
\r
49 graph.forSingleType(elementType, dr.Connection, new AsyncProcedure<Resource>() {
\r
51 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
52 procedure.exception(graph, throwable);
\r
55 public void execute(AsyncReadGraph graph, Resource connectionType) {
\r
56 procedure.execute(graph, SysdynConnectionClass.CLASS.newClassWith(false, new StaticObjectAdapter(connectionType)));
\r
62 public void load(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, final Resource elementResource,
\r
63 final IElement element, final AsyncProcedure<IElement> procedure) {
\r
65 final AtomicInteger ready = new AtomicInteger(1);
\r
66 final ConcurrentSkipListMap<String, Pair<Resource, Object>> properties = new ConcurrentSkipListMap<String, Pair<Resource, Object>>();
\r
68 element.setHint(DiagramHints.ROUTE_ALGORITHM, new DependencyRouter());
\r
70 graph.forEachPredicate(elementResource, new AsyncMultiProcedure<Resource>() {
\r
73 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
74 throwable.printStackTrace();
\r
78 public void execute(AsyncReadGraph graph, final Resource property) {
\r
80 ready.incrementAndGet();
\r
83 l0 = Layer0.getInstance(graph.getSession());
\r
84 } catch (DatabaseException e) {
\r
85 e.printStackTrace();
\r
88 graph.forIsSubrelationOf(property, l0.HasProperty, new AsyncProcedure<Boolean>() {
\r
91 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
92 throwable.printStackTrace();
\r
96 public void execute(AsyncReadGraph graph, final Boolean isProperty) {
\r
100 graph.forPossibleRelatedValue(elementResource, property, new AsyncProcedure<Object>() {
\r
103 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
104 throwable.printStackTrace();
\r
108 public void execute(AsyncReadGraph graph, final Object value) {
\r
112 l0 = Layer0.getInstance(graph.getSession());
\r
113 } catch (DatabaseException e) {
\r
114 e.printStackTrace();
\r
118 graph.forPossibleRelatedValue(property, l0.HasName, Bindings.STRING, new AsyncProcedure<String>() {
\r
121 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
122 throwable.printStackTrace();
\r
126 public void execute(AsyncReadGraph graph, String name) {
\r
128 properties.put(name, Pair.make(property, value));
\r
129 // System.out.println("load properties " + name + " => " + value);
\r
130 if(ready.decrementAndGet() == 0) {
\r
131 element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));
\r
132 procedure.execute(graph, element);
\r
146 if(ready.decrementAndGet() == 0) {
\r
147 element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));
\r
148 procedure.execute(graph, element);
\r
160 public void finished(AsyncReadGraph graph) {
\r
162 if(ready.decrementAndGet() == 0) {
\r
163 element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Object>(properties));
\r
164 procedure.execute(graph, element);
\r