001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Alexandre Russel
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.annotations.gwt.client.model;
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.Comparator;
025import java.util.List;
026
027import org.nuxeo.ecm.platform.annotations.gwt.client.configuration.AnnotationFilter;
028import org.nuxeo.ecm.platform.annotations.gwt.client.model.AnnotationChangeListener.ChangeEvent;
029
030import com.google.gwt.core.client.GWT;
031
032/**
033 * @author Alexandre Russel
034 */
035public class AnnotationModel implements AnnotationChangeNotifier {
036
037    private static final Comparator<Annotation> ANNOTATION_DATE_COMPARATOR = new Comparator<Annotation>() {
038        public int compare(Annotation o1, Annotation o2) {
039            return o1.getDate().compareTo(o2.getDate());
040        }
041    };
042
043    private Annotation newAnnotation;
044
045    private List<Annotation> annotations = new ArrayList<Annotation>();
046
047    private List<Annotation> filteredAnnotations;
048
049    private AnnotationFilter filter;
050
051    private List<AnnotationChangeListener> listeners = new ArrayList<AnnotationChangeListener>();
052
053    public void addChangeListener(AnnotationChangeListener listener) {
054        listeners.add(listener);
055    }
056
057    public Annotation getNewAnnotation() {
058        return newAnnotation;
059    }
060
061    public void setNewAnnotation(Annotation newAnnotation) {
062        this.newAnnotation = newAnnotation;
063        notifyListener(ChangeEvent.annotation);
064    }
065
066    private void notifyListener(ChangeEvent ce) {
067        GWT.log("Notifying listener.", null);
068        for (AnnotationChangeListener listener : listeners) {
069            listener.onChange(this, ce);
070        }
071    }
072
073    public List<Annotation> getAnnotations() {
074        if (filteredAnnotations != null) {
075            return filteredAnnotations;
076        } else {
077            return annotations;
078        }
079    }
080
081    public List<Annotation> getUnfilteredAnnotations() {
082        return annotations;
083    }
084
085    public void setAnnotations(List<Annotation> annotations) {
086        Collections.sort(annotations, ANNOTATION_DATE_COMPARATOR);
087        this.annotations = annotations;
088
089        int id = 0;
090        for (Annotation annotation : annotations) {
091            annotation.setId(id++);
092        }
093
094        if (filter != null) {
095            filteredAnnotations = filterAnnotations(filter);
096        }
097        notifyListener(ChangeEvent.annotationList);
098    }
099
100    public List<Annotation> filterAnnotations(AnnotationFilter filter) {
101        List<Annotation> filteredAnnotations = new ArrayList<Annotation>();
102        for (Annotation annotation : annotations) {
103            if (filter.accept(annotation)) {
104                filteredAnnotations.add(annotation);
105            }
106        }
107        return filteredAnnotations;
108    }
109
110    public void setFilter(AnnotationFilter filter) {
111        this.filter = filter;
112        filteredAnnotations = filterAnnotations(filter);
113        notifyListener(ChangeEvent.annotationList);
114    }
115
116    public AnnotationFilter getFilter() {
117        return filter;
118    }
119}