package log.swing;

import log.*;

import util.swing.*;

import java.util.*;

import java.text.*;

import javax.swing.*;
import javax.swing.table.*;

/**
  * A JTable that can display log file contents.
  *
  * <p>The contents of the table is read at creation time and does not change, even if the log file contents
  * changes.</p>
  *
  * @author Steffen Zschaler
  * @version 2.0 14/07/1999
  * @since v2.0
  */
public class JLogTable extends JAbstractTable {

  /**
    * A TableCellRenderer that takes a Date and prints it with the standard date and time format for the
    * default locale.
    */
  public static final TableCellRenderer DATE_TIME_CELL_RENDERER = new DefaultTableCellRenderer() {
    private final DateFormat s_dfFormatter = DateFormat.getDateTimeInstance();

    {
      setHorizontalAlignment (RIGHT);
    }

    public java.awt.Component getTableCellRendererComponent (JTable table,
                                                             Object value,
                                                             boolean isSelected,
                                                             boolean hasFocus,
                                                             int row,
                                                             int column) {
      super.getTableCellRendererComponent (table, value, isSelected, hasFocus, row, column);

      setText (s_dfFormatter.format ((Date) value));

      return this;
    }
  };

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

  /**
    * Create a new JLogTable. LogEntries are ordered by their log dates.
    *
    * @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 JLogTable (LogInputStream lis,
                    TableEntryDescriptor ted) {
    this (lis, null, ted);
  }

  /**
    * Create a new JLogTable. LogEntries are displayed using the {@link DefaultLogEntryTED}.
    *
    * @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 JLogTable (LogInputStream lis,
                    Comparator cmp) {
    this (lis, cmp, new DefaultLogEntryTED());
  }

  /**
    * Create a new JLogTable.
    *
    * @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 JLogTable (LogInputStream lis,
                    Comparator cmp,
                    TableEntryDescriptor ted) {
    super (new LogTableModel (lis, cmp, ted));

    setDefaultRenderer (Date.class, DATE_TIME_CELL_RENDERER);
  }
}