<commit_before>package com.ait.lienzo.client.core.shape.wires.util;

import com.ait.lienzo.client.core.shape.Text;
import com.ait.lienzo.client.core.shape.TextBoundsWrap;
import com.ait.lienzo.client.core.shape.wires.WiresConnector;
import com.ait.lienzo.client.core.types.BoundingBox;
import com.ait.lienzo.client.core.types.Point2D;
import com.ait.lienzo.client.core.types.Point2DArray;
import com.ait.lienzo.client.core.util.Geometry;
import com.ait.lienzo.shared.core.types.TextAlign;
import com.ait.tooling.common.api.java.util.function.BiConsumer;

public class WiresConnectorLabelFactory
{
    public static WiresConnectorLabel newLabelOnFirstSegment(final String text,
                                                             final WiresConnector connector)
    {
        return new WiresConnectorLabel(text, connector, new FirstSegmentLabelExecutor().consumer());
    }

    public static WiresConnectorLabel newLabelOnLongestSegment(final String text,
                                                               final WiresConnector connector)
    {
        return new WiresConnectorLabel(text, connector, new LongestSegmentLabelExecutor().consumer());
    }

    public static class SegmentLabelExecutor
    {
        private static final double TEXT_WRAP_MAX_WIDTH  = 350d;

        private static final double TEXT_WRAP_MAX_HEIGHT = 11d;

        private static final double OFFSET               = 10d;

        public BiConsumer<Segment, Text> consumer()
        {
            return new BiConsumer<Segment, Text>()
            {
                @Override
                public void accept(final Segment segment,
                                   final Text text)
                {
                    // Calculate some first segment dimensions on a cartesian axis.
                    final Point2D start    = segment.start;
                    final Point2D end      = segment.end;
                    final double  distance = segment.length;
                    final double  rotation = segment.tetha;
                    final Point2D center   = Geometry.findCenter(start, end);
                    final double  cos      = Math.cos(rotation);
                    final double  sin      = Math.sin(rotation);

                    // Wrap the text.
                    final double         maxDistanceX = TEXT_WRAP_MAX_WIDTH;
                    final double         maxDistanceY = distance > TEXT_WRAP_MAX_HEIGHT ? TEXT_WRAP_MAX_HEIGHT : distance;
                    final BoundingBox    wrap         = new BoundingBox(0, 0, maxDistanceX, maxDistanceY);
                    final TextBoundsWrap textWrap     = new TextBoundsWrap(text, wrap);
                    text.setWrapper(textWrap);
                    text.setTextAlign(TextAlign.LEFT);
                    text.moveToTop();


                    // Set the right location.
                    final BoundingBox tbb = textWrap.getTextBoundaries();
                    final Point2D offset = new Point2D(Math.abs(OFFSET * sin), Math.abs(OFFSET * cos) * -1);
                    final double middleCenterSegmentWidth = Math.abs(start.getX() - center.getX()) / 2;
                    text.setLocation(center.minus(middleCenterSegmentWidth, tbb.getHeight()).add(offset));
                }
            };
        }

    }

    public static class FirstSegmentLabelExecutor
    {
        private final BiConsumer<Segment, Text> executor;

        public FirstSegmentLabelExecutor() {
            this(new SegmentLabelExecutor().consumer());
        }

        FirstSegmentLabelExecutor(BiConsumer<Segment, Text> executor) {<commit_msg>JBPM-7718 reverting WiresConnectorLabelFactory and apply line break<commit_after>package com.ait.lienzo.client.core.shape.wires.util;

import com.ait.lienzo.client.core.shape.Text;
import com.ait.lienzo.client.core.shape.TextBoundsAndLineBreaksWrap;
import com.ait.lienzo.client.core.shape.TextBoundsWrap;
import com.ait.lienzo.client.core.shape.wires.WiresConnector;
import com.ait.lienzo.client.core.types.BoundingBox;
import com.ait.lienzo.client.core.types.Point2D;
import com.ait.lienzo.client.core.types.Point2DArray;
import com.ait.lienzo.client.core.util.Geometry;
import com.ait.lienzo.shared.core.types.TextAlign;
import com.ait.tooling.common.api.java.util.function.BiConsumer;

public class WiresConnectorLabelFactory
{
    public static WiresConnectorLabel newLabelOnFirstSegment(final String text,
                                                             final WiresConnector connector)
    {
        return new WiresConnectorLabel(text, connector, new FirstSegmentLabelExecutor().consumer());
    }

    public static WiresConnectorLabel newLabelOnLongestSegment(final String text,
                                                               final WiresConnector connector)
    {
        return new WiresConnectorLabel(text, connector, new LongestSegmentLabelExecutor().consumer());
    }

    public static class SegmentLabelExecutor
    {
        private static final double TEXT_WRAP_MAX_WIDTH  = 200d;

        private static final double TEXT_WRAP_MAX_HEIGHT = 11d;

        private static final double OFFSET               = 10d;

        public BiConsumer<Segment, Text> consumer()
        {
            return new BiConsumer<Segment, Text>()
            {
                @Override
                public void accept(final Segment segment,
                                   final Text text)
                {
                    // Calculate some first segment dimensions on a cartesian axis.
                    final Point2D start    = segment.start;
                    final Point2D end      = segment.end;
                    final double  distance = segment.length;
                    final double  rotation = segment.tetha;
                    final Point2D center   = Geometry.findCenter(start, end);
                    final double  cos      = Math.cos(rotation);
                    final double  sin      = Math.sin(rotation);

                    // Wrap the text.
                    final double         maxDistanceX = distance > TEXT_WRAP_MAX_WIDTH ? TEXT_WRAP_MAX_WIDTH : distance;
                    final double         maxDistanceY = distance > TEXT_WRAP_MAX_HEIGHT ? TEXT_WRAP_MAX_HEIGHT : distance;
                    final BoundingBox    wrap         = new BoundingBox(0, 0, maxDistanceX, maxDistanceY);
                    final TextBoundsAndLineBreaksWrap textWrap     = new TextBoundsAndLineBreaksWrap(text, wrap);
                    text.setWrapper(textWrap);
                    text.setTextAlign(TextAlign.LEFT);
                    text.moveToTop();


                    // Set the right location.
                    final BoundingBox tbb = textWrap.getTextBoundaries();
                    final double  tbbw   = tbb.getWidth() / 2;
                    final double  tox    = Math.abs(tbbw * cos);
                    final Point2D offset = new Point2D(Math.abs(OFFSET * sin), Math.abs(OFFSET * cos) * -1);
                    text.setLocation(center.minus(tox, tbb.getHeight()).add(offset));
                }
            };
        }

    }

    public static class FirstSegmentLabelExecutor
    {
        private final BiConsumer<Segment, Text> executor;

        public FirstSegmentLabelExecutor() {
            this(new SegmentLabelExecutor().consumer());
        }

        FirstSegmentLabelExecutor(BiConsumer<Segment, Text> executor) {
{'fraction_non_alphanumeric': 0.07568004309183948, 'fraction_numerical': 0.00538647993536224, 'mean_word_length': 1.8853923853923853, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1120935', 'n_tokens_mistral': 1950, 'n_tokens_neox': 1843, 'n_words': 419}