]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
ca2f0921615b2ac73cfc5aa9ce5182e3382e1ce4
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.elements2.connections;\r
13 \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
21 \r
22 import org.simantics.diagram.connection.rendering.arrows.ILineEndStyle;\r
23 \r
24 \r
25 /**\r
26  * Copied from ArrowLLineEndStyle\r
27  */\r
28 public class FlowArrowLineStyle implements ILineEndStyle, Serializable {\r
29 \r
30     private static final long serialVersionUID = 5348566089660986479L;\r
31 \r
32     public static enum ArrowType { None, Stroke, Fill }\r
33 \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
37 \r
38     protected ArrowType type;\r
39     protected Path2D path;\r
40     protected double lineEndLength;\r
41     \r
42     protected Rectangle2D bounds = new Rectangle2D.Double();\r
43     \r
44     public FlowArrowLineStyle(String desc, Rectangle2D bounds) {\r
45         this.type = ArrowType.None;\r
46         this.lineEndLength = 0.0;\r
47 \r
48         double l = length;\r
49         double w = width;\r
50         double s = space;\r
51         this.bounds = bounds;\r
52 \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
57 \r
58             if (tokenizer.hasMoreTokens()) {\r
59                 String ls = tokenizer.nextToken();\r
60                 l = parseSize(ls, length);\r
61 \r
62                 if (tokenizer.hasMoreTokens()) {\r
63                     String ws = tokenizer.nextToken();\r
64                     w = parseSize(ws, width);\r
65 \r
66                     if (tokenizer.hasMoreTokens()) {\r
67                         String ss = tokenizer.nextToken();\r
68                         s = parseSize(ss, space);\r
69                     }\r
70                 }\r
71             }\r
72             if (this.type != ArrowType.None) {\r
73                 this.path = arrow(l, w, s);\r
74                 lineEndLength = l+s;\r
75             }\r
76         }\r
77     }\r
78 \r
79     @Override\r
80     public void render(Graphics2D g, double x, double y, int dir) {\r
81         if (type == ArrowType.None || path == null)\r
82             return;\r
83         // Calculate coordinates to the border of the terminal\r
84         switch(dir) {\r
85             case 0:\r
86                 x = bounds.getMinX();\r
87                 break;\r
88             case 1:\r
89                 y = bounds.getMinY();\r
90                 break;\r
91             case 2:\r
92                 x = bounds.getMaxX();\r
93                 break;\r
94             case 3:\r
95                 y = bounds.getMaxY();\r
96                 break;\r
97             default:\r
98                 return;\r
99         }\r
100         AffineTransform old = g.getTransform();\r
101         g.translate(x, y);\r
102         g.rotate(dir*Math.PI*0.5);\r
103         g.setColor(Color.BLACK);\r
104 \r
105         switch (type) {\r
106             case Fill:\r
107                 g.fill(path);\r
108                 break;\r
109             case Stroke:\r
110                 g.draw(path);\r
111                 break;\r
112         }\r
113 \r
114         g.setTransform(old);\r
115     }\r
116 \r
117     @Override\r
118     public double getLineEndLength(int direction) {\r
119         switch(direction) {\r
120             case 0:\r
121                 lineEndLength = bounds.getWidth() / 2.0;\r
122                 break;\r
123             case 1:\r
124                 lineEndLength = bounds.getHeight() / 2.0;\r
125                 break;\r
126             case 2:\r
127                 lineEndLength =  bounds.getWidth() / 2.0;\r
128                 break;\r
129             case 3:\r
130                 lineEndLength = bounds.getHeight() / 2.0;\r
131                 break;\r
132         }\r
133         return lineEndLength;\r
134     }\r
135 \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
141         path.closePath();\r
142         return path;\r
143     }\r
144 \r
145     private double parseSize(String size, double defaultValue) {\r
146         try {\r
147             return Double.parseDouble(size);\r
148         } catch (NumberFormatException e) {\r
149             return defaultValue;\r
150         }\r
151     }\r
152 \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
162     }\r
163 \r
164     @Override\r
165     public String toString() {\r
166         return getClass().getSimpleName() + "[" + type + ", " + path + "]";\r
167     }\r
168 \r
169 }\r