<commit_before>package com.github.weisj.darklaf.ui.demo;

import com.github.weisj.darklaf.ui.DemoPanel;
import com.github.weisj.darklaf.util.LazyValue;
import sun.java2d.loops.CustomComponent;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Component;
import java.awt.Container;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

public abstract class BaseComponentDemo implements ComponentDemo, DemoExecutionSpec {

    private static final boolean DEBUG = false;
    private final List<DemoSpec<?>> demoSpecList = new ArrayList<>();
    private final LazyValue<JComponent> componentLazyValue = new LazyValue<>(this::createComponent);

    protected void init() {}

    protected final void addSpec(final DemoSpec<?> spec) {
        demoSpecList.add(spec);
    }

    protected abstract JComponent createComponent();

    @Override
    public JComponent getComponent() {
        return componentLazyValue.get();
    }

    @SuppressWarnings("unchecked")
    @Override
    public JComponent getContentPane() {
        JComponent component = getComponent();
        if (component instanceof DemoPanel) return component;
        init();

        final Runnable updateComponent = () -> {
            if (!DEBUG) {
                component.invalidate();
                component.doLayout();
                Component parent = component.getParent();
                if (parent != null) {
                    parent.invalidate();
                    parent.doLayout();
                }
            }
        };

        DemoPanel demoPanel = new DemoPanel(component);
        JComponent controls = demoPanel.addControls();
        for (DemoSpec<?> demoSpec : demoSpecList) {
            if (demoSpec instanceof SpacerSpec) {
                if (controls.getComponentCount() > 0) {
                    controls = demoPanel.addControls(2);
                }
            } else if (demoSpec instanceof BooleanDemoSpec) {
                JCheckBox checkBox = new JCheckBox(demoSpec.getName());
                checkBox.setSelected(((BooleanDemoSpec) demoSpec).get(component) == Boolean.TRUE);
                checkBox.addActionListener(e -> {
                    ((BooleanDemoSpec) demoSpec).set(component, checkBox.isSelected());
                    updateComponent.run();
                });
                controls.add(checkBox);
            } else if (demoSpec instanceof CustomControlSpec) {
                if (controls.getComponentCount() > 0) {
                    controls = demoPanel.addControls();
                }
                ((CustomControlSpec) demoSpec).builderClosure.accept(controls);
                controls = demoPanel.addControls();
            } else {
                JComboBox<Object> comboBox = new JComboBox<>(demoSpec.getPossibleValues().toArray());
                Object initial = demoSpec.get(component);
                if (!demoSpec.getPossibleValues().contains(initial)) {
                    initial = demoSpec.getPossibleValues().get(0);
                }
                comboBox.addItemListener(e -> {
                    ((DemoSpec<Object>) demoSpec).set(component, e.getItem());
                    updateComponent.run();
                });
                comboBox.setSelectedItem(initial);
                controls.add(new JLabel(demoSpec.getName()));
                controls.add(comboBox);
            }
        }

        return demoPanel;
    }

    protected final void booleanSpec(final String key) {
        addSpec(new BooleanDemoSpec(key, new ClientPropertySpecBinding<>(key)));
    }

    protected final <C extends JComponent> void booleanSpec(final String name,
            final BiConsumer<C, Boolean> setter, final Function<C, Boolean> getter) {
        addSpec(new BooleanDemoSpec(name, new FunctionBinding<>(setter, getter)));
    }

    @SuppressWarnings("unchecked")
    protected final <C extends JComponent, T> void binaryOptionSpec(final String name,
            final BiConsumer<C, T> setter, final Function<C, T> getter,
            final T option1, final T option2) {
        booleanSpec(name,
                (c, b) -> setter.accept((C) c, b ? option1 : option2),
                c -> !Objects.equals(getter.apply((C) c), option2));
    }<commit_msg>Demo: Don't add control separator if no controls are defined.<commit_after>package com.github.weisj.darklaf.ui.demo;

import com.github.weisj.darklaf.ui.DemoPanel;
import com.github.weisj.darklaf.util.LazyValue;
import sun.java2d.loops.CustomComponent;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Component;
import java.awt.Container;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

public abstract class BaseComponentDemo implements ComponentDemo, DemoExecutionSpec {

