Search in sources :

Example 1 with WindowListener

use of java.awt.event.WindowListener in project antlr4 by antlr.

the class TreeViewer method showInDialog.

protected static JFrame showInDialog(final TreeViewer viewer) {
    final JFrame dialog = new JFrame();
    dialog.setTitle("Parse Tree Inspector");
    final Preferences prefs = Preferences.userNodeForPackage(TreeViewer.class);
    // Make new content panes
    final Container mainPane = new JPanel(new BorderLayout(5, 5));
    final Container contentPane = new JPanel(new BorderLayout(0, 0));
    contentPane.setBackground(Color.white);
    // Wrap viewer in scroll pane
    JScrollPane scrollPane = new JScrollPane(viewer);
    // Make the scrollpane (containing the viewer) the center component
    contentPane.add(scrollPane, BorderLayout.CENTER);
    JPanel wrapper = new JPanel(new FlowLayout());
    // Add button to bottom
    JPanel bottomPanel = new JPanel(new BorderLayout(0, 0));
    contentPane.add(bottomPanel, BorderLayout.SOUTH);
    JButton ok = new JButton("OK");
    ok.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
        }
    });
    wrapper.add(ok);
    // Add an export-to-png button right of the "OK" button
    JButton png = new JButton("Export as PNG");
    png.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            generatePNGFile(viewer, dialog);
        }
    });
    wrapper.add(png);
    bottomPanel.add(wrapper, BorderLayout.SOUTH);
    // Add scale slider
    double lastKnownViewerScale = prefs.getDouble(DIALOG_VIEWER_SCALE_PREFS_KEY, viewer.getScale());
    viewer.setScale(lastKnownViewerScale);
    int sliderValue = (int) ((lastKnownViewerScale - 1.0) * 1000);
    final JSlider scaleSlider = new JSlider(JSlider.HORIZONTAL, -999, 1000, sliderValue);
    scaleSlider.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            int v = scaleSlider.getValue();
            viewer.setScale(v / 1000.0 + 1.0);
        }
    });
    bottomPanel.add(scaleSlider, BorderLayout.CENTER);
    // Add a JTree representing the parser tree of the input.
    JPanel treePanel = new JPanel(new BorderLayout(5, 5));
    // An "empty" icon that will be used for the JTree's nodes.
    Icon empty = new EmptyIcon();
    UIManager.put("Tree.closedIcon", empty);
    UIManager.put("Tree.openIcon", empty);
    UIManager.put("Tree.leafIcon", empty);
    Tree parseTreeRoot = viewer.getTree().getRoot();
    TreeNodeWrapper nodeRoot = new TreeNodeWrapper(parseTreeRoot, viewer);
    fillTree(nodeRoot, parseTreeRoot, viewer);
    final JTree tree = new JTree(nodeRoot);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(TreeSelectionEvent e) {
            JTree selectedTree = (JTree) e.getSource();
            TreePath path = selectedTree.getSelectionPath();
            if (path != null) {
                TreeNodeWrapper treeNode = (TreeNodeWrapper) path.getLastPathComponent();
                // Set the clicked AST.
                viewer.setTree((Tree) treeNode.getUserObject());
            }
        }
    });
    treePanel.add(new JScrollPane(tree));
    // Create the pane for both the JTree and the AST
    final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treePanel, contentPane);
    mainPane.add(splitPane, BorderLayout.CENTER);
    dialog.setContentPane(mainPane);
    // make viz
    WindowListener exitListener = new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            prefs.putInt(DIALOG_WIDTH_PREFS_KEY, (int) dialog.getSize().getWidth());
            prefs.putInt(DIALOG_HEIGHT_PREFS_KEY, (int) dialog.getSize().getHeight());
            prefs.putDouble(DIALOG_X_PREFS_KEY, dialog.getLocationOnScreen().getX());
            prefs.putDouble(DIALOG_Y_PREFS_KEY, dialog.getLocationOnScreen().getY());
            prefs.putInt(DIALOG_DIVIDER_LOC_PREFS_KEY, splitPane.getDividerLocation());
            prefs.putDouble(DIALOG_VIEWER_SCALE_PREFS_KEY, viewer.getScale());
            dialog.setVisible(false);
            dialog.dispose();
        }
    };
    dialog.addWindowListener(exitListener);
    dialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    int width = prefs.getInt(DIALOG_WIDTH_PREFS_KEY, 600);
    int height = prefs.getInt(DIALOG_HEIGHT_PREFS_KEY, 500);
    dialog.setPreferredSize(new Dimension(width, height));
    dialog.pack();
    // After pack(): set the divider at 1/3 (200/600) of the frame.
    int dividerLocation = prefs.getInt(DIALOG_DIVIDER_LOC_PREFS_KEY, 200);
    splitPane.setDividerLocation(dividerLocation);
    if (prefs.getDouble(DIALOG_X_PREFS_KEY, -1) != -1) {
        dialog.setLocation((int) prefs.getDouble(DIALOG_X_PREFS_KEY, 100), (int) prefs.getDouble(DIALOG_Y_PREFS_KEY, 100));
    } else {
        dialog.setLocationRelativeTo(null);
    }
    dialog.setVisible(true);
    return dialog;
}
Also used : ActionEvent(java.awt.event.ActionEvent) WindowAdapter(java.awt.event.WindowAdapter) TreeSelectionListener(javax.swing.event.TreeSelectionListener) Tree(org.antlr.v4.runtime.tree.Tree) ChangeListener(javax.swing.event.ChangeListener) Preferences(java.util.prefs.Preferences) WindowListener(java.awt.event.WindowListener) ActionListener(java.awt.event.ActionListener) ChangeEvent(javax.swing.event.ChangeEvent) TreePath(javax.swing.tree.TreePath) WindowEvent(java.awt.event.WindowEvent) TreeSelectionEvent(javax.swing.event.TreeSelectionEvent)

