Difference between revisions of "Teams Winter 2011/team1/BlackBerry/Add Menu to the Application"

From CDOT Wiki
Jump to: navigation, search
(4.b. Add Image to the Menu)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
=== 4. Add Menu to the Application ===
 
=== 4. Add Menu to the Application ===
4.1. Open <code>ViewStudentApp</code> class and add menu items inside the constructor.<br/>
+
==== 4.a. Add Menu ====
4.2 Add add, edit and delete student options to the menu and set commands to execute:
+
4.a.1. Open <code>ViewStudentApp</code> class and add menu items inside the constructor.<br/>
  <pre>
+
4.a.2 Add add, edit and delete student options to the menu and set commands to execute:
 +
  <source lang="java">
 
       MyScreen _mainScreen = new MyScreen(this);       
 
       MyScreen _mainScreen = new MyScreen(this);       
 
        
 
        
Line 18: Line 19:
 
        
 
        
 
         pushScreen(_mainScreen);
 
         pushScreen(_mainScreen);
  </pre>
+
  </source>
4.3. Create inner classes to execute all commands:
+
4.a.3. Create inner classes to execute all commands:
  <pre>
+
  <source lang="java">
 
    
 
    
 
     class AddCommandHandler extends CommandHandler
 
     class AddCommandHandler extends CommandHandler
Line 40: Line 41:
 
         }
 
         }
 
     }
 
     }
  </pre>
+
  </source>
4.4. Run the application. Click on the menu you should see the following:<br/>
+
4.a.4. Run the application. Click on the menu you should see the following:<br/>
 
[[Image: BB_Menu.png | 300px]]<br/>
 
[[Image: BB_Menu.png | 300px]]<br/>
4.5. Select one of the menu options. The pop-up message should appear:<br/>
+
4.a.5. Select one of the menu options. The pop-up message should appear:<br/>
[[Image: BB_Menu2.png | 300px]]
+
[[Image: BB_Menu2.png | 300px]]<br/>
 +
 
 +
 
 +
==== 4.b. Add Image to the Menu ====
 +
4.b.1. Add static fields for menu item number:<br/>
 +
<source lang="java">
 +
final static int MENU_ADD = 0;
 +
 
 +
final static int MENU_EDIT = 1;
 +
final static int MENU_DELETE = 2;
 +
final static int MENU_EMAIL = 3;
 +
final static int MENU_MAP = 4;
 +
</source>
 +
 
 +
4.b.2. Create an abstract class that extends MenuItem to be used for creating items with image:<br/>
 +
<source lang="java">
 +
/**
 +
* Concrete implementation of abstract class MenuItem
 +
*/
 +
