package users.swing;

import javax.swing.*;

import users.*;

import util.*;

import java.util.*;

/**
  * A ComboBoxModel modelling a UserManager.
  *
  * @see UserManager
  * @see User
  *
  * @author Steffen Zschaler
  * @version 2.0 05/05/1999
  * @since v2.0
  */
public class UserComboBoxModel extends UserListModel implements ComboBoxModel {

  /**
    * The currently selected user.
    *
    * @serial
    */
  protected User m_usrSelection;

  /**
    * Create a new UserComboBoxModel modelling the global UserManager.
    */
  public UserComboBoxModel() {
    super();
  }

  /**
    * Create a new UserComboBoxModel modelling the global UserManager.
    *
    * @param uf a filter that defines the set of users to be displayed. If <code>null</code>, no filtering will
    * occur.
    * @param cmp a Comparator that defines the order of the users to be displayed. The objects to be compared
    * by this comparator will be Users. If <code>null</code>, users will be ordered by their names.
    */
  public UserComboBoxModel (UserFilter uf,
                            Comparator cmp) {
    super (uf, cmp);
  }

  /**
    * Create a new UserComboBoxModel modelling a given UserManager.
    *
    * @param um the UserManager to be modelled.
    */
  public UserComboBoxModel(UserManager um) {
    super (um);
  }

  /**
    * Create a new UserComboBoxModel modelling a given UserManager.
    *
    * @param um the UserManager to be modelled.
    * @param uf a filter that defines the set of users to be displayed. If <code>null</code>, no filtering will
    * occur.
    * @param cmp a Comparator that defines the order of the users to be displayed. The objects to be compared
    * by this comparator will be Users. If <code>null</code>, users will be ordered by their names.
    */
  public UserComboBoxModel (UserManager um,
                            UserFilter uf,
                            Comparator cmp) {
    super (um, uf, cmp);
  }

  // ComboBoxModel interface methods

  /**
    * Return the currently selected user.
    *
    * @return the currently selected user.
    *
    * @override Never
    */
  public Object getSelectedItem() {
    // make sure our model is up to date
    ((ListenerHelper) listenerList).needModelUpdate();

    return m_usrSelection;
  }

  /**
    * Set the currently selected user, making sure that it is known to the
    * <code>UserManager</code>. If the given <code>User</code> is not known to the <code>UserManager</code>,
    * the selection is not altered.
    *
    * @param oSelectedItem the new selection.
    *
    * @override Never
    */
  public void setSelectedItem (Object oSelectedItem) {
    // make sure our model is up to date
    ((ListenerHelper) listenerList).needModelUpdate();

    if (m_lUsers.contains (oSelectedItem)) {
      m_usrSelection = (User) oSelectedItem;

      fireContentsChanged (this, 0, m_lUsers.size() - 1);
    }
  }

  // HelpableListener interface method
  /**
    * Update the local model.
    *
    * @override Never
    */
  public void updateModel() {
    super.updateModel();

    if ((m_usrSelection != null) &&
        (!m_lUsers.contains (m_usrSelection))) {
      m_usrSelection = null;

      fireContentsChanged (this, 0, m_lUsers.size() - 1);
    }
  }
}