package ca.odell.glazedlists.example;

import ca.odell.glazedlists.swing.AutoCompleteSupport;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.TextFilterator;
import ca.odell.glazedlists.matchers.TextMatcherEditor;

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;

public class AutoCompleteSupportExample {

    public static final class Url {
        private final String location;

        public Url(String location) {
            this.location = location;
        }

        public String getLocation() {
            return location;
        }

        public static Url valueOf(String s) {
            return new Url(s);
        }

        public String toString() {
            return location;
        }
    }

    private static final Url[] URLS = {
        new Url("http://mail.google.com/mail/"),
        new Url("http://slashdot.org/"),
        new Url("http://www.clientjava.com/blog"),
        new Url("del.icio.us"),
        new Url("http://java.sun.com/javase/6/"),
        new Url("http://java.sun.com/"),
        new Url("http://java.sun.com/j2se/1.5.0/download.jsp"),
        new Url("http://java.sun.com/javaone/sf/"),
        new Url("http://www.jetbrains.com/"),
        new Url("http://www.jetbrains.com/idea/?ggl502"),
        new Url("http://www.wilshipley.com/blog/"),
        new Url("http://jroller.com/page/fate"),
        new Url("http://wilwheaton.typepad.com/"),
        new Url("http://www.theonion.com/content/"),
        new Url("http://www.indeed.com/")
    };

    public static void main(String[] args) throws Exception {
        final JComboBox comboBox = new JComboBox();
        comboBox.setPrototypeDisplayValue(new Url("http://java.sun.com/j2se/1.5.0/download.jsp"));

        final EventList<Url> urls = GlazedLists.eventList(Arrays.asList(URLS));

        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                AutoCompleteSupport support = AutoCompleteSupport.install(comboBox, urls, new URLTextFilterator());
                support.setFilterMode(TextMatcherEditor.CONTAINS);
            }
        });

        final JFrame frame = new JFrame("AutoComplete Test");
        frame.getContentPane().setLayout(new GridBagLayout());
        frame.getContentPane().add(comboBox, new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(20, 20, 20, 20), 0, 0));

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    private static final class URLTextFilterator implements TextFilterator<Url> {
        public void getFilterStrings(List<String> baseList, Url element) {
            final String location = element.getLocation();
            baseList.add(location);

            if (location.startsWith("http://"))
                baseList.add(location.substring(7));
            if (location.startsWith("http://www."))
                baseList.add(location.substring(11));
        }
    }
}