+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.diagram.content;\r
+\r
+import java.util.EnumSet;\r
+import java.util.StringTokenizer;\r
+\r
+import org.simantics.g2d.connection.EdgeVisualsConfigurer;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.handler.EdgeVisuals;\r
+import org.simantics.g2d.element.handler.EdgeVisuals.ArrowType;\r
+import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ArrowConfigurer implements EdgeVisualsConfigurer {\r
+\r
+ private ArrowType arrow = ArrowType.None;\r
+ private double arrowSize = 1.0;\r
+\r
+ public ArrowConfigurer() {\r
+ }\r
+\r
+ public ArrowConfigurer(ArrowType arrow, double arrowSize) {\r
+ this.arrow = arrow;\r
+ this.arrowSize = arrowSize;\r
+ }\r
+\r
+ public ArrowConfigurer(String description) {\r
+ StringTokenizer tokenizer = new StringTokenizer(description);\r
+ if (tokenizer.hasMoreTokens()) {\r
+ String type = tokenizer.nextToken();\r
+ this.arrow = parseType(type);\r
+ }\r
+ if (tokenizer.hasMoreTokens()) {\r
+ String size = tokenizer.nextToken();\r
+ this.arrowSize = parseSize(size);\r
+ }\r
+ }\r
+\r
+ private double parseSize(String size) {\r
+ try {\r
+ return Double.parseDouble(size);\r
+ } catch (NumberFormatException e) {\r
+ return 1.0;\r
+ }\r
+ }\r
+\r
+ private ArrowType parseType(String type) {\r
+ String lower = type.toLowerCase();\r
+ if ("none".equals(lower))\r
+ return ArrowType.None;\r
+ if ("stroke".equals(lower))\r
+ return ArrowType.Stroke;\r
+ if ("fill".equals(lower))\r
+ return ArrowType.Fill;\r
+ if ("both".equals(lower))\r
+ return ArrowType.Both;\r
+ throw new IllegalArgumentException("unrecognized arrow type: " + type);\r
+ }\r
+\r
+ @Override\r
+ public void configure(IElement edge, EdgeVisuals visuals, EnumSet<EdgeEnd> ends) {\r
+ for (EdgeEnd end : ends) {\r
+ visuals.setArrowType(edge, end, arrow);\r
+ visuals.setArrowSize(edge, end, arrowSize);\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ return getClass().getSimpleName() + "[" + arrow + " " + arrowSize + "]";\r
+ }\r
+\r
+}\r