Search in sources :

Example 1 with AncestorListener

use of javax.swing.event.AncestorListener in project smile by haifengl.

the class PlotCanvas method initContextMenauAndToolBar.

/**
     * Initialize context menu and toolbar.
     */
private void initContextMenauAndToolBar() {
    toolbar = new JToolBar();
    toolbar.add(new Button(saveAction));
    toolbar.add(new Button(printAction));
    toolbar.addSeparator();
    toolbar.add(new Button(zoomInAction));
    toolbar.add(new Button(zoomOutAction));
    toolbar.add(new Button(resetAction));
    toolbar.addSeparator();
    toolbar.add(new Button(enlargePlotAreaAction));
    toolbar.add(new Button(shrinkPlotAreaAction));
    toolbar.add(new Button(increaseHeightAction));
    toolbar.add(new Button(decreaseHeightAction));
    toolbar.add(new Button(increaseWidthAction));
    toolbar.add(new Button(decreaseWidthAction));
    toolbar.addSeparator();
    toolbar.add(new Button(propertyAction));
    decreaseHeightAction.setEnabled(false);
    decreaseWidthAction.setEnabled(false);
    //Initialize popup menu.
    popup = new JPopupMenu();
    popup.add(new JMenuItem(saveAction));
    popup.add(new JMenuItem(printAction));
    popup.addSeparator();
    popup.add(new JMenuItem(zoomInAction));
    popup.add(new JMenuItem(zoomOutAction));
    popup.add(new JMenuItem(resetAction));
    popup.addSeparator();
    popup.add(new JMenuItem(enlargePlotAreaAction));
    popup.add(new JMenuItem(shrinkPlotAreaAction));
    popup.add(new JMenuItem(increaseHeightAction));
    popup.add(new JMenuItem(decreaseHeightAction));
    popup.add(new JMenuItem(increaseWidthAction));
    popup.add(new JMenuItem(decreaseWidthAction));
    popup.addSeparator();
    popup.add(new JMenuItem(propertyAction));
    AncestorListener ancestorListener = new AncestorListener() {

        @Override
        public void ancestorAdded(AncestorEvent ae) {
            boolean inScrollPane = false;
            Container parent = getParent();
            while (parent != null) {
                if (parent instanceof JScrollPane) {
                    inScrollPane = true;
                    scrollPane = (JScrollPane) parent;
                    break;
                }
                parent = parent.getParent();
            }
            increaseHeightAction.setEnabled(inScrollPane);
            increaseWidthAction.setEnabled(inScrollPane);
        }

        @Override
        public void ancestorRemoved(AncestorEvent ae) {
        }

        @Override
        public void ancestorMoved(AncestorEvent ae) {
        }
    };
    addAncestorListener(ancestorListener);
}
Also used : JScrollPane(javax.swing.JScrollPane) Container(java.awt.Container) Button(smile.swing.Button) JButton(javax.swing.JButton) AncestorListener(javax.swing.event.AncestorListener) JToolBar(javax.swing.JToolBar) JMenuItem(javax.swing.JMenuItem) AncestorEvent(javax.swing.event.AncestorEvent) JPopupMenu(javax.swing.JPopupMenu)

Aggregations

Container (java.awt.Container)1 JButton (javax.swing.JButton)1 JMenuItem (javax.swing.JMenuItem)1 JPopupMenu (javax.swing.JPopupMenu)1 JScrollPane (javax.swing.JScrollPane)1 JToolBar (javax.swing.JToolBar)1 AncestorEvent (javax.swing.event.AncestorEvent)1 AncestorListener (javax.swing.event.AncestorListener)1 Button (smile.swing.Button)1