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.elements.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.io.Serializable;
\r
19 import java.util.StringTokenizer;
\r
21 import org.simantics.diagram.connection.rendering.arrows.ILineEndStyle;
\r
25 * Copied from ArrowLLineEndStyle
\r
27 public class FlowArrowLineStyle implements ILineEndStyle, Serializable {
\r
29 private static final long serialVersionUID = 5348566089660986479L;
\r
31 public static enum ArrowType { None, Stroke, Fill }
\r
33 public static final double length = 8.0;
\r
34 public static final double width = 4.0;
\r
35 public static final double space = 0.0;
\r
37 protected ArrowType type;
\r
38 protected Path2D path;
\r
39 protected double lineEndLength;
\r
40 protected Color color;
\r
42 public FlowArrowLineStyle(String desc, Color color) {
\r
43 this.type = ArrowType.None;
\r
44 this.lineEndLength = 0.0;
\r
53 this.color = Color.BLACK;
\r
55 StringTokenizer tokenizer = new StringTokenizer(desc);
\r
56 if (tokenizer.hasMoreTokens()) {
\r
57 String type = tokenizer.nextToken();
\r
58 this.type = parseType(type);
\r
60 if (tokenizer.hasMoreTokens()) {
\r
61 String ls = tokenizer.nextToken();
\r
62 l = parseSize(ls, length);
\r
64 if (tokenizer.hasMoreTokens()) {
\r
65 String ws = tokenizer.nextToken();
\r
66 w = parseSize(ws, width);
\r
68 if (tokenizer.hasMoreTokens()) {
\r
69 String ss = tokenizer.nextToken();
\r
70 s = parseSize(ss, space);
\r
74 if (this.type != ArrowType.None) {
\r
75 this.path = arrow(l, w, s);
\r
76 lineEndLength = l+s;
\r
82 public void render(Graphics2D g, double x, double y, int dir) {
\r
83 if (type == ArrowType.None || path == null)
\r
85 AffineTransform old = g.getTransform();
\r
87 g.rotate(dir*Math.PI*0.5);
\r
99 g.setTransform(old);
\r
103 public double getLineEndLength(int direction) {
\r
104 return lineEndLength;
\r
107 private static Path2D arrow(double length, double width, double space) {
\r
108 Path2D.Double path = new Path2D.Double();
\r
109 path.moveTo(-space, 0);
\r
110 path.lineTo(-length-space, -width);
\r
111 path.lineTo(-length-space, +width);
\r
116 private double parseSize(String size, double defaultValue) {
\r
118 return Double.parseDouble(size);
\r
119 } catch (NumberFormatException e) {
\r
120 return defaultValue;
\r
124 private ArrowType parseType(String type) {
\r
125 String lower = type.toLowerCase();
\r
126 if ("none".equals(lower))
\r
127 return ArrowType.None;
\r
128 if ("stroke".equals(lower))
\r
129 return ArrowType.Stroke;
\r
130 if ("fill".equals(lower))
\r
131 return ArrowType.Fill;
\r
132 throw new IllegalArgumentException("unrecognized arrow type: " + type);
\r
136 public String toString() {
\r
137 return getClass().getSimpleName() + "[" + type + ", " + path + "]";
\r