Search in sources :

Example 1 with GElement

use of org.antlr.xjlib.appkit.gview.object.GElement in project antlrworks by antlr.

the class GDOTImporter method generateGraph.

public GElement generateGraph(String dotFile) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader(dotFile));
    try {
        graph = null;
        /** The problem here is that DOT sometime inserts a '\' at the end of a long
             * line so we have to skip it and continue to parse until a "real" EOL is reached.
             * Example:
             * 	statement -> compoundStatement [pos="e,3264,507 3271,2417 3293,2392 ... 3237,565 3234,560 32\
             39,545 3243,534 3249,523 3257,514"];
             */
        StringBuilder line = new StringBuilder();
        // current character
        int c;
        // previous character
        int pc = -1;
        while ((c = br.read()) != -1) {
            if (c == '\n') {
                if (pc == '\\') {
                    // Remove the last \ if it was part of the DOT wrapping character
                    line.deleteCharAt(line.length() - 1);
                } else {
                    GElement element = parseLine(line.toString());
                    if (element != null) {
                        if (graph == null)
                            graph = element;
                        else
                            graph.addElement(element);
                    }
                    line.delete(0, line.length());
                }
            } else if (c != '\r') {
                line.append((char) c);
            } else if (c == '\r') {
                continue;
            }
            pc = c;
        }
    } finally {
        br.close();
    }
    return graph;
}
Also used : GElement(org.antlr.xjlib.appkit.gview.object.GElement)

Example 2 with GElement

use of org.antlr.xjlib.appkit.gview.object.GElement in project antlrworks by antlr.

the class GDOTImporterDOT method createGraphEdge.

public GElement createGraphEdge(String[] tokens) throws IOException {
    //  DOT -> foo_bar [pos="e,65,36 65,72 65,64 65,55 65,46"];
    //   0  12     3   4 5 6               7                 8 9
    //  DOT -> A [label=foo, pos="e,153,33 119,88 124,78 129,65 136,54 139,49 142,45 146,40", lp="146,62"];
    //   0  12 3 4 5   6 7 8  9 10     11
    String sourceName = tokens[0];
    String targetName = tokens[3];
    String labelName = null;
    Vector2D labelPosition = null;
    Vector2D[] points = null;
    int index = 4;
    while (index < tokens.length - 1) {
        index++;
        if (tokens[index].equals("label")) {
            // Label name
            labelName = tokens[index += 2];
        } else if (tokens[index].equals("lp")) {
            // Label position
            String[] lpos = parseTokens(tokens[index += 2]);
            labelPosition = new Vector2D(Float.parseFloat(lpos[0]), height - Float.parseFloat(lpos[2]));
        } else if (tokens[index].equals("pos")) {
            // Edge control points
            points = parseControlPoints(tokens[index += 2]);
        } else if (tokens[index].equals(";"))
            break;
    }
    GElement source = graph.findElementWithLabel(sourceName);
    GElement target = graph.findElementWithLabel(targetName);
    GLink link = new GLink(source, GElementCircle.ANCHOR_CENTER, target, GElementCircle.ANCHOR_CENTER, GLink.SHAPE_BEZIER, labelName, 0);
    if (points == null) {
        System.err.println("No points for " + sourceName + ", " + targetName + ", " + tokens.length);
        for (int i = 0; i < tokens.length; i++) {
            String token = tokens[i];
            System.out.println(token);
        }
    }
    link.setBezierControlPoints(points);
    link.setBezierLabelPosition(labelPosition);
    return link;
}
Also used : Vector2D(org.antlr.xjlib.appkit.gview.base.Vector2D) GElement(org.antlr.xjlib.appkit.gview.object.GElement) GLink(org.antlr.xjlib.appkit.gview.object.GLink)

Example 3 with GElement

use of org.antlr.xjlib.appkit.gview.object.GElement in project antlrworks by antlr.

the class GDOTImporterPlain method createGraphEdge.

