package cnp.ew.list;

import java.util.*;
import cnp.ew.util.*;

public class CpDefaultSelectionModel extends CpDefaultObservable implements CpSelectionModel, CpObservable
{
    // Should we handle selection policy here?  like select(i, keyModifiers) and let it
    // figure out that shift should do x and control should toggle, etc.
    // Would be nice...

    Vector selectedIndexes = new Vector();
    Integer lastSelected;

    public boolean isSelected(int index)
    {
        for (int i = 0; i < selectedIndexes.size(); i++) {
            Integer tempIndex = (Integer)selectedIndexes.elementAt(i);
            if (tempIndex.intValue() == index) {
                return true;
            }
        }
        return false;
    }

    public int lastSelected()
    {
        if (lastSelected == null) {
            // TBD: Hack by Ken.  This should be explicitly called.
            initializeSelection();
        }
        return lastSelected.intValue();
    }

    public void initializeSelection()
    {
        // SENT when a list gets a new bunch of items
        selectedIndexes.removeAllElements();
        selectedIndexes.addElement(new Integer(0));
        lastSelected = new Integer(0);
    }

    public void select(int indexInt, int modifiers)
    {
        // For now, just do single select...

        Vector itemsSelected = new Vector();
        Vector itemsDeselected = new Vector();
        Vector both = new Vector();
        Integer index = new Integer(indexInt);

        // Is this right?  for the first time?
  //      if (selectedIndexes.isEmpty()) {
  //          selectedIndexes.addElement(index);
  //          return;
  //      }

        // if in toggle mode, if index is selected deselect,
        if (modifiers == TOGGLE_SELECT) {
            if (isSelected(index.intValue())) {
                selectedIndexes.removeElement(index);
                itemsDeselected.addElement(index);
                lastSelected = null;
            } else {
                selectedIndexes.addElement(index);
                itemsSelected.addElement(index);
                lastSelected = index;
            }
        } else if (modifiers == NORMAL_SELECT) {
            if (!isSelected(index.intValue()) || (selectedIndexes.size() > 1)) {
                for (int d = 0; d < selectedIndexes.size(); d++) {
                    itemsDeselected.addElement(selectedIndexes.elementAt(d));
                }
                selectedIndexes.removeAllElements();
                selectedIndexes.addElement(index);
                itemsSelected.addElement(index);
                lastSelected = index;
            }
        } else {
            if (lastSelected == null) {
                lastSelected = index;
            }
            for (int d = 0; d < selectedIndexes.size(); d++) {
                itemsDeselected.addElement(selectedIndexes.elementAt(d));
            }
            selectedIndexes.removeAllElements();
            for (int i = Math.min(lastSelected.intValue(), index.intValue()); i <= Math.max(lastSelected.intValue(), index.intValue()); i++) {
                selectedIndexes.addElement(new Integer(i));
                itemsSelected.addElement(new Integer(i));
            }
        }
  //      System.out.println("Items sel size = " + itemsSelected.size() + " desel size = " + itemsDeselected.size());
        both.addElement(itemsSelected);
        both.addElement(itemsDeselected);
        notifyObservers(CpEvent.LIST_SELECTION_CHANGED, both);
    }
}

