2
0

Some fixes to the Java GUI. Thanks cuardin!

[UPDATED]   Allow lookup by short name.
[FIXED]     GUI only supports generated types, only show generated types in its list.
[UPDATED]   Update site password when type changes.
[FIXED]     Exceptions on illegal input arguments for generation, eg. empty site name.
This commit is contained in:
Maarten Billemont 2014-07-01 12:41:35 -04:00
parent b84c9b79c8
commit 2eb074be87
2 changed files with 63 additions and 23 deletions

View File

@ -19,8 +19,10 @@ public enum MPElementType {
GeneratedShort( "Short Password", "Short", "Copy-friendly, 4 characters, no symbols.", MPElementTypeClass.Generated ), GeneratedShort( "Short Password", "Short", "Copy-friendly, 4 characters, no symbols.", MPElementTypeClass.Generated ),
GeneratedPIN( "PIN", "PIN", "4 numbers.", MPElementTypeClass.Generated ), GeneratedPIN( "PIN", "PIN", "4 numbers.", MPElementTypeClass.Generated ),
StoredPersonal( "Personal Password", "Personal", "AES-encrypted, exportable.", MPElementTypeClass.Stored, MPElementFeature.ExportContent ), StoredPersonal( "Personal Password", "Personal", "AES-encrypted, exportable.", MPElementTypeClass.Stored,
StoredDevicePrivate( "Device Private Password", "Private", "AES-encrypted, not exported.", MPElementTypeClass.Stored, MPElementFeature.DevicePrivate ); MPElementFeature.ExportContent ),
StoredDevicePrivate( "Device Private Password", "Private", "AES-encrypted, not exported.", MPElementTypeClass.Stored,
MPElementFeature.DevicePrivate );
static final Logger logger = Logger.get( MPElementType.class ); static final Logger logger = Logger.get( MPElementType.class );
@ -30,7 +32,8 @@ public enum MPElementType {
private final String shortName; private final String shortName;
private final String description; private final String description;
MPElementType(final String name, final String shortName, final String description, final MPElementTypeClass typeClass, final MPElementFeature... typeFeatures) { MPElementType(final String name, final String shortName, final String description, final MPElementTypeClass typeClass,
final MPElementFeature... typeFeatures) {
this.name = name; this.name = name;
this.shortName = shortName; this.shortName = shortName;
@ -38,8 +41,9 @@ public enum MPElementType {
this.description = description; this.description = description;
ImmutableSet.Builder<MPElementFeature> typeFeaturesBuilder = ImmutableSet.builder(); ImmutableSet.Builder<MPElementFeature> typeFeaturesBuilder = ImmutableSet.builder();
for (final MPElementFeature typeFeature : typeFeatures) for (final MPElementFeature typeFeature : typeFeatures) {
typeFeaturesBuilder.add( typeFeature ); typeFeaturesBuilder.add( typeFeature );
}
this.typeFeatures = typeFeaturesBuilder.build(); this.typeFeatures = typeFeaturesBuilder.build();
} }
@ -68,12 +72,37 @@ public enum MPElementType {
return description; return description;
} }
/**
* @param name The full or short name of the type we want to look up. It is matched case insensitively.
*
* @return The type with the given name.
*/
public static MPElementType forName(final String name) { public static MPElementType forName(final String name) {
for (final MPElementType type : values()) for (final MPElementType type : values()) {
if (type.getName().equals( name )) if (type.getName().equalsIgnoreCase( name ) || type.getShortName().equalsIgnoreCase( name )) {
return type; return type;
}
}
throw logger.bug( "Element type not known: %s", name ); throw logger.bug( "Element type not known: %s", name );
} }
/**
* @param typeClass The class for which we look up types.
*
* @return All types that support the given class.
*/
public static ImmutableSet<MPElementType> forClass(final MPElementTypeClass typeClass) {
ImmutableSet.Builder<MPElementType> types = ImmutableSet.builder();
for (final MPElementType type : values()) {
if (type.getTypeClass() == typeClass) {
types.add( type );
}
}
return types.build();
}
} }

View File

@ -2,14 +2,11 @@ package com.lyndir.lhunath.masterpassword;
import static com.lyndir.lhunath.opal.system.util.StringUtils.*; import static com.lyndir.lhunath.opal.system.util.StringUtils.*;
import com.google.common.io.Resources; import com.google.common.collect.Iterables;
import com.lyndir.lhunath.masterpassword.util.Components; import com.lyndir.lhunath.masterpassword.util.Components;
import java.awt.*; import java.awt.*;
import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent; import java.awt.event.*;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.*; import javax.swing.border.*;
import javax.swing.event.*; import javax.swing.event.*;
@ -83,9 +80,11 @@ public class PasswordFrame extends JFrame implements DocumentListener {
} ); } );
// Site Type & Counter // Site Type & Counter
MPElementType[] types = Iterables.toArray( MPElementType.forClass( MPElementTypeClass.Generated ), MPElementType.class );
JComponent siteSettings = Components.boxLayout( BoxLayout.LINE_AXIS, // JComponent siteSettings = Components.boxLayout( BoxLayout.LINE_AXIS, //
siteTypeField = new JComboBox<>( MPElementType.values() ), siteTypeField = new JComboBox<>( types ), //
siteCounterField = new JSpinner( new SpinnerNumberModel( 1, 1, Integer.MAX_VALUE, 1 ) ) { siteCounterField = new JSpinner(
new SpinnerNumberModel( 1, 1, Integer.MAX_VALUE, 1 ) ) {
@Override @Override
public Dimension getMaximumSize() { public Dimension getMaximumSize() {
return new Dimension( 20, getPreferredSize().height ); return new Dimension( 20, getPreferredSize().height );
@ -96,6 +95,13 @@ public class PasswordFrame extends JFrame implements DocumentListener {
siteTypeField.setAlignmentX( LEFT_ALIGNMENT ); siteTypeField.setAlignmentX( LEFT_ALIGNMENT );
siteTypeField.setAlignmentY( CENTER_ALIGNMENT ); siteTypeField.setAlignmentY( CENTER_ALIGNMENT );
siteTypeField.setSelectedItem( MPElementType.GeneratedLong ); siteTypeField.setSelectedItem( MPElementType.GeneratedLong );
siteTypeField.addItemListener( new ItemListener() {
@Override
public void itemStateChanged(final ItemEvent e) {
updatePassword( null );
}
} );
siteCounterField.setAlignmentX( RIGHT_ALIGNMENT ); siteCounterField.setAlignmentX( RIGHT_ALIGNMENT );
siteCounterField.setAlignmentY( CENTER_ALIGNMENT ); siteCounterField.setAlignmentY( CENTER_ALIGNMENT );
siteCounterField.addChangeListener( new ChangeListener() { siteCounterField.addChangeListener( new ChangeListener() {
@ -124,6 +130,11 @@ public class PasswordFrame extends JFrame implements DocumentListener {
final String siteName = siteNameField.getText(); final String siteName = siteNameField.getText();
final int siteCounter = (Integer) siteCounterField.getValue(); final int siteCounter = (Integer) siteCounterField.getValue();
if (siteType.getTypeClass() != MPElementTypeClass.Generated || siteName == null || siteName.isEmpty() || !user.hasKey()) {
passwordLabel.setText( null );
return;
}
Res.execute( new Runnable() { Res.execute( new Runnable() {
@Override @Override
public void run() { public void run() {