// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

import java.awt.*;
import java.util.stream.Stream;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

public final class MainPanel extends JPanel {
  public static final int HEADER_HEIGHT = 32;

  private MainPanel() {
    super(new BorderLayout());
    JPanel p = new JPanel(new GridLayout(2, 1));

    JTable table1 = makeTable();
    // Bad: >>>>
    JTableHeader header = table1.getTableHeader();
    // Dimension d = header.getPreferredSize();
    // d.height = HEADER_HEIGHT;
    // header.setPreferredSize(d); // addColumn case test
    header.setPreferredSize(new Dimension(100, HEADER_HEIGHT));
    p.add(makeTitledPanel("Bad: JTableHeader#setPreferredSize(...)", new JScrollPane(table1)));
    // <<<<

    JTable table2 = makeTable();
    JScrollPane scroll = new JScrollPane(table2);
    scroll.setColumnHeader(new JViewport() {
      @Override public Dimension getPreferredSize() {
        Dimension d = super.getPreferredSize();
        d.height = HEADER_HEIGHT;
        return d;
      }
    });
    // // or
    // table2.setTableHeader(new JTableHeader(table2.getColumnModel()) {
    //   @Override public Dimension getPreferredSize() {
    //     Dimension d = super.getPreferredSize();
    //     d.height = HEADER_HEIGHT;
    //     return d;
    //   }
    // });
    p.add(makeTitledPanel("Override getPreferredSize()", scroll));

    JButton button = new JButton("addColumn");
    button.addActionListener(e -> {
      Stream.of(table1, table2).forEach(t -> {
        t.getColumnModel().addColumn(new TableColumn());
        JTableHeader h = t.getTableHeader();
        Dimension d = h.getPreferredSize();
        System.out.println(d);
      });
    });

    add(p);
    add(button, BorderLayout.SOUTH);
    setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    setPreferredSize(new Dimension(320, 240));
  }

  private static JTable makeTable() {
    JTable table = new JTable(new DefaultTableModel(2, 20));
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    return table;
  }

  private static Component makeTitledPanel(String title, Component c) {
    JPanel p = new JPanel(new BorderLayout());
    p.setBorder(BorderFactory.createTitledBorder(title));
    p.add(c);
    return p;
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(MainPanel::createAndShowGui);
  }

  private static void createAndShowGui() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
      ex.printStackTrace();
      Toolkit.getDefaultToolkit().beep();
    }
    JFrame frame = new JFrame("@title@");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new MainPanel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

@@ -1620,24 +1620,16 @@
 ner(e -%3E
- %7B%0A     
  Stream.
@@ -1662,18 +1662,16 @@
 (t -%3E %7B%0A
-  
       t.
@@ -1723,18 +1723,16 @@
 ;%0A      
-  
 JTableHe
@@ -1762,26 +1762,24 @@
 er();%0A      
- 
- 
 Dimension d 
@@ -1808,18 +1808,16 @@
 ;%0A      
-  
 System.o
@@ -1839,19 +1839,10 @@
     
-  %7D);%0A    
 %7D
+)
 );%0A%0A

statement lambda can be replaced with expression lambda