Java Reference
In-Depth Information
Using the two methods loadEntry() and saveEntry() , you have implemented two basic
methods that are responsible for loading and storing records to a record store in both the MIDP and
PDAP versions of the diary application. The next step to implement the diary application is to open the
diary record store in the constructor of the application.
As described in the previous section, the method openRecordStore() can throw
RecordStoreException s when it is not able to open the record store for some reason. In order to
handle these exceptions, it is necessary to put the call in a corresponding try-catch block. In the
following snippet, you throw a RuntimeException , terminating the whole application in case of a
failure. In real applications, more elaborate error handling may be necessary:
try {
diary = RecordStore.openRecordStore ("diary", true);
// load and display the last entry here.
}
catch (RecordStoreException e) {
throw new RuntimeException ("Cannot open diary; reason: "+e);
}
After the record store is opened, you jump to the last record of the Diary record store and pass the
stored String to the text widget of the Diary implementation. Because the UI widgets in MIDP and
PDAP are different, we included a comment as placeholder for the actual code. The complete code of
the constructors is contained in both full listings.
Note
If you compare the listings to the code snippets for loadEntry() and saveEntry() , you will
notice that you save an additional empty space to each record, and use String.trim() when
loading it from the RecordStore . You need this workaround because the J2ME WTK 1.0 throws an
exception if empty records are stored in a record store.
In order to make sure that the current record is written back and to avoid orphan resources, you need to
save the current entry and close the record store when the application is terminated. You do so by
implementing a corresponding destroyApp() method. Again, we've included a comment instead of
profile-dependent code where the current entry is obtained from the user interface widget:
public void destroyApp (boolean unconditional) {
try {
String text;
// fill text with content of the UI widget here
saveEntry (text);
diary.closeRecordStore();
}
catch (RecordStoreException e) {
throw new RuntimeException ("Cannot close Diary; reason: "+e);
}
}
When the code is completed with a corresponding user interface, you have a full diary application as
shown in Listings 5.1 and 5.2 . Figures 5.2 and 5.3 show the running application.
Figure 5.2. The running RmsDemoMidp application.
 
Search WWH ::




Custom Search