1 /*******************************************************************************
\r
2 * Copyright (c) 2010, 2012 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.elements.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.ReadGraph;
\r
21 import org.simantics.db.Resource;
\r
22 import org.simantics.db.exception.DatabaseException;
\r
23 import org.simantics.db.procedure.AsyncMultiProcedure;
\r
24 import org.simantics.db.procedure.AsyncProcedure;
\r
25 import org.simantics.db.procedure.SyncProcedure;
\r
26 import org.simantics.diagram.G2DUtils;
\r
27 import org.simantics.diagram.adapter.ElementFactoryAdapter;
\r
28 import org.simantics.diagram.stubs.DiagramResource;
\r
29 import org.simantics.diagram.stubs.G2DResource;
\r
30 import org.simantics.g2d.canvas.ICanvasContext;
\r
31 import org.simantics.g2d.diagram.DiagramHints;
\r
32 import org.simantics.g2d.diagram.IDiagram;
\r
33 import org.simantics.g2d.element.ElementClass;
\r
34 import org.simantics.g2d.element.ElementHints;
\r
35 import org.simantics.g2d.element.IElement;
\r
36 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
\r
37 import org.simantics.layer0.Layer0;
\r
38 import org.simantics.sysdyn.ui.editor.routing.DependencyRouter;
\r
39 import org.simantics.utils.datastructures.Pair;
\r
42 * An element class for single connection entity elements. A connection entity
\r
43 * consists of connection edge segments and branch points as its children.
\r
45 * @author Tuukka Lehtonen
\r
47 public class DependencyConnectionFactory extends ElementFactoryAdapter {
\r
49 public static final ElementClass CLASS = SysdynConnectionClass.CLASS;
\r
52 public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, final AsyncProcedure<ElementClass> procedure) {
\r
53 procedure.execute(graph, SysdynConnectionClass.CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));
\r
57 protected Resource getElementClassBaseType(AsyncReadGraph graph) {
\r
58 return graph.getService(DiagramResource.class).Connection;
\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, DependencyRouter.INSTANCE);
\r
72 G2D = G2DResource.getInstance(graph.getSession());
\r
73 } catch (DatabaseException e) {
\r
74 e.printStackTrace();
\r
78 // Find possible font
\r
79 graph.forPossibleObject(elementResource, G2D.HasFont, new SyncProcedure<Resource>() {
\r
82 public void execute(ReadGraph graph, Resource result) throws DatabaseException {
\r
83 if(result != null) {
\r
84 element.setHint(ElementHints.KEY_FONT, G2DUtils.getFont(graph, result));
\r
89 public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {
\r
90 throwable.printStackTrace();
\r
94 // Find possible color
\r
95 graph.forPossibleObject(elementResource, G2D.HasColor, new SyncProcedure<Resource>() {
\r
98 public void execute(ReadGraph graph, Resource result) throws DatabaseException {
\r
99 if(result != null) {
\r
100 element.setHint(ElementHints.KEY_TEXT_COLOR, G2DUtils.getColor(graph, result));
\r
105 public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {
\r
106 throwable.printStackTrace();
\r
111 // A complicated-looking procedure for obtaining all HasProperties to properties map
\r
112 graph.forEachPredicate(elementResource, new AsyncMultiProcedure<Resource>() {
\r
115 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
116 throwable.printStackTrace();
\r
120 public void execute(AsyncReadGraph graph, final Resource property) {
\r
122 ready.incrementAndGet();
\r
125 l0 = Layer0.getInstance(graph.getSession());
\r
126 } catch (DatabaseException e) {
\r
127 e.printStackTrace();
\r
131 graph.forIsSubrelationOf(property, l0.HasProperty, new AsyncProcedure<Boolean>() {
\r
134 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
135 throwable.printStackTrace();
\r
139 public void execute(AsyncReadGraph graph, final Boolean isProperty) {
\r
143 graph.forPossibleRelatedValue(elementResource, property, new AsyncProcedure<Object>() {
\r
146 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
147 throwable.printStackTrace();
\r
151 public void execute(AsyncReadGraph graph, final Object value) {
\r
155 l0 = Layer0.getInstance(graph.getSession());
\r
156 } catch (DatabaseException e) {
\r
157 e.printStackTrace();
\r
161 graph.forPossibleRelatedValue(property, l0.HasName, Bindings.STRING, new AsyncProcedure<String>() {
\r
164 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
165 throwable.printStackTrace();
\r
169 public void execute(AsyncReadGraph graph, String name) {
\r
171 properties.put(name, Pair.make(property, value));
\r
172 if(ready.decrementAndGet() == 0) {
\r
173 element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));
\r
174 procedure.execute(graph, element);
\r
188 if(ready.decrementAndGet() == 0) {
\r
189 element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));
\r
190 procedure.execute(graph, element);
\r
201 public void finished(AsyncReadGraph graph) {
\r
203 if(ready.decrementAndGet() == 0) {
\r
204 element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Object>(properties));
\r
205 procedure.execute(graph, element);
\r