Click here to Skip to main content
15,895,142 members
Articles / Mobile Apps / Blackberry

End-to-End Real World BlackBerry Application, Part 2

Rate me:
Please Sign up or sign in to vote.
3.67/5 (3 votes)
15 Jul 2008CPOL2 min read 31.8K   186   19  
End-to-end real world BlackBerry application walkthrough, Part 2.
package KnowledgeBase;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.system.Bitmap;

class HomeScreen extends MainScreen  {
    
    private String mnuSearchTitles;
    private String mnuBrowseTags;
    private String mnuViewRecent;
    private String[] mainMenuItems;
    private MyObjectListField menu;
    private SearchScreen searchScreen;
    private TagsScreen tagsScreen;
    private ArticlesScreen articlesScreen;
    private OptionsScreen optionsScreen;
    
    private MenuItem optionsMenu = new MenuItem("Options",100,10) {
       public void run() {
           showOptionsScreen();
       } 
    };
    
    HomeScreen() {
        
        this.setTitle("Knowledge Base");
        
        menu = new MyObjectListField();
        mnuSearchTitles = "Search by Title";
        mnuBrowseTags = "Browse Tags";
        mnuViewRecent = "View Recent";
        mainMenuItems = new String[]{mnuSearchTitles,mnuBrowseTags,mnuViewRecent};
        menu.set(mainMenuItems);
        this.add(menu);
    }
    
    private void showOptionsScreen() {
        if (null == optionsScreen) {
            optionsScreen = new OptionsScreen();
        }
        UiApplication.getUiApplication().pushScreen(optionsScreen);
    }
    
    protected void makeMenu(Menu menu, int instance) {
        menu.add(optionsMenu);
        menu.add(MenuItem.separator(optionsMenu.getOrdinal() + 1));
        super.makeMenu(menu,instance);
    }
    
    protected boolean navigationClick(int status, int time) {
        
        // Determine which menu item was clicked.
        int selected = menu.getSelectedIndex();
        switch (selected) {
            case 0:
                if (null == articlesScreen) {
                 articlesScreen = new ArticlesScreen();   
                }
                if (null == searchScreen) {
                 searchScreen = new SearchScreen(articlesScreen);   
                }
                UiApplication.getUiApplication().pushScreen(searchScreen);
            break;
            case 1:
                if (null == articlesScreen) {
                 articlesScreen = new ArticlesScreen();   
                }
                if (null == tagsScreen) {
                 tagsScreen = new TagsScreen(articlesScreen);   
                }
                UiApplication.getUiApplication().pushScreen(tagsScreen);
            break;
            case 2:
                if (null == articlesScreen) {
                 articlesScreen = new ArticlesScreen();   
                }
                UiApplication.getUiApplication().pushScreen(articlesScreen);
            break;
        }
        
        return true;
    }
    
    private class MyObjectListField extends ObjectListField {
        
        private Bitmap icon = Bitmap.getBitmapResource("img/star_green.png");  
        
        // We are going to take care of drawing the item.
        public void drawListRow(ListField listField, Graphics graphics, 
                int index, int y, int width) {
            if (null != icon) {
                int offsetY = (this.getRowHeight() - icon.getHeight())/2;
                graphics.drawBitmap(1,y + offsetY, icon.getWidth(),
                        icon.getHeight(),icon,0,0);
                graphics.drawText(mainMenuItems[index], 
                    icon.getWidth() + 2, y, DrawStyle.ELLIPSIS, width - icon.getWidth() + 2);
            } else {
                graphics.drawText("- " + mainMenuItems[index], 0,
                    y, DrawStyle.ELLIPSIS, width - graphics.getFont().getAdvance("- "));
            }
        }
    };
    
} 

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
United States United States
I run a small development team for an international Law Firm.

Comments and Discussions