/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.charitas.view;
import com.charitas.dao.ProductDAO;
import com.charitas.dao.impl.ProductDAOImpl;
import com.charitas.db.DatabaseConnection;
import com.charitas.model.Product;
import com.charitas.util.CustomeTabelModel;
import com.charitas.util.SwingUtil;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
/**
*
* @author edepe1
*/
public class InternalFrameProduct extends javax.swing.JInternalFrame {
/**
* Creates new form InternalFrameProduct
*/
public InternalFrameProduct() {
initComponents();
try {
productDAO = new ProductDAOImpl(DatabaseConnection.getInstance().getConnection() );
} catch (ClassNotFoundException ex) {
Logger.getLogger(InternalFrameProduct.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(InternalFrameProduct.class.getName()).log(Level.SEVERE, null, ex);
}
reloadProduct();
}
public final void reloadProduct(){
List<Product> products = null;
try {
products = productDAO.findAll();
} catch (SQLException ex) {
Logger.getLogger(InternalFrameProduct.class.getName()).log(Level.SEVERE, null, ex);
}
int row = ( products == null ? 0 : products.size() );
int col = 6;
Object[][] data = new Object[row][col];
for (int i = 0; products != null && i < products.size(); i++ ){
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
Product product = products.get(i);
DecimalFormat decimalFormat = new DecimalFormat("#,###.00");
data[i][0] = product.getId();
data[i][1] = product.getName();
data[i][2] = product.getCategory().getName();
data[i][3] = decimalFormat.format(product.getPrice());
//product.getPrice();
data[i][4] = sdf.format(product.getExpiredDate());
//product.getExpiredDate();
data[i][5] = product.getDescription();
}
String[] Judul = {"id","nama","category", "price","expireed","desc"};
//TableModel tableModel = new DefaultTableModel(data, Judul);
//TableProduct.setModel(tableModel);
CustomeTabelModel tableModel = new CustomeTabelModel(data, Judul);
TableProduct.setModel(tableModel);
}
/**
* 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() {
ButtonAdd = new javax.swing.JButton();
ButtonDelete = new javax.swing.JButton();
ButtonReload = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
TableProduct = new javax.swing.JTable();
setClosable(true);
setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
setIconifiable(true);
setMaximizable(true);
setResizable(true);
setTitle("Product");
ButtonAdd.setText("Add");
ButtonAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ButtonAddActionPerformed(evt);
}
});
ButtonDelete.setText("Delete");
ButtonDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ButtonDeleteActionPerformed(evt);
}
});
ButtonReload.setText("Reload");
ButtonReload.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ButtonReloadActionPerformed(evt);
}
});
TableProduct.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"
}
));
TableProduct.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
TableProductMouseClicked(evt);
}
});
jScrollPane1.setViewportView(TableProduct);
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, javax.swing.GroupLayout.DEFAULT_SIZE, 425, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(ButtonAdd)
.addGap(18, 18, 18)
.addComponent(ButtonDelete)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(ButtonReload)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(ButtonAdd)
.addComponent(ButtonDelete)
.addComponent(ButtonReload))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void ButtonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonAddActionPerformed
// TODO add your handling code here:
if (dialog == null ){
dialog = new DialogProduct(SwingUtil.getParentJFrame(this), true);
}
dialog.setProduct(new Product());
dialog.setVisible(true);
if ( dialog.getMode() == DialogProduct.SAVE_MODE ){
Product product = dialog.getProduct();
try{
if (product.getId() <=0 ){
productDAO.save(product);
}else{
productDAO.update(product.getId(), product);
}
} catch(SQLException ex){
JOptionPane.showMessageDialog(this, ex.getMessage(),"Sql Exception",JOptionPane.ERROR_MESSAGE);
}
reloadProduct();
}
}//GEN-LAST:event_ButtonAddActionPerformed
private void ButtonReloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonReloadActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_ButtonReloadActionPerformed
private void ButtonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonDeleteActionPerformed
int id =(Integer) TableProduct.getValueAt(TableProduct.getSelectedRow(), 0);
try {
// TODO add your handling code here:
productDAO.delete(id);
} catch (SQLException ex) {
Logger.getLogger(InternalFrameProduct.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this, ex.getMessage(),"Sql Exception",JOptionPane.ERROR_MESSAGE );
}
reloadProduct();
}//GEN-LAST:event_ButtonDeleteActionPerformed
private void TableProductMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TableProductMouseClicked
// TODO add your handling code here:
if(evt.getClickCount() ==2 ){
if (dialog == null){
dialog = new DialogProduct(SwingUtil.getParentJFrame(this), true);
}
int id = (Integer) TableProduct.getValueAt(TableProduct.getSelectedRow(), 0);
Product product = null;
try {
product = productDAO.findById(id);
} catch (SQLException ex) {
Logger.getLogger(InternalFrameProduct.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this, ex.getMessage(),"Sql Exception", JOptionPane.ERROR_MESSAGE);
return ;
}
dialog.setProduct(product);
dialog.setVisible(true);
if(dialog.getMode() == DialogProduct.SAVE_MODE){
product = dialog.getProduct();
try {
productDAO.update(product.getId(), product );
} catch (SQLException ex) {
Logger.getLogger(InternalFrameProduct.class.getName()).log(Level.SEVERE, null, ex);
}
}
reloadProduct();
}
}//GEN-LAST:event_TableProductMouseClicked
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton ButtonAdd;
private javax.swing.JButton ButtonDelete;
private javax.swing.JButton ButtonReload;
private javax.swing.JTable TableProduct;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables
private DialogProduct dialog;
private ProductDAO productDAO;
}