package net.trevize.galatee.main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.File;
import java.net.URI;
import java.util.Arrays;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

import net.trevize.galatee.GEvent;
import net.trevize.galatee.GListener;
import net.trevize.galatee.Galatee;
import net.trevize.tinker.SystemCommandHandler2;

import org.apache.commons.io.comparator.NameFileComparator;

/**
 * 
 * 
 * @author Nicolas James <nicolas.james@gmail.com> [[http://njames.trevize.net]]
 * DirectoryExplorer.java - Jan 29, 2010
 */

public class DirectoryExplorer {

	private String path;

	public DirectoryExplorer(String path) {
		this.path = path;
		init();
	}

	private void init() {
		// setting the system look and feel
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		System.out.println("*** DirectoryExplorer with Galatee main test ***");
		System.out.println("initializing...");

		int n = 3; //initial number of columns.
		System.out.println("path=[" + path + "]");
		System.out.println("display in " + n + " columns");

		//loading files and metadata.
		File[] files = new File(path).listFiles();
		Arrays.sort(files, NameFileComparator.NAME_INSENSITIVE_COMPARATOR);
		Vector<URI> v_uri = new Vector<URI>();
		Vector<Vector<Object>> v_object = new Vector<Vector<Object>>();
		for (int i = 0; i < files.length; ++i) {
			if (files[i].getName().endsWith(".png")
					|| files[i].getName().endsWith(".jpg")
					|| files[i].getName().endsWith(".gif")
					|| files[i].getName().endsWith(".jpeg")
					|| files[i].getName().endsWith(".svg")
					|| files[i].getName().endsWith(".bmp")
					|| files[i].getName().endsWith(".ppm")) {

				v_uri.add(files[i].toURI());
				Vector<Object> v = new Vector<Object>();
				v.add(files[i].getName());
				v_object.add(v);
			}
		}

		JFrame f = new JFrame("*** Galatee main test ***");

		JPanel p = new JPanel();
		p.setLayout(new BorderLayout());
		p.setBorder(new EmptyBorder(3, 3, 3, 3));
		f.getContentPane().add(p);

		Galatee g = new Galatee(v_uri, v_object, new Dimension(128, 128), 120,
				n);

		p.add(g);

		GListener listener = new GListener() {

			class SystemCommandThread extends Thread {
				private SystemCommandHandler2 sch2 = new SystemCommandHandler2();
				private String command;

				public SystemCommandThread(String command) {
					this.command = command;
				}

				public void run() {
					String[] commands = new String[1];
					commands[0] = command;
					sch2.exec(commands);
				}
			}


			@Override
			public void itemDoubleClicked(GEvent e) {
				System.out.println("item doubleclicked ["
						+ e.getSelectedItem().getLocalFilepath() + "]");
				SystemCommandThread sct = new SystemCommandThread("display \""
						+ e.getSelectedItem().getLocalFilepath() + "\"");
				sct.start();
			}

			@Override
			public void selectionChanged(GEvent e) {
				System.out.println("changing selection for ["
						+ e.getSelectedItem().getLocalFilepath() + "]");
			}

		};
		g.addGalateeListener(listener);

		System.out.println("intialization ended.");

		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(512, 512);
		f.setLocationRelativeTo(null);
		f.setVisible(true);
	}

	public static void main(String args[]) {
		String path = "/home/nicolas/datasets/voc2006/voc2006_trainval/VOCdevkit/VOC2006/PNGImages";
		DirectoryExplorer explorer = new DirectoryExplorer(path);
	}

}

