/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StartupFrame.java
 *
 * Created on Apr 9, 2010, 11:17:43 AM
 */
package ex1.database;

import javax.swing.JOptionPane;

/**
 *
 * @author Joseph Levin <joelevin@weru.ksu.edu>
 */
public class StartupFrame extends javax.swing.JFrame {

    private static final long serialVersionUID = 1L;

    private DatabaseInformation c_info;
    private DataType c_type;

    /**
     * Creates new form StartupFrame
     */
    public StartupFrame() {
        initComponents();

        //Init the database providers
        WepsDatabasesProvider wepsProvider = new WepsDatabasesProvider();

        databaseChooser.addDatabaseProvider(wepsProvider);
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT
     * modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        databaseLabel = new javax.swing.JLabel();
        databaseChooser = new ex1.database.DatabaseChooser();
        recordsLabel = new javax.swing.JLabel();
        cancelButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        dataTypeChooser = new ex1.database.DataTypeChooser();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("WEPS DB Editor");

        databaseLabel.setText("Database:");

        databaseChooser.setBackground(new java.awt.Color(255, 255, 255));
        databaseChooser.setBrowseDatabaseProviderUsed(true);

        recordsLabel.setText("Record Type:");

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        dataTypeChooser.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(databaseLabel)
                    .addComponent(databaseChooser, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(recordsLabel)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(okButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton))
                    .addComponent(dataTypeChooser, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(databaseLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(databaseChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(recordsLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dataTypeChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-408)/2, (screenSize.height-251)/2, 408, 251);
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        c_info = null;
        c_type = null;
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        c_info = databaseChooser.getSelectedDatabaseInformation();
        c_type = dataTypeChooser.getSelectedDataType();

        if (c_info == null) {
            JOptionPane.showMessageDialog(this, "Please select a database.", "No Database Selected", JOptionPane.WARNING_MESSAGE);
            return;
        }

        if (c_type == null) {
            JOptionPane.showMessageDialog(this, "Please select a data type.", "No type Selected", JOptionPane.WARNING_MESSAGE);
            return;
        }
        dispose();
    }//GEN-LAST:event_okButtonActionPerformed

    /**
     *
     * @return
     */
    public DatabaseInformation getDatabaseInformation() {
        return c_info;
    }

    /**
     *
     * @return
     */
    public DataType getDataType() {
        return c_type;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new StartupFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private ex1.database.DataTypeChooser dataTypeChooser;
    private ex1.database.DatabaseChooser databaseChooser;
    private javax.swing.JLabel databaseLabel;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel recordsLabel;
    // End of variables declaration//GEN-END:variables

}
