package view;

import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.DefaultListModel;
import javax.swing.BoxLayout;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.SwingWorker;
import java.awt.Image;
import java.awt.Dimension;
import java.awt.Component;
import java.util.Observer;
import java.util.Observable;
import java.util.List;
import java.io.File;
import javax.imageio.ImageIO;
import controller.Controller;
import model.Thumbnail;
import model.Path;
import model.Language;

public class ExplorerView extends BaseView implements Observer
{
	private final Controller controller;
	private Language language;
	private Path path;

	private JScrollPane scroll;

	private DefaultListModel iconListModel;
	private JList<Thumbnail> iconList;

	private JButton browse;

	private SwingWorker<Void, Thumbnail> loadImageWorker = null;

	public void createImages()
	{
		if (loadImageWorker != null)
			loadImageWorker.cancel(true);

		loadImageWorker = new SwingWorker<Void, Thumbnail>() {
			@Override
			protected Void doInBackground() throws Exception
			{
				iconListModel.clear();
				File folder = new File(path.getPath());
				File[] files = folder.listFiles();
				if (files == null)
					return null;

				for (File f : files) {
					if (isCancelled())
						return null;

					if(model.Image.isImage(f.getName())) {
						try {
							Thumbnail t = new Thumbnail(f.getAbsolutePath(), 100, 100);
							publish(t);
						}
						catch (Exception e)
						{}
					}
				}

				return null;
			}

			@Override
			protected void process(List<Thumbnail> chunks)
			{
				if (!isCancelled()) {
					for (Thumbnail t : chunks) {
						iconListModel.addElement(t);
						if(Path.isSelected(t.getName()))
								iconList.setSelectedValue(t, true);
					}
				}
			}
		};

		loadImageWorker.execute();
	}

	public ExplorerView(final Controller controller)
	{
		super();

		this.controller = controller;

		browse = new JButton("Browse");

		iconListModel = new DefaultListModel();
		iconList = new JList<Thumbnail>(iconListModel);
		iconList.setCellRenderer(new iconListCellRenderer());
		iconList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
		iconList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		iconList.setVisibleRowCount(-1);
		iconList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e)
			{
				Thumbnail t = (Thumbnail)iconList.getSelectedValue();
				if (t != null)
					controller.thumbnailSelected(t);
			}
		});

		createImages();

		scroll = new JScrollPane(iconList);
		scroll.setPreferredSize(new Dimension(300, 300));

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		add(browse);
		add(scroll);
		controller.init(this);
	}

	public void setLanguage(Language language) {
		this.language = language;
		super.setTitle(language.getString("explorer"));
	}

	public void setPath(Path path) {
		Path p = path;
		String tmp = path.getPath();
		this.path = p;
		if(tmp.equals(path.getPath()))
			select();
		else
			createImages();
	}

	public void update(Observable o, Object arg)
	{
		String tmp = (String)arg;
		if (tmp.equals("path"))
			setPath((Path)o);
		else if (tmp.equals("language"))
			setLanguage((Language)o);
	}

	private void select() {
		for(int i = 0; i<iconListModel.getSize(); ++i) {
			if(Path.isSelected(((Thumbnail)iconListModel.getElementAt(i)).getName())) {
				iconList.setSelectedValue(iconListModel.getElementAt(i), true);
				return;
			}
		}
		iconList.clearSelection();
	}

	private class iconListCellRenderer extends JLabel implements ListCellRenderer<Thumbnail>
	{
		public iconListCellRenderer()
		{
			setOpaque(true);
		}

		public Component getListCellRendererComponent(JList<? extends Thumbnail> list, Thumbnail value, int index, boolean isSelected, boolean cellHasFocus)
		{
			ImageIcon image = value.getImage();
			if (image != null) {
				setPreferredSize(new Dimension(image.getIconWidth(), image.getIconHeight() + 20));

				setText(value.getName());
				setVerticalTextPosition(JLabel.BOTTOM);
				setHorizontalTextPosition(JLabel.CENTER);

				setIcon(value.getImage());
				setHorizontalAlignment(JLabel.CENTER);

				if (isSelected) {
					setBackground(list.getSelectionBackground());
					setForeground(list.getSelectionForeground());
				}
				else {
					setBackground(list.getBackground());
					setForeground(list.getForeground());
				}
			}

			return this;
		}
	}
}

@@ -863,20 +863,22 @@
 private 
-Path
+String
  path;%0A%0A
@@ -1355,26 +1355,16 @@
 ile(path
-.getPath()
 );%0A%09%09%09%09F
@@ -1899,24 +1899,25 @@
 t);%0A%09%09%09%09%09%09if
+ 
 (Path.isSele
@@ -3102,88 +3102,39 @@
 th p
-ath) %7B%0A%09%09Path p = path;%0A%09%09String tmp = path.getPath();%0A%09%09this.path = p;%0A%09%09if(tmp
+)%0A%09%7B%0A%09%09if (path != null && path
 .equ
@@ -3138,19 +3138,16 @@
 equals(p
-ath
 .getPath
@@ -3146,24 +3146,26 @@
 .getPath()))
+ %7B
 %0A%09%09%09select()
@@ -3168,20 +3168,49 @@
 ct();%0A%09%09
-else
+%7D%0A%09%09else %7B%0A%09%09%09path = p.getPath();
 %0A%09%09%09crea
@@ -3217,24 +3217,28 @@
 teImages();%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09public 

Fix regression. ExplorerView wasn't updating anymore