001 /* 002 * $Id: DefaultUserNameStore.java,v 1.6 2005/11/11 22:09:54 rbair Exp $ 003 * 004 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, 005 * Santa Clara, California 95054, U.S.A. All rights reserved. 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * You should have received a copy of the GNU Lesser General Public 018 * License along with this library; if not, write to the Free Software 019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 020 */ 021 022 023 package org.jdesktop.swingx.auth; 024 025 import java.beans.PropertyChangeSupport; 026 import java.util.prefs.Preferences; 027 028 import org.jdesktop.swingx.JXLoginPanel; 029 030 /** 031 * Saves the user names in Preferences. Because any string could be part 032 * of the user name, for every user name that must be saved a new Preferences 033 * key/value pair must be stored. 034 * 035 * @author Bino George 036 * @author rbair 037 */ 038 public class DefaultUserNameStore extends UserNameStore { 039 /** 040 * The key for one of the preferences 041 */ 042 private static final String USER_KEY = "usernames"; 043 /** 044 */ 045 private static final String NUM_KEY = "usernames.length"; 046 /** 047 * A name that is used when retrieving preferences. By default, the 048 * app name is "default". This should be set by the application 049 * if the application wants it's own list of user names. 050 */ 051 private static final String DEFAULT_APP_NAME = "default"; 052 /** 053 * The preferences node 054 */ 055 private Preferences prefs; 056 /** 057 * Contains the user names. Since the list of user names is not 058 * frequently updated, there is no penalty in storing the values 059 * in an array. 060 */ 061 private String[] userNames; 062 /** 063 * Used for propogating bean changes 064 */ 065 private PropertyChangeSupport pcs; 066 067 /** 068 * Creates a new instance of DefaultUserNameStore 069 */ 070 public DefaultUserNameStore() { 071 pcs = new PropertyChangeSupport(this); 072 userNames = new String[0]; 073 } 074 075 /** 076 * Loads the user names from Preferences 077 */ 078 public void loadUserNames() { 079 initPrefs(); 080 if (prefs != null) { 081 int n = prefs.getInt(NUM_KEY, 0); 082 String[] names = new String[n]; 083 for (int i = 0; i < n; i++) { 084 names[i] = prefs.get(USER_KEY + "." + i, null); 085 } 086 setUserNames(names); 087 } 088 } 089 090 /** 091 * Saves the user names to Preferences 092 */ 093 public void saveUserNames() { 094 initPrefs(); 095 if (prefs != null) { 096 prefs.putInt(NUM_KEY, userNames.length); 097 for (int i = 0; i < userNames.length; i++) { 098 prefs.put(USER_KEY + "." + i, userNames[i]); 099 } 100 } 101 } 102 103 /** 104 * @inheritDoc 105 */ 106 public String[] getUserNames() { 107 return userNames; 108 } 109 110 /** 111 * @inheritDoc 112 */ 113 public void setUserNames(String[] userNames) { 114 if (this.userNames != userNames) { 115 String[] old = this.userNames; 116 this.userNames = userNames == null ? new String[0] : userNames; 117 pcs.firePropertyChange("userNames", old, this.userNames); 118 } 119 } 120 121 /** 122 * Add a username to the store. 123 * @param name 124 */ 125 public void addUserName(String name) { 126 if (!containsUserName(name)) { 127 String[] newNames = new String[userNames.length + 1]; 128 for (int i=0; i<userNames.length; i++) { 129 newNames[i] = userNames[i]; 130 } 131 newNames[newNames.length - 1] = name; 132 setUserNames(newNames); 133 } 134 } 135 136 /** 137 * Removes a username from the list. 138 * 139 * @param name 140 */ 141 public void removeUserName(String name) { 142 if (containsUserName(name)) { 143 String[] newNames = new String[userNames.length - 1]; 144 int index = 0; 145 for (String s : userNames) { 146 if (!s.equals(name)) { 147 newNames[index++] = s; 148 } 149 } 150 setUserNames(newNames); 151 } 152 } 153 154 /** 155 * @inheritDoc 156 */ 157 public boolean containsUserName(String name) { 158 for (String s : userNames) { 159 if (s.equals(name)) { 160 return true; 161 } 162 } 163 return false; 164 } 165 166 /** 167 * @return Returns Preferences node in which the user names will be stored 168 */ 169 public Preferences getPreferences() { 170 return prefs; 171 } 172 173 /** 174 * @param prefs the Preferences node to store the user names in. If null, 175 * or undefined, then they are stored in /org/jdesktop/swingx/auth/DefaultUserNameStore. 176 */ 177 public void setPreferences(Preferences prefs) { 178 initPrefs(); 179 if (this.prefs != prefs) { 180 Preferences old = this.prefs; 181 this.prefs = prefs; 182 pcs.firePropertyChange("preferences", old, prefs); 183 //if prefs is null, this next method will create the default prefs node 184 loadUserNames(); 185 } 186 } 187 188 /** 189 * Creates the default prefs node 190 */ 191 private void initPrefs() { 192 if (prefs == null) { 193 prefs = Preferences.userNodeForPackage(DefaultUserNameStore.class); 194 prefs = prefs.node("DefaultUserNameStore"); 195 } 196 } 197 }