001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle-sdk-ext.
005 *
006 * Dicoogle/dicoogle-sdk-ext is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle-sdk-ext is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package pt.ua.dicoogle.sdk.observables;
020
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.Collections;
024import java.util.Observable;
025
026/**
027 * Observable responsible for the notification of user interfaces about any change in the
028 * list of members of the view.
029 *
030 * @author Carlos Ferreira
031 * @author Pedro Bento
032 */
033public class ListObservable<type> extends Observable
034{
035    //array of the members of the view.
036    private Collection<type> array;
037
038    /**
039     * Constructor of the class. It does nothing.
040     */
041    public ListObservable()
042    {
043        this.array = Collections.synchronizedCollection(new ArrayList<type>());
044    }
045
046    /**
047     * Setter of the memberlist, it receives a vector and puts all members
048     * in the array.
049     * After that it notifies all observers.
050     * @param members
051     */
052    public synchronized void setArray(Collection<type> vec)
053    {
054        //initialization of the memberlist
055        this.array.clear();
056        this.array.addAll(vec);
057        
058        //notification of the observers.
059        this.setChanged();
060        this.notifyObservers();
061    }
062
063    public synchronized void addAll(Collection list)
064    {
065        this.array.addAll(list);
066        this.setChanged();
067        this.notifyObservers();
068    }
069
070    public synchronized void add(type object)
071    {
072        this.array.add(object);
073        this.setChanged();
074        this.notifyObservers();
075    }
076
077    /**
078     * Getter of copy of the array list of the members
079     * @return the list of the members of the view
080     */
081    public ArrayList getArray()
082    {
083        ArrayList newArray = new ArrayList();
084        newArray.addAll(this.array);
085        return newArray;
086    }
087    public void resetArray()
088    {
089        this.array.clear();
090        //notification of the observers.
091        this.setChanged();
092        this.notifyObservers();
093
094    }
095
096    @Override
097    public String toString()
098    {
099        String string = "[ ";
100        for(type element: array)
101        {
102            string += element.toString() + "  ";
103        }
104        string += "]";
105        return string;
106    }
107}