TavernerPOS - Source Code
Clone:
git clone http://www.librarysmith.co.uk/tavernerPOS
/*
* CouponAndDiscountDialog.java
*
* Created on September 23, 2007, 1:59 PM
*/
package com.floreantpos.ui.dialog;
import java.awt.Component;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.AbstractListModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import com.floreantpos.IconFactory;
import com.floreantpos.Messages;
import com.floreantpos.PosException;
import com.floreantpos.model.CouponAndDiscount;
import com.floreantpos.model.Ticket;
import com.floreantpos.model.TicketCouponAndDiscount;
import com.floreantpos.model.dao.CouponAndDiscountDAO;
import com.floreantpos.util.NumberUtil;
/**
*
* @author mahbub.shahriar
*/
public class CouponAndDiscountDialog extends POSDialog implements ActionListener, ListSelectionListener{
private List<CouponAndDiscount> couponList;
private TicketCouponAndDiscount ticketCoupon;
private Ticket ticket;
/** Creates new form CouponAndDiscountDialog */
public CouponAndDiscountDialog() {
super();
initComponents();
tfValue.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
try {
double totalDiscount = Double.parseDouble(tfValue.getText());
lblTotalDiscount.setText(NumberUtil.formatNumber(totalDiscount));
} catch (Exception x) {
}
}
public void removeUpdate(DocumentEvent e) {
try {
double totalDiscount = Double.parseDouble(tfValue.getText());
lblTotalDiscount.setText(NumberUtil.formatNumber(totalDiscount));
} catch (Exception x) {
}
}
public void changedUpdate(DocumentEvent e) {
try {
double totalDiscount = Double.parseDouble(tfValue.getText());
lblTotalDiscount.setText(NumberUtil.formatNumber(totalDiscount));
} catch (Exception x) {
}
}
});
lblTotalDiscount.setText(""); //$NON-NLS-1$
btnEditValue.setEnabled(false);
btnUp.setActionCommand("scrollUP"); //$NON-NLS-1$
btnDown.setActionCommand("scrollDown"); //$NON-NLS-1$
btnUp.addActionListener(this);
btnDown.addActionListener(this);
listCoupons.addListSelectionListener(this);
listCoupons.setCellRenderer(new CouponListRenderer());
ticketCoupon = new TicketCouponAndDiscount();
}
/** 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
titlePanel1 = new com.floreantpos.ui.TitlePanel();
jScrollPane1 = new javax.swing.JScrollPane();
listCoupons = new javax.swing.JList();
btnCancel = new com.floreantpos.swing.PosButton();
btnOk = new com.floreantpos.swing.PosButton();
jSeparator1 = new javax.swing.JSeparator();
btnUp = new com.floreantpos.swing.PosButton();
btnDown = new com.floreantpos.swing.PosButton();
jSeparator2 = new javax.swing.JSeparator();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
tfName = new javax.swing.JTextField();
tfNumber = new javax.swing.JTextField();
tfType = new javax.swing.JTextField();
tfValue = new javax.swing.JTextField();
btnEditValue = new javax.swing.JButton();
jSeparator3 = new javax.swing.JSeparator();
jLabel5 = new javax.swing.JLabel();
lblTotalDiscount = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
titlePanel1.setTitle(Messages.getString("CouponAndDiscountDialog.3")); //$NON-NLS-1$
jScrollPane1.setViewportView(listCoupons);
btnCancel.setIcon(IconFactory.getIcon("/ui_icons/", "cancel.png")); // NOI18N //$NON-NLS-1$ //$NON-NLS-2$
btnCancel.setText(Messages.getString("CouponAndDiscountDialog.6")); //$NON-NLS-1$
btnCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
doCancel(evt);
}
});
btnOk.setIcon(IconFactory.getIcon("/ui_icons/", "finish.png")); // NOI18N //$NON-NLS-1$ //$NON-NLS-2$
btnOk.setText(Messages.getString("CouponAndDiscountDialog.9")); //$NON-NLS-1$
btnOk.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
doOk(evt);
}
});
btnUp.setIcon(IconFactory.getIcon("/ui_icons/", "up.png")); // NOI18N //$NON-NLS-1$ //$NON-NLS-2$
btnDown.setIcon(IconFactory.getIcon("/ui_icons/", "down.png")); // NOI18N //$NON-NLS-1$ //$NON-NLS-2$
jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);
jLabel1.setText(Messages.getString("CouponAndDiscountDialog.14") + ":"); //$NON-NLS-1$ //$NON-NLS-2$
jLabel2.setText(Messages.getString("CouponAndDiscountDialog.16") + ":"); //$NON-NLS-1$ //$NON-NLS-2$
jLabel3.setText(Messages.getString("CouponAndDiscountDialog.18") + ":"); //$NON-NLS-1$ //$NON-NLS-2$
jLabel4.setText(Messages.getString("CouponAndDiscountDialog.20") + ":"); //$NON-NLS-1$ //$NON-NLS-2$
tfName.setEditable(false);
tfNumber.setEditable(false);
tfType.setEditable(false);
tfValue.setEditable(false);
btnEditValue.setText(Messages.getString("CouponAndDiscountDialog.22")); //$NON-NLS-1$
btnEditValue.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
doEnterValue(evt);
}
});
jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18)); //$NON-NLS-1$
jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel5.setText(Messages.getString("CouponAndDiscountDialog.24")); //$NON-NLS-1$
lblTotalDiscount.setFont(new java.awt.Font("Tahoma", 1, 18)); //$NON-NLS-1$
lblTotalDiscount.setForeground(new java.awt.Color(204, 51, 0));
lblTotalDiscount.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 642, Short.MAX_VALUE)
.add(titlePanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 642, Short.MAX_VALUE)
.add(layout.createSequentialGroup()
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 216, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(btnUp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(btnDown, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(btnOk, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 114, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(btnCancel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 117, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(17, 17, 17)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel4)
.add(jLabel3)
.add(jLabel2)
.add(jLabel1))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(tfValue, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(btnEditValue))
.add(tfType, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
.add(tfNumber, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
.add(tfName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)))
.add(layout.createSequentialGroup()
.add(18, 18, 18)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
.add(jSeparator3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
.add(lblTotalDiscount, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE))))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(titlePanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
.add(jSeparator2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel1)
.add(tfName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel2)
.add(tfNumber, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel3)
.add(tfType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jLabel4)
.add(btnEditValue)
.add(tfValue, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jSeparator3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(5, 5, 5)
.add(btnUp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(btnDown, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jLabel5)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(lblTotalDiscount)))))
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(btnCancel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(btnOk, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
public TicketCouponAndDiscount getSelectedCoupon() {
try {
double parseDouble = NumberUtil.parse(tfValue.getText()).doubleValue();
ticketCoupon.setValue(parseDouble);
}catch(Exception x) {
throw new PosException(Messages.getString("CouponAndDiscountDialog.27")); //$NON-NLS-1$
}
return ticketCoupon;
}
private void doEnterValue(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doEnterValue
NumberSelectionDialog2 dialog = new NumberSelectionDialog2();
dialog.setFloatingPoint(true);
dialog.setTitle(Messages.getString("CouponAndDiscountDialog.28")); //$NON-NLS-1$
dialog.pack();
dialog.open();
if(!dialog.isCanceled()) {
double value = dialog.getValue();
tfValue.setText(NumberUtil.formatNumber(value));
}
}//GEN-LAST:event_doEnterValue
private void doOk(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doOk
try {
TicketCouponAndDiscount selectedCoupon = getSelectedCoupon();
if (selectedCoupon == null) {
POSMessageDialog.showError(this, Messages.getString("CouponAndDiscountDialog.29")); //$NON-NLS-1$
return;
}
setCanceled(false);
dispose();
} catch (PosException e) {
POSMessageDialog.showError(this, e.getMessage());
}
}//GEN-LAST:event_doOk
private void doCancel(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doCancel
setCanceled(true);
dispose();
}//GEN-LAST:event_doCancel
public void initData() throws Exception {
CouponAndDiscountDAO dao = new CouponAndDiscountDAO();
couponList = dao.getValidCoupons();
listCoupons.setModel(new CouponListModel());
}
public void actionPerformed(ActionEvent e) {
if ("scrollUP".equals(e.getActionCommand())) { //$NON-NLS-1$
if (couponList == null || couponList.size() == 0)
return;
int selectedRow = listCoupons.getSelectedIndex();
if (selectedRow <= 0) {
selectedRow = 0;
}
else {
--selectedRow;
}
listCoupons.setSelectedIndex(selectedRow);
Rectangle cellRect = listCoupons.getCellBounds(selectedRow, selectedRow);
listCoupons.scrollRectToVisible(cellRect);
}
else if ("scrollDown".equals(e.getActionCommand())) { //$NON-NLS-1$
if (couponList == null || couponList.size() == 0)
return;
int selectedRow = listCoupons.getSelectedIndex();
if (selectedRow < 0) {
selectedRow = 0;
}
else if (selectedRow >= couponList.size() - 1) {
//return;
}
else {
++selectedRow;
}
listCoupons.setSelectedIndex(selectedRow);
Rectangle cellRect = listCoupons.getCellBounds(selectedRow, selectedRow);
cellRect.y += 20;
listCoupons.scrollRectToVisible(cellRect);
}
}
public void updateCouponView(CouponAndDiscount coupon) {
if (coupon == null) {
tfName.setText(""); //$NON-NLS-1$
tfNumber.setText(""); //$NON-NLS-1$
tfType.setText(""); //$NON-NLS-1$
tfValue.setText(""); //$NON-NLS-1$
return;
}
btnEditValue.setEnabled(false);
tfName.setText(coupon.getName());
if (coupon.getType() == CouponAndDiscount.FREE_AMOUNT) {
btnEditValue.setEnabled(true);
}
tfNumber.setText(String.valueOf(coupon.getId()));
tfType.setText(CouponAndDiscount.COUPON_TYPE_NAMES[coupon.getType()]);
tfValue.setText(NumberUtil.formatNumber(coupon.getValue()));
double totalDiscount = 0;
double subtotal = ticket.getSubtotalAmount();
ticketCoupon.setCouponAndDiscountId(coupon.getId());
ticketCoupon.setName(coupon.getName());
ticketCoupon.setType(coupon.getType());
ticketCoupon.setValue(coupon.getValue());
totalDiscount = ticket.calculateDiscountFromType(ticketCoupon, subtotal);
ticketCoupon.setValue(totalDiscount);
lblTotalDiscount.setText(NumberUtil.formatNumber(totalDiscount));
}
public void valueChanged(ListSelectionEvent e) {
CouponAndDiscount coupon = (CouponAndDiscount) listCoupons.getSelectedValue();
updateCouponView(coupon);
}
public Ticket getTicket() {
return ticket;
}
public void setTicket(Ticket ticket) {
this.ticket = ticket;
}
class CouponListModel extends AbstractListModel {
public int getSize() {
if (couponList == null)
return 0;
return couponList.size();
}
public Object getElementAt(int index) {
return couponList.get(index);
}
}
class CouponListRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
CouponAndDiscount coupon = (CouponAndDiscount) value;
// String displayText = "<html><body>";
// displayText += "<p align='center'>Name:" + coupon.getName() + "</p>";
// displayText += "<p align='center'>" + coupon.getValue() + "</p>";
// displayText += "</body></html>";
return super.getListCellRendererComponent(list, coupon.getName(), index, isSelected, cellHasFocus);
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private com.floreantpos.swing.PosButton btnCancel;
private com.floreantpos.swing.PosButton btnDown;
private javax.swing.JButton btnEditValue;
private com.floreantpos.swing.PosButton btnOk;
private com.floreantpos.swing.PosButton btnUp;
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.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JSeparator jSeparator3;
private javax.swing.JLabel lblTotalDiscount;
private javax.swing.JList listCoupons;
private javax.swing.JTextField tfName;
private javax.swing.JTextField tfNumber;
private javax.swing.JTextField tfType;
private javax.swing.JTextField tfValue;
private com.floreantpos.ui.TitlePanel titlePanel1;
// End of variables declaration//GEN-END:variables
}
- Details
- Last Updated: Monday, 04 April 2016 02:04
- Hits: 39815560