Package club.ui

Source Code of club.ui.MembershipDialog

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

import club.beans.MembershipTypeBean;
import club.data.DataMember;
import club.data.DataMembership;
import club.ulti.Validator;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
*
* @author sior
*/
public class MembershipDialog extends javax.swing.JDialog {

    private String actionFlag;

    /**
     * Creates new form MembershipDialog
     */
    private void refreshData() throws SQLException {
        DataMembership dataMembership = new DataMembership();
        DefaultTableModel model = dataMembership.getAllTypesAsModel();
        tblMembership.setModel(model);
    }

    public MembershipDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        try {
            initComponents();
            this.setLocation(400,100);
            refreshData();
        } catch (SQLException ex) {
            Logger.getLogger(MembershipDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
        panelForm.setVisible(false);
        this.setSize(this.getWidth(), 280);
    }

    /**
     * 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() {

        jButton1 = new javax.swing.JButton();
        btn_Add = new javax.swing.JButton();
        btn_Update = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblMembership = new javax.swing.JTable();
        btn_Delete = new javax.swing.JButton();
        panelForm = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtTypeName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtMainDuty = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        spinFee = new javax.swing.JSpinner();
        spinMinAttendance = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        spinDiscount = new javax.swing.JSpinner();
        btn_Submit = new javax.swing.JButton();
        btn_Abort = new javax.swing.JButton();
        txtHiddenID = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Membership");
        setResizable(false);

        btn_Add.setText("Add");
        btn_Add.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_AddActionPerformed(evt);
            }
        });

        btn_Update.setText("Update");
        btn_Update.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_UpdateActionPerformed(evt);
            }
        });

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

        tblMembership.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblMembership);

        btn_Delete.setText("Delete");
        btn_Delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_DeleteActionPerformed(evt);
            }
        });

        panelForm.setBorder(javax.swing.BorderFactory.createTitledBorder("Membership Details"));

        jLabel1.setText("Type");

        jLabel2.setText("Main Duty");

        jLabel3.setText("Fee");

        jLabel4.setText("Min Attendance");

        jLabel5.setText("Discount");

        btn_Submit.setText("Submit");
        btn_Submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_SubmitActionPerformed(evt);
            }
        });

        btn_Abort.setText("Abort");
        btn_Abort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_AbortActionPerformed(evt);
            }
        });

        txtHiddenID.setEditable(false);
        txtHiddenID.setBackground(new java.awt.Color(204, 204, 204));
        txtHiddenID.setText("jTextField1");

        jLabel6.setText("ID");

        javax.swing.GroupLayout panelFormLayout = new javax.swing.GroupLayout(panelForm);
        panelForm.setLayout(panelFormLayout);
        panelFormLayout.setHorizontalGroup(
            panelFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelFormLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(txtHiddenID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spinDiscount, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(panelFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1)
                    .addComponent(txtTypeName)
                    .addComponent(jLabel2)
                    .addComponent(txtMainDuty, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(panelFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel3)
                        .addComponent(spinFee)
                        .addComponent(spinMinAttendance, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel4))
                .addGap(53, 53, 53)
                .addGroup(panelFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelFormLayout.createSequentialGroup()
                        .addGap(83, 83, 83)
                        .addComponent(btn_Abort))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelFormLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_Submit)))
                .addContainerGap())
        );
        panelFormLayout.setVerticalGroup(
            panelFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelFormLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtTypeName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spinFee, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_Submit)
                    .addComponent(txtHiddenID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtMainDuty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spinMinAttendance, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_Abort)
                    .addComponent(spinDiscount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        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(panelForm, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btn_Add)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_Update)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_Delete)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_Add)
                    .addComponent(btn_Update)
                    .addComponent(btnCancel)
                    .addComponent(btn_Delete))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelForm, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btn_UpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_UpdateActionPerformed
        int selectedRow = tblMembership.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(rootPane, "You must chose one membership type first!", "Error!", JOptionPane.ERROR_MESSAGE);
        } else {
            int id = (int) tblMembership.getModel().getValueAt(selectedRow, 0);
            DataMembership dataMembership = new DataMembership();
            MembershipTypeBean bean = dataMembership.getTypeByID(id);
            txtTypeName.setText(bean.getTypeName());
            txtMainDuty.setText(bean.getMain_Duty());
            spinFee.setValue(bean.getFee());
            spinMinAttendance.setValue(bean.getMin_Attendance());
            spinDiscount.setValue(bean.getDiscount());
            txtHiddenID.setText(String.valueOf(bean.getMembershipTypeID()));
            panelForm.setVisible(true);
            this.setSize(this.getWidth(), 450);
            actionFlag = "Update";
        }
    }//GEN-LAST:event_btn_UpdateActionPerformed

    private void btn_AddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_AddActionPerformed
        txtTypeName.setText("");
        txtMainDuty.setText("");
        spinFee.setValue(0);
        spinMinAttendance.setValue(0);
        spinDiscount.setValue(0);
        txtHiddenID.setText("#");
        panelForm.setVisible(true);
        this.setSize(this.getWidth(), 450);
        actionFlag = "Add";
        System.out.println(panelForm.getHeight());
    }//GEN-LAST:event_btn_AddActionPerformed

    private void btn_AbortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_AbortActionPerformed
        panelForm.setVisible(false);
        this.setSize(this.getWidth(), 280);
    }//GEN-LAST:event_btn_AbortActionPerformed

    private void btn_DeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_DeleteActionPerformed
        int selectedRow = tblMembership.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(rootPane, "You must chose one membership type first!", "Error!", JOptionPane.ERROR_MESSAGE);
        } else {
            int id = (int) tblMembership.getModel().getValueAt(selectedRow, 0);
            boolean isPossibleToDelete = DataMember.checkMemberInType(id);
            if (isPossibleToDelete == true) {
                JOptionPane.showMessageDialog(rootPane, "Membership still has member in it!\n Can't delete !", "Error!", JOptionPane.ERROR_MESSAGE);
            } else {
                try {
                    DataMembership.removeTypeByID(id);
                    refreshData();
                } catch (SQLException ex) {
                    Logger.getLogger(MembershipDialog.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }//GEN-LAST:event_btn_DeleteActionPerformed

    private boolean validateData() {
        boolean flag = true;
        flag = Validator.checkIsNotEmpty(txtTypeName.getText());
        return flag;
    }
    private void btn_SubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_SubmitActionPerformed
        if (validateData() == true) {
            if (actionFlag.equals("Update")) {
                try {
                    int id = Integer.parseInt(txtHiddenID.getText());
                    int intFee = (int) spinFee.getModel().getValue();
                    double fee = (double) intFee;
                    int min = (int) spinMinAttendance.getModel().getValue();
                    int dis = (int) spinDiscount.getModel().getValue();
                    DataMembership.updateTypeByID(id, txtTypeName.getText(), fee, min, txtMainDuty.getText(), dis);
                    panelForm.setVisible(false);
                    this.setSize(this.getWidth(), 280);
                    refreshData();
                } catch (SQLException ex) {
                    Logger.getLogger(MembershipDialog.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (actionFlag.equals("Add")) {
                try {
                    int intFee = (int) spinFee.getModel().getValue();
                    double fee = (double) intFee;
                    int min = (int) spinMinAttendance.getModel().getValue();
                    int dis = (int) spinDiscount.getModel().getValue();
                    DataMembership.insertNewType(txtTypeName.getText(), fee, min, txtMainDuty.getText(), dis);
                    panelForm.setVisible(false);
                    this.setSize(this.getWidth(), 280);
                    refreshData();
                } catch (SQLException ex) {
                    Logger.getLogger(MembershipDialog.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } else {
            JOptionPane.showMessageDialog(rootPane, "Error Input !", "Error!", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btn_SubmitActionPerformed

    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btn_Abort;
    private javax.swing.JButton btn_Add;
    private javax.swing.JButton btn_Delete;
    private javax.swing.JButton btn_Submit;
    private javax.swing.JButton btn_Update;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel panelForm;
    private javax.swing.JSpinner spinDiscount;
    private javax.swing.JSpinner spinFee;
    private javax.swing.JSpinner spinMinAttendance;
    private javax.swing.JTable tblMembership;
    private javax.swing.JTextField txtHiddenID;
    private javax.swing.JTextField txtMainDuty;
    private javax.swing.JTextField txtTypeName;
    // End of variables declaration//GEN-END:variables
}
TOP

Related Classes of club.ui.MembershipDialog

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.