2
0

Read avatar from user export and allow user to modify it by clicking the picture.

This commit is contained in:
Maarten Billemont 2014-12-12 17:25:32 -05:00
parent 0fdf894bf0
commit 1bd76dbb61
6 changed files with 43 additions and 13 deletions

View File

@ -11,6 +11,7 @@ import javax.swing.*;
public abstract class AuthenticationPanel extends JPanel {
protected final UnlockFrame unlockFrame;
protected final JLabel avatarLabel;
public AuthenticationPanel(final UnlockFrame unlockFrame) {
this.unlockFrame = unlockFrame;
@ -19,7 +20,7 @@ public abstract class AuthenticationPanel extends JPanel {
// Avatar
add( Box.createVerticalGlue() );
add( new JLabel( Res.avatar(0) ) {
add( avatarLabel = new JLabel( Res.avatar( 0 ) ) {
@Override
public Dimension getMaximumSize() {
return new Dimension( Integer.MAX_VALUE, Integer.MAX_VALUE );
@ -29,14 +30,14 @@ public abstract class AuthenticationPanel extends JPanel {
}
protected void updateUser(boolean repack) {
unlockFrame.setUser( getUser() );
unlockFrame.setUser( getSelectedUser() );
validate();
if (repack)
unlockFrame.repack();
}
protected abstract User getUser();
protected abstract User getSelectedUser();
public Component getFocusComponent() {
return null;

View File

@ -62,7 +62,7 @@ public class IncognitoAuthenticationPanel extends AuthenticationPanel implements
}
@Override
protected User getUser() {
protected User getSelectedUser() {
return new IncognitoUser( fullNameField.getText(), new String( masterPasswordField.getPassword() ) );
}

View File

@ -22,9 +22,21 @@ public class ModelAuthenticationPanel extends AuthenticationPanel implements Ite
private final JPasswordField masterPasswordField;
public ModelAuthenticationPanel(final UnlockFrame unlockFrame) {
super( unlockFrame );
// Avatar
avatarLabel.addMouseListener( new MouseAdapter() {
@Override
public void mouseClicked(final MouseEvent e) {
ModelUser selectedUser = getSelectedUser();
if (selectedUser != null) {
selectedUser.setAvatar( selectedUser.getAvatar() + 1 );
updateUser( false );
}
}
} );
// User
super( unlockFrame );
JLabel userLabel = new JLabel( "User:" );
userLabel.setAlignmentX( LEFT_ALIGNMENT );
userLabel.setHorizontalAlignment( SwingConstants.CENTER );
@ -68,9 +80,9 @@ public class ModelAuthenticationPanel extends AuthenticationPanel implements Ite
@Override
protected void updateUser(boolean repack) {
int selectedIndex = userField.getSelectedIndex();
if (selectedIndex >= 0) {
ModelUser selectedUser = userField.getModel().getElementAt( selectedIndex );
ModelUser selectedUser = getSelectedUser();
if (selectedUser != null) {
avatarLabel.setIcon( Res.avatar( selectedUser.getAvatar() ) );
boolean showPasswordField = !selectedUser.keySaved();
if (masterPasswordField.isVisible() != showPasswordField) {
masterPasswordLabel.setVisible( showPasswordField );
@ -83,7 +95,7 @@ public class ModelAuthenticationPanel extends AuthenticationPanel implements Ite
}
@Override
protected User getUser() {
protected ModelUser getSelectedUser() {
int selectedIndex = userField.getSelectedIndex();
if (selectedIndex < 0)
return null;

View File

@ -29,6 +29,16 @@ public class ModelUser extends User {
return masterPassword;
}
@Override
public int getAvatar() {
return user.getAvatar();
}
public void setAvatar(final int avatar) {
user.setAvatar( avatar % Res.avatars() );
MPUserFileManager.get().save();
}
public void setMasterPassword(final String masterPassword) {
this.masterPassword = masterPassword;
}
@ -39,8 +49,7 @@ public class ModelUser extends User {
if (!user.hasKeyID()) {
user.setKeyID( key.getKeyID() );
MPUserFileManager.get().save();
}
else if (!user.hasKeyID( key.getKeyID() ))
} else if (!user.hasKeyID( key.getKeyID() ))
throw new IllegalStateException( strf( "Incorrect master password for user: %s", getFullName() ) );
return key;

View File

@ -54,7 +54,11 @@ public abstract class Res {
}
public static Icon avatar(final int index) {
return new RetinaIcon( Resources.getResource( strf( "media/avatar-%d@2x.png", index ) ) );
return new RetinaIcon( Resources.getResource( strf( "media/avatar-%d@2x.png", index % avatars() ) ) );
}
public static int avatars() {
return 19;
}
public static Font sourceCodeProBlack() {
@ -119,7 +123,7 @@ public abstract class Res {
private static final class RetinaIcon extends ImageIcon {
private static final Pattern scalePattern = Pattern.compile(".*@(\\d+)x.[^.]+$");
private static final Pattern scalePattern = Pattern.compile( ".*@(\\d+)x.[^.]+$" );
private final float scale;

View File

@ -17,6 +17,10 @@ public abstract class User {
protected abstract String getMasterPassword();
public int getAvatar() {
return 0;
}
public boolean hasKey() {
String masterPassword = getMasterPassword();
return key != null || (masterPassword != null && !masterPassword.isEmpty());