001    package users.swing;
002    
003    import users.*;
004    
005    import javax.swing.*;
006    import java.awt.*;
007    import java.util.*;
008    
009    /**
010     * A list box that will display all users managed by a UserManager.
011     *
012     * @see UserManager
013     * @see User
014     *
015     * @author Steffen Zschaler
016     * @version 2.0 05/05/1999
017     * @since v2.0
018     */
019    public class JUserList extends JList {
020    
021        /**
022         * Create a new JUserList displaying the set of users managed by the global
023         * UserManager. All Users will be displayed and they will be
024         * sorted by their names.
025         */
026        public JUserList() {
027            this(UserManager.getGlobalUM());
028        }
029    
030        /**
031         * Create a new JUserList displaying the set of users managed by a given UserManager. All Users will be
032         * displayed and they will be sorted by their names.
033         *
034         * @param um the UserManager to be displayed.
035         */
036        public JUserList(UserManager um) {
037            this(um, null, null);
038        }
039    
040        /**
041         * Create a new JUserList modelling the global UserManager.
042         *
043         * @param uf a filter that defines the set of users to be displayed. If <code>null</code>, no filtering will
044         * occur.
045         * @param cmp a Comparator that defines the order of the users to be displayed. The objects to be compared
046         * by this comparator will be Users. If <code>null</code>, users will be ordered by their names.
047         */
048        public JUserList(UserFilter uf, Comparator cmp) {
049            this(UserManager.getGlobalUM(), uf, cmp);
050        }
051    
052        /**
053         * Create a new JUserList modelling a given UserManager.
054         *
055         * @param um the UserManager to be modelled.
056         * @param uf a filter that defines the set of users to be displayed. If <code>null</code>, no filtering will
057         * occur.
058         * @param cmp a Comparator that defines the order of the users to be displayed. The objects to be compared
059         * by this comparator will be Users. If <code>null</code>, users will be ordered by their names.
060         */
061        public JUserList(UserManager um, UserFilter uf, Comparator cmp) {
062            super();
063    
064            setModel(new UserListModel(um, uf, cmp));
065    
066            setCellRenderer(new UserListCellRenderer());
067        }
068    
069        /**
070         * The list cell renderer to render {@link User} objects in list cells.
071         *
072         * <p>This list cell renderer can be used with JComboBoxes as well.</p>
073         *
074         * @author Steffen Zschaler
075         * @version 2.0 05/05/1999
076         * @since v2.0
077         */
078        public static class UserListCellRenderer extends JLabel implements ListCellRenderer {
079    
080            /**
081             * Create a new UserListCellRenderer.
082             */
083            public UserListCellRenderer() {
084                setOpaque(true);
085            }
086    
087            /**
088             * Configure this list cell renderer to display the given user's data and return
089             * <code>this</code>.
090             *
091             * @param list the list in which to render the User object
092             * @param value assumed to be the User object to be rendered
093             * @param index the position in the list at which to render the User object.
094             * @param isSelected true if the User object is to be rendered in a selected state
095             * @param cellHasFocus true if the cell to be rendered has the input focus.
096             *
097             * @return this list cell renderer configured to render the given User object.
098             *
099             * @override Sometimes
100             */
101            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
102                    boolean cellHasFocus) {
103                if (value != null) {
104                    setText(((User)value).getName());
105                } else {
106                    setText("");
107                }
108    
109                setFont(list.getFont());
110    
111                setBackground(isSelected ? list.getSelectionBackground() : Color.white);
112                setForeground(isSelected ? list.getSelectionForeground() : Color.black);
113    
114                return this;
115            }
116        }
117    
118        /**
119         * Convenience method returning the currently selected user.
120         *
121         * @return the currently selected user.
122         *
123         * @override Never
124         */
125        public User getSelectedUser() {
126            return (User)getSelectedValue();
127        }
128    }