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

package example;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Optional;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.StreamHandler;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.Caret;
import javax.swing.text.JTextComponent;

public final class MainPanel extends JPanel {
  private static final Logger LOGGER = Logger.getLogger(MainPanel.class.getName());

  private MainPanel() {
    super(new GridLayout(2, 1));
    JTextArea log = new JTextArea();
    LOGGER.setUseParentHandlers(false);
    LOGGER.addHandler(new TextAreaHandler(new TextAreaOutputStream(log)));

    JTextArea textArea = new JTextArea("012345 67890 123456789") {
      private transient CopyOnSelectListener handler;
      @Override public void updateUI() {
        removeCaretListener(handler);
        removeMouseListener(handler);
        removeKeyListener(handler);
        super.updateUI();
        handler = new CopyOnSelectListener();
        addCaretListener(handler);
        addMouseListener(handler);
        addKeyListener(handler);
      }
    };

    add(makeTitledPanel("Copy On Select", new JScrollPane(textArea)));
    add(makeTitledPanel("log", new JScrollPane(log)));
    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;
  }

  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 CopyOnSelectListener extends MouseAdapter implements CaretListener, KeyListener {
  private static final Logger LOGGER = Logger.getLogger(CopyOnSelectListener.class.getName());
  private boolean dragActive;
  private boolean shiftActive;
  private int dot;
  private int mark;

  @Override public void caretUpdate(CaretEvent e) {
    if (!dragActive && !shiftActive) {
      fire(e.getSource());
    }
  }

  @Override public void mousePressed(MouseEvent e) {
    dragActive = true;
  }

  @Override public void mouseReleased(MouseEvent e) {
    dragActive = false;<commit_msg>SonarLint: update this logger<commit_after>// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Optional;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.StreamHandler;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.Caret;
import javax.swing.text.JTextComponent;

public final class MainPanel extends JPanel {
  private static final Logger LOGGER = Logger.getLogger("java-swing-tips");

  private MainPanel() {
    super(new GridLayout(2, 1));
    JTextArea log = new JTextArea();
    LOGGER.setUseParentHandlers(false);
    LOGGER.addHandler(new TextAreaHandler(new TextAreaOutputStream(log)));

    JTextArea textArea = new JTextArea("012345 67890 123456789") {
      private transient CopyOnSelectListener handler;
      @Override public void updateUI() {
        removeCaretListener(handler);
        removeMouseListener(handler);
        removeKeyListener(handler);
        super.updateUI();
        handler = new CopyOnSelectListener();
        addCaretListener(handler);
        addMouseListener(handler);
        addKeyListener(handler);
      }
    };

    add(makeTitledPanel("Copy On Select", new JScrollPane(textArea)));
    add(makeTitledPanel("log", new JScrollPane(log)));
    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;
  }

  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 CopyOnSelectListener extends MouseAdapter implements CaretListener, KeyListener {
  private static final Logger LOGGER = Logger.getLogger("java-swing-tips");
  private boolean dragActive;
  private boolean shiftActive;
  private int dot;
  private int mark;

  @Override public void caretUpdate(CaretEvent e) {
    if (!dragActive && !shiftActive) {
      fire(e.getSource());
    }
  }

  @Override public void mousePressed(MouseEvent e) {
    dragActive = true;
  }

  @Override public void mouseReleased(MouseEvent e) {
    dragActive = false;
{'fraction_non_alphanumeric': 0.10868548261344145, 'fraction_numerical': 0.009355995633868705, 'mean_word_length': 4.362876254180602, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '517832', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1862, 'n_words': 402}