WindowGUI.java

package ac.essex.ooechs.imaging.commons.window; 
 
import ac.essex.ooechs.imaging.commons.util.panels.FileTree; 
import ac.essex.ooechs.imaging.commons.util.panels.ImageFrame; 
import ac.essex.ooechs.imaging.commons.util.ImageFilenameFilter; 
import ac.essex.ooechs.imaging.commons.util.CSVWriter; 
import ac.essex.ooechs.imaging.commons.PixelLoader; 
import ac.essex.ooechs.imaging.commons.window.data.*; 
import ac.essex.ooechs.imaging.commons.window.data.Window; 
import ac.essex.ooechs.imaging.commons.window.util.WindowFeatures; 
import ac.essex.ooechs.imaging.commons.window.util.FileFilters; 
import ac.essex.ooechs.imaging.commons.window.classifiers.PipelineClassifier; 
import ac.essex.ooechs.imaging.commons.window.classifiers.WindowClassifier; 
 
import javax.swing.*; 
import javax.swing.filechooser.FileFilter; 
import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.io.File; 
 
/** 
 * Simple GUI which assigns windows to images. 
 * 
 * @author Olly Oechsle, University of Essex, Date: 19-Mar-2008 
 * @version 1.0 
 */ 
public class WindowGUI extends JFrame implements ActionListener { 
 
    public static final String APP_NAME = "Window GUI (In Development)"; 
 
    public static final String DEFAULT_PROJECT_FILE_LOCATION = "/home/ooechs/Desktop/Documents/Papers/Pipelines/extracted"; 
 
    FileTree fileTree; 
 
    WindowImagePanel imagePanel; 
 
    JTextField windowWidth, windowHeight; 
 
    JMenuItem fileNew, fileOpen, fileSave, fileSaveAs, fileGenerateFeatures, fileExit; 
 
    JMenuItem editClear; 
 
    JMenuItem toolsTest; 
 
    WindowImage currentImage = null; 
 
    WindowClass currentClass = null; 
 
    WindowProject project = null; 
 
    JLabel status; 
 
    JFileChooser fc; 
 
    public static void main(String[] args) { 
        new WindowGUI(); 
    } 
 
    public WindowGUI() { 
 
        super(APP_NAME); 
 
        project = new WindowProject(); 
 
        fc = new JFileChooser(DEFAULT_PROJECT_FILE_LOCATION); 
 
        init(); 
 
    } 
 
    public void init() { 
 
        Container c = getContentPane(); 
 
        fileTree = new FileTree(new File(DEFAULT_PROJECT_FILE_LOCATION), new ImageFilenameFilter()) { 
 
            /** 
             * Called when a file is selected in the tree 
             */ 
            public void onSelectFile(File f) { 
                if (!f.isDirectory()) { 
                    selectFile(f); 
                } 
            } 
 
        }; 
 
        imagePanel = new WindowImagePanel(this); 
 
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, new JScrollPane(fileTree), new JScrollPane(imagePanel)); 
 
        c.add(splitPane); 
 
        JToolBar toolbar = new JToolBar(); 
        toolbar.add(new JLabel("Width: ")); 
        windowWidth = new JTextField("50"); 
        toolbar.add(windowWidth); 
        toolbar.add(new JLabel("Height: ")); 
        windowHeight = new JTextField("5"); 
        toolbar.add(windowHeight); 
 
        toolbar.addSeparator(); 
 
        JComboBox box = new JComboBox(new EasyComboModel()); 
        toolbar.add(new JLabel("Class: ")); 
        toolbar.add(box); 
 
        c.add(toolbar, BorderLayout.NORTH); 
 
        status = new JLabel(APP_NAME); 
        c.add(status, BorderLayout.SOUTH); 
 
        JMenu file = new JMenu("File"); 
        fileNew = new JMenuItem("New..."); 
        fileNew.addActionListener(this); 
        fileOpen = new JMenuItem("Open..."); 
        fileOpen.addActionListener(this); 
        fileSave = new JMenuItem("Save"); 
        fileSave.addActionListener(this); 
        fileSaveAs = new JMenuItem("Save As..."); 
        fileSaveAs.addActionListener(this); 
        fileGenerateFeatures = new JMenuItem("Generate Features"); 
        fileGenerateFeatures.addActionListener(this); 
        fileExit = new JMenuItem("Exit"); 
        fileExit.addActionListener(this); 
 
