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