1 31 32 package org.antlr.works.visualization.skin.syntaxdiagram; 33 34 import org.antlr.works.visualization.graphics.GContext; 35 import org.antlr.works.visualization.graphics.shape.GLink; 36 37 import java.awt.*; 38 39 public class SDLink { 40 41 public static boolean linkContainsPoint(GLink link, Point p) { 42 GContext context = link.getContext(); 43 float ox = link.source.getX(); 44 float oy = link.source.getY(); 45 float width = link.source.linkDimension.getPixelWidth(context); 46 47 return p.x>=ox && p.x<=ox+width && 48 p.y>=oy-context.getPixelBoxDown() && p.y<=oy+context.getPixelBoxDown()+context.getPixelBoxUp(); 49 } 50 51 public static void draw(GLink link) { 52 GContext context = link.getContext(); 53 54 float sx = link.source.getX(); 55 float sy = link.source.getY(); 56 57 float tx = link.target.getX(); 58 float ty = link.target.getY(); 59 60 float sloopBaseWidth = context.getPixelValue(GContext.EPSILON_WIDTH); 61 62 context.setColor(context.linkColor); 63 64 if(link.transition.isEpsilon()) { 65 if(link.virtualPosition != null) { 66 drawDownSloop(context, link, sx, sy, sx+sloopBaseWidth, link.getVirtualY()); 67 context.drawLine(sx+sloopBaseWidth, link.getVirtualY(), tx-sloopBaseWidth, link.getVirtualY()); 68 drawUpSloop(context, link, tx-sloopBaseWidth, link.getVirtualY(), tx, ty); 69 } else if(sy > ty) { 70 if((tx-sx>sloopBaseWidth) && sloopBaseWidth>0) { 72 context.drawLine(sx, sy, tx-sloopBaseWidth, sy); 73 drawUpSloop(context, link, tx-sloopBaseWidth, sy, tx, ty); 74 } else 75 drawUpSloop(context, link, sx, sy, tx, ty); 76 } else if(ty > sy) { 77 drawDownSloop(context, link, sx, sy, tx, ty); 79 } else { 80 context.drawLine(sx, sy, tx, ty); 82 83 if(link.target.lastNodeOfRule) 85 context.drawRightArrow(tx, ty, context.getPixelArrowWidth(), context.getPixelArrowHeight()); 86 } 87 88 } else { 89 drawBox(context, link); 90 } 91 } 92 93 public static void drawUpSloop(GContext context, GLink link, float x0, float y0, float x1, float y1) { 94 float xm = x1; 95 if(link.last) { 96 float r = (x1-x0)/4; 97 if(link.transition.loop) { 98 context.drawLine(x0, y0, xm-r, y0); 99 context.drawArc(xm-2*r, y0, 2*r, 2*r, 0, 90); 100 context.drawLine(xm, y0+r, xm, y1); 101 } else { 102 context.drawLine(x0, y0, xm-r, y0); 103 context.drawArc(xm-2*r, y0-2*r, 2*r, 2*r, 0, -90); 104 context.drawLine(xm, y0-r, xm, y1); 105 } 106 } else { 107 context.drawLine(x0, y0, xm, y0); 108 context.drawLine(xm, y0, xm, y1); 109 110 context.drawRightArrow(xm, y0, context.getPixelArrowWidth(), context.getPixelArrowHeight()); 111 } 112 113 if(!link.transition.loop) 114 context.drawUpArrow(xm, y1, context.getPixelArrowWidth(), context.getPixelArrowHeight()); 115 } 116 117 public static void drawDownSloop(GContext context, GLink link, float x0, float y0, float x1, float y1) { 118 float r = (x1-x0)/4; 120 if(link.last) { 121 if(link.transition.loop) { 122 context.drawLine(x0, y0, x0, y1+r); 123 context.drawArc(x0, y1, 2*r, 2*r, 90, 90); 124 context.drawLine(x0+r, y1, x1, y1); 125 context.drawDownArrow(x0, y0, context.getPixelArrowWidth(), context.getPixelArrowHeight()); 126 } else { 127 context.drawLine(x0, y0, x0, y1-r); 130 context.drawArc(x0, y1-2*r, 2*r, 2*r, -90, -90); 131 context.drawLine(x0+r, y1, x1, y1); 132 } 133 } else { 134 context.drawLine(x0, y0+r, x0, y1); 135 context.drawLine(x0, y1, x1, y1); 136 } 137 } 138 139 public static void drawBox(GContext context, GLink link) { 140 float ox = link.source.getX(); 141 float oy = link.source.getY(); 142 float width = link.source.linkDimension.getPixelWidth(context); 143 144 context.drawLine(ox, oy, ox+context.getPixelBoxEdge(), oy); 145 context.drawRightArrow(ox+context.getPixelBoxEdge(), oy, context.getPixelArrowWidth(), context.getPixelArrowHeight()); 146 147 Font font; 148 if(link.transition.externalRuleRef) { 149 font = context.getRuleFont(); 150 context.drawRoundRect(ox+context.getPixelBoxEdge(), oy-context.getPixelBoxDown(), 153 width-2*context.getPixelBoxEdge(), context.getPixelBoxDown()+context.getPixelBoxUp(), 154 8, 8, true); 155 } else { 156 font = context.getBoxFont(); 157 context.drawRect(ox+context.getPixelBoxEdge(), oy-context.getPixelBoxDown(), 158 width-2*context.getPixelBoxEdge(), context.getPixelBoxDown()+context.getPixelBoxUp(), true); 159 } 160 161 context.drawLine(ox+width-context.getPixelBoxEdge(), oy, ox+width, oy); 162 163 context.setColor(context.getColorForLabel(link.transition.label)); 164 context.drawString(font, link.transition.label, ox+width/2, oy, GContext.ALIGN_CENTER); 165 } 166 167 } 168 | Popular Tags |