KickJava   Java API By Example, From Geeks To Geeks.

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


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 SLinkElbowBottomBottom {
39
40     public static final int BOTTOM_BOTTOM = 0;
41     public static final int TOP_TOP = 1;
42     public static final int LABEL_OFFSET = 10;
43
44     public SLinkElbow link = null;
45     public Path2D path = null;
46
47     public SLinkElbowBottomBottom(SLinkElbow link) {
48         this.link = link;
49     }
50
51     public void updateBottomBottom() {
52         this.path = link.path;
53         if(Math.abs(link.end.x-link.start.x)>=GElementRect.DEFAULT_WIDTH*0.5+10) {
54             buildHorizontalPath(BOTTOM_BOTTOM);
55         } else {
56             // Objects are too close
57
if(link.start.y>=link.end.y)
58                 buildHorizontalRightBelowPath();
59             else
60                 buildHorizontalRightAbovePath();
61         }
62     }
63
64     public void updateTopTop() {
65         this.path = link.path;
66         if(Math.abs(link.end.x-link.start.x)>=GElementRect.DEFAULT_WIDTH*0.5+10) {
67             buildHorizontalPath(TOP_TOP);
68         } else {
69             // Objects are too close
70
if(link.start.y>=link.end.y)
71                 buildHorizontalRightAbovePath();
72             else
73                 buildHorizontalRightBelowPath();
74         }
75     }
76
77     public void buildHorizontalPath(int direction) {
78         Vector2D start_ = link.getStartWithOffset();
79         Vector2D end_ = link.getEndWithOffset();
80
81         if(link.offsetToMouse != null) {
82             double y = link.offsetToMouse.y+link.start.y;
83             start_.y = end_.y = y;
84         }
85
86         double farest_y = 0;
87         if(direction == BOTTOM_BOTTOM)
88             farest_y = Math.max(start_.y, end_.y);
89         else
90             farest_y = Math.min(start_.y, end_.y);
91
92         Vector2D p1 = start_.add(new Vector2D(0, farest_y-start_.y));
93         Vector2D p2 = end_.add(new Vector2D(0, farest_y-end_.y));
94
95         path.add(link.start);
96         path.add(start_);
97         path.add(p1);
98         path.add(p2);
99         path.add(end_);
100         path.add(link.end);
101
102         if(direction == BOTTOM_BOTTOM)
103             link.label.setPosition(p1.add(p2.sub(p1).stretch(0.5)).shift(0, LABEL_OFFSET));
104         else
105             link.label.setPosition(p1.add(p2.sub(p1).stretch(0.5)).shift(0, -LABEL_OFFSET));
106     }
107
108     public void buildHorizontalRightBelowPath() {
109         Vector2D start_ = link.getStartWithOffset();
110         Vector2D end_ = link.getEndWithOffset();
111
112         Vector2D start = link.start;
113         Vector2D end = link.end;
114
115         double farest_x = start_.x+GElementRect.DEFAULT_WIDTH*0.5+10;
116
117         // Extend the link to the RIGHT
118

119         Vector2D p1 = start_.add(new Vector2D(farest_x-start_.x, 0));
120         Vector2D p2 = end_.add(new Vector2D(farest_x-end_.x, 0));
121
122         path.add(start);
123         path.add(start_);
124         path.add(p1);
125         path.add(p2);
126         path.add(end_);
127         path.add(end);
128
129         link.label.setPosition(p1.add(p2.sub(p1).stretch(0.5)).shift(LABEL_OFFSET, 0));
130     }
131
132     public void buildHorizontalRightAbovePath() {
133         Vector2D start_ = link.getStartWithOffset();
134         Vector2D end_ = link.getEndWithOffset();
135
136         Vector2D start = link.start;
137         Vector2D end = link.end;
138
139         double farest_x = end_.x+GElementRect.DEFAULT_WIDTH*0.5+10;
140
141         // Extend the link to the RIGHT
142

143         Vector2D p1 = start_.add(new Vector2D(farest_x-start_.x, 0));
144         Vector2D p2 = end_.add(new Vector2D(farest_x-end_.x, 0));
145
146         path.add(start);
147         path.add(start_);
148         path.add(p1);
149         path.add(p2);
150         path.add(end_);
151         path.add(end);
152
153         link.label.setPosition(p1.add(p2.sub(p1).stretch(0.5)).shift(LABEL_OFFSET, 0));
154     }
155
156 }
157
Popular Tags