NewCommentView.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.OptionsMenu;
import com.nokia.example.wordpress.helpers.KeyCodes;
import com.nokia.example.wordpress.networking.GetCommentsOperation;
import com.nokia.example.wordpress.networking.NewCommentOperation;
import com.nokia.example.wordpress.networking.Post;
import com.nokia.mid.ui.TextEditor;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.TextField;

/**
 * View for writing a new post comment.
 */
public class NewCommentView extends BaseView {

    private TextEditor contentEditor;

    NewCommentView(Graphics g, int x, int y, int width, int height) {
        super(g, ViewMaster.VIEW_NEWCOMMENT, x, y, width, height);
        haveTabs = false;

        int border = 2;
        int editorWidth = width - 2 * border;

        contentEditor = TextEditor.createTextEditor(1000, TextField.ANY, editorWidth, 8);
        contentEditor.setFont(Visual.SMALL_FONT);
        contentEditor.setBackgroundColor(Visual.EDITOR_ACTIVE_BACKGROUND_COLOR); // white
        contentEditor.setForegroundColor(Visual.EDITOR_ACTIVE_FOREGROUND_COLOR); // black
        contentEditor.setContent("");
        contentEditor.setParent(viewMaster);
        contentEditor.setPosition(border, tabsHeight() + Visual.SMALL_FONT.getHeight());
        contentEditor.setVisible(false);
        
        // Create options menu
        menu = new OptionsMenu(width, height) {

            {
                addExitItem();
            }
        };

        softkey1Label = "Options";
        softkey2Label = "Ok";
        softkey3Label = "Cancel";
    }

    public void activate() {
        System.out.println("newcomment activate");
        super.activate();
        contentEditor.setContent("");
    }

    public void transitionFinished()
    {
        contentEditor.setVisible(true);
        contentEditor.setFocus(true);
    }

    public void deactivate() {
        System.out.println("newcomment deactivate");
        contentEditor.setVisible(false);
    }

    public void draw(int x, int y, int width, int height) {
        System.out.println("newcomment render");
        if (menu.isVisible()) {
            menu.draw(g);
            return;
        }
        //drawTabs();
        viewMaster.drawBackground(g, x, y, width, height, false);

        g.setColor(Visual.LIST_PRIMARY_COLOR);
        g.setFont(Visual.SMALL_FONT);
        g.drawString("Comment:", contentEditor.getPositionX() + contentEditor.getWidth() / 2,
                contentEditor.getPositionY() - Visual.SMALL_FONT.getHeight(), g.TOP | g.HCENTER);
        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:
                // Ok, send commentt
                sendComment();
                break;
            case KeyCodes.RIGHT_SOFTKEY:
                // Cancel
                viewMaster.setPreviousView();
                return;
            default:
                break;

        }
    }

    /**
     * Sends the comment to the server.
     */
    private void sendComment() {
        contentEditor.setFocus(false);
        viewMaster.drawDimmedBackground();
        busy = true; // this will trigger busy animation

        final NewCommentOperation.Listener commentListener = new NewCommentOperation.Listener() {

            // After comment sent, get the posts from the server.
            // This could be done more efficiently by adding the new comment manually
            // to the comments vector and reloading in background.
            public void newCommentSent(Post post, String commentId) {
                getComments();
            }
        };

        NewCommentOperation op = new NewCommentOperation(commentListener, data.getCurrentPost(), contentEditor.getContent());
        op.start();
    }

    /**
     * Loads comments from the server and activates the comments for post view.
     */
    private void getComments() {
        final GetCommentsOperation.Listener commentsListener = new GetCommentsOperation.Listener() {

            // After receiving comments, store them and enter the comments view.
            public void commentsReceived(Vector comments) {
                System.out.println("commentsReceived " + comments.size());
                data.setComments(comments);
                viewMaster.setView(ViewMaster.VIEW_COMMENTSFORPOST);
            }
        };

        GetCommentsOperation op = new GetCommentsOperation(commentsListener, data.getBlog(), data.getPosts());
        op.start();
    }

}