View Javadoc

1   package org.kite9.diagram.primitives;
2   
3   import org.kite9.diagram.position.Direction;
4   import org.kite9.framework.logging.LogicException;
5   
6   import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
7   
8   public abstract class AbstractBiDirectional<X> implements BiDirectional<X> {
9   
10      /***
11       * For serialization
12       */
13      public AbstractBiDirectional() {
14      }
15  
16      public AbstractBiDirectional(X from, X to, Direction drawDirection) {
17  	super();
18  	this.from = from;
19  	this.to = to;
20  	this.drawDirection = drawDirection;
21      }
22  
23      private static final long serialVersionUID = -2932750084676000416L;
24      protected X from;
25      protected X to;
26  
27  	@XStreamAsAttribute
28      protected Direction drawDirection = null;
29  
30      public Direction getDrawDirection() {
31  	return drawDirection;
32      }
33  
34      public X getFrom() {
35  	return from;
36      }
37  
38      public X getTo() {
39  	return to;
40      }
41  
42      public void setFrom(X v) {
43  	this.from = v;
44      }
45  
46      public void setTo(X v) {
47  	this.to = v;
48      }
49  
50      @Override
51      public String toString() {
52  	return "[" + from + "-" + to + "]";
53      }
54  
55      public X otherEnd(X end) {
56  	if (end == from)
57  	    return to;
58  	if (end == to)
59  	    return from;
60  	throw new LogicException("This is not an end: " + end + " of " + this);
61      }
62  
63      public boolean meets(BiDirectional<X> e) {
64  	return getFrom().equals(e.getTo()) || getFrom().equals(e.getFrom()) || getTo().equals(e.getTo())
65  		|| getTo().equals(e.getFrom());
66      }
67  
68      public boolean meets(X v) {
69  	return getFrom().equals(v) || getTo().equals(v);
70      }
71  
72      public Direction getDrawDirectionFrom(X end) {
73  	if (drawDirection == null)
74  	    return null;
75  
76  	if (end.equals(getFrom())) {
77  	    return drawDirection;
78  	}
79  
80  	if (end.equals(getTo())) {
81  	    return Direction.values()[(drawDirection.ordinal() + 2) % 4];
82  	}
83  
84  	throw new RuntimeException("Trying to get direction from an end that's not set: " + end + " in " + this);
85      }
86  
87      public void setDrawDirection(Direction d) {
88  	this.drawDirection = d;
89      }
90  
91      public void setDrawDirectionFrom(Direction d, X end) {
92  
93  	if (end.equals(getFrom())) {
94  	    this.drawDirection = d;
95  	}
96  
97  	if (end.equals(getTo())) {
98  	    this.drawDirection = Direction.reverse(d);
99  	}
100 
101 	throw new RuntimeException("Trying to set direction from an end that's not set: " + end + " in " + this);
102 
103     }
104 }