
//Lesson3Screen.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.menuitem.*;
import net.rim.blackberry.api.browser.BrowserSession.*;
import net.rim.blackberry.api.browser.*;

//this is the class for the Lesson3Screen
class Lesson3Screen extends MainScreen { 

    //ButtonField.CONSUME_CLICK will prevent the Menu from showing up when the user clicks the button.
    ButtonField btnWebpage = new ButtonField("Open Google", ButtonField.CONSUME_CLICK){
        protected boolean navigationClick( int status, int time ){
                    BrowserSession session = Browser.getDefaultSession();
                    session.displayPage("http://www.google.com");
                    session.showBrowser();   
            return true;
        }
    };
    
    ButtonField btnYesNo = new ButtonField("Yes/No Dialog",ButtonField.CONSUME_CLICK){
        protected boolean navigationClick(int status, int time){
                    int response = Dialog.ask(Dialog.D_YES_NO, "Yes or No?");
                    
                            if (Dialog.YES == response) {
                                Dialog.alert("Yes");
                            } 
                            else {
                                Dialog.alert("No");
                            }            
            return true;
        }
    };
    
    ButtonField btnAddMenu = new ButtonField("Add a Menu Item to the System", ButtonField.CONSUME_CLICK){
        protected boolean navigationClick( int status, int time ){
                ApplicationMenuItemRepository amir = ApplicationMenuItemRepository.getInstance();
                amir.addMenuItem(ApplicationMenuItemRepository.MENUITEM_SYSTEM,new SampleMenuItem());  
                /*
                Instead of adding it to the system-wide Menu (.MENUITEM_SYSTEM) you can add it also only to the following:
 
 
 Category: Signed  static long     MENUITEM_ADDRESSBOOK_LIST
          ApplicationMenuItem instances registered with this ID appear when the address book is open in list mode.
 Category: Signed  static long     MENUITEM_ADDRESSCARD_EDIT
          ApplicationMenuItem instances registered with this ID appear when an address card is open in edit mode.
 Category: Signed   static long     MENUITEM_ADDRESSCARD_VIEW
          ApplicationMenuItem instances registered with this ID appear when an address card is open in view mode.
 Category: Signed   static long     MENUITEM_ALARM
          ApplicationMenuItem instances registered with this ID appear when the alarm application is running.
 Category: Signed  static long     MENUITEM_BROWSER
          ApplicationMenuItem instances registered with this ID appear when the browser application is running.
 Category: Signed  static long     MENUITEM_CALENDAR
          ApplicationMenuItem instances registered with this ID appear when the calendar is open in view mode.
 Category: Signed  static long     MENUITEM_CALENDAR_EVENT
          ApplicationMenuItem instances registered with this ID appear when a calendar event is open in view/edit mode.
 Category: Signed  static long     MENUITEM_CAMERA_PREVIEW
          ApplicationMenuItem instances registered with this ID appear when using the camera preview screen.
 Category: Signed  static long     MENUITEM_EMAIL_EDIT
          ApplicationMenuItem instances registered with this ID appear when the email application is open in edit mode.
 Category: Signed  static long     MENUITEM_EMAIL_VIEW
          ApplicationMenuItem instances registered with this ID appear when the email application is open in view mode.
 Category: Signed  static long     MENUITEM_FILE_EXPLORER
          ApplicationMenuItem instances registered with this ID appear when the File Explorer application is running.
 Category: Signed  static long     MENUITEM_FILE_EXPLORER_BROWSE
          ApplicationMenuItem instances registered with this ID appear when a user uses the File Explorer application to browse files and folders.
 Category: Signed   static long     MENUITEM_FILE_EXPLORER_ITEM
          ApplicationMenuItem instances registered with this ID appear when a user uses the File Explorer application to open a file.
 Category: Signed   static long     MENUITEM_MAPS
          ApplicationMenuItem instances registered with this ID appear when the maps application is running.
 Category: Signed    static long     MENUITEM_MEMO_EDIT
          ApplicationMenuItem instances registered with this ID appear when a memo is opened for editing.
 Category: Signed  static long     MENUITEM_MEMO_LIST
          ApplicationMenuItem instances registered with this ID appear when the memo list is displayed.
 Category: Signed    static long     MENUITEM_MEMO_VIEW
          ApplicationMenuItem instances registered with this ID appear when a memo is opened for viewing.
 Category: Signed  static long     MENUITEM_MESSAGE_LIST
          ApplicationMenuItem instances registered with this ID appear when the message list is displayed.
 Category: Signed      static long     MENUITEM_MMS_EDIT
          ApplicationMenuItem instances registered with this ID appear when the MMS application is open in edit mode.
 Category: Signed       static long     MENUITEM_MMS_VIEW
          ApplicationMenuItem instances registered with this ID appear when the MMS application is open in view mode.
 Category: Signed       static long     MENUITEM_MUSIC_SERVICE_ITEM
          ApplicationMenuItem instances registered with this ID appear in the services section in the Music domain of the Media application.
 Category: Signed      static long     MENUITEM_PHONE
          ApplicationMenuItem instances registered with this ID appear when the phone application is running.
 Category: Signed  static long     MENUITEM_PHONELOG_VIEW
          ApplicationMenuItem instances registered with this ID appear when a call log is opened for viewing.
 Category: Signed        static long     MENUITEM_SMS_EDIT
          ApplicationMenuItem instances registered with this ID appear when the SMS application is open in edit mode.
 Category: Signed       static long     MENUITEM_SMS_VIEW
          ApplicationMenuItem instances registered with this ID appear when the SMS application is open in view mode.
 Category: Signed     static long     MENUITEM_TASK_EDIT
          ApplicationMenuItem instances registered with this ID appear when a task is opened in view/edit mode.
 Category: Signed    static long     MENUITEM_TASK_LIST
          ApplicationMenuItem instances registered with this ID appear when the task list is displayed.
 Category: Signed    static long     MENUITEM_VIDEO_RECORDER
          ApplicationMenuItem instances registered with this ID appear when using the video recorder screen.
 Category: Signed  static long     MENUITEM_VIDEO_SERVICE_ITEM
  */
                Dialog.alert("A Menu Item was added to the System.");
            return true;
        }
    };
    
    public Lesson3Screen() {
        super();
        LabelField title = new LabelField("Open a Website");
        //set the title of the application
        setTitle(title);
        
        add(btnAddMenu);
        add(btnYesNo);
        add(btnWebpage); //adds the button to the screen
     
    }
   
        private static class SampleMenuItem extends ApplicationMenuItem {
        SampleMenuItem() {
            super(1);
        }
        
        public String toString() {
            return "My Menu Item!";
        }
        
        public Object run(Object context) {
            Dialog.alert("My Menu was clicked");
            return null;
        }
    }
    
    
    //...when this screen closes, onClose() will be run
    public boolean onClose() {
        //force the app to quit
        System.exit(0);
        return true;
    }  
}

