Adding text - object

The paragraph’s method to add text do understand simple strings and collections. Additionally, some special objects can be used to add text:

  • String Template version 4 objects ST

  • Objects implementing the HasText interface from skb-interfaces

  • Objects implementing the HasTextCluster interface from skb-interfaces

  • Objects implementing the DoesRender interface from skb-interfaces

  • Objects implementing the DoesRenderToWidth interface from skb-interfaces

  • Objects implementing the RendersToCluster interface from skb-interfaces

  • Objects implementing the RendersToClusterWidth interface from skb-interfaces

The following examples show this behavior.

ST

We start with ST object. The following example creates a simple ST object with some text, and adds it to a paragraph.

1
2
3
4
ST st = new ST(new LoremIpsum().getWords(10));
AsciiParagraph ap = new AsciiParagraph();
ap.addText(st);
System.out.println(ap.render());

The resulting output shows that the rendered ST is used as text for the paragraph:

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam

HasText

Next, an object that implements HasText. The following example creates a simple HasText object with some text, and adds it to a paragraph.

1
2
3
4
5
6
7
8
9
10
AsciiParagraph ap = new AsciiParagraph();
class ObjectHasText implements HasText{
        @Override
        public String getText() {
                return new LoremIpsum().getWords(10);
        }
}

ap.addText(new ObjectHasText());
System.out.println(ap.render());

The resulting output:

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam

HasTextCluster

Next, an object that implements HasTextCluster. The following example creates a simple HasTextCluster object with some text, and adds it to a paragraph.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
AsciiParagraph ap = new AsciiParagraph();
class ObjectHasTextCluster implements HasTextCluster{
        @Override
        public Collection<String> getTextAsCollection() {
                ArrayList<String> text = new ArrayList<>();
                text.add(new LoremIpsum().getWords(10));
                text.add(new LoremIpsum().getWords(10));
                text.add(new LoremIpsum().getWords(10));
                return text;
        }
}

ap.addText(new ObjectHasTextCluster());
System.out.println(ap.render());

The resulting output:

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed  diam  Lorem  ipsum
dolor sit amet, consetetur sadipscing elitr, sed  diam  Lorem  ipsum  dolor  sit
amet, consetetur sadipscing elitr, sed diam

DoesRender

Next, an object that implements DoesRender. The following example creates a simple DoesRender object with some text, and adds it to a paragraph.

1
2
3
4
5
6
7
8
9
10
AsciiParagraph ap = new AsciiParagraph();
class ObjectDoesRender implements DoesRender{
        @Override
        public String render() {
                return new LoremIpsum().getWords(10);
        }
}

ap.addText(new ObjectDoesRender());
System.out.println(ap.render());

The resulting output:

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam

DoesRenderToWidth

Next, an object that implements DoesRenderToWidth. The following example uses an AsciiParagraph itself as a DoesRenderToWidth object with some text, and adds it to a paragraph.

1
2
3
4
5
6
7
AsciiParagraph renderToWidth = new AsciiParagraph();
renderToWidth.addText(new LoremIpsum().getWords(30));

AsciiParagraph ap = new AsciiParagraph();
ap.getContext().setWidth(40);
ap.addText((DoesRenderToWidth)renderToWidth);
System.out.println(ap.render());

The resulting output:

Lorem ipsum dolor sit  amet,  consetetur
sadipscing elitr, sed diam nonumy eirmod
tempor  invidunt  ut  labore  et  dolore
magna aliquyam erat, sed diam  voluptua.
At vero eos et accusam et

RendersToCluster

Next, an object that implements RendersToCluster. The following example creates a simple RendersToCluster object with some text, and adds it to a paragraph.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
AsciiParagraph ap = new AsciiParagraph();
class ObjectRendersToCluster implements RendersToCluster{
        @Override
        public Collection<String> renderAsCollection() {
                ArrayList<String> text = new ArrayList<>();
                text.add(new LoremIpsum().getWords(10));
                text.add(new LoremIpsum().getWords(10));
                text.add(new LoremIpsum().getWords(10));
                return text;
        }
}

ap.addText(new ObjectRendersToCluster());
System.out.println(ap.render());

The resulting output:

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed  diam  Lorem  ipsum
dolor sit amet, consetetur sadipscing elitr, sed  diam  Lorem  ipsum  dolor  sit
amet, consetetur sadipscing elitr, sed diam

RendersToClusterWidth

Next, an object that implements RendersToClusterWidth. The following example uses an AsciiParagraph itself as a RendersToClusterWidth object with some text, and adds it to a paragraph.

1
2
3
4
5
6
7
AsciiParagraph renderToClusterWidth = new AsciiParagraph();
renderToClusterWidth.addText(new LoremIpsum().getWords(30));

AsciiParagraph ap = new AsciiParagraph();
ap.getContext().setWidth(40);
ap.addText((RendersToClusterWidth)renderToClusterWidth);
System.out.println(ap.render());

The resulting output:

Lorem ipsum dolor sit  amet,  consetetur
sadipscing elitr, sed diam nonumy eirmod
tempor  invidunt  ut  labore  et  dolore
magna aliquyam erat, sed diam  voluptua.
At vero eos et accusam et