From 5935365c5971edcf97ca3ccb871358d6e205de15 Mon Sep 17 00:00:00 2001 From: Hydrox6 Date: Tue, 18 Feb 2020 22:43:00 +0000 Subject: [PATCH] config: fix entering values directly into options with units not working --- .../client/plugins/config/ConfigPanel.java | 7 +- .../client/plugins/config/UnitFormatter.java | 86 +++++++++++++++++++ 2 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/config/UnitFormatter.java diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/config/ConfigPanel.java b/runelite-client/src/main/java/net/runelite/client/plugins/config/ConfigPanel.java index 6e6b49a774..20c04f79fd 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/config/ConfigPanel.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/config/ConfigPanel.java @@ -36,7 +36,6 @@ import java.awt.event.ItemEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; -import java.text.DecimalFormat; import javax.inject.Inject; import javax.swing.BorderFactory; import javax.swing.ImageIcon; @@ -262,11 +261,7 @@ class ConfigPanel extends PluginPanel Units units = cid.getUnits(); if (units != null) { - DecimalFormat df = ((JSpinner.NumberEditor) spinner.getEditor()).getFormat(); - df.setPositiveSuffix(units.value()); - df.setNegativeSuffix(units.value()); - // Force update the spinner to have it add the units initially - spinnerTextField.setValue(value); + spinnerTextField.setFormatterFactory(new UnitFormatterFactory(units)); } item.add(spinner, BorderLayout.EAST); diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/config/UnitFormatter.java b/runelite-client/src/main/java/net/runelite/client/plugins/config/UnitFormatter.java new file mode 100644 index 0000000000..4945a4a65e --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/config/UnitFormatter.java @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2020, Hydrox6 + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.plugins.config; + +import java.text.ParseException; +import java.util.HashMap; +import java.util.Map; +import javax.swing.JFormattedTextField; +import lombok.RequiredArgsConstructor; +import net.runelite.client.config.Units; + +final class UnitFormatter extends JFormattedTextField.AbstractFormatter +{ + private final String units; + + UnitFormatter(Units units) + { + this.units = units.value(); + } + + @Override + public Object stringToValue(final String text) throws ParseException + { + final String trimmedText; + + // Using the spinner controls causes the value to have the unit on the end, so remove it + if (text.endsWith(units)) + { + trimmedText = text.substring(0, text.length() - units.length()); + } + else + { + trimmedText = text; + } + + try + { + return Integer.valueOf(trimmedText); + } + catch (NumberFormatException e) + { + throw new ParseException(trimmedText + " is not an integer.", 0); + } + } + + @Override + public String valueToString(final Object value) + { + return value + units; + } +} + +@RequiredArgsConstructor +final class UnitFormatterFactory extends JFormattedTextField.AbstractFormatterFactory +{ + private final Units units; + private final Map formatters = new HashMap<>(); + + @Override + public JFormattedTextField.AbstractFormatter getFormatter(final JFormattedTextField tf) + { + return formatters.computeIfAbsent(tf, (key) -> new UnitFormatter(units)); + } +} \ No newline at end of file