Registry

There is another pair of classes in the WFC app package that can be very useful to the app programmer. The classes Registry and RegistryKey allow the app to save values from one execution to another. For example, your app might store the current directory before it exits so that the next time it's executed it can start in that directory.

NOTE
The Windows registry is a hierarchical database within Windows, which is saved to disk when Windows exits and restored when Windows starts back up.

The RegistryDemo app below demonstrates how an app can access the registry from Visual J Plus Plus.

RegistryDemo app

The RegistryDemo app attempts to read the time of last execution from the registry. If no time is found stored there, the app displays the appropriate message. If a time is found, it's displayed in text format. RegistryDemo then stores the current time into the registry so that it can be read the next time the app runs.

To further enhance the demonstration, I have added a Clear Registry button to remove the time from the registry and a ReRead Registry button to read and store the time as if the app were just starting.

Forms Designer work

After creating the RegistryDemo app as a Windows app, open Form1.java in the Forms Designer. Change the form's text property to Registry Demo. Now add an Edit control. Rename the control to outputEdit, and anchor it to the top, left, and right edges of the form. Now add a Label control with the text app last executed on: above the Edit control.

Add two buttons immediately below outputEdit. Label the left button Clear Registry and the right button ReRead Registry. Anchor the left button to the left and bottom edges and the right button to the right and bottom edges of the form. Finally, double-click each button to create the button_click() methods.

code

The code for the RegistryDemo app is contained in the following Form1.java file:

import com.ms.wfc.app.*;
import com.ms.wfc.core.*;
import com.ms.wfc.ui.*;
import com.ms.wfc.html.*;
/**
 * Demonstrate the storing of information in the registry.
 */
public class Form1 extends Form
{
 public Form1()
 {
 // Required for Visual J Plus Plus Form Designer support
 initForm();
 // read time from registry, and then update registry
 readAndUpdateRegistry(outputEdit);
 }
 /**
 * Read the time of last execution from the registry,
 * and write it to the output control; then update the
 * registry with the current time.
 */
 public void readAndUpdateRegistry(Control output)
 {
 // get the time of last execution from the registry
 Time time = getRegistryTime();
 // if it's null…
 if (time == null)
 {
 // then app hasn't executed before
 output.setText("app never run before");
 }
 else
 {
 // otherwise, convert the time into long format
 // and display it
 String sTime = toString(time);
 output.setText(sTime);
 }
 // now store the current date and time into the
 // registry for next time
 setRegistryTime(new Time());
 }
 /**
 * Display the specified time in the output control.
 */
 String toString(Time time)
 {
 String sDate = time.formatLongDate();
 String sTime = time.formatLongTime();
 return sDate + " " + sTime;
 }
 /**
 * Get the Time object stored in the registery; if there
 * is none, return a null.
 */
 public Time getRegistryTime()
 {
 try
 {
 // get the string representation of the time
 // last executed out of the registry
 RegistryKey appKey = getAppKey();
 String sTime = (String)appKey.getValue("Time");
 // now convert this into a Time object
 Time time = new Time(sTime);
 return time;
 }
 catch(Exception e)
 {
 }
 // in the event of an error (e.g., if there is no
 // time stored), return null
 return null;
 }
 /**
 * Store the specified time into the registry.
 */
 public void setRegistryTime(Time time)
 {
 // store the string representation of time into
 // the "Time" subkey of our app's registry entry
 RegistryKey appKey = getAppKey();
 appKey.setValue("Time", time.toString());
 }
 /**
 * Delete the registry time.
 */
 public void deleteRegistryTime()
 {
 try
 {
 RegistryKey appKey = getAppKey();
 appKey.deleteValue("Time");
 }
 catch(Exception e)
 {
 }
 }
 /**
 * Return the registry key for this app.
 */
 private RegistryKey getAppKey()
 {
 return Registry.CURRENT_USER.createSubKey(
 "Software\\ProgrammingJava\\RegistryDemo");
 }
 private void button1_click(Object source, Event e)
 {
 deleteRegistryTime();
 }
 private void button2_click(Object source, Event e)
 {
 readAndUpdateRegistry(outputEdit);
 }
 /**
 * NOTE: The following code is required by the Visual J Plus Plus form
 * designer. It can be modified using the form editor. Do not
 * modify it using the code editor.
 */
 Container components = new Container();
 Edit outputEdit = new Edit();
 Label label1 = new Label();
 Button button1 = new Button();
 Button button2 = new Button();
 private void initForm()
 {
 // …created by the Forms Designer…
 }
 /**
 * The main entry point for the app. */
 public static void main(String args[])
 {
 app.run(new Form1());
 }
}


After calling initForm(), the Form1() constructor calls readAndUpdateRegistry().

The readAndUpdateRegistry() method starts by invoking Form1.getRegistryTime() to fetch a Time object containing the time of last execution from the registry. If getRegistryTime() returns null, the program outputs the message "app never run before". If a Time object is returned, the Time object is converted in long format into a String object by invoking the local toString() method. Next the String is written to the output control. Finally, the current time is passed to the Form1.setRegistryTime() method to be stored into the registry.

The setRegistryTime(time) method starts by fetching a RegistryKey object for the current app by calling Form1.getAppKey(). The getAppKey() method returns the registry key "CURRENT_USER\Software\ProgrammingJava\RegistryDemo". The setRegistryTime() method converts time into a String and stores that string under the subkey "Time" using the setValue() method.

The getRegistryTime() method calls getAppKey() to retrieve the RegistryKey for this app. Then getRegistryTime() calls RegistryKey.getValue() to return the time string stored earlier under the subkey "Time". This string, sTime, is then converted into a Time object that is returned to the caller. If an exception is thrown at any point (for example, if there is no "Time" subkey), getRegistryTime() returns null.

Finally, Form1.deleteRegistryTime() calls RegistryKey.deleteValue() to delete the "Time" subkey in the app's registry entry.

The Clear Registry button handler (button1_click()) invokes the method delete- RegistryTime(), and the ReRead Time button handler (button2_click()) simply calls readAndUpdateRegistry().

result

Screenshot-6 shows the output from RegistryDemo the first time it runs (or the first time that it's run after pressing the Clear Registry button). Figure 10-7 shows the previous execution time when I ran RegistryDemo a subsequent time.

Screenshot

Screenshot-6. The output from RegistryDemo the first time the program runs.

Screenshot

Screenshot-7. The output from the RegistryDemo app the second time I ran the program. Comments