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

package example;

import java.awt.*;
import java.util.Enumeration;
import javax.swing.*;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.ExpandVetoException;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new BorderLayout());

    JTree tree = new JTree(makeModel());
    tree.setRootVisible(false);
    tree.addTreeWillExpandListener(new TreeWillExpandListener() {
      private boolean isAdjusting;
      @Override public void treeWillExpand(TreeExpansionEvent e) throws ExpandVetoException {
        // collapseAll(tree); // StackOverflowError when collapsing nodes below 2nd level
        if (isAdjusting) {
          return;
        }
        isAdjusting = true;
        collapseFirstHierarchy(tree);
        tree.setSelectionPath(e.getPath());
        isAdjusting = false;
      }

      @Override public void treeWillCollapse(TreeExpansionEvent e) throws ExpandVetoException {
        // throw new ExpandVetoException(e, "Tree collapse cancelled");
      }
    });

    JScrollPane scroll = new JScrollPane(tree) {
      @Override public void updateUI() {
        setViewportBorder(null);
        super.updateUI();
        EventQueue.invokeLater(() -> {
          setViewportBorder(BorderFactory.createLineBorder(getViewport().getView().getBackground(), 5));
        });
      }
    };
    add(scroll);
    setPreferredSize(new Dimension(320, 240));
  }

  private static DefaultTreeModel makeModel() {
    DefaultMutableTreeNode set1 = new DefaultMutableTreeNode("Set 001");
    set1.add(new DefaultMutableTreeNode("111111111"));
    set1.add(new DefaultMutableTreeNode("22222222222"));
    set1.add(new DefaultMutableTreeNode("33333"));

    DefaultMutableTreeNode set2 = new DefaultMutableTreeNode("Set 002");
    set2.add(new DefaultMutableTreeNode("asdfasdfas"));
    set2.add(new DefaultMutableTreeNode("asdf"));

    DefaultMutableTreeNode set3 = new DefaultMutableTreeNode("Set 003");
    set3.add(new DefaultMutableTreeNode("asdfasdfasdf"));
    set3.add(new DefaultMutableTreeNode("qwerqwer"));
    set3.add(new DefaultMutableTreeNode("zvxcvzxcvzxzxcvzxcv"));

    DefaultMutableTreeNode set4 = new DefaultMutableTreeNode("Set 004");
    set4.add(new DefaultMutableTreeNode("444"));<commit_msg>refactor: ExpandVetoException is never thrown in the method<commit_after>// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

import java.awt.*;
import java.util.Enumeration;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new BorderLayout());

    JTree tree = new JTree(makeModel());
    tree.setRootVisible(false);
    tree.addTreeWillExpandListener(new TreeWillExpandListener() {
      private boolean isAdjusting;
      @Override public void treeWillExpand(TreeExpansionEvent e) { // throws ExpandVetoException {
        // collapseAll(tree); // StackOverflowError when collapsing nodes below 2nd level
        if (isAdjusting) {
          return;
        }
        isAdjusting = true;
        collapseFirstHierarchy(tree);
        tree.setSelectionPath(e.getPath());
        isAdjusting = false;
      }

      @Override public void treeWillCollapse(TreeExpansionEvent e) { // throws ExpandVetoException {
        // throw new ExpandVetoException(e, "Tree collapse cancelled");
      }
    });

    JScrollPane scroll = new JScrollPane(tree) {
      @Override public void updateUI() {
        setViewportBorder(null);
        super.updateUI();
        Border b = BorderFactory.createLineBorder(getViewport().getView().getBackground(), 5);
        EventQueue.invokeLater(() -> setViewportBorder(b));
      }
    };
    add(scroll);
    setPreferredSize(new Dimension(320, 240));
  }

  private static DefaultTreeModel makeModel() {
    DefaultMutableTreeNode set1 = new DefaultMutableTreeNode("Set 001");
    set1.add(new DefaultMutableTreeNode("111111111"));
    set1.add(new DefaultMutableTreeNode("22222222222"));
    set1.add(new DefaultMutableTreeNode("33333"));

    DefaultMutableTreeNode set2 = new DefaultMutableTreeNode("Set 002");
    set2.add(new DefaultMutableTreeNode("4444444444444"));
    set2.add(new DefaultMutableTreeNode("55555555"));

    DefaultMutableTreeNode set3 = new DefaultMutableTreeNode("Set 003");
    set3.add(new DefaultMutableTreeNode("666666666666666"));
    set3.add(new DefaultMutableTreeNode("777777777777"));
    set3.add(new DefaultMutableTreeNode("888888888888888888"));

    DefaultMutableTreeNode set4 = new DefaultMutableTreeNode("Set 004");
    set4.add(new DefaultMutableTreeNode("444"));
{'fraction_non_alphanumeric': 0.11228609882714863, 'fraction_numerical': 0.03691597769659681, 'mean_word_length': 4.1301775147929, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 9, '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': '7650994', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1479, 'n_words': 297}