001    package users;
002    
003    import java.util.*;
004    
005    import users.events.*;
006    import users.swing.*;
007    
008    /**
009     * Filters a UserManager's users with help of a UserFilter
010     *
011     * @see UserManager
012     * @see UserFilter
013     *
014     * @author Andreas Bartho
015     * @version 3.1 05/03/2003
016     * @since v3.1
017     */
018    public class UserManagerFilter extends UserManager {
019    
020        /**
021             * ID for serialization.
022             */
023            private static final long serialVersionUID = 7358240746287302078L;
024    
025            /**
026         * The UserManager to be filtered.
027         */
028        UserManager m_umManager = UserManager.getGlobalUM();
029    
030        /**
031         * The filter to be applied to the UserManager.
032         */
033        UserFilter m_ufFilter = new UserFilter() {
034                    private static final long serialVersionUID = 7272361080669856514L;
035    
036                    public boolean match(User u) {
037                return true;
038            }
039        };
040    
041        /**
042         * Creates a new UserManagerFilter. The UserManager used is the globally set UserManager.
043         * All Users are visible, which means that the applied UserFilter returns true for every User.
044         * The UserManagerFilter created with this constructor behaves like a normal UserManager.
045         *
046         * @see UserManager#setGlobalUM
047         */
048        public UserManagerFilter() {
049        }
050    
051        /**
052         * Creates a new UserManagerFilter from a given UserManager. All Users are visible,
053         * which means that the applied UserFilter returns true for every User.
054         *
055         * @param um The UserManager to be filtered.
056         */
057        public UserManagerFilter(UserManager um) {
058            m_umManager = um;
059        }
060    
061        /**
062         * Creates a new UserManagerFilter. The UserManager used is the globally set UserManager.
063         *
064         * @param uf The UserFilter to be applied.
065         *
066         * @see UserManager#setGlobalUM
067         *
068         */
069        public UserManagerFilter(UserFilter uf) {
070            m_ufFilter = uf;
071        }
072    
073        /**
074         * Creates a new UserManagerFilter from a given UserManager and a UserFilter.
075         *
076         * @param um The UserManager to be filtered.
077         * @param uf The UserFilter to be applied.
078         */
079        public UserManagerFilter(UserManager um, UserFilter uf) {
080            m_umManager = um;
081            m_ufFilter = uf;
082        }
083    
084        /**
085         * Sets the UserFilter. A previously set UserFilter will be replaced.
086         *
087         * @param uf The new UserFilter.
088         */
089        public void setFilter(UserFilter uf) {
090            m_ufFilter = uf;
091        }
092    
093        /**
094         * Returns all users registered with this UserManagerFilter's UserManager that match the filter.
095         */
096        public Collection<User> getUsers() {
097            SortedMap<String, User> sm = new TreeMap<String, User>();
098            Iterator<User> it = m_umManager.getUsers().iterator();
099            while (it.hasNext()) {
100                User u = it.next();
101                if (m_ufFilter.match(u)) {
102                    sm.put(u.getName(), u);
103                }
104            }
105            return Collections.unmodifiableCollection(sm.values());
106        }
107    
108        public void addUserDataListener(UserDataListener udl) {
109            m_umManager.addUserDataListener(udl);
110        }
111    }