diff --git a/app/src/main/groovy/se/alipsa/accounting/ui/VoucherPanel.groovy b/app/src/main/groovy/se/alipsa/accounting/ui/VoucherPanel.groovy index 8ed5c87d..944034bf 100644 --- a/app/src/main/groovy/se/alipsa/accounting/ui/VoucherPanel.groovy +++ b/app/src/main/groovy/se/alipsa/accounting/ui/VoucherPanel.groovy @@ -26,6 +26,7 @@ import se.alipsa.datepicker.TextFieldPosition import java.awt.BorderLayout import java.awt.Color import java.awt.Desktop +import java.awt.Dimension import java.awt.FlowLayout import java.awt.Frame import java.awt.GridBagConstraints @@ -48,9 +49,7 @@ import java.util.logging.Logger import javax.swing.AbstractAction import javax.swing.BorderFactory import javax.swing.DefaultCellEditor -import javax.swing.Icon import javax.swing.JButton -import javax.swing.JCheckBox import javax.swing.JComboBox import javax.swing.JEditorPane import javax.swing.JLabel @@ -58,10 +57,12 @@ import javax.swing.JMenuItem import javax.swing.JPanel import javax.swing.JPopupMenu import javax.swing.JScrollPane +import javax.swing.JSeparator import javax.swing.JTabbedPane import javax.swing.JTable import javax.swing.JTextArea import javax.swing.JTextField +import javax.swing.JToggleButton import javax.swing.KeyStroke import javax.swing.ListSelectionModel import javax.swing.SwingConstants @@ -80,7 +81,6 @@ import javax.swing.table.DefaultTableCellRenderer final class VoucherPanel extends JPanel implements PropertyChangeListener, ListenerLifecycle { private static final Logger log = Logger.getLogger(VoucherPanel.name) - private static final Icon SAVE_ICON = new VoucherSaveIcon() private static final LocalDate DATE_PICKER_RANGE_FROM = LocalDate.of(1900, 1, 1) private static final LocalDate DATE_PICKER_RANGE_TO = LocalDate.now().plusYears(50) @@ -108,11 +108,6 @@ final class VoucherPanel extends JPanel implements PropertyChangeListener, Liste showRecorrectionConfirmDialog(existingNumbers) } - @PackageScope - Closure cannotDeleteConfirmer = { Voucher voucher -> - showCannotDeleteConfirmDialog(voucher) - } - private final JLabel voucherNumberLabel = new JLabel('') private final JLabel unsavedLabel = new JLabel() private final DatePicker datePicker = createDatePicker() @@ -129,7 +124,7 @@ final class VoucherPanel extends JPanel implements PropertyChangeListener, Liste @PackageScope final JTextArea feedbackArea = new JTextArea(2, 40) private final JTextField jumpField = new JTextField(8) - private final JCheckBox advanceAfterSaveCheckBox = new JCheckBox() + private final JToggleButton advanceAfterSaveToggle = VoucherToolbar.advanceToggle() private final JLabel voucherNumberCaptionLabel = new JLabel(I18n.instance.getString('voucherPanel.label.voucherNumber')) private final JLabel dateCaptionLabel = new JLabel(I18n.instance.getString('voucherPanel.label.date')) private final JLabel descriptionCaptionLabel = new JLabel(I18n.instance.getString('voucherPanel.label.description')) @@ -144,7 +139,6 @@ final class VoucherPanel extends JPanel implements PropertyChangeListener, Liste private JButton printButton private JButton duplicateButton private JButton correctionButton - private JButton voidButton private JButton addAttachmentButton private JButton openAttachmentButton private JButton newSeriesButton @@ -320,7 +314,7 @@ final class VoucherPanel extends JPanel implements PropertyChangeListener, Liste seriesComboBox.addActionListener { onSeriesSelectionChanged() } constraints.gridx++ panel.add(seriesComboBox, constraints) - newSeriesButton = navigationButton('+', 'voucherPanel.button.newSeries') { createNewSeries() } + newSeriesButton = VoucherToolbar.button('new-series', 'voucherPanel.button.newSeries') { createNewSeries() } constraints.gridx++ panel.add(newSeriesButton, constraints) } @@ -342,12 +336,12 @@ final class VoucherPanel extends JPanel implements PropertyChangeListener, Liste } private JPanel buildNavigationToolbar() { - JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 6, 2)) + JPanel panel = new VoucherToolbarPanel() - firstButton = navigationButton('\u23EE', 'voucherPanel.button.first') { navigateFirst() } + firstButton = VoucherToolbar.button('first', 'voucherPanel.button.first') { navigateFirst() } panel.add(firstButton) - prevButton = navigationButton('\u25C0', 'voucherPanel.button.prev') { navigatePrev() } + prevButton = VoucherToolbar.button('prev', 'voucherPanel.button.prev') { navigatePrev() } panel.add(prevButton) panel.add(jumpCaptionLabel) @@ -355,27 +349,23 @@ final class VoucherPanel extends JPanel implements PropertyChangeListener, Liste jumpField.toolTipText = I18n.instance.getString('voucherPanel.label.jump') panel.add(jumpField) - nextButton = navigationButton('\u25B6', 'voucherPanel.button.next') { navigateNext() } + nextButton = VoucherToolbar.button('next', 'voucherPanel.button.next') { navigateNext() } panel.add(nextButton) - lastButton = navigationButton('\u23ED', 'voucherPanel.button.last') { navigateLast() } + lastButton = VoucherToolbar.button('last', 'voucherPanel.button.last') { navigateLast() } panel.add(lastButton) + addToolbarSeparator(panel) addSaveControls(panel) + addToolbarSeparator(panel) - printButton = new JButton('\u2399') - printButton.toolTipText = I18n.instance.getString('voucherPanel.button.print') - printButton.addActionListener { printCurrentVoucher() } + printButton = VoucherToolbar.button('print', 'voucherPanel.button.print') { printCurrentVoucher() } panel.add(printButton) - duplicateButton = new JButton('\u29C9') - duplicateButton.toolTipText = I18n.instance.getString('voucherPanel.button.duplicate') - duplicateButton.addActionListener { duplicateVoucher() } + duplicateButton = VoucherToolbar.button('duplicate', 'voucherPanel.button.duplicate') { duplicateVoucher() } panel.add(duplicateButton) - correctionButton = new JButton('\u270E') - correctionButton.toolTipText = I18n.instance.getString('voucherPanel.button.createCorrection') - correctionButton.addActionListener { + correctionButton = VoucherToolbar.button('correction', 'voucherPanel.button.createCorrection') { if (currentVoucher != null && !confirmRecorrectionIfNeeded(currentVoucher.id)) { return } @@ -385,31 +375,19 @@ final class VoucherPanel extends JPanel implements PropertyChangeListener, Liste } panel.add(correctionButton) - voidButton = new JButton('\u2716') - voidButton.toolTipText = I18n.instance.getString('voucherPanel.button.void') - voidButton.addActionListener { deleteOrCancelVoucher() } - panel.add(voidButton) - panel } private void addSaveControls(JPanel panel) { - saveButton = new JButton(SAVE_ICON) - saveButton.margin = new Insets(5, 6, 5, 6) - saveButton.toolTipText = I18n.instance.getString('voucherPanel.button.save') - saveButton.addActionListener { voucherEditorActions.save() } + saveButton = VoucherToolbar.saveButton { voucherEditorActions.save() } panel.add(saveButton) - - advanceAfterSaveCheckBox.text = I18n.instance.getString('voucherPanel.checkbox.advanceAfterSave') - advanceAfterSaveCheckBox.selected = true - panel.add(advanceAfterSaveCheckBox) + panel.add(advanceAfterSaveToggle) } - private static JButton navigationButton(String symbol, String tooltipKey, Closure action) { - JButton button = new JButton(symbol) - button.toolTipText = I18n.instance.getString(tooltipKey) - button.addActionListener { action.call() } - button + private static void addToolbarSeparator(JPanel panel) { + JSeparator separator = new JSeparator(SwingConstants.VERTICAL) + separator.preferredSize = UIScale.scale(new Dimension(1, 22)) + panel.add(separator) } private JTabbedPane buildMainTabs() { @@ -763,7 +741,7 @@ final class VoucherPanel extends JPanel implements PropertyChangeListener, Liste String attachmentPathToApply = pendingAttachmentPath pendingAttachmentPath = null pendingAttachmentPreview = null - reloadVoucherList(advanceAfterSaveCheckBox.selected ? null : savedVoucher) + reloadVoucherList(advanceAfterSaveToggle.selected ? null : savedVoucher) if (attachmentPathToApply) { try { attachmentService.addAttachment(savedVoucher.id, Path.of(attachmentPathToApply)) @@ -1151,28 +1129,6 @@ final class VoucherPanel extends JPanel implements PropertyChangeListener, Liste dateFocusRequester.call() } - @PackageScope - void deleteOrCancelVoucher() { - if (currentVoucher == null) { - return - } - List existingCorrectionNumbers = voucherService.findCorrectionVoucherNumbers(currentVoucher.id) - if (!confirmRecorrectionIfNeeded(existingCorrectionNumbers)) { - return - } - try { - if (existingCorrectionNumbers.isEmpty() && !cannotDeleteConfirmer.call(currentVoucher)) { - return - } - Voucher correction = voucherService.createCorrectionVoucher(currentVoucher.id, null) - showInfo(I18n.instance.format('voucherPanel.message.correctionCreated', - correction.voucherNumber ?: '')) - reloadVoucherList() - } catch (Exception ex) { - showError(ex.message ?: I18n.instance.getString('voucherPanel.error.voidFailed')) - } - } - @PackageScope static boolean shouldProceedWithRecorrection(List existingCorrectionNumbers, boolean confirmed) { existingCorrectionNumbers.isEmpty() || confirmed @@ -1199,17 +1155,6 @@ final class VoucherPanel extends JPanel implements PropertyChangeListener, Liste choice == javax.swing.JOptionPane.YES_OPTION } - private boolean showCannotDeleteConfirmDialog(Voucher voucher) { - int choice = javax.swing.JOptionPane.showConfirmDialog( - this, - I18n.instance.getString('voucherPanel.confirm.cannotDelete') - .replace('{0}', voucher.voucherNumber ?: ''), - I18n.instance.getString('voucherPanel.button.void'), - javax.swing.JOptionPane.YES_NO_OPTION - ) - choice == javax.swing.JOptionPane.YES_OPTION - } - private void removeSelectedLine() { int selectedRow = lineTable.selectedRow if (lineTable.editing) { @@ -1254,7 +1199,6 @@ final class VoucherPanel extends JPanel implements PropertyChangeListener, Liste saveButton.enabled = !readOnly printButton.enabled = true duplicateButton.enabled = currentVoucher != null - voidButton.enabled = false correctionButton.enabled = currentVoucher != null && currentVoucher.accountingDate != null && currentVoucher.status == VoucherStatus.ACTIVE @@ -1503,17 +1447,18 @@ final class VoucherPanel extends JPanel implements PropertyChangeListener, Liste private void updateLabels() { datePicker.locale = I18n.instance.locale refreshCaptionLabels() - prevButton.toolTipText = I18n.instance.getString('voucherPanel.button.prev') - nextButton.toolTipText = I18n.instance.getString('voucherPanel.button.next') - firstButton.toolTipText = I18n.instance.getString('voucherPanel.button.first') - lastButton.toolTipText = I18n.instance.getString('voucherPanel.button.last') - saveButton.toolTipText = I18n.instance.getString('voucherPanel.button.save') - advanceAfterSaveCheckBox.text = I18n.instance.getString('voucherPanel.checkbox.advanceAfterSave') - printButton.toolTipText = I18n.instance.getString('voucherPanel.button.print') - duplicateButton.toolTipText = I18n.instance.getString('voucherPanel.button.duplicate') - correctionButton.toolTipText = I18n.instance.getString('voucherPanel.button.createCorrection') - voidButton.toolTipText = I18n.instance.getString('voucherPanel.button.void') - newSeriesButton.toolTipText = I18n.instance.getString('voucherPanel.button.newSeries') + VoucherToolbar.updateLabel(prevButton, 'voucherPanel.button.prev') + VoucherToolbar.updateLabel(nextButton, 'voucherPanel.button.next') + VoucherToolbar.updateLabel(firstButton, 'voucherPanel.button.first') + VoucherToolbar.updateLabel(lastButton, 'voucherPanel.button.last') + VoucherToolbar.updateLabel(saveButton, 'voucherPanel.button.save') + saveButton.text = I18n.instance.getString('voucherPanel.button.save') + VoucherToolbar.updateAdvanceToggleLabel(advanceAfterSaveToggle) + VoucherToolbar.updateLabel(datePicker.calendarButton, 'voucherPanel.button.chooseDate') + VoucherToolbar.updateLabel(printButton, 'voucherPanel.button.print') + VoucherToolbar.updateLabel(duplicateButton, 'voucherPanel.button.duplicate') + VoucherToolbar.updateLabel(correctionButton, 'voucherPanel.button.createCorrection') + VoucherToolbar.updateLabel(newSeriesButton, 'voucherPanel.button.newSeries') addAttachmentButton.text = I18n.instance.getString('voucherPanel.button.addAttachment') openAttachmentButton.text = I18n.instance.getString('voucherPanel.button.openAttachment') jumpField.toolTipText = I18n.instance.getString('voucherPanel.label.jump') @@ -1547,7 +1492,8 @@ final class VoucherPanel extends JPanel implements PropertyChangeListener, Liste // Use explicit bounds so historical vouchers and AI drafts are not silently rejected. DatePicker picker = new DatePicker( DATE_PICKER_RANGE_FROM, DATE_PICKER_RANGE_TO, null, I18n.instance.locale) - picker.textFieldPosition = TextFieldPosition.RIGHT + picker.textFieldPosition = TextFieldPosition.LEFT + VoucherToolbar.configure(picker.calendarButton, 'calendar', 'voucherPanel.button.chooseDate') picker } diff --git a/app/src/main/groovy/se/alipsa/accounting/ui/VoucherSaveIcon.groovy b/app/src/main/groovy/se/alipsa/accounting/ui/VoucherSaveIcon.groovy deleted file mode 100644 index 88328af0..00000000 --- a/app/src/main/groovy/se/alipsa/accounting/ui/VoucherSaveIcon.groovy +++ /dev/null @@ -1,34 +0,0 @@ -package se.alipsa.accounting.ui - -import java.awt.Color -import java.awt.Component -import java.awt.Graphics -import java.awt.Graphics2D -import java.awt.RenderingHints - -import javax.swing.Icon - -/** - * Cross-platform save icon that does not depend on an emoji font. - */ -final class VoucherSaveIcon implements Icon { - - final int iconWidth = 16 - final int iconHeight = 16 - - @Override - void paintIcon(Component component, Graphics graphics, int x, int y) { - Graphics2D g2 = graphics.create() as Graphics2D - try { - g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON) - Color color = component.foreground ?: Color.DARK_GRAY - g2.setColor(color) - g2.drawRoundRect(x + 2, y + 1, 12, 14, 2, 2) - g2.fillRect(x + 5, y + 2, 6, 4) - g2.drawRect(x + 5, y + 9, 6, 4) - g2.drawLine(x + 7, y + 2, x + 7, y + 5) - } finally { - g2.dispose() - } - } -} diff --git a/app/src/main/groovy/se/alipsa/accounting/ui/VoucherToolbar.groovy b/app/src/main/groovy/se/alipsa/accounting/ui/VoucherToolbar.groovy new file mode 100644 index 00000000..bff7344d --- /dev/null +++ b/app/src/main/groovy/se/alipsa/accounting/ui/VoucherToolbar.groovy @@ -0,0 +1,109 @@ +package se.alipsa.accounting.ui + +import com.formdev.flatlaf.FlatClientProperties +import com.formdev.flatlaf.extras.FlatSVGIcon +import com.formdev.flatlaf.util.UIScale + +import se.alipsa.accounting.support.I18n + +import java.awt.Color +import java.awt.Component +import java.awt.Dimension +import java.awt.FontMetrics +import java.awt.Insets +import java.util.function.BiFunction + +import javax.swing.AbstractButton +import javax.swing.JButton +import javax.swing.JToggleButton +import javax.swing.UIManager + +/** Shared sizing, accessible names and theme-aware SVG icons for voucher commands. */ +final class VoucherToolbar { + + private VoucherToolbar() { } + + static JButton button(String iconName, String labelKey, Closure action) { + JButton button = new CommandButton() + configure(button, iconName, labelKey) + button.addActionListener { action.call() } + button + } + + static JButton saveButton(Closure action) { + JButton button = button('save', 'voucherPanel.button.save', action) + button.text = I18n.instance.getString('voucherPanel.button.save') + button.margin = new Insets(0, 10, 0, 10) + button.putClientProperty(FlatClientProperties.STYLE, + 'background: $List.selectionBackground; foreground: $List.selectionForeground; focusedBackground: $List.selectionBackground') + button + } + + static JToggleButton advanceToggle() { + JToggleButton button = new AdvanceToggleButton() + configure(button, 'new-after-save', 'voucherPanel.toggle.advanceAfterSave') + button.margin = new Insets(0, 8, 0, 8) + button.addItemListener { updateAdvanceToggleLabel(button) } + button.selected = true + button + } + + static void updateAdvanceToggleLabel(JToggleButton button) { + updateLabel(button, 'voucherPanel.toggle.advanceAfterSave') + String state = button.selected ? 'on' : 'off' + button.text = I18n.instance.getString("voucherPanel.toggle.advanceAfterSave.${state}") + } + + static void configure(AbstractButton button, String iconName, String labelKey) { + FlatSVGIcon icon = new FlatSVGIcon("icons/voucher/${iconName}.svg", 16, 16) + icon.setColorFilter(new FlatSVGIcon.ColorFilter({ Component component, Color ignored -> + if (component != null && !component.enabled) { + return UIManager.getColor('Button.disabledText') + } + if (component instanceof JToggleButton && component.selected) { + return UIManager.getColor('ToggleButton.selectedForeground') ?: component.foreground + } + component?.foreground ?: UIManager.getColor('Label.foreground') + } as BiFunction)) + button.icon = icon + // The filter already supplies the disabled color; avoid applying a second gray filter. + button.disabledIcon = icon + button.margin = new Insets(0, 0, 0, 0) + updateLabel(button, labelKey) + } + + static void updateLabel(AbstractButton button, String labelKey) { + String label = I18n.instance.getString(labelKey) + button.toolTipText = label + button.accessibleContext.accessibleName = label + } + + private static Dimension commandSize(Dimension preferred) { + int minimum = UIScale.scale(34) + new Dimension(Math.max(minimum, preferred.width as int), Math.max(minimum, preferred.height as int)) + } + + /** Recompute command sizing from the current font and UI scale on every layout. */ + private static final class CommandButton extends JButton { + + @Override + Dimension getPreferredSize() { + commandSize(super.getPreferredSize()) + } + } + + /** Keep labelled mode controls aligned with commands without fixing their text width. */ + private static final class AdvanceToggleButton extends JToggleButton { + + @Override + Dimension getPreferredSize() { + Dimension preferred = super.getPreferredSize() + FontMetrics metrics = getFontMetrics(font) + int widestLabel = Math.max( + metrics.stringWidth(I18n.instance.getString('voucherPanel.toggle.advanceAfterSave.on')), + metrics.stringWidth(I18n.instance.getString('voucherPanel.toggle.advanceAfterSave.off'))) + int extraWidth = Math.max(0, widestLabel - metrics.stringWidth(text ?: '')) + commandSize(new Dimension((preferred.width as int) + extraWidth, preferred.height as int)) + } + } +} diff --git a/app/src/main/groovy/se/alipsa/accounting/ui/VoucherToolbarPanel.groovy b/app/src/main/groovy/se/alipsa/accounting/ui/VoucherToolbarPanel.groovy new file mode 100644 index 00000000..0b915141 --- /dev/null +++ b/app/src/main/groovy/se/alipsa/accounting/ui/VoucherToolbarPanel.groovy @@ -0,0 +1,47 @@ +package se.alipsa.accounting.ui + +import com.formdev.flatlaf.util.UIScale + +import java.awt.Component +import java.awt.Dimension +import java.awt.FlowLayout +import java.awt.Insets + +import javax.swing.JPanel + +/** Reserves the height of wrapped command rows when placed in BorderLayout.NORTH. */ +final class VoucherToolbarPanel extends JPanel { + + VoucherToolbarPanel() { + super(new FlowLayout(FlowLayout.LEFT, UIScale.scale(6), UIScale.scale(2))) + } + + @Override + Dimension getPreferredSize() { + Dimension natural = super.getPreferredSize() + if (parent == null || parent.width <= 0) { + return natural + } + FlowLayout flow = layout as FlowLayout + Insets padding = insets + int available = parent.width - parent.insets.left - parent.insets.right - + padding.left - padding.right - 2 * flow.hgap + int height = padding.top + padding.bottom + 2 * flow.vgap + int rowWidth = 0 + int rowHeight = 0 + for (Component component : components) { + if (!component.visible) { + continue + } + Dimension size = component.preferredSize + if (rowWidth > 0 && rowWidth + size.width > available) { + height += rowHeight + flow.vgap + rowWidth = 0 + rowHeight = 0 + } + rowWidth += (rowWidth > 0 ? flow.hgap : 0) + (size.width as int) + rowHeight = Math.max(rowHeight, size.height as int) + } + new Dimension(natural.width as int, height + rowHeight) + } +} diff --git a/app/src/main/resources/i18n/messages.properties b/app/src/main/resources/i18n/messages.properties index 08f201de..c3c52300 100644 --- a/app/src/main/resources/i18n/messages.properties +++ b/app/src/main/resources/i18n/messages.properties @@ -847,17 +847,19 @@ voucherPanel.label.corrects=Corrects voucherPanel.label.correctedBy=Corrected by {0} voucherPanel.label.series=Series voucherPanel.button.newSeries=Create new series... -voucherPanel.button.prev=\u25C0 -voucherPanel.button.next=\u25B6 +voucherPanel.button.prev=Previous voucher +voucherPanel.button.next=Next voucher voucherPanel.button.first=First voucher voucherPanel.button.last=Last voucher voucherPanel.button.save=Save -voucherPanel.checkbox.advanceAfterSave=New voucher after save +voucherPanel.toggle.advanceAfterSave=New voucher after save +voucherPanel.toggle.advanceAfterSave.on=New after save: On +voucherPanel.toggle.advanceAfterSave.off=New after save: Off +voucherPanel.button.chooseDate=Choose date voucherPanel.button.print=Print voucher voucherPanel.button.removeLine=Remove line voucherPanel.button.duplicate=Duplicate voucherPanel.button.createCorrection=Create correction -voucherPanel.button.void=Delete voucher voucherPanel.button.addAttachment=Add attachment... voucherPanel.button.openAttachment=Open attachment... voucherPanel.tab.lines=Lines @@ -877,13 +879,11 @@ voucherPanel.message.deleted=Voucher {0} deleted. voucherPanel.message.voided=Voucher {0} voided. voucherPanel.message.printed=Voucher {0} was sent to the printer. voucherPanel.confirm.delete=Delete voucher {0}? -voucherPanel.confirm.cannotDelete=Voucher {0} cannot be deleted because there are later vouchers in the series. Would you like to create a correction voucher that cancels it instead? voucherPanel.confirm.alreadyCorrected=This voucher was already corrected by {0}. Would you like to create another correction anyway? voucherPanel.confirm.alreadyCorrected.title=Confirm correction voucherPanel.message.correctionCreated=Correction created as {0}. voucherPanel.message.duplicateCreated=Draft copy created. Choose a date and save. voucherPanel.error.saveFailed=The voucher could not be saved. -voucherPanel.error.voidFailed=The voucher could not be voided. voucherPanel.error.correctionFailed=The correction could not be created. voucherPanel.error.existingVoucherReadOnly=Recorded vouchers are read-only. Create a correction voucher instead. voucherPanel.error.noFiscalYear=Select a fiscal year before registering vouchers. diff --git a/app/src/main/resources/i18n/messages_sv.properties b/app/src/main/resources/i18n/messages_sv.properties index 72799c83..0f5c195b 100644 --- a/app/src/main/resources/i18n/messages_sv.properties +++ b/app/src/main/resources/i18n/messages_sv.properties @@ -847,17 +847,19 @@ voucherPanel.label.corrects=Korrigerar voucherPanel.label.correctedBy=Korrigerad av {0} voucherPanel.label.series=Serie voucherPanel.button.newSeries=Skapa ny serie... -voucherPanel.button.prev=◀ -voucherPanel.button.next=▶ +voucherPanel.button.prev=Föregående verifikat +voucherPanel.button.next=Nästa verifikat voucherPanel.button.first=Första verifikatet voucherPanel.button.last=Sista verifikatet voucherPanel.button.save=Spara -voucherPanel.checkbox.advanceAfterSave=Nytt verifikat efter sparning +voucherPanel.toggle.advanceAfterSave=Nytt verifikat efter sparning +voucherPanel.toggle.advanceAfterSave.on=Nytt efter sparning: På +voucherPanel.toggle.advanceAfterSave.off=Nytt efter sparning: Av +voucherPanel.button.chooseDate=Välj datum voucherPanel.button.print=Skriv ut verifikation voucherPanel.button.removeLine=Ta bort rad voucherPanel.button.duplicate=Duplicera voucherPanel.button.createCorrection=Skapa korrigering -voucherPanel.button.void=Ta bort verifikation voucherPanel.button.addAttachment=Lägg till bilaga... voucherPanel.button.openAttachment=Öppna bilaga... voucherPanel.tab.lines=Rader @@ -877,13 +879,11 @@ voucherPanel.message.deleted=Verifikation {0} togs bort. voucherPanel.message.voided=Verifikation {0} makulerades. voucherPanel.message.printed=Verifikation {0} skickades till skrivaren. voucherPanel.confirm.delete=Ta bort verifikation {0}? -voucherPanel.confirm.cannotDelete=Verifikation {0} kan inte tas bort eftersom det finns senare verifikationer i serien. Vill du skapa en korrigeringsverifikation som tar ut den istället? voucherPanel.confirm.alreadyCorrected=Verifikationen har redan korrigerats av {0}. Vill du skapa ytterligare en korrigeringsverifikation ändå? voucherPanel.confirm.alreadyCorrected.title=Bekräfta korrigering voucherPanel.message.correctionCreated=Korrigering skapad som {0}. voucherPanel.message.duplicateCreated=Utkastkopia skapad. Välj datum och spara. voucherPanel.error.saveFailed=Verifikationen kunde inte sparas. -voucherPanel.error.voidFailed=Verifikationen kunde inte makuleras. voucherPanel.error.correctionFailed=Korrigeringen kunde inte skapas. voucherPanel.error.existingVoucherReadOnly=Registrerade verifikationer är skrivskyddade. Skapa en korrigeringsverifikation i stället. voucherPanel.error.noFiscalYear=Välj ett räkenskapsår innan verifikationer registreras. diff --git a/app/src/main/resources/icons/voucher/LICENSE-lucide.txt b/app/src/main/resources/icons/voucher/LICENSE-lucide.txt new file mode 100644 index 00000000..325e8ff0 --- /dev/null +++ b/app/src/main/resources/icons/voucher/LICENSE-lucide.txt @@ -0,0 +1,15 @@ +ISC License + +Copyright (c) for portions of Lucide are held by Cole Bemis 2013-2022 as part of Feather (MIT). All other copyright (c) for Lucide are held by Lucide Contributors 2022. + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/app/src/main/resources/icons/voucher/calendar.svg b/app/src/main/resources/icons/voucher/calendar.svg new file mode 100644 index 00000000..eb1e4d8e --- /dev/null +++ b/app/src/main/resources/icons/voucher/calendar.svg @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/app/src/main/resources/icons/voucher/correction.svg b/app/src/main/resources/icons/voucher/correction.svg new file mode 100644 index 00000000..b571a770 --- /dev/null +++ b/app/src/main/resources/icons/voucher/correction.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/app/src/main/resources/icons/voucher/duplicate.svg b/app/src/main/resources/icons/voucher/duplicate.svg new file mode 100644 index 00000000..49bfe72a --- /dev/null +++ b/app/src/main/resources/icons/voucher/duplicate.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/resources/icons/voucher/first.svg b/app/src/main/resources/icons/voucher/first.svg new file mode 100644 index 00000000..6602bfe2 --- /dev/null +++ b/app/src/main/resources/icons/voucher/first.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/resources/icons/voucher/last.svg b/app/src/main/resources/icons/voucher/last.svg new file mode 100644 index 00000000..dc230247 --- /dev/null +++ b/app/src/main/resources/icons/voucher/last.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/resources/icons/voucher/new-after-save.svg b/app/src/main/resources/icons/voucher/new-after-save.svg new file mode 100644 index 00000000..51de5441 --- /dev/null +++ b/app/src/main/resources/icons/voucher/new-after-save.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/app/src/main/resources/icons/voucher/new-series.svg b/app/src/main/resources/icons/voucher/new-series.svg new file mode 100644 index 00000000..63f14777 --- /dev/null +++ b/app/src/main/resources/icons/voucher/new-series.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/resources/icons/voucher/next.svg b/app/src/main/resources/icons/voucher/next.svg new file mode 100644 index 00000000..81c3d14e --- /dev/null +++ b/app/src/main/resources/icons/voucher/next.svg @@ -0,0 +1,4 @@ + + + + diff --git a/app/src/main/resources/icons/voucher/prev.svg b/app/src/main/resources/icons/voucher/prev.svg new file mode 100644 index 00000000..e21b6186 --- /dev/null +++ b/app/src/main/resources/icons/voucher/prev.svg @@ -0,0 +1,4 @@ + + + + diff --git a/app/src/main/resources/icons/voucher/print.svg b/app/src/main/resources/icons/voucher/print.svg new file mode 100644 index 00000000..b89cf650 --- /dev/null +++ b/app/src/main/resources/icons/voucher/print.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/app/src/main/resources/icons/voucher/save.svg b/app/src/main/resources/icons/voucher/save.svg new file mode 100644 index 00000000..eadf8354 --- /dev/null +++ b/app/src/main/resources/icons/voucher/save.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/app/src/test/groovy/integration/se/alipsa/accounting/ui/VoucherPanelHeaderLayoutTest.groovy b/app/src/test/groovy/integration/se/alipsa/accounting/ui/VoucherPanelHeaderLayoutTest.groovy index 74c262b5..ac81d8bc 100644 --- a/app/src/test/groovy/integration/se/alipsa/accounting/ui/VoucherPanelHeaderLayoutTest.groovy +++ b/app/src/test/groovy/integration/se/alipsa/accounting/ui/VoucherPanelHeaderLayoutTest.groovy @@ -1,12 +1,21 @@ package se.alipsa.accounting.ui -import static org.junit.jupiter.api.Assertions.assertEquals +import static org.junit.jupiter.api.Assertions.* +import com.formdev.flatlaf.extras.FlatSVGIcon import org.junit.jupiter.api.Test +import se.alipsa.accounting.support.I18n import se.alipsa.datepicker.DatePicker +import java.awt.Component import java.awt.Container +import java.awt.FlowLayout + +import javax.swing.AbstractButton +import javax.swing.JButton +import javax.swing.JTabbedPane +import javax.swing.JToggleButton final class VoucherPanelHeaderLayoutTest extends VoucherPanelTestSupport { @@ -27,4 +36,97 @@ final class VoucherPanelHeaderLayoutTest extends VoucherPanelTestSupport { assertEquals(datePicker.textField.preferredSize.width as int, datePicker.textField.width) } } + + @Test + void realCommandsAndCalendarHaveSvgIconsAndTranslatedActionNames() { + withLocales { + onEdt { + Map actions = [first: 'first', prev: 'prev', next: 'next', last: 'last', + save: 'save', print: 'print', duplicate: 'duplicate', correction: 'createCorrection', 'new-series': 'newSeries'] + actions.each { String iconName, String action -> + String label = I18n.instance.getString("voucherPanel.button.${action}") + JButton button = findComponent(panel, JButton) { JButton candidate -> candidate.toolTipText == label } + assertEquals(label, button.accessibleContext.accessibleName) + assertSvg(button, iconName) + } + DatePicker picker = findComponent(panel, DatePicker) { true } + String chooseDate = I18n.instance.getString('voucherPanel.button.chooseDate') + assertEquals(chooseDate, picker.calendarButton.toolTipText) + assertEquals(chooseDate, picker.calendarButton.accessibleContext.accessibleName) + assertNotEquals(I18n.instance.getString('voucherPanel.label.date'), chooseDate) + assertSvg(picker.calendarButton, 'calendar') + JToggleButton toggle = findComponent(panel, JToggleButton) { true } + assertSvg(toggle, 'new-after-save') + assertEquals(I18n.instance.getString('voucherPanel.toggle.advanceAfterSave'), toggle.toolTipText) + assertEquals(I18n.instance.getString('voucherPanel.toggle.advanceAfterSave.on'), toggle.text) + assertTrue(toggle.selected) + VoucherToolbarPanel toolbar = findComponent(panel, VoucherToolbarPanel) { true } + Set commandIcons = toolbar.components.findAll { Component child -> + child instanceof AbstractButton + }.collect { Component child -> ((child as AbstractButton).icon as FlatSVGIcon).name }.toSet() + assertEquals(['first', 'prev', 'next', 'last', 'save', 'new-after-save', 'print', 'duplicate', 'correction'] + .collect { String name -> "icons/voucher/${name}.svg".toString() }.toSet(), commandIcons) + } + } + } + + @Test + void wrappedCommandsRemainVisibleAtNarrowWidthsInBothLanguages() { + withLocales { + onEdt { + VoucherToolbarPanel toolbar = findComponent(panel, VoucherToolbarPanel) { true } + JTabbedPane tabs = findComponent(panel, JTabbedPane) { true } + JToggleButton toggle = findComponent(panel, JToggleButton) { true } + ((300..1300).toList() + [400, 300, 1300]).each { int width -> + panel.setSize(width, 600) + layoutTree(panel) + toolbar.components.findAll { Component child -> child.visible }.each { Component child -> + assertTrue(child.x >= 0 && child.x + child.width <= toolbar.width, + "Command must fit horizontally at width ${width}".toString()) + assertTrue(child.y >= 0 && child.y + child.height <= toolbar.height, + "Wrapped command must fit vertically at width ${width}".toString()) + } + int lastBottom = toolbar.components.collect { Component child -> child.y + child.height }.max() as int + assertEquals(lastBottom + toolbar.insets.bottom + (toolbar.layout as FlowLayout).vgap, toolbar.height, + "Toolbar must reserve exactly the wrapped rows at width ${width}".toString()) + def positions = toolbar.components.collect { Component child -> child.bounds } + toggle.selected = !toggle.selected + layoutTree(panel) + assertEquals(positions, toolbar.components.collect { Component child -> child.bounds }, + "Toggling must not move commands at width ${width}".toString()) + assertTrue(tabs.y >= toolbar.y + toolbar.height) + } + } + } + } + + private void withLocales(Closure test) { + Locale original = I18n.instance.locale + try { + [Locale.ENGLISH, Locale.forLanguageTag('sv')].each { Locale locale -> + onEdt { I18n.instance.locale = locale } + onEdt { null } // Drain the queued label refresh before inspecting the controls. + test.call() + } + } finally { + onEdt { I18n.instance.locale = original } + onEdt { null } + } + } + + private static void assertSvg(AbstractButton button, String iconName) { + assertInstanceOf(FlatSVGIcon, button.icon) + FlatSVGIcon icon = button.icon as FlatSVGIcon + assertTrue(icon.hasFound()) + assertEquals("icons/voucher/${iconName}.svg".toString(), icon.name) + } + + private static void layoutTree(Container container) { + container.doLayout() + container.components.each { Component child -> + if (child instanceof Container) { + layoutTree(child) + } + } + } } diff --git a/app/src/test/groovy/integration/se/alipsa/accounting/ui/VoucherPanelNavigationTest.groovy b/app/src/test/groovy/integration/se/alipsa/accounting/ui/VoucherPanelNavigationTest.groovy index c4f26ae8..e9141a45 100644 --- a/app/src/test/groovy/integration/se/alipsa/accounting/ui/VoucherPanelNavigationTest.groovy +++ b/app/src/test/groovy/integration/se/alipsa/accounting/ui/VoucherPanelNavigationTest.groovy @@ -23,13 +23,13 @@ import java.time.LocalDate import java.util.concurrent.TimeUnit import javax.swing.JButton -import javax.swing.JCheckBox import javax.swing.JComboBox import javax.swing.JLabel import javax.swing.JTabbedPane import javax.swing.JTable import javax.swing.JTextArea import javax.swing.JTextField +import javax.swing.JToggleButton final class VoucherPanelNavigationTest extends VoucherPanelTestSupport { @@ -49,8 +49,7 @@ final class VoucherPanelNavigationTest extends VoucherPanelTestSupport { } assertNotNull(saveButton.icon) - assertEquals(6, saveButton.margin.left) - assertEquals(6, saveButton.margin.right) + assertEquals(I18n.instance.getString('voucherPanel.button.save'), saveButton.text) } @Test @@ -213,8 +212,8 @@ final class VoucherPanelNavigationTest extends VoucherPanelTestSupport { assertEquals('Försäljning', onEdt { panel.lineTableModel.rows[1].accountName }) assertEquals('Rad två', onEdt { panel.lineTableModel.rows[1].description }) - JCheckBox advanceAfterSave = findComponent(panel, JCheckBox) { JCheckBox checkBox -> - checkBox.text == I18n.instance.getString('voucherPanel.checkbox.advanceAfterSave') + JToggleButton advanceAfterSave = findComponent(panel, JToggleButton) { JToggleButton toggle -> + toggle.toolTipText == I18n.instance.getString('voucherPanel.toggle.advanceAfterSave') } assertTrue(onEdt { advanceAfterSave.selected }) onEdt { @@ -285,8 +284,8 @@ final class VoucherPanelNavigationTest extends VoucherPanelTestSupport { void savingWithDefaultAdvanceAfterSaveShowsANewBlankDraft() { JTextField description = findComponent(panel, JTextField) { JTextField field -> field.columns == 30 } JTextField voucherJumpField = findComponent(panel, JTextField) { JTextField field -> field.columns == 8 } - JCheckBox advanceAfterSave = findComponent(panel, JCheckBox) { JCheckBox checkBox -> - checkBox.text == I18n.instance.getString('voucherPanel.checkbox.advanceAfterSave') + JToggleButton advanceAfterSave = findComponent(panel, JToggleButton) { JToggleButton toggle -> + toggle.toolTipText == I18n.instance.getString('voucherPanel.toggle.advanceAfterSave') } JLabel unsaved = findComponent(panel, JLabel) { JLabel label -> label.text == I18n.instance.getString('voucherPanel.label.unsaved') @@ -647,60 +646,6 @@ final class VoucherPanelNavigationTest extends VoucherPanelTestSupport { assertFalse(onEdt { correctedBy.visible }) } - @Test - void deleteOrCancelVoucherSkipsCannotDeleteConfirmationForAlreadyCorrectedVoucher() { - Voucher original = voucherService.createVoucher( - fiscalYear.id, 'A', LocalDate.of(2030, 4, 8), 'Original', - [voucherLine('1510', 'Kundfordringar', '', 100.00G, 0.00G), - voucherLine('3010', 'Försäljning', '', 0.00G, 100.00G)] - ) - Voucher firstCorrection = voucherService.createCorrectionVoucher(original.id) - panel?.dispose() - panel = buildPanel() - boolean cannotDeleteConfirmationShown = false - panel.recorrectionConfirmer = { List existing -> true } - panel.cannotDeleteConfirmer = { Voucher voucher -> - cannotDeleteConfirmationShown = true - false - } - - onEdt { - clickButtonWithTooltip(panel, I18n.instance.getString('voucherPanel.button.prev')) - clickButtonWithTooltip(panel, I18n.instance.getString('voucherPanel.button.prev')) - panel.deleteOrCancelVoucher() - } - - assertFalse(cannotDeleteConfirmationShown) - assertEquals(2, voucherService.findCorrectionVoucherNumbers(original.id).size()) - assertEquals(firstCorrection.voucherNumber, - voucherService.findCorrectionVoucherNumbers(original.id).first()) - } - - @Test - void deleteOrCancelVoucherUsesCannotDeleteConfirmationBeforeFirstCorrection() { - Voucher original = voucherService.createVoucher( - fiscalYear.id, 'A', LocalDate.of(2030, 4, 9), 'Original', - [voucherLine('1510', 'Kundfordringar', '', 100.00G, 0.00G), - voucherLine('3010', 'Försäljning', '', 0.00G, 100.00G)] - ) - panel?.dispose() - panel = buildPanel() - int cannotDeleteConfirmationCount = 0 - panel.cannotDeleteConfirmer = { Voucher voucher -> - cannotDeleteConfirmationCount++ - cannotDeleteConfirmationCount > 1 - } - - onEdt { - clickButtonWithTooltip(panel, I18n.instance.getString('voucherPanel.button.prev')) - panel.deleteOrCancelVoucher() - panel.deleteOrCancelVoucher() - } - - assertEquals(2, cannotDeleteConfirmationCount) - assertEquals(1, voucherService.findCorrectionVoucherNumbers(original.id).size()) - } - @Test void printableVoucherHtmlContainsHeaderLinesAndTotals() { Voucher voucher = voucherService.createVoucher( diff --git a/app/src/test/groovy/unit/se/alipsa/accounting/ui/VoucherToolbarPanelTest.groovy b/app/src/test/groovy/unit/se/alipsa/accounting/ui/VoucherToolbarPanelTest.groovy new file mode 100644 index 00000000..d0c14ebf --- /dev/null +++ b/app/src/test/groovy/unit/se/alipsa/accounting/ui/VoucherToolbarPanelTest.groovy @@ -0,0 +1,47 @@ +package se.alipsa.accounting.ui + +import static org.junit.jupiter.api.Assertions.* + +import org.junit.jupiter.api.Test + +import java.awt.BorderLayout +import java.awt.Component +import java.awt.Dimension +import java.awt.FlowLayout + +import javax.swing.BorderFactory +import javax.swing.JButton +import javax.swing.JPanel +import javax.swing.SwingUtilities + +final class VoucherToolbarPanelTest { + + @Test + void wrappedHeightMatchesFlowLayoutWithPanelAndParentInsetsAtEveryWidth() { + SwingUtilities.invokeAndWait { + JPanel parent = new JPanel(new BorderLayout()) + parent.border = BorderFactory.createEmptyBorder(3, 7, 11, 13) + VoucherToolbarPanel toolbar = new VoucherToolbarPanel() + toolbar.border = BorderFactory.createEmptyBorder(4, 9, 6, 5) + [51, 88, 99, 34, 107, 68, 42].each { int width -> + JButton button = new JButton() + button.preferredSize = new Dimension(width, width == 88 ? 42 : 34) + toolbar.add(button) + } + parent.add(toolbar, BorderLayout.NORTH) + (200..1300).each { int width -> + parent.setSize(width, 600) + parent.doLayout() + toolbar.doLayout() + toolbar.components.each { Component child -> + assertTrue(child.x >= 0 && child.x + child.width <= toolbar.width) + assertTrue(child.y >= 0 && child.y + child.height <= toolbar.height, + "Wrapped row must fit at width ${width}".toString()) + } + int bottom = toolbar.components.collect { Component child -> child.y + child.height }.max() as int + assertEquals(bottom + toolbar.insets.bottom + (toolbar.layout as FlowLayout).vgap, toolbar.height, + "No extra blank row at width ${width}".toString()) + } + } + } +} diff --git a/app/src/test/groovy/unit/se/alipsa/accounting/ui/VoucherToolbarTest.groovy b/app/src/test/groovy/unit/se/alipsa/accounting/ui/VoucherToolbarTest.groovy new file mode 100644 index 00000000..96b7109c --- /dev/null +++ b/app/src/test/groovy/unit/se/alipsa/accounting/ui/VoucherToolbarTest.groovy @@ -0,0 +1,163 @@ +package se.alipsa.accounting.ui + +import static org.junit.jupiter.api.Assertions.* + +import com.formdev.flatlaf.FlatDarkLaf +import com.formdev.flatlaf.FlatLightLaf +import com.formdev.flatlaf.extras.FlatSVGIcon +import com.formdev.flatlaf.util.UIScale +import org.junit.jupiter.api.Test + +import se.alipsa.accounting.support.I18n + +import java.awt.Color +import java.awt.Graphics2D +import java.awt.image.BufferedImage + +import javax.swing.AbstractButton +import javax.swing.Icon +import javax.swing.JButton +import javax.swing.JToggleButton +import javax.swing.LookAndFeel +import javax.swing.SwingUtilities +import javax.swing.UIDefaults +import javax.swing.UIManager + +final class VoucherToolbarTest { + + @Test + void commandIconsLoadAndFollowComponentForegroundInBothThemes() { + withThemes { + Map actions = [first: 'first', prev: 'prev', next: 'next', last: 'last', + print: 'print', duplicate: 'duplicate', correction: 'createCorrection', 'new-series': 'newSeries'] + actions.each { String iconName, String action -> + String label = I18n.instance.getString("voucherPanel.button.${action}") + JButton button = VoucherToolbar.button(iconName, "voucherPanel.button.${action}") { null } + assertTrue((button.icon as FlatSVGIcon).hasFound()) + assertEquals(UIScale.scale(34), button.preferredSize.height as int) + assertEquals(UIScale.scale(34), button.preferredSize.width as int) + assertEquals(label, button.toolTipText) + assertEquals(label, button.accessibleContext.accessibleName) + button.foreground = Color.MAGENTA + assertIconColor(button, Color.MAGENTA) + } + } + } + + @Test + void disabledSaveIconUsesDisabledTextWithoutAnAdditionalGrayFilter() { + withThemes { + JButton button = VoucherToolbar.saveButton { null } + assertIconColor(button, button.foreground) + button.enabled = false + assertIconColor(button, UIManager.getColor('Button.disabledText')) + } + } + + @Test + void advanceToggleShowsItsStateInTextAndUsesTheThemeToggleForeground() { + withThemes { + JToggleButton toggle = VoucherToolbar.advanceToggle() + assertTrue(toggle.selected) + assertEquals(I18n.instance.getString('voucherPanel.toggle.advanceAfterSave.on'), toggle.text) + assertIconColor(toggle, UIManager.getColor('ToggleButton.selectedForeground') ?: toggle.foreground) + toggle.enabled = false + assertIconColor(toggle, UIManager.getColor('Button.disabledText')) + toggle.enabled = true + toggle.doClick() + assertFalse(toggle.selected) + assertEquals(I18n.instance.getString('voucherPanel.toggle.advanceAfterSave.off'), toggle.text) + assertIconColor(toggle, toggle.foreground) + } + } + + @Test + void labelledButtonsRecomputeTheirSizeWhenTheFontChanges() { + withThemes { + [VoucherToolbar.saveButton { null }, VoucherToolbar.advanceToggle()].each { AbstractButton button -> + int originalHeight = button.preferredSize.height as int + int originalWidth = button.preferredSize.width as int + button.font = button.font.deriveFont(36F) + assertTrue(button.preferredSize.height > originalHeight) + assertTrue(button.preferredSize.width > originalWidth) + } + } + } + + @Test + void advanceToggleHasTheSameWidthForBothStatesInBothLanguages() { + withThemes { + Locale original = I18n.instance.locale + try { + [Locale.ENGLISH, Locale.forLanguageTag('sv')].each { Locale locale -> + I18n.instance.locale = locale + JToggleButton toggle = VoucherToolbar.advanceToggle() + [12F, 36F].each { float fontSize -> + toggle.font = toggle.font.deriveFont(fontSize) + def selectedSize = toggle.preferredSize + toggle.selected = false + assertEquals(selectedSize, toggle.preferredSize) + toggle.selected = true + } + } + } finally { + I18n.instance.locale = original + } + } + } + + @Test + void selectedToggleIconHonorsADistinctThemeSelectionColor() { + withThemes { + UIDefaults defaults = UIManager.lookAndFeelDefaults + Object original = defaults.get('ToggleButton.selectedForeground') + try { + defaults.put('ToggleButton.selectedForeground', Color.MAGENTA) + JToggleButton toggle = VoucherToolbar.advanceToggle() + assertIconColor(toggle, Color.MAGENTA) + toggle.selected = false + assertIconColor(toggle, toggle.foreground) + } finally { + defaults.put('ToggleButton.selectedForeground', original) + } + } + } + + private static void withThemes(Closure test) { + SwingUtilities.invokeAndWait { + LookAndFeel original = UIManager.lookAndFeel + try { + [new FlatLightLaf(), new FlatDarkLaf()].each { LookAndFeel theme -> + UIManager.setLookAndFeel(theme) + test.call() + } + } finally { + UIManager.setLookAndFeel(original) + } + } + } + + private static void assertIconColor(AbstractButton button, Color color) { + Icon icon = button.enabled ? button.icon : button.disabledIcon + BufferedImage image = new BufferedImage(icon.iconWidth, icon.iconHeight, BufferedImage.TYPE_INT_ARGB) + Graphics2D graphics = image.createGraphics() + try { + icon.paintIcon(button, graphics, 0, 0) + } finally { + graphics.dispose() + } + assertTrue(containsColor(image, color), "Icon must render with ${color}".toString()) + } + + private static boolean containsColor(BufferedImage image, Color color) { + for (int y = 0; y < image.height; y++) { + for (int x = 0; x < image.width; x++) { + Color pixel = new Color(image.getRGB(x, y), true) + if (pixel.alpha > 0 && pixel.red == color.red && pixel.green == color.green && pixel.blue == color.blue) { + return true + } + } + } + false + } +} diff --git a/docs/screenshots/issue-125/voucher-toolbar-dark.png b/docs/screenshots/issue-125/voucher-toolbar-dark.png new file mode 100644 index 00000000..0d9c7aeb Binary files /dev/null and b/docs/screenshots/issue-125/voucher-toolbar-dark.png differ diff --git a/docs/screenshots/issue-125/voucher-toolbar-light.png b/docs/screenshots/issue-125/voucher-toolbar-light.png new file mode 100644 index 00000000..bc970a0d Binary files /dev/null and b/docs/screenshots/issue-125/voucher-toolbar-light.png differ diff --git a/docs/screenshots/issue-125/voucher-toolbar-narrow-sv.png b/docs/screenshots/issue-125/voucher-toolbar-narrow-sv.png new file mode 100644 index 00000000..285d0d1e Binary files /dev/null and b/docs/screenshots/issue-125/voucher-toolbar-narrow-sv.png differ diff --git a/docs/screenshots/issue-125/voucher-toolbar-saved-light.png b/docs/screenshots/issue-125/voucher-toolbar-saved-light.png new file mode 100644 index 00000000..bff8c941 Binary files /dev/null and b/docs/screenshots/issue-125/voucher-toolbar-saved-light.png differ diff --git a/docs/screenshots/issue-125/voucher-toolbar-windows-dark.png b/docs/screenshots/issue-125/voucher-toolbar-windows-dark.png new file mode 100644 index 00000000..225d2203 Binary files /dev/null and b/docs/screenshots/issue-125/voucher-toolbar-windows-dark.png differ diff --git a/docs/screenshots/issue-125/voucher-toolbar-windows-light.png b/docs/screenshots/issue-125/voucher-toolbar-windows-light.png new file mode 100644 index 00000000..3e497c25 Binary files /dev/null and b/docs/screenshots/issue-125/voucher-toolbar-windows-light.png differ diff --git a/docs/screenshots/issue-125/voucher-toolbar-windows-narrow-sv.png b/docs/screenshots/issue-125/voucher-toolbar-windows-narrow-sv.png new file mode 100644 index 00000000..f18b4229 Binary files /dev/null and b/docs/screenshots/issue-125/voucher-toolbar-windows-narrow-sv.png differ diff --git a/docs/screenshots/issue-125/voucher-toolbar-windows-saved-light.png b/docs/screenshots/issue-125/voucher-toolbar-windows-saved-light.png new file mode 100644 index 00000000..2dc6b1da Binary files /dev/null and b/docs/screenshots/issue-125/voucher-toolbar-windows-saved-light.png differ