diff --git a/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/masterpassword/gui/PasswordFrame.java b/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/masterpassword/gui/PasswordFrame.java index 0f2127de..6aabcb01 100755 --- a/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/masterpassword/gui/PasswordFrame.java +++ b/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/masterpassword/gui/PasswordFrame.java @@ -1,14 +1,16 @@ package com.lyndir.masterpassword.gui; +import static com.lyndir.lhunath.opal.system.util.ObjectUtils.ifNotNullElse; import static com.lyndir.lhunath.opal.system.util.StringUtils.*; +import com.google.common.base.Predicate; import com.google.common.collect.FluentIterable; import com.google.common.collect.Iterables; import com.google.common.primitives.UnsignedInteger; import com.google.common.util.concurrent.*; -import com.lyndir.lhunath.opal.system.util.PredicateNN; import com.lyndir.masterpassword.*; import com.lyndir.masterpassword.gui.util.Components; +import com.lyndir.masterpassword.gui.util.UnsignedIntegerModel; import java.awt.*; import java.awt.datatransfer.StringSelection; import java.awt.event.*; @@ -31,6 +33,7 @@ public class PasswordFrame extends JFrame implements DocumentListener { private final JComboBox siteTypeField; private final JComboBox siteVersionField; private final JSpinner siteCounterField; + private final UnsignedIntegerModel siteCounterModel; private final JPasswordField passwordField; private final JLabel tipLabel; private final JCheckBox maskPasswordField; @@ -112,14 +115,14 @@ public class PasswordFrame extends JFrame implements DocumentListener { sitePanel.add( Components.stud() ); // Site Type & Counter + siteCounterModel = new UnsignedIntegerModel( UnsignedInteger.ONE, UnsignedInteger.ONE ); MPSiteType[] types = Iterables.toArray( MPSiteType.forClass( MPSiteTypeClass.Generated ), MPSiteType.class ); - JComponent siteSettings = Components.boxLayout( BoxLayout.LINE_AXIS, // + JComponent siteSettings = Components.boxLayout( BoxLayout.LINE_AXIS, // siteTypeField = Components.comboBox( types ), // Components.stud(), // siteVersionField = Components.comboBox( MasterKey.Version.values() ), // Components.stud(), // - siteCounterField = Components.spinner( - new SpinnerNumberModel( UnsignedInteger.ONE, UnsignedInteger.ONE, UnsignedInteger.MAX_VALUE, UnsignedInteger.ONE ) ) ); + siteCounterField = Components.spinner( siteCounterModel ) ); sitePanel.add( siteSettings ); siteTypeField.setFont( Res.valueFont().deriveFont( 12f ) ); siteTypeField.setSelectedItem( MPSiteType.GeneratedLong ); @@ -212,18 +215,18 @@ public class PasswordFrame extends JFrame implements DocumentListener { final MPSiteType siteType = siteTypeField.getModel().getElementAt( siteTypeField.getSelectedIndex() ); final MasterKey.Version siteVersion = siteVersionField.getItemAt( siteVersionField.getSelectedIndex() ); - final UnsignedInteger siteCounter = UnsignedInteger.valueOf( ((Number) siteCounterField.getValue()).longValue() ); + final UnsignedInteger siteCounter = siteCounterModel.getNumber(); Iterable siteResults = user.findSitesByName( siteNameQuery ); if (!allowNameCompletion) - siteResults = FluentIterable.from( siteResults ).filter( new PredicateNN() { + siteResults = FluentIterable.from( siteResults ).filter( new Predicate() { @Override - public boolean apply(Site input) { - return siteNameQuery.equals( input.getSiteName() ); + public boolean apply(@Nullable Site siteResult) { + return siteResult != null && siteNameQuery.equals( siteResult.getSiteName() ); } } ); - final Site site = Iterables.getFirst( siteResults, - new IncognitoSite( siteNameQuery, siteType, siteCounter, siteVersion ) ); + final Site site = ifNotNullElse( Iterables.getFirst( siteResults, null ), + new IncognitoSite( siteNameQuery, siteType, siteCounter, siteVersion ) ); if (currentSite != null && currentSite.getSiteName().equals( site.getSiteName() )) { site.setSiteType( siteType ); site.setAlgorithmVersion( siteVersion ); diff --git a/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/masterpassword/gui/util/UnsignedIntegerModel.java b/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/masterpassword/gui/util/UnsignedIntegerModel.java new file mode 100644 index 00000000..b294f820 --- /dev/null +++ b/MasterPassword/Java/masterpassword-gui/src/main/java/com/lyndir/masterpassword/gui/util/UnsignedIntegerModel.java @@ -0,0 +1,37 @@ +package com.lyndir.masterpassword.gui.util; + +import com.google.common.primitives.UnsignedInteger; +import javax.swing.*; + + +/** + * @author lhunath, 2016-10-29 + */ +public class UnsignedIntegerModel extends SpinnerNumberModel { + + public UnsignedIntegerModel() { + this( UnsignedInteger.ZERO, UnsignedInteger.ZERO, UnsignedInteger.MAX_VALUE, UnsignedInteger.ONE ); + } + + public UnsignedIntegerModel(final UnsignedInteger value) { + this( value, UnsignedInteger.ZERO, UnsignedInteger.MAX_VALUE, UnsignedInteger.ONE ); + } + + public UnsignedIntegerModel(final UnsignedInteger value, final UnsignedInteger minimum) { + this( value, minimum, UnsignedInteger.MAX_VALUE, UnsignedInteger.ONE ); + } + + public UnsignedIntegerModel(final UnsignedInteger value, final UnsignedInteger minimum, final UnsignedInteger maximum) { + this( value, minimum, maximum, UnsignedInteger.ONE ); + } + + public UnsignedIntegerModel(final UnsignedInteger value, final UnsignedInteger minimum, final UnsignedInteger maximum, + final UnsignedInteger stepSize) { + super( value, minimum, maximum, stepSize ); + } + + @Override + public UnsignedInteger getNumber() { + return (UnsignedInteger) super.getNumber(); + } +}