1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.content;
14 import java.util.EnumSet;
15 import java.util.StringTokenizer;
17 import org.simantics.g2d.connection.EdgeVisualsConfigurer;
18 import org.simantics.g2d.element.IElement;
19 import org.simantics.g2d.element.handler.EdgeVisuals;
20 import org.simantics.g2d.element.handler.EdgeVisuals.ArrowType;
21 import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd;
24 * @author Tuukka Lehtonen
26 public class ArrowConfigurer implements EdgeVisualsConfigurer {
28 private ArrowType arrow = ArrowType.None;
29 private double arrowSize = 1.0;
31 public ArrowConfigurer() {
34 public ArrowConfigurer(ArrowType arrow, double arrowSize) {
36 this.arrowSize = arrowSize;
39 public ArrowConfigurer(String description) {
40 StringTokenizer tokenizer = new StringTokenizer(description);
41 if (tokenizer.hasMoreTokens()) {
42 String type = tokenizer.nextToken();
43 this.arrow = parseType(type);
45 if (tokenizer.hasMoreTokens()) {
46 String size = tokenizer.nextToken();
47 this.arrowSize = parseSize(size);
51 private double parseSize(String size) {
53 return Double.parseDouble(size);
54 } catch (NumberFormatException e) {
59 private ArrowType parseType(String type) {
60 String lower = type.toLowerCase();
61 if ("none".equals(lower))
62 return ArrowType.None;
63 if ("stroke".equals(lower))
64 return ArrowType.Stroke;
65 if ("fill".equals(lower))
66 return ArrowType.Fill;
67 if ("both".equals(lower))
68 return ArrowType.Both;
69 throw new IllegalArgumentException("unrecognized arrow type: " + type);
73 public void configure(IElement edge, EdgeVisuals visuals, EnumSet<EdgeEnd> ends) {
74 for (EdgeEnd end : ends) {
75 visuals.setArrowType(edge, end, arrow);
76 visuals.setArrowSize(edge, end, arrowSize);
81 public String toString() {
82 return getClass().getSimpleName() + "[" + arrow + " " + arrowSize + "]";