Search in sources :

Example 1 with GGraphGroup

use of org.antlr.works.visualization.graphics.graph.GGraphGroup in project antlrworks by antlr.

the class GFactory method buildGraphGroup.

private GGraphGroup buildGraphGroup(Grammar grammar, GrammarError error) {
    // Create one GGraph for each error rules
    List<GGraph> graphs = new ArrayList<GGraph>();
    FAFactory factory = new FAFactory(grammar);
    for (String rule : error.rules) {
        NFAState startState = grammar.getRuleStartState(rule);
        FAState state = factory.buildNFA(startState, optimize);
        GGraph graph = renderer.render(state);
        graph.setName(rule);
        graphs.add(graph);
    }
    // Add only graphs that are referenced by at least one error path.
    // For example, the statement rule of the java.g grammar produces
    // states that do not exist in the graph (they are after the accepted state
    // and are ignored by the FAFactory)
    GGraphGroup gg = new GGraphGroup();
    for (GGraph graph : graphs) {
        if (graph.containsAtLeastOneState(error.states))
            gg.add(graph);
    }
    // Attach all error paths to the GGraphGroup
    for (int i = 0; i < error.paths.size(); i++) {
        List states = error.paths.get(i);
        Boolean disabled = error.pathsDisabled.get(i);
        try {
            gg.addPath(states, disabled, factory.getSkippedStatesMap());
        } catch (Exception e) {
            if (console == null)
                e.printStackTrace();
            else
                console.println(e);
        }
    }
    // Attach all unreacheable alts to the GGraphGroup
    for (Object[] unreachableAlt : error.unreachableAlts) {
        gg.addUnreachableAlt((NFAState) unreachableAlt[0], (Integer) unreachableAlt[1]);
    }
    if (error.paths.size() > 0)
        gg.getPathGroup().setPathVisible(0, true);
    return gg;
}
Also used : GGraph(org.antlr.works.visualization.graphics.graph.GGraph) ArrayList(java.util.ArrayList) FAFactory(org.antlr.works.visualization.fa.FAFactory) NFAState(org.antlr.analysis.NFAState) FAState(org.antlr.works.visualization.fa.FAState) NFAState(org.antlr.analysis.NFAState) List(java.util.List) ArrayList(java.util.ArrayList) GGraphGroup(org.antlr.works.visualization.graphics.graph.GGraphGroup)

Example 2 with GGraphGroup

use of org.antlr.works.visualization.graphics.graph.GGraphGroup in project antlrworks by antlr.

the class GPanel method createPathSelectionButtons.

private void createPathSelectionButtons() {
    pathButtonSelectionBox.removeAll();
    if (!(view.getCurrentGraph() instanceof GGraphGroup))
        return;
    GGraphGroup gg = (GGraphGroup) view.getCurrentGraph();
    int count = gg.getPathGroup().getNumberOfPaths();
    if (count <= 1)
        pathButtonSelectionBox.add(new JLabel("Alternative:"));
    else
        pathButtonSelectionBox.add(new JLabel("Alternatives:"));
    for (int i = 0; i < count; i++) {
        pathButtonSelectionBox.add(createPathSelectionButton(i));
    }
}
Also used : GGraphGroup(org.antlr.works.visualization.graphics.graph.GGraphGroup)

Example 3 with GGraphGroup

use of org.antlr.works.visualization.graphics.graph.GGraphGroup in project antlrworks by antlr.

