001    package users.stdforms;
002    
003    import sale.*;
004    import util.swing.*;
005    import util.swing.AbstractTableModel;
006    import users.User;
007    import users.UserManager;
008    import users.swing.JUserTable;
009    
010    import java.util.Comparator;
011    import javax.swing.*;
012    import javax.swing.table.*;
013    
014    /**
015     * A FormSheet displaying the contents of a {@link UserManager}.
016     *
017     * @author Thomas Medack
018     * @author Andreas Bartho
019     * @version 3.0 13/06/2001
020     * @since v3.0
021     */
022    public class UserTableFormSheet extends FormSheet {
023    
024        /**
025         * Reference to the currently selected index.
026         *
027         * @serial
028         */
029        private int[] m_anSelection = new int[] {
030                 -1};
031    
032        /**
033         * The {@link TableModel} of the table displayed.
034         */
035        private transient util.swing.AbstractTableModel m_atmModel;
036    
037        /**
038         * The {@link TableColumnModel} of the table displayed.
039         */
040        private transient TableColumnModel m_tcmModel;
041    
042        /**
043         * The displayed {@link JTable}.
044         */
045        private transient JTable m_jtTable;
046    
047        /**
048         * The {@link ListSelectionModel} of the table displayed.
049         */
050        private transient ListSelectionModel m_lsmModel;
051    
052        /**
053         * The UserManager from which the table is created.
054         */
055        private UserManager m_umMan;
056    
057        /**
058         * The {@link Gate} at which the FormSheet is being displayed.
059         *
060         * @serial
061         */
062        private UIGate m_uigGate;
063    
064        private transient JComponent m_jcComp;
065    
066        /**
067         * Create a new UserTableFormSheet. The "{@link FormSheet#waitResponse}" property will default
068         * to true.
069         *
070         * @param sCaption the FormSheet's caption.
071         * @param um the UserManager which will be displayed
072         * @param c a JComponent which can be optionally displayed with the JUserTable. The
073         * JComponent of the FormSheet will be a JPanel. The JPanels contains a JSplitPane.
074         * The first JComponent of the JPanel is the JUserTable and the second is this JComponent.
075         * If it is null, only the JUserTable will be displayed.
076         * @param uigGate the Gate at which the FormSheet is displayed.
077         * @param cmp a comparator defining the order in which to display the individual users. If <code>null</code>
078         * the Userrecords will be ordered by their names.
079         * @param ted a TableEntryDescriptor that can split the UserManager's entries into a table's cells.
080         * Must not be <code>null</code>.
081         */
082        public UserTableFormSheet(String sCaption, UserManager um, JComponent c, UIGate uigGate,
083                final Comparator cmp, final TableEntryDescriptor ted) {
084    
085            super(sCaption, (JComponent)null, true);
086            setGate(uigGate);
087    
088            m_umMan = um;
089            m_jcComp = c;
090    
091            addContentCreator(new FormSheetContentCreator() {
092                protected void createFormSheetContent(FormSheet fs) {
093                    JUserTable jut = new JUserTable(m_umMan, cmp, ted);
094    
095                    if (m_jcComp == null) {
096                        fs.setComponent(new JScrollPane(jut));
097                    } else {
098                        JSplitPane p = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JScrollPane(jut), m_jcComp);
099                        p.setDividerSize(4);
100                        p.setDividerLocation(200);
101    
102                        fs.setComponent(p);
103                    }
104    
105                    ((UserTableFormSheet)fs).m_atmModel = (util.swing.AbstractTableModel)jut.getModel(); ((
106                            UserTableFormSheet)fs).m_lsmModel = jut.getSelectionModel(); ((UserTableFormSheet)fs).
107                            m_tcmModel = jut.getColumnModel();
108    
109                    jut.setSelectionObserver(((UserTableFormSheet)fs).m_anSelection);
110                    m_jtTable = jut;
111                }
112            });
113        }
114    
115        /**
116         * Gets the table.
117         *
118         * @override Never
119         */
120        public JTable getTable() {
121            return m_jtTable;
122        }
123    
124        /**
125         * Changes the {@link TableModel} of a table.
126         * @param the new TableModel
127         */
128        public void setTableModel(AbstractTableModel tm) {
129            m_atmModel.fireTableDataChanged(); //unselect a possibly selected record
130            if (tm instanceof TableSorter) {
131                m_atmModel = tm;
132            } else {
133                m_atmModel = new TableSorter(tm);
134            }
135            m_jtTable.setModel(m_atmModel);
136    
137            ((JAbstractTable)m_jtTable).initialize();
138        }
139    
140        /**
141         * Get the table's source.
142         *
143         * @override Never
144         * @return the UserManager from which the table was built. The class of the returned UserManager
145         * is <code>Object</code> to keep consistency with {@link data.stdforms.SingleTableFormSheet}.
146         */
147        public Object getTableSource() {
148            return m_umMan;
149        }
150    
151        /**
152         * Set the gate at which to display the FormSheet. This will also move the FormSheet to that gate, i.e. make
153         * the FormSheet the FormSheet of the given gate.
154         *
155         * @param uigGate the new Gate.
156         *
157         * @override Never
158         *
159         * @see UIGate#setFormSheet
160         */
161        public void setGate(UIGate uigGate) {
162            if (m_uigGate != null) {
163                m_uigGate.setFormSheet(null);
164            }
165    
166            m_uigGate = uigGate;
167    
168            if (m_uigGate != null) {
169                m_uigGate.setFormSheet(this);
170            }
171        }
172    
173        /**
174         * Get the Gate this FormSheet is currently being displayed at.
175         *
176         * @override Never
177         */
178        public UIGate getGate() {
179            return m_uigGate;
180        }
181    
182        /**
183         * Get the record currently selected.
184         *
185         * <p>The actual class of the record depends on the concrete type of
186         * TableModel used. See the TableModel's <code>getRecord()</code> method for
187         * details.
188         */
189        public User getSelectedRecord() {
190            return (User)m_atmModel.getRecord(m_anSelection[0]);
191        }
192    
193        /**
194         * Get the ListSelectionModel of the JUserTable which is displayed.
195         */
196        public ListSelectionModel getSelectionModel() {
197            return m_lsmModel;
198        }
199    
200        /**
201         * Get the TableColumnModel of the JUserTable which is displayed.
202         */
203        public TableColumnModel getColumnModel() {
204            return m_tcmModel;
205        }
206    }