1 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 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 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 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 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 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 |