package ca.odell.glazedlists.example;

import ca.odell.glazedlists.*;
import ca.odell.glazedlists.gui.TableFormat;
import ca.odell.glazedlists.matchers.AbstractMatcherEditor;
import ca.odell.glazedlists.matchers.Matcher;
import ca.odell.glazedlists.swing.EventTableModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CustomMatcherEditorExample {

    public static class AmericanIdol {
        private String name;
        private int votes;
        private String nationality;

        public AmericanIdol(String name, int votes, String nationality) {
            this.name = name;
            this.votes = votes;
            this.nationality = nationality;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getNationality() {
            return nationality;
        }

        public void setNationality(String nationality) {
            this.nationality = nationality;
        }

        public int getVotes() {
            return votes;
        }

        public void setVotes(int votes) {
            this.votes = votes;
        }

        public void incrementVotes() {
            this.votes++;
        }
    }

    public static void main(String[] args) {
        // create an EventList of AmericanIdol
        final EventList idols = new BasicEventList();
        idols.add(new AmericanIdol("Simon Cowell", 0, "British"));
        idols.add(new AmericanIdol("Paula Abdul", 0, "American"));
        idols.add(new AmericanIdol("Randy Jackson", 0, "American"));
        idols.add(new AmericanIdol("Ryan Seacrest", 0, "American"));

        final NationalityMatcherEditor nationalityMatcherEditor = new NationalityMatcherEditor();
        final FilterList filteredIdols = new FilterList(idols, nationalityMatcherEditor);

        // build a JTable
        String[] propertyNames = new String[] {"name", "votes"};
        String[] columnLabels = new String[] {"Name", "Votes"};
        TableFormat tf = GlazedLists.tableFormat(AmericanIdol.class, propertyNames, columnLabels);
        JTable t = new JTable(new EventTableModel(filteredIdols, tf));

        // place the table in a JFrame
        JFrame f = new JFrame();
        f.setLayout(new BorderLayout());
        f.add(nationalityMatcherEditor.getComponent(), BorderLayout.NORTH);
        f.add(new JScrollPane(t), BorderLayout.CENTER);

        // show the frame
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static class NationalityMatcherEditor extends AbstractMatcherEditor implements ActionListener {
        private JComboBox nationalityChooser;

        public NationalityMatcherEditor() {
            this.nationalityChooser = new JComboBox(new Object[] {"British", "American"});
            this.nationalityChooser.getModel().setSelectedItem("Filter by Nationality...");
            this.nationalityChooser.addActionListener(this);
        }

        public Component getComponent() {
            return this.nationalityChooser;
        }

        public void actionPerformed(ActionEvent e) {
            final String nationality = (String) this.nationalityChooser.getSelectedItem();
            if (nationality == null)
                this.fireMatchAll();
            else
                this.fireChanged(new NationalityMatcher(nationality));
        }

        private static class NationalityMatcher implements Matcher {
            private final String nationality;

            public NationalityMatcher(String nationality) {
                this.nationality = nationality;
            }

            public boolean matches(Object item) {
                final AmericanIdol idol = (AmericanIdol) item;
                return this.nationality.equals(idol.getNationality());
            }
        }
    }
}