001    package users.swing;
002    
003    import javax.swing.*;
004    
005    import users.*;
006    
007    import util.*;
008    
009    import java.util.*;
010    
011    /**
012     * A ComboBoxModel modelling a UserManager.
013     *
014     * @see UserManager
015     * @see User
016     *
017     * @author Steffen Zschaler
018     * @version 2.0 05/05/1999
019     * @since v2.0
020     */
021    public class UserComboBoxModel extends UserListModel implements ComboBoxModel {
022    
023        /**
024         * The currently selected user.
025         *
026         * @serial
027         */
028        protected User m_usrSelection;
029    
030        /**
031         * Create a new UserComboBoxModel modelling the global UserManager.
032         */
033        public UserComboBoxModel() {
034            super();
035        }
036    
037        /**
038         * Create a new UserComboBoxModel modelling the global UserManager.
039         *
040         * @param uf a filter that defines the set of users to be displayed. If <code>null</code>, no filtering will
041         * occur.
042         * @param cmp a Comparator that defines the order of the users to be displayed. The objects to be compared
043         * by this comparator will be Users. If <code>null</code>, users will be ordered by their names.
044         */
045        public UserComboBoxModel(UserFilter uf, Comparator cmp) {
046            super(uf, cmp);
047        }
048    
049        /**
050         * Create a new UserComboBoxModel modelling a given UserManager.
051         *
052         * @param um the UserManager to be modelled.
053         */
054        public UserComboBoxModel(UserManager um) {
055            super(um);
056        }
057    
058        /**
059         * Create a new UserComboBoxModel modelling a given UserManager.
060         *
061         * @param um the UserManager to be modelled.
062         * @param uf a filter that defines the set of users to be displayed. If <code>null</code>, no filtering will
063         * occur.
064         * @param cmp a Comparator that defines the order of the users to be displayed. The objects to be compared
065         * by this comparator will be Users. If <code>null</code>, users will be ordered by their names.
066         */
067        public UserComboBoxModel(UserManager um, UserFilter uf, Comparator cmp) {
068            super(um, uf, cmp);
069        }
070    
071        // ComboBoxModel interface methods
072    
073        /**
074         * Return the currently selected user.
075         *
076         * @return the currently selected user.
077         *
078         * @override Never
079         */
080        public Object getSelectedItem() {
081            // make sure our model is up to date
082            ((ListenerHelper)listenerList).needModelUpdate();
083    
084            return m_usrSelection;
085        }
086    
087        /**
088         * Set the currently selected user, making sure that it is known to the
089         * <code>UserManager</code>. If the given <code>User</code> is not known to the <code>UserManager</code>,
090         * the selection is not altered.
091         *
092         * @param oSelectedItem the new selection.
093         *
094         * @override Never
095         */
096        public void setSelectedItem(Object oSelectedItem) {
097            // make sure our model is up to date
098            ((ListenerHelper)listenerList).needModelUpdate();
099    
100            if (m_lUsers.contains(oSelectedItem)) {
101                m_usrSelection = (User)oSelectedItem;
102    
103                fireContentsChanged(this, 0, m_lUsers.size() - 1);
104            }
105        }
106    
107        // HelpableListener interface method
108        /**
109         * Update the local model.
110         *
111         * @override Never
112         */
113        public void updateModel() {
114            super.updateModel();
115    
116            if ((m_usrSelection != null) && (!m_lUsers.contains(m_usrSelection))) {
117                m_usrSelection = null;
118    
119                fireContentsChanged(this, 0, m_lUsers.size() - 1);
120            }
121        }
122    }