/**
* Application to search for local image files by histogram comparison.
*/
package com.smoker.imagesearch.controller;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import com.smoker.imagesearch.model.DisplayData;
import com.smoker.imagesearch.model.IndexCreator;
import com.smoker.imagesearch.model.IndexLoader;
import com.smoker.imagesearch.model.ListData;
import com.smoker.imagesearch.model.Searcher;
import com.smoker.imagesearch.model.Settings;
import com.smoker.imagesearch.model.TaskHandler;
import com.smoker.imagesearch.resources.ResourceStrings;
import com.smoker.imagesearch.view.ImageDisplay;
import com.smoker.imagesearch.view.ListDisplay;
import com.smoker.imagesearch.view.ProgressDialog;
import com.smoker.imagesearch.view.SettingsDialog;
/**
* @author Aidan Smoker
* @version 1.0
*/
public class JImageSearch implements Observer, UncaughtExceptionHandler
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
JImageSearch controller = new JImageSearch();
Thread.currentThread().setUncaughtExceptionHandler(controller);
setLookAndFeel();
controller.createAndShowMainGUI();
}
});
}
private static void setLookAndFeel()
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (ClassNotFoundException ignore)
{
}
catch (InstantiationException ignore)
{
}
catch (IllegalAccessException ignore)
{
}
catch (UnsupportedLookAndFeelException ignore)
{
}
}
private ImageDisplay imageDisplay;
private JFileChooser chooser;
private ProgressDialog progressDialog;
private ListDisplay listDisplay;
private TaskHandler taskHandler;
private IndexCreator indexCreator;
private IndexLoader indexLoader;
private Searcher searcher;
private DisplayData displayData;
private ListData listData;
private Settings settings;
public JImageSearch()
{
searcher = new Searcher();
indexCreator = new IndexCreator(searcher);
indexLoader = new IndexLoader(searcher);
displayData = new DisplayData(searcher);
}
public void about()
{
displayMessage(JOptionPane.INFORMATION_MESSAGE, ResourceStrings.AboutText);
}
private void createAndShowMainGUI()
{
imageDisplay = new ImageDisplay(this);
settings = new Settings();
if (indexCreator.isIndexFiled())
{
load();
}
else
{
index();
}
}
private void createAndShowSearchGUI()
{
listData = new ListData(searcher.getSearchList());
ArrayList<String> searchList = listData.getPaths();
if (searchList.size() > 0)
{
listDisplay = new ListDisplay(this, imageDisplay, listData.getPaths(), listData.getImages());
}
else
{
displayMessage(JOptionPane.WARNING_MESSAGE, ResourceStrings.SearchImagesMissing);
imageDisplay.setMenuItemsEnabled(true, 0, 2, 3, 4);
}
}
private void displayMessage(int type, String message)
{
if (type != JOptionPane.INFORMATION_MESSAGE) Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(imageDisplay, message, ResourceStrings.InformationMessageTitles[type], type);
}
public void exit()
{
if (taskHandler != null && taskHandler.isAlive())
{
displayMessage(JOptionPane.WARNING_MESSAGE, ResourceStrings.WaitForTask);
return;
}
else
{
Toolkit.getDefaultToolkit().beep();
int exit = JOptionPane.showConfirmDialog(imageDisplay, ResourceStrings.ExitMessage,
ResourceStrings.ExitDialogTitle, JOptionPane.OK_CANCEL_OPTION);
if (exit == JOptionPane.CANCEL_OPTION) return;
}
if (listDisplay != null) listDisplay.dispose();
System.exit(0);
}
public void index()
{
imageDisplay.setMenuItemsEnabled(false, 2, 3, 4);
progressDialog = new ProgressDialog(imageDisplay, ResourceStrings.ProgressMessageIndex);
indexCreator.deleteObservers();
indexCreator.addObserver(imageDisplay);
indexCreator.addObserver(progressDialog);
indexCreator.addObserver(this);
taskHandler = new TaskHandler(indexCreator, this, "creator");
taskHandler.start();
}
public void instructions()
{
displayMessage(JOptionPane.INFORMATION_MESSAGE, ResourceStrings.InstructionsText);
}
public void load()
{
imageDisplay.setMenuItemsEnabled(false, 2, 3, 4);
progressDialog = new ProgressDialog(imageDisplay, ResourceStrings.ProgressMessageLoad);
indexLoader.deleteObservers();
indexLoader.addObserver(imageDisplay);
indexLoader.addObserver(progressDialog);
indexLoader.addObserver(this);
taskHandler = new TaskHandler(indexLoader, this, "loader");
taskHandler.start();
}
public void open()
{
chooser = new JFileChooser();
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
String fileChosen = chooser.getSelectedFile().getAbsolutePath();
setDisplayerImage(fileChosen);
}
}
public void search()
{
if (!displayData.isLoaded())
{
displayMessage(JOptionPane.WARNING_MESSAGE, ResourceStrings.PleaseChooseImage);
return;
}
imageDisplay.setMenuItemsEnabled(false, 0, 2, 3, 4);
progressDialog = new ProgressDialog(imageDisplay, ResourceStrings.ProgressMessageSearch);
searcher.deleteObservers();
searcher.addObserver(imageDisplay);
searcher.addObserver(progressDialog);
searcher.addObserver(this);
taskHandler = new TaskHandler(searcher, this, "searcher");
taskHandler.start();
}
public void setDisplayerImage(String filePath)
{
try
{
displayData.loadImage(filePath);
imageDisplay.displayImage(displayData.getImage());
imageDisplay.setImagePath(filePath);
imageDisplay.setRawHisto(displayData.getRawHisto());
}
catch (IOException e)
{
displayMessage(JOptionPane.ERROR_MESSAGE, ResourceStrings.ErrorOpeningImage);
}
}
public void setSettings(String[] settingsData, boolean reindex)
{
settings.setSettings(settingsData);
if (reindex) index();
}
public void settings()
{
new SettingsDialog(this, imageDisplay, settings.getSettingsRanges(), settings.getSettings());
}
@Override
public void uncaughtException(Thread t, Throwable e)
{
displayMessage(JOptionPane.ERROR_MESSAGE, ResourceStrings.UnhandledExceptionMessage);
File indexFile = new File(ResourceStrings.IndexFileName);
if (indexFile.exists()) indexFile.deleteOnExit();
System.exit(0);
}
@Override
public void update(Observable o, Object arg)
{
final String broadcast = (String) arg;
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
if (broadcast.startsWith("searcher.finished"))
{
createAndShowSearchGUI();
}
else if (broadcast.startsWith("searcher.empty"))
{
displayMessage(JOptionPane.WARNING_MESSAGE, ResourceStrings.FileSystemEmpty);
imageDisplay.setMenuItemsEnabled(true, 0, 2, 3, 4);
}
else if (broadcast.startsWith("creator.error"))
{
displayMessage(JOptionPane.ERROR_MESSAGE, ResourceStrings.ErrorCreatingIndex);
}
else if (broadcast.startsWith("loader.error"))
{
displayMessage(JOptionPane.ERROR_MESSAGE, ResourceStrings.ErrorLoadingIndex);
}
}
});
}
}