DefaultUserNameStore.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:5k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: DefaultUserNameStore.java,v 1.4 2005/10/10 18:02:49 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  */
  7. package org.jdesktop.swingx.auth;
  8. import java.beans.PropertyChangeSupport;
  9. import java.util.prefs.Preferences;
  10. import org.jdesktop.swingx.JXLoginPanel;
  11. /**
  12.  * Saves the user names in Preferences. Because any string could be part
  13.  * of the user name, for every user name that must be saved a new Preferences
  14.  * key/value pair must be stored.
  15.  *
  16.  * @author Bino George
  17.  * @author rbair
  18.  */
  19. public class DefaultUserNameStore extends UserNameStore {
  20.     /**
  21.      * The key for one of the preferences
  22.      */
  23.         private static final String USER_KEY = "usernames";
  24.     /**
  25.      */
  26.     private static final String NUM_KEY = "usernames.length";
  27.     /**
  28.      * The preferences node
  29.      */
  30.     private Preferences prefs;
  31.     /**
  32.      * A name that is used when retrieving preferences. By default, the
  33.      * app name is "default&quot. This should be set by the application
  34.      * if the application wants it's own list of user names.
  35.      */
  36.         private String appNameForPreferences = "default";
  37.     /**
  38.      * Contains the user names. Since the list of user names is not
  39.      * frequently updated, there is no penalty in storing the values
  40.      * in an array.
  41.      */
  42.     private String[] userNames;
  43.     /**
  44.      * Used for propogating bean changes
  45.      */
  46.     private PropertyChangeSupport pcs;
  47.     /**
  48.      * Creates a new instance of DefaultUserNameStore
  49.      */
  50.     public DefaultUserNameStore() {
  51.         pcs = new PropertyChangeSupport(this);
  52.         userNames = new String[0];
  53.     }
  54.     public void loadUserNames() {
  55.         initPrefs();
  56.         if (prefs != null) {
  57.             String numPrefix = getNumPrefix();
  58.             int n = prefs.getInt(numPrefix, 0);
  59.             String valuePrefix = getValuePrefix();
  60.             String[] names = new String[n];
  61.             for (int i = 0; i < n; i++) {
  62.                 names[i] = prefs.get(valuePrefix + "." + i, null);
  63.             }
  64.             setUserNames(names);
  65.         }
  66.     }
  67.     public void saveUserNames() {
  68.         initPrefs();
  69.         if (prefs != null) {
  70.             String numPrefix = getNumPrefix();
  71.             String valuePrefix = getValuePrefix();
  72.             prefs.putInt(numPrefix, userNames.length);
  73.             for (int i = 0; i < userNames.length; i++) {
  74.                 prefs.put(valuePrefix + "." + i, userNames[i]);
  75.             }
  76.         }
  77.     }
  78.     public String[] getUserNames() {
  79.         return userNames;
  80.     }
  81.     public void setUserNames(String[] userNames) {
  82.         if (this.userNames != userNames) {
  83.             String[] old = this.userNames;
  84.             this.userNames = userNames == null ? new String[0] : userNames;
  85.             pcs.firePropertyChange("userNames", old, this.userNames);
  86.         }
  87.     }
  88.         /**
  89.          * Add a username to the store.
  90.          * @param name
  91.          */
  92.         public void addUserName(String name) {
  93.                 if (!containsUserName(name)) {
  94.             String[] newNames = new String[userNames.length + 1];
  95.             for (int i=0; i<userNames.length; i++) {
  96.                 newNames[i] = userNames[i];
  97.             }
  98.             newNames[newNames.length - 1] = name;
  99.             setUserNames(newNames);
  100.                 }
  101.         }
  102.         /**
  103.          * Removes a username from the list.
  104.          *
  105.          * @param name
  106.          */
  107.         public void removeUserName(String name) {
  108.                 if (containsUserName(name)) {
  109.             String[] newNames = new String[userNames.length - 1];
  110.             int index = 0;
  111.             for (String s : userNames) {
  112.                 if (!s.equals(name)) {
  113.                     newNames[index++] = s;
  114.                 }
  115.             }
  116.             setUserNames(newNames);
  117.                 }
  118.         }
  119.     public boolean containsUserName(String name) {
  120.         for (String s : userNames) {
  121.             if (s.equals(name)) {
  122.                 return true;
  123.             }
  124.         }
  125.         return false;
  126.     }
  127.         /**
  128.          * @return Returns the appNameForPreferences.
  129.          */
  130.         public String getAppNameForPreferences() {
  131.                 return appNameForPreferences;
  132.         }
  133.         /**
  134.          * @param appNameForPreferences The appNameForPreferences to set.
  135.          */
  136.         public void setAppNameForPreferences(String appNameForPreferences) {
  137.         if (this.appNameForPreferences != appNameForPreferences) {
  138.             String old = this.appNameForPreferences;
  139.             this.appNameForPreferences = appNameForPreferences;
  140.             pcs.firePropertyChange("appNameForPreferences", old, appNameForPreferences);
  141.             prefs = null;
  142.             loadUserNames();
  143.         }
  144.         }
  145.         private String getNumPrefix() {
  146.                 return this.getClass().getName() + "." + getAppNameForPreferences()
  147.                                 + "." + NUM_KEY;
  148.         }
  149.         private String getValuePrefix() {
  150.                 return this.getClass().getName() + "." + getAppNameForPreferences()
  151.                                 + "." + USER_KEY;
  152.         }
  153.     private void initPrefs() {
  154.         if (prefs == null) {
  155.             prefs = Preferences.userNodeForPackage(JXLoginPanel.class);
  156.         }
  157.     }
  158. }