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             * ID for serialization.
033             */
034            private static final long serialVersionUID = -3464265240556952223L;
035    
036            /**
037         * Selection observer storing the index of the selected record.
038         *
039         * @serial
040         */
041        private int[] m_anSelection = {
042                 -1};
043    
044        /**
045         * The model of underlying the table that is being displayed.
046         */
047        private transient AbstractTableModel m_atmModel;
048    
049        private LogFileContent m_lfcContent;
050    
051        /**
052         * The displayed {@link JTable}.
053         */
054        private transient JTable m_jtTable;
055    
056        /**
057         * Create a new LogTableForm. LogEntries are ordered by their log dates and displayed using the
058         * {@link DefaultLogEntryTED}.
059         *
060         * @param sCaption the FormSheet's caption.
061         * @param lis the LogInputStream that contains the LogEntries to be displayed. May be filtered.
062         */
063        public LogTableForm(String sCaption, LogInputStream lis) {
064            this(sCaption, lis, null, new DefaultLogEntryTED());
065        }
066    
067        /**
068         * Create a new LogTableForm. LogEntries are ordered by their log dates.
069         *
070         * @param sCaption the FormSheet's caption.
071         * @param lis the LogInputStream that contains the LogEntries to be displayed. May be filtered.
072         * @param ted a TableEntryDescriptor defining how LogEntries are to be displayed with respect to rows and
073         * columns in a table.
074         */
075        public LogTableForm(String sCaption, LogInputStream lis, TableEntryDescriptor ted) {
076            this(sCaption, lis, null, ted);
077        }
078    
079        /**
080         * Create a new LogTableForm. LogEntries are displayed using the {@link DefaultLogEntryTED}.
081         *
082         * @param sCaption the FormSheet's caption.
083         * @param lis the LogInputStream that contains the LogEntries to be displayed. May be filtered.
084         * @param cmp a Comparator that defines the order the LogEntries appear in. May be <code>null</code> in
085         * which case LogEntries will be sorted by the {@link LogEntry#getLogDate log date}.
086         */
087        public LogTableForm(String sCaption, LogInputStream lis, Comparator<LogEntry> cmp) {
088            this(sCaption, lis, cmp, new DefaultLogEntryTED());
089        }
090    
091        /**
092         * Create a new LogTableForm.
093         *
094         * @param sCaption the FormSheet's caption.
095         * @param lis the LogInputStream that contains the LogEntries to be displayed. May be filtered.
096         * @param cmp a Comparator that defines the order the LogEntries appear in. May be <code>null</code> in
097         * which case LogEntries will be sorted by the {@link LogEntry#getLogDate log date}.
098         * @param ted a TableEntryDescriptor defining how LogEntries are to be displayed with respect to rows and
099         * columns in a table.
100         */
101        public LogTableForm(String sCaption, LogInputStream lis, Comparator<LogEntry> cmp, TableEntryDescriptor ted) {
102            this(sCaption, lis, cmp, ted, true);
103        }
104    
105        /**
106         * Create a new LogTableForm.
107         *
108         * @param sCaption the FormSheet's caption.
109         * @param lis the LogInputStream that contains the LogEntries to be displayed. May be filtered.
110         * @param cmp a Comparator that defines the order the LogEntries appear in. May be <code>null</code> in
111         * which case LogEntries will be sorted by the {@link LogEntry#getLogDate log date}.
112         * @param ted a TableEntryDescriptor defining how LogEntries are to be displayed with respect to rows and
113         * columns in a table.
114         * @param fWaitResponse the initial value of the waitResponse property. If true,
115         * {@link sale.Display#setFormSheet} will block until the FormSheet is closed.
116         */
117        public LogTableForm(String sCaption, final LogInputStream lis, final Comparator<LogEntry> cmp,
118                final TableEntryDescriptor ted, boolean fWaitResponse) {
119            this(sCaption, new LogFileContent(lis), cmp, ted, fWaitResponse);
120        }
121    
122    
123        public LogTableForm(String sCaption, final LogFileContent lfc, final Comparator<LogEntry> cmp,
124                final TableEntryDescriptor ted, boolean fWaitResponse) {
125            super(sCaption, (JComponent)null, fWaitResponse);
126            m_lfcContent = lfc;
127            addContentCreator(new FormSheetContentCreator() {
128                            private static final long serialVersionUID = -6240009092780870600L;
129    
130                            protected void createFormSheetContent(FormSheet fs) {
131                    JLogTable jlt = new JLogTable(m_lfcContent, cmp, ted);
132                    m_jtTable = jlt;
133                    fs.setComponent(new JScrollPane(jlt));
134    
135                    jlt.setSelectionObserver(m_anSelection);
136                    m_atmModel = (AbstractTableModel)jlt.getModel();
137                }
138            });
139        }
140    
141    
142        /**
143         * Get the currently selected log entry.
144         */
145        public LogEntry getSelectedEntry() {
146            return (LogEntry)m_atmModel.getRecord(m_anSelection[0]);
147        }
148    
149        /**
150         * Same as {@link #getSelectedEntry}, but return type is Object.
151         * Added for naming consistency within the framework.
152         * @return the currently selected record.
153         */
154        public Object getSelectedRecord() {
155            return m_atmModel.getRecord(m_anSelection[0]);
156        }
157    
158        /**
159         * Gets the table.
160         *
161         * @override Never
162         */
163        public JTable getTable() {
164            return m_jtTable;
165        }
166    
167        /**
168         * Changes the {@link TableModel} of a table.
169         * @param tm the new TableModel
170         */
171        public void setTableModel(AbstractTableModel tm) {
172            m_atmModel.fireTableDataChanged(); //unselect a possibly selected record
173            if (tm instanceof TableSorter) {
174                m_atmModel = tm;
175            } else {
176                m_atmModel = new TableSorter(tm);
177            }
178            m_jtTable.setModel(m_atmModel);
179            ((JAbstractTable)m_jtTable).initialize();
180        }
181    }