package org.mansart.mongocount.gui;

import org.mansart.mongocount.Configuration;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public final class ConfigurationDialog extends JDialog {
    private Configuration configuration = null;
    private JTextField dbnameField = new JTextField();
    private JTextField collnameField = new JTextField();
    private JSpinner intervalField = new JSpinner();
    private JButton cancelButton = new JButton("Cancel");
    private JButton okButton = new JButton("OK");

    public ConfigurationDialog(Configuration configuration, JComponent owner) {
        super((Frame) SwingUtilities.windowForComponent(owner), "Configuration", true);

        this.configuration = configuration;

        this.setupGraphics();
        this.setupListeners();
    }

    private void setupGraphics() {
        this.setSize(300, 150);

        JLabel dbnameLabel = new JLabel("Database");
        dbnameLabel.setHorizontalAlignment(JLabel.RIGHT);
        JLabel collnameLabel = new JLabel("Collection");
        collnameLabel.setHorizontalAlignment(JLabel.RIGHT);
        JLabel intervalLabel = new JLabel("Interval");
        intervalLabel.setHorizontalAlignment(JLabel.RIGHT);

        JPanel formPanel = new JPanel(new GridLayout(3, 2));
        formPanel.add(dbnameLabel);
        formPanel.add(this.dbnameField);
        formPanel.add(collnameLabel);
        formPanel.add(this.collnameField);
        formPanel.add(intervalLabel);
        formPanel.add(this.intervalField);

        JPanel controlsPanel = new JPanel(new FlowLayout());
        controlsPanel.add(this.cancelButton);
        controlsPanel.add(this.okButton);

        JPanel mainPanel = new JPanel(new BorderLayout());
        mainPanel.add(formPanel, BorderLayout.CENTER);
        mainPanel.add(controlsPanel, BorderLayout.SOUTH);
        this.setContentPane(mainPanel);
    }

    private void setupListeners() {
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowOpened(WindowEvent e) {
                ConfigurationDialog.this.dbnameField.setText(ConfigurationDialog.this.configuration.getDbname());
                ConfigurationDialog.this.collnameField.setText(ConfigurationDialog.this.configuration.getCollname());
                ConfigurationDialog.this.intervalField.setValue(ConfigurationDialog.this.configuration.getInterval());
            }
        });

        this.okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String dbname = ConfigurationDialog.this.dbnameField.getText();
                String collname = ConfigurationDialog.this.collnameField.getText();
                int interval = (Integer) ConfigurationDialog.this.intervalField.getValue();
                ConfigurationDialog.this.configuration.udpate(dbname, collname, interval);
                ConfigurationDialog.this.setVisible(false);
            }
        });

        this.cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ConfigurationDialog.this.setVisible(false);
            }
        });
    }
}

@@ -1386,217 +1386,1492 @@
 Grid
+Bag
 Layout(
-3, 2));%0A        formPanel.add(dbnameLabel);%0A        formPanel.add(this.dbnameField);%0A        formPanel.add(collnameLabel);%0A        formPanel.add(this.collnameField);%0A        formPanel.add(intervalLabel)
+));%0A        GridBagConstraints constraints = new GridBagConstraints();%0A        constraints.fill = GridBagConstraints.HORIZONTAL;%0A        double labelWeightx = 0.1;%0A        double controlWeightx = 0.9;%0A        Insets labelInsets = new Insets(0, 0, 0, 0);%0A        Insets controlInsets = new Insets(0, 5, 0, 5);%0A%0A        constraints.gridx = 0;%0A        constraints.gridy = 0;%0A        constraints.weightx = labelWeightx;%0A        constraints.insets = labelInsets;%0A        formPanel.add(dbnameLabel, constraints);%0A%0A        constraints.gridx = 1;%0A        constraints.gridy = 0;%0A        constraints.weightx = controlWeightx;%0A        constraints.insets = controlInsets;%0A        formPanel.add(this.dbnameField, constraints);%0A%0A        constraints.gridx = 0;%0A        constraints.gridy = 1;%0A        constraints.weightx = labelWeightx;%0A        constraints.insets = labelInsets;%0A        formPanel.add(collnameLabel, constraints);%0A%0A        constraints.gridx = 1;%0A        constraints.gridy = 1;%0A        constraints.weightx = controlWeightx;%0A        constraints.insets = controlInsets;%0A        formPanel.add(this.collnameField, constraints);%0A%0A        constraints.gridx = 0;%0A        constraints.gridy = 2;%0A        constraints.weightx = labelWeightx;%0A        constraints.insets = labelInsets;%0A        formPanel.add(intervalLabel, constraints);%0A%0A        constraints.gridx = 1;%0A        constraints.gridy = 2;%0A        constraints.weightx = controlWeightx;%0A        constraints.insets = controlInsets
 ;%0A  
@@ -2908,16 +2908,29 @@
 valField
+, constraints
 );%0A%0A    

improve configuration dialog box UI