diff --git a/src/main/java/com/osiris/desku/ui/display/RTable.java b/src/main/java/com/osiris/desku/ui/display/RTable.java index 684f78a..bd213d1 100644 --- a/src/main/java/com/osiris/desku/ui/display/RTable.java +++ b/src/main/java/com/osiris/desku/ui/display/RTable.java @@ -57,7 +57,7 @@ public RTable headers(Class clazz, Predicate predicate) { for (Field f : clazz.getFields()) { if (predicate.test(f)) { String name = f.getName(); - table.headers.add(new Table.Header().add(new Text(name))); + table.headers.add(new Text(name)); } } table.recalcMaxColumnWidthPercent(); diff --git a/src/main/java/com/osiris/desku/ui/display/Table.css b/src/main/java/com/osiris/desku/ui/display/Table.css index 7d3c116..82ef8e2 100644 --- a/src/main/java/com/osiris/desku/ui/display/Table.css +++ b/src/main/java/com/osiris/desku/ui/display/Table.css @@ -9,10 +9,9 @@ border: 1px solid #ddd; padding: 4px; width: 100%; - flex-grow: 1; align-items: center; } -.desku-table-row *, .desku-table-header * { +.desku-table-cell{ justify-content: center; align-items: center; flex-grow: 1; diff --git a/src/main/java/com/osiris/desku/ui/display/Table.java b/src/main/java/com/osiris/desku/ui/display/Table.java index 268dca2..b76c054 100644 --- a/src/main/java/com/osiris/desku/ui/display/Table.java +++ b/src/main/java/com/osiris/desku/ui/display/Table.java @@ -5,6 +5,8 @@ import com.osiris.events.Event; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import java.util.function.Consumer; public class Table extends Component { @@ -16,13 +18,15 @@ public class Table extends Component
{ } } - public Headers headers = new Headers(this); + public Row headers = new Row().addClass("desku-table-header"); public Rows rows = new Rows(this); /** - * Gets recalculated in {@link #headers(Header...)}. + * Gets recalculated in {@link #headers(Row...)}.
+ * Set to -1 or smaller, to disable. */ public double maxColumnWidthPercent = 100; public void recalcMaxColumnWidthPercent(){ + if(maxColumnWidthPercent < 0) return; maxColumnWidthPercent = (1.0 / headers.children.size()) * 100.0; for (Component header : headers.children) { header.putStyle("max-width", maxColumnWidthPercent+"%"); @@ -38,7 +42,7 @@ public Table() { add(headers, rows); addClass("desku-table"); rows.onAddedChild.addAction(e -> { - if(e.childComp instanceof Row){ + if(maxColumnWidthPercent > 0 && e.childComp instanceof Row){ Row row = (Row) e.childComp; for (Component rowColumn : row.children) { rowColumn.putStyle("max-width", maxColumnWidthPercent+"%"); @@ -54,7 +58,7 @@ public Table() { public Table headers(String... headers) { this.headers.removeAll(); for (String header : headers) { - this.headers.add(new Header().add(new Text(header))); + this.headers.add(new Text(header)); } recalcMaxColumnWidthPercent(); return _this; @@ -63,9 +67,9 @@ public Table headers(String... headers) { /** * Easily set/replace the headers. */ - public Table headers(Header... headers) { + public Table headers(Component... headers) { this.headers.removeAll(); - for (Header header : headers) { + for (Component header : headers) { this.headers.add(header); } recalcMaxColumnWidthPercent(); @@ -118,8 +122,8 @@ public Table row(int index, Row newRow) { return this; } - public Header getHeaderAt(int index){ - return (Header) headers.children.get(index); + public Component getHeaderAt(int index){ + return headers.children.get(index); } public static class Headers extends Component { @@ -127,7 +131,7 @@ public static class Headers extends Component { * Reference to parent table if needed for method chaining. */ public final Table t; - public Event
_onHeaderClick = new Event<>(); + public Event _onHeaderClick = new Event<>(); public Headers(Table table) { super("headers"); @@ -135,16 +139,16 @@ public Headers(Table table) { width("100%"); Consumer superAdd = this._add; this._add = e -> { - if(e.isFirstAdd && e.childComp instanceof Header){ + if(e.isFirstAdd && e.childComp instanceof Row){ e.childComp.onClick(click -> { - _onHeaderClick.execute((Header) e.childComp); + _onHeaderClick.execute((Row) e.childComp); }); } superAdd.accept(e); }; } - public Headers onHeaderClick(Consumer
code){ + public Headers onHeaderClick(Consumer code){ _onHeaderClick.addAction((event) -> code.accept(event)); return this; } @@ -182,27 +186,38 @@ public Rows onRowClick(Consumer code){ public static class Row extends Component { public Row() { addClass("desku-table-row"); - } - } - public static class Header extends Component
{ - /** - * Width and height styles of this header are - * also used for each of its rows. - */ - private boolean isForwardSizeToRows = true; + // Wrap all added children first into cell + Consumer superAdd = _add; + _add = e -> { + Cell cell = new Cell(); + cell.add(e.childComp); + e.childComp = cell; + superAdd.accept(e); + }; + } - public Header() { - addClass("desku-table-header"); + public List getCells(){ + List cells = new ArrayList<>(); + for (Component child : children) { + cells.add((Cell) child); + } + return cells; } - public boolean isForwardSizeToRows(){ - return isForwardSizeToRows; + public List> getContents(){ + List> contents = new ArrayList<>(); + for (Cell cell : getCells()) { + contents.add(cell.getContent()); + } + return contents; } + } - public Header forwardSizeToRows(boolean b){ - this.isForwardSizeToRows = b; - return this; + public static class Cell extends Component { + + public Cell(){ + addClass("desku-table-cell"); } public Component getContent(){ diff --git a/src/main/java/com/osiris/desku/ui/input/FileChooser.java b/src/main/java/com/osiris/desku/ui/input/FileChooser.java index 08fd6b2..5070c44 100644 --- a/src/main/java/com/osiris/desku/ui/input/FileChooser.java +++ b/src/main/java/com/osiris/desku/ui/input/FileChooser.java @@ -162,6 +162,7 @@ public void setDir(File dir) { removeAll(); table = new Table(); + table.maxColumnWidthPercent = -1; add(table); table.headers("Select", "Icon", dir == null ? ".." : // If this the case then parent shows drives/roots dir.getAbsolutePath().replace("\\", "/"), "Modified");