LibraryInternalCrossPackageStyleUtil.java

package swingtree.style;

import com.google.errorprone.annotations.DoNotCall;
import org.jspecify.annotations.Nullable;
import sprouts.Result;
import swingtree.UI;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.util.Objects;


/**
 *  <b>This class is technically public, but NOT INTENDED TO BE CALLED BY CLIENT CODE!</b>
 *  It has to be public to allow for code reuse between the {@link swingtree} and {@link swingtree.style} packages.
 */
@Deprecated
public final class LibraryInternalCrossPackageStyleUtil {
    private static final Insets ZERO_INSETS = new Insets(0,0,0,0);

    @DoNotCall @Deprecated
    public static Result<Insets> _onlyBorderInsetsOf(@Nullable Border b, java.awt.Component c) {
        return Result.ofTry(Insets.class, ()->{
            if ( b == null )
                return ZERO_INSETS;

            if ( b instanceof StyleAndAnimationBorder )
                return ((StyleAndAnimationBorder<?>)b).getFullPaddingInsets();

            // Compound border
            if ( b instanceof javax.swing.border.CompoundBorder ) {
                javax.swing.border.CompoundBorder cb = (javax.swing.border.CompoundBorder) b;
                return cb.getOutsideBorder().getBorderInsets(c);
            }
            Insets insets = b.getBorderInsets(c);
            if ( _needsNimbusBorderInsetsCorrection(c) ) {
                int min = Math.min(insets.top, Math.min(insets.left, Math.min(insets.bottom, insets.right)));
                insets = new Insets(min, min, min, min);
            }
            return insets;
        });
    }

    private static boolean _needsNimbusBorderInsetsCorrection(Component c) {
        if ( UI.currentLookAndFeel() == UI.LookAndFeel.NIMBUS ) {
            return c instanceof JButton || c instanceof JToggleButton;
        }
        return false;
    }

    @DoNotCall @Deprecated
    public static void applyFontConfAlignmentsToComponent(FontConf fontConf, JComponent owner) {
        fontConf.horizontalAlignment().forSwing().ifPresent( forSwing -> {
            if ( owner instanceof JLabel ) {
                JLabel label = (JLabel) owner;
                if ( !Objects.equals( label.getHorizontalAlignment(), forSwing ) )
                    label.setHorizontalAlignment( forSwing );
            }
            if ( owner instanceof AbstractButton ) {
                AbstractButton button = (AbstractButton) owner;
                if ( !Objects.equals( button.getHorizontalAlignment(), forSwing ) )
                    button.setHorizontalAlignment( forSwing );
            }
            if ( owner instanceof JTextField ) {
                JTextField textField = (JTextField) owner;
                if ( !Objects.equals( textField.getHorizontalAlignment(), forSwing ) )
                    textField.setHorizontalAlignment( forSwing );
            }
            if ( owner instanceof JTextPane ) {
                int forStyle = StyleConstants.ALIGN_LEFT;
                if ( forSwing == SwingConstants.LEFT ) {
                    forStyle = StyleConstants.ALIGN_LEFT;
                } else if ( forSwing == SwingConstants.RIGHT ) {
                    forStyle = StyleConstants.ALIGN_RIGHT;
                } else if ( forSwing == SwingConstants.CENTER ) {
                    forStyle = StyleConstants.ALIGN_CENTER;
                } else {
                    forStyle = StyleConstants.ALIGN_JUSTIFIED;
                }
                JTextPane textPane = (JTextPane) owner;
                StyledDocument style = textPane.getStyledDocument();
                SimpleAttributeSet align= new SimpleAttributeSet();
                StyleConstants.setAlignment(align, forStyle);
                style.setParagraphAttributes(0, style.getLength(), align, false);
            }
        });
        fontConf.verticalAlignment().forSwing().ifPresent( forSwing -> {
            if ( owner instanceof JLabel ) {
                JLabel label = (JLabel) owner;
                if ( !Objects.equals( label.getVerticalAlignment(), forSwing ) )
                    label.setVerticalAlignment( forSwing );
            }
            if ( owner instanceof AbstractButton ) {
                AbstractButton button = (AbstractButton) owner;
                if ( !Objects.equals( button.getVerticalAlignment(), forSwing ) )
                    button.setVerticalAlignment( forSwing );
            }
        });
    }

}