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             * ID for serialization.
025             */
026            private static final long serialVersionUID = 1360051367774625013L;
027            
028            /**
029         * The currently selected user.
030         *
031         * @serial
032         */
033        protected User m_usrSelection;
034    
035        /**
036         * Create a new UserComboBoxModel modelling the global UserManager.
037         */
038        public UserComboBoxModel() {
039            super();
040        }
041    
042        /**
043         * Create a new UserComboBoxModel modelling the global UserManager.
044         *
045         * @param uf a filter that defines the set of users to be displayed. If <code>null</code>, no filtering will
046         * occur.
047         * @param cmp a Comparator that defines the order of the users to be displayed. The objects to be compared
048         * by this comparator will be Users. If <code>null</code>, users will be ordered by their names.
049         */
050        public UserComboBoxModel(UserFilter uf, Comparator<User> cmp) {
051            super(uf, cmp);
052        }
053    
054        /**
055         * Create a new UserComboBoxModel modelling a given UserManager.
056         *
057         * @param um the UserManager to be modelled.
058         */
059        public UserComboBoxModel(UserManager um) {
060            super(um);
061        }
062    
063        /**
064         * Create a new UserComboBoxModel modelling a given UserManager.
065         *
066         * @param um the UserManager to be modelled.
067         * @param uf a filter that defines the set of users to be displayed. If <code>null</code>, no filtering will
068         * occur.
069         * @param cmp a Comparator that defines the order of the users to be displayed. The objects to be compared
070         * by this comparator will be Users. If <code>null</code>, users will be ordered by their names.
071         */
072        public UserComboBoxModel(UserManager um, UserFilter uf, Comparator<User> cmp) {
073            super(um, uf, cmp);
074        }
075    
076        // ComboBoxModel interface methods
077    
078        /**
079         * Return the currently selected user.
080         *
081         * @return the currently selected user.
082         *
083         * @override Never
084         */
085        public Object getSelectedItem() {
086            // make sure our model is up to date
087            ((ListenerHelper)listenerList).needModelUpdate();
088    
089            return m_usrSelection;
090        }
091    
092        /**
093         * Set the currently selected user, making sure that it is known to the
094         * <code>UserManager</code>. If the given <code>User</code> is not known to the <code>UserManager</code>,
095         * the selection is not altered.
096         *
097         * @param oSelectedItem the new selection.
098         *
099         * @override Never
100         */
101        public void setSelectedItem(Object oSelectedItem) {
102            // make sure our model is up to date
103            ((ListenerHelper)listenerList).needModelUpdate();
104    
105            if (m_lUsers.contains(oSelectedItem)) {
106                m_usrSelection = (User)oSelectedItem;
107    
108                fireContentsChanged(this, 0, m_lUsers.size() - 1);
109            }
110        }
111    
112        // HelpableListener interface method
113        /**
114         * Update the local model.
115         *
116         * @override Never
117         */
118        public void updateModel() {
119            super.updateModel();
120    
121            if ((m_usrSelection != null) && (!m_lUsers.contains(m_usrSelection))) {
122                m_usrSelection = null;
123    
124                fireContentsChanged(this, 0, m_lUsers.size() - 1);
125            }
126        }
127    }