HomeScreen.java



/*
 * HomeScreen.java
 *
 * © <your company here>, 2003-2005
 * Confidential and proprietary.
 */

package myapp;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import java.util.*;

/**
 * 
 */
class HomeScreen extends MainScreen {
    HomeScreen() {
        // Set the window title
        LabelField applicationTitle = new LabelField("Hello BlackBerry");
        setTitle(applicationTitle);
        
        // Add a label to show application status
        _statusLabel = new LabelField( "Status: Started");
        add( _statusLabel );
        
        // Add an edit field for entering new names
        _nameEditField = new EditField( "Name: ", "", 50, EditField.USE_ALL_WIDTH );
        add (_nameEditField );
        
        // Add an ObjectListField for displaying a list of names
        _nameListField = new ObjectListField();
        add( _nameListField );

        // Add a menu item
        addMenuItem(_addToListMenuItem);
        
        // Add sync menu item
        addMenuItem(_syncMenuItem);        

        // Add reset menu item
        addMenuItem(_resetMenuItem);        
                
        // Create database and connect
        try{
            _da = DataAccess.getDataAccess(false);
            _statusLabel.setText("Status: Connected");
        }
        catch( Exception ex)
        {
            _statusLabel.setText("Exception: " + ex.toString() );
        }
    
        // Fill the ObjectListField
        this.refreshNameList();
    }
    
    public void refreshNameList(){
        try{
            //Clear the list
            _nameListField.setSize(0);
            //Refill from the list of names
            Vector nameVector = _da.getNameVector();
            for( Enumeration e = nameVector.elements(); e.hasMoreElements(); ){
                NameRow nr = ( NameRow )e.nextElement();
                _nameListField.insert(0, nr);
            }
        } catch ( Exception ex){
            Dialog.alert(ex.toString());
        }
    }
    
    private void onAddToList(){
        String name = _nameEditField.getText();
        _da.insertName(name);
        this.refreshNameList();
        _nameEditField.setText("");
        _statusLabel.setText(name + " added to list");
    }

    private void onSync(){
        try{
            if( _da.sync() ){
                _statusLabel.setText("Synchronization succeeded");
            } else {
                _statusLabel.setText("Synchronization failed");
            }
            this.refreshNameList();
        } catch ( Exception ex){
            Dialog.alert( ex.toString() );
        }
    }

    private void onReset(){
        _da.complete();
        try{
            _da = DataAccess.getDataAccess(true);
            _statusLabel.setText("Status: Connected");
            this.refreshNameList();
        }
        catch( Exception ex)
        {
            _statusLabel.setText("Exception: " + ex.toString() );
        }      
    }
    
    private LabelField _statusLabel;
    private DataAccess _da;
    private EditField _nameEditField;
    private ObjectListField _nameListField;
    private MenuItem _addToListMenuItem = new MenuItem("Add", 1, 1){
        public void run() {
            onAddToList();
        }
    };
    private MenuItem _syncMenuItem = new MenuItem("Sync", 2, 1){
        public void run() {
            onSync();
        }
    };
    private MenuItem _resetMenuItem = new MenuItem("Reset", 3, 1){
        public void run() {
            onReset();
        }
    };
}