public GElement createGraphEdge(String[] tokens) {
    //  0    1     2  3 4
    //  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
    //  g 1.194 8.194 solid black
    //  edge rule foo 4 0.375 1.000 0.375 0.889 0.375 0.764 0.375 0.639 solid black
    int controlCount = (int) Float.parseFloat(tokens[3]);
    Vector2D[] points = new Vector2D[controlCount];
    for (int index = 0; index < controlCount; index++) {
        points[index] = new Vector2D(Float.parseFloat(tokens[4 + index * 2]) * factor, (height - Float.parseFloat(tokens[4 + index * 2 + 1])) * factor);
    }
    int labelIndex = 3 + 2 * controlCount + 1;
    String label = null;
    Vector2D labelPosition = null;
    if (isFloatString(tokens[labelIndex + 1])) {
        // Apparently there is a label because there is a float coordinate
        label = tokens[labelIndex];
        labelPosition = new Vector2D(Float.parseFloat(tokens[labelIndex + 1]) * factor, (height - Float.parseFloat(tokens[labelIndex + 2])) * factor);
    }
    GElement source = graph.findElementWithLabel(tokens[1]);
    GElement target = graph.findElementWithLabel(tokens[2]);
    GLink link = new GLink(source, GElementCircle.ANCHOR_CENTER, target, GElementCircle.ANCHOR_CENTER, GLink.SHAPE_BEZIER, label, 0);
    link.setBezierControlPoints(points);
    link.setBezierLabelPosition(labelPosition);
    return link;
}
Also used : Vector2D(org.antlr.xjlib.appkit.gview.base.Vector2D) GElement(org.antlr.xjlib.appkit.gview.object.GElement) GLink(org.antlr.xjlib.appkit.gview.object.GLink)

Example 4 with GElement

use of org.antlr.xjlib.appkit.gview.object.GElement in project antlrworks by antlr.

the class AWTreeGraphView method rebuildNoModel.

/** This method rebuild the tree completely. This can be expensive if the tree
     * contains many node. Use a AWTreeModel instead (see rebuildWithModel)
     */
public void rebuildNoModel() {
    clearMaps();
    GElement element = buildGraph(null);
    element.move(MARGIN, MARGIN);
    setSizeMargin(MARGIN);
    setRootElement(element);
}
Also used : GElement(org.antlr.xjlib.appkit.gview.object.GElement)

Example 5 with GElement

use of org.antlr.xjlib.appkit.gview.object.GElement in project antlrworks by antlr.

the class GEventCreateLinkElement method mousePressed.

public void mousePressed(MouseEvent e, Point mousePosition) {
    GElement selectedElement = delegate.eventQueryElementAtPoint(mousePosition);
    if (startElement != null) {
        if (selectedElement != null) {
            int type;
            if (linkElement instanceof SLinkElbow)
                type = GLink.SHAPE_ELBOW;
            else
                type = GLink.SHAPE_ARC;
            delegate.eventCreateLink(startElement, startAnchorKey, selectedElement, selectedElement.getAnchorKeyClosestToPoint(mousePosition), type, mousePosition);
        }
        removeExclusiveValue(GEventManager.EXCLUSIVE_CREATE_LINK_VALUE);
        startElement = null;
        linkElement = null;
        delegate.eventShouldRepaint();
        return;
    }
    if (selectedElement == null || !selectedElement.acceptOutgoingLink()) {
        return;
    }
    int mask = InputEvent.SHIFT_DOWN_MASK + InputEvent.BUTTON1_DOWN_MASK;
    if ((e.getModifiersEx() & mask) == mask || delegate.eventCanCreateLink()) {
        startElement = selectedElement;
        startAnchorKey = startElement.getAnchorKeyClosestToPoint(mousePosition);
        linkArc = new SLinkArc();
        linkArc.setStartTangentOffset(startElement.getDefaultAnchorOffset(startAnchorKey));
        linkElbow = new SLinkElbow();
        if (view.defaultLinkShape() == GLink.SHAPE_ARC)
            linkElement = linkArc;
        else
            linkElement = linkElbow;
        linkElement.setFlateness(delegate.eventLinkFlateness());
        addExclusiveValue(GEventManager.EXCLUSIVE_CREATE_LINK_VALUE);
    }
}
Also used : SLinkArc(org.antlr.xjlib.appkit.gview.shape.SLinkArc) SLinkElbow(org.antlr.xjlib.appkit.gview.shape.SLinkElbow) GElement(org.antlr.xjlib.appkit.gview.object.GElement)

Aggregations

GElement (org.antlr.xjlib.appkit.gview.object.GElement)7 GLink (org.antlr.xjlib.appkit.gview.object.GLink)3 Vector2D (org.antlr.xjlib.appkit.gview.base.Vector2D)2 Anchor2D (org.antlr.xjlib.appkit.gview.base.Anchor2D)1 Rect (org.antlr.xjlib.appkit.gview.base.Rect)1 SLinkArc (org.antlr.xjlib.appkit.gview.shape.SLinkArc)1 SLinkElbow (org.antlr.xjlib.appkit.gview.shape.SLinkElbow)1