KickJava   Java API By Example, From Geeks To Geeks.

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


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 SLinkElbowLeftBottom {
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 SLinkElbowLeftBottom(SLinkElbow link) {
49         this.link = link;
50     }
51
52     public void updateRightBottom() {
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 updateLeftBottom() {
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
90             Vector2D p1 = new Vector2D(link.end.x, link.start.y+GElementRect.DEFAULT_HEIGHT*0.5+10);
91             Vector2D p2 = new Vector2D(start_.x, p1.y);
92
93             path.add(link.start);
94             path.add(start_);
95             path.add(p2);
96             path.add(p1);
97             path.add(link.end);
98
99             link.label.setPosition(p1.add(p2.sub(p1).stretch(0.5)).shift(0, LABEL_OFFSET));
100         } else {
101             Vector2D ab = start_.sub(link.end);
102             Vector2D p1 = new Vector2D(link.end.x, link.end.y+ab.y*0.5);
103             Vector2D p2 = new Vector2D(p1.x+ab.x, p1.y);
104             Vector2D p3 = new Vector2D(p2.x, start_.y);
105
106             path.add(link.start);
107             path.add(start_);
108             path.add(p3);
109             path.add(p2);
110             path.add(p1);
111             path.add(link.end);
112
113             link.label.setPosition(p1.add(p2.sub(p1).stretch(0.5)).shift(0, -LABEL_OFFSET));
114         }
115     }
116
117     public void buildHorizontalPath() {
118         Vector2D start_ = link.getStartWithOffset();
119         Vector2D end_ = link.getEndWithOffset();
120
121         Vector2D ab = link.start.sub(end_);
122         Vector2D p1 = new Vector2D(end_.x+ab.x*0.5, end_.y);
123         Vector2D p2 = new Vector2D(p1.x, start_.y);
124
125         path.add(link.start);
126         path.add(start_);
127         path.add(p2);
128         path.add(p1);
129         path.add(end_);
130         path.add(link.end);
131
132         link.label.setPosition(p1.add(p2.sub(p1).stretch(0.5)).shift(LABEL_OFFSET, 0));
133     }
134
135     public void buildHorizontalLeftPath(boolean left) {
136         Vector2D start_ = link.getStartWithOffset();
137         Vector2D end_ = link.getEndWithOffset();
138
139         double farest_x;
140         double farest_y = Math.max(end_.y, start_.y+GElementRect.DEFAULT_HEIGHT*0.5+10);
141
142         if(left)
143             farest_x = Math.min(end_.x-GElementRect.DEFAULT_WIDTH*0.5-10, start_.x);
144         else
145             farest_x = Math.max(end_.x+GElementRect.DEFAULT_WIDTH*0.5+10, start_.x);
146         
147         Vector2D p1 = new Vector2D(end_.x, farest_y);
148         Vector2D p2 = new Vector2D(farest_x, p1.y);
149         Vector2D p3 = new Vector2D(p2.x, start_.y);
150
151         path.add(link.start);
152         path.add(p3);
153         path.add(p2);
154         path.add(p1);
155         path.add(end_);
156         path.add(link.end);
157
158         link.label.setPosition(p1.add(p2.sub(p1).stretch(0.5)).shift(0, LABEL_OFFSET));
159     }
160
161 }
162
Popular Tags