2
0

Update UI instead of resetting when site is updated.

This commit is contained in:
Maarten Billemont 2018-07-29 01:38:15 -04:00
parent 0b7494ecbf
commit a6e9e89ace

View File

@ -2,11 +2,11 @@ package com.lyndir.masterpassword.gui.view;
import static com.lyndir.lhunath.opal.system.util.StringUtils.*; import static com.lyndir.lhunath.opal.system.util.StringUtils.*;
import com.google.common.base.Joiner; import com.google.common.base.*;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.primitives.UnsignedInteger; import com.google.common.primitives.UnsignedInteger;
import com.lyndir.lhunath.opal.system.logging.Logger; import com.lyndir.lhunath.opal.system.logging.Logger;
import com.lyndir.lhunath.opal.system.util.ObjectUtils;
import com.lyndir.masterpassword.*; import com.lyndir.masterpassword.*;
import com.lyndir.masterpassword.gui.model.MPNewSite; import com.lyndir.masterpassword.gui.model.MPNewSite;
import com.lyndir.masterpassword.gui.util.*; import com.lyndir.masterpassword.gui.util.*;
@ -44,7 +44,8 @@ public class UserContentPanel extends JPanel implements FilesPanel.Listener, MPU
private final JPanel siteToolbar = Components.panel( BoxLayout.PAGE_AXIS ); private final JPanel siteToolbar = Components.panel( BoxLayout.PAGE_AXIS );
@Nullable @Nullable
private MPUser<?> listeningUser; private MPUser<?> activeUser;
private ContentMode contentMode;
public UserContentPanel() { public UserContentPanel() {
userToolbar.setPreferredSize( iconButton.getPreferredSize() ); userToolbar.setPreferredSize( iconButton.getPreferredSize() );
@ -85,29 +86,32 @@ public class UserContentPanel extends JPanel implements FilesPanel.Listener, MPU
private void setUser(@Nullable final MPUser<?> user) { private void setUser(@Nullable final MPUser<?> user) {
Res.ui( () -> { Res.ui( () -> {
if (listeningUser != null) if (activeUser != null)
listeningUser.removeListener( this ); activeUser.removeListener( this );
listeningUser = user;
userToolbar.removeAll(); ContentMode newContentMode = ContentMode.getContentMode( user );
siteToolbar.removeAll(); if ((newContentMode != contentMode) || !ObjectUtils.equals( activeUser, user )) {
removeAll(); userToolbar.removeAll();
siteToolbar.removeAll();
if (user == null) removeAll();
add( new NoUserPanel() ); activeUser = user;
switch (contentMode = newContentMode) {
else { case NO_USER:
user.addListener( this ); add( new NoUserPanel() );
break;
if (!user.isMasterKeyAvailable()) case AUTHENTICATE:
add( new AuthenticateUserPanel( user ) ); add( new AuthenticateUserPanel( Preconditions.checkNotNull( activeUser ) ) );
break;
else case AUTHENTICATED:
add( new AuthenticatedUserPanel( user ) ); add( new AuthenticatedUserPanel( Preconditions.checkNotNull( activeUser ) ) );
break;
}
revalidate();
transferFocus();
} }
revalidate(); if (activeUser != null)
transferFocus(); activeUser.addListener( this );
} ); } );
} }
@ -121,6 +125,22 @@ public class UserContentPanel extends JPanel implements FilesPanel.Listener, MPU
setUser( MPFileUserManager.get().add( fullName.toString() ) ); setUser( MPFileUserManager.get().add( fullName.toString() ) );
} }
private enum ContentMode {
NO_USER,
AUTHENTICATE,
AUTHENTICATED;
static ContentMode getContentMode(@Nullable final MPUser<?> user) {
if (user == null)
return NO_USER;
else if (!user.isMasterKeyAvailable())
return AUTHENTICATE;
else
return AUTHENTICATED;
}
}
private final class NoUserPanel extends JPanel { private final class NoUserPanel extends JPanel {
private NoUserPanel() { private NoUserPanel() {
@ -288,7 +308,7 @@ public class UserContentPanel extends JPanel implements FilesPanel.Listener, MPU
} }
private final class AuthenticatedUserPanel extends JPanel implements KeyListener { private final class AuthenticatedUserPanel extends JPanel implements KeyListener, MPUser.Listener {
public static final int SIZE_RESULT = 48; public static final int SIZE_RESULT = 48;
@ -351,6 +371,8 @@ public class UserContentPanel extends JPanel implements FilesPanel.Listener, MPU
add( Components.scrollPane( sitesList ) ); add( Components.scrollPane( sitesList ) );
sitesModel.registerList( sitesList ); sitesModel.registerList( sitesList );
add( Box.createGlue() ); add( Box.createGlue() );
user.addListener( this );
} }
public void showUserPreferences() { public void showUserPreferences() {
@ -545,5 +567,19 @@ public class UserContentPanel extends JPanel implements FilesPanel.Listener, MPU
Res.ui( () -> sitesModel.set( sites ) ); Res.ui( () -> sitesModel.set( sites ) );
} ); } );
} }
@Override
public void onUserUpdated(final MPUser<?> user) {
updateSites( queryField.getText() );
showSiteResult( sitesModel.getSelectedItem() );
}
@Override
public void onUserAuthenticated(final MPUser<?> user) {
}
@Override
public void onUserInvalidated(final MPUser<?> user) {
}
} }
} }