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.awt.Color;
\r
15 import java.awt.Font;
\r
16 import java.util.HashMap;
\r
17 import java.util.concurrent.ConcurrentSkipListMap;
\r
18 import java.util.concurrent.atomic.AtomicInteger;
\r
20 import org.eclipse.jface.preference.PreferenceConverter;
\r
21 import org.eclipse.jface.resource.StringConverter;
\r
22 import org.eclipse.swt.graphics.FontData;
\r
23 import org.eclipse.swt.graphics.RGB;
\r
24 import org.simantics.databoard.Bindings;
\r
25 import org.simantics.db.AsyncReadGraph;
\r
26 import org.simantics.db.ReadGraph;
\r
27 import org.simantics.db.Resource;
\r
28 import org.simantics.db.Statement;
\r
29 import org.simantics.db.exception.DatabaseException;
\r
30 import org.simantics.db.procedure.AsyncMultiProcedure;
\r
31 import org.simantics.db.procedure.AsyncProcedure;
\r
32 import org.simantics.db.procedure.SyncProcedure;
\r
33 import org.simantics.diagram.G2DUtils;
\r
34 import org.simantics.diagram.adapter.ElementFactoryAdapter;
\r
35 import org.simantics.diagram.stubs.DiagramResource;
\r
36 import org.simantics.diagram.stubs.G2DResource;
\r
37 import org.simantics.g2d.canvas.ICanvasContext;
\r
38 import org.simantics.g2d.diagram.DiagramHints;
\r
39 import org.simantics.g2d.diagram.IDiagram;
\r
40 import org.simantics.g2d.element.ElementClass;
\r
41 import org.simantics.g2d.element.ElementHints;
\r
42 import org.simantics.g2d.element.IElement;
\r
43 import org.simantics.g2d.element.handler.impl.StaticObjectAdapter;
\r
44 import org.simantics.layer0.Layer0;
\r
45 import org.simantics.sysdyn.ui.editor.routing.DependencyRouter;
\r
46 import org.simantics.sysdyn.ui.preferences.SysdynDiagramPreferences;
\r
47 import org.simantics.sysdyn.ui.preferences.SysdynDiagramPropertyExternalRead;
\r
48 import org.simantics.utils.datastructures.Pair;
\r
51 * An element class for single connection entity elements. A connection entity
\r
52 * consists of connection edge segments and branch points as its children.
\r
54 * @author Tuukka Lehtonen
\r
56 public class DependencyConnectionFactory extends ElementFactoryAdapter {
\r
58 public static final ElementClass CLASS = SysdynConnectionClass.CLASS;
\r
61 public void create(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, Resource elementType, final AsyncProcedure<ElementClass> procedure) {
\r
62 procedure.execute(graph, SysdynConnectionClass.CLASS.newClassWith(false, new StaticObjectAdapter(elementType)));
\r
66 protected Resource getElementClassBaseType(AsyncReadGraph graph) {
\r
67 return graph.getService(DiagramResource.class).Connection;
\r
71 public void load(AsyncReadGraph graph, ICanvasContext canvas, IDiagram diagram, final Resource elementResource,
\r
72 final IElement element, final AsyncProcedure<IElement> procedure) {
\r
74 final AtomicInteger ready = new AtomicInteger(1);
\r
75 final ConcurrentSkipListMap<String, Pair<Resource, Object>> properties = new ConcurrentSkipListMap<String, Pair<Resource, Object>>();
\r
77 element.setHint(DiagramHints.ROUTE_ALGORITHM, DependencyRouter.INSTANCE);
\r
81 G2D = G2DResource.getInstance(graph.getSession());
\r
82 } catch (DatabaseException e) {
\r
83 e.printStackTrace();
\r
87 // Find possible font
\r
88 graph.forPossibleStatement(elementResource, G2D.HasFont, new SyncProcedure<Statement>() {
\r
91 public void execute(ReadGraph graph, Statement result) throws DatabaseException {
\r
92 if(result != null && !result.isAsserted(elementResource)) {
\r
93 element.setHint(ElementHints.KEY_FONT, G2DUtils.getFont(graph, result.getObject()));
\r
95 String fontdata = graph.syncRequest(new SysdynDiagramPropertyExternalRead(new Pair<Resource, String>(elementResource, SysdynDiagramPreferences.ARROW_FONT)));
\r
96 if(fontdata != null) {
\r
97 FontData[] fdArray = PreferenceConverter.basicGetFontData(fontdata);
\r
98 if(fdArray != null) {
\r
99 if(fdArray.length == 1) {
\r
100 FontData fd = fdArray[0];
\r
102 Font font = new Font(fd.getName(), fd.getStyle(), fd.getHeight());
\r
103 element.setHint(ElementHints.KEY_FONT, font);
\r
113 public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {
\r
114 throwable.printStackTrace();
\r
118 // Find possible color
\r
119 graph.forPossibleStatement(elementResource, G2D.HasColor, new SyncProcedure<Statement>() {
\r
122 public void execute(ReadGraph graph, Statement result) throws DatabaseException {
\r
123 if(result != null && !result.isAsserted(elementResource)) {
\r
124 element.setHint(ElementHints.KEY_TEXT_COLOR, G2DUtils.getColor(graph, result.getObject()));
\r
126 String color = graph.syncRequest(new SysdynDiagramPropertyExternalRead(new Pair<Resource, String>(elementResource, SysdynDiagramPreferences.ARROW_COLOR)));
\r
127 if(color != null) {
\r
128 RGB rgb = StringConverter.asRGB(color, null);
\r
130 Color c = new Color(rgb.red, rgb.green, rgb.blue);
\r
131 element.setHint(ElementHints.KEY_TEXT_COLOR, c);
\r
139 public void exception(ReadGraph graph, Throwable throwable) throws DatabaseException {
\r
140 throwable.printStackTrace();
\r
145 // A complicated-looking procedure for obtaining all HasProperties to properties map
\r
146 graph.forEachPredicate(elementResource, new AsyncMultiProcedure<Resource>() {
\r
149 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
150 throwable.printStackTrace();
\r
154 public void execute(AsyncReadGraph graph, final Resource property) {
\r
156 ready.incrementAndGet();
\r
159 l0 = Layer0.getInstance(graph.getSession());
\r
160 } catch (DatabaseException e) {
\r
161 e.printStackTrace();
\r
165 graph.forIsSubrelationOf(property, l0.HasProperty, new AsyncProcedure<Boolean>() {
\r
168 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
169 throwable.printStackTrace();
\r
173 public void execute(AsyncReadGraph graph, final Boolean isProperty) {
\r
177 graph.forPossibleRelatedValue(elementResource, property, new AsyncProcedure<Object>() {
\r
180 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
181 throwable.printStackTrace();
\r
185 public void execute(AsyncReadGraph graph, final Object value) {
\r
189 l0 = Layer0.getInstance(graph.getSession());
\r
190 } catch (DatabaseException e) {
\r
191 e.printStackTrace();
\r
195 graph.forPossibleRelatedValue(property, l0.HasName, Bindings.STRING, new AsyncProcedure<String>() {
\r
198 public void exception(AsyncReadGraph graph, Throwable throwable) {
\r
199 throwable.printStackTrace();
\r
203 public void execute(AsyncReadGraph graph, String name) {
\r
205 properties.put(name, Pair.make(property, value));
\r
206 if(ready.decrementAndGet() == 0) {
\r
207 element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));
\r
208 procedure.execute(graph, element);
\r
222 if(ready.decrementAndGet() == 0) {
\r
223 element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Pair<Resource, Object>>(properties));
\r
224 procedure.execute(graph, element);
\r
235 public void finished(AsyncReadGraph graph) {
\r
237 if(ready.decrementAndGet() == 0) {
\r
238 element.setHint(DiagramHints.PROPERTIES, new HashMap<String, Object>(properties));
\r
239 procedure.execute(graph, element);
\r