org.jdesktop.swingx.auth.DefaultUserNameStore Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swingx Show documentation
Show all versions of swingx Show documentation
Contains extensions to the Swing GUI toolkit, including new and enhanced components that provide functionality commonly required by rich client applications.
/*
* $Id: DefaultUserNameStore.java,v 1.8 2007/11/02 16:41:20 kschaefe Exp $
*
* Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
* Santa Clara, California 95054, U.S.A. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.jdesktop.swingx.auth;
import java.beans.PropertyChangeSupport;
import java.util.prefs.Preferences;
import org.jdesktop.swingx.JXLoginPane;
/**
* Saves the user names in Preferences. Because any string could be part
* of the user name, for every user name that must be saved a new Preferences
* key/value pair must be stored.
*
* @author Bino George
* @author rbair
*/
public class DefaultUserNameStore extends UserNameStore {
/**
* The key for one of the preferences
*/
private static final String USER_KEY = "usernames";
/**
*/
private static final String NUM_KEY = "usernames.length";
/**
* A name that is used when retrieving preferences. By default, the
* app name is "default". This should be set by the application
* if the application wants it's own list of user names.
*/
private static final String DEFAULT_APP_NAME = "default";
/**
* The preferences node
*/
private Preferences prefs;
/**
* Contains the user names. Since the list of user names is not
* frequently updated, there is no penalty in storing the values
* in an array.
*/
private String[] userNames;
/**
* Creates a new instance of DefaultUserNameStore
*/
public DefaultUserNameStore() {
userNames = new String[0];
}
/**
* Loads the user names from Preferences
*/
public void loadUserNames() {
initPrefs();
if (prefs != null) {
int n = prefs.getInt(NUM_KEY, 0);
String[] names = new String[n];
for (int i = 0; i < n; i++) {
names[i] = prefs.get(USER_KEY + "." + i, null);
}
setUserNames(names);
}
}
/**
* Saves the user names to Preferences
*/
public void saveUserNames() {
initPrefs();
if (prefs != null) {
prefs.putInt(NUM_KEY, userNames.length);
for (int i = 0; i < userNames.length; i++) {
prefs.put(USER_KEY + "." + i, userNames[i]);
}
}
}
/**
* @inheritDoc
*/
public String[] getUserNames() {
return userNames;
}
/**
* @inheritDoc
*/
public void setUserNames(String[] userNames) {
userNames = userNames == null ? new String[0] : userNames;
String[] old = getUserNames();
this.userNames = userNames;
firePropertyChange("userNames", old, getUserNames());
}
/**
* Add a username to the store.
* @param name
*/
public void addUserName(String name) {
if (!containsUserName(name)) {
String[] newNames = new String[userNames.length + 1];
for (int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy