]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/content/ArrowConfigurer.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / content / ArrowConfigurer.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.content;
13
14 import java.util.EnumSet;
15 import java.util.StringTokenizer;
16
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;
22
23 /**
24  * @author Tuukka Lehtonen
25  */
26 public class ArrowConfigurer implements EdgeVisualsConfigurer {
27
28     private ArrowType arrow     = ArrowType.None;
29     private double    arrowSize = 1.0;
30
31     public ArrowConfigurer() {
32     }
33
34     public ArrowConfigurer(ArrowType arrow, double arrowSize) {
35         this.arrow = arrow;
36         this.arrowSize = arrowSize;
37     }
38
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);
44         }
45         if (tokenizer.hasMoreTokens()) {
46             String size = tokenizer.nextToken();
47             this.arrowSize = parseSize(size);
48         }
49     }
50
51     private double parseSize(String size) {
52         try {
53             return Double.parseDouble(size);
54         } catch (NumberFormatException e) {
55             return 1.0;
56         }
57     }
58
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);
70     }
71
72     @Override
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);
77         }
78     }
79
80     @Override
81     public String toString() {
82         return getClass().getSimpleName() + "[" + arrow + " " + arrowSize + "]";
83     }
84
85 }