view.ManageOrganization.java Source code

Java tutorial

Introduction

Here is the source code for view.ManageOrganization.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import Db.Dbcon;
import General.Configuration;
import General.Nimbus;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

/**
 *
 * @author Jithinpv
 */
public class ManageOrganization extends javax.swing.JFrame {

    String organisationIconName;
    int selectedOrganisationStatus = 0;

    /**
     * Creates new form ManageOrganization
     */
    public ManageOrganization() {
        Nimbus.loadLoogAndFeel();

        initComponents();
        this.setLocationRelativeTo(null);
        loadValuesInTable();
        active_deactive_button.setEnabled(false);
        Configuration.setIconOnLabel("createOrg.jpg", bg);
    }

    private void disableAll() {
        user_name_text.setEditable(false);
        email_text.setEditable(false);
        phone_text.setEditable(false);
        password_text.setEditable(false);
        phone_text.setEditable(false);
    }

    private void loadValuesInTable() {
        Dbcon dbcon = new Dbcon();
        DefaultTableModel dt = (DefaultTableModel) jTable1.getModel();
        ResultSet rs = dbcon.select("select * from tbl_organisation order by organisation_id asc");
        try {
            while (rs.next()) {
                dt.addRow(new String[] { rs.getString(1), rs.getString(2),
                        (rs.getString(10).trim().equals("1")) ? "active" : "blocked" });
            }
            jTable1.setModel(dt);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        disableAll();
    }

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

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        user_name_text = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        email_text = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        phone_text = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        password_text = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        place_text = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        active_deactive_button = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        organisation_logo_label = new javax.swing.JLabel();
        bg = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTable1.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

        }, new String[] { "ID", "NAME", "STATUS" }) {
            Class[] types = new Class[] { java.lang.Integer.class, java.lang.String.class, java.lang.String.class };
            boolean[] canEdit = new boolean[] { false, false, false };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        if (jTable1.getColumnModel().getColumnCount() > 0) {
            jTable1.getColumnModel().getColumn(0).setResizable(false);
            jTable1.getColumnModel().getColumn(1).setResizable(false);
            jTable1.getColumnModel().getColumn(2).setResizable(false);
        }

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(25, 11, 250, 329));

        jLabel1.setText("Name");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(312, 47, 88, -1));

        user_name_text.setEditable(false);
        getContentPane().add(user_name_text, new org.netbeans.lib.awtextra.AbsoluteConstraints(433, 44, 122, -1));

        jLabel2.setText("E-Mail");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(312, 85, 88, -1));

        email_text.setEditable(false);
        getContentPane().add(email_text, new org.netbeans.lib.awtextra.AbsoluteConstraints(433, 82, 122, -1));

        jLabel3.setText("Phone Number");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(312, 123, 88, -1));

        phone_text.setEditable(false);
        getContentPane().add(phone_text, new org.netbeans.lib.awtextra.AbsoluteConstraints(433, 120, 122, -1));

        jLabel4.setText("Password");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(312, 161, 88, -1));

        password_text.setEditable(false);
        getContentPane().add(password_text, new org.netbeans.lib.awtextra.AbsoluteConstraints(433, 158, 122, -1));

        jLabel5.setText("Place");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(312, 200, 88, -1));

        place_text.setEditable(false);
        getContentPane().add(place_text, new org.netbeans.lib.awtextra.AbsoluteConstraints(433, 197, 122, -1));

        jLabel6.setText("Logo");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(312, 235, 88, -1));

        jButton1.setText("EDIT");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(289, 370, -1, -1));

        jButton2.setText("SAVE");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(459, 370, -1, -1));

        jButton3.setText("CLEAR");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(522, 370, -1, -1));

        active_deactive_button.setText("DEACTIVATE");
        active_deactive_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                active_deactive_buttonActionPerformed(evt);
            }
        });
        getContentPane().add(active_deactive_button,
                new org.netbeans.lib.awtextra.AbsoluteConstraints(354, 370, -1, -1));

        jButton5.setText("HOME");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(121, 370, -1, -1));

        jButton6.setText("Browse");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton6, new org.netbeans.lib.awtextra.AbsoluteConstraints(561, 235, -1, -1));

        organisation_logo_label.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        getContentPane().add(organisation_logo_label,
                new org.netbeans.lib.awtextra.AbsoluteConstraints(435, 235, 122, 129));

        bg.setText("jLabel7");
        getContentPane().add(bg, new org.netbeans.lib.awtextra.AbsoluteConstraints(-6, -6, 670, 440));

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

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        this.dispose();
        AdminHome adminHome = new AdminHome();
        adminHome.setVisible(true);

    }//GEN-LAST:event_jButton5ActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        jButton6.setEnabled(false);
        jButton2.setEnabled(false);
        jButton3.setEnabled(false);
        jButton1.setEnabled(false);
    }//GEN-LAST:event_formWindowOpened

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        jButton3.setEnabled(true);
        jButton1.setEnabled(true);
        disableAll();
        String id = jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString();
        Dbcon dbcon = new Dbcon();
        ResultSet rs = dbcon.select("select * from tbl_organisation where organisation_id='" + id + "'");
        String name, email, phone_num, password, place, path;
        BufferedImage img = null;
        try {
            if (rs.next()) {
                name = rs.getString(2);
                user_name_text.setText(name);
                email = rs.getString(3);
                email_text.setText(email);
                phone_num = rs.getString(4);
                phone_text.setText(phone_num);
                password = rs.getString(5);
                password_text.setText(password);
                place = rs.getString(6);
                place_text.setText(place);
                path = rs.getString(7);
                System.out.println(path);
                organisationIconName = rs.getString("logo_image");
                organisation_logo_label.setIcon(null);
                Configuration.setOrganisationIcon(organisationIconName, organisation_logo_label);
                active_deactive_button.setEnabled(true);

                selectedOrganisationStatus = Integer.parseInt(rs.getString("org_status"));
                if (selectedOrganisationStatus == 0) {
                    active_deactive_button.setText("RECOVER");
                } else {
                    active_deactive_button.setText("DELETE");
                }

            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

    }//GEN-LAST:event_jTable1MouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        jButton6.setEnabled(true);
        user_name_text.setEditable(true);
        email_text.setEditable(true);
        phone_text.setEditable(true);
        password_text.setEditable(true);
        place_text.setEditable(true);
        jButton2.setEnabled(true);

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        String id = jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString();
        String name = user_name_text.getText();
        String email = email_text.getText();
        String phone_num = phone_text.getText();
        String password = password_text.getText();
        String place = place_text.getText();
        Dbcon dbcon = new Dbcon();
        dbcon.update("update tbl_organisation set name='" + name + "',email_id='" + email + "',phone_num='"
                + phone_num + "',password='" + password + "',place='" + place + "',updated_at='"
                + System.currentTimeMillis() + "' ,logo_image='" + organisationIconName
                + "' where organisation_id='" + id + "'");

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        user_name_text.setText("");
        email_text.setText("");
        phone_text.setText("");
        password_text.setText("");
        place_text.setText("");
        jLabel6.setText("");
        disableAll();
        jTable1.clearSelection();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void active_deactive_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_active_deactive_buttonActionPerformed
        // TODO add your handling code here:
        int showConfirmDialog = JOptionPane.showConfirmDialog(rootPane, "Are you sure ?");
        if (showConfirmDialog == JOptionPane.OK_OPTION) {
            String id = jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString();
            String status = jTable1.getValueAt(jTable1.getSelectedRow(), 2).toString();
            System.out.println(status);
            Dbcon dbcon = new Dbcon();
            int nextStatusCode = 0;
            if (status.toLowerCase().trim().equals("active")) {
                nextStatusCode = 0;
                jTable1.setValueAt("blocked", jTable1.getSelectedRow(), 2);
                active_deactive_button.setText("RECOVER");
            } else {
                nextStatusCode = 1;
                jTable1.setValueAt("active", jTable1.getSelectedRow(), 2);
                active_deactive_button.setText("DELETE");
            }
            dbcon.update("update tbl_organisation set org_status=" + nextStatusCode + " where organisation_id='"
                    + id + "'");
        }

    }//GEN-LAST:event_active_deactive_buttonActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");

        chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            chooser.getSelectedFile().getPath();
            System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
            BufferedImage img = null;
            try {
                img = ImageIO.read(new File(chooser.getSelectedFile().getPath()));
                Image scaledInstance = img.getScaledInstance(organisation_logo_label.getWidth(),
                        organisation_logo_label.getHeight(), Image.SCALE_SMOOTH);
                ImageIcon imageIcon = new ImageIcon(scaledInstance);
                organisation_logo_label.setIcon(imageIcon);

                organisationIconName = System.currentTimeMillis() + "."
                        + FilenameUtils.getExtension(chooser.getSelectedFile().getPath());
                FileUtils.copyFile(chooser.getSelectedFile(),
                        new File(Configuration.organisationImages + organisationIconName));

            } catch (IOException e) {
                e.printStackTrace();
            }
            long size = (chooser.getSelectedFile().length()) / 1024;

        }
    }//GEN-LAST:event_jButton6ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ManageOrganization.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ManageOrganization.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ManageOrganization.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ManageOrganization.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ManageOrganization().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton active_deactive_button;
    private javax.swing.JLabel bg;
    private javax.swing.JTextField email_text;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    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.JTable jTable1;
    private javax.swing.JLabel organisation_logo_label;
    private javax.swing.JTextField password_text;
    private javax.swing.JTextField phone_text;
    private javax.swing.JTextField place_text;
    private javax.swing.JTextField user_name_text;
    // End of variables declaration//GEN-END:variables
}