SinglePostView.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.List;
import com.nokia.example.wordpress.components.MenuItemCallback;
import com.nokia.example.wordpress.components.OptionsMenu;
import com.nokia.example.wordpress.helpers.KeyCodes;
import com.nokia.example.wordpress.helpers.TextWrapper;
import com.nokia.example.wordpress.networking.Post;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * View to display a single blog post.
 */
public class SinglePostView extends BaseView {

    private List postsList;
    private Image postIcon;

    SinglePostView(Graphics g, int x, int y, int width, int height) {
        super(g, ViewMaster.VIEW_SINGLEPOST, x, y, width, height);
        haveTabs = true;
    }

    public void activate() {
        System.out.println("posts activate");
        super.activate();

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

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

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

                // This menu item is displayed only if there
                // are some comments for the current post to be displayed.
                if (data.getCommentsForCurrentPost().size() > 0) {
                    addItem("Comments for this post", new MenuItemCallback() {

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

        softkey1Label = "Options";
        softkey2Label = "";
        softkey3Label = "Back";

        if (data.getCommentsForCurrentPost().size() > 0) {
            // Special condition as in the menu.
            softkey2Label = "Comments";
        }


    }

    public void draw(int x, int y, int width, int height) {
        System.out.println("singlepost render");
        if (menu.isVisible()) {
            menu.draw(g);
            return;
        }
        drawTabs();
        drawPost(tabsHeight());
        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;
        }

        switch (keyCode) {
            case KeyCodes.MIDDLE_SOFTKEY:
                if (data.getCommentsForCurrentPost().size() > 0) {
                    viewMaster.setView(ViewMaster.VIEW_COMMENTSFORPOST);
                    return;
                }
                break;
            case KeyCodes.RIGHT_SOFTKEY:
                viewMaster.setView(ViewMaster.VIEW_POSTS);
                return;
            case KeyCodes.RIGHT:
                viewMaster.setView(ViewMaster.VIEW_COMMENTS);
                return;
            default:
                break;

        }
        viewMaster.draw();
    }

    private void drawPost(int y) {
        viewMaster.drawBackground(g, 0, y, width, height - Visual.SOFTKEYBAR_HEIGHT, false);
        int x = 0;

        Post post = data.getCurrentPost();
        Vector lines;

        // Date
        g.setFont(Visual.SMALL_FONT);
        g.setColor(Visual.LIST_SECONDARY_COLOR);
        g.drawString(post.date, x, y, g.TOP | g.LEFT);
        y += Visual.SMALL_FONT.getHeight();

        // Title
        lines = TextWrapper.wrapTextToWidth(post.title, width, Visual.SMALL_FONT);
        g.setFont(Visual.SMALL_BOLD_FONT);
        g.drawString(post.title, x, y, g.TOP | g.LEFT);
        for (int i = 0; i < lines.size(); i++) {
            g.drawString((String) lines.elementAt(i), x, y, g.TOP | g.LEFT);
            y += Visual.SMALL_FONT.getHeight();
        }

        // Content
        g.setColor(Visual.LIST_PRIMARY_COLOR);
        g.setFont(Visual.SMALL_FONT);
        lines = TextWrapper.wrapTextToWidth(post.description, width, Visual.SMALL_FONT);
        for (int i = 0; i < lines.size(); i++) {
            g.drawString((String) lines.elementAt(i), x, y, g.TOP | g.LEFT);
            y += Visual.SMALL_FONT.getHeight();
        }

    }
}