]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/content/ArrowConfigurer.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / content / ArrowConfigurer.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in 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.diagram.content;\r
13 \r
14 import java.util.EnumSet;\r
15 import java.util.StringTokenizer;\r
16 \r
17 import org.simantics.g2d.connection.EdgeVisualsConfigurer;\r
18 import org.simantics.g2d.element.IElement;\r
19 import org.simantics.g2d.element.handler.EdgeVisuals;\r
20 import org.simantics.g2d.element.handler.EdgeVisuals.ArrowType;\r
21 import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd;\r
22 \r
23 /**\r
24  * @author Tuukka Lehtonen\r
25  */\r
26 public class ArrowConfigurer implements EdgeVisualsConfigurer {\r
27 \r
28     private ArrowType arrow     = ArrowType.None;\r
29     private double    arrowSize = 1.0;\r
30 \r
31     public ArrowConfigurer() {\r
32     }\r
33 \r
34     public ArrowConfigurer(ArrowType arrow, double arrowSize) {\r
35         this.arrow = arrow;\r
36         this.arrowSize = arrowSize;\r
37     }\r
38 \r
39     public ArrowConfigurer(String description) {\r
40         StringTokenizer tokenizer = new StringTokenizer(description);\r
41         if (tokenizer.hasMoreTokens()) {\r
42             String type = tokenizer.nextToken();\r
43             this.arrow = parseType(type);\r
44         }\r
45         if (tokenizer.hasMoreTokens()) {\r
46             String size = tokenizer.nextToken();\r
47             this.arrowSize = parseSize(size);\r
48         }\r
49     }\r
50 \r
51     private double parseSize(String size) {\r
52         try {\r
53             return Double.parseDouble(size);\r
54         } catch (NumberFormatException e) {\r
55             return 1.0;\r
56         }\r
57     }\r
58 \r
59     private ArrowType parseType(String type) {\r
60         String lower = type.toLowerCase();\r
61         if ("none".equals(lower))\r
62             return ArrowType.None;\r
63         if ("stroke".equals(lower))\r
64             return ArrowType.Stroke;\r
65         if ("fill".equals(lower))\r
66             return ArrowType.Fill;\r
67         if ("both".equals(lower))\r
68             return ArrowType.Both;\r
69         throw new IllegalArgumentException("unrecognized arrow type: " + type);\r
70     }\r
71 \r
72     @Override\r
73     public void configure(IElement edge, EdgeVisuals visuals, EnumSet<EdgeEnd> ends) {\r
74         for (EdgeEnd end : ends) {\r
75             visuals.setArrowType(edge, end, arrow);\r
76             visuals.setArrowSize(edge, end, arrowSize);\r
77         }\r
78     }\r
79 \r
80     @Override\r
81     public String toString() {\r
82         return getClass().getSimpleName() + "[" + arrow + " " + arrowSize + "]";\r
83     }\r
84 \r
85 }\r