The Source for Java Technology Collaboration

Home » java.net Forums » Java Desktop Technologies » SwingLabs

Thread: JXLoginPane and PasswordStore

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
This question is answered. Helpful answers available: 1. Correct answers available: 1.

Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 12 - Last Post: Mar 27, 2009 1:43 PM by: jogiles Threads: [ Previous | Next ]
jogiles

Posts: 34
JXLoginPane and PasswordStore
Posted: Mar 25, 2009 1:19 AM
 
  Click to reply to this thread Reply

Hi all,

I'm using the JXLoginPane, and am trying to work out how to use the PasswordStore. I have successfully got the login pane working, and a user store is loading a list of users who have previously logged in. The problem I am having is that I don't understand how the 'remember password' option comes into play. I want it to be possible for a user to select from the drop down list their username, and if the password is stored, it should auto-fill the password.

Despite implementing PasswordStore, I can never seem to get a call to the 'get' method. Am I doing something wrong, or is this not implemented in the current JXLoginPane?

Cheers,
Jonathan Giles

jogiles

Posts: 34
Re: JXLoginPane and PasswordStore
Posted: Mar 25, 2009 2:43 AM   in response to: jogiles
 
  Click to reply to this thread Reply

After a bit of investigation I've developed a patch that seems to work in my scenario. I will polish it up a bit and make it available in the next 24 hours (it's time for sleep now!).

-- Jonathan Giles

jogiles

Posts: 34
Re: JXLoginPane and PasswordStore
Posted: Mar 25, 2009 2:58 AM   in response to: jogiles
 
  Click to reply to this thread Reply

Attached is the first draft of a patch that allows for the password store to work. I'm not sure if it is possible to format nicely in this forum, so I apologise in advance for the quality of this patch :-)


#P SwingX
Index: src/java/org/jdesktop/swingx/JXLoginPane.java
===================================================================
RCS file: /cvs/swingx/src/java/org/jdesktop/swingx/JXLoginPane.java,v
retrieving revision 1.28
diff -u -r1.28 JXLoginPane.java
--- src/java/org/jdesktop/swingx/JXLoginPane.java 1 Feb 2009 15:01:01 -0000 1.28
+++ src/java/org/jdesktop/swingx/JXLoginPane.java 25 Mar 2009 09:56:03 -0000
@@ -618,9 +618,9 @@
NameComponent oldPanel = namePanel;
//create the NameComponent
if (saveMode == SaveMode.NONE) {
- namePanel = new SimpleNamePanel();
+ namePanel = new SimpleNamePanel(passwordStore);
} else {
- namePanel = new ComboNamePanel(userNameStore);
+ namePanel = new ComboNamePanel(userNameStore, passwordStore);
}
if (oldPanel != null) {
// need to reset here otherwise value will get lost during LAF change as panel gets recreated.
@@ -876,6 +876,10 @@
firePropertyChange("saveMode", oldMode, getSaveMode());
}
}
+
+ public boolean isRememberPassword() {
+ return saveCB.isSelected();
+ }

