Example usage for javax.swing JPanel getTopLevelAncestor

List of usage examples for javax.swing JPanel getTopLevelAncestor

Introduction

In this page you can find the example usage for javax.swing JPanel getTopLevelAncestor.

Prototype

@BeanProperty(bound = false)
@SuppressWarnings("deprecation")
public Container getTopLevelAncestor() 

Source Link

Document

Returns the top-level ancestor of this component (either the containing Window or Applet), or null if this component has not been added to any container.

Usage

From source file:com.haulmont.cuba.desktop.sys.DesktopWindowManager.java

protected void showOptionDialog(final String title, final String message, @Nullable MessageType messageType,
        final Icon icon, boolean alwaysModal, final Action[] actions, String debugName) {
    final DialogWindow dialog = new DialogWindow(frame, title);

    if (App.getInstance().isTestMode()) {
        dialog.setName(debugName);/*  w w w .j  a v  a  2s .  com*/
    }
    dialog.setModal(false);

    if (actions.length > 1) {
        dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    }

    int width = 500;
    SizeUnit unit = null;
    DialogParams dialogParams = getDialogParams();
    if (messageType != null && messageType.getWidth() != null) {
        width = messageType.getWidth().intValue();
        unit = messageType.getWidthUnit();
    } else if (dialogParams.getWidth() != null) {
        width = dialogParams.getWidth().intValue();
        unit = dialogParams.getWidthUnit();
    }

    LC lc = new LC();
    lc.insets("10");

    MigLayout layout = new MigLayout(lc);
    final JPanel panel = new JPanel(layout);
    if (icon != null) {
        JLabel iconLabel = new JLabel(icon);
        panel.add(iconLabel, "aligny top");
    }

    JLabel msgLabel = new JLabel(message);

    if (width != AUTO_SIZE_PX) {
        panel.add(msgLabel, "width 100%, wrap, growy 0");
    } else {
        panel.add(msgLabel, "wrap");
    }

    if (icon != null) {
        panel.add(new JLabel(" "));
    }

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            dialog.requestFocus();
        }
    });

    final JPanel buttonsPanel = createButtonsPanel(actions, dialog);
    panel.add(buttonsPanel, "alignx right");

    if (width != AUTO_SIZE_PX) {
        if (unit != null && unit != SizeUnit.PIXELS) {
            throw new UnsupportedOperationException("Dialog size can be set only in pixels");
        }
        dialog.setLayout(new MigLayout(new LC().insets("0").width(width + "px")));
        dialog.setFixedWidth(width);
        dialog.add(panel, "width 100%, growy 0");
    } else {
        dialog.add(panel);
    }

    assignDialogShortcuts(dialog, panel, actions);

    dialog.pack();
    dialog.setResizable(false);
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            panel.revalidate();
            panel.repaint();

            java.awt.Container container = panel.getTopLevelAncestor();
            if (container instanceof JDialog) {
                JDialog dialog = (JDialog) container;
                dialog.pack();
            }
        }
    });
    dialog.setLocationRelativeTo(frame);

    boolean modal = true;
    if (!alwaysModal) {
        if (!hasModalWindow()) {
            if (messageType != null && messageType.getModal() != null) {
                modal = messageType.getModal();
            } else if (dialogParams.getModal() != null) {
                modal = dialogParams.getModal();
            }
        }
    } else {
        if (messageType != null && messageType.getModal() != null) {
            log.warn("MessageType.modal is not supported for showOptionDialog");
        }
    }

    if (modal) {
        DialogWindow lastDialogWindow = getLastDialogWindow();
        if (lastDialogWindow == null) {
            frame.deactivate(null);
        } else {
            lastDialogWindow.disableWindow(null);
        }
    }

    dialog.setVisible(true);
}