Saturday, December 17, 2011

PopupScreen example in Blackberry


import java.util.Vector;

import mypackage.LoginScreen;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiEngine;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;



public class PasswordBox extends PopupScreen implements KeyListener, TrackwheelListener
{
   private               String                 m_response;
   private               PasswordEditField      m_answer;
   private               String                 m_password;
   private               String                 m_userName;
   
   public PasswordBox()
   {
       super(new VerticalFieldManager(),Field.FOCUSABLE);

       m_password="moorthy";
                        
       LabelField question = new LabelField("Enter password to unlock.");
       m_answer = new PasswordEditField(" ","");
       add(question);
       add(new SeparatorField());
       add(m_answer);
     
   }
 
 
// This function gets called if the password match
 public void accept()
   {
 close();
 UiEngine ui = Ui.getUiEngine();
 ui.pushScreen(new LoginScreen(0));

        //System.exit(0);
     
   }
 
 

 
 
   ////////////////////////////////////////////
   /// implementation of TrackwheelListener
   ////////////////////////////////////////////
   public boolean trackwheelClick(int status, int time)
   {
       m_response = m_answer.getText();
       if (m_response.equals(m_password))
       {
           accept();
       }
       else
       {
         //  Dialog.alert("Invalid Password !");
       }
       return true;
   }
 
 
   /** Invoked when the trackwheel is released */
   public boolean trackwheelUnclick(int status, int time)
   {
       return false;
   }
 
 
   /** Invoked when the trackwheel is rolled. */
   public boolean trackwheelRoll(int amount, int status, int time)
   {
       return true;
   }
 
 
   /////////////////////////////////////
   /// implementation of Keylistener
   /////////////////////////////////////
   public boolean keyChar(char key, int status, int time)
   {
       //intercept the ESC key - exit the app on its receipt
       boolean retval = false;
       switch (key)
       {
           case Characters.ENTER:
               m_response = m_answer.getText();
               if (m_response.equals(m_password))
               {
                   accept();
               }
               // an alert is displayed if the password is incorrect
               else
               {
                //   Dialog.alert("Invalid Password !");
               }
               retval = true;
               break;
         
             
             
           default:
               retval = super.keyChar(key,status,time);
       }
       return retval;
   }
 
   /** Implementation of KeyListener.keyDown */
   public boolean keyDown(int keycode, int time)
   {
      if(Keypad.key(keycode)==Keypad.KEY_END)
        {
          Application.getApplication().requestForeground();
                  return false;
                 
       }
      return false;
   }
   /** Implementation of KeyListener.keyRepeat */
   public boolean keyRepeat(int keycode, int time)
   {
     
       return false;
   }
   /** Implementation of KeyListener.keyStatus */
   public boolean keyStatus(int keycode, int time)
   {
       return false;
   }
   /** Implementation of KeyListener.keyUp */
   public boolean keyUp(int keycode, int time)
 
   {
    if(Keypad.key(keycode)==Keypad.KEY_END)
        {
             Application.getApplication().requestForeground();
                  return false;
                 
       }
       return false;
   }
 
 /* Invoked when this screen is obscured. A Screen is obscured when it is was the topmost and is no longer by means of:
     (1).  a new screen pushed on the display stack
        (2).  a global screen is displayed above this screen
  (3).  this screen's application goes into the background
      (4).  this screen is pushed and it is deemed obscured by the above rules
 */

   protected void onObscured()
   {
   
     // Application.getApplication().requestForeground();
   }
}

No comments:

Post a Comment