/*
* 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 Models.Pojo.Author;
import Models.AuthorModel;
import Models.BookModel;
import Models.Pojo.Book;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
/**
*
* @author Tejas
*/
public class panelNewBook extends javax.swing.JPanel {
/**
* Creates new form panelNewBook
*/
public panelNewBook() {
initComponents();
fillComboBox();
}
private void fillComboBox(){
ArrayList arrayList = new ArrayList(); // vector is Obsolete. Hence ArrayList
AuthorModel authorModel = new AuthorModel();
List<Author> authors = authorModel.all(); // This should be query based (Type text to fetch).
// Loading everything at once is just unnecessary.
for (Author author : authors) {
arrayList.add(author);
}
cmboxAuthor.setModel(new javax.swing.DefaultComboBoxModel(arrayList.toArray()));
AutoCompleteDecorator.decorate(cmboxAuthor); // inject AutoComplete Feature.
}
private void clearAllInputFields(){
txtBookName.setText(null);
txtBookPrice.setText(null);
txtBookTotalPages.setText(null);
}
/**
* 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() {
lblBookName = new javax.swing.JLabel();
lblAuthor = new javax.swing.JLabel();
lblPrice = new javax.swing.JLabel();
lblTotalPages = new javax.swing.JLabel();
lblTitle = new javax.swing.JLabel();
txtBookName = new javax.swing.JTextField();
txtBookPrice = new javax.swing.JTextField();
txtBookTotalPages = new javax.swing.JTextField();
btnSubmitBook = new javax.swing.JButton();
cmboxAuthor = new javax.swing.JComboBox();
lblBookName.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
lblBookName.setText("Book Name");
lblAuthor.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
lblAuthor.setText("Author");
lblPrice.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
lblPrice.setText("Price");
lblTotalPages.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
lblTotalPages.setText("Total Pages");
lblTitle.setFont(new java.awt.Font("Verdana", 1, 18)); // NOI18N
lblTitle.setForeground(new java.awt.Color(66, 95, 211));
lblTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblTitle.setText("New Book");
txtBookName.setColumns(15);
txtBookName.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
txtBookPrice.setColumns(15);
txtBookPrice.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
txtBookPrice.setToolTipText("In Digit ex.24");
txtBookTotalPages.setColumns(15);
txtBookTotalPages.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
btnSubmitBook.setFont(new java.awt.Font("Verdana", 0, 13)); // NOI18N
btnSubmitBook.setText("Submit");
btnSubmitBook.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSubmitBookActionPerformed(evt);
}
});
cmboxAuthor.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmboxAuthorActionPerformed(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(100, 100, 100)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 411, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblBookName)
.addComponent(lblAuthor)
.addComponent(lblPrice)
.addComponent(lblTotalPages))
.addGap(121, 121, 121)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtBookTotalPages)
.addComponent(txtBookPrice)
.addComponent(btnSubmitBook, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(cmboxAuthor, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txtBookName, javax.swing.GroupLayout.PREFERRED_SIZE, 1, Short.MAX_VALUE))
.addGap(62, 62, 62)))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addComponent(lblTitle)
.addGap(40, 40, 40)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblBookName)
.addComponent(txtBookName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblAuthor)
.addComponent(cmboxAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblPrice)
.addComponent(txtBookPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblTotalPages)
.addComponent(txtBookTotalPages, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(37, 37, 37)
.addComponent(btnSubmitBook)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
}// </editor-fold>//GEN-END:initComponents
private void cmboxAuthorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmboxAuthorActionPerformed
// TODO add your handling code here:
Author author = (Author)cmboxAuthor.getSelectedItem();
System.out.println("Selected --"+author.getFull_name());
}//GEN-LAST:event_cmboxAuthorActionPerformed
private void btnSubmitBookActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitBookActionPerformed
//Prepare all the user inputed data
String bookName = txtBookName.getText();
float price = Float.parseFloat(txtBookPrice.getText());
int total_pages = Integer.parseInt(txtBookTotalPages.getText());
//set author id from combobox
Author author = (Author)cmboxAuthor.getSelectedItem();
int author_id = author.getId();
Book book = new Book();
book.setName(bookName);
book.setAuthor_id(author_id);
book.setPrice(price);
book.setTotal_pages(total_pages);
BookModel bookModel = new BookModel();
int flag = bookModel.save(book);
if(flag == 1)
{
JOptionPane.showMessageDialog(null, "Book added !", "Message", JOptionPane.INFORMATION_MESSAGE);
clearAllInputFields();
}
else{
JOptionPane.showMessageDialog(null, "Failed to add book!", "Message", JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_btnSubmitBookActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnSubmitBook;
private javax.swing.JComboBox cmboxAuthor;
private javax.swing.JLabel lblAuthor;
private javax.swing.JLabel lblBookName;
private javax.swing.JLabel lblPrice;
private javax.swing.JLabel lblTitle;
private javax.swing.JLabel lblTotalPages;
private javax.swing.JTextField txtBookName;
private javax.swing.JTextField txtBookPrice;
private javax.swing.JTextField txtBookTotalPages;
// End of variables declaration//GEN-END:variables
}