        file.add(fileNew); 
        file.add(fileOpen); 
        file.add(fileSave); 
        file.add(fileSaveAs); 
        file.addSeparator(); 
        file.add(fileGenerateFeatures); 
        file.addSeparator(); 
        file.add(fileExit); 
 
        JMenu edit = new JMenu("Edit"); 
        editClear = new JMenuItem("Clear Windows"); 
        editClear.addActionListener(this); 
 
        edit.add(editClear); 
 
        JMenu tools = new JMenu("Tools"); 
        toolsTest = new JMenuItem("Test Classifier"); 
        toolsTest.addActionListener(this); 
 
        tools.add(toolsTest); 
 
        JMenuBar menuBar = new JMenuBar(); 
        menuBar.add(file); 
        menuBar.add(edit); 
        menuBar.add(tools); 
 
        setJMenuBar(menuBar); 
 
        addWindowListener(new WindowAdapter() { 
            public void windowClosing(WindowEvent e) { 
                exit(); 
            } 
        }); 
 
        setSize(800, 600); 
        setVisible(true); 
 
    } 
 
    public ac.essex.ooechs.imaging.commons.window.data.Window getWindow() { 
        return new ac.essex.ooechs.imaging.commons.window.data.Window(Integer.parseInt(windowWidth.getText()), Integer.parseInt(windowHeight.getText()), 0, 0, currentClass); 
    } 
 
    public void actionPerformed(ActionEvent e) { 
 
        if (e.getSource() == fileNew) { 
            project = new WindowProject(); 
            currentImage = null; 
            currentClass = null; 
        } 
 
        if (e.getSource() == fileOpen) { 
            File f = getFileOpen("Open Project", FileFilters.getWindowProjectFilter()); 
            if (f != null) { 
                try { 
                    project = WindowProject.load(f); 
                    currentImage = project.getFirstImage(); 
                    refresh(); 
                    setStatus("Loaded project OK"); 
                } catch (Exception err) { 
                    alert("Cannot open project: " + err.getMessage()); 
                    err.printStackTrace(); 
                } 
            } 
 
        } 
 
        if (e.getSource() == fileSave) { 
            if (project.getFile() == null) { 
                saveAs(); 
            } else { 
                try { 
                    project.saveAs(project.getFile()); 
                    setStatus("Saved project OK"); 
                } catch (Exception err) { 
                    alert("Cannot save project: " + err.getMessage()); 
                    err.printStackTrace(); 
                } 
            } 
        } 
 
        if (e.getSource() == fileGenerateFeatures) { 
            generateFeatures(); 
        } 
 
        if (e.getSource() == fileSaveAs) { 
            saveAs(); 
        } 
 
        if (e.getSource() == fileExit) { 
            exit(); 
        } 
 
        if (e.getSource() == editClear) { 
            if (currentImage != null)  { 
                int clearCount = currentImage.clearWindows(); 
                setStatus("Cleared " + clearCount + " windows"); 
            } 
            refresh(); 
        } 
 
        if (e.getSource() == toolsTest) { 
            test(new PipelineClassifier()); 
        } 
 
    } 
 
    public void test(WindowClassifier classifier) { 
 
        if (currentImage == null)  { 
            alert("No image selected - cannot test."); 
            return; 
        } 
 
        try { 
            PixelLoader image = currentImage.getPixelLoader(); 
            BufferedImage img = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); 
            Graphics g = img.getGraphics(); 
 
            Window w = getWindow(); 
 
            // create the windows we want to test 
            // TODO: Only vertical at the moment 
            for (int y = 0; y < image.getHeight() - w.getHeight(); y++)  { 
                Window w2 = new Window(w.getWidth(), w.getHeight(), 0, y, null); 
                w2.setClass(project.getClassById(classifier.classify(image, w2))); 
                w2.drawRect(g); 
            } 
 
            new ImageFrame(img); 
 
        } catch (Exception e)  { 
            alert("Can't load image"); 
        } 
 
    } 
 
    public void saveAs() { 
        File f = getFileSave("Save Project As", FileFilters.getWindowProjectFilter()); 
        if (f != null) { 
            try { 
                project.saveAs(f); 
                refresh(); 
                setStatus("Saved project OK"); 
            } catch (Exception err) { 
                alert("Cannot save project: " + err.getMessage()); 
                err.printStackTrace(); 
            } 
        } 
    } 
 
    public void generateFeatures() { 
 
        File f = getFileSave("Save Features", FileFilters.getCSVFilter()); 
 
        if (f != null) { 
 
            CSVWriter writer = new CSVWriter(); 
 
            project.clearUnusedImages(); 
 
            int windowCount = 0; 
 
            for (int i = 0; i < project.getImages().size(); i++) { 
                WindowImage image = project.getImages().elementAt(i); 
                try { 
                    PixelLoader img = image.getPixelLoader(); 
                    for (int j = 0; j < image.getWindows().size(); j++) { 
                        Window window = image.getWindows().elementAt(j); 
                        double[] values = WindowFeatures.getFeatures(img, window); 
                        writer.addData(values); 
                        writer.addData(window.getWindowClass().getClassID()); 
                        writer.newLine(); 
                        windowCount++; 
                    } 
                } catch (Exception e) { 
                    alert("Cannot load image: " + image.getFile().getAbsolutePath()); 
                    e.printStackTrace(); 
                } 
            } 
 
            try { 
                writer.save(f); 
                status.setText("Saved " + windowCount + " features"); 
            } catch (Exception e) { 
                alert("Cannot save CSV file"); 
                e.printStackTrace(); 
            } 
 
        } 
 
    } 
 
    public void exit() { 
        System.exit(0); 
    } 
 
    private File getFileSave(String title, FileFilter filter) { 
        fc.setDialogTitle(title); 
        fc.setFileFilter(filter); 
        int returnVal = fc.showSaveDialog(this); 
        if (returnVal == JFileChooser.APPROVE_OPTION) { 
            return fc.getSelectedFile(); 
        } 
        return null; 
    } 
 
    private File getFileOpen(String title, FileFilter filter) { 
        fc.setDialogTitle(title); 
        fc.setFileFilter(filter); 
        int returnVal = fc.showOpenDialog(this); 
        if (returnVal == JFileChooser.APPROVE_OPTION) { 
            return fc.getSelectedFile(); 
        } 
        return null; 
    } 
 
    private void alert(String message) { 
        JOptionPane.showMessageDialog(this, message); 
    } 
 
    public void refresh() { 
        imagePanel.currentWindow = null; 
        display(currentImage); 
        if (project.file != null)  { 
            setTitle(APP_NAME + " - " + project.file.getName()); 
        } else { 
            setTitle(APP_NAME); 
        } 
    } 
 
    public void display(WindowImage image) { 
        if (image == null) { 
            imagePanel.setImageNull(); 
        } else { 
            try { 
                imagePanel.setImage(new PixelLoader(image.getFile())); 
            } catch (Exception e) { 
                alert("Cannot load image: " + image.getFile()); 
                e.printStackTrace(); 
            } 
        } 
    } 
 
    public void selectFile(File f) { 
 
        currentImage = project.getImageByFile(f); 
 
        if (currentImage == null) { 
            currentImage = new WindowImage(f); 
            project.addImage(currentImage); 
        } 
 
        refresh(); 
 
    } 
 
    class ClassButton extends JButton { 
 
        public ClassButton(final WindowClass c) { 
 
            super(c.getName()); 
            setBackground(c.getColor()); 
 
            addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent e) { 
                    currentClass = c; 
                } 
            }); 
 
        } 
 
    } 
 
    public void setStatus(String message) { 
        status.setText(message); 
    } 
 
    class EasyComboModel extends AbstractListModel implements ComboBoxModel { 
 
        private Object selectedObject = null; 
 
        public EasyComboModel() { 
            fireContentsChanged(this, -1, -1); 
        } 
 
        public void setSelectedItem(Object item) { 
            selectedObject = item; 
            currentClass = (WindowClass) item; 
            fireContentsChanged(this, -1, -1); 
        } 
 
        public Object getSelectedItem() { 
            return selectedObject; 
        } 
 
        public Object getElementAt(int i) { 
            if (project.classes != null && i >= 0 && i <= getSize()) { 
                return project.classes.elementAt(i); 
            } 
            return null; 
        } 
 
        public int getSize() { 
            if (project.classes != null) { 
                return project.classes.size(); 
            } 
            return 0; 
        } 
 
    } 
 
}