CommentsView.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.ImageLoader;
import com.nokia.example.wordpress.helpers.KeyCodes;
import com.nokia.example.wordpress.networking.Comment;
import com.nokia.example.wordpress.networking.DeleteCommentOperation;
import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class CommentsView extends BaseView {
private List commentsList;
private Image postIcon;
CommentsView(Graphics g, int x, int y, int width, int height) {
super(g, ViewMaster.VIEW_COMMENTS, 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();
commentsList = new List(viewMaster, 0, listY, width, listHeight,
new CommentDrawer(viewMaster, viewMaster.gravatarCache, viewMaster.defaultGravatarIcon));
// Create options menu
menu = new OptionsMenu(width, height) {
{
addItem("Delete comment", new MenuItemCallback() {
public void select() {
deleteComment();
}
});
addExitItem();
}
};
softkey1Label = "Options";
softkey2Label = "Open";
softkey3Label = "Exit";
}
public void activate() {
System.out.println("comments activate");
super.activate();
commentsList.setData(data.getComments());
}
public void draw(int x, int y, int width, int height) {
System.out.println("comments 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;
}
// update focus in the model
data.setCurrentCommentIndex(commentsList.focusIndex());
switch (keyCode) {
case KeyCodes.MIDDLE_SOFTKEY:
viewMaster.setView(ViewMaster.VIEW_SINGLECOMMENT);
return;
case KeyCodes.RIGHT_SOFTKEY:
midlet.commandExit();
break;
case KeyCodes.DOWN:
commentsList.focusDown();
break;
case KeyCodes.UP:
commentsList.focusUp();
break;
case KeyCodes.LEFT:
viewMaster.setView(ViewMaster.VIEW_POSTS);
return;
default:
break;
}
viewMaster.draw();
}
/**
* Remove comment from the data and refresh view.
*/
private void deleteComment() {
Comment comment = data.getCurrentComment();
data.getComments().removeElement(comment);
commentsList.setData(data.getComments());
viewMaster.draw();
// Fire up the actual deletion on the server side, no listener.
DeleteCommentOperation op = new DeleteCommentOperation(null, comment);
op.start();
}
}