/**
* @return the List of servers
@@ -1438,11 +1442,36 @@
* If a UserNameStore is not used, then this text field is presented allowing the user
* to simply enter their user name
*/
- public static final class SimpleNamePanel extends JTextField implements NameComponent {
+ public final class SimpleNamePanel extends JTextField implements NameComponent {
private static final long serialVersionUID = 6513437813612641002L;
+
public SimpleNamePanel() {
- super("", 15);
- }
+ this(null);
+ }
+
+ public SimpleNamePanel(final PasswordStore passwordStore) {
+ super("", 15);
+
+ // listen to text input, and offer password suggestion based on current
+ // text
+ if (passwordStore != null) {
+ addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(final ActionEvent e) {
+ final char[] password = passwordStore.get(getText(), null);
+ if (password != null) {
+ SwingUtilities.invokeLater(new Runnable() {
+ @Override
+ public void run() {
+ setPassword(password);
+ }
+ });
+ }
+ }
+ });
+ }
+ }
+
public String getUserName() {
return getText();
}
@@ -1459,16 +1488,40 @@
* If a UserNameStore is used, then this combo box is presented allowing the user
* to select a previous login name, or type in a new login name
*/
- public static final class ComboNamePanel extends JComboBox implements NameComponent {
+ public final class ComboNamePanel extends JComboBox implements NameComponent {
private static final long serialVersionUID = 2511649075486103959L;
private UserNameStore userNameStore;
- public ComboNamePanel(UserNameStore userNameStore) {
+
+ public ComboNamePanel(final UserNameStore userNameStore) {
+ this(userNameStore, null);
+ }
+
+ public ComboNamePanel(final UserNameStore userNameStore, final PasswordStore passwordStore) {
super();
this.userNameStore = userNameStore;
setModel(new NameComboBoxModel());
setEditable(true);

+ // listen to selection or text input, and offer password suggestion based on current
+ // text
+ if (passwordStore != null) {
+ addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(final ActionEvent e) {
+ final char[] password = passwordStore.get((String) getSelectedItem(), null);
+ if (password != null) {
+ SwingUtilities.invokeLater(new Runnable() {
+ @Override
+ public void run() {
+ setPassword(password);
+ }
+ });
+ }
+ }
+ });
+ }
}
+
public String getUserName() {
Object item = getModel().getSelectedItem();
return item == null ? null : item.toString();

kschaefe

Posts: 1,662
Re: JXLoginPane and PasswordStore
Posted: Mar 25, 2009 6:00 AM   in response to: jogiles
Helpful
  Click to reply to this thread Reply

Jonathan,

There are several known problems with JXLoginPane. You can review the issues in the tracker. I have found what looks (to me) to be a similar problem in Bug 358. Can you check that out and see if it matches your issues.

If so, please add this thread to the issue and submit your patch to it. If the patch exceeds 20 lines, we need a signed SCA to utilize it.

Karl

jogiles

Posts: 34
Re: JXLoginPane and PasswordStore
Posted: Mar 25, 2009 12:12 PM   in response to: kschaefe
 
  Click to reply to this thread Reply

Karl,

You're correct, my patch is intended to fix that issue. My patch is very similar to the patch that was posted in the middle of 2007, but obviously a bit more updated based on the latest JXLoginPane. Despite this, I have plans to improve my patch a little more.

If necessary, how do I go about signing a SCA?

Cheers,
Jonathan

kschaefe

Posts: 1,662
Re: JXLoginPane and PasswordStore
Posted: Mar 25, 2009 12:41 PM   in response to: jogiles
 
  Click to reply to this thread Reply

I believe the latest is at https://sca.dev.java.net/

Karl

jogiles

Posts: 34
Re: JXLoginPane and PasswordStore
Posted: Mar 25, 2009 12:44 PM   in response to: kschaefe
 
  Click to reply to this thread Reply

Thanks,

It looks like I'll have to sign and mail it back, which will take a long time. I'm hoping that this can get into the next release?

I've updated the patch to work better, and it is included below. It is much improved over the last patch, and works for both text input and combobox selection.

Cheers,
Jonathan Giles

=================================
Index: src/java/org/jdesktop/swingx/JXLoginPane.java
===================================================================
RCS file: /cvs/swingx/src/java/org/jdesktop/swingx/JXLoginPane.java,v
retrieving revision 1.28
diff -u -r1.28 JXLoginPane.java
--- src/java/org/jdesktop/swingx/JXLoginPane.java 1 Feb 2009 15:01:01 -0000 1.28
+++ src/java/org/jdesktop/swingx/JXLoginPane.java 25 Mar 2009 19:40:45 -0000
@@ -45,6 +45,8 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
+import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
@@ -82,6 +84,8 @@
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
+import javax.swing.event.ListDataEvent;
+import javax.swing.event.ListDataListener;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.text.View;

@@ -614,13 +618,22 @@
*/
private JXPanel createLoginPanel() {
JXPanel loginPanel = new JXPanel();
+
+ JPasswordField oldPwd = passwordField;
+ //create the password component
+ passwordField = new JPasswordField("", 15);
+ JLabel passwordLabel = new JLabel(UIManagerExt.getString(CLASS_NAME + ".passwordString", getLocale()));
+ passwordLabel.setLabelFor(passwordField);
+ if (oldPwd != null) {
+ passwordField.setText(new String(oldPwd.getPassword()));
+ }

NameComponent oldPanel = namePanel;
//create the NameComponent
if (saveMode == SaveMode.NONE) {
- namePanel = new SimpleNamePanel();
+ namePanel = new SimpleNamePanel(passwordStore, passwordField);
} else {
- namePanel = new ComboNamePanel(userNameStore);
+ namePanel = new ComboNamePanel(userNameStore, passwordStore, passwordField);
}
if (oldPanel != null) {
// need to reset here otherwise value will get lost during LAF change as panel gets recreated.
@@ -634,15 +647,6 @@
JLabel nameLabel = new JLabel(UIManagerExt.getString(CLASS_NAME + ".nameString", getLocale()));
nameLabel.setLabelFor(namePanel.getComponent());

- JPasswordField oldPwd = passwordField;
- //create the password component
- passwordField = new JPasswordField("", 15);
- JLabel passwordLabel = new JLabel(UIManagerExt.getString(CLASS_NAME + ".passwordString", getLocale()));
- passwordLabel.setLabelFor(passwordField);
- if (oldPwd != null) {
- passwordField.setText(new String(oldPwd.getPassword()));
- }
-
//create the server combo box if necessary
JLabel serverLabel = new JLabel(UIManagerExt.getString(CLASS_NAME + ".serverString", getLocale()));
if (servers.size() > 1) {
@@ -876,6 +880,10 @@
firePropertyChange("saveMode", oldMode, getSaveMode());
}
}
+
+ public boolean isRememberPassword() {
+ return saveCB.isSelected();
+ }

/**
* @return the List of servers
@@ -1440,9 +1448,26 @@
*/
public static final class SimpleNamePanel extends JTextField implements NameComponent {
private static final long serialVersionUID = 6513437813612641002L;
+
public SimpleNamePanel() {
- super("", 15);
- }
+ this(null, null);
+ }
+
+ public SimpleNamePanel(final PasswordStore passwordStore, final JPasswordField passwordField) {
+ super("", 15);
+
+ // listen to text input, and offer password suggestion based on current
+ // text
+ if (passwordStore != null && passwordField!=null) {
+ addKeyListener(new KeyAdapter() {
+ @Override
+ public void keyReleased(KeyEvent e) {
+ updatePassword(getText(), passwordStore, passwordField);
+ }
+ });
+ }
+ }
+
public String getUserName() {
return getText();
}
@@ -1453,6 +1478,15 @@
return this;
}

+ private void updatePassword(final String username, final PasswordStore passwordStore, final JPasswordField passwordField) {
+ String password = "";
+ if (username != null) {
+ char[] pw = passwordStore.get(username, null);
+ password = pw == null ? "" : new String(pw);
+ }
+
+ passwordField.setText(password);
+ }
}

/**
@@ -1462,13 +1496,47 @@
public static final class ComboNamePanel extends JComboBox implements NameComponent {
private static final long serialVersionUID = 2511649075486103959L;
private UserNameStore userNameStore;
- public ComboNamePanel(UserNameStore userNameStore) {
+
+ public ComboNamePanel(final UserNameStore userNameStore) {
+ this(userNameStore, null,null);
+ }
+
+ public ComboNamePanel(final UserNameStore userNameStore, final PasswordStore passwordStore, final JPasswordField passwordField) {
super();
this.userNameStore = userNameStore;
setModel(new NameComboBoxModel());
setEditable(true);

+ // listen to selection or text input, and offer password suggestion based on current
+ // text
+ if (passwordStore != null && passwordField!=null) {
+ final JTextField textfield = (JTextField) getEditor().getEditorComponent();
+ textfield.addKeyListener(new KeyAdapter() {
+ @Override
+ public void keyReleased(KeyEvent e) {
+ updatePassword(textfield.getText(), passwordStore, passwordField);
+ }
+ });
+
+ super.addItemListener(new ItemListener() {
+ @Override
+ public void itemStateChanged(ItemEvent e) {
+ updatePassword((String)getSelectedItem(), passwordStore, passwordField);
+ }
+ });
+ }
}
+
+ private void updatePassword(final String username, final PasswordStore passwordStore, final JPasswordField passwordField) {
+ String password = "";
+ if (username != null) {
+ char[] pw = passwordStore.get(username, null);
+ password = pw == null ? "" : new String(pw);
+ }
+
+ passwordField.setText(password);
+ }
+
public String getUserName() {
Object item = getModel().getSelectedItem();
return item == null ? null : item.toString();
=====================================

kschaefe

Posts: 1,662
Re: JXLoginPane and PasswordStore
Posted: Mar 25, 2009 1:14 PM   in response to: jogiles
 
  Click to reply to this thread Reply

Jonathan,

> It looks like I'll have to sign and mail it back,
> which will take a long time. I'm hoping that this can
> get into the next release?

I don't know where you are, but copy centers like Kinko's (in the US) provide scanners for use as well as faxes and either is an acceptable means for submittal. Seeing how the next release was supposed to be two weekends ago...I'm not sure when it will get out.

Jan, what's the status of 0.9.6?

Karl

jogiles

Posts: 34
Re: JXLoginPane and PasswordStore
Posted: Mar 25, 2009 1:19 PM   in response to: kschaefe
 
  Click to reply to this thread Reply

Karl,

I live in the slightly more remote New Zealand, and even worse, I live in a small town :-)
I just had someone scan my SCA, and it is now at Sun.

Cheers,
Jonathan

Message was edited by: jogiles

jogiles

Posts: 34
Re: JXLoginPane and PasswordStore
Posted: Mar 27, 2009 12:30 PM   in response to: jogiles
 
  Click to reply to this thread Reply

Hello,

My SCA has been accepted, as shown at [1]. Is it possible to get this small patch added in to SwingX 0.9.6 before its release?

[1] https://sca.dev.java.net/CA_signatories.htm

Cheers,
Jonathan Giles

rah003

Posts: 894
Re: JXLoginPane and PasswordStore
Posted: Mar 27, 2009 1:28 PM   in response to: jogiles
 
  Click to reply to this thread Reply

I'm planning to do the release this Sunday. I'll apply your patch before that. Thanks for your contribution. If you want to take up more of the JXLoginPane issues you would be most welcome to do so :)
Thanks,
Jan

jogiles

Posts: 34
Re: JXLoginPane and PasswordStore
Posted: Mar 27, 2009 1:35 PM   in response to: rah003
 
  Click to reply to this thread Reply

Thanks for including the patch.

Now that I have the SCA all done, I'll keep an eye out for areas I can help out with.

-- Jonathan

jogiles

Posts: 34
Re: JXLoginPane and PasswordStore
Posted: Mar 27, 2009 1:43 PM   in response to: jogiles
 
  Click to reply to this thread Reply

By the way, issue 301 is a dupe of the bug I fixed, so you can probably close that as well.

-- Jonathan Giles




 XML java.net RSS