View Single Post
Old 02-27-2009, 12:20 AM   #2 (permalink)
icess
New Member
 
Join Date: Feb 2009
Model: 8320
PIN: N/A
Carrier: china mobile
Posts: 9
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

the full code :
Code:
package hs.bb;

import net.rim.device.api.system.KeypadListener;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.container.MainScreen;
public class TestScreen extends MainScreen {
	public TestScreen() {
		this.setTitle("Test Table Field");
		 String[] header = {"header one","header two","header three"}; 
	        String[][] value = {{"value 1 1","value 1 2","value 1 3"},{"value 1 1","value 1 2","value 1 3"},{"value 1 1","value 1 2","value 1 3"},{"value 1 1","value 1 2","value 1 3"},{"value 1 1","value 1 2","value 1 3"},{"value 1 1","value 1 2","value 1 3"},{"value 1 1","value 1 2","value 1 3"},{"value 1 1","value 1 2","value 1 3"},{"value 2 1","value 2 2","value 2 3"},{"value 3 1","value 3 2","value 3 3"}};
	        TableField st = new TableField(header,value,Field.FOCUSABLE);
	        add(st);
	}

	public class TableField extends Field {
		private int fontColor = 0x0;
		private int focusLineColor = 0xFFFFFF;
		private int[] columnWidths;
		private Font valueFont;
		private Font headerFont;
		private int headerColor = 0xFFF000;
		private int headerBgColor = 0x444444;
		private int headerHeight;
		
		private int selectedRowIndex = -1;
		private int selectedColumnIndex = -1; 
		
		private String[] headers;
		private String[][] values;
		
		private int rowPadding = 3;
		private int columnPadding = 3;
		private int horizontalPadding = 5;  
		private int verticalPadding = 5;  
		
		private int rowHeight;
		private int colNum;
		private int rowNum;
		
		public TableField(String[] headers,String[][] values,long style ) {
			super(style);
			if(headers != null && headers.length > 0) {
				this.headers = headers;
				headerFont = Font.getDefault();
				headerHeight = headerFont.getHeight()+rowPadding;
			}
			this.values = values;
			this.valueFont = Font.getDefault();
			rowHeight = this.valueFont.getHeight()+rowPadding;
			this.rowNum = values.length;
			this.colNum = values[0].length;
			int tw = 0;
			columnWidths = new int[colNum];
			for (int i = 0; i < colNum; i++) {
				for (int j = 0; j < rowNum; j++) {
					tw = valueFont.getAdvance(values[j][i]);
					if(tw > columnWidths[i]) {
						columnWidths[i] = tw;
					}
				}
			}
		}

		protected void layout(int width, int height) {
			int pw = getPreferredWidth();
			if(pw < width) {
				pw = (width - pw)/this.colNum;
				for (int i = 0; i < colNum; i++) {
					columnWidths[i] += pw;
				}
			}
			setExtent(width, getPreferredHeight());
		}

		private int xoffset = 0;
		protected void paint(Graphics g) {
			
			int x = horizontalPadding;
			int y = verticalPadding;
			if(headers != null && headers.length > 0) {
				if(headerBgColor != -1) {
					g.setColor(headerBgColor);
					g.fillRect(0, 0, getWidth(), verticalPadding+headerHeight);
				}
				if(headerColor != -1) {
					g.setColor(headerColor);
				}
				for (int i = 0; i < colNum; i++) {
					g.drawText(headers[i], x+xoffset, y);
					x += columnWidths[i] + columnPadding;
				}
				y+= headerHeight;
			}
			
			x = horizontalPadding;
			if(values != null && values.length > 0) {
				if(fontColor != -1) {
					g.setColor(fontColor);
				}
				for (int i = 0; i < colNum; i++) {
					int vy = y;
					for (int j = 0; j < rowNum; j++) {
						g.drawText(values[j][i], x+xoffset, vy);
						vy += rowHeight;
					}
					x+=columnWidths[i]+columnPadding;
				}
			}
			//paint focus row line
			if(focusLineColor != -1 && isFocus()) {
				g.setColor(focusLineColor);
				int fly = y + rowHeight * (selectedRowIndex+1)-rowPadding/2;
				g.drawLine(0, fly, getWidth(), fly);
			}
			g.setColor(fontColor);
			g.drawRect(0, 0, getWidth(), getHeight());
		}

		public void getFocusRect(XYRect rect) {
			rect.set(0, verticalPadding+headerHeight+rowHeight*selectedRowIndex, getWidth(), rowHeight);
		}

		protected int moveFocus(int amount, int status, int time) {
			//TODO  how to implement this method?
			return super.moveFocus(amount, status, time);
		}
		

		protected boolean navigationMovement(int dx, int dy, int status, int time) {
			if ((status&KeypadListener.STATUS_FOUR_WAY) == KeypadListener.STATUS_FOUR_WAY) { 
				if (dx > 0) {
					xoffset -= 10;
					invalidate();
					return true;
				} else if (dx < 0) {
					xoffset += 10;
					invalidate();
					if(xoffset > 0) {
						xoffset = 0;
						return true;
					}
				} else if (dy > 0) {
					if(selectedRowIndex < this.rowNum) {
						selectedRowIndex ++;
						if(selectedRowIndex == rowNum) {
							selectedRowIndex = rowNum-1;
							return false;
						}
						invalidate();
						return true;
					}
				} else if (dy < 0) {
					if(selectedRowIndex > 0) {
						selectedRowIndex --;
						if(selectedRowIndex < 0) {
							selectedRowIndex = 0;
							return false;
						}
						invalidate();
						return true;
					}
				}
			}  
			return super.navigationMovement(dx, dy, status, time);
		}

		protected void onFocus(int direction) {
			if(selectedRowIndex == -1) {
				selectedRowIndex = 0;
			}
			super.onFocus(direction);
		}

		protected void onUnfocus() {
			super.onUnfocus();
			invalidate();
		}

		public int getPreferredHeight() {
			return  headerHeight+(rowHeight*rowNum)+(verticalPadding<<1);
		}

		public int getPreferredWidth() {
			int w = 0;
			for (int i = 0; i < colNum; i++) {
				w += columnWidths[i]+columnPadding;
			}
			if(w > 0) w -= columnPadding;
			w += (horizontalPadding<<1);
			return w;
		}
	}

}
when the selected row is outside the screen , how to scroll the screen to dispaly the row?
Offline   Reply With Quote