2
0

Some minor code improvements.

This commit is contained in:
Maarten Billemont 2016-10-29 08:18:20 -04:00
parent 77bb2ef4cd
commit 0498a245c6
2 changed files with 50 additions and 10 deletions

View File

@ -1,14 +1,16 @@
package com.lyndir.masterpassword.gui; 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 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.FluentIterable;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.primitives.UnsignedInteger; import com.google.common.primitives.UnsignedInteger;
import com.google.common.util.concurrent.*; import com.google.common.util.concurrent.*;
import com.lyndir.lhunath.opal.system.util.PredicateNN;
import com.lyndir.masterpassword.*; import com.lyndir.masterpassword.*;
import com.lyndir.masterpassword.gui.util.Components; import com.lyndir.masterpassword.gui.util.Components;
import com.lyndir.masterpassword.gui.util.UnsignedIntegerModel;
import java.awt.*; import java.awt.*;
import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.StringSelection;
import java.awt.event.*; import java.awt.event.*;
@ -31,6 +33,7 @@ public class PasswordFrame extends JFrame implements DocumentListener {
private final JComboBox<MPSiteType> siteTypeField; private final JComboBox<MPSiteType> siteTypeField;
private final JComboBox<MasterKey.Version> siteVersionField; private final JComboBox<MasterKey.Version> siteVersionField;
private final JSpinner siteCounterField; private final JSpinner siteCounterField;
private final UnsignedIntegerModel siteCounterModel;
private final JPasswordField passwordField; private final JPasswordField passwordField;
private final JLabel tipLabel; private final JLabel tipLabel;
private final JCheckBox maskPasswordField; private final JCheckBox maskPasswordField;
@ -112,14 +115,14 @@ public class PasswordFrame extends JFrame implements DocumentListener {
sitePanel.add( Components.stud() ); sitePanel.add( Components.stud() );
// Site Type & Counter // Site Type & Counter
siteCounterModel = new UnsignedIntegerModel( UnsignedInteger.ONE, UnsignedInteger.ONE );
MPSiteType[] types = Iterables.toArray( MPSiteType.forClass( MPSiteTypeClass.Generated ), MPSiteType.class ); 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 ), // siteTypeField = Components.comboBox( types ), //
Components.stud(), // Components.stud(), //
siteVersionField = Components.comboBox( MasterKey.Version.values() ), // siteVersionField = Components.comboBox( MasterKey.Version.values() ), //
Components.stud(), // Components.stud(), //
siteCounterField = Components.spinner( siteCounterField = Components.spinner( siteCounterModel ) );
new SpinnerNumberModel( UnsignedInteger.ONE, UnsignedInteger.ONE, UnsignedInteger.MAX_VALUE, UnsignedInteger.ONE ) ) );
sitePanel.add( siteSettings ); sitePanel.add( siteSettings );
siteTypeField.setFont( Res.valueFont().deriveFont( 12f ) ); siteTypeField.setFont( Res.valueFont().deriveFont( 12f ) );
siteTypeField.setSelectedItem( MPSiteType.GeneratedLong ); siteTypeField.setSelectedItem( MPSiteType.GeneratedLong );
@ -212,17 +215,17 @@ public class PasswordFrame extends JFrame implements DocumentListener {
final MPSiteType siteType = siteTypeField.getModel().getElementAt( siteTypeField.getSelectedIndex() ); final MPSiteType siteType = siteTypeField.getModel().getElementAt( siteTypeField.getSelectedIndex() );
final MasterKey.Version siteVersion = siteVersionField.getItemAt( siteVersionField.getSelectedIndex() ); final MasterKey.Version siteVersion = siteVersionField.getItemAt( siteVersionField.getSelectedIndex() );
final UnsignedInteger siteCounter = UnsignedInteger.valueOf( ((Number) siteCounterField.getValue()).longValue() ); final UnsignedInteger siteCounter = siteCounterModel.getNumber();
Iterable<Site> siteResults = user.findSitesByName( siteNameQuery ); Iterable<Site> siteResults = user.findSitesByName( siteNameQuery );
if (!allowNameCompletion) if (!allowNameCompletion)
siteResults = FluentIterable.from( siteResults ).filter( new PredicateNN<Site>() { siteResults = FluentIterable.from( siteResults ).filter( new Predicate<Site>() {
@Override @Override
public boolean apply(Site input) { public boolean apply(@Nullable Site siteResult) {
return siteNameQuery.equals( input.getSiteName() ); return siteResult != null && siteNameQuery.equals( siteResult.getSiteName() );
} }
} ); } );
final Site site = Iterables.getFirst( siteResults, final Site site = ifNotNullElse( Iterables.getFirst( siteResults, null ),
new IncognitoSite( siteNameQuery, siteType, siteCounter, siteVersion ) ); new IncognitoSite( siteNameQuery, siteType, siteCounter, siteVersion ) );
if (currentSite != null && currentSite.getSiteName().equals( site.getSiteName() )) { if (currentSite != null && currentSite.getSiteName().equals( site.getSiteName() )) {
site.setSiteType( siteType ); site.setSiteType( siteType );

View File

@ -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();
}
}