
//Lesson2Screen.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.*;
import net.rim.blackberry.api.invoke.*;
import net.rim.blackberry.api.mail.*;

//this is the class for the Lesson2Screen
class Lesson2Screen extends MainScreen { 
    //create a button here with the text 'Create eMail'
    //ButtonField.CONSUME_CLICK will prevent the Menu from showing up when the user clicks the button.
    ButtonField btnSubmit = new ButtonField("Create eMail", ButtonField.CONSUME_CLICK){
        protected boolean navigationClick( int status, int time ){
            Invoke.invokeApplication(Invoke.APP_TYPE_MESSAGES,new MessageArguments(MessageArguments.ARG_NEW,eTo.getText(),eSubject.getText(),eBody.getText()));        
            return true;
        }
    };
    
    //create a button to send of the mail
    ButtonField btnSend = new ButtonField("Send eMail", ButtonField.CONSUME_CLICK){
        protected boolean navigationClick( int status, int time ){
            sendMail(eTo.getText(),eSubject.getText(),eBody.getText());
            return true;
        }
    };    
    
    //ordinal - Ordering parameter, lower values are placed closer to the top of the menu screen.
    //priority - The priority of the menu item. A lower value indicates a higher priority, conversely a higher value indicates a lower priority.
    MenuItem myMenu = new MenuItem("My Menu!", 1/*ordinal*/, 100/*priority*/) {
        public void run() {
            Dialog.alert("You clicked on my Menu!");
        }
    };
    
    //this creates a colored label
    LabelField topLabel = new LabelField("** Hello World **", LabelField.FIELD_HCENTER){
        public void paint(Graphics graphics){
            //you can set the color here
            graphics.setColor(0x00AA0099);
            super.paint(graphics);
        }
    }; 
    
    EditField eTo = new EditField("To:","");
    EditField eSubject = new EditField("Subject:","");
    EditField eBody = new EditField("Body:","");
    
    public Lesson2Screen() {
        super();
        LabelField title = new LabelField("Create eMail messages");
        //set the title of the application
        setTitle(title);
        
        add(topLabel); //adds the colored label here on top of the other fields
        add(new SeparatorField());
        
        add(eTo); //adds the address field to the screen
        add(eSubject); //the editfield for the subject line
        add(eBody); //edit field for the body
        
        //a separator item; long, thin, horizontal line from the left to the right of the screen
        add(new SeparatorField());
        
        addMenuItem(myMenu); //adds the menu to the screen
        add(btnSubmit); //adds the button to the screen
        add(btnSend);
    }
   
        public void sendMail(String to, String subject, String body){
                        try {
                    // set dst folder
                    Store store = Session.getDefaultInstance().getStore();
                    Folder[] folders = store.list(Folder.SENT);
                    Folder sentfolder = folders[0];
                    
                    // create a new message
                    Message msg = new Message(sentfolder);
                    
                    // make adressed
                    Address toList[] = new Address[1];
                    toList[0]= new Address(to, to);
                    msg.addRecipients(Message.RecipientType.TO, toList);
                    
                    // make message
                    msg.setSubject(subject);
                    msg.setContent(body);
                    Transport.send(msg); // send it off
                    } catch(Exception e){}
        }
        
    //...when this screen closes, onClose() will be run
    public boolean onClose() {
        //force the app to quit
        System.exit(0);
        return true;
    }  
}

