/*
 * Created on 05-08-2003
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package net.sf.hibern8ide;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.MutableComboBoxModel;

/**
 * @author MAX
 */
public class History extends AbstractListModel implements MutableComboBoxModel, Serializable {

	List objects = new ArrayList(10);
	Object selectedObject;
    int position = 0;

	/**
	 * Set the value of the selected item. The selected item may be null.
	 * <p>
	 * @param anObject The combo box value or null for no selection.
	 */
	public void setSelectedItem(Object anObject) {
		if ((selectedObject != null && !selectedObject.equals(anObject)) || selectedObject == null && anObject != null) {
			selectedObject = anObject;
            position = objects.indexOf(anObject);
			fireContentsChanged(this, -1, -1);
		}
	}

	// implements javax.swing.ComboBoxModel
	public Object getSelectedItem() {
		return selectedObject;
	}

	// implements javax.swing.ListModel
	public int getSize() {
		return objects.size();
	}

	// implements javax.swing.ListModel
	public Object getElementAt(int index) {
		if (index >= 0 && index < objects.size())
			return objects.get(index);
		else
			return null;
	}

    // implements javax.swing.MutableComboBoxModel
       public void addElement(Object anObject) {
           if(anObject!=null && objects.size()>0 && objects.contains(anObject)) {
               // Do not add if element is allready there!
               // Just set the current position...
               selectedObject = anObject;
               position = objects.indexOf(anObject);
           } else {
            objects.add(anObject);
            fireIntervalAdded(this,objects.size()-1, objects.size()-1);
            if ( objects.size() == 1 && selectedObject == null && anObject != null ) {
                   setSelectedItem( anObject );
            }
           }
       }

       // implements javax.swing.MutableComboBoxModel
       public void insertElementAt(Object anObject,int index) {
           objects.add(index, anObject);
           fireIntervalAdded(this, index, index);
       }

       // implements javax.swing.MutableComboBoxModel
       public void removeElementAt(int index) {
           if ( getElementAt( index ) == selectedObject ) {
               if ( index == 0 ) {
                   setSelectedItem( getSize() == 1 ? null : getElementAt( index + 1 ) );
               }
               else {
                   setSelectedItem( getElementAt( index - 1 ) );
               }
           }

           objects.remove(index);

           fireIntervalRemoved(this, index, index);
       }

       // implements javax.swing.MutableComboBoxModel
       public void removeElement(Object anObject) {
           int index = objects.indexOf(anObject);
           if ( index != -1 ) {
               removeElementAt(index);
           }
       }

       /**
        * Empties the list.
        */
       public void removeAllElements() {
           if ( objects.size() > 0 ) {
               int firstIndex = 0;
               int lastIndex = objects.size() - 1;
               objects.clear();
               selectedObject = null;
               fireIntervalRemoved(this, firstIndex, lastIndex);
           }
       }

	/**
	 * @return
	 */
	protected Object back() {
        int p=-1;
		if(position>0) {
            p = position-1;      
        } else {
            p = objects.size()-1;
        }
		
        setSelectedItem(objects.get(p));
        
        return getSelectedItem();
        
	}

	/**
	 * @return
	 */
	protected Object forward() {
        int p=-1;
        if(position<objects.size()-1) {
            p = position+1;      
        } else {
            p = 0;
       }
        
        setSelectedItem(objects.get(p));
        
        return getSelectedItem();
        
	}
	
}
