Skip to content

Commit

Permalink
chore: Improve interactive table
Browse files Browse the repository at this point in the history
  • Loading branch information
bric3 committed Mar 25, 2024
1 parent 20bb9be commit e5a5f3b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ public InteractiveJTable(InteractiveTableModel dm, TableColumnModel cm, ListSele
setSurrendersFocusOnKeystroke(true);

ProperMouseEnterExitListener.install(this).register(
new RowHoveringListener(this),
new AutoEditCellListener(this)
);
new RowHoveringListener(this),
new AutoEditCellListener(this)
);

adjustRowHeights();
}
Expand Down Expand Up @@ -256,8 +256,8 @@ private void prepareRowBackground(JComponent cellComponent, int row, String prep
});
} else {
var background = isStriped() ?
row % 2 == 1 ? getBackground() : getRowStripeBackground() :
getBackground();
row % 2 == 1 ? getBackground() : getRowStripeBackground() :
getBackground();
applyBackgroundToHierarchy(cellComponent, c -> {
if (background != c.getBackground()) {
c.setBackground(background);
Expand All @@ -275,9 +275,9 @@ public void editingCanceled(ChangeEvent e) {
private static void applyBackgroundToHierarchy(@NotNull JComponent container, Consumer<JComponent> backgroundApplier) {
container.setOpaque(true);
SwingUtils.descendantOrSelf(container)
.filter(JComponent.class::isInstance)
.map(JComponent.class::cast)
.forEach(backgroundApplier);
.filter(JComponent.class::isInstance)
.map(JComponent.class::cast)
.forEach(backgroundApplier);
}

/**
Expand Down Expand Up @@ -528,8 +528,8 @@ public boolean stopCellEditing() {
public boolean shouldSelectCell(final EventObject anEvent) {
var locationWithin = new Point(3, 3); // arbitrary ?
if (editorComponent != null
&& anEvent instanceof MouseEvent
&& ((MouseEvent) anEvent).getID() == MouseEvent.MOUSE_PRESSED
&& anEvent instanceof MouseEvent
&& ((MouseEvent) anEvent).getID() == MouseEvent.MOUSE_PRESSED
) {
MouseEvent originalMouseEvent = (MouseEvent) anEvent;
Component dispatchComponent =
Expand Down Expand Up @@ -664,7 +664,7 @@ private static class RowHoveringListener implements ProperMouseEnterExitListener
public RowHoveringListener(InteractiveJTable interactiveTable) {
this.table = interactiveTable;
}

@Override
public void mouseMoveWithin(MouseEvent e) {
performHovering(() -> table.rowAtPoint(e.getPoint()));
Expand Down Expand Up @@ -722,14 +722,14 @@ public void mouseMoveWithin(MouseEvent e) {

var currentCellEditor = table.getCellEditor();
if (table.isCellEditable(r, c)
&& (table.getEditingRow() != r || table.getEditingColumn() != c) // avoid flickering, when the mouse move over the same cell
&& (table.getEditingRow() != r || table.getEditingColumn() != c) // avoid flickering, when the mouse move over the same cell
) {
// Cancel previous, otherwise editCellAt will invoke stopCellEditing which
// actually get the current value from the editor and set it to the model (see editingStopped)
if (table.isEditing() && r >= 0 && c >= 0) {
currentCellEditor.cancelCellEditing();
}

table.editCellAt(r, c);
var newCellEditorComponent = table.getEditorComponent();
var newCellEditor = table.getCellEditor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
import java.lang.reflect.Method;

class JTableHacker {
static {
if (!JTable.class.getModule().isOpen(JTable.class.getPackageName(), ClassLoader.getSystemClassLoader().getUnnamedModule())) {
throw new IllegalStateException("Needs JVM option \"--add-opens java.desktop/javax.swing=ALL-UNNAMED\"");
}
}

private static final Method getRowModelMethod;

static {
Expand Down Expand Up @@ -52,9 +58,9 @@ SizeSequence getRowModel() {

void sortManager_setViewRowHeight(int row, int rowHeight) {
try {
Object sortManager = sortManagerField.get(table);
var sortManager = sortManagerField.get(table);
if (sortManager != null) {
Method setViewRowHeight = sortManager.getClass().getDeclaredMethod("setViewRowHeight", int.class, int.class);
var setViewRowHeight = sortManager.getClass().getDeclaredMethod("setViewRowHeight", int.class, int.class);
setViewRowHeight.setAccessible(true);
setViewRowHeight.invoke(sortManager, row, rowHeight);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.bric3.fireplace.ui.table;

import com.formdev.flatlaf.FlatLightLaf;
import io.github.bric3.fireplace.core.ui.Colors;
import io.github.bric3.fireplace.ui.table.InteractiveJTable.InteractiveTableCellEditor;
import io.github.bric3.fireplace.ui.table.InteractiveJTable.InteractiveTableCellRenderer;

Expand All @@ -15,6 +17,10 @@
public class InteractiveTableDemo extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
FlatLightLaf.setup();
Colors.setDarkMode(false);
//UIManager.put("InteractiveJTable.rowHoverBackground", new LightDarkColor(0xE5EBF3, 0x4E5357));
//UIManager.put("InteractiveJTable.rowStripeBackground", new LightDarkColor(0xE6E9EE, 0x34383A));
var interactiveTableLayout = new DefaultInteractiveTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
Expand All @@ -26,8 +32,9 @@ public boolean isCellEditable(int row, int column) {
// create data and put into table, first column is a string,
// the second is derived from a JPanel.

interactiveTableLayout.addRow("Chart 1", new int[] { 20, 100}); // new ChartPanel("block1", 20, 100, 20)
interactiveTableLayout.addRow("Chart 2", new int[] { 40, 150}); // new ChartPanel("block2", 40, 150, 40)
interactiveTableLayout.addRow("Chart 1", new int[] { 20, 100});
interactiveTableLayout.addRow("Chart 2", new int[] { 40, 150});
// TODO fix JTextArea
// interactiveTableLayout.addRow(
// "Text",
// "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " +
Expand All @@ -52,6 +59,7 @@ public boolean isCellEditable(int row, int column) {
JTextArea jTextArea = new JTextArea();
jTextArea.setLineWrap(true);
jTextArea.setEditable(false);
jTextArea.setWrapStyleWord(true);
jTextArea.setForeground(Color.BLACK);
myTable.setDefaultRenderer(
String.class,
Expand Down Expand Up @@ -217,7 +225,6 @@ static class ExpandablePanel extends JPanel {
}

public void setValue(boolean[] v) {
// setPreferredSize(new Dimension(getPreferredSize().width, v[0] ? 100 : 20));
live.setSelected(v[0]);
condition.setSelected(v[1]);
disable.setSelected(v[2]);
Expand Down

0 comments on commit e5a5f3b

Please sign in to comment.