Skip to content

Commit

Permalink
Fixed regression in DateSpinner
Browse files Browse the repository at this point in the history
A change was made to spinners here: cfac9a6
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.
  • Loading branch information
shai-almog committed Feb 21, 2024
1 parent cb2244b commit 7126a57
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CodenameOne/src/com/codename1/ui/spinner/Spinner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object>());
Spinner s = new Spinner(new SpinnerNumberModel(min, max, currentValue, step, 0), new SpinnerRenderer<Object>());
s.setRenderingPrototype(new Integer(max * 10));
return s;
}
Expand Down
17 changes: 16 additions & 1 deletion CodenameOne/src/com/codename1/ui/spinner/SpinnerNumberModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}


Expand Down

0 comments on commit 7126a57

Please sign in to comment.