KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > gview > shape > SLinkElbow


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.xjlib.appkit.gview.shape;
33
34 import org.antlr.xjlib.appkit.gview.base.Anchor2D;
35 import org.antlr.xjlib.appkit.gview.base.Path2D;
36 import org.antlr.xjlib.appkit.gview.base.Vector2D;
37 import org.antlr.xjlib.foundation.XJXMLSerializable;
38
39 import java.awt.*;
40
41 public class SLinkElbow extends SLink implements XJXMLSerializable {
42
43     protected SLinkElbowTopBottom topbottom = new SLinkElbowTopBottom(this);
44     protected SLinkElbowLeftRight leftright = new SLinkElbowLeftRight(this);
45     protected SLinkElbowBottomBottom bottombottom = new SLinkElbowBottomBottom(this);
46     protected SLinkElbowLeftLeft leftleft = new SLinkElbowLeftLeft(this);
47     protected SLinkElbowBottomLeft bottomleft = new SLinkElbowBottomLeft(this);
48     protected SLinkElbowTopLeft topleft = new SLinkElbowTopLeft(this);
49     protected SLinkElbowLeftTop lefttop = new SLinkElbowLeftTop(this);
50     protected SLinkElbowLeftBottom leftbottom = new SLinkElbowLeftBottom(this);
51
52     protected transient Path2D path = new Path2D();
53     protected Vector2D offsetToMouse = null;
54
55     protected int outOffsetLength = 15;
56
57     public SLinkElbow() {
58         super();
59     }
60
61     public void setOutOffsetLength(int length) {
62         this.outOffsetLength = length;
63     }
64
65     public Vector2D getStartWithOffset() {
66         return start.add(startDirection.vectorLength(outOffsetLength));
67     }
68
69     public Vector2D getEndWithOffset() {
70         return end.add(endDirection.vectorLength(outOffsetLength));
71     }
72
73     public void setMousePosition(Vector2D position) {
74         setOffsetToMouse(position.sub(start));
75     }
76
77     public void setOffsetToMouse(Vector2D offset) {
78         this.offsetToMouse = offset;
79     }
80
81     public Vector2D getOffsetToMouse() {
82         return offsetToMouse;
83     }
84
85     public boolean contains(double x, double y) {
86         return path.contains(x, y);
87     }
88
89     public void update() {
90         path.clear();
91
92         if(startDirection == Anchor2D.DIRECTION_BOTTOM && endDirection == Anchor2D.DIRECTION_TOP)
93             topbottom.updateBottomTop();
94         else if(startDirection == Anchor2D.DIRECTION_TOP && endDirection == Anchor2D.DIRECTION_BOTTOM)
95             topbottom.updateTopBottom();
96         else if(startDirection == Anchor2D.DIRECTION_LEFT && endDirection == Anchor2D.DIRECTION_RIGHT)
97             leftright.updateLeftRight();
98         else if(startDirection == Anchor2D.DIRECTION_RIGHT && endDirection == Anchor2D.DIRECTION_LEFT)
99             leftright.updateRightLeft();
100         else if(startDirection == Anchor2D.DIRECTION_BOTTOM && endDirection == Anchor2D.DIRECTION_BOTTOM)
101             bottombottom.updateBottomBottom();
102         else if(startDirection == Anchor2D.DIRECTION_TOP && endDirection == Anchor2D.DIRECTION_TOP)
103             bottombottom.updateTopTop();
104         else if(startDirection == Anchor2D.DIRECTION_LEFT && endDirection == Anchor2D.DIRECTION_LEFT)
105             leftleft.updateLeftLeft();
106         else if(startDirection == Anchor2D.DIRECTION_RIGHT && endDirection == Anchor2D.DIRECTION_RIGHT)
107             leftleft.updateRightRight();
108         else if(startDirection == Anchor2D.DIRECTION_BOTTOM && endDirection == Anchor2D.DIRECTION_LEFT)
109             bottomleft.updateBottomLeft();
110         else if(startDirection == Anchor2D.DIRECTION_BOTTOM && endDirection == Anchor2D.DIRECTION_RIGHT)
111             bottomleft.updateBottomRight();
112         else if(startDirection == Anchor2D.DIRECTION_TOP && endDirection == Anchor2D.DIRECTION_LEFT)
113             topleft.updateTopLeft();
114         else if(startDirection == Anchor2D.DIRECTION_TOP && endDirection == Anchor2D.DIRECTION_RIGHT)
115             topleft.updateTopRight();
116         else if(startDirection == Anchor2D.DIRECTION_LEFT && endDirection == Anchor2D.DIRECTION_TOP)
117             lefttop.updateLeftTop();
118         else if(startDirection == Anchor2D.DIRECTION_RIGHT && endDirection == Anchor2D.DIRECTION_TOP)
119             lefttop.updateRightTop();
120         else if(startDirection == Anchor2D.DIRECTION_LEFT && endDirection == Anchor2D.DIRECTION_BOTTOM)
121             leftbottom.updateLeftBottom();
122         else if(startDirection == Anchor2D.DIRECTION_RIGHT && endDirection == Anchor2D.DIRECTION_BOTTOM)
123             leftbottom.updateRightBottom();
124         else
125             topleft.updateTopLeft();
126     }
127
128     public void draw(Graphics2D g) {
129         if(path == null || arrow == null || label == null)
130             return;
131
132         g.setColor(color);
133
134         drawShape(g);
135         label.draw(g);
136     }
137
138     public void drawShape(Graphics2D g) {
139         if(path == null || arrow == null || label == null)
140             return;
141
142         path.draw(g);
143
144         arrow.setAnchor(end);
145         arrow.setDirection(path.getEndDirection());
146         arrow.draw(g);
147     }
148
149 }
150
Popular Tags