1 31 32 package org.antlr.works.visualization.graphics.path; 33 34 import org.antlr.xjlib.appkit.gview.timer.GTimer; 35 import org.antlr.xjlib.appkit.gview.timer.GTimerDelegate; 36 import org.antlr.xjlib.foundation.notification.XJNotificationCenter; 37 import org.antlr.works.visualization.graphics.GContext; 38 import org.antlr.works.visualization.graphics.GObject; 39 40 import java.awt.*; 41 import java.util.ArrayList ; 42 import java.util.Iterator ; 43 import java.util.List ; 44 45 public class GPathGroup extends GObject implements GTimerDelegate { 46 47 public static final String NOTIF_CURRENT_PATH_DID_CHANGE = "NOTIF_CURRENT_PATH_DID_CHANGE"; 48 public static int DEFAULT_PATH_WIDTH = 1; 49 50 protected List <GPath> graphicPaths = new ArrayList <GPath>(); 51 protected int selectedPathIndex = 0; 52 protected GTimer timer = new GTimer(this); 53 54 protected boolean showRuleLinks = true; 55 56 public GPathGroup() { 57 } 58 59 public void setEnable(boolean flag) { 60 if(flag) 61 timer.start(); 62 else 63 timer.stop(); 64 } 65 66 public void addPath(GPath path) { 67 graphicPaths.add(path); 68 } 69 70 public List <GPath> getPaths() { 71 return graphicPaths; 72 } 73 74 public GPath getPath(int index) { 75 return graphicPaths.get(index); 76 } 77 78 public int getNumberOfPaths() { 79 return graphicPaths.size(); 80 } 81 82 public void setSelectedPath(int index) { 83 if(index<0) 84 selectedPathIndex = graphicPaths.size()-1; 85 else if(index>=graphicPaths.size()) 86 selectedPathIndex = 0; 87 else 88 selectedPathIndex = index; 89 90 updateShowRuleLinks(); 91 XJNotificationCenter.defaultCenter().postNotification(this, NOTIF_CURRENT_PATH_DID_CHANGE); 92 } 93 94 public void setPathVisible(int index, boolean flag) { 95 getPath(index).setVisible(flag); 96 } 97 98 public boolean isPathVisible(int index) { 99 return getPath(index).isVisible(); 100 } 101 102 public void makeSureCurrentPathIsVisible() { 103 if(getCurrentPath().isVisible()) 104 return; 105 106 for (Iterator <GPath> iterator = graphicPaths.iterator(); iterator.hasNext();) { 107 GPath path = iterator.next(); 108 if(path.isVisible()) { 109 setSelectedPath(graphicPaths.indexOf(path)); 110 break; 111 } 112 } 113 } 114 115 public void selectPreviousPath() { 116 setSelectedPath(selectedPathIndex-1); 117 } 118 119 public void selectNextPath() { 120 setSelectedPath(selectedPathIndex+1); 121 } 122 123 public GPath getCurrentPath() { 124 if(graphicPaths.size() > 0) 125 return graphicPaths.get(selectedPathIndex); 126 else 127 return null; 128 } 129 130 public int getSelectedPathIndex() { 131 return selectedPathIndex; 132 } 133 134 public void setContext(GContext context) { 135 super.setContext(context); 136 for (Iterator <GPath> iterator = graphicPaths.iterator(); iterator.hasNext();) { 137 GPath path = iterator.next(); 138 path.setContext(context); 139 } 140 141 timer.start(); 144 } 145 146 public void toggleShowRuleLinks() { 147 showRuleLinks = !showRuleLinks; 148 updateShowRuleLinks(); 149 } 150 151 public void updateShowRuleLinks() { 152 for (Iterator <GPath> iterator = graphicPaths.iterator(); iterator.hasNext();) { 153 GPath path = iterator.next(); 154 path.setShowRuleLinks(showRuleLinks); 155 } 156 } 157 158 public void selectPath(Point p) { 159 List <GPath> paths = getPathsAtPoint(p); 160 if(paths.isEmpty()) 161 return; 162 163 GPath selectPath = paths.get(0); 164 if(paths.size()>1) { 165 int i = 1; 166 while(!selectPath.isVisible() && i<paths.size()) { 167 selectPath = paths.get(i++); 168 } 169 } 170 171 setSelectedPath(graphicPaths.indexOf(selectPath)); 172 context.repaint(); 173 } 174 175 public List <GPath> getPathsAtPoint(Point p) { 176 List <GPath> paths = new ArrayList <GPath>(); 177 for (Iterator <GPath> iterator = graphicPaths.iterator(); iterator.hasNext();) { 178 GPath path = iterator.next(); 179 if(path.containsPoint(p)) 180 paths.add(path); 181 } 182 return paths; 183 } 184 185 public void draw() { 186 GPath currentPath = getCurrentPath(); 187 188 for (Iterator <GPath> iterator = graphicPaths.iterator(); iterator.hasNext();) { 189 GPath path = iterator.next(); 190 if(path != currentPath) { 191 path.deselectElement(); 192 } else { 193 currentPath.selectElement(); 194 } 195 196 if(path.isVisible() && path != currentPath) 197 path.draw(DEFAULT_PATH_WIDTH, null); 198 } 199 200 if(currentPath.isVisible()) 201 currentPath.draw(DEFAULT_PATH_WIDTH, null); 202 } 203 204 public void drawSelectedElement() { 205 if(getCurrentPath().isVisible() && getCurrentPath().isSelectable()) 206 getCurrentPath().drawSelectedElement(); 207 } 208 209 public void timerFired(GTimer timer) { 210 if(getCurrentPath() == null || context == null || !getCurrentPath().isSelectable()) 211 return; 212 213 getCurrentPath().incrementWidth(); 214 context.repaint(); 215 } 216 217 } 218 | Popular Tags |