From 0eff06096bc4852f2580f20a0c5bf970ecf66987 Mon Sep 17 00:00:00 2001 From: 2xsaiko Date: Wed, 29 Apr 2020 18:24:29 +0200 Subject: Rewrite search dialog (#233) * Fix searching * Make buttons use localization * Fix rename field opening when pressing shift+space * Tweak search algorithm * Add a bit of documentation * Remove duplicate example line * Use max() when building the inner map instead of overwriting the old value * Keep search dialog state * Formatting * Fix cursor key selection not scrolling to selected item * Don't set font size * Rename close0 to exit * Fix wrong scrolling when selecting search dialog entry--- .../enigma/gui/util/AbstractListCellRenderer.java | 75 ++++++++++++++++++++++ .../java/cuchaz/enigma/gui/util/ScaleUtil.java | 6 ++ 2 files changed, 81 insertions(+) create mode 100644 src/main/java/cuchaz/enigma/gui/util/AbstractListCellRenderer.java (limited to 'src/main/java/cuchaz/enigma/gui/util') diff --git a/src/main/java/cuchaz/enigma/gui/util/AbstractListCellRenderer.java b/src/main/java/cuchaz/enigma/gui/util/AbstractListCellRenderer.java new file mode 100644 index 0000000..e071fe1 --- /dev/null +++ b/src/main/java/cuchaz/enigma/gui/util/AbstractListCellRenderer.java @@ -0,0 +1,75 @@ +package cuchaz.enigma.gui.util; + +import java.awt.Component; +import java.awt.event.MouseEvent; + +import javax.swing.*; +import javax.swing.border.Border; + +public abstract class AbstractListCellRenderer extends JPanel implements ListCellRenderer { + + private static final Border NO_FOCUS_BORDER = BorderFactory.createEmptyBorder(1, 1, 1, 1); + + public AbstractListCellRenderer() { + setBorder(getNoFocusBorder()); + } + + protected Border getNoFocusBorder() { + Border border = UIManager.getLookAndFeel().getDefaults().getBorder("List.List.cellNoFocusBorder"); + if (border == null) { + return NO_FOCUS_BORDER; + } + return border; + } + + protected Border getBorder(boolean isSelected, boolean cellHasFocus) { + Border b = null; + if (cellHasFocus) { + UIDefaults defaults = UIManager.getLookAndFeel().getDefaults(); + if (isSelected) { + b = defaults.getBorder("List.focusSelectedCellHighlightBorder"); + } + if (b == null) { + b = defaults.getBorder("List.focusCellHighlightBorder"); + } + } else { + b = getNoFocusBorder(); + } + return b; + } + + public abstract void updateUiForEntry(JList list, E value, int index, boolean isSelected, boolean cellHasFocus); + + @Override + public Component getListCellRendererComponent(JList list, E value, int index, boolean isSelected, boolean cellHasFocus) { + updateUiForEntry(list, value, index, isSelected, cellHasFocus); + + if (isSelected) { + setBackground(list.getSelectionBackground()); + setForeground(list.getSelectionForeground()); + } else { + setBackground(list.getBackground()); + setForeground(list.getForeground()); + } + + setEnabled(list.isEnabled()); + setFont(list.getFont()); + + setBorder(getBorder(isSelected, cellHasFocus)); + + // This isn't the width of the cell, but it's close enough for where it's needed (getComponentAt in getToolTipText) + setSize(list.getWidth(), getPreferredSize().height); + + return this; + } + + @Override + public String getToolTipText(MouseEvent event) { + Component c = getComponentAt(event.getPoint()); + if (c instanceof JComponent) { + return ((JComponent) c).getToolTipText(); + } + return getToolTipText(); + } + +} diff --git a/src/main/java/cuchaz/enigma/gui/util/ScaleUtil.java b/src/main/java/cuchaz/enigma/gui/util/ScaleUtil.java index 8bc826f..9f722e9 100644 --- a/src/main/java/cuchaz/enigma/gui/util/ScaleUtil.java +++ b/src/main/java/cuchaz/enigma/gui/util/ScaleUtil.java @@ -7,7 +7,9 @@ import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; +import javax.swing.BorderFactory; import javax.swing.UIManager; +import javax.swing.border.Border; import com.github.swingdpi.UiDefaultsScaler; import com.github.swingdpi.plaf.BasicTweaker; @@ -69,6 +71,10 @@ public class ScaleUtil { return (int) (i * getScaleFactor()); } + public static Border createEmptyBorder(int top, int left, int bottom, int right) { + return BorderFactory.createEmptyBorder(scale(top), scale(left), scale(bottom), scale(right)); + } + public static int invert(int i) { return (int) (i / getScaleFactor()); } -- cgit v1.2.3