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