Skip to content

Commit

Permalink
Fix borders for full row focus mode in tables.
Browse files Browse the repository at this point in the history
Ensure empty file list has the correct background.
Ensure editing component doesn't get removed when in IconWrapper. Fixes #199.
  • Loading branch information
weisJ committed Aug 5, 2020
1 parent cb3e8c2 commit 5af510e
Show file tree
Hide file tree
Showing 22 changed files with 232 additions and 110 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.components.border;

import java.awt.*;

import javax.swing.border.Border;

public class BorderWrapper implements Border {

private final Border border;

public BorderWrapper(final Border border) {
this.border = border;
}

@Override
public void paintBorder(final Component c, final Graphics g, final int x, final int y, final int width,
final int height) {
border.paintBorder(c, g, x, y, width, height);
}

@Override
public Insets getBorderInsets(final Component c) {
return border.getBorderInsets(c);
}

@Override
public boolean isBorderOpaque() {
return border.isBorderOpaque();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicBorders;

public class MarginBorderWrapper extends CompoundBorder implements UIResource {
public class MarginBorderWrapper extends CompoundBorder {

public MarginBorderWrapper(final Border border) {
super(border, new BasicBorders.MarginBorder());
Expand All @@ -39,7 +39,15 @@ public MarginBorderWrapper(final Border border) {
public static void installBorder(final JComponent c) {
Border border = c.getBorder();
if (!(border instanceof MarginBorderWrapper)) {
c.setBorder(new MarginBorderWrapper(border));
c.setBorder(wrapBorder(border));
}
}

private static MarginBorderWrapper wrapBorder(final Border border) {
if (border instanceof UIResource) {
return new MarginBorderWrapper.UIBorder(border);
} else {
return new MarginBorderWrapper(border);
}
}

Expand All @@ -57,4 +65,11 @@ public static Border getBorder(final JComponent c) {
}
return border;
}

public static class UIBorder extends MarginBorderWrapper implements UIResource {

public UIBorder(final Border border) {
super(border);
}
}
}
24 changes: 19 additions & 5 deletions core/src/main/java/com/github/weisj/darklaf/ui/cell/CellUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.github.weisj.darklaf.graphics.ColorWrapper;
import com.github.weisj.darklaf.ui.list.DarkListUI;
import com.github.weisj.darklaf.ui.table.DarkTableUI;
import com.github.weisj.darklaf.ui.table.TableConstants;
import com.github.weisj.darklaf.ui.table.renderer.IconWrapper;
import com.github.weisj.darklaf.ui.tree.DarkTreeUI;
import com.github.weisj.darklaf.util.DarkUIUtil;
Expand Down Expand Up @@ -248,8 +249,13 @@ public static void updateColors(final UIDefaults defaults) {
listCellInactiveBackgroundSelectedNoFocus = d.getColor("List.inactiveBackgroundSelectedNoFocus");
}

public static void setupTableForeground(final Component comp, final JTable parent, final boolean selected) {
setupForeground(comp, parent, selected,
public static void setupTableForeground(final Component comp, final JTable parent, final boolean selected,
final int row) {
boolean sel = selected;
if (parent.getSelectionModel().getLeadSelectionIndex() == row) {
sel = !PropertyUtil.getBooleanProperty(parent, TableConstants.KEY_FULL_ROW_FOCUS_BORDER);
}
setupForeground(comp, parent, sel,
tableCellForeground, tableCellForegroundSelected,
tableCellForegroundNoFocus, tableCellForegroundSelectedNoFocus,
tableCellInactiveForeground, tableCellInactiveForegroundSelected,
Expand Down Expand Up @@ -305,7 +311,11 @@ public static void setupForeground(final Component comp, final JComponent parent
public static Color getTableBackground(final Component comp, final JTable parent, final boolean selected,
final int row) {
boolean alt = row % 2 == 1 && PropertyUtil.getBooleanProperty(parent, DarkTableUI.KEY_ALTERNATE_ROW_COLOR);
return getColor(comp, hasFocus(parent, comp), selected,
boolean sel = selected;
if (parent.getSelectionModel().getLeadSelectionIndex() == row) {
sel = !PropertyUtil.getBooleanProperty(parent, TableConstants.KEY_FULL_ROW_FOCUS_BORDER);
}
return getColor(comp, hasFocus(parent, comp), sel,
alt ? tableCellBackgroundAlternative : tableCellBackground,
tableCellBackgroundSelected,
alt ? tableCellBackgroundNoFocusAlternative : tableCellBackgroundNoFocus,
Expand Down Expand Up @@ -500,9 +510,13 @@ public static void paintTableEditorBorder(final Graphics g, final Component c, f
if (row > CellUtil.getMinRowIndex(table)) g.fillRect(0, 0, width, 1);
g.fillRect(0, height - 1, width, 1);
}
boolean isWrapper = isInWrapper(c);
ComponentOrientation orientation = table.getComponentOrientation();
if (!table.getShowVerticalLines()) {
if (col > CellUtil.getMinColumnIndex(table)) g.fillRect(0, 0, 1, height);
if (col < CellUtil.getMaxColumnIndex(table)) g.fillRect(width - 1, 0, 1, height);
if ((isWrapper && orientation.isLeftToRight())
|| col > CellUtil.getMinColumnIndex(table)) g.fillRect(0, 0, 1, height);
if ((isWrapper && orientation.isLeftToRight())
|| col < CellUtil.getMaxColumnIndex(table)) g.fillRect(width - 1, 0, 1, height);
} else if (isInWrapper(c)) {
if (table.getComponentOrientation().isLeftToRight()) {
g.fillRect(0, 0, 1, height);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.UIResource;

/**
* @author Jannis Weis
*/
public class DarkCellBorder extends EmptyBorder implements UIResource {
public class DarkCellBorder extends EmptyBorder {

public DarkCellBorder() {
this(UIManager.getInsets("Cell.borderInsets"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.plaf.UIResource;

/**
* @author Jannis Weis
*/
public class DarkCellBorderUIResource extends DarkCellBorder implements UIResource {

public DarkCellBorderUIResource() {}

public DarkCellBorderUIResource(final Insets insets) {
super(insets);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class DarkCellRendererToggleButton<T extends JToggleButton & CellEditorTo
implements TableCellRenderer, TreeCellRenderer, SwingConstants {

private final T toggleButton;
private final Border border = new DarkCellBorder();
private final Border border = new DarkCellBorderUIResource();

public DarkCellRendererToggleButton(final T toggleButton) {
this.toggleButton = toggleButton;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,30 @@

import javax.accessibility.AccessibleContext;
import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.event.TableModelEvent;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.text.JTextComponent;
import javax.swing.text.Position;

import sun.swing.SwingUtilities2;

import com.github.weisj.darklaf.components.OverlayScrollPane;
import com.github.weisj.darklaf.listener.AncestorAdapter;
import com.github.weisj.darklaf.ui.table.DarkTableUI;
import com.github.weisj.darklaf.ui.table.TextTableCellEditorBorder;
import com.github.weisj.darklaf.ui.table.renderer.DarkTableCellEditor;
import com.github.weisj.darklaf.ui.table.renderer.DarkTableCellEditorDelegate;
import com.github.weisj.darklaf.ui.text.DarkTextUI;
import com.github.weisj.darklaf.util.DarkUIUtil;

public class DarkFilePane extends DarkFilePaneUIBridge {

protected TableCellEditor tableCellEditor;

public DarkFilePane(final FileChooserUIAccessor fileChooserUIAccessor) {
super(fileChooserUIAccessor);
}
Expand All @@ -61,6 +69,8 @@ protected void installDefaults() {
megaByteString = UIManager.getString("FileChooser.fileSizeMegaBytes");
gigaByteString = UIManager.getString("FileChooser.fileSizeGigaBytes");
editCell = new JTextField();
editCell.setName("FileChooser.listEditCell");
editCell.addActionListener(new EditActionListener());
editCell.setBorder(new TextTableCellEditorBorder());
editCell.putClientProperty("JTextField.listCellEditor", true);
editCell.putClientProperty(DarkTextUI.KEY_IS_LIST_EDITOR, true);
Expand Down Expand Up @@ -88,6 +98,16 @@ public int getNextMatch(final String prefix, final int startIndex, final Positio
}
return -1;
}

@Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
// for (Component c = this; c != null; c = c.getParent()) {
// System.out.println(c);
// System.out.println(c.getBackground());
// System.out.println(c.isOpaque());
// }
}
};
list.setCellRenderer(new DarkFileRenderer());
list.setLayoutOrientation(JList.VERTICAL_WRAP);
Expand Down Expand Up @@ -136,6 +156,7 @@ public void contentsChanged(final ListDataEvent e) {
JScrollPane scrollPane = overlayScrollPane.getScrollPane();
if (listViewBackground != null) {
list.setBackground(listViewBackground);
p.setBackground(listViewBackground);
}
if (listViewBorder != null) {
scrollPane.setBorder(listViewBorder);
Expand Down Expand Up @@ -187,8 +208,9 @@ protected boolean processKeyBinding(final KeyStroke ks, final KeyEvent e,
detailsTable.setShowGrid(false);
detailsTable.putClientProperty("JTable.autoStartsEdit", Boolean.FALSE);
detailsTable.addKeyListener(detailsKeyListener);
detailsTable.putClientProperty("JTable.rowFocusBorder", true);
detailsTable.putClientProperty("JTable.fileChooserParent", (Supplier<JFileChooser>) this::getFileChooser);
detailsTable.putClientProperty(DarkTableUI.KEY_FULL_ROW_FOCUS_BORDER, true);
detailsTable.putClientProperty(DarkTableUI.KEY_FILE_CHOOSER_PARENT,
(Supplier<JFileChooser>) this::getFileChooser);
detailsTable.putClientProperty("JTable.fileNameColumnIndex", COLUMN_FILENAME);

Font font = list.getFont();
Expand Down Expand Up @@ -284,13 +306,6 @@ protected void editFileName(final int index) {
case VIEWTYPE_LIST :
editFile = (File) getModel().getElementAt(getRowSorter().convertRowIndexToModel(index));
Rectangle r = list.getCellBounds(index, index);
if (editCell == null) {
editCell = new JTextField();
editCell.setName("Tree.cellEditor");
editCell.addActionListener(new EditActionListener());
editCell.addFocusListener(editorFocusListener);
editCell.setNextFocusableComponent(list);
}
list.add(editCell);
editCell.setText(chooser.getName(editFile));
ComponentOrientation orientation = list.getComponentOrientation();
Expand Down Expand Up @@ -490,4 +505,35 @@ public Component getListCellRendererComponent(final JList<?> list, final Object
return comp;
}
}

protected TableCellEditor getDetailsTableCellEditor() {
if (tableCellEditor == null) {
tableCellEditor = new DarkTableCellEditorDelegate(new DetailsTableCellEditor());
}
return tableCellEditor;
}

protected class DetailsTableCellEditor extends DarkTableCellEditor {

public DetailsTableCellEditor() {
editorComponent.addFocusListener(editorFocusListener);
editorComponent.addAncestorListener(new AncestorAdapter() {
@Override
public void ancestorAdded(final AncestorEvent event) {
SwingUtilities.invokeLater(() -> {
editorComponent.requestFocus();
((JTextComponent) editorComponent).selectAll();
});
}
});
}

public Component getTableCellEditorComponent(final JTable table, final Object value,
final boolean isSelected, final int row, final int column) {
Object realValue = value instanceof File
? getFileChooser().getName((File) value)
: value;
return super.getTableCellEditorComponent(table, realValue, isSelected, row, column);
}
}
}
Loading

0 comments on commit 5af510e

Please sign in to comment.