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

package example;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.Objects;
import javax.swing.*;

public final class MainPanel extends JPanel {
  private transient JFileChooser fileChooser;

  public MainPanel() {
    super(new GridBagLayout());
    JButton button = new JButton("Open JFileChooser");
    button.addActionListener(e -> {
      fileChooser.showOpenDialog(getRootPane());
    });
    add(button);
    setPreferredSize(new Dimension(320, 240));
  }

  @Override public void updateUI() {
    super.updateUI();
    fileChooser = new JFileChooser();
    fileChooser.setAccessory(new ImagePreview(fileChooser));
  }

  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);
  }
}

// How to Use File Choosers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
// https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html
// https://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#FileChooserDemo2
// https://docs.oracle.com/javase/tutorial/uiswing/examples/components/FileChooserDemo2Project/src/components/ImagePreview.java
class ImagePreview extends JComponent implements PropertyChangeListener {
  private static final int PREVIEW_WIDTH = 90;
  private static final int PREVIEW_MARGIN = 5;
  private ImageIcon thumbnail;
  private File file;

  protected ImagePreview(JFileChooser fc) {
    super();
    fc.addPropertyChangeListener(this);
    // setBorder(BorderFactory.createMatteBorder(1, 0, 1, 1, SystemColor.inactiveCaption));
  }

  @Override public Dimension getPreferredSize() {
    return new Dimension(PREVIEW_WIDTH + PREVIEW_MARGIN * 2, 50);
  }

  private static ImageIcon getImageThumbnail(File file) {
    if (Objects.isNull(file)) {
      return null;
    }
    ImageIcon tmpIcon = new ImageIcon(file.getPath());
    if (tmpIcon.getIconWidth() > PREVIEW_WIDTH) {
      // Image img = tmpIcon.getImage().getScaledInstance(PREVIEW_WIDTH, -1, Image.SCALE_DEFAULT);
      // The Perils of Image.getScaledInstance() | Java.net
      // <del>http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html</del>
      // The Perils of Image.getScaledInstance() Blog | Oracle Community
      // https://community.oracle.com/docs/DOC-983611
      float scale = PREVIEW_WIDTH / (float) tmpIcon.getIconWidth();
      int newW = (int) (tmpIcon.getIconWidth() * scale);
      int newH = (int) (tmpIcon.getIconHeight() * scale);
      BufferedImage img = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2 = img.createGraphics();
      g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      g2.drawImage(tmpIcon.getImage(), 0, 0, newW, newH, null);
      g2.dispose();
      return new ImageIcon(img);
    } else {<commit_msg>refactor: statement lambda can be replaced with expression lambda<commit_after>// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.Objects;
import javax.swing.*;

public final class MainPanel extends JPanel {
  private transient JFileChooser fileChooser;

  public MainPanel() {
    super(new GridBagLayout());
    JButton button = new JButton("Open JFileChooser");
    button.addActionListener(e -> fileChooser.showOpenDialog(getRootPane()));
    add(button);
    setPreferredSize(new Dimension(320, 240));
  }

  @Override public void updateUI() {
    super.updateUI();
    fileChooser = new JFileChooser();
    fileChooser.setAccessory(new ImagePreview(fileChooser));
  }

  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);
  }
}

// How to Use File Choosers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
// https://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html
// https://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#FileChooserDemo2
// https://docs.oracle.com/javase/tutorial/uiswing/examples/components/FileChooserDemo2Project/src/components/ImagePreview.java
class ImagePreview extends JComponent implements PropertyChangeListener {
  private static final int PREVIEW_WIDTH = 90;
  private static final int PREVIEW_MARGIN = 5;
  private ImageIcon thumbnail;
  private File file;

  protected ImagePreview(JFileChooser fc) {
    super();
    fc.addPropertyChangeListener(this);
    // setBorder(BorderFactory.createMatteBorder(1, 0, 1, 1, SystemColor.inactiveCaption));
  }

  @Override public Dimension getPreferredSize() {
    return new Dimension(PREVIEW_WIDTH + PREVIEW_MARGIN * 2, 50);
  }

  private static ImageIcon getImageThumbnail(File file) {
    if (Objects.isNull(file)) {
      return null;
    }
    ImageIcon tmpIcon = new ImageIcon(file.getPath());
    if (tmpIcon.getIconWidth() > PREVIEW_WIDTH) {
      // Image img = tmpIcon.getImage().getScaledInstance(PREVIEW_WIDTH, -1, Image.SCALE_DEFAULT);
      // The Perils of Image.getScaledInstance() | Java.net
      // <del>http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html</del>
      // The Perils of Image.getScaledInstance() Blog | Oracle Community
      // https://community.oracle.com/docs/DOC-983611
      float scale = PREVIEW_WIDTH / (float) tmpIcon.getIconWidth();
      int newW = Math.round(tmpIcon.getIconWidth() * scale);
      int newH = Math.round(tmpIcon.getIconHeight() * scale);
      BufferedImage img = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
      Graphics2D g2 = img.createGraphics();
      g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
      g2.drawImage(tmpIcon.getImage(), 0, 0, newW, newH, null);
      g2.dispose();
      return new ImageIcon(img);
    } else {
{'fraction_non_alphanumeric': 0.1177430319510537, 'fraction_numerical': 0.01142080217539089, 'mean_word_length': 4.191249117854622, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6712501', 'n_tokens_mistral': 2395, 'n_tokens_neox': 2260, 'n_words': 450}