GlobalVar.java     Preferences mechanism     Retrieving last values entered     Keeping astro classes independent

GlobalVar.java

This static class holds global variables of the program, accessible via get methods :
  • The application paths : directories that the application uses - accessible via getXxxDir()
  • General preferences, that can be needed by several classes - accessible via getXxxPrefs().
  • Some Gui components : JEphem's main frame, the status bar, and the main HTML pane.
  • General language bundles : internationalized strings that can be needed by several classes - accessible via getBundle(int whichBundle).


  • These data remain loaded during the execution of the application, and can be accessed by any clas of JEphem.

    Preferences mechanism

    Some data can be parametrized by the user. They are stored in data/prefs/ directory in '.pref' files.
    Sun provides several mechanisms to store preferences. I used the basic one, based on java.util.properties. This class permits to easily retrieve valus from a property file.
    In the property file, the syntax is just : myKey = myValue

    To use it : load(new FileInputStream(new File("myFile.prefs")))
    The String "myValue" can be retrieved through getProperty("myKey")

    I wrote a generic sublcass, tig.TigProperties, to add some functionalities :
  • get/setIntProperty(), get/setDoubleProperty(), get/setBooleanProperty(), get/setIntArrayProperty(), to retrieve properties of various types without having to convert from String
  • .
    Like in the superclass (java.util.Properties), the get methods can be called with default values.

    An other method, public static int getIntConstant(String value, String className), permits to write clearer property files.
    Let's see its use through an example : imagine I want to store the current frame in a property file. And the application uses constants of class jephem.astro.spacetime.SpaceConstants to identify frames.
    So I should have written in the file a line like : currentFrame=2.
    Instead, I write : currentFrame=FRAME_ECLIPTIC
    and retrieve the values calling : getIntConstant("currentFrame", jephem.astro.spacetime.SpaceConstants);.

    This way, the property file is clearer, and I can change the value of FRAME_ECLIPTIC in SpaceConstants without modifying the property file.

    Retrieving last values entered

    The purpose of this mechanism is to be able to retrieve the values entered by the user in a graphical component. It has been implemented for the GUI class jephem.gui.NewEphemerisDialog.
  • The GUI class holds a static variable of type TigProperties : _lastValues. It contains the information that permit to rebuild the GUI class with the last values entered. A static boolean, _lastValuesLoaded, indicates if _lastValues have already been loaded.
    As _lastValues is static, it is kept into memory during the program execution, so instances of the class can be deleted and instantiated, the same variable will keep the last values.
  • At the end of GUI class' constructor, a method, initFields() is called. If _lastValuesLoaded == false, the values are loaded from the file.
    The GUI components of the class are then initialized from the last values.
  • The user enters the values in the GUI class, and presses OK.
  • A method, storeLastValues() is then called. _lastValues is then rebuilt from the contents of the GUI components, and stored in the file.
    This way, even if the user quits the application, the values will be restored at next run.
  • Keeping astro classes independent

    The astro API is used by GUI classes, which introduce features like preferences, internationalization... I wanted the keep the possibility to use the astro classes without this. This lead to the additions of methods, which are not used by the program :
  • jephem.astro.vsop87.VSOP87.setDataPath() permits to specify where the data are stored, without relying on jephem.astro.AstroPrefs.
  • the getXXXEnglishName methods, to be able to produce understandable results without the