PostsView.java

/*
 * Copyright © 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */ 

package com.nokia.example.wordpress.views;

import com.nokia.example.wordpress.components.DataModel;
import com.nokia.example.wordpress.components.List;
import com.nokia.example.wordpress.components.MenuItemCallback;
import com.nokia.example.wordpress.components.OptionsMenu;
import com.nokia.example.wordpress.components.PostDrawer;
import com.nokia.example.wordpress.helpers.ImageLoader;
import com.nokia.example.wordpress.helpers.KeyCodes;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * View to display blog posts in a list.
 */
public class PostsView extends BaseView {

    private List postsList;
    private Image postIcon;

    PostsView(Graphics g, int x, int y, int width, int height) {
        super(g, ViewMaster.VIEW_POSTS, x, y, width, height);
        haveTabs = true;

        try {
            postIcon = ImageLoader.getInstance().loadImage("/post-icon.png");
        } catch (IOException e) {
        }

        final int listHeight = height - tabsHeight() - Visual.SOFTKEYBAR_HEIGHT;
        final int listY = tabsHeight();
        postsList = new List(viewMaster, 0, listY, width, listHeight, new PostDrawer(viewMaster, postIcon));

        // Create options menu
        menu = new OptionsMenu(width, height) {

            {
                addItem("New post", new MenuItemCallback() {

                    public void select() {
                        viewMaster.setView(ViewMaster.VIEW_NEWPOST);
                    }
                });
                addExitItem();
            }
        };

        softkey1Label = "Options";
        softkey2Label = "Open";
        softkey3Label = "Exit";
    }

    public void activate() {
        System.out.println("posts activate");
        super.activate();
        postsList.setData(data.getPosts());
    }

    public void draw(int x, int y, int width, int height) {
        System.out.println("posts render");
        if (menu.isVisible()) {
            menu.draw(g);
            return;
        }
        drawTabs();
        postsList.draw(g);
        drawSoftkeys();
    }

    public void keyPressed(int keyCode) {

        if (menu != null && menu.notifyKeyEvents(keyCode)) {
            // menu used this keycode, let's not use for anything else.
            viewMaster.draw();
            return;
        }

        // update focus in the model
        data.setCurrentPostIndex(postsList.focusIndex());

        switch (keyCode) {
            case KeyCodes.MIDDLE_SOFTKEY:
                viewMaster.setView(ViewMaster.VIEW_SINGLEPOST);
                return;
            case KeyCodes.RIGHT_SOFTKEY:
                midlet.commandExit();
                break;
            case KeyCodes.DOWN:
                postsList.focusDown();
                break;
            case KeyCodes.UP:
                postsList.focusUp();
                break;
            case KeyCodes.RIGHT:
                viewMaster.setView(ViewMaster.VIEW_COMMENTS);
                return;
            default:
                break;

        }
        viewMaster.draw();
    }
}