Newer
Older
POS_for_GUI / src / panels / ShowHistoryPanel.java
package panels;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import frames.IMainFrame;
import resources.Customer;
import resources.CustomersModel;

public class ShowHistoryPanel extends JPanel {

	private IMainFrame mainFrame;

	private Customer selectedCustomer;
	private CustomersModel model;

	private JList historiesList;
	private DefaultListModel<Integer> historiesListModel;

	private JLabel nameLabel;
	private JTextField textField;
	private JButton backButton;
	private JButton payButton;

	public ShowHistoryPanel(IMainFrame mainFrame, CustomersModel model) {
		super(new BorderLayout());

		this.mainFrame = mainFrame;

		this.model = model;

		historiesListModel = new DefaultListModel<Integer>();
		historiesList = new JList(historiesListModel);

		BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
		this.setLayout(layout);

		nameLabel = new JLabel();
		nameLabel.setAlignmentX(CENTER_ALIGNMENT);
		add(nameLabel);

		JScrollPane scrollPanel = new JScrollPane(historiesList);
		add(scrollPanel);

		JLabel inputLabel = new JLabel("payment?");
		textField = new JTextField(25);
		payButton = new JButton("Payment");
		backButton = new JButton("Back");
		backButton.setAlignmentX(CENTER_ALIGNMENT);

		JPanel panel = new JPanel();
		panel.add(inputLabel);
		panel.add(textField);
		panel.add(payButton);
		panel.add(backButton);

		this.add(panel);
		this.add(backButton);

		backButton.addActionListener(new BackActionHandler());
		payButton.addActionListener(new PaymentActionHandler());

	}

	public void updateListModelByIndex(int customerId) {
		selectedCustomer = model.getCustomerById(customerId);

		historiesListModel.removeAllElements();
		historiesListModel.addAll(model.getCustomerById(customerId).getHistory());

		nameLabel.setText("name: " + selectedCustomer.getName());
	}

	private void transitionToMain() {
		mainFrame.showMainPanel();
	}

	private class BackActionHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			transitionToMain();
		}
	}

	private class PaymentActionHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			String text = textField.getText();

			if (!text.matches("^[0-9]+$"))
				return;
			if (Integer.parseInt(text) <= 0)
				return;

			selectedCustomer.purchase(Integer.parseInt(textField.getText()));
			historiesListModel.removeAllElements();
			historiesListModel.addAll(selectedCustomer.getHistory());
		}
	}
}