WindowProject.java

package ac.essex.ooechs.imaging.commons.window.data; 
 
import java.util.Vector; 
import java.io.*; 
import java.awt.*; 
 
/** 
 * Stores the windows associated with an image. 
 * 
 * @author Olly Oechsle, University of Essex, Date: 19-Mar-2008 
 * @version 1.0 
 */ 
public class WindowProject implements Serializable { 
 
    public Vector<WindowImage> images; 
 
    public Vector<WindowClass> classes; 
 
    public File file;         
 
    public WindowProject() { 
        this.file = null; 
        images = new Vector<WindowImage>(); 
        classes = new Vector<WindowClass>(); 
        classes.add(new WindowClass("Background", 0, Color.RED)); 
        classes.add(new WindowClass("Foreground", 1, Color.GREEN)); 
    } 
 
    public WindowClass getClassById(int id) { 
        for (int i = 0; i < classes.size(); i++) { 
            WindowClass windowClass =  classes.elementAt(i); 
            if (windowClass.getClassID() == id) return windowClass; 
        } 
        return null; 
    } 
 
    public static WindowProject load(File f) throws Exception { 
        FileInputStream fis = new FileInputStream(f); 
        ObjectInputStream in = new ObjectInputStream(fis); 
        WindowProject project = (WindowProject) in.readObject(); 
        in.close(); 
        if (project.images == null)  { 
            project.images = new Vector<WindowImage>(); 
            project.file = f; 
        } 
        return project; 
    } 
 
 
    public Vector<WindowImage> getImages() { 
        return images; 
    } 
 
    public Vector<WindowClass> getClasses() { 
        return classes; 
    } 
 
    public WindowImage getFirstImage() { 
        if (images.size() > 0)  { 
            return images.elementAt(0); 
        } else { 
            return null; 
        } 
    } 
 
 
    public File getFile() { 
        return file; 
    } 
 
    public WindowImage getImageByFile(File f) { 
        for (int i = 0; i < images.size(); i++) { 
            WindowImage image = images.elementAt(i); 
            if (image.getFile().equals(f))  { 
                return image; 
            } 
        } 
        return null; 
    } 
 
    public void addImage(WindowImage image) { 
        if (!images.contains(image)) { 
            images.add(image); 
        } 
    } 
 
    public void clearUnusedImages() { 
        Vector<WindowImage> remove = new Vector<WindowImage>(); 
        for (int i = 0; i < images.size(); i++) { 
            WindowImage image = images.elementAt(i); 
            if (image.countWindows() == 0) remove.add(image); 
        } 
        images.removeAll(remove); 
    } 
 
    public void saveAs(File f) throws IOException { 
        clearUnusedImages(); 
        this.file = f; 
        FileOutputStream fos = new FileOutputStream(f); 
        ObjectOutputStream out = new ObjectOutputStream(fos); 
        out.writeObject(this); 
        out.close(); 
    } 
 
}