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

package example;

import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.Arrays;
import java.util.List;
import javax.swing.*;
import javax.swing.text.JTextComponent;

public class MainPanel extends JPanel {
  protected static final int MAX_LEN = 6;
  protected final JCheckBox check = new JCheckBox("use FocusTraversalPolicy", true);
  protected final JButton button = new JButton("Next");
  protected final List<JTextField> list = Arrays.asList(makeTextField(), makeTextField());

  public MainPanel() {
    super(new BorderLayout());
    setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
      @Override public Component getComponentAfter(Container focusCycleRoot, Component cmp) {
        System.out.println("getComponentAfter");
        button.setEnabled(isAllValid());
        return super.getComponentAfter(focusCycleRoot, cmp);
      }

      @Override public Component getComponentBefore(Container focusCycleRoot, Component cmp) {
        System.out.println("getComponentAfter");
        button.setEnabled(isAllValid());
        return super.getComponentBefore(focusCycleRoot, cmp);
      }
    });
    setFocusCycleRoot(true);

    button.setEnabled(false);
    check.addActionListener(e -> setFocusCycleRoot(((JCheckBox) e.getSource()).isSelected()));

    Box box = Box.createVerticalBox();
    box.add(check);
    box.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    list.forEach(c -> {
      box.add(Box.createVerticalStrut(10));
      box.add(c);
    });
    JPanel p = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    p.add(button);
    add(box, BorderLayout.NORTH);
    add(p, BorderLayout.SOUTH);
    setPreferredSize(new Dimension(320, 240));
  }

  protected final boolean isAllValid() {
    return list.stream().allMatch(t -> t.getInputVerifier().verify(t));
  }

  protected final void updateButton() {
    if (isAllValid()) {
      button.setEnabled(true);
      // EventQueue.invokeLater(button::requestFocusInWindow);
    } else {
      button.setEnabled(false);
    }
  }

  protected final JTextField makeTextField() {
    JTextField textField = new JTextField(24);
    textField.setInputVerifier(new InputVerifier() {
      @Override public boolean verify(JComponent c) {
        if (c instanceof JTextComponent) {
          JTextComponent tc = (JTextComponent) c;
          String str = tc.getText().trim();
          return !str.isEmpty() && MAX_LEN - str.length() >= 0;
        }
        return false;
      }

      @Override public boolean shouldYieldFocus(JComponent input) {
        System.out.println("shouldYieldFocus");
        updateButton();
        return super.shouldYieldFocus(input);
      }
    });
    textField.addFocusListener(new FocusAdapter() {
      @Override public void focusLost(FocusEvent e) {
        if (e.isTemporary()) {
          return;
        }
        System.out.println("focusLost");
        updateButton();
      }
    });
    // // TEST:
    // textField.getDocument().addDocumentListener(new DocumentListener() {
    //   @Override public void insertUpdate(DocumentEvent e) {
    //     System.out.println("insertUpdate");
    //   }
    //   @Override public void removeUpdate(DocumentEvent e) {
    //     System.out.println("removeUpdate");
    //   }
    //   @Override public void changedUpdate(DocumentEvent e) {
    //     /* not needed */<commit_msg>refactor: replace if else statement by a single method invocation<commit_after>// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.Arrays;
import java.util.List;
import javax.swing.*;
import javax.swing.text.JTextComponent;

public class MainPanel extends JPanel {
  protected static final int MAX_LEN = 6;
  protected final JCheckBox check = new JCheckBox("use FocusTraversalPolicy", true);
  protected final JButton button = new JButton("Next");
  protected final transient List<JTextField> list = Arrays.asList(makeTextField(), makeTextField());

  public MainPanel() {
    super(new BorderLayout());
    setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
      @Override public Component getComponentAfter(Container focusCycleRoot, Component cmp) {
        System.out.println("getComponentAfter");
        button.setEnabled(isAllValid());
        return super.getComponentAfter(focusCycleRoot, cmp);
      }

      @Override public Component getComponentBefore(Container focusCycleRoot, Component cmp) {
        System.out.println("getComponentAfter");
        button.setEnabled(isAllValid());
        return super.getComponentBefore(focusCycleRoot, cmp);
      }
    });
    setFocusCycleRoot(true);

    button.setEnabled(false);
    check.addActionListener(e -> setFocusCycleRoot(((JCheckBox) e.getSource()).isSelected()));

    Box box = Box.createVerticalBox();
    box.add(check);
    box.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    list.forEach(c -> {
      box.add(Box.createVerticalStrut(10));
      box.add(c);
    });
    JPanel p = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    p.add(button);
    add(box, BorderLayout.NORTH);
    add(p, BorderLayout.SOUTH);
    setPreferredSize(new Dimension(320, 240));
  }

  protected final boolean isAllValid() {
    return list.stream().allMatch(t -> t.getInputVerifier().verify(t));
  }

  protected final JTextField makeTextField() {
    JTextField textField = new JTextField(24);
    textField.setInputVerifier(new InputVerifier() {
      @Override public boolean verify(JComponent c) {
        if (c instanceof JTextComponent) {
          JTextComponent tc = (JTextComponent) c;
          String str = tc.getText().trim();
          return !str.isEmpty() && MAX_LEN - str.length() >= 0;
        }
        return false;
      }

      @Override public boolean shouldYieldFocus(JComponent input) {
        System.out.println("shouldYieldFocus");
        button.setEnabled(isAllValid());
        return super.shouldYieldFocus(input);
      }
    });
    textField.addFocusListener(new FocusAdapter() {
      @Override public void focusLost(FocusEvent e) {
        if (e.isTemporary()) {
          return;
        }
        System.out.println("focusLost");
        button.setEnabled(isAllValid());
      }
    });
    // // TEST:
    // textField.getDocument().addDocumentListener(new DocumentListener() {
    //   @Override public void insertUpdate(DocumentEvent e) {
    //     System.out.println("insertUpdate");
    //   }
    //   @Override public void removeUpdate(DocumentEvent e) {
    //     System.out.println("removeUpdate");
    //   }
    //   @Override public void changedUpdate(DocumentEvent e) {
    //     /* not needed */
{'fraction_non_alphanumeric': 0.12598081952920662, 'fraction_numerical': 0.006393490264458007, 'mean_word_length': 3.2123623011015914, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '3951606', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1961, 'n_words': 410}