package ca.odell.glazedlists.example;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.gui.TableFormat;
import ca.odell.glazedlists.swing.EventTableModel;
import ca.odell.glazedlists.swing.AutoCompleteSupport;

import javax.swing.*;
import javax.swing.table.TableColumn;
import java.awt.*;

public class AutoCompleteSupportColumnExample {

    public static class Athletes {
        private String name;
        private String sport;

        public Athletes(String name, String sport) {
            this.name = name;
            this.sport = sport;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getSport() {
            return sport;
        }

        public void setSport(String sport) {
            this.sport = sport;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // create an EventList of Athletes
                final EventList<Athletes> athletes = new BasicEventList<Athletes>();
                athletes.add(new Athletes("Michael Jordan", "Basketball"));
                athletes.add(new Athletes("Joe Montana", "Football"));
                athletes.add(new Athletes("Barry Bonds", "Baseball"));
                athletes.add(new Athletes("", ""));
                athletes.add(new Athletes("Mario Lemieux", "Hockey"));
                athletes.add(new Athletes("Ronaldo", "Soccer"));
                athletes.add(new Athletes("Wayne Gretzky", "Hockey"));
                athletes.add(new Athletes("Babe Ruth", "Baseball"));
                athletes.add(new Athletes("Dan Marino", "Football"));
                athletes.add(new Athletes("Kareem Abdul-Jabbar", "Basketball"));
        
                // build a JTable
                String[] propertyNames = new String[] {"name", "sport"};
                String[] columnLabels = new String[] {"Name", "Sport"};
                boolean[] editable = new boolean[] {true, true};
                TableFormat<Athletes> tf = GlazedLists.tableFormat(Athletes.class, propertyNames, columnLabels, editable);
                JTable t = new JTable(new EventTableModel<Athletes>(athletes, tf));

                DefaultCellEditor sportCellEditor = AutoCompleteSupport.createTableCellEditor(tf, athletes, 1);
                TableColumn sportColumn = t.getColumnModel().getColumn(1);
                sportColumn.setCellEditor(sportCellEditor);

                // place the table in a JFrame
                JFrame f = new JFrame();
                f.setLayout(new BorderLayout());
                f.add(new JScrollPane(t), BorderLayout.CENTER);

                // show the frame
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }
        });
    }
}