package ca.odell.glazedlists.example;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.gui.TableFormat;
import ca.odell.glazedlists.swing.EventTableModel;
import ca.odell.glazedlists.swing.TableComparatorChooser;

import javax.swing.*;
import java.awt.*;
import java.util.Random;
import java.util.Comparator;

public class SortedListExample {

    public static class MP3 {
        private int track;
        private String song;
        private String album;
        private String artist;

        public MP3(String artist, String album, String name, int track) {
            this.song = name;
            this.album = album;
            this.artist = artist;
            this.track = track;
        }

        public String getSong() {
            return song;
        }

        public String getAlbum() {
            return album;
        }

        public String getArtist() {
            return artist;
        }

        public int getTrack() {
            return track;
        }
    }

    private static final String[] musicalStrings =
            {"Seven", "Mary", "Three", "Alice", "In", "Chains",
             "Green", "Day", "Led", "Zeppelin", "Beatles", "Prince",
             "Holy", "Cake", "White", "Black", "Sgt.", "Pepper"};

    private static final Random dice = new Random();

    private static String makeRandomMusicString(int numParts) {
        StringBuffer musicBuffer = new StringBuffer();

        for (int i = 0; i < numParts; i++) {
            if (musicBuffer.length() > 0)
                musicBuffer.append(' ');
            musicBuffer.append(musicalStrings[dice.nextInt(musicalStrings.length)]);
        }

        return musicBuffer.toString();
    }

    public static void main(String[] args) {
        // create an EventList of MP3s
        final EventList mp3s = new BasicEventList();

        // populate the MP3 list
        for (int i = 1; i <= 100; i++) {
            String artist = makeRandomMusicString(2 + dice.nextInt(3));
            String album = makeRandomMusicString(2 + dice.nextInt(3));
            String song = makeRandomMusicString(2 + dice.nextInt(3));
            mp3s.add(new MP3(artist, album, song, i));
        }

        SortedList sortedMP3s = new SortedList(mp3s, null);

        // build a JTable
        String[] propertyNames = new String[] {"track", "artist", "album", "song"};
        String[] columnLabels = new String[] {"Track", "Artist", "Album", "Song"};
        TableFormat tf = GlazedLists.tableFormat(MP3.class, propertyNames, columnLabels);
        JTable t = new JTable(new EventTableModel(sortedMP3s, tf));

        new TableComparatorChooser(t, sortedMP3s, true);

        // 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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}