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

package example;

import com.sun.java.swing.plaf.windows.WindowsComboBoxUI;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Objects;
import java.util.stream.IntStream;
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;
import javax.swing.text.JTextComponent;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new BorderLayout());
    // https://stackoverflow.com/questions/72240670/prevent-a-jcombobox-popup-from-appearing-passing-inputverifier
    JTextField field = new JTextField();
    field.setInputVerifier(new LengthInputVerifier());

    JComboBox<String> combo1 = makeComboBox(10);
    combo1.setEditable(true);
    if (combo1.getUI() instanceof WindowsComboBoxUI) {
      combo1.setUI(new WindowsComboBoxUI() {
        @Override protected ComboPopup createPopup() {
          return new BasicComboPopup2(comboBox);
        }
      });
    } else {
      combo1.setUI(new BasicComboBoxUI() {
        @Override protected ComboPopup createPopup() {
          return new BasicComboPopup2(comboBox);
        }
      });
    }

    JComboBox<String> combo2 = makeComboBox(20);
    combo2.setFocusable(false);

    JComboBox<String> combo3 = makeComboBox(15);
    combo3.setEditable(true);

    Box box = Box.createVerticalBox();
    box.add(makeTitledPanel("InputVerifier:", field));
    box.add(Box.createVerticalStrut(4));
    box.add(makeTitledPanel("Default:", makeComboBox(5)));
    box.add(Box.createVerticalStrut(4));
    box.add(makeTitledPanel("setFocusable(false):", combo2));
    box.add(Box.createVerticalStrut(4));
    box.add(makeTitledPanel("setEditable(true):", combo3));
    box.add(Box.createVerticalStrut(4));
    box.add(makeTitledPanel("Override BasicComboPopup:", combo1));
    box.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    add(box, BorderLayout.NORTH);
    setPreferredSize(new Dimension(320, 240));
  }

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

  private static JComboBox<String> makeComboBox(int size) {
    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
    IntStream.range(0, size).forEach(i -> model.addElement("No." + i));
    return new JComboBox<>(model);
  }

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

class LengthInputVerifier extends InputVerifier {
  private static final int MIN_LEN = 5;

  @Override public boolean verify(JComponent c) {
    boolean verified = false;
    if (c instanceof JTextComponent) {
      String str = ((JTextComponent) c).getText();
      verified = str.length() > MIN_LEN;
    }
    if (!verified) {
      UIManager.getLookAndFeel().provideErrorFeedback(c);
      String msg = String.format("Enter at least %s characters.", MIN_LEN);
      JOptionPane.showMessageDialog(c.getRootPane(), msg, "Error", JOptionPane.ERROR_MESSAGE);
    }
    return verified;
  }
}

class BasicComboPopup2 extends BasicComboPopup {
  private transient MouseListener handler2;

  @Override public void uninstallingUI() {
    super.uninstallingUI();
    handler2 = null;
  }

  // Java 9: protected BasicComboPopup2(JComboBox<Object> combo) {
  protected BasicComboPopup2(JComboBox<?> combo) {
    super(combo);<commit_msg>java9: incompatible types: JComboBox<CAP#1> cannot be converted to JComboBox<Object><commit_after>// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

import com.sun.java.swing.plaf.windows.WindowsComboBoxUI;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Objects;
import java.util.stream.IntStream;
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxUI;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;
import javax.swing.text.JTextComponent;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new BorderLayout());
    // https://stackoverflow.com/questions/72240670/prevent-a-jcombobox-popup-from-appearing-passing-inputverifier
    JTextField field = new JTextField();
    field.setInputVerifier(new LengthInputVerifier());

    JComboBox<String> combo1 = makeComboBox(10);
    combo1.setEditable(true);
    if (combo1.getUI() instanceof WindowsComboBoxUI) {
      combo1.setUI(new WindowsComboBoxUI() {
        @Override protected ComboPopup createPopup() {
          return new BasicComboPopup2(comboBox);
        }
      });
    } else {
      combo1.setUI(new BasicComboBoxUI() {
        @Override protected ComboPopup createPopup() {
          return new BasicComboPopup2(comboBox);
        }
      });
    }

    JComboBox<String> combo2 = makeComboBox(20);
    combo2.setFocusable(false);

    JComboBox<String> combo3 = makeComboBox(15);
    combo3.setEditable(true);

    Box box = Box.createVerticalBox();
    box.add(makeTitledPanel("InputVerifier:", field));
    box.add(Box.createVerticalStrut(4));
    box.add(makeTitledPanel("Default:", makeComboBox(5)));
    box.add(Box.createVerticalStrut(4));
    box.add(makeTitledPanel("setFocusable(false):", combo2));
    box.add(Box.createVerticalStrut(4));
    box.add(makeTitledPanel("setEditable(true):", combo3));
    box.add(Box.createVerticalStrut(4));
    box.add(makeTitledPanel("Override BasicComboPopup:", combo1));
    box.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    add(box, BorderLayout.NORTH);
    setPreferredSize(new Dimension(320, 240));
  }

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

  private static JComboBox<String> makeComboBox(int size) {
    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
    IntStream.range(0, size).forEach(i -> model.addElement("No." + i));
    return new JComboBox<>(model);
  }

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

class LengthInputVerifier extends InputVerifier {
  private static final int MIN_LEN = 5;

  @Override public boolean verify(JComponent c) {
    boolean verified = false;
    if (c instanceof JTextComponent) {
      String str = ((JTextComponent) c).getText();
      verified = str.length() > MIN_LEN;
    }
    if (!verified) {
      UIManager.getLookAndFeel().provideErrorFeedback(c);
      String msg = String.format("Enter at least %s characters.", MIN_LEN);
      JOptionPane.showMessageDialog(c.getRootPane(), msg, "Error", JOptionPane.ERROR_MESSAGE);
    }
    return verified;
  }
}

class BasicComboPopup2 extends BasicComboPopup {
  private transient MouseListener handler2;

  @Override public void uninstallingUI() {
    super.uninstallingUI();
    handler2 = null;
  }

  // Java 8: protected BasicComboPopup2(JComboBox<?> combo) {
  // Java 9: protected BasicComboPopup2(JComboBox<Object> combo) {
  @SuppressWarnings("unchecked")
  protected BasicComboPopup2(JComboBox combo) {
    super(combo);
{'fraction_non_alphanumeric': 0.12423507678097218, 'fraction_numerical': 0.012700611938575222, 'mean_word_length': 4.233836858006042, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 2, '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': '6985096', 'n_tokens_mistral': 2747, 'n_tokens_neox': 2717, 'n_words': 500}