KickJava   Java API By Example, From Geeks To Geeks.

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


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

163         Vector2D p1 = start_.add(new Vector2D(farest_x-start.x, 0));
164         Vector2D p2 = end_.add(new Vector2D(farest_x-end.x, 0));
165
166         path.add(start);
167         path.add(start_);
168         path.add(p1);
169         path.add(p2);
170         path.add(end_);
171         path.add(end);
172
173         link.label.setPosition(p1.add(p2.sub(p1).stretch(0.5)).shift(LABEL_OFFSET, 0));
174     }
175
176 }
177
Popular Tags