Newer
Older
POS_for_GUI / src / panels / MainPanel.java
student on 5 Apr 2022 5 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.EventListener;
import java.util.HashMap;
import java.util.Random;

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.JTextField;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

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

//-------------------------------------------------------------------------
// overview of customers
public class MainPanel extends JPanel {

	// frame
	private MainFrame mainFrame;

	// model
	private CustomersModel model;

	// table
	private JTable panelTable;
	private DefaultTableModel panelTableModel;
	private String[] columnNames = { "name", "point", "total" };

	// buttons
	private JButton addButton;
	private JButton removeButton;
	private JButton showDetailButton;

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// constructor
	// -------------------------------------------------------------------------
	//
	public MainPanel(MainFrame mainFrame, CustomersModel model) {

		super(new BorderLayout());

		// initialize frame
		this.mainFrame = mainFrame;

		// initialize model
		this.model = model;
//		customerTable = new HashMap<Integer, Customer>();

		// initialize table
		panelTableModel = new DefaultTableModel(columnNames, 0);
		panelTable = new JTable(panelTableModel);
		// deny to edit cell
		panelTable.setDefaultEditor(Object.class, null);

		// add buttons
		addButton = new JButton("Add");
		removeButton = new JButton("Remove");
		showDetailButton = new JButton("Show History");

		// add listener
		addButton.addActionListener(new AddActionHandler());
		removeButton.addActionListener(new RemoveActionHandler());
		showDetailButton.addActionListener(new ShowHistoryActionHandler());

		JPanel buttonPanel = new JPanel();
		buttonPanel.add(addButton, BorderLayout.WEST);
		buttonPanel.add(removeButton, BorderLayout.EAST);
		add(buttonPanel, BorderLayout.CENTER);

		// add show detail
		JPanel showButtonPanel = new JPanel();
		FlowLayout flowLayout = new FlowLayout();
		showButtonPanel.add(showDetailButton, BorderLayout.SOUTH);
		showButtonPanel.setLayout(flowLayout);
		add(showButtonPanel, BorderLayout.SOUTH);

		// add scroll menu
		JScrollPane listScrollPane = new JScrollPane(panelTable);
		add(listScrollPane, BorderLayout.NORTH);

	}

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// public
	// -------------------------------------------------------------------------
	//
	public void updateTableByTransition() {
		for (int i = 0; i < panelTableModel.getRowCount(); i++) {
			// name, points, total
			panelTableModel.setValueAt(model.getCustomers().get(i).getName(), i, 0);
			panelTableModel.setValueAt(model.getCustomers().get(i).getPoints(), i, 1);
			panelTableModel.setValueAt(model.getCustomers().get(i).getTotal(), i, 2);
		}
	}

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// private
	// -------------------------------------------------------------------------
	//
	private void transitionToHistory(MainFrame mainFrame, int index) {
		mainFrame.showHistoryPanel(this, index);
	}

	// -------------------------------------------------------------------------
	//
	private Customer createNewCustomerByName(String name) {
		Customer customer = new Customer(name + model.getCustomers().size());
		return customer;
	}

	// -------------------------------------------------------------------------
	//
	private Object[] generateCustomerRow(Customer customer) {
		Object[] data = { customer.getName(), customer.getPoints(), customer.getTotal() };
		return data;
	}

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// interface
	// -------------------------------------------------------------------------
	// add button's event
	private class AddActionHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			Customer customer = createNewCustomerByName("name");
			model.getCustomers().add(customer);
			panelTableModel.addRow(generateCustomerRow(customer));
		}
	}

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

			if (panelTable.getSelectedRow() < 0)
				return;

			if (1 < panelTable.getRowCount()) {
				model.getCustomers().remove(panelTable.getSelectedRow());
				panelTableModel.removeRow(panelTable.getSelectedRow());
			}
		}
	}

	// -------------------------------------------------------------------------
	// transition to history
	private class ShowHistoryActionHandler implements ActionListener, EventListener {

		// -------------------------------------------------------------------------
		//
		public void actionPerformed(ActionEvent e) {
			if (-1 < panelTable.getSelectedRow())
				transitionToHistory(mainFrame, panelTable.getSelectedRow());
		}
	}
}