Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.newsapp.ui;
- import java.util.Vector;
- import com.newsapp.Item;
- import net.rim.blackberry.api.browser.Browser;
- import net.rim.blackberry.api.browser.BrowserSession;
- import net.rim.device.api.system.Display;
- import net.rim.device.api.ui.Color;
- import net.rim.device.api.ui.Font;
- import net.rim.device.api.ui.Graphics;
- import net.rim.device.api.ui.component.ListField;
- import net.rim.device.api.ui.component.ListFieldCallback;
- public class TableList extends ListField {
- private Vector _data = null;
- private TableListCallback _callback = null;
- private Font _font = null;
- private int currentPosition = 0;
- public TableList(Vector data) {
- this._data = data;
- this._font = this.getFont().derive(Font.PLAIN, 13);
- this.setFont(this._font);
- this.setRowHeight(-3);
- this.init();
- }
- private void init() {
- this._callback = new TableListCallback();
- this.setCallback(this._callback);
- }
- protected boolean navigationClick(int status, int time) {
- int index = this.getSelectedIndex();
- Item item = (Item)_data.elementAt(index);
- if(item.link != "") {
- BrowserSession browser = Browser.getDefaultSession();
- browser.displayPage(item.link);
- }
- return super.navigationClick(status, time);
- }
- private class TableListCallback implements ListFieldCallback {
- public TableListCallback() {
- }
- public void drawListRow(ListField list, Graphics graphics, int index, int y, int width) {
- currentPosition = index;
- Item item = (Item)_data.elementAt(index);
- Vector lines = wrap(item.description, width - 20);
- int fontHeight = TableList.this.getFont().getHeight();
- int xPos = 10;
- int yPos = 5 + y;
- graphics.setColor(Color.BLACK);
- graphics.drawRect(0, y, width, (lines.size() + 2)*fontHeight);
- graphics.setColor(Color.BLACK);
- graphics.setFont(TableList.this.getFont().derive(Font.BOLD, 13));
- graphics.drawText(item.title + ":", xPos, yPos, 0, width);
- yPos = yPos + fontHeight;
- int yLine = 0;
- for(int i=0; i<lines.size(); i++) {
- yLine = i * fontHeight;
- graphics.setColor(Color.BLACK);
- graphics.setFont(TableList.this.getFont().derive(Font.PLAIN, 13));
- graphics.drawText((String)lines.elementAt(i), xPos, yLine + yPos, 0, width);
- }
- list.setRowHeight(index, (lines.size() + 2) * fontHeight);
- }
- public Object get(ListField list, int index) {
- return _data.elementAt(index);
- }
- public int getPreferredWidth(ListField list) {
- return Display.getWidth();
- }
- public int indexOfList(ListField list, String prefix, int start) {
- return _data.indexOf(prefix, start);
- }
- private Vector wrap(String text, int width) {
- Vector result = new Vector();
- if(text == null){
- return result;
- }
- boolean hasMore = true;
- int current = 0;
- int lineBreak = -1;
- int nextSpace = -1;
- while (hasMore){
- while (true){
- lineBreak = nextSpace;
- if(lineBreak == text.length() - 1){
- hasMore = false;
- break;
- } else {
- nextSpace = text.indexOf(' ', lineBreak+1);
- if(nextSpace == -1){
- nextSpace = text.length() -1;
- }
- int linewidth = TableList.this.getFont().getAdvance(text,current, nextSpace-current);
- if(linewidth > width){
- break;
- }
- }
- }
- String line = text.substring(current, lineBreak + 1);
- result.addElement(line);
- current = lineBreak + 1;
- }
- return result;
- }
- }
- public int getCurrentPosition() {
- return this.currentPosition;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement