To implement the FileItem
class:
Create the FileItem.java class file.
Import the required packages and classes.
import javax.microedition.lcdui.CustomItem; import javax.microedition.lcdui.Font; import javax.microedition.lcdui.Graphics; import com.nokia.mid.ui.FileSelectDetail;
Set FileItem
class to extend CustomItem
.
public class FileItem extends CustomItem {
4. Create the required variables and 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; private FileSelectDetail fileDetails = null; private Listener listener = null; private int fontHeight = 0; private int height = 0; private boolean isSelected = false;
Create the constructor and corresponding listener.
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); }
Create the getFileDetails
method to retrieve fileDetails
.
public FileSelectDetail getFileDetails() { return fileDetails; }
Create the setIsSelected
method to select/deselect
the item.
public void setIsSelected(boolean isSelected) { if (this.isSelected != isSelected) { this.isSelected = isSelected; repaint(); } }
Implement the methods from CustomItem
interface.
protected int getMinContentHeight() { return height; } protected int getMinContentWidth() { return ITEM_WIDTH; } protected int getPrefContentHeight(int arg0) { return height; } protected int getPrefContentWidth(int arg0) { return ITEM_WIDTH; } protected void pointerPressed(int x, int y) { if (listener != null) { listener.onPressed(this); } } protected void pointerReleased(int x, int y) { if (listener != null) { listener.onReleased(this); } } 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); }
Create the Listener
interface to get notified
when an item receives touch input.
public interface Listener { void onPressed(FileItem fileItem); void onReleased(FileItem fileItem); }