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 { // 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); BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS); this.setLayout(layout); // nameLabel = new JLabel(); nameLabel.setAlignmentX(CENTER_ALIGNMENT); add(nameLabel); // add scroll menu JScrollPane scrollPanel = new JScrollPane(historiesList); add(scrollPanel); // add payment fields 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); add(panel); add(backButton); // 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()); } } }