class ImageMenuItem extends MenuItem
 +
{
 +
/**
 +
* Creates a new MenuDemoMenuItem object
 +
*/
 +
ImageMenuItem(String title, int ordinal, int property, int itemNum)
 +
{
 +
super(new StringProvider(title), ordinal, property);
 +
 
 +
// Create Image object from project resource
 +
Bitmap bitmap;
 +
switch(itemNum)
 +
{
 +
case 0:
 +
bitmap = Bitmap.getBitmapResource("add.png");
 +
break;
 +
case 1:
 +
bitmap = Bitmap.getBitmapResource("edit.png");
 +
break;
 +
case 2:
 +
bitmap = Bitmap.getBitmapResource("delete.png");
 +
break;
 +
case 3:
 +
bitmap = Bitmap.getBitmapResource("email.png");
 +
break;
 +
case 4:
 +
bitmap = Bitmap.getBitmapResource("map.png");
 +
break;
 +
default:
 +
bitmap = Bitmap.getBitmapResource("none.png");
 +
break;
 +
}
 +
Image image = ImageFactory.createImage(bitmap);
 +
 
 +
// Set image as this menu item's icon
 +
setIcon(image);
 +
}
 +
 +
</source>
 +
 
 +
4.b.3. Change the instantiation of menu items to use the new class created ''ImageMenuItem''. Pass the name of the menu item to the constructor:<br/>
 +
<source lang="java">
 +
        // MenuItem addItem = new MenuItem(new StringProvider("Add Student"), 200, 2);  // without image
 +
ImageMenuItem addItem = new ImageMenuItem("Add Student", 100, 1, MENU_ADD);
 +
</source>
 +
[[Image: BB_MenuImage.png | 300px]]<br/>

Latest revision as of 13:24, 9 April 2011

4. Add Menu to the Application

4.a. Add Menu

4.a.1. Open ViewStudentApp class and add menu items inside the constructor.
4.a.2 Add add, edit and delete student options to the menu and set commands to execute:

      MyScreen _mainScreen = new MyScreen(this);       
       
        MenuItem addItem = new MenuItem(new StringProvider("Add Student"), 200, 2);
        adds.setCommand(new Command(new AddCommandHandler()));
        _mainScreen.addMenuItem(addItem);
       
        MenuItem editItem = new MenuItem(new StringProvider("Edit Student"), 300, 3);
        edit.setCommand(new Command(new EditCommandHandler()));
        _mainScreen.addMenuItem(editItem);
       
        MenuItem delete = new MenuItem(new StringProvider("Delete Student"), 400, 4);
        delete.setCommand(new Command(new DeleteCommandHandler()));
        _mainScreen.addMenuItem(deleteItem);
       
        pushScreen(_mainScreen);

4.a.3. Create inner classes to execute all commands:

  
    class AddCommandHandler extends CommandHandler
    {
        public void execute(ReadOnlyCommandMetadata metadata, Object context){
            Dialog.alert("Add was selected");
        }
    }
    class EditCommandHandler extends CommandHandler
    {
        public void execute(ReadOnlyCommandMetadata metadata, Object context){
            Dialog.alert("Edit was selected");
        }
    }
    class DeleteCommandHandler extends CommandHandler
    {
        public void execute(ReadOnlyCommandMetadata metadata, Object context){
            Dialog.alert("Delete was selected");
        }
    }

4.a.4. Run the application. Click on the menu you should see the following:
BB Menu.png
4.a.5. Select one of the menu options. The pop-up message should appear:
BB Menu2.png


4.b. Add Image to the Menu

4.b.1. Add static fields for menu item number:

final static int MENU_ADD = 0;

	final static int MENU_EDIT = 1;
	final static int MENU_DELETE = 2;
	final static int MENU_EMAIL = 3;
	final static int MENU_MAP = 4;

4.b.2. Create an abstract class that extends MenuItem to be used for creating items with image:

	/**
	 * Concrete implementation of abstract class MenuItem
	 */
	class ImageMenuItem extends MenuItem 
	{
		/**
		 * Creates a new MenuDemoMenuItem object
		 */
		ImageMenuItem(String title, int ordinal, int property, int itemNum) 
		{
			super(new StringProvider(title), ordinal, property);

			// Create Image object from project resource
			Bitmap bitmap;
			switch(itemNum) 
			{
				case 0:
					bitmap = Bitmap.getBitmapResource("add.png");
					break;
				case 1:
					bitmap = Bitmap.getBitmapResource("edit.png");
					break;
				case 2:
					bitmap = Bitmap.getBitmapResource("delete.png");
					break;
				case 3:
					bitmap = Bitmap.getBitmapResource("email.png");
					break;	
				case 4: 
					bitmap = Bitmap.getBitmapResource("map.png");
					break;
				default:
					bitmap = Bitmap.getBitmapResource("none.png");
					break;
			}
			Image image = ImageFactory.createImage(bitmap);

			// Set image as this menu item's icon
			setIcon(image);
		}
	}

4.b.3. Change the instantiation of menu items to use the new class created ImageMenuItem. Pass the name of the menu item to the constructor:

        // MenuItem addItem = new MenuItem(new StringProvider("Add Student"), 200, 2);  // without image
	ImageMenuItem addItem = new ImageMenuItem("Add Student", 100, 1, MENU_ADD);

BB MenuImage.png