KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > gview > utils > GDOTImporterPlain


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 JavaDoc;
9 /*
10
11 [The "BSD licence"]
12 Copyright (c) 2005-2006 Jean Bovet
13 All rights reserved.
14
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions
17 are met:
18
19 1. Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
21 2. Redistributions in binary form must reproduce the above copyright
22 notice, this list of conditions and the following disclaimer in the
23 documentation and/or other materials provided with the distribution.
24 3. The name of the author may not be used to endorse or promote products
25 derived from this software without specific prior written permission.
26
27 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
38 */

39
40 public class GDOTImporterPlain extends GDOTImporter {
41
42     public static int factor = 80;
43
44     public GElement parseLine(String JavaDoc line) throws IOException JavaDoc {
45         /*
46             graph 1.000 2.583 9.056
47         */

48
49         String JavaDoc[] tokens = parseTokens(line);
50         if(tokens[0].equals("graph"))
51             height = Float.parseFloat(tokens[3]);
52         else if(tokens[0].equals("node"))
53             return createGraphNode(tokens);
54         else if(tokens[0].equals("edge"))
55             return createGraphEdge(tokens);
56 // else if(tokens[0].equals("stop"))
57

58         return null;
59     }
60
61     public GElement createGraphNode(String JavaDoc[] tokens) {
62         /*
63 node s0 1.097 1.917 0.506 0.506 s0 solid circle black lightgrey
64 node "s1=>2" 0.486 0.486 0.969 0.969 "s1=>2" solid doublecircle black lightgrey
65         */

66
67         float x = Float.parseFloat(tokens[2])*factor;
68         float y = (height - Float.parseFloat(tokens[3]))*factor;
69         float w = Float.parseFloat(tokens[4])*factor;
70         float h = Float.parseFloat(tokens[5])*factor;
71
72         Node node = new Node();
73         node.setDraggable(true);
74         node.setPosition(x, y);
75         node.setSize(w, h);
76         node.setRadius(w/2);
77         node.setLabel(tokens[6]);
78         node.setDouble(tokens[8].equals("doublecircle"));
79
80         return node;
81     }
82
83     public GElement createGraphEdge(String JavaDoc[] tokens) {
84 // 0 1 2 3 4
85
// edge start n1 7 1.153 8.556 1.125 8.417 1.097 8.236 1.111 8.083 1.111 8.042 1.125 8.014 1.125 7.972
86
// g 1.194 8.194 solid black
87
// edge rule foo 4 0.375 1.000 0.375 0.889 0.375 0.764 0.375 0.639 solid black
88

89         int controlCount = (int) Float.parseFloat(tokens[3]);
90         Vector2D points[] = new Vector2D[controlCount];
91         for(int index=0; index<controlCount; index++) {
92             points[index] = new Vector2D(Float.parseFloat(tokens[4+index*2])*factor,
93                     (height-Float.parseFloat(tokens[4+index*2+1]))*factor);
94         }
95
96         int labelIndex = 3+2*controlCount+1;
97         String JavaDoc label = null;
98         Vector2D labelPosition = null;
99         if(isFloatString(tokens[labelIndex+1])) {
100             // Apparently there is a label because there is a float coordinate
101
label = tokens[labelIndex];
102             labelPosition = new Vector2D(Float.parseFloat(tokens[labelIndex+1])*factor,
103                                 (height-Float.parseFloat(tokens[labelIndex+2]))*factor);
104         }
105
106         GElement source = graph.findElementWithLabel(tokens[1]);
107         GElement target = graph.findElementWithLabel(tokens[2]);
108
109         GLink link = new GLink(source, GElementCircle.ANCHOR_CENTER,
110                 target, GElementCircle.ANCHOR_CENTER,
111                 GLink.SHAPE_BEZIER, label, 0);
112
113         link.setBezierControlPoints(points);
114         link.setBezierLabelPosition(labelPosition);
115
116         return link;
117     }
118
119 }
120
Popular Tags