| 1 31 32 package org.antlr.works.visualization.graphics.path; 33 34 import org.antlr.works.visualization.graphics.GContext; 35 import org.antlr.works.visualization.graphics.GObject; 36 import org.antlr.works.visualization.graphics.shape.GLink; 37 import org.antlr.works.visualization.graphics.shape.GNode; 38 39 import java.awt.*; 40 import java.awt.geom.Point2D ; 41 import java.util.HashSet ; 42 import java.util.Set ; 43 44 public class GPathElement extends GObject { 45 46 public GObject nodeOrLink; 47 public GObject source; 48 public GNode target; 49 50 public boolean isRuleLink = false; 51 52 public static GPathElement createLink(GNode node, GNode nextNode) { 53 GPathElement element = new GPathElement(node, nextNode); 54 element.setRuleLink(true); 55 return element; 56 } 57 58 public static GPathElement createElement(GObject object) { 59 return new GPathElement(object); 60 } 61 62 public GPathElement(GObject object) { 63 this.nodeOrLink = object; 64 } 65 66 public GPathElement(GObject source, GNode target) { 67 this.source = source; 68 this.target = target; 69 } 70 71 public void setContext(GContext context) { 72 super.setContext(context); 73 if(nodeOrLink != null) 74 nodeOrLink.setContext(context); 75 if(source != null) 76 source.setContext(context); 77 if(target != null) 78 target.setContext(context); 79 } 80 81 public boolean containsPoint(Point p) { 82 if(nodeOrLink != null) 83 return nodeOrLink.containsPoint(p); 84 85 return false; 86 } 87 88 public void setRuleLink(boolean flag) { 89 this.isRuleLink = flag; 90 } 91 92 public boolean isVisible() { 93 if(nodeOrLink != null) 94 return context.isObjectVisible(nodeOrLink); 95 else 96 return true; 97 } 98 99 public Set <GObject> getObjects() { 100 Set <GObject> objects = new HashSet <GObject>(); 101 102 if(nodeOrLink != null) 103 objects.add(nodeOrLink); 104 105 if(source != null) 106 objects.add(source); 107 if(target != null) 108 objects.add(target); 109 110 return objects; 111 } 112 113 protected Point2D getBeginPoint() { 114 float x0; 115 float y0; 116 if(source instanceof GNode) { 117 GNode node = (GNode)source; 118 x0 = node.getCenterX(); 119 y0 = node.getCenterY(); 120 } else { 121 GLink link = (GLink)source; 122 x0 = link.target.getBeginX(); 123 y0 = link.target.getBeginY(); 124 } 125 return new Point2D.Float (x0, y0); 126 } 127 128 public Rectangle getBounds() { 129 if(nodeOrLink != null) { 130 return nodeOrLink.getBounds(); 131 } else { 132 Point2D a = getBeginPoint(); 133 int x1 = (int)a.getX(); 134 int y1 = (int)a.getY(); 135 int x2 = (int)target.getCenterX(); 136 int y2 = (int)target.getCenterY(); 137 return new Rectangle(x1, y1, x2-x1, y2-y1); 138 } 139 } 140 141 public void draw() { 142 if(nodeOrLink != null) 143 nodeOrLink.draw(); 144 145 if(source != null && target != null) { 146 Point2D p = getBeginPoint(); 147 context.setColor(context.linkColor); 148 context.drawSpline((float)p.getX(), (float)p.getY(), target.getCenterX(), target.getCenterY(), 149 0, context.getEndOffset(), 0, true); 150 } 151 } 152 153 } 154 | Popular Tags |