SingleCommentView.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.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.Comment;
import com.nokia.example.wordpress.networking.DeleteCommentOperation;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * View to display a single post comment.
 * Used when the user selects a comment from the comment view or from
 * the comments-for-post view.
 */
public class SingleCommentView extends BaseView {

    SingleCommentView(Graphics g, int x, int y, int width, int height) {
        super(g, ViewMaster.VIEW_SINGLECOMMENT, x, y, width, height);
        haveTabs = true;
        softkey1Label = "Options";
        softkey2Label = "";
        softkey3Label = "Back";


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

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

                    public void select() {
                        deleteComment();
                    }
                });
                addExitItem();
            }
        };

    }

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

    public void draw(int x, int y, int width, int height) {
        System.out.println("singlepost render");
        if (menu.isVisible()) {
            menu.draw(g);
            return;
        }
        drawTabs();
        drawComment(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:
                break;
            case KeyCodes.RIGHT_SOFTKEY:
                viewMaster.setPreviousView();
                return;
            case KeyCodes.LEFT:
                viewMaster.setView(ViewMaster.VIEW_POSTS);
                return;
            default:
                break;

        }
        viewMaster.draw();
    }

    private void drawComment(int yOrigin) {
        viewMaster.drawBackground(g, 0, yOrigin, width, height - Visual.SOFTKEYBAR_HEIGHT, false);

        Comment comment;
        // Depending on the previous state, select which array of comments to use.
        if (viewMaster.getPreviousViewId() == ViewMaster.VIEW_COMMENTSFORPOST) {
            comment = data.getCurrentCommentForPost();
            System.out.println("comment from current post "+comment.content);
        } else {
            comment = data.getCurrentComment();
            System.out.println("comment from comments "+comment.content);
        }
        Vector lines = TextWrapper.wrapTextToWidth(comment.content, width, Visual.SMALL_FONT);
        int x = 0;
        int y = yOrigin;

        // Comment owner gravatar missing.
        Image image = null;
        if (image != null) {
            g.drawImage(image, 0, yOrigin, g.TOP | g.LEFT);
            x += image.getWidth() + 4;
        }

        g.setFont(Visual.SMALL_BOLD_FONT);
        g.setColor(Visual.LIST_SECONDARY_COLOR);
        g.drawString(comment.author, x, y, g.TOP | g.LEFT);
        g.setFont(Visual.SMALL_FONT);
        y += Visual.SMALL_FONT.getHeight();
        g.drawString(comment.author_email, x, y, g.TOP | g.LEFT);
        y += Visual.SMALL_FONT.getHeight();
        g.drawString(comment.date, x, y, g.TOP | g.LEFT);
        y += Visual.SMALL_FONT.getHeight();

        x = 0;
        g.setColor(Visual.LIST_PRIMARY_COLOR);

        if (image != null) {
            y = yOrigin + image.getHeight() + 2;
        }

        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();
        }

    }

    /**
     * Deletes the comment and returns to previous view.
     */
    private void deleteComment() {
        Comment comment;
        // Find out where we came to this view to decide which comment
        // to delete.
        if (viewMaster.getPreviousViewId() == ViewMaster.VIEW_COMMENTSFORPOST) {
            comment = data.getCurrentCommentForPost();
        } else {
            comment = data.getCurrentComment();
        }
        data.getComments().removeElement(comment);

        // Fire up the actual deletion on the server side, no listener.
        DeleteCommentOperation op = new DeleteCommentOperation(null, comment);
        op.start();

        // Return to previous view.
        viewMaster.setPreviousView();
    }
}