2
0

Fix issues with UnsignedIntegerModel in a spinner.

This commit is contained in:
Maarten Billemont 2018-07-29 01:38:58 -04:00
parent a6e9e89ace
commit 18ecc41b39
3 changed files with 60 additions and 6 deletions

View File

@ -31,6 +31,7 @@ import javax.swing.border.Border;
import javax.swing.border.CompoundBorder; import javax.swing.border.CompoundBorder;
import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener; import javax.swing.event.DocumentListener;
import javax.swing.text.DefaultFormatterFactory;
/** /**
@ -310,6 +311,10 @@ public abstract class Components {
CompoundBorder editorBorder = BorderFactory.createCompoundBorder( CompoundBorder editorBorder = BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder( Res.colors().controlBorder(), 1, true ), BorderFactory.createLineBorder( Res.colors().controlBorder(), 1, true ),
BorderFactory.createEmptyBorder( 4, 4, 4, 4 ) ); BorderFactory.createEmptyBorder( 4, 4, 4, 4 ) );
DefaultFormatterFactory formatterFactory = new DefaultFormatterFactory();
if (model instanceof UnsignedIntegerModel)
formatterFactory.setDefaultFormatter( ((UnsignedIntegerModel)model).getFormatter() );
((DefaultEditor) getEditor()).getTextField().setFormatterFactory( formatterFactory );
((DefaultEditor) getEditor()).getTextField().setBorder( editorBorder ); ((DefaultEditor) getEditor()).getTextField().setBorder( editorBorder );
setAlignmentX( LEFT_ALIGNMENT ); setAlignmentX( LEFT_ALIGNMENT );
setBorder( null ); setBorder( null );

View File

@ -19,20 +19,18 @@
package com.lyndir.masterpassword.gui.util; package com.lyndir.masterpassword.gui.util;
import com.google.common.primitives.UnsignedInteger; import com.google.common.primitives.UnsignedInteger;
import java.text.ParseException;
import java.util.function.Consumer; import java.util.function.Consumer;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener; import javax.swing.event.ChangeListener;
/** /**
* @author lhunath, 2016-10-29 * @author lhunath, 2016-10-29
*/ */
public class UnsignedIntegerModel extends SpinnerNumberModel @SuppressWarnings("serial")
implements Selectable<UnsignedInteger, UnsignedIntegerModel> { public class UnsignedIntegerModel extends SpinnerNumberModel implements Selectable<UnsignedInteger, UnsignedIntegerModel> {
private static final long serialVersionUID = 1L;
@Nullable @Nullable
private ChangeListener changeListener; private ChangeListener changeListener;
@ -64,6 +62,37 @@ implements Selectable<UnsignedInteger, UnsignedIntegerModel> {
return (UnsignedInteger) super.getNumber(); return (UnsignedInteger) super.getNumber();
} }
@Override
public UnsignedInteger getMinimum() {
return (UnsignedInteger) super.getMinimum();
}
@Override
public UnsignedInteger getMaximum() {
return (UnsignedInteger) super.getMaximum();
}
@Override
public UnsignedInteger getStepSize() {
return (UnsignedInteger) super.getStepSize();
}
@Override
public UnsignedInteger getNextValue() {
if ((getMaximum() == null) || (getMaximum().compareTo( getNumber() ) > 0))
return getNumber().plus( getStepSize() );
return getMaximum();
}
@Override
public UnsignedInteger getPreviousValue() {
if ((getMinimum() == null) || (getMinimum().compareTo( getNumber() ) < 0))
return getNumber().minus( getStepSize() );
return getMinimum();
}
@Override @Override
public UnsignedIntegerModel selection(@Nullable final Consumer<UnsignedInteger> selectionConsumer) { public UnsignedIntegerModel selection(@Nullable final Consumer<UnsignedInteger> selectionConsumer) {
if (changeListener != null) { if (changeListener != null) {
@ -89,4 +118,22 @@ implements Selectable<UnsignedInteger, UnsignedIntegerModel> {
setValue( selectedItem ); setValue( selectedItem );
return selection( selectionConsumer ); return selection( selectionConsumer );
} }
public JFormattedTextField.AbstractFormatter getFormatter() {
return new JFormattedTextField.AbstractFormatter() {
@Override
@Nullable
public Object stringToValue(@Nullable final String text)
throws ParseException {
return (text != null)? UnsignedInteger.valueOf( text ): null;
}
@Override
@Nullable
public String valueToString(final Object value)
throws ParseException {
return (value != null)? value.toString(): null;
}
};
}
} }

View File

@ -75,7 +75,9 @@ public class MPFileSite extends MPBasicSite<MPFileUser, MPFileQuestion> {
return url; return url;
} }
public void setUrl(@Nullable final String url) { public void setUrl(@Nullable String url) {
if ((url != null) && url.isEmpty())
url = null;
if (Objects.equals( this.url, url)) if (Objects.equals( this.url, url))
return; return;