KickJava   Java API By Example, From Geeks To Geeks.

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


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.appkit.gview.object.GElementRect;
38
39 public class SLinkElbowLeftTop {
40
41     public static final int TOP_BOTTOM = 0;
42     public static final int BOTTOM_TOP = 1;
43     public static final int LABEL_OFFSET = 10;
44
45     public SLinkElbow link = null;
46     public Path2D path = null;
47
48     public SLinkElbowLeftTop(SLinkElbow link) {
49         this.link = link;
50     }
51
52     public void updateRightTop() {
53         this.path = link.path;
54         if(link.getEndWithOffset().y>link.start.y)
55             buildVerticalPath();
56         else if(link.start.x+link.outOffsetLength<link.end.x-GElementRect.DEFAULT_WIDTH+10)
57             buildHorizontalPath();
58         else
59             buildHorizontalLeftPath(false);
60     }
61
62     public void updateLeftTop() {
63         this.path = link.path;
64         if(link.getEndWithOffset().y>link.start.y)
65             buildVerticalPath();
66         else if(link.start.x>link.end.x+GElementRect.DEFAULT_WIDTH+10)
67             buildHorizontalPath();
68         else
69             buildHorizontalLeftPath(true);
70     }
71
72     public void buildVerticalPath() {
73         Vector2D start_ = link.getStartWithOffset();
74         Vector2D end_ = link.getEndWithOffset();
75
76         if(link.start.x > link.end.x+link.outOffsetLength && link.startDirection == Anchor2D.DIRECTION_LEFT) {
77             path.add(link.start);
78             path.add(link.end.x, link.start.y);
79             path.add(link.end);
80
81             link.label.setPosition(link.end.x+(link.start.x-link.end.x)*0.5, link.start.y-LABEL_OFFSET);
82         } else if(link.start.x < link.end.x-link.outOffsetLength && link.startDirection == Anchor2D.DIRECTION_RIGHT) {
83             path.add(link.start);
84             path.add(link.end.x, link.start.y);
85             path.add(link.end);
86
87             link.label.setPosition(link.end.x+(link.start.x-link.end.x)*0.5, link.start.y-LABEL_OFFSET);
88         } else if(link.start.y+GElementRect.DEFAULT_HEIGHT*0.5>end_.y-10) {
89             Vector2D p1 = new Vector2D(link.end.x, link.start.y-GElementRect.DEFAULT_HEIGHT*0.5-10);
90             Vector2D p2 = new Vector2D(start_.x, p1.y);
91
92             path.add(link.start);
93             path.add(start_);
94             path.add(p2);
95             path.add(p1);
96             path.add(link.end);
97
98             link.label.setPosition(p1.add(p2.sub(p1).stretch(0.5)).shift(0, -LABEL_OFFSET));
99         } else {
100             Vector2D ab = start_.sub(link.end);
101             Vector2D p1 = new Vector2D(link.end.x, link.end.y+ab.y*0.5);
102             Vector2D p2 = new Vector2D(p1.x+ab.x, p1.y);
103             Vector2D p3 = new Vector2D(p2.x, start_.y);
104
105             path.add(link.start);
106             path.add(start_);
107             path.add(p3);
108             path.add(p2);
109             path.add(p1);
110             path.add(link.end);
111
112             link.label.setPosition(p1.add(p2.sub(p1).stretch(0.5)).shift(0, LABEL_OFFSET));
113         }
114     }
115
116     public void buildHorizontalPath() {
117         Vector2D start_ = link.getStartWithOffset();
118         Vector2D end_ = link.getEndWithOffset();
119
120         Vector2D ab = link.start.sub(end_);
121         Vector2D p1 = new Vector2D(end_.x+ab.x*0.5, end_.y);
122         Vector2D p2 = new Vector2D(p1.x, start_.y);
123
124         path.add(link.start);
125         path.add(start_);
126         path.add(p2);
127         path.add(p1);
128         path.add(end_);
129         path.add(link.end);
130
131         link.label.setPosition(p1.add(p2.sub(p1).stretch(0.5)).shift(LABEL_OFFSET, 0));
132     }
133
134     public void buildHorizontalLeftPath(boolean left) {
135         Vector2D start_ = link.getStartWithOffset();
136         Vector2D end_ = link.getEndWithOffset();
137
138         double farest_x;
139         double farest_y = Math.min(end_.y, start_.y-GElementRect.DEFAULT_HEIGHT*0.5-10);
140
141         if(left)
142             farest_x = Math.min(end_.x-GElementRect.DEFAULT_WIDTH*0.5-10, start_.x);
143         else
144             farest_x = Math.max(end_.x+GElementRect.DEFAULT_WIDTH*0.5+10, start_.x);
145
146         Vector2D p1 = new Vector2D(end_.x, farest_y);
147         Vector2D p2 = new Vector2D(farest_x, p1.y);
148         Vector2D p3 = new Vector2D(p2.x, start_.y);
149
150         path.add(link.start);
151         path.add(p3);
152         path.add(p2);
153         path.add(p1);
154         path.add(end_);
155         path.add(link.end);
156
157         link.label.setPosition(p1.add(p2.sub(p1).stretch(0.5)).shift(0, -LABEL_OFFSET));
158     }
159
160 }
161
Popular Tags