Adding text - object

The table’s method to add content (add row with content) 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 table.

1
2
3
4
5
6
ST st = new ST(new LoremIpsum().getWords(10));
AsciiTable at = new AsciiTable();
at.addRule();
at.addRow(st);
at.addRule();
System.out.println(at.render());

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

┌──────────────────────────────────────────────────────────────────────────────┐
│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 table.

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

at.addRule();
at.addRow(new ObjectHasText());
at.addRule();
System.out.println(at.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 table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
AsciiTable at = new AsciiTable();
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;
        }
}

at.addRule();
at.addRow(new ObjectHasTextCluster());
at.addRule();
System.out.println(at.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 table.

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

at.addRule();
at.addRow(new ObjectDoesRender());
at.addRule();
System.out.println(at.render());

The resulting output:

┌──────────────────────────────────────────────────────────────────────────────┐
│Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam             │
└──────────────────────────────────────────────────────────────────────────────┘

DoesRenderToWidth

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

1
2
3
4
5
6
7
8
9
10
11
12
AsciiTable at = new AsciiTable();
class ObjectDoesRenderToWidth implements DoesRenderToWidth{
        @Override
        public String render(int width) {
                return new StrBuilder().appendWithSeparators(Text_To_FormattedText.left(new LoremIpsum().getWords(10), width), "\n").toString();
        }
}

at.addRule();
at.addRow(new ObjectDoesRenderToWidth());
at.addRule();
System.out.println(at.render(30));

The resulting output:

┌────────────────────────────┐
│Lorem ipsum dolor sit amet, │
│consetetur sadipscing elitr,│
│sed diam                    │
└────────────────────────────┘

RendersToCluster

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
AsciiTable at = new AsciiTable();
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;
        }
}

at.addRule();
at.addRow(new ObjectRendersToCluster());
at.addRule();
System.out.println(at.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 creates a simple RendersToClusterWidth object with some text, and adds it to a table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
AsciiTable at = new AsciiTable();
class ObjectRendersToClusterWidth implements RendersToClusterWidth{
        @Override
        public Collection<String> renderAsCollection(int width) {
                return ClusterElementTransformer.create().transform(
                                Text_To_FormattedText.justified(new LoremIpsum().getWords(30), width),
                                StrBuilder_To_String.create(),
                                ArrayListStrategy.create()
                );
        }
}

at.addRule();
at.addRow(new ObjectRendersToClusterWidth());
at.addRule();
System.out.println(at.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                                           │
└──────────────────────────────────────────────────────────────────────────────┘