19 package org.sleuthkit.autopsy.datasourcesummary.uiutils;
21 import java.awt.BorderLayout;
22 import java.awt.Graphics;
23 import java.awt.event.MouseAdapter;
24 import java.awt.event.MouseEvent;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.function.Function;
28 import java.util.stream.Collectors;
29 import javax.swing.JComponent;
30 import javax.swing.JLayer;
31 import javax.swing.JScrollPane;
32 import javax.swing.JTable;
33 import javax.swing.plaf.LayerUI;
34 import javax.swing.table.DefaultTableColumnModel;
35 import javax.swing.table.TableColumn;
36 import javax.swing.table.TableColumnModel;
51 private final MouseEvent
e;
53 private final int row;
54 private final int col;
66 public CellMouseEvent(MouseEvent e, JTable table,
int row,
int col, Object cellValue) {
130 private static class Overlay extends LayerUI<JComponent> {
141 void setVisible(
boolean visible) {
150 void setMessage(String message) {
155 public void paint(Graphics g, JComponent c) {
157 overlayDelegate.
paintOverlay(g, c.getWidth(), c.getHeight());
220 private static final long serialVersionUID = 1L;
232 TableColumnModel
tableModel =
new DefaultTableColumnModel();
234 for (
int i = 0; i < columns.size(); i++) {
235 TableColumn col =
new TableColumn(i);
236 ColumnModel<T> model = columns.get(i);
239 if (model.getWidth() != null && model.getWidth() >= 0) {
240 col.setPreferredWidth(model.getWidth());
244 col.setHeaderValue(model.getHeaderTitle());
247 col.setCellRenderer(DEFAULT_CELL_RENDERER);
249 tableModel.addColumn(col);
264 List<Function<T, ? extends Object>> columnRenderers = columns.stream()
265 .map((colModel) -> colModel.getCellRenderer())
266 .collect(Collectors.toList());
293 private Function<T, ? extends Object>
keyFunction = (rowItem) -> rowItem;
310 this.table.addMouseListener(
new MouseAdapter() {
313 public void mouseClicked(MouseEvent e) {
315 if (cellListener != null) {
316 int row = table.rowAtPoint(e.getPoint());
317 int col = table.columnAtPoint(e.getPoint());
320 if (tableModel != null
321 && row >= 0 && row < tableModel.getRowCount()
322 && col >= 0 && col < tableModel.getColumnCount()) {
324 Object cellValue = tableModel.getValueAt(row, col);
341 if (tableModel == null) {
342 throw new IllegalArgumentException(
"Null table model passed to setModel");
346 table.setModel(tableModel);
374 return this.table.getColumnModel();
385 this.table.setColumnModel(columnModel);
406 if (keyFunction == null) {
407 throw new IllegalArgumentException(
"Key function must be non-null");
417 int prevSelectedRow = this.table.getSelectedRow();
419 T prevValue = (tableRows != null && prevSelectedRow >= 0 && prevSelectedRow < tableRows.size())
420 ? this.tableModel.
getDataRows().get(prevSelectedRow)
423 Object prevKeyValue = (prevValue == null) ? null : this.keyFunction.apply(prevValue);
427 List<T> dataToSet = (data == null) ? Collections.emptyList() : data;
433 if (prevKeyValue != null) {
434 for (
int objIndex = 0; objIndex < dataToSet.size(); objIndex++) {
435 Object thisKey = this.keyFunction.apply(dataToSet.get(objIndex));
436 if (prevKeyValue.equals(thisKey)) {
437 this.table.setRowSelectionInterval(objIndex, objIndex);
447 this.overlayLayer.setVisible(visible);
448 this.overlayLayer.setMessage(message);
455 table =
new JTable();
456 table.getTableHeader().setReorderingAllowed(
false);
457 overlayLayer =
new Overlay();
458 tableScrollPane =
new JScrollPane(table);
460 setLayout(
new BorderLayout());
461 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)
CellMouseListener getCellListener()
ColumnModel(String headerTitle, Function< T, CellModelTableCellRenderer.CellModel > cellRenderer, Integer width)
Function< T,?extends Object > keyFunction
synchronized void setResults(List< T > data)
JTablePanel< T > setCellListener(CellMouseListener cellListener)
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)
CellMouseListener cellListener
JTablePanel(ListTableModel< T > tableModel)
MouseEvent getMouseEvent()
static final long serialVersionUID
static CellMouseListener getMouseListener()
final Function< T, CellModelTableCellRenderer.CellModel > cellRenderer
void setVisible(boolean visible)
CellMouseEvent(MouseEvent e, JTable table, int row, int col, Object cellValue)
void mouseClicked(CellMouseEvent e)
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)