Class SaveState

Direct Known Subclasses:
PreferenceState

public class SaveState extends XmlProperties
Class for saving name/value pairs as XML or Json. Classes that want to be able to save their state can do so using the SaveState object. The idea is that each state variable in the class is first saved into a SaveState object via a String key. Then the SaveState object is written out as XML or Json. When the save state object is restored, the SaveState object is constructed with an XML Element or JsonObject that contains all of the name/value pairs. Since the "get" methods require a default value, the object that is recovering its state variables will be successfully initialized even if the given key,value pair is not found in the SaveState object.

Note: Names for options are assumed to be unique. When a putXXX() method is called, if a value already exists for a name, it will be overwritten.

The SaveState supports the following types:

      java primitives
      arrays of java primitives
      String
      Color
      Font
      KeyStroke
      File
      Date
      Enum
      SaveState (values can be nested SaveStates)
  
  • Constructor Details

    • SaveState

      public SaveState(String name)
      Creates a new SaveState object with a non-default name. The name serves no real purpose other than as a hint as to what the SaveState represents
      Parameters:
      name - of the state
    • SaveState

      public SaveState()
      Default Constructor for SaveState; uses "SAVE_STATE" as the name of the state.
      See Also:
    • SaveState

      public SaveState(File file) throws IOException
      Construct a SaveState from a file containing XML from a previously saved SaveState.
      Parameters:
      file - the file containing the XML to read.
      Throws:
      IOException - if the file can't be read or is not formatted properly for a SaveState
    • SaveState

      public SaveState(org.jdom.Element element)
  • Method Details

    • saveToFile

      public void saveToFile(File file) throws IOException
      Write the saveState to a file as XML
      Parameters:
      file - the file to write to.
      Throws:
      IOException - if the file could not be written
    • putSaveState

      public void putSaveState(String name, SaveState value)
      Associates a sub SaveState value with the given name.
      Parameters:
      name - The name in the name,value pair.
      value - The value in the name,value pair.
    • getSaveState

      public SaveState getSaveState(String name)
      Returns the sub SaveState associated with the given name.
      Parameters:
      name - The name associated with the desired Element.
      Returns:
      The SaveState object associated with the given name.
    • processElement

      protected void processElement(org.jdom.Element element)
      Overrides:
      processElement in class GProperties
    • createElement

      protected org.jdom.Element createElement(String key, Object value)
      Overrides:
      createElement in class GProperties