FileItem.java

/**
 * Copyright (c) 2013 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.fileselectexample;

import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

import com.nokia.mid.ui.FileSelectDetail;

/**
 * An item for displaying details of a file.
 */
public class FileItem extends CustomItem {
    // Constants
    private static final int ITEM_WIDTH = 240;
    private static final int MARGIN = 5;
    private static final int TEXT_COLOR_NOT_SELECTED = 0x595959;
    private static final int TEXT_COLOR_SELECTED = 0xfdfefe;
    private static final int HILIGHT_COLOR = 0x29a7cc;
    private static final int BACKGROUND_COLOR = 0xf4f4f4;

    // Members
    private FileSelectDetail fileDetails = null;
    private Listener listener = null;
    private int fontHeight = 0;
    private int height = 0;
    private boolean isSelected = false;

    /**
     * Constructor.
     * @param fileDetails The FileSelectDetail instance containing file details
     * to show.
     * @param listener A listener for this item. Can be null.
     * @throws IllegalArgumentException Thrown if the given FileSelectDetail
     * instance is null.
     */
    public FileItem(FileSelectDetail fileDetails, Listener listener)
        throws IllegalArgumentException
    {
        super(null);
        
        if (fileDetails == null) {
            throw new IllegalArgumentException(
                "The given FileSelectDetail object cannot be null!");
        }
        
        this.fileDetails = fileDetails;
        this.listener = listener;
        
        setLabel(fileDetails.displayName);
        
        fontHeight = Font.getDefaultFont().getHeight();
        height = fontHeight * 3 + MARGIN * 2; // Name, MIME type, size
        System.out.println("FileItem::FileItem(): height == " + height);
    }

    /** 
     * @return The FileSelectDetails instance corresponding to this item.
     */
    public FileSelectDetail getFileDetails() {
        return fileDetails;
    }

    /**
     * Selects/deselects the item.
     * @param isSelected Whether this item is selected or not.
     */
    public void setIsSelected(boolean isSelected) {
        if (this.isSelected != isSelected) {
            this.isSelected = isSelected;
            repaint();
        }
    }

    /**
     * @see javax.microedition.lcdui.CustomItem#getMinContentHeight()
     */
    protected int getMinContentHeight() {
        return height;
    }

    /**
     * @see javax.microedition.lcdui.CustomItem#getMinContentWidth()
     */
    protected int getMinContentWidth() {
        return ITEM_WIDTH;
    }

    /**
     * @see javax.microedition.lcdui.CustomItem#getPrefContentHeight(int)
     */
    protected int getPrefContentHeight(int arg0) {
        return height;
    }

    /**
     * @see javax.microedition.lcdui.CustomItem#getPrefContentWidth(int)
     */
    protected int getPrefContentWidth(int arg0) {
        return ITEM_WIDTH;
    }

    /**
     * @see javax.microedition.lcdui.CustomItem#pointerPressed(int, int)
     */
    protected void pointerPressed(int x, int y) {
        if (listener != null) {
            listener.onPressed(this);
        }
    }

    /**
     * @see javax.microedition.lcdui.CustomItem#pointerReleased(int, int)
     */
    protected void pointerReleased(int x, int y) {
        if (listener != null) {
            listener.onReleased(this);
        }
    }

    /**
     * @see javax.microedition.lcdui.CustomItem#paint(Graphics, int, int)
     */
    protected void paint(Graphics graphics, int width, int height) {
        if (isSelected) {
            graphics.setColor(HILIGHT_COLOR);
        }
        else {
            graphics.setColor(BACKGROUND_COLOR);
        }
        
        graphics.fillRect(0, 0, ITEM_WIDTH, height);
        
        if (isSelected) {
            graphics.setColor(TEXT_COLOR_SELECTED);
        }
        else {
            graphics.setColor(TEXT_COLOR_NOT_SELECTED);
        }
        
        final int anchor = Graphics.TOP | Graphics.LEFT;
        graphics.drawString("Name: " + fileDetails.displayName, MARGIN, MARGIN, anchor);
        graphics.drawString("MIME Type: " + fileDetails.mimeType, MARGIN, MARGIN + fontHeight, anchor);
        graphics.drawString("Size: " + fileDetails.size, MARGIN, MARGIN + fontHeight * 2, anchor);
    }

    /**
     * An interface for listeners who get notified when this item receives
     * touch input.
     */
    public interface Listener {
        void onPressed(FileItem fileItem);
        void onReleased(FileItem fileItem);
    }
}