Browse Source

Ensure boolean cell renderer uses custom color provided by cell renderers. Fixes #224

pull/229/head
weisj 4 years ago
parent
commit
b50dfa32c6
  1. 4
      core/src/main/java/com/github/weisj/darklaf/components/ArrowButton.java
  2. 29
      core/src/main/java/com/github/weisj/darklaf/ui/cell/ComponentBackedRenderer.java
  3. 28
      core/src/main/java/com/github/weisj/darklaf/ui/cell/ComponentBasedTableCellRenderer.java
  4. 27
      core/src/main/java/com/github/weisj/darklaf/ui/cell/ComponentBasedTreeCellRenderer.java
  5. 79
      core/src/main/java/com/github/weisj/darklaf/ui/cell/DarkBooleanCellRenderer.java
  6. 14
      core/src/main/java/com/github/weisj/darklaf/ui/cell/DarkCellRendererToggleButton.java
  7. 9
      core/src/main/java/com/github/weisj/darklaf/ui/table/DarkTableUI.java
  8. 2
      core/src/main/java/com/github/weisj/darklaf/ui/table/TableConstants.java
  9. 48
      core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkTableCellRendererDelegate.java
  10. 4
      core/src/main/java/com/github/weisj/darklaf/ui/tree/DarkDefaultTreeEditor.java
  11. 28
      core/src/main/java/com/github/weisj/darklaf/ui/tree/DarkTreeCellRendererDelegate.java

4
core/src/main/java/com/github/weisj/darklaf/components/ArrowButton.java

