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