jgame.platform
Class AppConfig

java.lang.Object
  extended by jgame.platform.AppConfig

public class AppConfig
extends java.lang.Object

A generic class for handling the user configuration of application settings. It manages a set of fields that can be loaded, saved, and edited. It can load and save configurations from/to both a file and an object (reading/setting fields in the object), and it can create a GUI for editing them. It can also be run on just an object (excluding the possibility of saving to a file) or on just a file. The class has a standard main() for editing a previously saved file, use java jgame.AppConfig [filename].

The types and names of the fields can be obtained by either loading them from a previously saved file, or by deriving them from the object to be configured. AppConfig maintains a copy of the configurable fields and their types. You can choose when to load or save the contents of the AppConfig object to/from object or file. The GUI comes with a "Save" button (save to file and to object) and an "Apply" button (save to object, not to file). This class does not handle mismatches between fields in an object and in a corresponding file very robustly yet.

The Gui consists of a list of editable form fields. The name to display next to each field can be set manually or determined automatically from the field name in the object. The order in which the Gui fields are displayed is the same as that in which they are defined.

GameConfig is able to handle several types of fields. It determines the type of Gui element and the file representation automatically from the type. The following file format is used: each field is found on a separate line, in the format [fieldname] '\t' [guiname] '\t' [type] '\t' [value] It handles the following field types:

 Class/Type    GUI representation    file representation
 int            number field          int [number]
 int            keystroke field       key [keycode]
 double         number field          double [number]
 boolean        button                boolean [true/false]
 String         textfield             String [string, \n is escaped as '\n']
 


Constructor Summary
AppConfig(java.lang.String title, java.lang.Class cls)
          Configure a class with static fields without the possibility of file storage.
AppConfig(java.lang.String title, java.lang.Class cls, java.lang.String filename)
          Configure a class with static fields with corresponding file.
AppConfig(java.lang.String title, java.lang.Object obj)
          Configure an object without the possibility of file storage.
AppConfig(java.lang.String title, java.lang.Object obj, java.lang.String filename)
          Configure an object with corresponding file.
AppConfig(java.lang.String title, java.lang.String filename)
          Configure a file.
 
Method Summary
 void closeGui()
          Hide the Gui in case it was visible.
 void defineField(java.lang.String fieldname, java.lang.String guiname, java.lang.String type)
          Define a field as configurable, ignore if field already exists.
 void defineFields(java.lang.String prefix, java.lang.String suffix, java.lang.String guiprefix, java.lang.String guisuffix)
          Define a set of fields as configurable, by matching the object's fields with the given pattern.
 void defineFields(java.lang.String prefix, java.lang.String suffix, java.lang.String guiprefix, java.lang.String guisuffix, java.lang.String type)
          Define a set of fields as configurable, by matching the object's fields with the given pattern.
 java.lang.String getFilename()
          Get the filespec of the file that this AppConfig is associated with, null if only object or class is defined.
 java.lang.Object getObject()
          Get the object that this AppConfig is associated with, null if not defined.
 java.lang.Class getObjectClass()
          Get the class that this AppConfig is associated with, null if only a file is defined.
 boolean isGuiOpen()
          Check if GUI is visible right now.
 void loadFromFile()
          Load settings from file.
 void loadFromObject()
          Load settings from previously specified fields in object.
static void main(java.lang.String[] args)
          Edit a previously created file.
 void openGui()
          Make the Gui visible on screen, and let the user make a selection whether to save, apply, or just close.
 void saveToFile()
          Save settings to file.
 void saveToObject()
          Save settings into object.
 void setField(java.lang.String field, java.lang.Object value, boolean update_gui)
          Set field's value, given that field is already defined.
 void setGuiSettings(java.awt.Font mainfont, java.awt.Font bigfont, java.awt.Color textcolor, java.awt.Color bgcolor, java.awt.Color hltcolor)
          Configure the look of the GUI
 void setListener(java.awt.event.ActionListener list)
          Set listener for special events.
 void setObject(java.lang.Object obj)
          Set or replace the object that this AppConfig should be associated with.
 java.lang.String toString()
           
 void waitUntilGuiClosed()
          Block until the Gui closes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AppConfig

public AppConfig(java.lang.String title,
                 java.lang.String filename)
Configure a file. No settings are defined or loaded from the file.


AppConfig

