+/*
+ * 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 habeas;
+
+import java.util.ArrayList;
+import javax.swing.JOptionPane;
+import javax.swing.event.ListSelectionEvent;
+import javax.swing.event.ListSelectionListener;
+import javax.swing.event.TableModelEvent;
+import javax.swing.event.TableModelListener;
+import javax.swing.plaf.basic.BasicListUI;
+import javax.swing.table.DefaultTableModel;
+import javax.swing.table.TableModel;
+
+/**
+ *
+ * @author hari
+ */
+public class ClientMasterDialog extends javax.swing.JDialog {
+
+ /**
+ * Creates new form ClientMasterDialog
+ * @param parent
+ * @param modal
+ */
+ public ClientMasterDialog(java.awt.Frame parent, boolean modal) {
+ super(parent, modal);
+ initComponents();
+ populateTable ();
+ tableClients.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
+ @Override
+ public void valueChanged(ListSelectionEvent lse) {
+ int r = getSelectedClient ();
+ if (r == -1) {
+ buttonAdd.setEnabled(true);
+ buttonUpdate.setEnabled(false);
+ buttonDelete.setEnabled(false);
+ return;
+ }
+
+ ArrayList<Object> clients = Utility.getClientDetails (r);
+ for (int q = 0; q < clients.size(); q += 5) {
+ textClientName.setText (clients.get(q).toString());
+ textClientAddress.setText(clients.get(q+1).toString());
+ textContactPerson.setText(clients.get(q+2).toString());
+ textMailID.setText(clients.get(q+3).toString());
+ textPhoneNumber.setText(clients.get(q+4).toString());
+ }
+ buttonAdd.setEnabled(false);
+ buttonUpdate.setEnabled(true);
+ buttonDelete.setEnabled(true);
+ }
+ });
+ }
+
+ private int getSelectedClient () {
+ int i = tableClients.getSelectedRow();
+ if (i == -1)
+ return -1;
+
+ DefaultTableModel mdl = (DefaultTableModel)tableClients.getModel();
+ int r = (int)mdl.getValueAt(i, 0);
+ return r;
+ }
+
+ private void populateTable () {
+ DefaultTableModel mdl = (DefaultTableModel)tableClients.getModel();
+ mdl.setRowCount(0);
+ ArrayList<Object> clientsdata = Utility.getClientsNameAndId();
+ if (clientsdata == null) return;
+ for (int i = 0 ; i < clientsdata.size(); i +=2) {
+ mdl.addRow(new Object[] {
+ clientsdata.get(i), clientsdata.get(i+1)});
+ }
+ }
+
+ /**
+ * 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();
+ tableClients = new javax.swing.JTable();
+ buttonAdd = new javax.swing.JButton();
+ buttonDelete = new javax.swing.JButton();
+ buttonUpdate = new javax.swing.JButton();
+ jLabel1 = new javax.swing.JLabel();
+ textClientName = new javax.swing.JTextField();
+ jLabel2 = new javax.swing.JLabel();
+ jScrollPane2 = new javax.swing.JScrollPane();
+ textClientAddress = new javax.swing.JTextArea();
+ jLabel3 = new javax.swing.JLabel();
+ textContactPerson = new javax.swing.JTextField();
+ jLabel4 = new javax.swing.JLabel();
+ textPhoneNumber = new javax.swing.JTextField();
+ jLabel5 = new javax.swing.JLabel();
+ textMailID = new javax.swing.JTextField();
+ buttonClose = new javax.swing.JButton();
+ buttonClearSelection = new javax.swing.JButton();
+
+ setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
+ setTitle("Clients Master");
+
+ tableClients.setModel(new javax.swing.table.DefaultTableModel(
+ new Object [][] {
+
+ },
+ new String [] {
+ "id", "Client Name"
+ }
+ ) {
+ Class[] types = new Class [] {
+ java.lang.Integer.class, java.lang.String.class
+ };
+
+ public Class getColumnClass(int columnIndex) {
+ return types [columnIndex];
+ }
+ @Override
+ public boolean isCellEditable (int row, int col) {
+ return false;
+ }
+ });
+ tableClients.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
+ tableClients.setShowGrid(true);
+ jScrollPane1.setViewportView(tableClients);
+ tableClients.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
+
+ buttonAdd.setMnemonic('A');
+ buttonAdd.setText("Add");
+ buttonAdd.addActionListener(new java.awt.event.ActionListener() {
+ public void actionPerformed(java.awt.event.ActionEvent evt) {
+ buttonAddActionPerformed(evt);
+ }
+ });
+
+ buttonDelete.setMnemonic('D');
+ buttonDelete.setText("Delete");
+ buttonDelete.setEnabled(false);
+ buttonDelete.addActionListener(new java.awt.event.ActionListener() {
+ public void actionPerformed(java.awt.event.ActionEvent evt) {
+ buttonDeleteActionPerformed(evt);
+ }
+ });
+
+ buttonUpdate.setMnemonic('U');
+ buttonUpdate.setText("Update");
+ buttonUpdate.setEnabled(false);
+ buttonUpdate.addActionListener(new java.awt.event.ActionListener() {
+ public void actionPerformed(java.awt.event.ActionEvent evt) {
+ buttonUpdateActionPerformed(evt);
+ }
+ });
+
+ jLabel1.setText("Client Name");
+
+ jLabel2.setText("Client Address");
+
+ textClientAddress.setColumns(20);
+ textClientAddress.setRows(5);
+ jScrollPane2.setViewportView(textClientAddress);
+
+ jLabel3.setText("Contact Person");
+
+ jLabel4.setText("Phone Number");
+
+ jLabel5.setText("Mail ID");
+
+ buttonClose.setMnemonic('C');
+ buttonClose.setText("Close");
+ buttonClose.addActionListener(new java.awt.event.ActionListener() {
+ public void actionPerformed(java.awt.event.ActionEvent evt) {
+ buttonCloseActionPerformed(evt);
+ }
+ });
+
+ buttonClearSelection.setMnemonic('S');
+ buttonClearSelection.setText("Clear Selection");
+ buttonClearSelection.addActionListener(new java.awt.event.ActionListener() {
+ public void actionPerformed(java.awt.event.ActionEvent evt) {
+ buttonClearSelectionActionPerformed(evt);
+ }
+ });
+
+ 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(jScrollPane1)
+ .addGroup(layout.createSequentialGroup()
+ .addComponent(buttonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+ .addComponent(buttonUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+ .addComponent(buttonDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
+ .addComponent(buttonClearSelection)
+ .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+ .addComponent(buttonClose, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
+ .addGroup(layout.createSequentialGroup()
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
+ .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+ .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
+ .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+ .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+ .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
+ .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+ .addComponent(textClientName)
+ .addComponent(jScrollPane2)
+ .addComponent(textContactPerson)
+ .addComponent(textPhoneNumber)
+ .addComponent(textMailID))))
+ .addContainerGap())
+ );
+ layout.setVerticalGroup(
+ layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+ .addGroup(layout.createSequentialGroup()
+ .addContainerGap()
+ .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+ .addComponent(buttonAdd)
+ .addComponent(buttonUpdate)
+ .addComponent(buttonDelete)
+ .addComponent(buttonClose)
+ .addComponent(buttonClearSelection))
+ .addGap(18, 18, 18)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+ .addComponent(jLabel1)
+ .addComponent(textClientName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
+ .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+ .addComponent(jLabel2)
+ .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
+ .addGap(10, 10, 10)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+ .addComponent(textContactPerson, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(jLabel3))
+ .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+ .addComponent(jLabel4)
+ .addComponent(textPhoneNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
+ .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+ .addComponent(jLabel5)
+ .addComponent(textMailID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
+ .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
+ );
+
+ pack();
+ }// </editor-fold>//GEN-END:initComponents
+
+ private void buttonCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCloseActionPerformed
+ // TODO add your handling code here:
+ dispose ();
+ }//GEN-LAST:event_buttonCloseActionPerformed
+
+ private void buttonClearSelectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClearSelectionActionPerformed
+ clearFields();
+ }//GEN-LAST:event_buttonClearSelectionActionPerformed
+
+ private void buttonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddActionPerformed
+ // TODO add your handling code here:
+ boolean rt = Utility.addClient (textClientName.getText(), textClientAddress.getText(),
+ textContactPerson.getText(), textMailID.getText(),
+ textPhoneNumber.getText());
+
+ if (rt == true) {
+ JOptionPane.showMessageDialog(this, SUCCESSFULLY_ADDED);
+ clearFields();
+ populateTable();
+ }
+ else
+ JOptionPane.showMessageDialog (this, ERROR_IN_ADDING);
+ }//GEN-LAST:event_buttonAddActionPerformed
+ private static final String SUCCESSFULLY_ADDED = "Successfully added";
+ private static final String ERROR_IN_ADDING = "Error in adding";
+
+ private void buttonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDeleteActionPerformed
+ // TODO add your handling code here:
+ int r = getSelectedClient();
+ if (r == -1) return;
+ int conf = JOptionPane.showConfirmDialog(this, CONFIRM_DELETE_CLIENT);
+ if (conf == JOptionPane.YES_OPTION) {
+ boolean rt = Utility.deleteClient (r);
+ if (rt == true) {
+ JOptionPane.showMessageDialog(this, SUCCESSFULLY_DELETED);
+ clearFields();
+ populateTable();
+ }
+ else
+ JOptionPane.showMessageDialog(this, ERROR_IN_DELETING);
+ }
+ }//GEN-LAST:event_buttonDeleteActionPerformed
+ private static final String ERROR_IN_DELETING = "Error in deleting";
+
+ private void buttonUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonUpdateActionPerformed
+ // TODO add your handling code here:
+ int r = getSelectedClient();
+ if (r == -1 ) return;
+ boolean rt = Utility.updateClient (r, textClientName.getText(),
+ textClientAddress.getText(), textContactPerson.getText(),
+ textMailID.getText(), textPhoneNumber.getText());
+ if (rt == true) {
+ JOptionPane.showMessageDialog(this, SUCCESSFULLY_UPDATED);
+ clearFields();
+ populateTable();
+ } else {
+ JOptionPane.showMessageDialog(this, ERROR_IN_UPDATING);
+ }
+
+ }//GEN-LAST:event_buttonUpdateActionPerformed
+ private static final String ERROR_IN_UPDATING = "Error in updating";
+ private static final String SUCCESSFULLY_UPDATED = "Successfully updated";
+ private static final String SUCCESSFULLY_DELETED = "Successfully deleted";
+ private static final String CONFIRM_DELETE_CLIENT = "Are you sure? This will"
+ + " also delete all other data associated with this client";
+
+ private void clearFields() {
+ // TODO add your handling code here:
+ tableClients.clearSelection();
+ textClientAddress.setText ("");
+ textClientName.setText ("");
+ textContactPerson.setText("");
+ textMailID.setText("");
+ textPhoneNumber.setText("");
+ }
+
+ /**
+ * @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(ClientMasterDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+ } catch (InstantiationException ex) {
+ java.util.logging.Logger.getLogger(ClientMasterDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+ } catch (IllegalAccessException ex) {
+ java.util.logging.Logger.getLogger(ClientMasterDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+ } catch (javax.swing.UnsupportedLookAndFeelException ex) {
+ java.util.logging.Logger.getLogger(ClientMasterDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
+ }
+ //</editor-fold>
+
+ /* Create and display the dialog */
+ java.awt.EventQueue.invokeLater(new Runnable() {
+ public void run() {
+ ClientMasterDialog dialog = new ClientMasterDialog(new javax.swing.JFrame(), true);
+ dialog.addWindowListener(new java.awt.event.WindowAdapter() {
+ @Override
+ public void windowClosing(java.awt.event.WindowEvent e) {
+ System.exit(0);
+ }
+ });
+ dialog.setVisible(true);
+ }
+ });
+ }
+
+ // Variables declaration - do not modify//GEN-BEGIN:variables
+ private javax.swing.JButton buttonAdd;
+ private javax.swing.JButton buttonClearSelection;
+ private javax.swing.JButton buttonClose;
+ private javax.swing.JButton buttonDelete;
+ private javax.swing.JButton buttonUpdate;
+ 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.JScrollPane jScrollPane1;
+ private javax.swing.JScrollPane jScrollPane2;
+ private javax.swing.JTable tableClients;
+ private javax.swing.JTextArea textClientAddress;
+ private javax.swing.JTextField textClientName;
+ private javax.swing.JTextField textContactPerson;
+ private javax.swing.JTextField textMailID;
+ private javax.swing.JTextField textPhoneNumber;
+ // End of variables declaration//GEN-END:variables
+}