
//GreetingScreen.java
//import all the needed packages
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

//this is the class for the greeting screen
class GreetingScreen extends MainScreen { 
    //create a button here with the text 'Submit!'
    //ButtonField.CONSUME_CLICK will prevent the Menu from showing up when the user clicks the button.
    ButtonField btnSubmit = new ButtonField("Submit!", ButtonField.CONSUME_CLICK);
    
    //create an EditField (where you can type in strings)
    EditField ef = new EditField("Enter your name: ","");
    
    public GreetingScreen() {
        super();
        LabelField title = new LabelField("My App");
        //set the title of the application
        setTitle(title);
        
        //a normal label in the app, just to display text, anchored left
        LabelField label = new LabelField("This is my first app!", LabelField.FIELD_LEFT);
        //add the label to the screen
        add(label);
       
        //a separator item; long, thin, horizontal line from the left to the right of the screen
        add(new SeparatorField());
       
        //add the edit field to the screen
        add(ef);
        
        //this listener 'listens' for any event (see at the bottom)
        FieldListener listener = new FieldListener();
        //assign that listener to the button
        btnSubmit.setChangeListener(listener);
        add(btnSubmit);
    }
    
    //...when this screen closes, onClose() will be run
    public boolean onClose() {
        //if the entered text in the EditField is not empty, show the message
        if(!ef.getText().equals("")){
            Dialog.alert("Cheers, "+ef.getText()+"!");    
        }
        //force the app to quit
        System.exit(0);
        return true;
    }
    
    //the field listener waiting for an event in the UI (like a button pressed)
    class FieldListener implements FieldChangeListener {    
        public void fieldChanged(Field f, int context){
            //if the submit button is clicked
            if (f == btnSubmit){
                //if the EditField is empty
                if(ef.getText().equals("")){
                    Dialog.alert("Please enter your name in the field.");
                }else{ // if it is not empty, display the message
                    Dialog.alert("Hello, I am "+ef.getText()+"!");
                }
            }
            if(f == ef) {
                //text changed in the ef-Field (EditField)
            }
        }    
    }    
}
