/*
* 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 Views;
import Helpers.AuthorTableModel;
import Models.AuthorModel;
import Models.Pojo.Author;
import java.util.List;
import javax.swing.JOptionPane;
/**
*
* @author Tejas
*/
public class panelSearchAuthor extends javax.swing.JPanel {
/**
* Creates new form panelSearchAuthor
*/
public panelSearchAuthor() {
initComponents();
fillTable();
}
private void fillTable(){
AuthorModel authorModel = new AuthorModel();
List<Author> authors = authorModel.all();
AuthorTableModel authorTableModel = new AuthorTableModel(authors);
jTable1.setModel(authorTableModel);
}
private void fillTableSearch(String author_name){
AuthorModel authorModel = new AuthorModel();
List<Author> authors = authorModel.whereFullName(author_name);
AuthorTableModel authorTableModel = new AuthorTableModel(authors);
jTable1.setModel(authorTableModel);
}
/**
* 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() {
jDialog1 = new javax.swing.JDialog();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
txtAuthorFullName = new javax.swing.JTextField();
txtAuthorLocation = new javax.swing.JTextField();
txtAuthorAge = new javax.swing.JTextField();
txtAuthorDetails = new javax.swing.JTextField();
btnUpdate = new javax.swing.JButton();
btnCancel = new javax.swing.JButton();
lblAuthor = new javax.swing.JLabel();
txtAuthor = new javax.swing.JTextField();
btnSearch = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
btnShowSelected = new javax.swing.JButton();
jDialog1.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jDialog1.setTitle("Update Author");
jDialog1.setBackground(java.awt.Color.lightGray);
jDialog1.setBounds(new java.awt.Rectangle(500, 200, 0, 0));
jDialog1.setFocusTraversalPolicyProvider(true);
jDialog1.setMinimumSize(new java.awt.Dimension(450, 400));
jLabel1.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
jLabel1.setText("Full Name");
jLabel2.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
jLabel2.setText("Location");
jLabel3.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
jLabel3.setText("Age");
jLabel5.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
jLabel5.setText("Details");
jLabel6.setFont(new java.awt.Font("Verdana", 1, 18)); // NOI18N
jLabel6.setForeground(new java.awt.Color(66, 95, 211));
jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel6.setText("Update Author");
txtAuthorFullName.setColumns(15);
txtAuthorFullName.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
txtAuthorLocation.setColumns(15);
txtAuthorLocation.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
txtAuthorAge.setColumns(15);
txtAuthorAge.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
txtAuthorAge.setToolTipText("In Digit ex.24");
txtAuthorDetails.setColumns(15);
txtAuthorDetails.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
btnUpdate.setBackground(new java.awt.Color(153, 153, 255));
btnUpdate.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
btnUpdate.setText("Update");
btnUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnUpdateActionPerformed(evt);
}
});
btnCancel.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
btnCancel.setText("Cancel");
btnCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCancelActionPerformed(evt);
}
});
javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
jDialog1.getContentPane().setLayout(jDialog1Layout);
jDialog1Layout.setHorizontalGroup(
jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jDialog1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jDialog1Layout.createSequentialGroup()
.addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jDialog1Layout.createSequentialGroup()
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(7, 7, 7))
.addGroup(jDialog1Layout.createSequentialGroup()
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(38, 38, 38))
.addGroup(jDialog1Layout.createSequentialGroup()
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(19, 19, 19)))
.addGap(121, 121, 121)
.addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtAuthorDetails)
.addComponent(txtAuthorAge)
.addComponent(txtAuthorLocation)
.addComponent(txtAuthorFullName)
.addGroup(jDialog1Layout.createSequentialGroup()
.addComponent(btnUpdate)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btnCancel)))
.addGap(35, 35, 35))
.addGroup(jDialog1Layout.createSequentialGroup()
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(23, 23, 23))))
);
jDialog1Layout.setVerticalGroup(
jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jDialog1Layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(jLabel6)
.addGap(40, 40, 40)
.addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(txtAuthorFullName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtAuthorLocation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(txtAuthorAge, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(txtAuthorDetails, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(34, 34, 34)
.addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnUpdate)
.addComponent(btnCancel))
.addContainerGap(44, Short.MAX_VALUE))
);
addAncestorListener(new javax.swing.event.AncestorListener() {
public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
}
public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
formAncestorAdded(evt);
}
public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
}
});
lblAuthor.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
lblAuthor.setText("Author's Name");
txtAuthor.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
btnSearch.setText("Search");
btnSearch.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSearchActionPerformed(evt);
}
});
jTable1.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"id", "Full Name", "Location", "Age", "Details"
}
));
jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
jTable1.setName(""); // NOI18N
jTable1.setRequestFocusEnabled(false);
jTable1.setRowHeight(22);
jScrollPane1.setViewportView(jTable1);
btnShowSelected.setText("Show Selected");
btnShowSelected.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnShowSelectedActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(78, 78, 78)
.addComponent(lblAuthor)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(btnSearch)
.addGap(35, 35, 35)
.addComponent(btnShowSelected)))
.addContainerGap(340, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblAuthor)
.addComponent(txtAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(17, 17, 17)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnSearch)
.addComponent(btnShowSelected))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
.addContainerGap())
);
}// </editor-fold>//GEN-END:initComponents
private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
String authorFullName = txtAuthor.getText();
fillTableSearch(authorFullName);
//JOptionPane.showMessageDialog(this, "Under developement ...");
}//GEN-LAST:event_btnSearchActionPerformed
private void btnShowSelectedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowSelectedActionPerformed
int row = jTable1.getSelectedRow();
System.out.println("row"+row);
if (row == -1) {
JOptionPane.showMessageDialog(this, "Select a row");
}
else{
AuthorTableModel tableModel = (AuthorTableModel) jTable1.getModel();
Author author = tableModel.getRowObject(row);
txtAuthorAge.setText(String.valueOf(author.getAge()));
txtAuthorFullName.setText(author.getFull_name());
txtAuthorDetails.setText(author.getDetails());
txtAuthorLocation.setText(author.getLocation());
jDialog1.setVisible(true);
}
}//GEN-LAST:event_btnShowSelectedActionPerformed
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
jDialog1.dispose();
}//GEN-LAST:event_btnCancelActionPerformed
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
//We need author object. Lets again fetch it from tablemodel
AuthorTableModel tableModel = (AuthorTableModel) jTable1.getModel();
int row = jTable1.getSelectedRow();
int age = 0;
Author author = tableModel.getRowObject(row);
author.setFull_name(txtAuthorFullName.getText());
try{
age = Integer.parseInt(txtAuthorAge.getText());
}catch(NumberFormatException e){
System.err.println("number not given, taken as 0");
//e.printStackTrace();
}
author.setAge(age);
author.setLocation(txtAuthorLocation.getText());
author.setDetails(txtAuthorDetails.getText());
AuthorModel authorModel = new AuthorModel();
int update_flag = authorModel.update(author);
if(update_flag == 1){
//tableModel.fireTableRowsUpdated(row-1, row+1);
tableModel.fireTableDataChanged();
jDialog1.dispose();
JOptionPane.showMessageDialog(this, "Author "+author.getFull_name()+" updated!", "Success!", JOptionPane.INFORMATION_MESSAGE);
}
}//GEN-LAST:event_btnUpdateActionPerformed
private void formAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_formAncestorAdded
System.out.println("ancestor event");
/* TRYING TO REFRESH : IT DOES NOT WORK !! **/
/*AuthorTableModel tableModel = (AuthorTableModel) jTable1.getModel();
jTable1.revalidate();
tableModel.fireTableDataChanged();*/
}//GEN-LAST:event_formAncestorAdded
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnCancel;
private javax.swing.JButton btnSearch;
private javax.swing.JButton btnShowSelected;
private javax.swing.JButton btnUpdate;
private javax.swing.JDialog jDialog1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JLabel lblAuthor;
private javax.swing.JTextField txtAuthor;
private javax.swing.JTextField txtAuthorAge;
private javax.swing.JTextField txtAuthorDetails;
private javax.swing.JTextField txtAuthorFullName;
private javax.swing.JTextField txtAuthorLocation;
// End of variables declaration//GEN-END:variables
}