package ac.essex.ooechs.music;

import ac.essex.ooechs.imaging.commons.PixelLoader;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Created by IntelliJ IDEA.
 * User: Olly
 * Date: 07-Jul-2007
 * Time: 11:33:27
 * To change this template use File | Settings | File Templates.
 */
public class JavaMusic extends JFrame implements ActionListener {

    protected MusicPanel mp;
    protected JMenuItem load, quit;
    protected JSlider tempo;

    protected JLabel message;

    public static void main(String[] args) throws Exception {
        try {
            UIManager.setLookAndFeel(
                UIManager.getSystemLookAndFeelClassName());
        } catch (UnsupportedLookAndFeelException ex) {
          System.out.println("Unable to load native look and feel");
        }
        new JavaMusic();
    }

    public JavaMusic() throws Exception {
        super("Olly's Visual Music Player");

        mp = new MusicPanel(this);

        Container c = getContentPane();
        c.add(mp, BorderLayout.CENTER);


        load = new JMenuItem("Open Music...");
        load.addActionListener(this);

        quit = new JMenuItem("Quit");
        quit.addActionListener(this);

        JMenuBar bar = new JMenuBar();
        JMenu music = new JMenu("Music");
        music.add(load);
        music.add(quit);
        bar.add(music);
        setJMenuBar(bar);

        JToolBar toolbar = new JToolBar();
        tempo = new JSlider(1, 30, 25);
        tempo.setPaintLabels(false);
        toolbar.add(new JLabel("Tempo:"));
        toolbar.add(tempo);

        message = new JLabel();
        toolbar.add(message);

        c.add(toolbar, BorderLayout.NORTH);

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onQuit();
            }
        });

        setSize(400, 200);
        setVisible(true);
    }

    public void setMessage(final String message) {
        // do nothing, for now.
    }

    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == load) {

        JFileChooser c = new JFileChooser(System.getProperty("user.dir"));
        c.setDialogTitle("Load Sheet Music Image");
        int r = c.showOpenDialog(this);

        if (r == JFileChooser.APPROVE_OPTION) {

            try {
                mp.loadImage(new PixelLoader(c.getSelectedFile()));
            } catch (Exception re) {}

        }

        }

        if (e.getSource() == quit) onQuit();

    }

    public void onQuit() {
        System.exit(0);
    }

}