public AppConfig(java.lang.String title,
                 java.lang.Object obj,
                 java.lang.String filename)
Configure an object with corresponding file. No settings are defined or loaded from the object or file.

Parameters:
filename - filespec, or null if no file

AppConfig

public AppConfig(java.lang.String title,
                 java.lang.Class cls,
                 java.lang.String filename)
Configure a class with static fields with corresponding file. No settings are defined or loaded from the object or file.

Parameters:
filename - filespec, or null if no file

AppConfig

public AppConfig(java.lang.String title,
                 java.lang.Object obj)
Configure an object without the possibility of file storage. No settings are defined or loaded from the object.


AppConfig

public AppConfig(java.lang.String title,
                 java.lang.Class cls)
Configure a class with static fields without the possibility of file storage. No settings are defined or loaded from the object.

Method Detail

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

setGuiSettings

public void setGuiSettings(java.awt.Font mainfont,
                           java.awt.Font bigfont,
                           java.awt.Color textcolor,
                           java.awt.Color bgcolor,
                           java.awt.Color hltcolor)
Configure the look of the GUI

Parameters:
mainfont - font used for labels and fields
bigfont - font used for title and buttons
textcolor - colour used for all text
bgcolor - colour used for background
hltcolor - colour used for highlighted background and fields

main

public static void main(java.lang.String[] args)
Edit a previously created file. The first argument is the filepath.


getObject

public java.lang.Object getObject()
Get the object that this AppConfig is associated with, null if not defined.


setObject

public void setObject(java.lang.Object obj)
Set or replace the object that this AppConfig should be associated with.


getObjectClass

public java.lang.Class getObjectClass()
Get the class that this AppConfig is associated with, null if only a file is defined.


getFilename

public java.lang.String getFilename()
Get the filespec of the file that this AppConfig is associated with, null if only object or class is defined.


setListener

public void setListener(java.awt.event.ActionListener list)
Set listener for special events. Currently, there is only the quit event: the action listener is called when the appconfig window is closed.

Parameters:
list - null means disable

loadFromFile

public void loadFromFile()
Load settings from file. Is ignored if filename is not specified or file does not exist. Throws error if i/o error occurs during read. If a field name found in the file is not yet defined, it is defined here. Behaviour is undefined if fields in object and file do not match.


saveToFile

public void saveToFile()
Save settings to file. Is ignored if filename is not specified. Throws error if file cannot be accessed.


loadFromObject

public void loadFromObject()
Load settings from previously specified fields in object.


saveToObject

public void saveToObject()
Save settings into object.


defineField

public void defineField(java.lang.String fieldname,
                        java.lang.String guiname,
                        java.lang.String type)
Define a field as configurable, ignore if field already exists. If guiname is null, fieldname is used as the name to display in the GUI. The type is given by type. The value of the field is set to the value found in the object or class, if defined, or null otherwise. If we have only a class, the field must be static.


defineFields

public void defineFields(java.lang.String prefix,
                         java.lang.String suffix,
                         java.lang.String guiprefix,
                         java.lang.String guisuffix)
Define a set of fields as configurable, by matching the object's fields with the given pattern. Field type is derived from the field.

See Also:
defineFields(String,String,String,String,String)

defineFields

public void defineFields(java.lang.String prefix,
                         java.lang.String suffix,
                         java.lang.String guiprefix,
                         java.lang.String guisuffix,
                         java.lang.String type)
Define a set of fields as configurable, by matching the object's fields with the given pattern. The configured object is searched for public fields matching the given prefix and suffix. The name displayed in the gui is obtained by removing the prefix and suffix, and adding the given guiprefix and guisuffix. Note that underscores in the fields are replaced by spaces.

Parameters:
type - fieldtype, null means derive from field
See Also:
defineField(String,String,String)

setField

public void setField(java.lang.String field,
                     java.lang.Object value,
                     boolean update_gui)
Set field's value, given that field is already defined. Value may be any class or String for String representations of fields.


openGui

public void openGui()
Make the Gui visible on screen, and let the user make a selection whether to save, apply, or just close.


closeGui

public void closeGui()
Hide the Gui in case it was visible.


isGuiOpen

public boolean isGuiOpen()
Check if GUI is visible right now.


waitUntilGuiClosed

public void waitUntilGuiClosed()
Block until the Gui closes.