Newer
Older
POS_for_GUI / src / panels / ShowHistoryPanel.java
student on 11 Apr 2022 4 KB [update]detail panel
package panels;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;

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

//-------------------------------------------------------------------------
//
public class ShowHistoryPanel extends JPanel {

	// frame
	private IMainFrame mainFrame;

	// model
	private Customer selectedCustomer;
	private CustomersModel model;

	// table
	private JList historiesList;
	private DefaultListModel<Integer> historiesListModel;

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

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

		// initialize frame
		this.mainFrame = mainFrame;

		// initialize model
		this.model = model;

		// initialize table
		historiesListModel = new DefaultListModel();
		historiesList = new JList(historiesListModel);

//		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		// add labels
		nameLabel = new JLabel();
		add(nameLabel, BorderLayout.NORTH);

		// add scroll menu
		JScrollPane scrollPanel = new JScrollPane(historiesList);
		add(scrollPanel, BorderLayout.CENTER);
		
		// add payment fields
		textField = new JTextField(1);
		payButton = new JButton("Payment");

		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
		panel.add(payButton);
		panel.add(textField, new Dimension(1,1));
		add(panel);

		// add back button
		backButton = new JButton("Back");
		add(backButton, BorderLayout.NORTH);
		

		// add listener
		backButton.addActionListener(new BackActionHandler());
		payButton.addActionListener(new PaymentActionHandler());

	}

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// public
	// -------------------------------------------------------------------------
	//
	public void updateListModelByIndex(int index) {
		selectedCustomer = model.getCustomers().get(index);

		historiesListModel.removeAllElements();
		historiesListModel.addAll(model.getCustomers().get(index).getHistory());

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

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// private
	// -------------------------------------------------------------------------
	//
	private void transitionToMain(IMainFrame mainFrame) {
		mainFrame.showMainPanel(this);
	}

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// interface
	// -------------------------------------------------------------------------
	// back button's event
	private class BackActionHandler implements ActionListener {

		// -------------------------------------------------------------------------
		//
		public void actionPerformed(ActionEvent e) {
			transitionToMain(mainFrame);
		}
	}

	// -------------------------------------------------------------------------
	// payment button's event
	private class PaymentActionHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			String text = textField.getText();

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

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