1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 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.awt.Color;
\r
15 import java.awt.Graphics2D;
\r
16 import java.awt.geom.AffineTransform;
\r
17 import java.awt.geom.Path2D;
\r
18 import java.awt.geom.Rectangle2D;
\r
19 import java.io.Serializable;
\r
20 import java.util.StringTokenizer;
\r
22 import org.simantics.diagram.connection.rendering.arrows.ILineEndStyle;
\r
26 * Copied from ArrowLLineEndStyle
\r
28 public class FlowArrowLineStyle implements ILineEndStyle, Serializable {
\r
30 private static final long serialVersionUID = 5348566089660986479L;
\r
32 public static enum ArrowType { None, Stroke, Fill }
\r
34 public static final double length = 8.0;
\r
35 public static final double width = 4.0;
\r
36 public static final double space = 0.0;
\r
38 protected ArrowType type;
\r
39 protected Path2D path;
\r
40 protected double lineEndLength;
\r
42 protected Rectangle2D bounds = new Rectangle2D.Double();
\r
44 public FlowArrowLineStyle(String desc, Rectangle2D bounds) {
\r
45 this.type = ArrowType.None;
\r
46 this.lineEndLength = 0.0;
\r
51 this.bounds = bounds;
\r
53 StringTokenizer tokenizer = new StringTokenizer(desc);
\r
54 if (tokenizer.hasMoreTokens()) {
\r
55 String type = tokenizer.nextToken();
\r
56 this.type = parseType(type);
\r
58 if (tokenizer.hasMoreTokens()) {
\r
59 String ls = tokenizer.nextToken();
\r
60 l = parseSize(ls, length);
\r
62 if (tokenizer.hasMoreTokens()) {
\r
63 String ws = tokenizer.nextToken();
\r
64 w = parseSize(ws, width);
\r
66 if (tokenizer.hasMoreTokens()) {
\r
67 String ss = tokenizer.nextToken();
\r
68 s = parseSize(ss, space);
\r
72 if (this.type != ArrowType.None) {
\r
73 this.path = arrow(l, w, s);
\r
74 lineEndLength = l+s;
\r
80 public void render(Graphics2D g, double x, double y, int dir) {
\r
81 if (type == ArrowType.None || path == null)
\r
83 // Calculate coordinates to the border of the terminal
\r
86 x = bounds.getMinX();
\r
89 y = bounds.getMinY();
\r
92 x = bounds.getMaxX();
\r
95 y = bounds.getMaxY();
\r
100 AffineTransform old = g.getTransform();
\r
102 g.rotate(dir*Math.PI*0.5);
\r
103 g.setColor(Color.BLACK);
\r
114 g.setTransform(old);
\r
118 public double getLineEndLength(int direction) {
\r
119 switch(direction) {
\r
121 lineEndLength = bounds.getWidth() / 2.0;
\r
124 lineEndLength = bounds.getHeight() / 2.0;
\r
127 lineEndLength = bounds.getWidth() / 2.0;
\r
130 lineEndLength = bounds.getHeight() / 2.0;
\r
133 return lineEndLength;
\r
136 private static Path2D arrow(double length, double width, double space) {
\r
137 Path2D.Double path = new Path2D.Double();
\r
138 path.moveTo(-space, 0);
\r
139 path.lineTo(-length-space, -width);
\r
140 path.lineTo(-length-space, +width);
\r
145 private double parseSize(String size, double defaultValue) {
\r
147 return Double.parseDouble(size);
\r
148 } catch (NumberFormatException e) {
\r
149 return defaultValue;
\r
153 private ArrowType parseType(String type) {
\r
154 String lower = type.toLowerCase();
\r
155 if ("none".equals(lower))
\r
156 return ArrowType.None;
\r
157 if ("stroke".equals(lower))
\r
158 return ArrowType.Stroke;
\r
159 if ("fill".equals(lower))
\r
160 return ArrowType.Fill;
\r
161 throw new IllegalArgumentException("unrecognized arrow type: " + type);
\r
165 public String toString() {
\r
166 return getClass().getSimpleName() + "[" + type + ", " + path + "]";
\r