|
| 1 | +/* |
| 2 | + Copyright 2026 Will Winder |
| 3 | +
|
| 4 | + This file is part of Universal Gcode Sender (UGS). |
| 5 | +
|
| 6 | + UGS is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + UGS is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with UGS. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | +package com.willwinder.ugs.nbp.core.actions; |
| 20 | + |
| 21 | +import com.willwinder.universalgcodesender.i18n.Localization; |
| 22 | +import com.willwinder.universalgcodesender.model.BackendAPI; |
| 23 | +import com.willwinder.universalgcodesender.utils.Settings; |
| 24 | +import com.willwinder.universalgcodesender.utils.SettingsFactory; |
| 25 | + |
| 26 | +import javax.swing.BoxLayout; |
| 27 | +import javax.swing.JCheckBox; |
| 28 | +import javax.swing.JLabel; |
| 29 | +import javax.swing.JOptionPane; |
| 30 | +import javax.swing.JPanel; |
| 31 | + |
| 32 | +/** |
| 33 | + * Shared helper that asks the user to confirm before resetting the work |
| 34 | + * coordinate zero. The confirmation can be permanently dismissed through a |
| 35 | + * "Don't ask again" checkbox which is persisted to the application settings. |
| 36 | + * <p> |
| 37 | + * The maintainer requested this behavior in |
| 38 | + * <a href="https://github.com/winder/Universal-G-Code-Sender/issues/3040">issue #3040</a>: |
| 39 | + * a confirmation box with an option to hide future questions, defaulting to ask. |
| 40 | + */ |
| 41 | +public final class ResetZeroConfirmation { |
| 42 | + |
| 43 | + private ResetZeroConfirmation() { |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns {@code true} if the reset-zero action should proceed. |
| 48 | + * <p> |
| 49 | + * When the user has opted out of the confirmation |
| 50 | + * ({@link Settings#isConfirmResetZero()} is {@code false}) this returns |
| 51 | + * {@code true} immediately without showing a dialog. Otherwise it shows a |
| 52 | + * yes/no confirmation dialog with a "Don't ask again" checkbox; checking the |
| 53 | + * box persists the opt-out to the settings. |
| 54 | + * |
| 55 | + * @param backend the active backend, used to read and persist settings |
| 56 | + * @return {@code true} if the user confirmed (or confirmation is disabled), |
| 57 | + * {@code false} if the user cancelled |
| 58 | + */ |
| 59 | + public static boolean confirmResetZero(BackendAPI backend) { |
| 60 | + Settings settings = backend.getSettings(); |
| 61 | + if (settings == null || !settings.isConfirmResetZero()) { |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + JPanel panel = new JPanel(); |
| 66 | + panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); |
| 67 | + panel.add(new JLabel(Localization.getString("platform.actions.resetZero.confirm.message"))); |
| 68 | + JCheckBox dontAsk = new JCheckBox(Localization.getString("platform.actions.resetZero.confirm.dontAsk")); |
| 69 | + panel.add(dontAsk); |
| 70 | + |
| 71 | + int result = JOptionPane.showConfirmDialog( |
| 72 | + null, |
| 73 | + panel, |
| 74 | + Localization.getString("platform.actions.resetZero.confirm.title"), |
| 75 | + JOptionPane.YES_NO_OPTION, |
| 76 | + JOptionPane.QUESTION_MESSAGE); |
| 77 | + |
| 78 | + if (dontAsk.isSelected()) { |
| 79 | + settings.setConfirmResetZero(false); |
| 80 | + SettingsFactory.saveSettings(settings); |
| 81 | + } |
| 82 | + |
| 83 | + return result == JOptionPane.YES_OPTION; |
| 84 | + } |
| 85 | +} |
0 commit comments