Example 2 with WindowListener

use of java.awt.event.WindowListener in project android by JetBrains.

the class WelcomeScreenWindowListener method install.

public static WindowListener install(@NotNull JFrame frame, @NotNull FirstRunWizardHost host) {
    WindowListener ideaListener = removeCloseListener(frame);
    // This code is a hack to replace IntelliJ window listener with ours.
    // That listener is an instance of anonymous class and our hack will stop working if
    // that class is removed or renamed. "DirectListener" is a fallback in case we don't
    // find an original listener.
    WindowListener ourListener = ideaListener != null ? new DelegatingListener(host, ideaListener) : new DirectListener(host);
    frame.addWindowListener(ourListener);
    return ourListener;
}
Also used : WindowListener(java.awt.event.WindowListener)

Example 3 with WindowListener

use of java.awt.event.WindowListener in project JMRI by JMRI.

the class VSDecoderPane method setDecoder.

/**
     * setDecoder()
     *
     * set the Decoder ID and update the soundsPanel
     */
public void setDecoder(VSDecoder dec) {
    if (dec != null) {
        // Store the new decoder
        decoder_id = dec.getID();
        log.debug("Decoder ID = " + decoder_id + " Decoder = " + dec);
        // Register the decoder as a listener on our frame... so it can react
        // to the window closing
        parent.addWindowListener(new WindowListener() {

            @Override
            public void windowActivated(WindowEvent e) {
            }

            @Override
            public void windowClosed(WindowEvent e) {
            }

            @Override
            public void windowClosing(WindowEvent e) {
                VSDecoderManager.instance().getVSDecoderByID(decoder_id).windowChange(e);
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
            }

            @Override
            public void windowDeiconified(WindowEvent e) {
            }

            @Override
            public void windowIconified(WindowEvent e) {
            }

            @Override
            public void windowOpened(WindowEvent e) {
            }
        });
        // Register ourselves as an event listener to the decoder
        dec.addEventListener(new VSDecoderListener() {

            @Override
            public void eventAction(VSDecoderEvent e) {
                decoderEventAction(e);
            }
        });
        // Update the status bar
        if (dec.getPosition() != null) {
            statusBar.setMessage("Location: " + dec.getPosition().toString());
        } else {
            statusBar.setMessage("Location: unknown");
        }
        // Update the sounds pane
        tabbedPane.remove(soundsPanel);
        soundsPanel = new VSDSoundsPanel(decoder_id, this);
        tabbedPane.addTab("Sounds", soundsPanel);
        tabbedPane.revalidate();
        tabbedPane.repaint();
    }
}
Also used : WindowListener(java.awt.event.WindowListener) WindowEvent(java.awt.event.WindowEvent)

Example 4 with WindowListener

use of java.awt.event.WindowListener in project JMRI by JMRI.

the class RpsPositionIconTest method tearDown.

@Override
protected void tearDown() {
    // now close panel window
    if (panel != null) {
        java.awt.event.WindowListener[] listeners = panel.getTargetFrame().getWindowListeners();
        for (WindowListener listener : listeners) {
            panel.getTargetFrame().removeWindowListener(listener);
        }
        junit.extensions.jfcunit.TestHelper.disposeWindow(panel.getTargetFrame(), this);
    }
    JUnitUtil.resetInstanceManager();
    apps.tests.Log4JFixture.tearDown();
}
Also used : WindowListener(java.awt.event.WindowListener)

Example 5 with WindowListener

use of java.awt.event.WindowListener in project JMRI by JMRI.

the class TurnoutIconTest method tearDown.

@Override
protected void tearDown() {
    // now close panel window
    if (panel != null) {
        java.awt.event.WindowListener[] listeners = panel.getTargetFrame().getWindowListeners();
        for (WindowListener listener : listeners) {
            panel.getTargetFrame().removeWindowListener(listener);
        }
        junit.extensions.jfcunit.TestHelper.disposeWindow(panel.getTargetFrame(), this);
    }
    apps.tests.Log4JFixture.tearDown();
}
Also used : WindowListener(java.awt.event.WindowListener)

Aggregations

WindowListener (java.awt.event.WindowListener)18 WindowEvent (java.awt.event.WindowEvent)7 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 JFrame (javax.swing.JFrame)2 BorderLayout (java.awt.BorderLayout)1 Component (java.awt.Component)1 Dialog (java.awt.Dialog)1 Frame (java.awt.Frame)1 GridBagConstraints (java.awt.GridBagConstraints)1 GridBagLayout (java.awt.GridBagLayout)1 Insets (java.awt.Insets)1 Window (java.awt.Window)1 WindowAdapter (java.awt.event.WindowAdapter)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Preferences (java.util.prefs.Preferences)1 JComponent (javax.swing.JComponent)1 JFileChooser (javax.swing.JFileChooser)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1