001package edu.pdx.cs410J.di;
002
003import javax.swing.*;
004import javax.swing.event.CellEditorListener;
005import javax.swing.event.TableModelEvent;
006import javax.swing.table.TableCellEditor;
007import javax.swing.table.TableCellRenderer;
008import javax.swing.table.TableColumn;
009import java.awt.*;
010import java.awt.event.ActionEvent;
011import java.awt.event.ActionListener;
012import java.util.EventObject;
013
014/**
015 * A table that shows the inventory of books and lets the user select one to be added to a shopping cart.  When a book
016 * is selected, a {@link TableModelEvent} is fired to listeners on this table.
017 */
018class CheckoutInventoryTable extends JTable
019{
020
021    public CheckoutInventoryTable( CheckoutInventoryTableModel model )
022    {
023        super(model);
024        TableColumn column = this.getColumnModel().getColumn( model.getColumnCount() - 1 );
025        column.setCellEditor( new TableButton( model ) );
026        column.setCellRenderer( new TableButton( model ) );
027    }
028
029    /**
030     * Renders a button that
031     */
032    private static class TableButton implements TableCellRenderer, TableCellEditor
033    {
034        private final BookInventoryTableModel model;
035
036        public TableButton( BookInventoryTableModel model )
037        {
038            this.model = model;
039        }
040
041        public Component getTableCellRendererComponent( JTable table, Object value,  boolean isSelected, boolean hasFocus, int row, int column )
042        {
043            return getButton( row );
044        }
045
046        private Component getButton( final int row )
047        {
048            JButton button = new JButton( "Add" );
049            button.addActionListener( new ActionListener() {
050
051                public void actionPerformed( ActionEvent event )
052                {
053                   model.fireTableChanged( new TableModelEvent( model, row, row, model.getColumnCount() -1 ) );
054                }
055            });
056            return button;
057        }
058
059        public Component getTableCellEditorComponent( JTable table, Object value, boolean isSelected, int row, int column )
060        {
061            return getButton(row);
062        }
063
064        public Object getCellEditorValue()
065        {
066            return null;
067        }
068
069        public boolean isCellEditable( EventObject eventObject )
070        {
071            return true;
072        }
073
074        public boolean shouldSelectCell( EventObject eventObject )
075        {
076            return true;
077        }
078
079        public boolean stopCellEditing()
080        {
081            return true;
082        }
083
084        public void cancelCellEditing()
085        {
086
087        }
088
089        public void addCellEditorListener( CellEditorListener cellEditorListener )
090        {
091
092        }
093
094        public void removeCellEditorListener( CellEditorListener cellEditorListener )
095        {
096
097        }
098    }
099}