001/* 002 * $Id: DefaultUserNameStore.java 4147 2012-02-01 17:13:24Z kschaefe $ 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 */ 021package org.jdesktop.swingx.auth; 022 023import java.util.prefs.Preferences; 024 025import org.jdesktop.beans.JavaBean; 026 027/** 028 * Saves the user names in Preferences. Because any string could be part 029 * of the user name, for every user name that must be saved a new Preferences 030 * key/value pair must be stored. 031 * 032 * @author Bino George 033 * @author rbair 034 */ 035@JavaBean 036public class DefaultUserNameStore extends UserNameStore { 037 /** 038 * The key for one of the preferences 039 */ 040 private static final String USER_KEY = "usernames"; 041 /** 042 */ 043 private static final String NUM_KEY = "usernames.length"; 044 /** 045 * The preferences node 046 */ 047 private Preferences prefs; 048 /** 049 * Contains the user names. Since the list of user names is not 050 * frequently updated, there is no penalty in storing the values 051 * in an array. 052 */ 053 private String[] userNames; 054 055 /** 056 * Creates a new instance of DefaultUserNameStore 057 */ 058 public DefaultUserNameStore() { 059 userNames = new String[0]; 060 } 061 062 /** 063 * Loads the user names from Preferences 064 */ 065 @Override 066 public void loadUserNames() { 067 initPrefs(); 068 if (prefs != null) { 069 int n = prefs.getInt(NUM_KEY, 0); 070 String[] names = new String[n]; 071 for (int i = 0; i < n; i++) { 072 names[i] = prefs.get(USER_KEY + "." + i, null); 073 } 074 setUserNames(names); 075 } 076 } 077 078 /** 079 * Saves the user names to Preferences 080 */ 081 @Override 082 public void saveUserNames() { 083 initPrefs(); 084 if (prefs != null) { 085 prefs.putInt(NUM_KEY, userNames.length); 086 for (int i = 0; i < userNames.length; i++) { 087 prefs.put(USER_KEY + "." + i, userNames[i]); 088 } 089 } 090 } 091 092 /** 093 * {@inheritDoc} 094 */ 095 @Override 096 public String[] getUserNames() { 097 String[] copy = new String[userNames.length]; 098 System.arraycopy(userNames, 0, copy, 0, userNames.length); 099 100 return copy; 101 } 102 103 /** 104 * {@inheritDoc} 105 */ 106 @Override 107 public void setUserNames(String[] userNames) { 108 userNames = userNames == null ? new String[0] : userNames; 109 String[] old = getUserNames(); 110 this.userNames = userNames; 111 firePropertyChange("userNames", old, getUserNames()); 112 } 113 114 /** 115 * Add a username to the store. 116 * @param name 117 */ 118 @Override 119 public void addUserName(String name) { 120 if (!containsUserName(name)) { 121 String[] newNames = new String[userNames.length + 1]; 122 for (int i=0; i<userNames.length; i++) { 123 newNames[i] = userNames[i]; 124 } 125 newNames[newNames.length - 1] = name; 126 setUserNames(newNames); 127 } 128 } 129 130 /** 131 * Removes a username from the list. 132 * 133 * @param name 134 */ 135 @Override 136 public void removeUserName(String name) { 137 if (containsUserName(name)) { 138 String[] newNames = new String[userNames.length - 1]; 139 int index = 0; 140 for (String s : userNames) { 141 if (!s.equals(name)) { 142 newNames[index++] = s; 143 } 144 } 145 setUserNames(newNames); 146 } 147 } 148 149 /** 150 * {@inheritDoc} 151 */ 152 @Override 153 public boolean containsUserName(String name) { 154 for (String s : userNames) { 155 if (s.equals(name)) { 156 return true; 157 } 158 } 159 return false; 160 } 161 162 /** 163 * @return Returns Preferences node in which the user names will be stored 164 */ 165 public Preferences getPreferences() { 166 return prefs; 167 } 168 169 /** 170 * @param prefs the Preferences node to store the user names in. If null, 171 * or undefined, then they are stored in /org/jdesktop/swingx/auth/DefaultUserNameStore. 172 */ 173 public void setPreferences(Preferences prefs) { 174 Preferences old = getPreferences(); 175 initPrefs(); 176 this.prefs = prefs; 177 firePropertyChange("preferences", old, getPreferences()); 178 if (this.prefs != old) { 179 //if prefs is null, this next method will create the default prefs node 180 loadUserNames(); 181 } 182 } 183 184 /** 185 * Creates the default prefs node 186 */ 187 private void initPrefs() { 188 if (prefs == null) { 189 prefs = Preferences.userNodeForPackage(DefaultUserNameStore.class); 190 prefs = prefs.node("DefaultUserNameStore"); 191 } 192 } 193}