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.modeling.ModelingResources;
\r
34 import org.simantics.sysdyn.SysdynResource;
\r
35 import org.simantics.sysdyn.ui.editor.routing.DependencyRouter;
\r
36 import org.simantics.utils.datastructures.Pair;
\r
39 * An element class for single connection entity elements. A connection entity
\r
40 * consists of connection edge segments and branch points as its children.
\r
42 * @author Tuukka Lehtonen
\r
44 public class DependencyConnectionFactory extends ElementFactoryAdapter {
\r
46 public static final ElementClass CLASS = SysdynConnectionClass.CLASS;
\r
49 public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, final AsyncProcedure<ElementClass> procedure) {
\r
50 DiagramResource dr = graph.getService(DiagramResource.class);
\r
51 graph.forSingleType(elementType, dr.Connection, new AsyncProcedure<Resource>() {
\r
53 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
54 procedure.exception(graph, throwable);
\r
57 public void execute(AsyncReadGraph graph, Resource connectionType) {
\r
58 procedure.execute(graph, SysdynConnectionClass.CLASS.newClassWith(false, new StaticObjectAdapter(connectionType)));
\r
64 public void load(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, final Resource elementResource,
\r
65 final IElement element, final AsyncProcedure<IElement> procedure) {
\r
67 final AtomicInteger ready = new AtomicInteger(1);
\r
68 final ConcurrentSkipListMap<String, Pair<Resource, Object>> properties = new ConcurrentSkipListMap<String, Pair<Resource, Object>>();
\r
70 element.setHint(DiagramHints.ROUTE_ALGORITHM, new DependencyRouter());
\r
72 graph.forEachPredicate(elementResource, new AsyncMultiProcedure<Resource>() {
\r
75 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
76 throwable.printStackTrace();
\r
80 public void execute(AsyncReadGraph graph, final Resource property) {
\r
82 ready.incrementAndGet();
\r
84 ModelingResources mr;
\r
86 l0 = Layer0.getInstance(graph.getSession());
\r
87 mr = ModelingResources.getInstance(graph.getSession());
\r
88 } catch (DatabaseException e) {
\r
89 e.printStackTrace();
\r
92 graph.forIsSubrelationOf(property, l0.HasProperty, new AsyncProcedure<Boolean>() {
\r
95 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
96 throwable.printStackTrace();
\r
100 public void execute(AsyncReadGraph graph, final Boolean isProperty) {
\r
104 graph.forPossibleRelatedValue(elementResource, property, new AsyncProcedure<Object>() {
\r
107 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
108 throwable.printStackTrace();
\r
112 public void execute(AsyncReadGraph graph, final Object value) {
\r
116 l0 = Layer0.getInstance(graph.getSession());
\r
117 } catch (DatabaseException e) {
\r
118 e.printStackTrace();
\r
122 graph.forPossibleRelatedValue(property, l0.HasName, Bindings.STRING, new AsyncProcedure<String>() {
\r
125 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
126 throwable.printStackTrace();
\r
130 public void execute(AsyncReadGraph graph, String name) {
\r
132 properties.put(name, Pair.make(property, value));
\r
133 // System.out.println("load properties " + name + " => " + value);
\r
134 if(ready.decrementAndGet() == 0) {
\r
135 element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));
\r
136 procedure.execute(graph, element);
\r
150 if(ready.decrementAndGet() == 0) {
\r
151 element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));
\r
152 procedure.execute(graph, element);
\r
162 // graph.forPossibleObject(elementResource, mr.DiagramConnectionToConnection, new AsyncProcedure<Resource>() {
\r
165 // public void execute(AsyncReadGraph graph, Resource result) {
\r
166 // SysdynResource sr;
\r
168 // sr = SysdynResource.getInstance(graph.getSession());
\r
169 // } catch (DatabaseException e) {
\r
170 // e.printStackTrace();
\r
173 // graph.forPossibleRelatedValue(result, sr.Polarity, Bindings.STRING, new AsyncProcedure<String>() {
\r
176 // public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
177 // throwable.printStackTrace();
\r
181 // public void execute(AsyncReadGraph graph, String name) {
\r
183 // properties.put("polarity", Pair.make(property, (Object)name));
\r
184 //// System.out.println("load properties " + name + " => " + value);
\r
185 // if(ready.decrementAndGet() == 0) {
\r
186 // element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));
\r
187 // procedure.execute(graph, element);
\r
197 // public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
198 // throwable.printStackTrace();
\r
206 public void finished(AsyncReadGraph graph) {
\r
208 if(ready.decrementAndGet() == 0) {
\r
209 element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Object>(properties));
\r
210 procedure.execute(graph, element);
\r