From 9ef0f78617b3167dd17406063dc5675800e0cda8 Mon Sep 17 00:00:00 2001 From: Zsombor Gegesy Date: Wed, 13 Apr 2022 00:11:04 +0200 Subject: [PATCH] Automatically update the configuration when the value has been changed --- .../com/rarchives/ripme/ui/MainWindow.java | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/rarchives/ripme/ui/MainWindow.java b/src/main/java/com/rarchives/ripme/ui/MainWindow.java index 96208632..1916053c 100644 --- a/src/main/java/com/rarchives/ripme/ui/MainWindow.java +++ b/src/main/java/com/rarchives/ripme/ui/MainWindow.java @@ -500,9 +500,9 @@ public final class MainWindow implements Runnable, RipStatusHandler { configThreadsLabel = new JLabel(Utils.getLocalizedString("max.download.threads") + ":", JLabel.RIGHT); configTimeoutLabel = new JLabel(Utils.getLocalizedString("timeout.mill"), JLabel.RIGHT); configRetriesLabel = new JLabel(Utils.getLocalizedString("retry.download.count"), JLabel.RIGHT); - configThreadsText = new JTextField(Integer.toString(Utils.getConfigInteger("threads.size", 3))); - configTimeoutText = new JTextField(Integer.toString(Utils.getConfigInteger("download.timeout", 60000))); - configRetriesText = new JTextField(Integer.toString(Utils.getConfigInteger("download.retries", 3))); + configThreadsText = configField("threads.size", 3); + configTimeoutText = configField("download.timeout", 60000); + configRetriesText = configField("download.retries", 3); configOverwriteCheckbox = addNewCheckbox(Utils.getLocalizedString("overwrite.existing.files"), "file.overwrite", false); configAutoupdateCheckbox = addNewCheckbox(Utils.getLocalizedString("auto.update"), "auto.update", true); @@ -587,6 +587,39 @@ public final class MainWindow implements Runnable, RipStatusHandler { gbc.fill = GridBagConstraints.HORIZONTAL; } + private JTextField configField(String key, int defaultValue) { + final var field = new JTextField(Integer.toString(Utils.getConfigInteger(key, defaultValue))); + field.getDocument().addDocumentListener(new DocumentListener() { + + @Override + public void insertUpdate(DocumentEvent e) { + checkAndUpdate(); + } + + @Override + public void removeUpdate(DocumentEvent e) { + checkAndUpdate(); + } + + @Override + public void changedUpdate(DocumentEvent e) { + checkAndUpdate(); + } + + private void checkAndUpdate() { + final var txt = field.getText(); + try { + final var newValue = Integer.parseInt(txt); + if (newValue>0) { + Utils.setConfigInteger(key, newValue); + } + } catch (final Exception e) { + } + } + }); + return field; + } + private void addItemToConfigGridBagConstraints(GridBagConstraints gbc, int gbcYValue, JLabel thing1ToAdd, JButton thing2ToAdd) { gbc.gridy = gbcYValue;