/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.charitas.view;
import com.charitas.dao.CategoryDAO;
import com.charitas.dao.ProductDAO;
import com.charitas.dao.impl.CategoryDAOImpl;
import com.charitas.db.DatabaseConnection;
import com.charitas.model.Category;
import com.charitas.model.Product;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
/**
*
* @author edepe1
*/
public class DialogProduct extends javax.swing.JDialog {
public int getMode() {
return mode;
}
public void setMode(int mode) {
this.mode = mode;
}
public Product getProduct() {
packData();
return product;
}
public void setProduct(Product product) {
this.product = product;
unPackData();
}
/**
* Creates new form DialogProduct
*/
public DialogProduct(java.awt.Frame parent, boolean modal) {
super(parent, modal);
try {
initComponents();
categoryDAO = new CategoryDAOImpl(DatabaseConnection.getInstance().getConnection());
Calendar cal = Calendar.getInstance();
DateChooserExpired.setDate(cal.getTime());
} catch (ClassNotFoundException ex) {
Logger.getLogger(DialogProduct.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(DialogProduct.class.getName()).log(Level.SEVERE, null, ex);
}
reModelComboCategory();
}
/**
* 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() {
jLabel2 = new javax.swing.JLabel();
TxtName = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
FormattedTextPrice = new javax.swing.JFormattedTextField();
ComboBoxCategory = new javax.swing.JComboBox();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
TextAreaDescription = new javax.swing.JTextArea();
Simpan = new javax.swing.JButton();
Cancel = new javax.swing.JButton();
DateChooserExpired = new com.toedter.calendar.JDateChooser();
jLabel2.setText("Nama");
jLabel3.setText("Price");
jLabel4.setText("Category");
ComboBoxCategory.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jLabel5.setText("Expired");
jLabel6.setText("Description");
TextAreaDescription.setColumns(20);
TextAreaDescription.setRows(5);
jScrollPane1.setViewportView(TextAreaDescription);
Simpan.setText("simpan");
Simpan.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
SimpanActionPerformed(evt);
}
});
Cancel.setText("cancel");
Cancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CancelActionPerformed(evt);
}
});
DateChooserExpired.setDateFormatString("dd - MM - yyyy");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(20, 20, 20)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel6)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(TxtName)
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(DateChooserExpired, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
.addComponent(ComboBoxCategory, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(FormattedTextPrice, javax.swing.GroupLayout.Alignment.LEADING))
.addGap(101, 101, 101))
.addGroup(layout.createSequentialGroup()
.addComponent(Simpan, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
.addGap(26, 26, 26)
.addComponent(Cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(39, 39, 39)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(TxtName, 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(FormattedTextPrice, 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(ComboBoxCategory, 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.TRAILING)
.addComponent(jLabel5)
.addComponent(DateChooserExpired, 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.LEADING)
.addComponent(jLabel6)
.addComponent(jScrollPane1, 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.LEADING)
.addComponent(Simpan, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
.addComponent(Cancel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(21, 21, 21))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void SimpanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SimpanActionPerformed
// TODO add your handling code here:
mode = SAVE_MODE;
this.setVisible(false);
}//GEN-LAST:event_SimpanActionPerformed
private void CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelActionPerformed
// TODO add your handling code here:
mode = CANCEL_MODE;
this.setVisible(false);
}//GEN-LAST:event_CancelActionPerformed
/**
* @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(DialogProduct.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DialogProduct.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DialogProduct.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DialogProduct.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() {
DialogProduct dialog = new DialogProduct(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 Cancel;
private javax.swing.JComboBox ComboBoxCategory;
private com.toedter.calendar.JDateChooser DateChooserExpired;
private javax.swing.JFormattedTextField FormattedTextPrice;
private javax.swing.JButton Simpan;
private javax.swing.JTextArea TextAreaDescription;
private javax.swing.JTextField TxtName;
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;
// End of variables declaration//GEN-END:variables
public final static int CANCEL_MODE = 0;
public final static int SAVE_MODE = 0;
private int mode = 0;
private Product product;
private List<Category> categorys;
//private ProductDAO productDAO;
private CategoryDAO categoryDAO;
private void packData() {
if (product == null) {
product = new Product();
}
product.setName(TxtName.getText());
product.setDescription(TextAreaDescription.getText());
product.setExpiredDate(DateChooserExpired.getDate());
product.setCategory(categorys.get(ComboBoxCategory.getSelectedIndex()));
try {
FormattedTextPrice.commitEdit();
} catch (ParseException ex) {
Logger.getLogger(DialogProduct.class.getName()).log(Level.SEVERE, null, ex);
}
Object value = FormattedTextPrice.getValue();
product.setPrice(new Double(value + ""));
}
private void unPackData() {
clearComponent();
if (product == null) {
return;
}
TxtName.setText(product.getName());
TextAreaDescription.setText(product.getDescription());
FormattedTextPrice.setValue(product.getPrice());
reModelComboCategory();
ComboBoxCategory.setSelectedItem(product.getCategory() == null ? null : product.getCategory().getName());
DateChooserExpired.setDate(product.getExpiredDate());
try {
FormattedTextPrice.commitEdit();
} catch (ParseException ex) {
Logger.getLogger(DialogProduct.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void clearComponent() {
TxtName.setText("");
TextAreaDescription.setText("");
FormattedTextPrice.setValue(null);
ComboBoxCategory.setSelectedIndex(-1);
DateChooserExpired.setDate(null);
try {
FormattedTextPrice.commitEdit();
} catch (ParseException ex) {
Logger.getLogger(DialogProduct.class.getName()).log(Level.SEVERE, null, ex);
}
}
public final void reModelComboCategory() {
categorys = new ArrayList<Category>();
try {
categorys = categoryDAO.findAll();
} catch (SQLException ex) {
Logger.getLogger(DialogProduct.class.getName()).log(Level.SEVERE, null, ex);
}
String[] data = new String[categorys == null ? 0 : categorys.size()];
for (int i = 0; i < data.length; i++) {
data[i] = categorys.get(i).getName();
}
DefaultComboBoxModel model = new DefaultComboBoxModel(data);
ComboBoxCategory.setModel(model);
ComboBoxCategory.setSelectedIndex(-1);
}
}