Path: blob/master/SLICK_HOME/src/org/newdawn/slick/SavedState.java
1456 views
package org.newdawn.slick;12import java.io.IOException;3import java.util.HashMap;45import javax.jnlp.ServiceManager;67import org.newdawn.slick.muffin.FileMuffin;8import org.newdawn.slick.muffin.Muffin;9import org.newdawn.slick.muffin.WebstartMuffin;10import org.newdawn.slick.util.Log;1112/**13* A utility to allow game setup/state to be stored locally. This utility will adapt to the14* current enviornment (webstart or file based). Note that this will not currently15* work in an applet.16*17* @author kappaOne18*/19public class SavedState {20/** file name of where the scores will be saved */21private String fileName;22/** Type of Muffin to use */23private Muffin muffin;24/** hash map where int data will be stored */25private HashMap numericData = new HashMap();26/** hash map where string data will be stored */27private HashMap stringData = new HashMap();2829/**30* Create and Test to see if the app is running31* as webstart or local app and select the appropriate32* muffin type33*34* @param fileName name of muffin where data will be saved35* @throws SlickException Indicates a failure to load the stored state36*/37public SavedState(String fileName) throws SlickException {38this.fileName = fileName;3940if (isWebstartAvailable()) {41muffin = new WebstartMuffin();42}43else {44muffin = new FileMuffin();45}4647try {48load();49} catch (IOException e) {50throw new SlickException("Failed to load state on startup",e);51}52}5354/**55* Get number stored at given location56*57* @param nameOfField The name of the number to retrieve58* @return The number saved at this location59*/60public double getNumber(String nameOfField) {61return getNumber(nameOfField, 0);62}6364/**65* Get number stored at given location66*67* @param nameOfField The name of the number to retrieve68* @param defaultValue The value to return if the specified value hasn't been set69* @return The number saved at this location70*/71public double getNumber(String nameOfField, double defaultValue) {72Double value = ((Double)numericData.get(nameOfField));7374if (value == null) {75return defaultValue;76}7778return value.doubleValue();79}8081/**82* Save the given value at the given location83* will overwrite any previous value at this location84*85* @param nameOfField The name to store the value against86* @param value The value to store87*/88public void setNumber(String nameOfField, double value){89numericData.put(nameOfField, new Double(value));90}9192/**93* Get the String at the given location94*95* @param nameOfField location of string96* @return String stored at the location given97*/98public String getString(String nameOfField) {99return getString(nameOfField, null);100}101102/**103* Get the String at the given location104*105* @param nameOfField location of string106* @param defaultValue The value to return if the specified value hasn't been set107* @return String stored at the location given108*/109public String getString(String nameOfField, String defaultValue) {110String value = (String) stringData.get(nameOfField);111112if (value == null) {113return defaultValue;114}115116return value;117}118119/**120* Save the given value at the given location121* will overwrite any previous value at this location122*123* @param nameOfField location to store int124* @param value The value to store125*/126public void setString(String nameOfField, String value){127stringData.put(nameOfField, value);128}129130/**131* Save the stored data to file/muffin132*133* @throws IOException Indicates it wasn't possible to store the state134*/135public void save() throws IOException {136muffin.saveFile(numericData, fileName + "_Number");137muffin.saveFile(stringData, fileName + "_String");138}139140/**141* Load the data from file/muffin142*143* @throws IOException Indicates it wasn't possible to load the state144*/145public void load() throws IOException {146numericData = muffin.loadFile(fileName + "_Number");147stringData = muffin.loadFile(fileName + "_String");148}149150/**151* Will delete all current data held in Score152*/153public void clear() {154numericData.clear();155stringData.clear();156}157158/**159* Quick test to see if running through Java webstart160*161* @return True if jws running162*/163private boolean isWebstartAvailable() {164try {165Class.forName("javax.jnlp.ServiceManager");166// this causes to go and see if the service is available167ServiceManager.lookup("javax.jnlp.PersistenceService");168Log.info("Webstart detected using Muffins");169} catch (Exception e) {170Log.info("Using Local File System");171return false;172}173return true;174}175}176177