the class GView method paintComponent.

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    ATEUtilities.prepareForText(g);
    if (!canDraw()) {
        paintPlaceholder(g);
        return;
    }
    int width = getPaintWidth();
    int height = getPaintHeight();
    if (useCachedImage) {
        boolean sizeChanged = cachedImage != null && (cachedImage.getWidth() != width || cachedImage.getHeight() != height);
        if (sizeChanged) {
            // instead of re-creating a new one (useful for fast live resize).
            if (!cachedImageResize && cachedImage != null) {
                cachedImage.flush();
                cachedImage = null;
            }
        }
        if (cachedImage == null) {
            // Create a new cache image.
            cachedImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
            Graphics2D gCache = (Graphics2D) cachedImage.getGraphics();
            ATEUtilities.prepareForText(gCache);
            gCache.setColor(Color.white);
            gCache.fillRect(0, 0, width, height);
            render(gCache);
            gCache.dispose();
        } else if (cachedImageRerender) {
            // Only render the cachedImage without re-creating it again
            Graphics2D gCache = (Graphics2D) cachedImage.getGraphics();
            ATEUtilities.prepareForText(gCache);
            gCache.setColor(Color.white);
            gCache.fillRect(0, 0, width, height);
            render(gCache);
            gCache.dispose();
            cachedImageRerender = false;
        }
    }
    if (cachedImage == null)
        render((Graphics2D) g);
    else
        g.drawImage(cachedImage, 0, 0, width, height, null);
    if (!cachedImageResize && getCurrentGraph() instanceof GGraphGroup) {
        // Draw the selected segment of a path (and only if we are not resizing using only the cached image)
        Graphics2D g2d = (Graphics2D) g;
        context.offsetX = offset_x;
        context.offsetY = offset_y;
        context.setGraphics2D(g2d);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        getCurrentPathGroup().drawSelectedElement();
    }
}
Also used : BufferedImage(java.awt.image.BufferedImage) GGraphGroup(org.antlr.works.visualization.graphics.graph.GGraphGroup)

Example 4 with GGraphGroup

use of org.antlr.works.visualization.graphics.graph.GGraphGroup in project antlrworks by antlr.

the class GPanel method createPathSelectionButton.

private JCheckBox createPathSelectionButton(int pathIndex) {
    JCheckBox button = new JCheckBox(String.valueOf(pathIndex + 1));
    button.setName(String.valueOf(pathIndex));
    button.setFocusable(false);
    button.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            JCheckBox button = (JCheckBox) event.getSource();
            GGraphGroup gg = (GGraphGroup) view.getCurrentGraph();
            gg.getPathGroup().setPathVisible(Integer.parseInt(button.getName()), button.isSelected());
            gg.getPathGroup().makeSureCurrentPathIsVisible();
            view.cacheRerender();
            view.repaint();
        }
    });
    GGraphGroup gg = (GGraphGroup) view.getCurrentGraph();
    button.setSelected(gg.getPathGroup().isPathVisible(pathIndex));
    return button;
}
Also used : ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) GGraphGroup(org.antlr.works.visualization.graphics.graph.GGraphGroup)

Example 5 with GGraphGroup

use of org.antlr.works.visualization.graphics.graph.GGraphGroup in project antlrworks by antlr.

the class GPanel method createShowCrossLinksButton.

private JToggleButton createShowCrossLinksButton() {
    XJRollOverButtonToggle button = XJRollOverButtonToggle.createMediumButton(IconManager.shared().getIconShowLinks());
    button.setSelected(true);
    button.setFocusable(false);
    button.setToolTipText("Show links between rules");
    button.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            for (Object o : view.getGraphs()) {
                GGraphGroup gg = (GGraphGroup) o;
                gg.getPathGroup().toggleShowRuleLinks();
            }
            view.cacheRerender();
            view.repaint();
        }
    });
    return button;
}
Also used : XJRollOverButtonToggle(org.antlr.xjlib.appkit.swing.XJRollOverButtonToggle) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) GGraphGroup(org.antlr.works.visualization.graphics.graph.GGraphGroup)

Aggregations

GGraphGroup (org.antlr.works.visualization.graphics.graph.GGraphGroup)5 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 BufferedImage (java.awt.image.BufferedImage)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 NFAState (org.antlr.analysis.NFAState)1 FAFactory (org.antlr.works.visualization.fa.FAFactory)1 FAState (org.antlr.works.visualization.fa.FAState)1 GGraph (org.antlr.works.visualization.graphics.graph.GGraph)1 XJRollOverButtonToggle (org.antlr.xjlib.appkit.swing.XJRollOverButtonToggle)1