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