19 package org.sleuthkit.autopsy.datasourcesummary.uiutils;
21 import java.awt.BorderLayout;
22 import java.awt.Graphics;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.function.Function;
26 import java.util.stream.Collectors;
27 import javax.swing.JComponent;
28 import javax.swing.JLayer;
29 import javax.swing.JScrollPane;
30 import javax.swing.JTable;
31 import javax.swing.plaf.LayerUI;
32 import javax.swing.table.DefaultTableColumnModel;
33 import javax.swing.table.TableColumn;
34 import javax.swing.table.TableColumnModel;
49 private static class Overlay extends LayerUI<JComponent> {
60 void setVisible(
boolean visible) {
69 void setMessage(String message) {
74 public void paint(Graphics g, JComponent c) {
76 overlayDelegate.
paintOverlay(g, c.getWidth(), c.getHeight());
139 private static final long serialVersionUID = 1L;
151 TableColumnModel
tableModel =
new DefaultTableColumnModel();
153 for (
int i = 0; i < columns.size(); i++) {
154 TableColumn col =
new TableColumn(i);
155 ColumnModel<T> model = columns.get(i);
158 if (model.getWidth() != null && model.getWidth() >= 0) {
159 col.setPreferredWidth(model.getWidth());
163 col.setHeaderValue(model.getHeaderTitle());
166 col.setCellRenderer(DEFAULT_CELL_RENDERER);
168 tableModel.addColumn(col);
183 List<Function<T, ? extends Object>> columnRenderers = columns.stream()
184 .map((colModel) -> colModel.getCellRenderer())
185 .collect(Collectors.toList());
208 private Function<T, ? extends Object>
keyFunction = (rowItem) -> rowItem;
236 if (tableModel == null) {
237 throw new IllegalArgumentException(
"Null table model passed to setModel");
241 table.setModel(tableModel);
249 return this.table.getColumnModel();
260 this.table.setColumnModel(columnModel);
282 if (keyFunction == null) {
283 throw new IllegalArgumentException(
"Key function must be non-null");
293 int prevSelectedRow = this.table.getSelectedRow();
295 T prevValue = (tableRows != null && prevSelectedRow >= 0 && prevSelectedRow < tableRows.size())
296 ? this.tableModel.
getDataRows().get(prevSelectedRow)
299 Object prevKeyValue = (prevValue == null) ? null : this.keyFunction.apply(prevValue);
303 List<T> dataToSet = (data == null) ? Collections.emptyList() : data;
309 if (prevKeyValue != null) {
310 for (
int objIndex = 0; objIndex < dataToSet.size(); objIndex++) {
311 Object thisKey = this.keyFunction.apply(dataToSet.get(objIndex));
312 if (prevKeyValue.equals(thisKey)) {
313 this.table.setRowSelectionInterval(objIndex, objIndex);
323 this.overlayLayer.setVisible(visible);
324 this.overlayLayer.setMessage(message);
331 table =
new JTable();
332 table.getTableHeader().setReorderingAllowed(
false);
334 overlayLayer =
new Overlay();
335 tableScrollPane =
new JScrollPane(table);
337 setLayout(
new BorderLayout());
338 add(dualLayer, BorderLayout.CENTER);
void paint(Graphics g, JComponent c)
JTablePanel< T > setColumnModel(TableColumnModel columnModel)
JScrollPane tableScrollPane
final BaseMessageOverlay overlayDelegate
Function< T,?extends Object > getKeyFunction()
void setMessage(boolean visible, String message)
ColumnModel(String headerTitle, Function< T, CellModelTableCellRenderer.CellModel > cellRenderer)
ColumnModel(String headerTitle, Function< T, CellModelTableCellRenderer.CellModel > cellRenderer, Integer width)
Function< T,?extends Object > keyFunction
synchronized void setResults(List< T > data)
void paintOverlay(Graphics g, int width, int height)
void setMessage(String message)
TableColumnModel getColumnModel()
static< T > JTablePanel< T > getJTablePanel(List< ColumnModel< T >> columns)
static< T > TableColumnModel getTableColumnModel(List< ColumnModel< T >> columns)
static< T > ListTableModel< T > getTableModel(List< ColumnModel< T >> columns)
void setDataRows(List< T > dataRows)
JTablePanel(ListTableModel< T > tableModel)
static final long serialVersionUID
final Function< T, CellModelTableCellRenderer.CellModel > cellRenderer
void setVisible(boolean visible)
final JTablePanel< T > setModel(ListTableModel< T > tableModel)
static final CellModelTableCellRenderer DEFAULT_CELL_RENDERER
ListTableModel< T > tableModel
Function< T, CellModel > getCellRenderer()
JTablePanel< T > setKeyFunction(Function< T,?extends Object > keyFunction)