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;
52 private final MouseEvent
e;
54 private final int row;
55 private final int col;
67 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());
161 private static final long serialVersionUID = 1L;
173 TableColumnModel
tableModel =
new DefaultTableColumnModel();
175 for (
int i = 0; i < columns.size(); i++) {
176 TableColumn col =
new TableColumn(i);
181 col.setPreferredWidth(model.
getWidth());
188 col.setCellRenderer(DEFAULT_CELL_RENDERER);
190 tableModel.addColumn(col);
205 List<Function<T, ? extends Object>> columnRenderers = columns.stream()
206 .map((colModel) -> colModel.getCellRenderer())
207 .collect(Collectors.toList());
234 private Function<T, ? extends Object>
keyFunction = (rowItem) -> rowItem;
252 this.table.addMouseListener(
new MouseAdapter() {
255 public void mouseClicked(MouseEvent e) {
257 if (cellListener != null) {
258 int row = table.rowAtPoint(e.getPoint());
259 int col = table.columnAtPoint(e.getPoint());
262 if (tableModel != null
263 && row >= 0 && row < tableModel.getRowCount()
264 && col >= 0 && col < tableModel.getColumnCount()) {
266 Object cellValue = tableModel.getValueAt(row, col);
272 table.setGridColor(javax.swing.UIManager.getDefaults().getColor(
"InternalFrame.borderColor"));
284 if (tableModel == null) {
285 throw new IllegalArgumentException(
"Null table model passed to setModel");
289 table.setModel(tableModel);
318 return this.table.getColumnModel();
329 this.table.setColumnModel(columnModel);
351 if (keyFunction == null) {
352 throw new IllegalArgumentException(
"Key function must be non-null");
365 int selectedRow = this.table.getSelectedRow();
366 int count = this.table.getSelectedRowCount();
367 if (selectedRow < 0 || this.tableModel == null || selectedRow + count > this.tableModel.
getDataRows().size()) {
370 return this.tableModel.
getDataRows().subList(selectedRow, selectedRow + count);
377 int prevSelectedRow = this.table.getSelectedRow();
379 T prevValue = (tableRows != null && prevSelectedRow >= 0 && prevSelectedRow < tableRows.size())
380 ? this.tableModel.
getDataRows().get(prevSelectedRow)
383 Object prevKeyValue = (prevValue == null) ? null : this.keyFunction.apply(prevValue);
387 List<T> dataToSet = (data == null) ? Collections.emptyList() : data;
393 if (prevKeyValue != null) {
394 for (
int objIndex = 0; objIndex < dataToSet.size(); objIndex++) {
395 Object thisKey = this.keyFunction.apply(dataToSet.get(objIndex));
396 if (prevKeyValue.equals(thisKey)) {
397 this.table.setRowSelectionInterval(objIndex, objIndex);
407 this.overlayLayer.setVisible(visible);
408 this.overlayLayer.setMessage(message);
415 table =
new JTable();
416 table.getTableHeader().setReorderingAllowed(
false);
417 overlayLayer =
new Overlay();
418 tableScrollPane =
new JScrollPane(table);
420 setLayout(
new BorderLayout());
421 add(dualLayer, BorderLayout.CENTER);
void paint(Graphics g, JComponent c)
JTablePanel< T > setColumnModel(TableColumnModel columnModel)
JScrollPane tableScrollPane
static< T, CextendsGuiCellModel > TableColumnModel getTableColumnModel(List< ColumnModel< T, C >> columns)
final BaseMessageOverlay overlayDelegate
Function< T,?extends Object > getKeyFunction()
void setMessage(boolean visible, String message)
CellMouseListener getCellListener()
static< T, CextendsGuiCellModel > ListTableModel< T > getTableModel(List< ColumnModel< T, C >> columns)
List< T > getSelectedItems()
static< T, CextendsGuiCellModel > JTablePanel< T > getJTablePanel(List< ColumnModel< T, C >> columns)
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()
void setDataRows(List< T > dataRows)
CellMouseListener cellListener
JTablePanel(ListTableModel< T > tableModel)
MouseEvent getMouseEvent()
static final int EXTRA_ROW_HEIGHT
static final long serialVersionUID
static CellMouseListener getMouseListener()
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
JTablePanel< T > setKeyFunction(Function< T,?extends Object > keyFunction)