-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This update removes the Web Start dependency from just the
PreferencesService abstract class. It also deleted that version of the preferences service, since we don't need it.
- Loading branch information
EJBQ
authored and
EJBQ
committed
Jan 19, 2015
1 parent
2aa1a1d
commit 853142f
Showing
2 changed files
with
82 additions
and
197 deletions.
There are no files selected for viewing
105 changes: 0 additions & 105 deletions
105
src/com/horstmann/violet/framework/JNLPPreferencesService.java
This file was deleted.
Oops, something went wrong.
174 changes: 82 additions & 92 deletions
174
src/com/horstmann/violet/framework/PreferencesService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,83 @@ | ||
package com.horstmann.violet.framework; | ||
|
||
import java.util.prefs.Preferences; | ||
|
||
/** | ||
* A service for storing and loading user preferences. | ||
* This service uses either the standard Java preferences API or the WebStart | ||
* persistence service ("muffins"). | ||
*/ | ||
public abstract class PreferencesService | ||
{ | ||
/** | ||
* Gets an instance of the service, suitable for the package of the given class. | ||
* @param appClass the main application class (only the package name is used as the path to | ||
* app-specific preferences storage) | ||
* @return an instance of the service | ||
*/ | ||
public static PreferencesService getInstance(Class appClass) | ||
{ | ||
if (service != null) return service; | ||
try | ||
{ | ||
service = new DefaultPreferencesService(appClass); | ||
return service; | ||
} | ||
catch (SecurityException exception) | ||
{ | ||
// that happens when we run under Web Start | ||
} | ||
try | ||
{ | ||
// we load this lazily so that the JAR can load without WebStart | ||
service = (PreferencesService) Class.forName("com.horstmann.violet.framework.JNLPPreferencesService").newInstance(); | ||
return service; | ||
} | ||
catch (Throwable exception) | ||
{ | ||
// that happens when we are an applet | ||
} | ||
|
||
return new NullPreferencesService(); | ||
} | ||
|
||
/** | ||
* Gets a previously stored string from the service. | ||
* @param key the key of the string | ||
* @param defval the value to return if no matching value was found | ||
* @return the value stored with the given key, or defval if none was found | ||
*/ | ||
public abstract String get(String key, String defval); | ||
/** | ||
* Saves a key/value pair for later retrieval. | ||
* @param key the key of the string to be stored | ||
* @param value the value to to be stored | ||
*/ | ||
public abstract void put(String key, String value); | ||
|
||
private static PreferencesService service; | ||
} | ||
|
||
/** | ||
* The default preferences service that uses the java.util.prefs API. | ||
*/ | ||
class DefaultPreferencesService extends PreferencesService | ||
{ | ||
|
||
private Preferences prefs; | ||
|
||
/** | ||
* Gets an instance of the service, suitable for the package of the given class. | ||
* @param appClass the main application class (only the package name is used as the path to | ||
* app-specific preferences storage) | ||
* @return an instance of the service | ||
*/ | ||
public DefaultPreferencesService(Class appClass) | ||
{ | ||
prefs = Preferences.userNodeForPackage(appClass); | ||
} | ||
|
||
public String get(String key, String defval) { return prefs.get(key, defval); } | ||
public void put(String key, String defval) { prefs.put(key, defval); } | ||
|
||
|
||
} | ||
|
||
/** | ||
* The null preferences service that is returned when we are an applet. | ||
*/ | ||
class NullPreferencesService extends PreferencesService | ||
{ | ||
public String get(String key, String defval) { return defval; } | ||
public void put(String key, String defval) { } | ||
package com.horstmann.violet.framework; | ||
|
||
import java.util.prefs.Preferences; | ||
|
||
/** | ||
* A service for storing and loading user preferences. | ||
* This service uses the standard Java preferences API | ||
* persistence service ("muffins"). | ||
*/ | ||
public abstract class PreferencesService | ||
{ | ||
/** | ||
* Gets an instance of the service, suitable for the package of the given class. | ||
* @param appClass the main application class (only the package name is used as the path to | ||
* app-specific preferences storage) | ||
* @return an instance of the service | ||
*/ | ||
public static PreferencesService getInstance(Class appClass) | ||
{ | ||
if (service != null) return service; | ||
try | ||
{ | ||
service = new DefaultPreferencesService(appClass); | ||
return service; | ||
} | ||
catch (Throwable exception) | ||
{ | ||
// that happens when we are an applet | ||
} | ||
|
||
return new NullPreferencesService(); | ||
} | ||
|
||
/** | ||
* Gets a previously stored string from the service. | ||
* @param key the key of the string | ||
* @param defval the value to return if no matching value was found | ||
* @return the value stored with the given key, or defval if none was found | ||
*/ | ||
public abstract String get(String key, String defval); | ||
/** | ||
* Saves a key/value pair for later retrieval. | ||
* @param key the key of the string to be stored | ||
* @param value the value to to be stored | ||
*/ | ||
public abstract void put(String key, String value); | ||
|
||
private static PreferencesService service; | ||
} | ||
|
||
/** | ||
* The default preferences service that uses the java.util.prefs API. | ||
*/ | ||
class DefaultPreferencesService extends PreferencesService | ||
{ | ||
|
||
private Preferences prefs; | ||
|
||
/** | ||
* Gets an instance of the service, suitable for the package of the given class. | ||
* @param appClass the main application class (only the package name is used as the path to | ||
* app-specific preferences storage) | ||
* @return an instance of the service | ||
*/ | ||
public DefaultPreferencesService(Class appClass) | ||
{ | ||
prefs = Preferences.userNodeForPackage(appClass); | ||
} | ||
|
||
public String get(String key, String defval) { return prefs.get(key, defval); } | ||
public void put(String key, String defval) { prefs.put(key, defval); } | ||
|
||
|
||
} | ||
|
||
/** | ||
* The null preferences service that is returned when we are an applet. | ||
*/ | ||
class NullPreferencesService extends PreferencesService | ||
{ | ||
public String get(String key, String defval) { return defval; } | ||
public void put(String key, String defval) { } | ||
} |