Newer
Older
POS_for_GUI / src / panels / ShowHistoryPanel.java
student on 5 Apr 2022 3 KB [add]featured
package panels;

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

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

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

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

	// frame
	private MainFrame mainFrame;

	// model
	private Customer selectedCustomer;
	private CustomersModel model;

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

	// buttons
	private JButton backButton;
	private JButton payButton;

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// constructor
	// -------------------------------------------------------------------------
	//
	public ShowHistoryPanel(MainFrame 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);

		// add buttons
		JPanel buttonPane = new JPanel();
		backButton = new JButton("Back");
		payButton = new JButton("Payment");
		buttonPane.add(backButton);
		buttonPane.add(payButton);
		add(buttonPane, BorderLayout.SOUTH);

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

		// add scroll menu
		JScrollPane scrollPanel = new JScrollPane(historiesList);
		add(scrollPanel, BorderLayout.NORTH);

	}

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// public
	// -------------------------------------------------------------------------
	//
	public void updateListModelByIndex(int index) {
		selectedCustomer = model.getCustomers().get(index);
		historiesListModel.removeAllElements();
		historiesListModel.addAll(model.getCustomers().get(index).getHistory());
	}

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// private
	// -------------------------------------------------------------------------
	//
	private void transitionToMain(MainFrame 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) {
			selectedCustomer.purchase(1000);
			historiesListModel.removeAllElements();
			historiesListModel.addAll(selectedCustomer.getHistory());
		}
	}

}