From 7126a578cb8dcf435c9c2f2dc61c0dc9a5528182 Mon Sep 17 00:00:00 2001 From: Shai Almog <67850168+shai-almog@users.noreply.github.com> Date: Wed, 21 Feb 2024 09:18:43 +0200 Subject: [PATCH] Fixed regression in DateSpinner A change was made to spinners here: https://github.com/codenameone/CodenameOne/commit/cfac9a6a1bb15027b48a9b822e2f21eb2835d38e that caused DateSpinner to have 32 days per month and December appeared twice. This change is needed for Picker so it can't be reverted, but this workaround restores the functionality of old code that relies on DateSpinner. --- .../src/com/codename1/ui/spinner/Spinner.java | 2 +- .../ui/spinner/SpinnerNumberModel.java | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CodenameOne/src/com/codename1/ui/spinner/Spinner.java b/CodenameOne/src/com/codename1/ui/spinner/Spinner.java index 90b7a6997a..fed4cb870a 100644 --- a/CodenameOne/src/com/codename1/ui/spinner/Spinner.java +++ b/CodenameOne/src/com/codename1/ui/spinner/Spinner.java @@ -137,7 +137,7 @@ public static Spinner createDate(long min, long max, long currentValue, char sep * @deprecated use NumericSpinner */ public static Spinner create(int min, int max, int currentValue, int step) { - Spinner s = new Spinner(new SpinnerNumberModel(min, max, currentValue, step), new SpinnerRenderer()); + Spinner s = new Spinner(new SpinnerNumberModel(min, max, currentValue, step, 0), new SpinnerRenderer()); s.setRenderingPrototype(new Integer(max * 10)); return s; } diff --git a/CodenameOne/src/com/codename1/ui/spinner/SpinnerNumberModel.java b/CodenameOne/src/com/codename1/ui/spinner/SpinnerNumberModel.java index 2433b219ee..16d0120de1 100644 --- a/CodenameOne/src/com/codename1/ui/spinner/SpinnerNumberModel.java +++ b/CodenameOne/src/com/codename1/ui/spinner/SpinnerNumberModel.java @@ -44,6 +44,13 @@ class SpinnerNumberModel implements ListModel { private double step; boolean realValues; + /** + * The old DateSpinner relies on behavior that was broken in this commit: + * https://github.com/codenameone/CodenameOne/commit/cfac9a6a1bb15027b48a9b822e2f21eb2835d38e#diff-d12531ab4b0dd8bf1233a09f3c5e2b2b5634bff3c3cd2f357ad0a001e5f19bbf + * This is a workaround to preserve compatibility + */ + private int maxOffset = 1; + private boolean setSelectedIndexReentrantLock; void setValue(Object value) { @@ -80,6 +87,14 @@ public SpinnerNumberModel(int min, int max, int currentValue, int step) { this.step = step; } + SpinnerNumberModel(int min, int max, int currentValue, int step, int maxOffset) { + this.max = max; + this.min = min; + this.currentValue = currentValue; + this.step = step; + this.maxOffset = maxOffset; + } + /** * Indicates the range of the spinner * @@ -111,7 +126,7 @@ public Object getItemAt(int index) { * {@inheritDoc} */ public int getSize() { - return (int)((max - min) / step) + 1; + return (int)((max - min) / step) + maxOffset; }