]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/query/ConnectionVisualsRequest.java
Improvements to styling of connection lines
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / query / ConnectionVisualsRequest.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.query;
13
14 import java.awt.Stroke;
15
16 import org.simantics.databoard.Bindings;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.request.ResourceRead;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.diagram.G2DUtils;
22 import org.simantics.diagram.connection.ConnectionVisuals;
23 import org.simantics.diagram.stubs.G2DResource;
24 import org.simantics.g2d.element.handler.EdgeVisuals.StrokeType;
25
26 /**
27  * @author Tuukka Lehtonen
28  */
29 public class ConnectionVisualsRequest extends ResourceRead<ConnectionVisuals> {
30
31     G2DResource g2d;
32
33     public ConnectionVisualsRequest(Resource connectionType) {
34         super(connectionType);
35         assert connectionType != null;
36     }
37
38     @Override
39     public ConnectionVisuals perform(ReadGraph g) throws DatabaseException {
40         g2d = G2DResource.getInstance(g);
41         Resource structuralConnectionType = resource;
42
43         // Load edge visual aspects
44         Resource c = g.getPossibleObject(structuralConnectionType, g2d.HasColor);
45         float[] color = null;
46         if (c != null) {
47             float[] col = g.getPossibleValue(c, Bindings.FLOAT_ARRAY);
48             if (col != null && col.length >= 3) {
49                 color = col;
50             }
51         }
52
53         StrokeType strokeType = toStrokeType(g.getPossibleObject(structuralConnectionType, g2d.HasStrokeType));
54         Stroke stroke = G2DUtils.getStroke(g, g.getPossibleObject(structuralConnectionType, g2d.HasStroke));
55         Double branchPointRadius = g.getPossibleRelatedValue(structuralConnectionType, g2d.HasBranchPointRadius, Bindings.DOUBLE);
56         Double rounding = g.getPossibleRelatedValue(structuralConnectionType, g2d.HasRounding, Bindings.DOUBLE);
57         Double offset = g.getPossibleRelatedValue(structuralConnectionType, g2d.HasOffset, Bindings.DOUBLE);
58         
59         return new ConnectionVisuals(color, strokeType, stroke, branchPointRadius, rounding, offset);
60     }
61
62     StrokeType toStrokeType(Resource strokeType) {
63         if (strokeType != null) {
64             if (strokeType.equals(g2d.StrokeType_Scaling))
65                 return StrokeType.Absolute;
66             if (strokeType.equals(g2d.StrokeType_Nonscaling))
67                 return StrokeType.Relative;
68         }
69         return null;
70     }
71
72 }