1 31 32 package org.antlr.xjlib.appkit.gview.object; 33 34 import org.antlr.xjlib.appkit.gview.base.Rect; 35 import org.antlr.xjlib.appkit.gview.base.Vector2D; 36 import org.antlr.xjlib.appkit.gview.shape.SLink; 37 import org.antlr.xjlib.appkit.gview.shape.SLinkArc; 38 import org.antlr.xjlib.appkit.gview.shape.SLinkBezier; 39 import org.antlr.xjlib.appkit.gview.shape.SLinkElbow; 40 import org.antlr.xjlib.foundation.XJXMLSerializable; 41 42 import java.awt.*; 43 44 45 public class GLink extends GElement implements XJXMLSerializable { 46 47 public static final int SHAPE_ARC = 0; 48 public static final int SHAPE_ELBOW = 1; 49 public static final int SHAPE_BEZIER = 2; 50 51 public GElement source = null; 52 public GElement target = null; 53 public String sourceAnchorKey = null; 54 public String targetAnchorKey = null; 55 public String pattern = null; 56 57 protected SLink link = null; 58 59 protected int shape = SHAPE_ARC; 60 61 public GLink() { 62 super(); 63 } 64 65 public GLink(GElement source, String sourceAnchorKey, GElement target, String targetAnchorKey, int shape, String pattern, Point mouse, double flateness) { 66 this.source = source; 67 this.target = target; 68 this.sourceAnchorKey = sourceAnchorKey; 69 this.targetAnchorKey = targetAnchorKey; 70 this.shape = shape; 71 this.pattern = pattern; 72 initializeLink(flateness); 73 setSourceTangentOffset(source.getDefaultAnchorOffset(sourceAnchorKey)); 74 setTargetTangentOffset(target.getDefaultAnchorOffset(targetAnchorKey)); 75 link.setDirection(Vector2D.vector(mouse).sub(target.getPosition())); 76 } 77 78 public GLink(GElement source, String sourceAnchorKey, GElement target, String targetAnchorKey, int shape, String pattern, double flateness) { 79 this.source = source; 80 this.target = target; 81 this.sourceAnchorKey = sourceAnchorKey; 82 this.targetAnchorKey = targetAnchorKey; 83 this.shape = shape; 84 this.pattern = pattern; 85 initializeLink(flateness); 86 if(source == target) 87 link.setDirection(new Vector2D(0, 1)); 88 else 89 link.setDirection(source.getPosition().sub(target.getPosition())); 90 setSourceTangentOffset(source.getDefaultAnchorOffset(sourceAnchorKey)); 91 setTargetTangentOffset(target.getDefaultAnchorOffset(targetAnchorKey)); 92 } 93 94 public void setBezierControlPoints(Vector2D points[]) { 95 if(link instanceof SLinkBezier) { 96 SLinkBezier lb = (SLinkBezier)link; 97 lb.setControlPoints(points); 98 } 99 } 100 101 public void setBezierLabelPosition(Vector2D position) { 102 if(link instanceof SLinkBezier) { 103 SLinkBezier lb = (SLinkBezier)link; 104 lb.setLabelPosition(position); 105 } 106 } 107 108 protected SLink createLinkInstance() { 109 switch(shape) { 110 case SHAPE_ARC: return new SLinkArc(); 111 case SHAPE_ELBOW: return new SLinkElbow(); 112 case SHAPE_BEZIER: return new SLinkBezier(); 113 } 114 return null; 115 } 116 117 protected void initializeLink(double flateness) { 118 if(link == null) { 119 link = createLinkInstance(); 120 link.setFlateness(flateness); 121 } 122 } 123 124 public void setSource(GElement source) { 125 this.source = source; 126 } 127 128 public GElement getSource() { 129 return source; 130 } 131 132 public void setTarget(GElement target) { 133 this.target = target; 134 } 135 136 public GElement getTarget() { 137 return target; 138 } 139 140 public void setSourceAnchorKey(String key) { 141 this.sourceAnchorKey = key; 142 } 143 144 public String getSourceAnchorKey() { 145 return sourceAnchorKey; 146 } 147 148 public void setTargetAnchorKey(String key) { 149 this.targetAnchorKey = key; 150 } 151 152 public String getTargetAnchorKey() { 153 return targetAnchorKey; 154 } 155 156 public void setSourceTangentOffset(double offset) { 157 link.setStartTangentOffset(offset); 158 } 159 160 public void setTargetTangentOffset(double offset) { 161 link.setEndTangentOffset(offset); 162 } 163 164 public void setSourceOffset(double x, double y) { 165 setSourceOffset(new Vector2D(x, y)); 166 } 167 168 public void setSourceOffset(Vector2D offset) { 169 link.setStartOffset(offset); 170 } 171 172 public void setTargetOffset(double x, double y) { 173 setTargetOffset(new Vector2D(x, y)); 174 } 175 176 public void setTargetOffset(Vector2D offset) { 177 link.setEndOffset(offset); 178 } 179 180 public void setLink(SLink link) { 181 this.link = link; 182 } 183 184 public SLink getLink() { 185 return link; 186 } 187 188 public void setPattern(String pattern) { 189 this.pattern = pattern; 190 } 191 192 public String getPattern() { 193 return pattern; 194 } 195 196 @Override 197 public void setLabel(String label) { 198 this.pattern = label; 199 } 200 201 @Override 202 public void setLabelColor(Color color) { 203 link.setLabelColor(color); 204 } 205 206 @Override 207 public void setLabelVisible(boolean flag) { 208 if(link != null) 209 link.setLabelVisible(flag); 210 } 211 212 @Override 213 public boolean isLabelVisible() { 214 return link != null && link.isLabelVisible(); 215 } 216 217 public void setShape(int type) { 218 this.shape = type; 219 } 220 221 public int getShape() { 222 return shape; 223 } 224 225 public void toggleShape() { 226 switch(shape) { 227 case SHAPE_ARC: 228 shape = SHAPE_ELBOW; 229 break; 230 case SHAPE_ELBOW: 231 shape = SHAPE_ARC; 232 break; 233 case SHAPE_BEZIER: 234 return; 236 } 237 double flateness = link.getFlateness(); 238 Vector2D direction = link.getDirection(); 239 240 link = createLinkInstance(); 241 link.setFlateness(flateness); 242 link.setDirection(direction); 243 } 244 245 public void setMousePosition(Point mouse) { 246 link.setDirection(Vector2D.vector(mouse).sub(target.getPosition())); 247 link.setMousePosition(Vector2D.vector(mouse)); 248 } 249 250 @Override 251 public Rect getFrame() { 252 update(); 253 return link.getFrame(); 254 } 255 256 @Override 257 public boolean isInside(Point p) { 258 return link != null && link.contains(p.x, p.y); 259 } 260 261 public void update() { 262 initializeLink(0); 263 264 source.updateAnchors(); 265 target.updateAnchors(); 266 267 link.setStartAnchor(source.getAnchor(sourceAnchorKey)); 268 link.setEndAnchor(target.getAnchor(targetAnchorKey)); 269 link.setLabel(pattern); 270 link.setSelfLoop(source == target); 271 272 link.update(); 273 } 274 275 @Override 276 public void draw(Graphics2D g) { 277 update(); 278 279 if(isVisibleInClip(g)) { 280 g.setStroke(new BasicStroke(penSize)); 281 282 if(color != null) 283 link.setColor(color); 284 else 285 link.setColor(Color.black); 286 287 link.draw(g); 288 } 289 } 290 291 @Override 292 public void drawShape(Graphics2D g) { 293 link.drawShape(g); 294 } 295 296 } 297 | Popular Tags |