/*
 * ListFilter.java
 *
 * Created on May 19, 2005, 12:15 PM
 */

package DDMT.core.misc;

import java.util.LinkedList;
import java.util.List;
import java.util.ArrayList;
import java.util.ListIterator;
import javax.swing.ListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

/**
 *
 * @author  jk3
 */
public class ListFilter extends LinkedList implements ListModel{
    
    //The linked list to filter from
    List data;
    
    //Whether to filter for nodes
    boolean nodeFilter = false;
    
    //The current nodes to filter for
    ArrayList nodes;
    
    //The current starting time filter
    double startTime = 0;
    
    //The current ending time filter
    double endTime = Double.MAX_VALUE;
    
    //The listener for this list
    ListDataListener listener;
    
    /** Creates a new instance of ListFilter */
    public ListFilter(List d) {
        data = d;
        nodes = new ArrayList();
    }
    
    //* Method to filter based on the current data list and filter settings **/
    public void filter()
    {
        //Clear out the current contents
        this.removeRange(0, this.size());
        
        ListIterator iter = data.listIterator();
        while( iter.hasNext() )
        {
            EventType curr = (EventType)iter.next();
            
            boolean passesNodeFilter = false;
            if( nodeFilter )
            {
                for( int i = 0; i< nodes.size(); i++)
                {
                    if( curr.involves( ((Integer)nodes.get(i)).intValue() ))
                        passesNodeFilter = true;
                }
            }else
            {
                passesNodeFilter = true;
            }

            boolean passesTimeFilter = false;
            if( curr.happenedDuring(startTime, endTime ) )
            {
                passesTimeFilter = true;
            }
            
            //If it passed both filters, then add it
            if( passesNodeFilter && passesTimeFilter )
                this.add(curr);
        }
        
        //Update the listener
        ListDataEvent e = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED ,0,this.size()-1 );
        listener.contentsChanged(e);
    }
    
    /** Sets the time-based filter **/
    public void setTimeFilter( double start, double end)
    {
        startTime = start;
        endTime = end;
    }
    
    /** Adds a node-based filter and activates node based filtering**/
    public void addNode(int node)
    {
        nodes.add(new Integer(node));
        this.nodeFilter = true;
    }
    
    /** Clears out all node based filters and turnes off node based filtering**/
    public void clearNodes()
    {
        nodes = new ArrayList();
        this.nodeFilter = false;
    }
    
    public void addListDataListener(javax.swing.event.ListDataListener listDataListener) {
        listener = listDataListener;
    }
    
    public Object getElementAt(int param) {
        return this.get(param);
    }
    
    public int getSize() {
        return this.size();
    }
    
    public void removeListDataListener(javax.swing.event.ListDataListener listDataListener) {
        listener = null;
    }
    
}
