mirror of https://github.com/weisJ/darklaf.git
weisj
5 years ago
11 changed files with 357 additions and 35 deletions
@ -0,0 +1,4 @@
|
||||
package com.weis.darklaf.decorators; |
||||
|
||||
public interface CellRenderer { |
||||
} |
@ -0,0 +1,92 @@
|
||||
package com.weis.darklaf.ui.table; |
||||
|
||||
import org.jetbrains.annotations.NotNull; |
||||
|
||||
import javax.swing.*; |
||||
import java.awt.*; |
||||
import java.util.EventObject; |
||||
|
||||
/** |
||||
* @author vincencopalazzo |
||||
* @author atarw |
||||
*/ |
||||
public class DarkTableCellEditor extends DefaultCellEditor { |
||||
|
||||
private final DarkTableCellEditorCheckBox booleanEditor = new DarkTableCellEditorCheckBox(this); |
||||
private boolean value; |
||||
private boolean isBooleanEditor; |
||||
|
||||
public DarkTableCellEditor() { |
||||
this(new JTextField()); |
||||
} |
||||
|
||||
public DarkTableCellEditor(final JComboBox<?> comboBox) { |
||||
super(comboBox); |
||||
} |
||||
|
||||
public DarkTableCellEditor(final JCheckBox checkBox) { |
||||
super(checkBox); |
||||
} |
||||
|
||||
public DarkTableCellEditor(final JTextField textField) { |
||||
super(textField); |
||||
textField.setBorder(new DarkTableCellBorder()); |
||||
} |
||||
|
||||
protected void setValue(final Object value) { |
||||
delegate.setValue(value); |
||||
if (value instanceof Boolean) { |
||||
this.value = (boolean) value; |
||||
} else { |
||||
isBooleanEditor = false; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public Component getTableCellEditorComponent(final JTable table, final Object value, |
||||
final boolean isSelected, final int row, final int column) { |
||||
|
||||
if (value instanceof Boolean && DarkTableCellRenderer.isBooleanRenderingEnabled(table)) { |
||||
isBooleanEditor = true; |
||||
return booleanEditor.getTableCellEditorComponent(table, value, isSelected, row, column); |
||||
} else { |
||||
isBooleanEditor = false; |
||||
} |
||||
|
||||
super.getTableCellEditorComponent(table, value, isSelected, row, column); |
||||
boolean alternativeRow = UIManager.getBoolean("Table.alternateRowColor"); |
||||
Color alternativeRowColor = UIManager.getColor("Table.alternateRowBackground"); |
||||
Color normalColor = UIManager.getColor("Table.background"); |
||||
if (alternativeRow) { |
||||
if (!isSelected) { |
||||
if (row % 2 == 1) { |
||||
editorComponent.setBackground(alternativeRowColor); |
||||
} else { |
||||
editorComponent.setBackground(normalColor); |
||||
} |
||||
} |
||||
} |
||||
return editorComponent; |
||||
} |
||||
|
||||
@Override |
||||
public Object getCellEditorValue() { |
||||
if (isBooleanEditor) { |
||||
return value; |
||||
} else { |
||||
return super.getCellEditorValue(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public boolean isCellEditable(@NotNull final EventObject anEvent) { |
||||
var table = ((JTable) anEvent.getSource()); |
||||
if (DarkTableCellRenderer.isBooleanRenderingEnabled(table)) { |
||||
var p = MouseInfo.getPointerInfo().getLocation(); |
||||
SwingUtilities.convertPointFromScreen(p, table); |
||||
var value = table.getValueAt(table.rowAtPoint(p), table.columnAtPoint(p)); |
||||
if (value instanceof Boolean) return true; |
||||
} |
||||
return super.isCellEditable(anEvent); |
||||
} |
||||
} |
@ -0,0 +1,77 @@
|
||||
package com.weis.darklaf.ui.table; |
||||
|
||||
import com.weis.darklaf.decorators.CellRenderer; |
||||
|
||||
import javax.swing.*; |
||||
import javax.swing.table.TableCellEditor; |
||||
import java.awt.*; |
||||
import java.util.EventObject; |
||||
|
||||
/** |
||||
* @author vincencopalazzo |
||||
* @author atarw |
||||
*/ |
||||
public class DarkTableCellEditorCheckBox extends AbstractCellEditor implements TableCellEditor, SwingConstants { |
||||
|
||||
private final JCheckBox checkBox; |
||||
|
||||
public DarkTableCellEditorCheckBox(final DarkTableCellEditor delegate) { |
||||
checkBox = new CellEditorCheckBox(); |
||||
checkBox.addChangeListener(e -> delegate.setValue(checkBox.isSelected())); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public Component getTableCellEditorComponent(final JTable table, final Object value, |
||||
final boolean isSelected, final int row, final int column) { |
||||
if (value instanceof Boolean) { |
||||
checkBox.setSelected((Boolean) value); |
||||
} |
||||
checkBox.setHorizontalAlignment(table.getComponentOrientation().isLeftToRight() ? LEFT : RIGHT); |
||||
|
||||
boolean alternativeRow = UIManager.getBoolean("Table.alternateRowColor"); |
||||
Color alternativeRowColor = UIManager.getColor("Table.alternateRowBackground"); |
||||
Color normalColor = UIManager.getColor("Table.background"); |
||||
if (alternativeRow) { |
||||
if (!isSelected) { |
||||
if (row % 2 == 1) { |
||||
checkBox.setBackground(alternativeRowColor); |
||||
} else { |
||||
checkBox.setBackground(normalColor); |
||||
} |
||||
checkBox.setForeground(table.getForeground()); |
||||
} else { |
||||
checkBox.setForeground(table.getSelectionForeground()); |
||||
checkBox.setBackground(table.getSelectionBackground()); |
||||
} |
||||
} |
||||
return checkBox; |
||||
} |
||||
|
||||
@Override |
||||
public Object getCellEditorValue() { |
||||
return checkBox.isSelected(); |
||||
} |
||||
|
||||
@Override |
||||
public boolean isCellEditable(final EventObject anEvent) { |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public boolean shouldSelectCell(final EventObject anEvent) { |
||||
return false; |
||||
} |
||||
|
||||
private static class CellEditorCheckBox extends JCheckBox implements CellRenderer { |
||||
@Override |
||||
public boolean hasFocus() { |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isFocusOwner() { |
||||
return super.hasFocus(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,68 @@
|
||||
package com.weis.darklaf.ui.table; |
||||
|
||||
import org.jetbrains.annotations.NotNull; |
||||
|
||||
import javax.swing.*; |
||||
import javax.swing.table.DefaultTableCellRenderer; |
||||
import java.awt.*; |
||||
|
||||
/** |
||||
* @author vincencopalazzo |
||||
* @author atarw |
||||
*/ |
||||
public class DarkTableCellRenderer extends DefaultTableCellRenderer { |
||||
|
||||
private final DarkTableCellRendererCheckBox booleanRenderer = new DarkTableCellRendererCheckBox(); |
||||
|
||||
protected static boolean isBooleanRenderingEnabled(@NotNull final JTable table) { |
||||
return Boolean.TRUE.equals(table.getClientProperty("JTable.renderBooleanAsCheckBox")); |
||||
} |
||||
|
||||
@Override |
||||
public Component getTableCellRendererComponent(final JTable table, final Object value, |
||||
final boolean isSelected, final boolean hasFocus, final int row, |
||||
final int column) { |
||||
if (value instanceof Boolean && isBooleanRenderingEnabled(table)) { |
||||
return booleanRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); |
||||
} |
||||
|
||||
JComponent component = (JComponent) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, |
||||
row, column); |
||||
this.setVerticalAlignment(SwingConstants.CENTER); |
||||
setHorizontalAlignment(table.getComponentOrientation().isLeftToRight() ? LEFT : RIGHT); |
||||
|
||||
boolean alternativeRow = UIManager.getBoolean("Table.alternateRowColor"); |
||||
Color alternativeRowColor = UIManager.getColor("Table.alternateRowBackground"); |
||||
Color normalColor = UIManager.getColor("Table.background"); |
||||
if (alternativeRow) { |
||||
if (!isSelected) { |
||||
if (row % 2 == 1) { |
||||
component.setBackground(alternativeRowColor); |
||||
setDefaultCellRenderWithAllType(table, value, isSelected, hasFocus, row, column, |
||||
alternativeRowColor); |
||||
} else { |
||||
component.setBackground(normalColor); |
||||
setDefaultCellRenderWithAllType(table, value, isSelected, hasFocus, row, column, normalColor); |
||||
} |
||||
component.setForeground(table.getSelectionForeground()); |
||||
} |
||||
} |
||||
return component; |
||||
} |
||||
|
||||
// This method setting a MaterialCellRender at the particular class
|
||||
// With this class not working correctly the color alternate in the Jtable
|
||||
// in particular the IconImage without this code the cell is painted not correctly or
|
||||
// in the cell did print the path of the image
|
||||
protected void setDefaultCellRenderWithAllType(final JTable table, final Object value, final boolean isSelected, |
||||
final boolean hasFocus, final int row, final int column, |
||||
final Color color) { |
||||
if (table == null) { |
||||
throw new IllegalArgumentException("Table is null"); |
||||
} |
||||
|
||||
Component component = table.getDefaultRenderer(ImageIcon.class) |
||||
.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); |
||||
component.setBackground(color); |
||||
} |
||||
} |
@ -0,0 +1,62 @@
|
||||
package com.weis.darklaf.ui.table; |
||||
|
||||
import javax.swing.*; |
||||
import javax.swing.table.TableCellRenderer; |
||||
import java.awt.*; |
||||
|
||||
/** |
||||
* @author vincencopalazzo |
||||
* @author atarw |
||||
*/ |
||||
public class DarkTableCellRendererCheckBox extends JCheckBox implements TableCellRenderer, SwingConstants { |
||||
|
||||
private boolean hasFocus; |
||||
|
||||
public DarkTableCellRendererCheckBox() { |
||||
this(false); |
||||
} |
||||
|
||||
|
||||
public DarkTableCellRendererCheckBox(final boolean selected) { |
||||
setSelected(selected); |
||||
} |
||||
|
||||
@Override |
||||
public boolean hasFocus() { |
||||
return hasFocus || super.hasFocus(); |
||||
} |
||||
|
||||
@Override |
||||
public boolean isFocusOwner() { |
||||
return super.hasFocus(); |
||||
} |
||||
|
||||
@Override |
||||
public Component getTableCellRendererComponent(final JTable table, final Object value, |
||||
final boolean isSelected, final boolean focus, |
||||
final int row, final int column) { |
||||
if (value instanceof Boolean) { |
||||
setSelected((Boolean) value); |
||||
} |
||||
setHorizontalAlignment(table.getComponentOrientation().isLeftToRight() ? LEFT : RIGHT); |
||||
hasFocus = focus; |
||||
|
||||
boolean alternativeRow = UIManager.getBoolean("Table.alternateRowColor"); |
||||
Color alternativeRowColor = UIManager.getColor("Table.alternateRowBackground"); |
||||
Color normalColor = UIManager.getColor("Table.background"); |
||||
if (alternativeRow) { |
||||
if (!isSelected) { |
||||
if (row % 2 == 1) { |
||||
this.setBackground(alternativeRowColor); |
||||
} else { |
||||
this.setBackground(normalColor); |
||||
} |
||||
this.setForeground(table.getForeground()); |
||||
} else { |
||||
this.setForeground(table.getSelectionForeground()); |
||||
this.setBackground(table.getSelectionBackground()); |
||||
} |
||||
} |
||||
return this; |
||||
} |
||||
} |
Loading…
Reference in new issue