KickJava   Java API By Example, From Geeks To Geeks.

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


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

170         Vector2D p1 = start_.add(new Vector2D(0, farest_y-start.y));
171         Vector2D p2 = end_.add(new Vector2D(0, farest_y-end.y));
172
173         path.clear();
174         path.add(start);
175         path.add(start_);
176         path.add(p1);
177         path.add(p2);
178         path.add(end_);
179         path.add(end);
180
181         Vector2D labelv = start_.sub(p2);
182         labelv.stretch(0.5);
183         link.label.setPosition(p2.add(labelv).shift(0, LABEL_OFFSET));
184     }
185 }
186
Popular Tags