import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class KDialog extends JDialog {

    protected KDialog(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
    }

    public static JTextField stdTextField(int columns) {
        final JTextField tf = new JTextField(columns) {
            public Dimension getMaximumSize() {
                Dimension max = super.getMaximumSize();
                Dimension prf = getPreferredSize();
                return new Dimension(max.width, prf.height);
            }
        };
        tf.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent event) {
                if (!event.isTemporary()) {
                    tf.selectAll();
                }
            }
        });
        return tf;
    }

    protected void stdLayout(Component widgetBox, JButton[] buttons) {
        Box buttonBox = Box.createHorizontalBox();
        buttonBox.add(Box.createHorizontalGlue());
        for (int i = 0; i < buttons.length; i++) {
            if (i > 0) {
                buttonBox.add(createHorizontalStrut(5));
            }
            buttonBox.add(buttons[i]);
        }
        matchComponentSize(buttons);
        Box mainBox = Box.createVerticalBox();
        mainBox.add(widgetBox);
        mainBox.add(createVerticalStrut(17));
        mainBox.add(buttonBox);
        Container content = getContentPane();
        content.add(createVerticalStrut(13), BorderLayout.NORTH);
        content.add(createVerticalStrut(10), BorderLayout.SOUTH);
        content.add(createHorizontalStrut(11), BorderLayout.WEST);
        content.add(createHorizontalStrut(10), BorderLayout.EAST);
        content.add(mainBox);
        pack();
    }

    public static void matchComponentSize(JComponent[] components) {
        Dimension size = new Dimension(0, 0);
        for (int i = 0; i < components.length; i++) {
            if (components[i].isVisible()) {
                Dimension bSize = components[i].getPreferredSize();
                size.width = (size.width < bSize.width) ? bSize.width
                                                        : size.width;
                size.height = (size.height < bSize.height) ? bSize.height
                                                           : size.height;
            }
        }
        for (int i = 0; i < components.length; i++) {
            components[i].setPreferredSize(size);
            components[i].setMinimumSize(size);
            components[i].setMaximumSize(size);
        }
    }

    public static void shrinkWrap(AbstractButton[] buttons) {
        Insets shrinkWrap = new Insets(0, 0, 0, 0);
        for (int i = 0; i < buttons.length; i++) {
            buttons[i].setMargin(shrinkWrap);
        }
    }

    public static Component createHorizontalStrut(int width) {
        return Box.createRigidArea(new Dimension(width, 0));
    }

    public static Component createVerticalStrut(int height) {
        return Box.createRigidArea(new Dimension(0, height));
    }
}


/*--- Formatted for WSTT code conventions ---*/
/*------ Formatted by Jindent 3.24 Basic 1.0 --- http://www.jindent.de ------*/

