001    package log.stdforms;
002    
003    import sale.FormSheet;
004    import sale.FormSheetContentCreator;
005    import util.swing.AbstractTableModel;
006    import util.swing.TableEntryDescriptor;
007    import log.LogEntry;
008    import log.LogInputStream;
009    import log.swing.DefaultLogEntryTED;
010    import log.swing.JLogTable;
011    import log.LogFileContent;
012    
013    import java.util.Comparator;
014    import javax.swing.JComponent;
015    import javax.swing.JScrollPane;
016    import javax.swing.*;
017    import javax.swing.table.*;
018    import util.swing.*;
019    
020    /**
021     * FormSheet displaying the contents of a log file.
022     *
023     * <p>The FormSheet will contain a {@link JLogTable} containing one line per log entry.</p>
024     *
025     * @author Steffen Zschaler
026     * @version 2.0 27/08/1999
027     * @since v2.0
028     */
029    public class LogTableForm extends FormSheet {
030    
031        /**
032         * Selection observer storing the index of the selected record.
033         *
034         * @serial
035         */
036        private int[] m_anSelection = {
037                 -1};
038    
039        /**
040         * The model of underlying the table that is being displayed.
041         */
042        private transient AbstractTableModel m_atmModel;
043    
044        private LogFileContent m_lfcContent;
045    
046        /**
047         * The displayed {@link JTable}.
048         */
049        private transient JTable m_jtTable;
050    
051        /**
052         * Create a new LogTableForm. LogEntries are ordered by their log dates and displayed using the
053         * {@link DefaultLogEntryTED}.
054         *
055         * @param sCaption the FormSheet's caption.
056         * @param lis the LogInputStream that contains the LogEntries to be displayed. May be filtered.
057         */
058        public LogTableForm(String sCaption, LogInputStream lis) {
059            this(sCaption, lis, null, new DefaultLogEntryTED());
060        }
061    
062        /**
063         * Create a new LogTableForm. LogEntries are ordered by their log dates.
064         *
065         * @param sCaption the FormSheet's caption.
066         * @param lis the LogInputStream that contains the LogEntries to be displayed. May be filtered.
067         * @param ted a TableEntryDescriptor defining how LogEntries are to be displayed with respect to rows and
068         * columns in a table.
069         */
070        public LogTableForm(String sCaption, LogInputStream lis, TableEntryDescriptor ted) {
071            this(sCaption, lis, null, ted);
072        }
073    
074        /**
075         * Create a new LogTableForm. LogEntries are displayed using the {@link DefaultLogEntryTED}.
076         *
077         * @param sCaption the FormSheet's caption.
078         * @param lis the LogInputStream that contains the LogEntries to be displayed. May be filtered.
079         * @param cmp a Comparator that defines the order the LogEntries appear in. May be <code>null</code> in
080         * which case LogEntries will be sorted by the {@link LogEntry#getLogDate log date}.
081         */
082        public LogTableForm(String sCaption, LogInputStream lis, Comparator cmp) {
083            this(sCaption, lis, cmp, new DefaultLogEntryTED());
084        }
085    
086        /**
087         * Create a new LogTableForm.
088         *
089         * @param sCaption the FormSheet's caption.
090         * @param lis the LogInputStream that contains the LogEntries to be displayed. May be filtered.
091         * @param cmp a Comparator that defines the order the LogEntries appear in. May be <code>null</code> in
092         * which case LogEntries will be sorted by the {@link LogEntry#getLogDate log date}.
093         * @param ted a TableEntryDescriptor defining how LogEntries are to be displayed with respect to rows and
094         * columns in a table.
095         */
096        public LogTableForm(String sCaption, LogInputStream lis, Comparator cmp, TableEntryDescriptor ted) {
097            this(sCaption, lis, cmp, ted, true);
098        }
099    
100        /**
101         * Create a new LogTableForm.
102         *
103         * @param sCaption the FormSheet's caption.
104         * @param lis the LogInputStream that contains the LogEntries to be displayed. May be filtered.
105         * @param cmp a Comparator that defines the order the LogEntries appear in. May be <code>null</code> in
106         * which case LogEntries will be sorted by the {@link LogEntry#getLogDate log date}.
107         * @param ted a TableEntryDescriptor defining how LogEntries are to be displayed with respect to rows and
108         * columns in a table.
109         * @param fWaitResponse the initial value of the waitResponse property. If true,
110         * {@link sale.Display#setFormSheet} will block until the FormSheet is closed.
111         */
112        public LogTableForm(String sCaption, final LogInputStream lis, final Comparator cmp,
113                final TableEntryDescriptor ted, boolean fWaitResponse) {
114            this(sCaption, new LogFileContent(lis), cmp, ted, fWaitResponse);
115        }
116    
117    
118        public LogTableForm(String sCaption, final LogFileContent lfc, final Comparator cmp,
119                final TableEntryDescriptor ted, boolean fWaitResponse) {
120            super(sCaption, (JComponent)null, fWaitResponse);
121            m_lfcContent = lfc;
122            addContentCreator(new FormSheetContentCreator() {
123                protected void createFormSheetContent(FormSheet fs) {
124                    JLogTable jlt = new JLogTable(m_lfcContent, cmp, ted);
125                    m_jtTable = jlt;
126                    fs.setComponent(new JScrollPane(jlt));
127    
128                    jlt.setSelectionObserver(m_anSelection);
129                    m_atmModel = (AbstractTableModel)jlt.getModel();
130                }
131            });
132        }
133    
134    
135        /**
136         * Get the currently selected log entry.
137         */
138        public LogEntry getSelectedEntry() {
139            return (LogEntry)m_atmModel.getRecord(m_anSelection[0]);
140        }
141    
142        /**
143         * Same as {@link #getSelectedEntry}, but return type is Object.
144         * Added for naming consistency within the framework.
145         * @return the currently selected record.
146         */
147        public Object getSelectedRecord() {
148            return m_atmModel.getRecord(m_anSelection[0]);
149        }
150    
151        /**
152         * Gets the table.
153         *
154         * @override Never
155         */
156        public JTable getTable() {
157            return m_jtTable;
158        }
159    
160        /**
161         * Changes the {@link TableModel} of a table.
162         * @param the new TableModel
163         */
164        public void setTableModel(AbstractTableModel tm) {
165            m_atmModel.fireTableDataChanged(); //unselect a possibly selected record
166            if (tm instanceof TableSorter) {
167                m_atmModel = tm;
168            } else {
169                m_atmModel = new TableSorter(tm);
170            }
171            m_jtTable.setModel(m_atmModel);
172            ((JAbstractTable)m_jtTable).initialize();
173        }
174    }