Code cleanup
This commit is contained in:
@@ -37,6 +37,7 @@ import java.util.List;
|
|||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.NonNull;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import net.runelite.client.ui.overlay.components.ComponentConstants;
|
import net.runelite.client.ui.overlay.components.ComponentConstants;
|
||||||
import net.runelite.client.ui.overlay.components.LayoutableRenderableEntity;
|
import net.runelite.client.ui.overlay.components.LayoutableRenderableEntity;
|
||||||
@@ -45,211 +46,111 @@ import net.runelite.client.ui.overlay.components.TextComponent;
|
|||||||
@Setter
|
@Setter
|
||||||
public class TableComponent implements LayoutableRenderableEntity
|
public class TableComponent implements LayoutableRenderableEntity
|
||||||
{
|
{
|
||||||
private static final TableRow EMPTY_ROW = TableRow.builder().build();
|
|
||||||
private static final TableElement EMPTY_ELEMENT = TableElement.builder().build();
|
private static final TableElement EMPTY_ELEMENT = TableElement.builder().build();
|
||||||
|
|
||||||
@Getter
|
|
||||||
private final Rectangle bounds = new Rectangle();
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final List<TableElement> columns = new ArrayList<>();
|
private final List<TableElement> columns = new ArrayList<>();
|
||||||
@Getter
|
@Getter
|
||||||
private final List<TableRow> rows = new ArrayList<>();
|
private final List<TableRow> rows = new ArrayList<>();
|
||||||
|
|
||||||
@Nonnull
|
@Getter
|
||||||
|
private final Rectangle bounds = new Rectangle();
|
||||||
|
|
||||||
private TableAlignment defaultAlignment = TableAlignment.LEFT;
|
private TableAlignment defaultAlignment = TableAlignment.LEFT;
|
||||||
@Nonnull
|
|
||||||
private Color defaultColor = Color.WHITE;
|
private Color defaultColor = Color.WHITE;
|
||||||
@Nonnull
|
|
||||||
private Dimension gutter = new Dimension(3, 0);
|
private Dimension gutter = new Dimension(3, 0);
|
||||||
@Nonnull
|
|
||||||
private Point preferredLocation = new Point();
|
private Point preferredLocation = new Point();
|
||||||
@Nonnull
|
|
||||||
private Dimension preferredSize = new Dimension(ComponentConstants.STANDARD_WIDTH, 0);
|
private Dimension preferredSize = new Dimension(ComponentConstants.STANDARD_WIDTH, 0);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dimension render(final Graphics2D graphics)
|
public Dimension render(final Graphics2D graphics)
|
||||||
{
|
{
|
||||||
final FontMetrics metrics = graphics.getFontMetrics();
|
final FontMetrics metrics = graphics.getFontMetrics();
|
||||||
final int[] columnWidths = getColumnWidths(metrics);
|
final TableRow colRow = TableRow.builder().elements(this.columns).build();
|
||||||
int height = 0;
|
final int[] columnWidths = getColumnWidths(metrics, colRow);
|
||||||
|
|
||||||
graphics.translate(preferredLocation.x, preferredLocation.y);
|
graphics.translate(preferredLocation.x, preferredLocation.y);
|
||||||
|
|
||||||
final int numRows = rows.size();
|
// Display the columns first
|
||||||
final int numCols = columns.size();
|
int height = displayRow(graphics, colRow, 0, columnWidths, metrics);
|
||||||
|
|
||||||
for (int row = 0; row < numRows; row++)
|
for (TableRow row : this.rows)
|
||||||
{
|
{
|
||||||
int x = 0;
|
height += displayRow(graphics, row, height, columnWidths, metrics);
|
||||||
int startingRowHeight = height;
|
|
||||||
for (int col = 0; col < numCols; col++)
|
|
||||||
{
|
|
||||||
int y = startingRowHeight;
|
|
||||||
final String[] lines = lineBreakText(getCellText(col, row), columnWidths[col], metrics);
|
|
||||||
for (String line : lines)
|
|
||||||
{
|
|
||||||
final TableAlignment alignment = getCellAlignment(row, col);
|
|
||||||
final int alignmentOffset = getAlignedPosition(line, alignment, columnWidths[col], metrics);
|
|
||||||
final TextComponent leftLineComponent = new TextComponent();
|
|
||||||
y += metrics.getHeight();
|
|
||||||
|
|
||||||
final Color lineColor = getCellColor(row, col);
|
|
||||||
|
|
||||||
leftLineComponent.setPosition(new Point(x + alignmentOffset, y));
|
|
||||||
leftLineComponent.setText(line);
|
|
||||||
leftLineComponent.setColor(lineColor);
|
|
||||||
leftLineComponent.render(graphics);
|
|
||||||
}
|
|
||||||
height = Math.max(height, y);
|
|
||||||
x += columnWidths[col] + gutter.width;
|
|
||||||
}
|
|
||||||
height += gutter.height;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
graphics.translate(-preferredLocation.x, -preferredLocation.y);
|
graphics.translate(-preferredLocation.x, -preferredLocation.y);
|
||||||
|
|
||||||
final Dimension dimension = new Dimension(preferredSize.width, height);
|
final Dimension dimension = new Dimension(preferredSize.width, height);
|
||||||
bounds.setLocation(preferredLocation);
|
bounds.setLocation(preferredLocation);
|
||||||
bounds.setSize(dimension);
|
bounds.setSize(dimension);
|
||||||
|
|
||||||
return dimension;
|
return dimension;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureColumnSize(final int size)
|
private int displayRow(Graphics2D graphics, TableRow row, int height, int[] columnWidths, FontMetrics metrics)
|
||||||
{
|
{
|
||||||
while (size > columns.size())
|
int x = 0;
|
||||||
|
int startingRowHeight = height;
|
||||||
|
|
||||||
|
final List<TableElement> elements = row.getElements();
|
||||||
|
for (int i = 0; i < elements.size(); i++)
|
||||||
{
|
{
|
||||||
columns.add(TableElement.builder().build());
|
int y = startingRowHeight;
|
||||||
}
|
final TableElement cell = elements.get(i);
|
||||||
}
|
|
||||||
|
|
||||||
public void setColumnColor(final int col, final Color color)
|
final String[] lines = lineBreakText(cell.getContent(), columnWidths[i], metrics);
|
||||||
{
|
final TableAlignment alignment = getCellAlignment(row, i);
|
||||||
assert columns.size() > col;
|
final Color color = getCellColor(row, i);
|
||||||
columns.get(col).setColor(color);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setColumnColors(@Nonnull final Color... colors)
|
for (String line : lines)
|
||||||
{
|
{
|
||||||
ensureColumnSize(colors.length);
|
final int alignmentOffset = getAlignedPosition(line, alignment, columnWidths[i], metrics);
|
||||||
for (int i = 0; i < colors.length; i++)
|
final TextComponent leftLineComponent = new TextComponent();
|
||||||
{
|
y += metrics.getHeight();
|
||||||
setColumnColor(i, colors[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setColumnAlignment(final int col, final TableAlignment alignment)
|
leftLineComponent.setPosition(new Point(x + alignmentOffset, y));
|
||||||
{
|
leftLineComponent.setText(line);
|
||||||
assert columns.size() > col;
|
leftLineComponent.setColor(color);
|
||||||
columns.get(col).setAlignment(alignment);
|
leftLineComponent.render(graphics);
|
||||||
}
|
}
|
||||||
|
height = Math.max(height, y);
|
||||||
public void setColumnAlignments(@Nonnull final TableAlignment... alignments)
|
x += columnWidths[i] + gutter.width;
|
||||||
{
|
|
||||||
ensureColumnSize(alignments.length);
|
|
||||||
for (int i = 0; i < alignments.length; i++)
|
|
||||||
{
|
|
||||||
setColumnAlignment(i, alignments[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addRow(@Nonnull final String... cells)
|
|
||||||
{
|
|
||||||
ensureColumnSize(cells.length);
|
|
||||||
|
|
||||||
final TableElement[] elements = new TableElement[cells.length];
|
|
||||||
for (int i = 0; i < cells.length; i++)
|
|
||||||
{
|
|
||||||
elements[i] = TableElement.builder().content(cells[i]).build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final TableRow row = TableRow.builder().build();
|
return height + gutter.height;
|
||||||
row.setElements(elements);
|
|
||||||
|
|
||||||
this.rows.add(row);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addRows(@Nonnull final String[]... rows)
|
/**
|
||||||
|
* Returns the width that each column should take up
|
||||||
|
* Based on https://stackoverflow.com/questions/22206825/algorithm-for-calculating-variable-column-widths-for-set-table-width
|
||||||
|
* @param metrics
|
||||||
|
* @return int[] of column width
|
||||||
|
*/
|
||||||
|
private int[] getColumnWidths(final FontMetrics metrics, final TableRow columnRow)
|
||||||
{
|
{
|
||||||
for (String[] row : rows)
|
int numCols = columns.size();
|
||||||
|
for (final TableRow r : rows)
|
||||||
{
|
{
|
||||||
addRow(row);
|
numCols = Math.max(r.getElements().size(), numCols);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addColumn(@Nonnull TableElement element)
|
|
||||||
{
|
|
||||||
this.columns.add(element);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setColumns(@Nonnull final TableElement... elements)
|
|
||||||
{
|
|
||||||
this.columns.clear();
|
|
||||||
this.columns.addAll(Arrays.asList(elements));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addColumn(@Nonnull final String col)
|
|
||||||
{
|
|
||||||
this.columns.add(TableElement.builder().content(col).build());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setColumns(@Nonnull final String... columns)
|
|
||||||
{
|
|
||||||
this.columns.clear();
|
|
||||||
for (String col : columns)
|
|
||||||
{
|
|
||||||
addColumn(col);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addRow(@Nonnull TableRow row)
|
|
||||||
{
|
|
||||||
this.rows.add(row);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addRows(@Nonnull final TableRow... rows)
|
|
||||||
{
|
|
||||||
for (TableRow row : rows)
|
|
||||||
{
|
|
||||||
addRow(row);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getCellText(final int col, final int row)
|
|
||||||
{
|
|
||||||
assert col < columns.size() && row < rows.size();
|
|
||||||
|
|
||||||
if (row == -1)
|
|
||||||
{
|
|
||||||
return columns.get(col).getContent();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TableElement[] elements = rows.get(row).getElements();
|
|
||||||
if (col >= elements.length)
|
|
||||||
{
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
final String result = elements[col].content;
|
|
||||||
return result != null ? result : "";
|
|
||||||
}
|
|
||||||
|
|
||||||
private int[] getColumnWidths(final FontMetrics metrics)
|
|
||||||
{
|
|
||||||
final int numRows = rows.size();
|
|
||||||
final int numCols = columns.size();
|
|
||||||
|
|
||||||
// Based on https://stackoverflow.com/questions/22206825/algorithm-for-calculating-variable-column-widths-for-set-table-width
|
|
||||||
int[] maxtextw = new int[numCols]; // max text width over all rows
|
int[] maxtextw = new int[numCols]; // max text width over all rows
|
||||||
int[] maxwordw = new int[numCols]; // max width of longest word
|
int[] maxwordw = new int[numCols]; // max width of longest word
|
||||||
boolean[] flex = new boolean[numCols]; // is column flexible?
|
boolean[] flex = new boolean[numCols]; // is column flexible?
|
||||||
boolean[] wrap = new boolean[numCols]; // can column be wrapped?
|
boolean[] wrap = new boolean[numCols]; // can column be wrapped?
|
||||||
int[] finalcolw = new int[numCols]; // final width of columns
|
int[] finalcolw = new int[numCols]; // final width of columns
|
||||||
|
|
||||||
for (int col = 0; col < numCols; col++)
|
final List<TableRow> rows = new ArrayList<>(this.rows);
|
||||||
|
rows.add(columnRow);
|
||||||
|
|
||||||
|
for (final TableRow r : rows)
|
||||||
{
|
{
|
||||||
for (int row = 0; row < numRows; row++)
|
final List<TableElement> elements = r.getElements();
|
||||||
|
for (int col = 0; col < elements.size(); col++)
|
||||||
{
|
{
|
||||||
final String cell = getCellText(col, row);
|
final TableElement ele = elements.get(col);
|
||||||
|
final String cell = ele.getContent();
|
||||||
final int cellWidth = getTextWidth(metrics, cell);
|
final int cellWidth = getTextWidth(metrics, cell);
|
||||||
|
|
||||||
maxtextw[col] = Math.max(maxtextw[col], cellWidth);
|
maxtextw[col] = Math.max(maxtextw[col], cellWidth);
|
||||||
@@ -397,43 +298,41 @@ public class TableComponent implements LayoutableRenderableEntity
|
|||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Color getCellColor(final int row, final int column)
|
/**
|
||||||
|
* Returns the color for the specified table element.
|
||||||
|
* Priority order: cell->row->column->default
|
||||||
|
* @param row TableRow element
|
||||||
|
* @param colIndex column index
|
||||||
|
*/
|
||||||
|
private Color getCellColor(final TableRow row, final int colIndex)
|
||||||
{
|
{
|
||||||
assert row < rows.size() && column < columns.size();
|
final List<TableElement> rowElements = row.getElements();
|
||||||
|
final TableElement cell = colIndex < rowElements.size() ? rowElements.get(colIndex) : EMPTY_ELEMENT;
|
||||||
|
final TableElement column = colIndex < columns.size() ? columns.get(colIndex) : EMPTY_ELEMENT;
|
||||||
|
|
||||||
// Row should be -1 for columns so use a empty TableRow
|
|
||||||
final TableRow rowEle = row != -1 ? rows.get(row) : EMPTY_ROW;
|
|
||||||
final TableElement columnElement = columns.get(column);
|
|
||||||
final TableElement[] elements = rowEle.getElements();
|
|
||||||
|
|
||||||
// Some rows may not have every element, even though they should..
|
|
||||||
final TableElement ele = column < elements.length ? elements[column] : EMPTY_ELEMENT;
|
|
||||||
|
|
||||||
// Color priorities goes as follow: cell->row->column->default
|
|
||||||
return firstNonNull(
|
return firstNonNull(
|
||||||
ele.getColor(),
|
cell.getColor(),
|
||||||
rowEle.getRowColor(),
|
row.getRowColor(),
|
||||||
columnElement.getColor(),
|
column.getColor(),
|
||||||
defaultColor);
|
defaultColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
private TableAlignment getCellAlignment(final int row, final int column)
|
/**
|
||||||
|
* Returns the alignment for the specified table element.
|
||||||
|
* Priority order: cell->row->column->default
|
||||||
|
* @param row TableRow element
|
||||||
|
* @param colIndex column index
|
||||||
|
*/
|
||||||
|
private TableAlignment getCellAlignment(final TableRow row, final int colIndex)
|
||||||
{
|
{
|
||||||
assert row < rows.size() && column < columns.size();
|
final List<TableElement> rowElements = row.getElements();
|
||||||
|
final TableElement cell = colIndex < rowElements.size() ? rowElements.get(colIndex) : EMPTY_ELEMENT;
|
||||||
|
final TableElement column = colIndex < columns.size() ? columns.get(colIndex) : EMPTY_ELEMENT;
|
||||||
|
|
||||||
// Row should be -1 for columns so use a empty TableRow
|
|
||||||
final TableRow rowEle = row != -1 ? rows.get(row) : EMPTY_ROW;
|
|
||||||
final TableElement columnElement = columns.get(column);
|
|
||||||
final TableElement[] elements = rowEle.getElements();
|
|
||||||
|
|
||||||
// Some rows may not have every element, even though they should..
|
|
||||||
final TableElement ele = column < elements.length ? elements[column] : EMPTY_ELEMENT;
|
|
||||||
|
|
||||||
// Alignment priorities goes as follow: cell->row->column->default
|
|
||||||
return firstNonNull(
|
return firstNonNull(
|
||||||
ele.getAlignment(),
|
cell.getAlignment(),
|
||||||
rowEle.getRowAlignment(),
|
row.getRowAlignment(),
|
||||||
columnElement.getAlignment(),
|
column.getAlignment(),
|
||||||
defaultAlignment);
|
defaultAlignment);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -455,4 +354,69 @@ public class TableComponent implements LayoutableRenderableEntity
|
|||||||
|
|
||||||
return cur;
|
return cur;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper functions for cleaner overlay code
|
||||||
|
public void addRow(@Nonnull final String... cells)
|
||||||
|
{
|
||||||
|
final List<TableElement> elements = new ArrayList<>();
|
||||||
|
for (final String cell : cells)
|
||||||
|
{
|
||||||
|
elements.add(TableElement.builder().content(cell).build());
|
||||||
|
}
|
||||||
|
|
||||||
|
final TableRow row = TableRow.builder().build();
|
||||||
|
row.setElements(elements);
|
||||||
|
|
||||||
|
this.rows.add(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addRows(@Nonnull final String[]... rows)
|
||||||
|
{
|
||||||
|
for (String[] row : rows)
|
||||||
|
{
|
||||||
|
addRow(row);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addRows(@NonNull final TableRow... rows)
|
||||||
|
{
|
||||||
|
this.rows.addAll(Arrays.asList(rows));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRows(@Nonnull final String[]... elements)
|
||||||
|
{
|
||||||
|
this.rows.clear();
|
||||||
|
addRows(elements);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRows(@Nonnull final TableRow... elements)
|
||||||
|
{
|
||||||
|
this.rows.clear();
|
||||||
|
this.rows.addAll(Arrays.asList(elements));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addColumn(@Nonnull final String col)
|
||||||
|
{
|
||||||
|
this.columns.add(TableElement.builder().content(col).build());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addColumns(@NonNull final TableElement... columns)
|
||||||
|
{
|
||||||
|
this.columns.addAll(Arrays.asList(columns));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setColumns(@Nonnull final TableElement... elements)
|
||||||
|
{
|
||||||
|
this.columns.clear();
|
||||||
|
this.columns.addAll(Arrays.asList(elements));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setColumns(@Nonnull final String... columns)
|
||||||
|
{
|
||||||
|
this.columns.clear();
|
||||||
|
for (String col : columns)
|
||||||
|
{
|
||||||
|
addColumn(col);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
package net.runelite.client.ui.overlay.components.table;
|
package net.runelite.client.ui.overlay.components.table;
|
||||||
|
|
||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
|
import java.util.List;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@@ -34,5 +35,5 @@ public class TableRow
|
|||||||
{
|
{
|
||||||
Color rowColor;
|
Color rowColor;
|
||||||
TableAlignment rowAlignment;
|
TableAlignment rowAlignment;
|
||||||
TableElement[] elements;
|
List<TableElement> elements;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ package net.runelite.client.ui.overlay.components.table;
|
|||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.FontMetrics;
|
import java.awt.FontMetrics;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
|
import java.util.List;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@@ -69,7 +70,11 @@ public class TableComponentTest
|
|||||||
{
|
{
|
||||||
TableComponent tableComponent = new TableComponent();
|
TableComponent tableComponent = new TableComponent();
|
||||||
tableComponent.addRow("test", "test", "test", "<col=ffff00>test", "test");
|
tableComponent.addRow("test", "test", "test", "<col=ffff00>test", "test");
|
||||||
tableComponent.setColumnColors(Color.RED, Color.GREEN, Color.BLUE);
|
tableComponent.setColumns("", "", "");
|
||||||
|
List<TableElement> elements = tableComponent.getColumns();
|
||||||
|
elements.get(0).setColor(Color.RED);
|
||||||
|
elements.get(1).setColor(Color.GREEN);
|
||||||
|
elements.get(2).setColor(Color.BLUE);
|
||||||
tableComponent.render(graphics);
|
tableComponent.render(graphics);
|
||||||
verify(graphics, atLeastOnce()).setColor(Color.RED);
|
verify(graphics, atLeastOnce()).setColor(Color.RED);
|
||||||
verify(graphics, atLeastOnce()).setColor(Color.GREEN);
|
verify(graphics, atLeastOnce()).setColor(Color.GREEN);
|
||||||
|
|||||||
Reference in New Issue
Block a user