@ -27,7 +27,7 @@ import javax.swing.*;
import javax.swing.plaf.DimensionUIResource;
import javax.swing.plaf.basic.BasicArrowButton;
import com.github.weisj.darklaf.ui.button.DarkButtonUI;
import com.github.weisj.darklaf.ui.button.ButtonConstants;
/** @author Jannis Weis */
public final class ArrowButton implements SwingConstants {
@ -59,7 +59,7 @@ public final class ArrowButton implements SwingConstants {
return new BasicArrowButton(orientation, null, null, null, null) {
private final Insets ins = insets != null ? insets : new Insets(0, 0, 0, 0);
{
putClientProperty(DarkButtonUI.KEY_NO_BORDERLESS_OVERWRITE, true);
putClientProperty(ButtonConstants.KEY_NO_BORDERLESS_OVERWRITE, true);
setMargin(new Insets(0, 0, 0, 0));
setBorder(BorderFactory.createEmptyBorder());
}

29
core/src/main/java/com/github/weisj/darklaf/ui/cell/ComponentBackedRenderer.java

@ -0,0 +1,29 @@
/*
* MIT License
*
* Copyright (c) 2020 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.github.weisj.darklaf.ui.cell;
import javax.swing.*;
public interface ComponentBackedRenderer {
JComponent getRendererComponent();
}

28
core/src/main/java/com/github/weisj/darklaf/ui/cell/ComponentBasedTableCellRenderer.java

@ -0,0 +1,28 @@
/*
* MIT License
*
* Copyright (c) 2020 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.github.weisj.darklaf.ui.cell;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
public interface ComponentBasedTableCellRenderer extends TableCellRenderer, ComponentBackedRenderer {
}

27
core/src/main/java/com/github/weisj/darklaf/ui/cell/ComponentBasedTreeCellRenderer.java

@ -0,0 +1,27 @@
/*
* MIT License
*
* Copyright (c) 2020 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.github.weisj.darklaf.ui.cell;
import javax.swing.tree.TreeCellRenderer;
public interface ComponentBasedTreeCellRenderer extends TreeCellRenderer, ComponentBackedRenderer {
}

79
core/src/main/java/com/github/weisj/darklaf/ui/cell/DarkBooleanCellRenderer.java

@ -0,0 +1,79 @@
/*
* MIT License
*
* Copyright (c) 2020 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.github.weisj.darklaf.ui.cell;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.tree.TreeCellRenderer;
import com.github.weisj.darklaf.ui.table.TableConstants;
import com.github.weisj.darklaf.ui.tree.DarkTreeUI;
import com.github.weisj.darklaf.util.PropertyUtil;
public class DarkBooleanCellRenderer implements TableCellRenderer, TreeCellRenderer {
private final DarkCellRendererCheckBox checkBoxRenderer;
private final DarkCellRendererRadioButton radioRenderer;
public DarkBooleanCellRenderer(final boolean opaque) {
checkBoxRenderer = new DarkCellRendererCheckBox(opaque);
radioRenderer = new DarkCellRendererRadioButton(opaque);
}
@Override
public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected,
final boolean hasFocus, final int row, final int column) {
return getBooleanRenderer(table).getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
}
@Override
public Component getTreeCellRendererComponent(final JTree tree, final Object value, final boolean selected,
final boolean expanded,
final boolean leaf, final int row, final boolean hasFocus) {
return getBooleanRenderer(tree).getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row,
hasFocus);
}
protected ComponentBasedTableCellRenderer getBooleanRenderer(final JTable table) {
return getRenderer(PropertyUtil.isPropertyEqual(table,
TableConstants.KEY_BOOLEAN_RENDER_TYPE, CellConstants.RENDER_TYPE_RADIOBUTTON));
}
protected ComponentBasedTreeCellRenderer getBooleanRenderer(final JTree tree) {
return getRenderer(PropertyUtil.isPropertyEqual(tree,
DarkTreeUI.KEY_BOOLEAN_RENDER_TYPE, CellConstants.RENDER_TYPE_RADIOBUTTON));
}
private DarkCellRendererToggleButton<?> getRenderer(final boolean useRadioButton) {
return useRadioButton ? radioRenderer : checkBoxRenderer;
}
public JComponent getRendererComponent(final JTree tree) {
return getBooleanRenderer(tree).getRendererComponent();
}
public JComponent getRendererComponent(final JTable table) {
return getBooleanRenderer(table).getRendererComponent();
}
}

14
core/src/main/java/com/github/weisj/darklaf/ui/cell/DarkCellRendererToggleButton.java

@ -25,8 +25,6 @@ import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;
import javax.swing.tree.TreeCellRenderer;
import com.github.weisj.darklaf.components.tree.LabeledTreeNode;
import com.github.weisj.darklaf.components.tristate.TristateCheckBox;
@ -36,7 +34,7 @@ import com.github.weisj.darklaf.util.DarkUIUtil;
/** @author Jannis Weis */
public class DarkCellRendererToggleButton<T extends JToggleButton & CellToggleButton>
implements TableCellRenderer, TreeCellRenderer, SwingConstants {
implements ComponentBasedTreeCellRenderer, ComponentBasedTableCellRenderer, SwingConstants {
private final T toggleButton;
private final Icon checkIcon;
@ -111,6 +109,11 @@ public class DarkCellRendererToggleButton<T extends JToggleButton & CellToggleBu
return toggleButton;
}
@Override
public JComponent getRendererComponent() {
return getButton();
}
public static class CellCheckBox extends JCheckBox implements CellRenderer, CellToggleButton {
private boolean hasFocus;
@ -150,11 +153,6 @@ public class DarkCellRendererToggleButton<T extends JToggleButton & CellToggleBu
putClientProperty(ToggleButtonConstants.KEY_IS_TABLE_EDITOR, true);
}
@Override
public void setSelected(final boolean b) {
super.setSelected(b);
}
public void setHasFocus(final boolean hasFocus) {
this.hasFocus = hasFocus;
}

9
core/src/main/java/com/github/weisj/darklaf/ui/table/DarkTableUI.java

@ -37,6 +37,7 @@ import sun.swing.SwingUtilities2;
import com.github.weisj.darklaf.components.OverlayScrollPane;
import com.github.weisj.darklaf.graphics.PaintUtil;
import com.github.weisj.darklaf.ui.cell.CellUtil;
import com.github.weisj.darklaf.ui.cell.DarkBooleanCellRenderer;
import com.github.weisj.darklaf.ui.cell.DarkCellRendererPane;
import com.github.weisj.darklaf.ui.table.renderer.DarkColorTableCellRendererEditor;
import com.github.weisj.darklaf.ui.table.renderer.DarkTableCellEditorDelegate;
@ -56,11 +57,13 @@ public class DarkTableUI extends DarkTableUIBridge implements TableConstants {
protected Handler handler;
protected DarkTableCellRendererDelegate rendererDelegate;
private TableCellRenderer booleanCellRenderer;
public static ComponentUI createUI(final JComponent c) {
return new DarkTableUI();
}
@Override
protected boolean pointOutsidePrefSize(final int row, final int column, final Point p) {
return false;
}
@ -125,12 +128,14 @@ public class DarkTableUI extends DarkTableUIBridge implements TableConstants {
return new DarkCellRendererPane();
}
protected static void setupRendererComponents(final JTable table) {
protected void setupRendererComponents(final JTable table) {
booleanCellRenderer = new DarkBooleanCellRenderer(true);
TableCellRenderer cellRenderer = new DarkTableCellRenderer();
TableCellEditor cellEditor = new DarkTableCellEditorDelegate();
DarkColorTableCellRendererEditor colorRendererEditor = new DarkColorTableCellRendererEditor();
table.setDefaultRenderer(Object.class, cellRenderer);
table.setDefaultRenderer(Boolean.class, booleanCellRenderer);
table.setDefaultRenderer(Color.class, colorRendererEditor);
table.setDefaultEditor(Object.class, cellEditor);
@ -427,7 +432,7 @@ public class DarkTableUI extends DarkTableUIBridge implements TableConstants {
TableCellRenderer renderer = table.getCellRenderer(row, column);
if (renderer instanceof DarkTableCellRendererDelegate) return renderer;
if (rendererDelegate == null) {
rendererDelegate = new DarkTableCellRendererDelegate(renderer);
rendererDelegate = new DarkTableCellRendererDelegate(renderer, booleanCellRenderer);
}
rendererDelegate.setDelegate(renderer);
return rendererDelegate;

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

@ -45,7 +45,7 @@ public interface TableConstants extends CellConstants {
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);
return PropertyUtil.getBooleanProperty(table, TableConstants.KEY_RENDER_BOOLEAN_AS_CHECKBOX);
}
static boolean useBooleanEditorForValue(final Object value, final JTable table, final int column,

48
core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkTableCellRendererDelegate.java

@ -31,8 +31,6 @@ import javax.swing.table.TableColumn;
import com.github.weisj.darklaf.delegate.TableCellRendererDelegate;
import com.github.weisj.darklaf.ui.cell.CellUtil;
import com.github.weisj.darklaf.ui.cell.DarkCellRendererCheckBox;
import com.github.weisj.darklaf.ui.cell.DarkCellRendererRadioButton;
import com.github.weisj.darklaf.ui.table.DarkTableCellFocusBorder;
import com.github.weisj.darklaf.ui.table.DarkTableUI;
import com.github.weisj.darklaf.ui.table.TableConstants;
@ -41,19 +39,22 @@ import com.github.weisj.darklaf.util.PropertyUtil;
public class DarkTableCellRendererDelegate extends TableCellRendererDelegate implements TableConstants {
private final DarkCellRendererCheckBox checkBoxRenderer = new DarkCellRendererCheckBox();
private final DarkCellRendererRadioButton radioRenderer = new DarkCellRendererRadioButton();
private final TableCellRenderer booleanCellRenderer;
public DarkTableCellRendererDelegate(final TableCellRenderer renderer) {
public TableCellRenderer getBooleanCellRenderer() {
return booleanCellRenderer;
}
public DarkTableCellRendererDelegate(final TableCellRenderer renderer,
final TableCellRenderer booleanCellRenderer) {
super(renderer);
this.booleanCellRenderer = booleanCellRenderer;
}
@Override
public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected,
final boolean hasFocus, final int row, final int column) {
boolean booleanRenderer = TableConstants.useBooleanEditorForValue(value, table, column);
TableCellRenderer renderer = booleanRenderer ? getBooleanRenderer(table) : super.getDelegate();
boolean booleanRenderer = useBooleanRenderer(table, value, column);
boolean isRowFocus = DarkTableCellFocusBorder.isRowFocusBorder(table)
&& table.getSelectionModel().getLeadSelectionIndex() == row;
boolean rowLeadSelection = table.getSelectionModel().getLeadSelectionIndex() == row;
@ -65,8 +66,20 @@ public class DarkTableCellRendererDelegate extends TableCellRendererDelegate imp
boolean isLeadSelectionCell = DarkUIUtil.hasFocus(table) && rowLeadSelection && columnLeadSelection;
boolean paintSelected = isSelected && !isLeadSelectionCell;
Component component =
renderer.getTableCellRendererComponent(table, value, paintSelected, hasFocus, row, column);
Component delegateComp = getDelegate()
.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
Component component;
if (booleanRenderer) {
Component booleanComp = getBooleanRenderer()
.getTableCellRendererComponent(table, value, paintSelected, hasFocus, row, column);
booleanComp.setForeground(delegateComp.getForeground());
booleanComp.setBackground(delegateComp.getBackground());
booleanComp.setFont(delegateComp.getFont());
component = booleanComp;
} else {
component = delegateComp;
}
if (component instanceof JComponent) {
setupBorderStyle(table, row, column, (JComponent) component, isLeadSelectionCell, isRowFocus);
@ -76,6 +89,14 @@ public class DarkTableCellRendererDelegate extends TableCellRendererDelegate imp
return component;
}
public TableCellRenderer getBooleanRenderer() {
return booleanCellRenderer;
}
protected boolean useBooleanRenderer(final JTable table, final Object value, final int column) {
return TableConstants.useBooleanEditorForValue(value, table, column);
}
public void setupBorderStyle(final JTable table, final int row, final int column, final JComponent component,
final boolean isLeadSelectionCell, final boolean isRowFocus) {
Border focusBorder = UIManager.getBorder("Table.focusSelectedCellHighlightBorder");
@ -101,11 +122,4 @@ public class DarkTableCellRendererDelegate extends TableCellRendererDelegate imp
component.setBorder(UIManager.getBorder("Table.cellNoFocusBorder"));
}
}
protected TableCellRenderer getBooleanRenderer(final JTable table) {
if (PropertyUtil.isPropertyEqual(table, KEY_BOOLEAN_RENDER_TYPE, RENDER_TYPE_RADIOBUTTON)) {
return radioRenderer;
}
return checkBoxRenderer;
}
}

4
core/src/main/java/com/github/weisj/darklaf/ui/tree/DarkDefaultTreeEditor.java

@ -202,12 +202,12 @@ public class DarkDefaultTreeEditor extends DefaultTreeCellEditor {
protected JComponent getBooleanRendererComponent(final JTree tree) {
DarkTreeCellRendererDelegate rend = getRendererDelegate(tree);
if (rend == null) return null;
return rend.getBooleanRenderer(tree).getButton();
return rend.getBooleanRenderer().getRendererComponent(tree);
}
protected JComponent getTristateRendererComponent(final JTree tree) {
DarkTreeCellRendererDelegate rend = getRendererDelegate(tree);
if (rend == null) return null;
return rend.getTristateRenderer(tree).getButton();
return rend.getTristateRenderer().getRendererComponent();
}
}

28
core/src/main/java/com/github/weisj/darklaf/ui/tree/DarkTreeCellRendererDelegate.java

@ -30,15 +30,17 @@ import javax.swing.tree.TreeCellRenderer;
import com.github.weisj.darklaf.components.tristate.TristateState;
import com.github.weisj.darklaf.delegate.TreeCellRendererDelegate;
import com.github.weisj.darklaf.ui.cell.*;
import com.github.weisj.darklaf.ui.cell.CellUtil;
import com.github.weisj.darklaf.ui.cell.ComponentBasedTreeCellRenderer;
import com.github.weisj.darklaf.ui.cell.DarkBooleanCellRenderer;
import com.github.weisj.darklaf.ui.cell.DarkCellRendererTristateButton;
import com.github.weisj.darklaf.util.DarkUIUtil;
import com.github.weisj.darklaf.util.PropertyUtil;
public class DarkTreeCellRendererDelegate extends TreeCellRendererDelegate implements TreeRendererSupport {
private final DarkCellRendererCheckBox checkBoxRenderer = new DarkCellRendererCheckBox(false);
private final DarkCellRendererRadioButton radioRenderer = new DarkCellRendererRadioButton(false);
private final DarkCellRendererTristateButton tristateRenderer = new DarkCellRendererTristateButton(false);
private final DarkBooleanCellRenderer booleanRenderer = new DarkBooleanCellRenderer(false);
private final ComponentBasedTreeCellRenderer tristateRenderer = new DarkCellRendererTristateButton(false);
private final TreeRendererComponent rendererComponent = new TreeRendererComponent();
public DarkTreeCellRendererDelegate(final TreeCellRenderer renderer) {
@ -52,12 +54,12 @@ public class DarkTreeCellRendererDelegate extends TreeCellRendererDelegate imple
Object unwrapped = unwrapValue(value);
Component renderer;
if (unwrapped instanceof Boolean && isBooleanRenderingEnabled(tree)) {
Component comp = getBooleanRenderer(tree).getTreeCellRendererComponent(tree, value, selected, expanded,
leaf, row, isFocused);
Component comp = getBooleanRenderer()
.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, isFocused);
renderer = prepareRendererComponent(tree, comp);
} else if (unwrapped instanceof TristateState) {
Component comp = getTristateRenderer(tree).getTreeCellRendererComponent(tree, value, selected, expanded,
leaf, row, isFocused);
Component comp = getTristateRenderer()
.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, isFocused);
renderer = prepareRendererComponent(tree, comp);
} else {
TreeCellRenderer delegate = getDelegate();
@ -110,15 +112,11 @@ public class DarkTreeCellRendererDelegate extends TreeCellRendererDelegate imple
return PropertyUtil.getBooleanProperty(tree, DarkTreeUI.KEY_RENDER_BOOLEAN_AS_CHECKBOX);
}
protected DarkCellRendererToggleButton<?> getBooleanRenderer(final JTree tree) {
if (PropertyUtil.isPropertyEqual(tree, DarkTreeUI.KEY_BOOLEAN_RENDER_TYPE,
DarkTreeUI.RENDER_TYPE_RADIOBUTTON)) {
return radioRenderer;
}
return checkBoxRenderer;
protected DarkBooleanCellRenderer getBooleanRenderer() {
return booleanRenderer;
}
protected DarkCellRendererToggleButton<?> getTristateRenderer(final JTree tree) {
protected ComponentBasedTreeCellRenderer getTristateRenderer() {
return tristateRenderer;
}

Loading…
Cancel
Save