1 package org.antlr.xjlib.appkit.gview.utils; 2 3 import org.antlr.xjlib.appkit.gview.base.Vector2D; 4 import org.antlr.xjlib.appkit.gview.object.GElement; 5 import org.antlr.xjlib.appkit.gview.object.GElementCircle; 6 import org.antlr.xjlib.appkit.gview.object.GLink; 7 8 import java.io.IOException ; 9 import java.util.ArrayList ; 10 import java.util.List ; 11 41 42 public class GDOTImporterDOT extends GDOTImporter { 43 44 public static final int INCH_TO_PIXEL = 72; 45 46 67 68 public GElement parseLine(String line) throws IOException { 69 String [] tokens = parseTokens(line); 70 if(tokens[0].equals("graph")) 71 parseGraphHeight(tokens); 72 else if(tokens.length >= 2 && !tokens[0].equals("node") && tokens[1].equals("[")) 73 return createGraphNode(tokens); 74 else if(tokens.length >= 3 && tokens[1].equals("-") && tokens[2].equals(">")) 75 return createGraphEdge(tokens); 76 77 return null; 78 } 79 80 public void parseGraphHeight(String [] tokens) throws IOException { 81 if(tokens[2].equals("bb")) { 83 String [] t = parseTokens(tokens[4]); 84 height = Float.parseFloat(t[6]); 85 } 86 } 87 88 public GElement createGraphNode(String [] tokens) throws IOException { 89 93 float x = 0; 94 float y = 0; 95 float w = 0; 96 float h = 0; 97 boolean doublecircle = false; 98 99 int index = 0; 100 while(index < tokens.length-1) { 101 index++; 102 if(tokens[index].equals("pos")) { 103 String [] posTokens = parseTokens(tokens[index+=2]); 104 x = Float.parseFloat(posTokens[0]); 105 y = (height - Float.parseFloat(posTokens[2])); 106 } else if(tokens[index].equals("width")) { 107 w = Float.parseFloat(tokens[index+=2])*INCH_TO_PIXEL; 108 } else if(tokens[index].equals("height")) { 109 h = Float.parseFloat(tokens[index+=2])*INCH_TO_PIXEL; 110 } else if(tokens[index].equals("shape")) { 111 doublecircle = tokens[index+=2].equals("doublecircle"); 112 } 113 } 114 115 Node node = new Node(); 116 node.setDraggable(true); 117 node.setPosition(x, y); 118 node.setSize(w, h); 119 node.setRadius(w/2); 120 node.setLabel(tokens[0]); 121 node.setDouble(doublecircle); 122 123 return node; 124 } 125 126 public GElement createGraphEdge(String [] tokens) throws IOException { 127 132 String sourceName = tokens[0]; 133 String targetName = tokens[3]; 134 135 String labelName = null; 136 Vector2D labelPosition = null; 137 Vector2D points[] = null; 138 139 int index = 4; 140 while(index < tokens.length-1) { 141 index++; 142 143 if(tokens[index].equals("label")) { 144 labelName = tokens[index+=2]; 146 } else if(tokens[index].equals("lp")) { 147 String [] lpos = parseTokens(tokens[index+=2]); 149 labelPosition = new Vector2D(Float.parseFloat(lpos[0]), height-Float.parseFloat(lpos[2])); 150 } else if(tokens[index].equals("pos")) { 151 points = parseControlPoints(tokens[index+=2]); 153 } else if(tokens[index].equals(";")) 154 break; 155 } 156 157 GElement source = graph.findElementWithLabel(sourceName); 158 GElement target = graph.findElementWithLabel(targetName); 159 160 GLink link = new GLink(source, GElementCircle.ANCHOR_CENTER, 161 target, GElementCircle.ANCHOR_CENTER, 162 GLink.SHAPE_BEZIER, labelName, 0); 163 164 if(points == null) { 165 System.err.println("No points for "+sourceName+", "+targetName+", "+tokens.length); 166 for (int i = 0; i < tokens.length; i++) { 167 String token = tokens[i]; 168 System.out.println(token); 169 } 170 } 171 172 link.setBezierControlPoints(points); 173 link.setBezierLabelPosition(labelPosition); 174 175 return link; 176 } 177 178 public Vector2D[] parseControlPoints(String s) throws IOException { 179 List <Vector2D> points = new ArrayList <Vector2D>(); 181 Vector2D endPoint = null; 182 183 String [] pts = parseTokens(s); 184 int index = -1; 185 while(index < pts.length-1) { 186 index++; 187 188 if(pts[index].equals("e")) { 189 String x = pts[index+=2]; 191 String y = pts[index+=2]; 192 endPoint = new Vector2D(Float.parseFloat(x), height-Float.parseFloat(y)); 193 } else if(isFloatString(pts[index]) && isFloatString(pts[index+2])) { 194 String x = pts[index]; 196 String y = pts[index+=2]; 197 points.add(new Vector2D(Float.parseFloat(x), height-Float.parseFloat(y))); 198 } 199 } 200 201 if(endPoint != null) 202 points.add(endPoint); 203 204 Vector2D p[] = new Vector2D[points.size()]; 205 for(int i=0; i<points.size(); i++) { 206 p[i] = points.get(i); 207 } 208 return p; 209 } 210 } 211 | Popular Tags |