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 37 import java.awt.*; 38 import java.util.HashSet ; 39 import java.util.Iterator ; 40 import java.util.List ; 41 import java.util.Set ; 42 43 public class GPath extends GObject { 44 45 public static int MIN_PATH_BLINK_WIDTH = 2; 46 public static int MAX_PATH_BLINK_WIDTH = 4; 47 48 49 protected List <GPathElement> elements; 50 51 52 protected boolean disabled = false; 53 54 55 protected boolean visible = false; 56 57 61 protected boolean selectable = true; 62 63 protected int currentIndex = -1; 64 protected float step = 0.2f; 65 protected float currentLineWidth = 1; 66 protected boolean showRuleLinks = true; 67 68 public GPath() { 69 70 } 71 72 public GPath(List <GPathElement> elements, boolean disabled) { 73 this.elements = elements; 74 this.disabled = disabled; 75 } 76 77 public void setContext(GContext context) { 78 super.setContext(context); 79 for (Iterator <GPathElement> iterator = elements.iterator(); iterator.hasNext();) { 80 GPathElement element = iterator.next(); 81 element.setContext(context); 82 } 83 } 84 85 public void setVisible(boolean flag) { 86 this.visible = flag; 87 } 88 89 public boolean isVisible() { 90 return visible; 91 } 92 93 public void setSelectable(boolean flag) { 94 this.selectable = flag; 95 } 96 97 public boolean isSelectable() { 98 return selectable; 99 } 100 101 public boolean isEnabled() { 102 return !disabled; 103 } 104 105 public void setShowRuleLinks(boolean flag) { 106 this.showRuleLinks = flag; 107 } 108 109 public int getNumberOfVisibleElements() { 110 int count = 0; 111 for(int i=0; i<elements.size(); i++) { 112 GPathElement element = elements.get(i); 113 if(element.isVisible()) 114 count++; 115 } 116 return count; 117 } 118 119 public void draw(float width, List ignoreElements) { 120 if(showRuleLinks) 121 drawElements(width, ignoreElements, true); 122 drawElements(width, ignoreElements, false); 123 } 124 125 public void drawElements(float width, List ignoreElements, boolean ruleLink) { 126 context.nodeColor = disabled?Color.red:Color.green.darker(); 127 context.linkColor = context.nodeColor; 128 context.setLineWidth(width); 129 130 for(int i=0; i<elements.size(); i++) { 131 GPathElement element = elements.get(i); 132 if(ignoreElements != null && ignoreElements.contains(element)) 133 continue; 134 135 if(!element.isVisible()) 136 continue; 137 138 if(element.isRuleLink && !ruleLink || ruleLink && !element.isRuleLink) 139 continue; 140 141 element.draw(); 142 } 143 } 144 145 public void drawSelectedElement() { 146 if(currentIndex == -1) 147 return; 148 149 context.nodeColor = disabled?Color.red:Color.green.darker(); 150 context.linkColor = context.nodeColor; 151 context.setLineWidth(currentLineWidth); 152 GPathElement element = elements.get(currentIndex); 153 element.draw(); 154 } 155 156 public Rectangle getBoundsOfSelectedElement() { 157 if(currentIndex == -1) 158 return null; 159 160 GPathElement element = elements.get(currentIndex); 161 return element.getBounds(); 162 } 163 164 public boolean containsPoint(Point p) { 165 for (Iterator <GPathElement> iterator = elements.iterator(); iterator.hasNext();) { 166 GPathElement element = iterator.next(); 167 if(element.containsPoint(p)) 168 return true; 169 } 170 return false; 171 } 172 173 public Set <GObject> getObjects() { 174 Set <GObject> objects = new HashSet <GObject>(); 175 for (Iterator <GPathElement> iterator = elements.iterator(); iterator.hasNext();) { 176 GPathElement element = iterator.next(); 177 objects.addAll(element.getObjects()); 178 } 179 return objects; 180 } 181 182 public boolean isCurrentElementVisible() { 183 GPathElement element = elements.get(currentIndex); 184 if(element.isRuleLink) 185 return showRuleLinks; 186 else 187 return element.isVisible(); 188 } 189 190 public void incrementWidth() { 191 currentLineWidth += step; 192 if(currentLineWidth >= MAX_PATH_BLINK_WIDTH || currentLineWidth <= MIN_PATH_BLINK_WIDTH) 193 step = -step; 194 } 195 196 public void selectElement() { 197 if(currentIndex == -1) 198 nextElement(); 199 } 200 201 public void deselectElement() { 202 currentIndex = -1; 203 } 204 205 public void nextElement() { 206 if(elements.isEmpty()) { 207 currentIndex = -1; 208 return; 209 } 210 211 int looping = currentIndex; 214 do { 215 currentIndex++; 216 if(currentIndex >= elements.size()) 217 currentIndex = 0; 218 if(looping == -1) 219 looping = 0; 220 else if(looping == currentIndex) 221 break; 222 } while(!isCurrentElementVisible()); 223 224 currentLineWidth = 3; 225 context.repaint(); 226 } 227 228 public void previousElement() { 229 if(elements.isEmpty()) { 230 currentIndex = -1; 231 return; 232 } 233 234 int looping = currentIndex; 237 do { 238 currentIndex--; 239 if(currentIndex<0) 240 currentIndex = elements.size()-1; 241 if(looping == -1) 242 looping = 0; 243 else if(looping == currentIndex) 244 break; 245 } while(!isCurrentElementVisible()); 246 247 currentLineWidth = 3; 248 context.repaint(); 249 } 250 251 public void firstElement() { 252 currentIndex = -1; 253 nextElement(); 254 } 255 256 public void lastElement() { 257 currentIndex = elements.size(); 258 previousElement(); 259 } 260 261 } 262 | Popular Tags |