001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Alexandre Russel
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.annotations.gwt.client.model;
023
024import java.util.ArrayList;
025import java.util.Collections;
026import java.util.Comparator;
027import java.util.List;
028
029import org.nuxeo.ecm.platform.annotations.gwt.client.configuration.AnnotationFilter;
030import org.nuxeo.ecm.platform.annotations.gwt.client.model.AnnotationChangeListener.ChangeEvent;
031
032import com.google.gwt.core.client.GWT;
033
034/**
035 * @author Alexandre Russel
036 */
037public class AnnotationModel implements AnnotationChangeNotifier {
038
039    private static final Comparator<Annotation> ANNOTATION_DATE_COMPARATOR = new Comparator<Annotation>() {
040        public int compare(Annotation o1, Annotation o2) {
041            return o1.getDate().compareTo(o2.getDate());
042        }
043    };
044
045    private Annotation newAnnotation;
046
047    private List<Annotation> annotations = new ArrayList<Annotation>();
048
049    private List<Annotation> filteredAnnotations;
050
051    private AnnotationFilter filter;
052
053    private List<AnnotationChangeListener> listeners = new ArrayList<AnnotationChangeListener>();
054
055    public void addChangeListener(AnnotationChangeListener listener) {
056        listeners.add(listener);
057    }
058
059    public Annotation getNewAnnotation() {
060        return newAnnotation;
061    }
062
063    public void setNewAnnotation(Annotation newAnnotation) {
064        this.newAnnotation = newAnnotation;
065        notifyListener(ChangeEvent.annotation);
066    }
067
068    private void notifyListener(ChangeEvent ce) {
069        GWT.log("Notifying listener.", null);
070        for (AnnotationChangeListener listener : listeners) {
071            listener.onChange(this, ce);
072        }
073    }
074
075    public List<Annotation> getAnnotations() {
076        if (filteredAnnotations != null) {
077            return filteredAnnotations;
078        } else {
079            return annotations;
080        }
081    }
082
083    public List<Annotation> getUnfilteredAnnotations() {
084        return annotations;
085    }
086
087    public void setAnnotations(List<Annotation> annotations) {
088        Collections.sort(annotations, ANNOTATION_DATE_COMPARATOR);
089        this.annotations = annotations;
090
091        int id = 0;
092        for (Annotation annotation : annotations) {
093            annotation.setId(id++);
094        }
095
096        if (filter != null) {
097            filteredAnnotations = filterAnnotations(filter);
098        }
099        notifyListener(ChangeEvent.annotationList);
100    }
101
102    public List<Annotation> filterAnnotations(AnnotationFilter filter) {
103        List<Annotation> filteredAnnotations = new ArrayList<Annotation>();
104        for (Annotation annotation : annotations) {
105            if (filter.accept(annotation)) {
106                filteredAnnotations.add(annotation);
107            }
108        }
109        return filteredAnnotations;
110    }
111
112    public void setFilter(AnnotationFilter filter) {
113        this.filter = filter;
114        filteredAnnotations = filterAnnotations(filter);
115        notifyListener(ChangeEvent.annotationList);
116    }
117
118    public AnnotationFilter getFilter() {
119        return filter;
120    }
121}