Java-GNOME 2.10 GNOME Tutorial
<<< Previous |  | Next >>>

# Non-Visual Classes

GNOME is comprised of numerous libraries that provide the APIs used by developers. The two main libraries are libgnomeui and libgnome. libgnomeui contains the graphical user interface functionality while libgnome contains common routines that may be used by your application. In this chapter we will look at a few of the classes that wrap functionality contained in libgnome. To get the entire API for each of these classes please refer to the javadocs.

# Config

It is possible to save and retrieve configuration information to a file from your GNOME application. You can use this capability to store user preferences like fonts, colors, size, etc. or you can use this to store userid and passwords. The information is stored in a file in one of two locations depending on the methods used. The two files are ${HOME}/.gnome/"appname" and ${HOME}/.gnome_private/"appname". The .gnome directory is viewable to the world while the .gnome_private directory is only viewable by the user of the application.

To demonstrate the usage of the Config class we have two small examples.

Example 1. WriteConfig.java - Config

    import org.gnu.gnome.Config;
    import org.gnu.gnome.Program;

    public class WriteConfig {

    	public WriteConfig() {

    		// items to appear in .gnome directory
    		Config.setString("writeconfig/cool/bindings", "java-gnome");
    		Config.setInt("writeconfig/cool/width", 16);
    		Config.setDouble("writeconfig/cool/version", 0.6);
    		Config.setBoolean("writeconfig/cool/very_cool", true);

    		// items to appear in .gnome_private directory
    		Config.setPrivateString("writeconfig/user/password", "javaman");
    		Config.setPrivateInt("writeconfig/user/age", 2);
    		Config.setPrivateDouble("writeconfig/user/salary", 100.10);
    		Config.setPrivateBoolean("writeconfig/user/is_lazy", true);

    		Config.sync();
    	}

    	public static void main(String[] args) {

    		Program.initGnome("WriteConfig Example", "1.0", args);

    		WriteConfig write = new WriteConfig();

    	}
    }

This simple example will write information to two separate files. The Config.set...() series of methods will write information to the ${HOME}/.gnome/writeconfig file while the Config.setPrivate...() series of methods will write information to the ${HOME}/.gnome_private/writeconfig file. The information in the .gnome file is placed under a section with the heading [cool] while the information in the .gnome_private file is placed under a section with the heading [user]. After all of the data is set a call is placed to the Config.sync() method to flush all of the information to disk. To read this information you could use the following example:

Example 2. ReadConfig.java - Config

    import org.gnu.gnome.Config;
    import org.gnu.gnome.Program;

    public class ReadConfig {

    	public ReadConfig() {

    		// items to appear in .gnome directory
    		System.out.println("Bindings = " +
    			Config.getString("writeconfig/cool/bindings"));
    		System.out.println("Width = " +
    			Config.getInt("writeconfig/cool/width"));
    		System.out.println("Version = " +
    			Config.getDouble("writeconfig/cool/version"));
    		System.out.println("Very Cool = " +
    			Config.getBoolean("writeconfig/cool/very_cool"));

    		// items to appear in .gnome_private directory
    		System.out.println("Private password = " +
    			Config.getPrivateString("writeconfig/user/password"));
    		System.out.println("Private age = " +
    			Config.getPrivateInt("writeconfig/user/age"));
    		System.out.println("Private salary = " +
    			Config.getPrivateDouble("writeconfig/user/salary"));
    		System.out.println("Private is lazy = " +
    			Config.getPrivateBoolean("writeconfig/user/is_lazy"));
    	}

    	public static void main(String[] args) {

    		Program.initGnome("ReadConfig Example", "1.0", args);
    		ReadConfig read = new ReadConfig();
    	}
    }

This example will read and print the values written to the configuration files by the previous example.

<<< Previous | Home | Next >>>
ButtonBox |  | Sound