Thursday, May 14, 2009

Custom List component for J2ME

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ali.ui;

import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Graphics;

/**
*
* @author ali hassan
*/
public class List extends CustomItem {

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
*/
public static final int DEFAULT_SELECTED_BGCOLOR = 0xFFFFFF;
/**
*
*/
public static final int DEFAULT_SELECTED_FGCOLOR = 0x0;
/**
*
*/
public static final int DEFAULT_FGCOLOR = 0xFFFFFF;
public static final int DEFAULT_BGCOLOR = 0x0;
private int x = 0;
private int y = 0;
private int width = 0;
private int height = 0;
private Vector items = new Vector();
private Vector itemFonts = new Vector();
private int selected = 0;
private int bgColor = DEFAULT_BGCOLOR;
private int fgColor = DEFAULT_FGCOLOR;
private int selectedBGColor = DEFAULT_SELECTED_BGCOLOR;
private int selectedFGColor = DEFAULT_SELECTED_FGCOLOR;
private int startPageItem = 0;
private int endPageItem = 0;
private boolean paintDown = true;

public List(String label) {
super(label);
}

public List(String label, int x, int y, int width, int height) {
this(label);
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

protected boolean traverse(int dir, int viewportWidth, int viewportHeight,
int[] visRect_inout) {
switch (dir) {
case Canvas.DOWN:
this.next();
// this.getSelectedString();
notifyStateChanged();
repaint();
break;

case Canvas.UP:
this.prev();
// this.getSelectedString();

notifyStateChanged();
repaint();
break;
}
System.out.println(this.getSelected());
return true;
}



public List(String label, int x, int y, int width, int height, String[] items,
Font[] fonts) {

this(label, x, y, width, height);
if (items == null) {
throw new NullPointerException("items");
}
if (fonts == null) {
throw new NullPointerException("itemFonts");
}
int length = items.length;
int fontsLength = fonts.length;
if (fontsLength > length) {
throw new IllegalArgumentException(
"size of fonts > size of items: " + fontsLength + " > " + length);
}
for (int i = 0; i < length; i++) {
if (i < fontsLength) {
this.append(items[i], fonts[i]);
} else {
this.append(items[i]);
}
}
}

protected int getMinContentWidth() {
return this.width;
}

protected int getMinContentHeight() {
return this.height;
}

protected int getPrefContentWidth(int height) {
return getMinContentWidth();
}

protected int getPrefContentHeight(int width) {
return getMinContentHeight();
}

protected void paint(Graphics g, int w, int h) {

int clipX = g.getClipX();
int clipY = g.getClipY();
int clipW = g.getClipWidth();
int clipH = g.getClipHeight();
g.setClip(this.x, this.y, this.width, this.height);
int itemHeight;
g.setColor(this.bgColor);
g.fillRect(this.x, this.y, this.width, this.height);
g.setColor(this.fgColor);
if (this.paintDown) {
int iLenght = this.items.size();
int curY = this.y;
for (int i = this.startPageItem; (i < iLenght) && (curY - this.y + (itemHeight = ((Font) this.itemFonts.elementAt(i)).getHeight()) <= this.height); i++) {
String item = (String) this.items.elementAt(i);
Font font = (Font) this.itemFonts.elementAt(i);
g.setFont(font);
int itemWidth = font.stringWidth(item);
if (i == this.selected) {
g.setColor(this.selectedBGColor);
g.fillRect(this.x, curY, itemWidth + 4, itemHeight);
g.setColor(this.selectedFGColor);
g.drawString(item, this.x + 2, curY, Graphics.TOP | Graphics.LEFT);
g.setColor(this.fgColor);
} else {
g.drawString((String) this.items.elementAt(i), this.x + 2, curY,
Graphics.TOP | Graphics.LEFT);
}
curY += itemHeight;
this.endPageItem = i;
}
if (this.endPageItem < this.items.size() - 1) {
g.drawString(
(String) this.items.elementAt(this.endPageItem + 1), this.x + 2,
curY, Graphics.TOP | Graphics.LEFT);
}

} else {
int curY = this.y + this.height;
for (int i = this.endPageItem; i >= 0 && curY - (itemHeight = ((Font) this.itemFonts.elementAt(i)).getHeight()) >= this.y; i--) {
String item = (String) this.items.elementAt(i);
Font font = (Font) this.itemFonts.elementAt(i);
g.setFont(font);
int itemWidth = font.stringWidth(item);
if (i == this.selected) {
g.setColor(this.selectedBGColor);
g.fillRect(this.x, curY - itemHeight, itemWidth + 4, itemHeight);
g.setColor(this.selectedFGColor);
g.drawString((String) this.items.elementAt(i), this.x + 2, curY,
Graphics.BOTTOM | Graphics.LEFT);
g.setColor(this.fgColor);
} else {
g.drawString((String) this.items.elementAt(i), this.x + 2, curY,
Graphics.BOTTOM | Graphics.LEFT);
}
curY -= itemHeight;
this.startPageItem = i;
}
if (this.startPageItem > 0) {
g.drawString((String) this.items.elementAt(this.startPageItem - 1), this.x + 2, curY, Graphics.BOTTOM | Graphics.LEFT);
}
}
g.setClip(clipX, clipY, clipW, clipH);
}

//////My Main List
public void setBGColor(int color) {
this.bgColor = color;
}

public void setFGColor(int color) {
this.fgColor = color;
}

public void setSelectedBGColor(int color) {
this.selectedBGColor = color;
}

public void setSelectedFGColor(int color) {
this.selectedFGColor = color;
}

public void append(String item, Font font) {
if (item == null) {
throw new NullPointerException("item");
}
if (font == null) {
font = Font.getDefaultFont();
}
this.items.addElement(item);
this.itemFonts.addElement(font);
}

public void append(String item) {
this.append(item, null);
}

public void delete(int pos) {
this.items.removeElementAt(pos);
this.itemFonts.removeElementAt(pos);
if (pos <= this.startPageItem) {
this.startPageItem--;
this.paintDown = true;
}
if (pos <= this.selected) {
this.selected--;
this.paintDown = true;
}
if (pos <= this.endPageItem) {
this.endPageItem--;
this.paintDown = true;
}
}

public void deleteAll() {
this.items.removeAllElements();
this.itemFonts.removeAllElements();
this.selected = 0;
this.startPageItem = 0;
this.endPageItem = 0;
this.paintDown = true;
}

public void set(int pos, String item, Font font) {
if (item == null) {
throw new NullPointerException("item");
}
if (font == null) {
font = Font.getDefaultFont();
}
this.items.setElementAt(item, pos);
this.itemFonts.setElementAt(font, pos);
}

public boolean isSelected(int pos) {
return pos == this.selected;
}

public int getSelectedIndex() {
return (this.items.size() > 0) ? this.selected : -1;
}

public String getSelected() {
return (this.items.size() > 0) ? (String) this.items.elementAt(this.selected) : null;
}

public boolean next() {
boolean res = false;
if (this.selected < this.items.size() - 1) {
res = true;
if (++this.selected > this.endPageItem) {
this.paintDown = false;
this.endPageItem = this.selected;
}
}
return res;
}

public boolean prev() {
boolean res = false;
if (this.selected > 0) {
res = true;
if (--this.selected < this.startPageItem) {
this.paintDown = true;
this.startPageItem = this.selected;
}
}
return res;
}

public void previous() {
if (this.selected > 0) {
this.selected--;
}
}

public void show(Graphics g) {
}

/**
* @return the y
*/
public int getY() {
return this.y;
}

/**
* @param y
* the y to set
*/
public void setY(int y) {
this.y = y;
}

/**
* @return the height
*/
public int getHeight() {
return this.height;
}

/**
* @return the width
*/
public int getWidth() {
return this.width;
}

/**
* @return the x
*/
public int getX() {
return this.x;
}
}