package ie.dcu.segment.annotate; import ie.dcu.swt.ObservableImage; import java.io.*; import java.util.*; import org.eclipse.swt.graphics.*; /** * Class that manages a set of image annotations. * * @author Kevin McGuinness */ public class AnnotationManager { private static final String FILE_TAG = "Annotations"; private final LinkedList annotations; private final LinkedList redos; private final List listeners; public AnnotationManager() { annotations = new LinkedList(); redos = new LinkedList(); listeners = new ArrayList(2); } public void add(Annotation a) { annotations.addLast(a); redos.clear(); fireAnnotationPerformed(a); } public boolean canUndo() { return !annotations.isEmpty(); } /** * Returns true only if there is atleast one Foreground Annotation and one Background Annotation. * * @return boolean value */ public boolean canLabel() { String[] temp = new String[annotations.size()]; int i = 0; boolean containForeGround = false, containBackGround = false; for(Annotation ann : annotations) { temp[i++] = ann.getType().toString(); } for(int j=0;j 0) { annotations.clear(); redos.clear(); fireAnnotationsCleared(); } } public Annotation getUndoable() { if (!canUndo()) { return null; } return annotations.getLast(); } public Annotation getRedoable() { if (!canRedo()) { return null; } return redos.getFirst(); } public Annotation last() { if (!annotations.isEmpty()) { return annotations.getLast(); } return null; } public List annotations() { return Collections.unmodifiableList(annotations); } public int count() { return annotations.size(); } public int countForeground() { int count = 0; for (Annotation a : annotations) { if (a.getType() == AnnotationType.Foreground) { count++; } } return count; } public int countBackground() { int count = 0; for (Annotation a : annotations) { if (a.getType() == AnnotationType.Background) { count++; } } return count; } public boolean hasForegroundAnnotation() { for (Annotation a : annotations) { if (a.getType() == AnnotationType.Foreground) { return true; } } return false; } public boolean hasBackgroundAnnotation() { for (Annotation a : annotations) { if (a.getType() == AnnotationType.Background) { return true; } } return false; } public void paint(ObservableImage image) { for (Annotation a : annotations) { a.paint(image); } } public void paint(GC gc, boolean antialias) { for (Annotation a : annotations) { a.paint(gc, antialias); } } public void paint(Image image, boolean antialias) { for (Annotation a : annotations) { a.paint(image, antialias); } } public Rectangle getBounds() { Rectangle rect = new Rectangle(0,0,0,0); for (Annotation a : annotations) { rect.add(a.getBounds()); } return rect; } public void addAnnotationListener(AnnotationListener listener) { listeners.add(listener); } public void removeAnnotationListener(AnnotationListener listener) { listeners.remove(listener); } private void fireAnnotationsCleared() { AnnotationEvent e = null; for (AnnotationListener listener : listeners) { if (e == null) { e = new AnnotationEvent(this, null, AnnotationEvent.Type.Cleared); } listener.annotationsCleared(e); } } private void fireAnnotationPerformed(Annotation a) { AnnotationEvent e = null; for (AnnotationListener listener : listeners) { if (e == null) { e = new AnnotationEvent(this, a, AnnotationEvent.Type.Added); } listener.annotationPerformed(e); } } private void fireAnnotationUndone(Annotation a) { AnnotationEvent e = null; for (AnnotationListener listener : listeners) { if (e == null) { e = new AnnotationEvent(this, a, AnnotationEvent.Type.Undone); } listener.annotationUndone(e); } } private void fireAnnotationRedone(Annotation a) { AnnotationEvent e = null; for (AnnotationListener listener : listeners) { if (e == null) { e = new AnnotationEvent(this, a, AnnotationEvent.Type.Redone); } listener.annotationRedone(e); } } public void save(File file) throws IOException { FileOutputStream out = new FileOutputStream(file); try { save(out); } finally { out.close(); } } public void save(OutputStream o) throws IOException { DataOutputStream out = new DataOutputStream( new BufferedOutputStream(o)); // Write tag and count out.writeUTF(FILE_TAG); out.writeInt(annotations.size()); for (Annotation a : annotations) { // Write parameters out.writeInt(a.getType().ordinal()); out.writeInt(a.getLineWidth()); out.writeInt(a.points().size()); // Write points for (Point p : a.points()) { out.writeInt(p.x); out.writeInt(p.y); } } // Flush out.flush(); } public void load(File file) throws IOException { FileInputStream in = new FileInputStream(file); try { load(in); } finally { in.close(); } } public void load(InputStream is) throws IOException { // In case container is being reused, clear old data first annotations.clear(); redos.clear(); // Create and buffer data input stream DataInputStream in = new DataInputStream( new BufferedInputStream(is) ); // Check tag String tag = in.readUTF(); if (!tag.equals(FILE_TAG)) { throw new IOException("Unrecognized file format"); } // Read annotations int n = in.readInt(); for (int i = 0; i < n; i++) { // Read parameters int typeid = in.readInt(); int lineWidth = in.readInt(); int npoints = in.readInt(); // Create annotation AnnotationType type = AnnotationType.valueOf(typeid); Annotation a = new Annotation(type, lineWidth); // Read points for (int j = 0; j < npoints; j++) { int x = in.readInt(); int y = in.readInt(); a.add(new Point(x, y)); } //Add annotation add(a); } } }