
package uk.co.wingpath.gui;

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.text.*;

public class WTable
    extends JTable
{
    public WTable ()
    {
        this (null, null, null);
    }

    public WTable (TableModel dm)
    {
        this (dm, null, null);
    }

    public WTable (TableModel dm, TableColumnModel cm)
    {
        this (dm, cm, null);
    }

    public WTable (TableModel dm, TableColumnModel cm, ListSelectionModel sm)
    {
        super (dm, cm, sm);
        putClientProperty ("terminateEditOnFocusLost", Boolean.TRUE);
        setSurrendersFocusOnKeystroke (true);
        setSelectionMode (ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        setRowSelectionAllowed (true);
        setColumnSelectionAllowed (false);
    }

    // The following method was inspired by
    // http://forums.sun.com/thread.jspa?messageID=4487840#4487840
    @Override
    public void changeSelection (int row, int column, boolean toggle,
        boolean extend)
    {
        super.changeSelection (row, column, toggle, extend);

        if (toggle || extend)
            return;

        // Don't touch anything other than text components - if we call
        // editCellAt for a checkbox we lose the border indicating selection,
        // and if we call editCellAt for a combo it messes up logic of
        // displaying the drop-down list (in a manner that it is L&F dependent).
        TableCellEditor editor = getCellEditor (row, column);
        if (!(editor instanceof DefaultCellEditor) ||
            !(((DefaultCellEditor) editor).getComponent () instanceof
                JTextComponent))
        {
            return;
        }

        if (editCellAt (row, column))
        {
            Component component = getEditorComponent ();
            boolean focussed = component.requestFocusInWindow ();
        }
    }
}


