Skip to content

Commit

Permalink
This update removes the Web Start dependency from just the
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 197 deletions.
105 changes: 0 additions & 105 deletions src/com/horstmann/violet/framework/JNLPPreferencesService.java

This file was deleted.

174 changes: 82 additions & 92 deletions src/com/horstmann/violet/framework/PreferencesService.java
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) { }
}

0 comments on commit 853142f

Please sign in to comment.