package log.stdforms;

import sale.*;

import log.*;
import log.swing.*;

import util.swing.*;

import javax.swing.*;
import java.util.*;

/**
  * FormSheet displaying the contents of a log file.
  *
  * <p>The FormSheet will contain a {@link JLogTable} containing one line per log entry.</p>
  *
  * @author Steffen Zschaler
  * @version 2.0 27/08/1999
  * @since v2.0
  */
public class LogTableForm extends FormSheet {

  /**
    * Selection observer storing the index of the selected record.
    *
    * @serial
    */
  private int[] m_anSelection = {-1};

  /**
    * The model of underlying the table that is being displayed.
    */
  private transient AbstractTableModel m_atmModel;

  /**
    * Create a new LogTableForm. LogEntries are ordered by their log dates and displayed using the
    * {@link DefaultLogEntryTED}.
    *
    * @param sCaption the FormSheet's caption.
    * @param lis the LogInputStream that contains the LogEntries to be displayed. May be filtered.
    */
  public LogTableForm (String sCaption,
                       LogInputStream lis) {
    this (sCaption, lis, null, new DefaultLogEntryTED());
  }

  /**
    * Create a new LogTableForm. LogEntries are ordered by their log dates.
    *
    * @param sCaption the FormSheet's caption.
    * @param lis the LogInputStream that contains the LogEntries to be displayed. May be filtered.
    * @param ted a TableEntryDescriptor defining how LogEntries are to be displayed with respect to rows and
    * columns in a table.
    */
  public LogTableForm (String sCaption,
                       LogInputStream lis,
                       TableEntryDescriptor ted) {
    this (sCaption, lis, null, ted);
  }

  /**
    * Create a new LogTableForm. LogEntries are displayed using the {@link DefaultLogEntryTED}.
    *
    * @param sCaption the FormSheet's caption.
    * @param lis the LogInputStream that contains the LogEntries to be displayed. May be filtered.
    * @param cmp a Comparator that defines the order the LogEntries appear in. May be <code>null</code> in
    * which case LogEntries will be sorted by the {@link LogEntry#getLogDate log date}.
    */
  public LogTableForm (String sCaption,
                       LogInputStream lis,
                       Comparator cmp) {
    this (sCaption, lis, cmp, new DefaultLogEntryTED());
  }

  /**
    * Create a new LogTableForm.
    *
    * @param sCaption the FormSheet's caption.
    * @param lis the LogInputStream that contains the LogEntries to be displayed. May be filtered.
    * @param cmp a Comparator that defines the order the LogEntries appear in. May be <code>null</code> in
    * which case LogEntries will be sorted by the {@link LogEntry#getLogDate log date}.
    * @param ted a TableEntryDescriptor defining how LogEntries are to be displayed with respect to rows and
    * columns in a table.
    */
  public LogTableForm (String sCaption,
                       LogInputStream lis,
                       Comparator cmp,
                       TableEntryDescriptor ted) {
    this (sCaption, lis, cmp, ted, true);
  }

  /**
    * Create a new LogTableForm.
    *
    * @param sCaption the FormSheet's caption.
    * @param lis the LogInputStream that contains the LogEntries to be displayed. May be filtered.
    * @param cmp a Comparator that defines the order the LogEntries appear in. May be <code>null</code> in
    * which case LogEntries will be sorted by the {@link LogEntry#getLogDate log date}.
    * @param ted a TableEntryDescriptor defining how LogEntries are to be displayed with respect to rows and
    * columns in a table.
    * @param fWaitResponse the initial value of the waitResponse property. If true,
    * {@link sale.Display#setFormSheet} will block until the FormSheet is closed.
    */
  public LogTableForm (String sCaption,
                       final LogInputStream lis,
                       final Comparator cmp,
                       final TableEntryDescriptor ted,
                       boolean fWaitResponse) {
    super (sCaption,
           (JComponent) null,
           fWaitResponse);

    addContentCreator (new FormSheetContentCreator() {
      protected void createFormSheetContent (FormSheet fs) {
        JLogTable jlt = new JLogTable (lis, cmp, ted);
        fs.setComponent (new JScrollPane (jlt));

        jlt.setSelectionObserver (m_anSelection);
        m_atmModel = (AbstractTableModel) jlt.getModel();
      }
    });
  }

  /**
    * Get the currently selected log entry.
    */
  public LogEntry getSelectedEntry() {
    return (LogEntry) m_atmModel.getRecord (m_anSelection[0]);
  }
}