001    package users.swing;
002    
003    import util.*;
004    import util.swing.*;
005    
006    import users.*;
007    import users.events.*;
008    import users.swing.*;
009    
010    import data.Catalog;
011    import data.events.*;
012    
013    import java.io.*;
014    import java.util.*;
015    
016    /**
017     * A {@link javax.swing.table.TableModel} that models the contents of a {@link UserManager}.
018     *
019     * @author Thomas Medack
020     * @version 3.0 12/06/2001
021     * @since v3.0
022     */
023    public class UserTableModel extends AbstractTableModel implements UserDataListener, HelpableListener,
024            Serializable {
025    
026        /**
027         * The UserManager that is being modelled.
028         *
029         * @serial
030         */
031        protected UserManager m_umManager;
032    
033        /**
034         * This abstract class is a special Comparator, which compares Users. It will be
035         * used in the UserTableModel to define the order of elements in the assoziated
036         * JUserTable.
037         *
038         * @serial
039         */
040        public static abstract class UserComparator implements Serializable {
041            public abstract int compare(User obj, User obj1);
042        }
043    
044        /**
045         * The Comparator that defines the sorting order of records in the model. It compares
046         * {@link User Users}.
047         *
048         * @serial
049         */
050       // /*protected UserComparator m_cmpComparator = new UserComparator() {
051       //     public int compare (User obj, User obj1) {
052       //          return new NaturalComparator().compare (obj.getName(), obj1.getName());
053       //     }
054       //      };*/
055    
056        protected Comparator m_cmpComparator = new NaturalComparator();
057    
058        /**
059         * The internal model. A list of the Users' names.
060         *
061         * @serial
062         */
063        protected List m_lKeys;
064        
065        /**
066         * Set the table's data. Data is {@link users.UserManager}
067         */
068            public void setData(Object n_umManager) {
069                    m_umManager = (UserManager) n_umManager;
070                    updateModel();
071                    fireTableDataChanged();
072            }
073    
074        /**
075         * Create a new UserTableModel.
076         *
077         * @param u the UserManager to be modelled.
078         * @param cmp a Comparator defining the sort order of the records. If <code>null</code>, records are ordered
079         * according to the natural ordering of the Users.
080         * @param ted a TableEntryDescriptor that can split individual Users into a table's cells.
081         */
082        public UserTableModel(UserManager u, Comparator cmp, TableEntryDescriptor ted) {
083            super(ted);
084            m_umManager = u;
085    
086            if (cmp != null) {
087                m_cmpComparator = cmp;
088            }
089    
090            listenerList = new ListenerHelper(this);
091    
092            updateModel();
093        }
094    
095        /**
096         * Get the record at the given index.
097         *
098         * @param row the index for which to retrieve the record. Element of [0, {@link #getRowCount}).
099         * @return the {@link User} to be displayed at the given index. May return <code>null</code> if
100         * either there is no record at the indicated position or an exception occurs.
101         *
102         * @override Never
103         */
104        public Object getRecord(int row) {
105            ((ListenerHelper)listenerList).needModelUpdate();
106    
107            if ((row > -1) && (row < getRowCount())) {
108                return m_lKeys.get(row);
109            } else {
110                return null;
111            }
112        }
113    
114        /**
115         * Get the number of records in this model.
116         *
117         * @override Never
118         */
119        public int getRowCount() {
120            ((ListenerHelper)listenerList).needModelUpdate();
121    
122            return m_lKeys.size();
123        }
124    
125        /**
126         * Subscribe as a listener to the model.
127         *
128         * @override Never
129         */
130        public void subscribe() {
131            m_umManager.addUserDataListener(this);
132        }
133    
134        /**
135         * Un-Subscribe as a listener from the model.
136         *
137         * @override Never
138         */
139        public void unsubscribe() {
140            m_umManager.removeUserDataListener(this);
141        }
142    
143        /**
144         * Update the internal model based on the modelled {@link UserManager}.
145         *
146         * @override Never
147         */
148        public synchronized void updateModel() {
149            List lKeys = new LinkedList(m_umManager.getUsers());
150            Collections.sort(lKeys, new Comparator() {
151                public int compare(Object o1, Object o2) {
152                    return m_cmpComparator.compare((User)o1, (User)o2);
153                }
154            });
155    
156            m_lKeys = lKeys;
157        }
158    
159        public void userAdded(UserDataEvent e) {
160            updateModel();
161    
162            int nIdx = m_lKeys.indexOf(e.getUser());
163    
164            if (nIdx > -1) {
165                fireTableRowsInserted(nIdx, nIdx);
166            }
167        }
168    
169        public void userDeleted(UserDataEvent e) {
170            int nIdx = m_lKeys.indexOf(e.getUser());
171    
172            updateModel();
173            if (nIdx > -1) {
174                fireTableRowsDeleted(nIdx, nIdx);
175            }
176        }
177    }