diff --git a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/util/Components.java b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/util/Components.java index a3d7ba04..229bd22b 100644 --- a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/util/Components.java +++ b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/util/Components.java @@ -31,6 +31,7 @@ import javax.swing.border.Border; import javax.swing.border.CompoundBorder; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; +import javax.swing.text.DefaultFormatterFactory; /** @@ -310,6 +311,10 @@ public abstract class Components { CompoundBorder editorBorder = BorderFactory.createCompoundBorder( BorderFactory.createLineBorder( Res.colors().controlBorder(), 1, true ), 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 ); setAlignmentX( LEFT_ALIGNMENT ); setBorder( null ); diff --git a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/util/UnsignedIntegerModel.java b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/util/UnsignedIntegerModel.java index 02fa57fb..68e24dc4 100644 --- a/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/util/UnsignedIntegerModel.java +++ b/platform-independent/java/gui/src/main/java/com/lyndir/masterpassword/gui/util/UnsignedIntegerModel.java @@ -19,20 +19,18 @@ package com.lyndir.masterpassword.gui.util; import com.google.common.primitives.UnsignedInteger; +import java.text.ParseException; import java.util.function.Consumer; import javax.annotation.Nullable; import javax.swing.*; -import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; /** * @author lhunath, 2016-10-29 */ -public class UnsignedIntegerModel extends SpinnerNumberModel -implements Selectable { - - private static final long serialVersionUID = 1L; +@SuppressWarnings("serial") +public class UnsignedIntegerModel extends SpinnerNumberModel implements Selectable { @Nullable private ChangeListener changeListener; @@ -64,6 +62,37 @@ implements Selectable { 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 public UnsignedIntegerModel selection(@Nullable final Consumer selectionConsumer) { if (changeListener != null) { @@ -89,4 +118,22 @@ implements Selectable { setValue( selectedItem ); 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; + } + }; + } } diff --git a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileSite.java b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileSite.java index 974f89fa..bb6200b1 100644 --- a/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileSite.java +++ b/platform-independent/java/model/src/main/java/com/lyndir/masterpassword/model/impl/MPFileSite.java @@ -75,7 +75,9 @@ public class MPFileSite extends MPBasicSite { 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)) return;