CommentsForPostView.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.CommentDrawer;
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.networking.Comment;
import com.nokia.example.wordpress.networking.DeleteCommentOperation;
import javax.microedition.lcdui.Graphics;
/**
* View to display comments for a specific post.
*/
public class CommentsForPostView extends BaseView {
private List commentsList;
CommentsForPostView(Graphics g, int x, int y, int width, int height) {
super(g, ViewMaster.VIEW_COMMENTSFORPOST, x, y, width, height);
haveTabs = true;
final int listHeight = height - tabHeader.getHeight() - Visual.SOFTKEYBAR_HEIGHT;
final int listY = tabHeader.getHeight();
commentsList = new List(viewMaster, 0, listY, width, listHeight,
new CommentDrawer(viewMaster, viewMaster.gravatarCache, viewMaster.defaultGravatarIcon));
// Create options menu
menu = new OptionsMenu(width, height) {
{
addItem("New comment", new MenuItemCallback() {
public void select() {
viewMaster.setView(ViewMaster.VIEW_NEWCOMMENT);
}
});
addItem("Delete comment", new MenuItemCallback() {
public void select() {
deleteComment();
}
});
addExitItem();
}
};
softkey1Label = "Options";
softkey2Label = "Open";
softkey3Label = "Back";
}
public void activate() {
System.out.println("commentsforpost activate");
super.activate();
commentsList.setData(data.getCommentsForCurrentPost());
if(data.getCommentsForCurrentPost().size() == 0)
{
// Nothing to display. Go back to single post view.
viewMaster.setView(ViewMaster.VIEW_SINGLEPOST);
}
}
public void draw(int x, int y, int width, int height) {
System.out.println("commentsforpost render");
if (menu.isVisible()) {
menu.draw(g);
return;
}
drawTabs();
commentsList.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;
}
data.setCommentForCurrentPostIndex(commentsList.focusIndex());
System.out.println("comment for current post idx: " + commentsList.focusIndex());
System.out.println("comment for current post: " + data.getCurrentCommentForPost().content);
switch (keyCode) {
case KeyCodes.MIDDLE_SOFTKEY:
viewMaster.setView(ViewMaster.VIEW_SINGLECOMMENT);
return;
case KeyCodes.RIGHT_SOFTKEY:
viewMaster.setPreviousView();
return;
case KeyCodes.LEFT:
viewMaster.setView(ViewMaster.VIEW_POSTS);
return;
case KeyCodes.DOWN:
commentsList.focusDown();
break;
case KeyCodes.UP:
commentsList.focusUp();
break;
default:
break;
}
viewMaster.draw();
}
/**
* Delete the comment and refresh view.
*/
private void deleteComment() {
Comment comment = (Comment) data.getCommentsForCurrentPost().elementAt(commentsList.focusIndex());
data.getComments().removeElement(comment);
commentsList.setData(data.getCommentsForCurrentPost());
viewMaster.draw();
// Fire up the actual deletion on the server side, no listener.
DeleteCommentOperation op = new DeleteCommentOperation(null, comment);
op.start();
if(data.getCommentsForCurrentPost().size() == 0)
{
// Nothing to display. Go back to single post view.
viewMaster.setView(ViewMaster.VIEW_SINGLEPOST);
}
}
}