    private static final boolean DEBUG = false;
    private final List<DemoSpec<?>> demoSpecList = new ArrayList<>();
    private final LazyValue<JComponent> componentLazyValue = new LazyValue<>(this::createComponent);

    protected void init() {}

    protected final void addSpec(final DemoSpec<?> spec) {
        demoSpecList.add(spec);
    }

    protected abstract JComponent createComponent();

    @Override
    public JComponent getComponent() {
        return componentLazyValue.get();
    }

    @Override
    public JComponent getContentPane() {
        JComponent component = getComponent();
        if (component instanceof DemoPanel) return component;
        init();

        DemoPanel demoPanel = new DemoPanel(component);
        if (!demoSpecList.isEmpty()) {
            setupControls(component, demoPanel);
        }

        return demoPanel;
    }

    @SuppressWarnings("unchecked")
    private void setupControls(final JComponent component, final DemoPanel demoPanel) {
        final Runnable updateComponent = () -> {
            if (!DEBUG) {
                component.invalidate();
                component.doLayout();
                Component parent = component.getParent();
                if (parent != null) {
                    parent.invalidate();
                    parent.doLayout();
                }
            }
        };

        JComponent controls = demoPanel.addControls();
        for (DemoSpec<?> demoSpec : demoSpecList) {
            if (demoSpec instanceof SpacerSpec) {
                if (controls.getComponentCount() > 0) {
                    controls = demoPanel.addControls(2);
                }
            } else if (demoSpec instanceof BooleanDemoSpec) {
                JCheckBox checkBox = new JCheckBox(demoSpec.getName());
                checkBox.setSelected(((BooleanDemoSpec) demoSpec).get(component) == Boolean.TRUE);
                checkBox.addActionListener(e -> {
                    ((BooleanDemoSpec) demoSpec).set(component, checkBox.isSelected());
                    updateComponent.run();
                });
                controls.add(checkBox);
            } else if (demoSpec instanceof CustomControlSpec) {
                if (controls.getComponentCount() > 0) {
                    controls = demoPanel.addControls();
                }
                ((CustomControlSpec) demoSpec).builderClosure.accept(controls);
                controls = demoPanel.addControls();
            } else {
                JComboBox<Object> comboBox = new JComboBox<>(demoSpec.getPossibleValues().toArray());
                Object initial = demoSpec.get(component);
                if (!demoSpec.getPossibleValues().contains(initial)) {
                    initial = demoSpec.getPossibleValues().get(0);
                }
                comboBox.addItemListener(e -> {
                    ((DemoSpec<Object>) demoSpec).set(component, e.getItem());
                    updateComponent.run();
                });
                comboBox.setSelectedItem(initial);
                controls.add(new JLabel(demoSpec.getName()));
                controls.add(comboBox);
            }
        }
    }

    protected final void booleanSpec(final String key) {
        addSpec(new BooleanDemoSpec(key, new ClientPropertySpecBinding<>(key)));
    }

    protected final <C extends JComponent> void booleanSpec(final String name,
            final BiConsumer<C, Boolean> setter, final Function<C, Boolean> getter) {
        addSpec(new BooleanDemoSpec(name, new FunctionBinding<>(setter, getter)));
    }

    @SuppressWarnings("unchecked")
    protected final <C extends JComponent, T> void binaryOptionSpec(final String name,
            final BiConsumer<C, T> setter, final Function<C, T> getter,
            final T option1, final T option2) {
        booleanSpec(name,
                (c, b) -> setter.accept((C) c, b ? option1 : option2),
                c -> !Objects.equals(getter.apply((C) c), option2));
    }
{'fraction_non_alphanumeric': 0.10516191086886822, 'fraction_numerical': 0.0021374372127818744, 'mean_word_length': 2.3172633817795107, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 53, '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': '831673', 'n_tokens_mistral': 2491, 'n_tokens_neox': 2367, 'n_words': 539}