Advertisement
jfrubiom

ui/TableList.java

Sep 17th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.27 KB | None | 0 0
  1. package com.newsapp.ui;
  2.  
  3. import java.util.Vector;
  4.  
  5. import com.newsapp.Item;
  6.  
  7. import net.rim.blackberry.api.browser.Browser;
  8. import net.rim.blackberry.api.browser.BrowserSession;
  9. import net.rim.device.api.system.Display;
  10. import net.rim.device.api.ui.Color;
  11. import net.rim.device.api.ui.Font;
  12. import net.rim.device.api.ui.Graphics;
  13. import net.rim.device.api.ui.component.ListField;
  14. import net.rim.device.api.ui.component.ListFieldCallback;
  15.  
  16. public class TableList extends ListField {
  17.         private Vector _data = null;
  18.         private TableListCallback _callback = null;
  19.         private Font _font = null;
  20.        
  21.         private int currentPosition = 0;
  22.        
  23.         public TableList(Vector data) {
  24.                 this._data = data;
  25.                 this._font = this.getFont().derive(Font.PLAIN, 13);
  26.                 this.setFont(this._font);
  27.                 this.setRowHeight(-3);
  28.                
  29.                 this.init();
  30.         }
  31.        
  32.         private void init() {
  33.                 this._callback = new TableListCallback();
  34.                 this.setCallback(this._callback);
  35.         }
  36.        
  37.         protected boolean navigationClick(int status, int time) {
  38.                 int index = this.getSelectedIndex();
  39.                 Item item = (Item)_data.elementAt(index);
  40.                
  41.                 if(item.link != "") {
  42.                         BrowserSession browser = Browser.getDefaultSession();
  43.                         browser.displayPage(item.link);
  44.                 }
  45.                
  46.                 return super.navigationClick(status, time);
  47.         }
  48.        
  49.         private class TableListCallback implements ListFieldCallback {
  50.                 public TableListCallback() {
  51.                                      
  52.                 }
  53.                
  54.                 public void drawListRow(ListField list, Graphics graphics, int index, int y, int width) {
  55.                         currentPosition = index;
  56.                        
  57.                         Item item = (Item)_data.elementAt(index);
  58.                         Vector lines = wrap(item.description, width - 20);
  59.                        
  60.                         int fontHeight = TableList.this.getFont().getHeight();
  61.                         int xPos = 10;
  62.                         int yPos = 5 + y;
  63.                        
  64.                         graphics.setColor(Color.BLACK);
  65.                         graphics.drawRect(0, y, width, (lines.size() + 2)*fontHeight);
  66.                         graphics.setColor(Color.BLACK);
  67.                         graphics.setFont(TableList.this.getFont().derive(Font.BOLD, 13));
  68.                         graphics.drawText(item.title + ":", xPos, yPos, 0, width);
  69.                         yPos = yPos + fontHeight;
  70.                        
  71.                         int yLine = 0;
  72.                        
  73.                         for(int i=0; i<lines.size(); i++) {
  74.                                 yLine = i * fontHeight;
  75.                                 graphics.setColor(Color.BLACK);
  76.                                 graphics.setFont(TableList.this.getFont().derive(Font.PLAIN, 13));
  77.                                 graphics.drawText((String)lines.elementAt(i), xPos, yLine + yPos, 0, width);                            
  78.                         }
  79.                        
  80.                         list.setRowHeight(index, (lines.size() + 2) * fontHeight);
  81.                 }
  82.                
  83.         public Object get(ListField list, int index) {
  84.             return _data.elementAt(index);
  85.         }
  86.        
  87.         public int getPreferredWidth(ListField list) {
  88.                 return Display.getWidth();
  89.         }
  90.        
  91.         public int indexOfList(ListField list, String prefix, int start) {              
  92.                 return _data.indexOf(prefix, start);
  93.         }
  94.                
  95.                 private Vector wrap(String text, int width) {
  96.             Vector result = new Vector();
  97.             if(text == null){
  98.                 return result;
  99.             }
  100.            
  101.             boolean hasMore = true;
  102.            
  103.            
  104.             int current = 0;
  105.             int lineBreak = -1;
  106.             int nextSpace = -1;
  107.            
  108.             while (hasMore){            
  109.                 while (true){
  110.                     lineBreak = nextSpace;
  111.                     if(lineBreak == text.length() - 1){
  112.                         hasMore = false;
  113.                         break;
  114.                     } else {
  115.                         nextSpace = text.indexOf(' ', lineBreak+1);
  116.                         if(nextSpace == -1){
  117.                             nextSpace = text.length() -1;
  118.                         }
  119.                         int linewidth = TableList.this.getFont().getAdvance(text,current, nextSpace-current);
  120.                         if(linewidth > width){
  121.                             break;
  122.                         }
  123.                     }
  124.                 }
  125.                
  126.                 String line = text.substring(current, lineBreak + 1);
  127.                 result.addElement(line);
  128.                 current = lineBreak + 1;
  129.             }
  130.            
  131.             return result;
  132.                 }
  133.         }
  134.        
  135.         public int getCurrentPosition() {
  136.                 return this.currentPosition;
  137.         }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement