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

package example;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.geom.Area;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import javax.swing.*;
import javax.swing.plaf.LayerUI;
import javax.swing.text.DefaultEditorKit;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new BorderLayout());
    add(new JScrollPane(new JTextArea()));
    setPreferredSize(new Dimension(320, 240));
  }

  public static JMenuBar makeManuBar() {
    Component edit = makeEditButtonBar(Arrays.asList(
        makeButton("Cut", new DefaultEditorKit.CutAction()),
        makeButton("Copy", new DefaultEditorKit.CopyAction()),
        makeButton("Paste", new DefaultEditorKit.PasteAction())));

    JMenu menu = new JMenu("File");
    menu.add("aaaaaaaaaa");
    menu.addSeparator();
    menu.add(makeEditMenuItem(edit));
    menu.addSeparator();
    menu.add("bbbb");
    menu.add("cccccc");
    menu.add("ddddd");

    JMenuBar mb = new JMenuBar();
    mb.add(menu);
    return mb;
  }

  private static JMenuItem makeEditMenuItem(Component edit) {
    JMenuItem item = new JMenuItem("Edit") {
      @Override public Dimension getPreferredSize() {
        Dimension d = super.getPreferredSize();
        d.width += edit.getPreferredSize().width;
        d.height = Math.max(edit.getPreferredSize().height, d.height);
        return d;
      }

      @Override protected void fireStateChanged() {
        setForeground(Color.BLACK);
        super.fireStateChanged();
      }
    };
    item.setEnabled(false);

    GridBagConstraints c = new GridBagConstraints();
    item.setLayout(new GridBagLayout());
    c.anchor = GridBagConstraints.LINE_END;
    // c.gridx = GridBagConstraints.RELATIVE;
    c.weightx = 1d;

    c.fill = GridBagConstraints.HORIZONTAL;
    item.add(Box.createHorizontalGlue(), c);
    c.fill = GridBagConstraints.NONE;
    item.add(edit, c);

    return item;
  }

  private static Component makeEditButtonBar(List<AbstractButton> list) {
    int size = list.size();
    JPanel p = new JPanel(new GridLayout(1, size, 0, 0)) {
      @Override public Dimension getMaximumSize() {
        return super.getPreferredSize();
      }
    };
    list.forEach(b -> {
      b.setIcon(new ToggleButtonBarCellIcon());
      p.add(b);
    });
    p.setBorder(BorderFactory.createEmptyBorder(4, 10, 4, 10));
    p.setOpaque(false);

    return new JLayer<>(p, new EditMenuLayerUI<>(list.get(size - 1)));
  }

  private static AbstractButton makeButton(String title, Action action) {
    AbstractButton b = new JButton(action);
    b.addActionListener(e -> {
      Component a = (Component) e.getSource();
      Container c = SwingUtilities.getAncestorOfClass(JPopupMenu.class, a);
      if (c instanceof JPopupMenu) {
        ((JPopupMenu) c).setVisible(false);
      }
    });
    b.setText(title);
    // b.setVerticalAlignment(SwingConstants.CENTER);
    // b.setVerticalTextPosition(SwingConstants.CENTER);
    // b.setHorizontalAlignment(SwingConstants.CENTER);
    b.setHorizontalTextPosition(SwingConstants.CENTER);
    b.setBorder(BorderFactory.createEmptyBorder());
    b.setContentAreaFilled(false);
    b.setFocusPainted(false);
    b.setOpaque(false);
    return b;
  }

  public static void main(String... args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGui();
      }
    });
  }

  public 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.setJMenuBar(makeManuBar());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

