-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e5f5b4
commit 2e54a5a
Showing
10 changed files
with
240 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
src/main/java/dev/latvian/apps/webutils/html/HTMLTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
package dev.latvian.apps.webutils.html; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class HTMLTable implements TagFunction { | ||
public static class Col { | ||
public final HTMLTable table; | ||
public final int x; | ||
private Cell label; | ||
private TagFunction applyToCell = null; | ||
|
||
private Col(HTMLTable table, int x) { | ||
this.table = table; | ||
this.x = x; | ||
} | ||
|
||
public Col applyToCell(TagFunction applyToCell) { | ||
this.applyToCell = applyToCell; | ||
return this; | ||
} | ||
|
||
public Col set(int y, TagFunction value) { | ||
table.set(x, y, value); | ||
return this; | ||
} | ||
} | ||
|
||
public static class Row { | ||
public final HTMLTable table; | ||
public final int y; | ||
public final List<Cell> cells; | ||
private TagFunction apply = null; | ||
private TagFunction applyToCell = null; | ||
|
||
public Row(HTMLTable table, int y) { | ||
this.table = table; | ||
this.y = y; | ||
this.cells = new ArrayList<>(2); | ||
} | ||
|
||
public Row apply(TagFunction apply) { | ||
this.apply = apply; | ||
return this; | ||
} | ||
|
||
public Row applyToCell(TagFunction applyToCell) { | ||
this.applyToCell = applyToCell; | ||
return this; | ||
} | ||
|
||
public Row set(int x, TagFunction value) { | ||
cells.get(x).set(value); | ||
return this; | ||
} | ||
} | ||
|
||
public static class Cell { | ||
public final Col col; | ||
public final Row row; | ||
private TagFunction value = TagFunction.IDENTITY; | ||
|
||
private Cell(Col col, Row row) { | ||
this.col = col; | ||
this.row = row; | ||
} | ||
|
||
public Cell set(TagFunction value) { | ||
this.value = value; | ||
return this; | ||
} | ||
} | ||
|
||
public final Row headRow; | ||
private final List<Col> cols; | ||
private final List<Row> rows; | ||
|
||
public HTMLTable() { | ||
this.headRow = new Row(this, -1); | ||
this.cols = new ArrayList<>(2); | ||
this.rows = new ArrayList<>(2); | ||
} | ||
|
||
public HTMLTable(TagFunction... labels) { | ||
this(); | ||
|
||
for (var label : labels) { | ||
addCol(label); | ||
} | ||
} | ||
|
||
public HTMLTable(String... labels) { | ||
this(); | ||
|
||
for (var label : labels) { | ||
addCol(t -> t.string(label)); | ||
} | ||
} | ||
|
||
public Col col(int x) { | ||
return cols.get(x); | ||
} | ||
|
||
public int cols() { | ||
return cols.size(); | ||
} | ||
|
||
public Row row(int y) { | ||
return rows.get(y); | ||
} | ||
|
||
public int rows() { | ||
return rows.size(); | ||
} | ||
|
||
public Col addCol(TagFunction label) { | ||
var col = new Col(this, cols.size()); | ||
cols.add(col); | ||
|
||
col.label = new Cell(col, headRow); | ||
headRow.cells.add(col.label); | ||
col.label.value = label; | ||
|
||
for (var row : rows) { | ||
row.cells.add(new Cell(col, row)); | ||
} | ||
|
||
return col; | ||
} | ||
|
||
public Row addRow() { | ||
var row = new Row(this, rows.size()); | ||
rows.add(row); | ||
|
||
for (var col : cols) { | ||
row.cells.add(new Cell(col, row)); | ||
} | ||
|
||
return row; | ||
} | ||
|
||
public void addRow(TagFunction... h) { | ||
var r = addRow(); | ||
|
||
for (int i = 0; i < h.length; i++) { | ||
r.set(i, h[i]); | ||
} | ||
} | ||
|
||
public HTMLTable set(int x, int y, TagFunction value) { | ||
rows.get(y).set(x, value); | ||
return this; | ||
} | ||
|
||
@Override | ||
public void acceptTag(Tag tag) { | ||
var table = tag.table(); | ||
var theadTag = table.thead(); | ||
var headTag = theadTag.tr().add(headRow.apply); | ||
|
||
for (var cell : headRow.cells) { | ||
headTag.th().add(cell.value).add(headRow.applyToCell).add(cell.col.applyToCell); | ||
} | ||
|
||
var tbodyTag = table.tbody(); | ||
|
||
for (var row : rows) { | ||
var rowTag = tbodyTag.tr().add(row.apply); | ||
|
||
for (var cell : row.cells) { | ||
rowTag.td().add(cell.value).add(row.applyToCell).add(cell.col.applyToCell); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
src/main/java/dev/latvian/apps/webutils/html/TagConvertible.java
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
src/main/java/dev/latvian/apps/webutils/html/TagFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dev.latvian.apps.webutils.html; | ||
|
||
@FunctionalInterface | ||
public interface TagFunction { | ||
TagFunction IDENTITY = t -> { | ||
}; | ||
|
||
void acceptTag(Tag tag); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/dev/latvian/apps/webutils/html/TagResponseContent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package dev.latvian.apps.webutils.html; | ||
|
||
import dev.latvian.apps.tinyserver.content.MimeType; | ||
import dev.latvian.apps.tinyserver.content.ResponseContent; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
public record TagResponseContent(Tag tag, boolean header) implements ResponseContent { | ||
@Override | ||
public String type() { | ||
return MimeType.HTML; | ||
} | ||
|
||
@Override | ||
public void write(OutputStream outputStream) throws IOException { | ||
} | ||
} |
19 changes: 0 additions & 19 deletions
19
src/main/java/dev/latvian/apps/webutils/html/UserError.java
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
src/test/java/dev/latvian/apps/webutils/test/TableTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package dev.latvian.apps.webutils.test; | ||
|
||
import dev.latvian.apps.ansi.ANSI; | ||
import dev.latvian.apps.ansi.log.Log; | ||
import dev.latvian.apps.webutils.html.HTMLTable; | ||
import dev.latvian.apps.webutils.html.PairedTag; | ||
import dev.latvian.apps.webutils.html.TagANSI; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class TableTest { | ||
@Test | ||
public void table() { | ||
var table = new HTMLTable("Col 1", "Col 2", "Col 3"); | ||
|
||
for (int i = 0; i < 10; i++) { | ||
var j = i; | ||
table.addRow(t -> t.string("Row " + j), t -> t.string("Value " + j), t -> t.string("Another value " + j)); | ||
} | ||
|
||
Log.info(ANSI.empty().append(ANSI.LINE).append(TagANSI.of(new PairedTag("").add(table), true))); | ||
} | ||
} |