Browse Source

Fix color renderer/editor for tables.

pull/222/head
weisj 4 years ago
parent
commit
b1ca895e55
  1. 2
      core/src/main/java/com/github/weisj/darklaf/ui/cell/CellUtil.java
  2. 1
      core/src/main/java/com/github/weisj/darklaf/ui/table/TableConstants.java
  3. 14
      core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkColorTableCellRendererEditor.java
  4. 14
      core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkMultiCellEditor.java
  5. 13
      core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkTableCellRenderer.java
  6. 21
      utils/src/main/java/com/github/weisj/darklaf/util/ColorUtil.java

2
core/src/main/java/com/github/weisj/darklaf/ui/cell/CellUtil.java

@ -326,7 +326,7 @@ public class CellUtil {
if (comp instanceof DefaultTreeCellRenderer) {
Color c = comp.getBackground();
if (ColorUtil.canOverwriteColor(c)) {
comp.setBackground(ColorUtil.stripUIResource(bg));
comp.setBackground(ColorUtil.stripUIResource(bg, true));
}
}
}

1
core/src/main/java/com/github/weisj/darklaf/ui/table/TableConstants.java

@ -42,6 +42,7 @@ public interface TableConstants extends CellConstants {
String KEY_VERTICAL_LINES = "showVerticalLines";
String KEY_IS_FILE_LIST = "Table.isFileList";
String KEY_IS_PRINT_MODE = "Table.printMode";
String KEY_CELL_VALUE_DETERMINES_EDITOR_CLASS = "Table.cellDeterminesClass";
static boolean isBooleanRenderingEnabled(final JTable table) {
return PropertyUtil.getBooleanProperty(table, DarkTableUI.KEY_RENDER_BOOLEAN_AS_CHECKBOX);

14
core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkColorTableCellRendererEditor.java

@ -29,15 +29,17 @@ import javax.swing.*;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import com.github.weisj.darklaf.util.ColorUtil;
/** @author Jannis Weis */
public class DarkColorTableCellRendererEditor extends AbstractCellEditor implements TableCellRenderer, TableCellEditor {
private final JLabel label;
private final JComponent colorComp;
private Color savedColor;
public DarkColorTableCellRendererEditor() {
label = new JLabel();
label.setOpaque(true);
colorComp = new JPanel();
colorComp.setOpaque(true);
}
@Override
@ -57,14 +59,14 @@ public class DarkColorTableCellRendererEditor extends AbstractCellEditor impleme
changeColor(color);
}
});
return label;
return colorComp;
}
}
private void changeColor(final Color color) {
if (color != null) {
savedColor = color;
label.setBackground(color);
colorComp.setBackground(ColorUtil.stripUIResource(color, false));
}
}
@ -85,6 +87,6 @@ public class DarkColorTableCellRendererEditor extends AbstractCellEditor impleme
public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected,
final boolean hasFocus, final int row, final int column) {
changeColor((Color) value);
return label;
return colorComp;
}
}

14
core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkMultiCellEditor.java

@ -28,14 +28,18 @@ import javax.swing.*;
import javax.swing.table.TableCellEditor;
import com.github.weisj.darklaf.delegate.TableCellEditorDelegate;
import com.github.weisj.darklaf.ui.table.TableConstants;
import com.github.weisj.darklaf.util.LazyValue;
import com.github.weisj.darklaf.util.PropertyUtil;
public class DarkMultiCellEditor extends TableCellEditorDelegate {
private final LazyValue<DarkTableCellEditor> numberEditor =
private final LazyValue<TableCellEditor> numberEditor =
new LazyValue<>(() -> new DarkTableCellEditor(new JSpinner()));
private final LazyValue<DarkTableCellEditor> dateEditor =
private final LazyValue<TableCellEditor> dateEditor =
new LazyValue<>(() -> new DarkTableCellEditor(new JSpinner(new SpinnerDateModel())));
private final LazyValue<TableCellEditor> colorEditor =
new LazyValue<>(DarkColorTableCellRendererEditor::new);
private TableCellEditor currentEditor;
public DarkMultiCellEditor() {
@ -51,11 +55,17 @@ public class DarkMultiCellEditor extends TableCellEditorDelegate {
private TableCellEditor getEditor(final JTable table, final Object value, final int column) {
Class<?> columnClass = table.getColumnClass(column);
if (value != null
&& PropertyUtil.getBooleanProperty(table, TableConstants.KEY_CELL_VALUE_DETERMINES_EDITOR_CLASS)) {
columnClass = value.getClass();
}
if (columnClass != null && columnClass.isInstance(value)) {
if (Number.class.isAssignableFrom(columnClass)) {
return numberEditor.get();
} else if (Date.class.isAssignableFrom(columnClass)) {
return dateEditor.get();
} else if (Color.class.isAssignableFrom(columnClass)) {
return colorEditor.get();
}
}
return null;

13
core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkTableCellRenderer.java

@ -26,13 +26,24 @@ import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import com.github.weisj.darklaf.util.ColorUtil;
/** @author Jannis Weis */
public class DarkTableCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected,
final boolean hasFocus, final int row, final int column) {
Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setBackground(null);
Object effectiveValue = value;
if (value instanceof Color) {
effectiveValue = "";
}
Component component =
super.getTableCellRendererComponent(table, effectiveValue, isSelected, hasFocus, row, column);
if (value instanceof Color) {
component.setBackground(ColorUtil.stripUIResource((Color) value, false));
}
int horizontalAlignment =
table.getComponentOrientation().isLeftToRight() ? SwingConstants.LEFT : SwingConstants.RIGHT;

21
utils/src/main/java/com/github/weisj/darklaf/util/ColorUtil.java

@ -89,11 +89,19 @@ public final class ColorUtil {
}
public static boolean canOverwriteColor(final Color c) {
return c == null || c instanceof UIResource || c instanceof NonUIResourceColorWrapper;
return c == null || c instanceof UIResource || c instanceof NonUIResourceOverwritableColorWrapper;
}
public static Color stripUIResource(final Color c) {
return c instanceof UIResource ? new NonUIResourceColorWrapper(c) : c;
public static Color stripUIResource(final Color c, final boolean canOverwrite) {
if (c instanceof UIResource) {
if (canOverwrite) {
return new NonUIResourceOverwritableColorWrapper(c);
} else {
return new NonUIResourceColorWrapper(c);
}
} else {
return c;
}
}
public static Color removeAlpha(final Color color) {
@ -131,4 +139,11 @@ public final class ColorUtil {
super(color);
}
}
public static class NonUIResourceOverwritableColorWrapper extends NonUIResourceColorWrapper {
public NonUIResourceOverwritableColorWrapper(final Color color) {
super(color);
}
}
}

Loading…
Cancel
Save