// https://ateraimemo.com/Swing/ToggleButtonBar.html
class ToggleButtonBarCellIcon implements Icon {
  @Override public void paintIcon(Component c, Graphics g, int x, int y) {
    Container parent = c.getParent();
    if (Objects.isNull(parent)) {
      return;
    }
    int r = 8;
    int w = c.getWidth();
    int h = c.getHeight() - 1;

    Graphics2D g2 = (Graphics2D) g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    Path2D p = new Path2D.Double();
    if (c == parent.getComponent(0)) {<commit_msg>refactor: use JRootPane#setJMenuBar(...) instead of JFrame#setJMenuBar(...)<commit_after>// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@

package example;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.geom.Area;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import javax.swing.*;
import javax.swing.plaf.LayerUI;
import javax.swing.text.DefaultEditorKit;

public final class MainPanel extends JPanel {
  private MainPanel() {
    super(new BorderLayout());
    EventQueue.invokeLater(() -> getRootPane().setJMenuBar(createManuBar()));
    add(new JScrollPane(new JTextArea()));
    setPreferredSize(new Dimension(320, 240));
  }

  public static JMenuBar createManuBar() {
    Component edit = makeEditButtonBar(Arrays.asList(
        makeButton("Cut", new DefaultEditorKit.CutAction()),
        makeButton("Copy", new DefaultEditorKit.CopyAction()),
        makeButton("Paste", new DefaultEditorKit.PasteAction())));

    JMenu menu = new JMenu("File");
    menu.add("aaaaaaaaaa");
    menu.addSeparator();
    menu.add(makeEditMenuItem(edit));
    menu.addSeparator();
    menu.add("bbbb");
    menu.add("cccccc");
    menu.add("ddddd");

    JMenuBar mb = new JMenuBar();
    mb.add(menu);
    return mb;
  }

  private static JMenuItem makeEditMenuItem(Component edit) {
    JMenuItem item = new JMenuItem("Edit") {
      @Override public Dimension getPreferredSize() {
        Dimension d = super.getPreferredSize();
        d.width += edit.getPreferredSize().width;
        d.height = Math.max(edit.getPreferredSize().height, d.height);
        return d;
      }

      @Override protected void fireStateChanged() {
        setForeground(Color.BLACK);
        super.fireStateChanged();
      }
    };
    item.setEnabled(false);

    GridBagConstraints c = new GridBagConstraints();
    item.setLayout(new GridBagLayout());
    c.anchor = GridBagConstraints.LINE_END;
    // c.gridx = GridBagConstraints.RELATIVE;
    c.weightx = 1d;

    c.fill = GridBagConstraints.HORIZONTAL;
    item.add(Box.createHorizontalGlue(), c);
    c.fill = GridBagConstraints.NONE;
    item.add(edit, c);

    return item;
  }

  private static Component makeEditButtonBar(List<AbstractButton> list) {
    int size = list.size();
    JPanel p = new JPanel(new GridLayout(1, size, 0, 0)) {
      @Override public Dimension getMaximumSize() {
        return super.getPreferredSize();
      }
    };
    list.forEach(b -> {
      b.setIcon(new ToggleButtonBarCellIcon());
      p.add(b);
    });
    p.setBorder(BorderFactory.createEmptyBorder(4, 10, 4, 10));
    p.setOpaque(false);

    return new JLayer<>(p, new EditMenuLayerUI<>(list.get(size - 1)));
  }

  private static AbstractButton makeButton(String title, Action action) {
    AbstractButton b = new JButton(action);
    b.addActionListener(e -> {
      Component a = (Component) e.getSource();
      Container c = SwingUtilities.getAncestorOfClass(JPopupMenu.class, a);
      if (c instanceof JPopupMenu) {
        ((JPopupMenu) c).setVisible(false);
      }
    });
    b.setText(title);
    // b.setVerticalAlignment(SwingConstants.CENTER);
    // b.setVerticalTextPosition(SwingConstants.CENTER);
    // b.setHorizontalAlignment(SwingConstants.CENTER);
    b.setHorizontalTextPosition(SwingConstants.CENTER);
    b.setBorder(BorderFactory.createEmptyBorder());
    b.setContentAreaFilled(false);
    b.setFocusPainted(false);
    b.setOpaque(false);
    return b;
  }

  public static void main(String... args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGui();
      }
    });
  }

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

// https://ateraimemo.com/Swing/ToggleButtonBar.html
class ToggleButtonBarCellIcon implements Icon {
  @Override public void paintIcon(Component c, Graphics g, int x, int y) {
    Container parent = c.getParent();
    if (Objects.isNull(parent)) {
      return;
    }
    int r = 8;
    int w = c.getWidth();
    int h = c.getHeight() - 1;

    Graphics2D g2 = (Graphics2D) g.create();
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    Path2D p = new Path2D.Double();
    if (c == parent.getComponent(0)) {
{'fraction_non_alphanumeric': 0.13102159468438537, 'fraction_numerical': 0.006229235880398671, 'mean_word_length': 3.4971988795518207, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 14, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '5929423', 'n_tokens_mistral': 3091, 'n_tokens_neox': 3041, 'n_words': 582}