package example;
//-*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
//@homepage@
import java.awt.*;
import java.awt.event.*;
import java.util.BitSet;
import java.util.Collections;
import java.util.stream.IntStream;
import javax.swing.*;
import javax.swing.undo.*;

public class MainPanel extends JPanel {
    // Long.MAX_VALUE
    // 0b111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111
    // protected static final int BIT_LENGTH = 63;
    protected static final int BIT_LENGTH = 72;
    protected BitSet status = BitSet.valueOf(new long[] {Long.valueOf("111000111", 2)});
    protected static final String ZEROPAD = String.join("", Collections.nCopies(BIT_LENGTH, "0"));
    protected final transient UndoableEditSupport undoSupport = new UndoableEditSupport();
    private final JLabel label = new JLabel(print(status));
    private final JPanel panel = new JPanel(new GridLayout(0, 8));
    private final UndoManager um = new UndoManager();
    private final Action undoAction = new UndoAction(um);
    private final Action redoAction = new RedoAction(um);
    private final Action selectAllAction = new AbstractAction("select all") {
        @Override public void actionPerformed(ActionEvent e) {
            BitSet newValue = new BitSet(BIT_LENGTH);
            newValue.set(0, BIT_LENGTH, true);
            undoSupport.postEdit(new StatusEdit(status, newValue));
            updateCheckBoxes(newValue);
        }
    };
    private final Action clearAllAction = new AbstractAction("clear all") {
        @Override public void actionPerformed(ActionEvent e) {
            BitSet newValue = new BitSet(BIT_LENGTH);
            undoSupport.postEdit(new StatusEdit(status, newValue));
            updateCheckBoxes(newValue);
        }
    };
    public MainPanel() {
        super(new BorderLayout());
        undoSupport.addUndoableEditListener(um);
        Box box = Box.createHorizontalBox();
        box.add(Box.createHorizontalGlue());
        box.add(new JButton(undoAction));
        box.add(Box.createHorizontalStrut(2));
        box.add(new JButton(redoAction));
        box.add(Box.createHorizontalStrut(2));
        box.add(new JButton(selectAllAction));
        box.add(Box.createHorizontalStrut(2));
        box.add(new JButton(clearAllAction));
        box.add(Box.createHorizontalStrut(2));

        IntStream.range(0, BIT_LENGTH).forEach(i -> {
            JCheckBox c = new JCheckBox(Integer.toString(i), status.get(i));
            c.addActionListener(e -> {
                JCheckBox cb = (JCheckBox) e.getSource();
                BitSet newValue = status.get(0, BIT_LENGTH);
                newValue.set(i, cb.isSelected());
                undoSupport.postEdit(new StatusEdit(status, newValue));
                status = newValue;
                label.setText(print(status));
            });
            panel.add(c);
        });

        label.setFont(label.getFont().deriveFont(8f));

        add(label, BorderLayout.NORTH);
        add(new JScrollPane(panel));
        add(box, BorderLayout.SOUTH);
        setPreferredSize(new Dimension(320, 240));
    }
    protected final void updateCheckBoxes(BitSet value) {
        status = value;
        for (int i = 0; i < BIT_LENGTH; i++) {
            ((JCheckBox) panel.getComponent(i)).setSelected(status.get(i));
        }
        label.setText(print(status));
    }
    private class StatusEdit extends AbstractUndoableEdit {
        private final BitSet oldValue;
        private final BitSet newValue;
        protected StatusEdit(BitSet oldValue, BitSet newValue) {
            super();
            this.oldValue = oldValue;
            this.newValue = newValue;
        }
        @Override public void undo() throws CannotUndoException {
            super.undo();
            updateCheckBoxes(oldValue);
        }
        @Override public void redo() throws CannotRedoException {
            super.redo();
            updateCheckBoxes(newValue);
        }
    }
    private static String print(BitSet l) {
        long[] la = l.toLongArray();
        StringBuilder buf = new StringBuilder();
        for (int i = la.length - 1; i >=  0; i--) {
            buf.append(Long.toUnsignedString(la[i], 2));
        }
        String b = buf.toString();
        int count = l.cardinality();
        return "<html>0b" + ZEROPAD.substring(b.length()) + b + "<br/> count: " + count;
    }
    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();
        }
        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 UndoAction extends AbstractAction {
    private final UndoManager um;
    protected UndoAction(UndoManager um) {
        super("undo");
        this.um = um;
    }
    @Override public void actionPerformed(ActionEvent e) {
        if (um.canUndo()) {
            um.undo();
        }
    }
}

class RedoAction extends AbstractAction {
    private final UndoManager um;
    protected RedoAction(UndoManager um) {
        super("redo");
        this.um = um;
    }
    @Override public void actionPerformed(ActionEvent e) {
        if (um.canRedo()) {
            um.redo();
        }
    }
}

@@ -4026,49 +4026,17 @@
 Set 
-l) %7B%0A        long%5B%5D la = l.toLongArray();
+bitSet) %7B
 %0A   
@@ -4098,43 +4098,37 @@
 or (
-int i = la.length - 1; i %3E=  0; i--
+long lv: bitSet.toLongArray()
 ) %7B%0A
@@ -4147,15 +4147,18 @@
 buf.
-append(
+insert(0, 
 Long
@@ -4180,12 +4180,9 @@
 ng(l
-a%5Bi%5D
+v
 , 2)
@@ -4249,17 +4249,22 @@
 count = 
-l
+bitSet
 .cardina

use StringBuilder#insert(0, ...) instead of reverse for loop StringBuilder#append(...)