package view;

import model.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.event.*;
import controller.Controller;

public class ExplorerView extends BaseView
{
	private Controller controller;

	private JScrollPane scroll;

	private DefaultListModel iconListModel;
	private JList<Thumbnail> iconList;

	private JButton browse;
	private String path = System.getProperty("user.home");

	public void createImages()
	{
		iconListModel.clear();

		File folder = new File(path);
		File[] files = folder.listFiles();
		for (File f : files) {
			String name = f.getName();
			String extension = null;
			int pos = name.lastIndexOf('.');
			if (pos > 0 &&  pos < name.length() - 1) {
				extension = name.substring(pos + 1).toLowerCase();
			}

			if (extension != null) {
				if (extension.equals("jpeg")
						|| extension.equals("jpg")
						|| extension.equals("gif")
						|| extension.equals("png")) {
					try {
						iconListModel.addElement(new Thumbnail(f.getAbsolutePath(), 100, 100));
					}
					catch (Exception e)
					{}
				}
			}
		}
	}

	public ExplorerView(Controller controller)
	{
		super("Explorer", 300, 300);

		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();
				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);
	}

	public void update(Observable o, Object arg)
	{}


	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();
			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;
		}
	}
}

@@ -19,37 +19,378 @@
 ort 
-model.*;%0Aimport javax.swing.*
+javax.swing.JScrollPane;%0Aimport javax.swing.JList;%0Aimport javax.swing.JLabel;%0Aimport javax.swing.JButton;%0Aimport javax.swing.ImageIcon;%0Aimport javax.swing.DefaultListModel;%0Aimport javax.swing.BoxLayout;%0Aimport javax.swing.ListCellRenderer;%0Aimport javax.swing.ListSelectionModel;%0Aimport javax.swing.event.ListSelectionListener;%0Aimport javax.swing.event.ListSelectionEvent
 ;%0Aim
@@ -403,17 +403,21 @@
 ava.awt.
-*
+Image
 ;%0Aimport
@@ -430,15 +430,71 @@
 awt.
+D
 im
-age.*
+ension;%0Aimport java.awt.Component;%0Aimport java.util.Observer
 ;%0Aim
@@ -508,17 +508,26 @@
 va.util.
-*
+Observable
 ;%0Aimport
@@ -535,17 +535,20 @@
 java.io.
-*
+File
 ;%0Aimport
@@ -575,36 +575,8 @@
 IO;%0A
-import javax.swing.event.*;%0A
 impo
@@ -600,16 +600,40 @@
 troller;
+%0Aimport model.Thumbnail;
 %0A%0Apublic
@@ -668,16 +668,36 @@
 BaseView
+ implements Observer
 %0A%7B%0A%09priv
@@ -2504,17 +2504,16 @@
 g)%0A%09%7B%7D%0A%0A
-%0A
 %09private

clean imports part 2