⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listcanvas.java

📁 tRSS是MIDP的的RSS客户端。用户可以从一些新闻网站和个人数据库和feed阅读标题。
💻 JAVA
字号:
/*
 * ListCanvas.java
 *
 * Created on January 13, 2006, 6:54 PM
 */

package com.dtw.midp.screen;

import java.util.Vector;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

import com.dtw.midp.data.ItemDetail;
import com.dtw.midp.Localization;
/**
 *
 * 
 * @author psaingtong
 * 
 *
 */
public class ListCanvas extends Canvas implements Runnable  {
    
    /** Creates a new instance of ListCanvas */
    public ListCanvas() {
    }
    private Display tDisplay;
  
  // The data we display.
    private Vector tItems, marks;
    private int tSelection;
  
  // Helpers for marking.
    private boolean markingAllowed = true;
    private static final Boolean kFalse = new Boolean(false);
    private static final Boolean kTrue = new Boolean(true);
  
  // General information.
    private int width, height;
    private int headerHeight, footerHeight,logoHeight;
    private Font tFont;
    private int lineHeight;
    private int leftMargin;
    private String tListCanvas;
 
  
  // Variables for paging.
    private int tScroll, lastVisible, pageSize;
  
    private ItemCanvas itemCanvas; 
    private MIDlet midlet;
  // Useful images.
    protected static Image kSelectionImage,
      kUpImage, kDownImage, kLeftImage, kRightImage,
      kMarkImage, logoImage ;

    /** Creates a new instance of tListCanvas */
    public ListCanvas(Display display, MIDlet _midlet, String _tListCanvas) {
      if (kSelectionImage == null) loadImages();
        tDisplay = display;
        midlet = _midlet;
        tListCanvas=_tListCanvas;
       
 
        logoHeight=logoImage.getHeight();
        headerHeight = kUpImage.getHeight();
        headerHeight=logoHeight+headerHeight;
        footerHeight = kDownImage.getHeight();
        leftMargin = kSelectionImage.getWidth() + kMarkImage.getWidth();

        width = getWidth();
        height = getHeight();
        
        tFont = Font.getFont(Font.FACE_PROPORTIONAL,
        Font.STYLE_PLAIN,Font.SIZE_SMALL);
        lineHeight = tFont.getHeight();

        clear();
    
        Thread t = new Thread(this);
        t.start();

    }
  /*
   *load image
   *
   */  
  private static void loadImages() {
    try {
      kSelectionImage = Image.createImage(Localization.getMessage("image.selection"));
      logoImage = Image.createImage(Localization.getMessage("image.logo"));
      kUpImage = Image.createImage(Localization.getMessage("image.up"));
      kDownImage = Image.createImage(Localization.getMessage("image.down"));
      kLeftImage = Image.createImage(Localization.getMessage("image.left"));
      kRightImage = Image.createImage(Localization.getMessage("image.right"));
      kMarkImage = Image.createImage(Localization.getMessage("image.mark"));
     
    }
    catch (java.io.IOException ioe) { System.out.println(ioe); }
  }
  /**
   * Determines whether marking is allowed.
   */
  public void setMarkingAllowed(boolean markingAllowed) {
    markingAllowed = markingAllowed;
  }
  
  /**
   * returns the number of items in this list.
   */
  public int size() { return tItems.size(); }
  
  /**
   * Removes all items from the list.
   */
  public void clear() {
    tItems = new Vector();
    marks = new Vector();
    tSelection = 0;
    tScroll = 0;
  }
   
  
  public ItemDetail getItem(int i) {
    return (ItemDetail)tItems.elementAt(i);
  }
  
   
  /**
   * Returns the title for the item at the specified
   * index.
   */
  public String getItemTitle(int i) {
    ItemDetail item = (ItemDetail)tItems.elementAt(i);
    return item.getItemTitle();
  }

  /**
   * Returns the link for the item at the specified
   * index.
   */
  public String getItemLink(int i) {
    ItemDetail item = (ItemDetail)tItems.elementAt(i);
    return item.getItemLink();
  }

  /**
   * Returns the description for the item at the specified
   * index.
   */
  public String getItemDescription(int i) {
    ItemDetail item = (ItemDetail)tItems.elementAt(i);
    return item.getItemDescription();
  }

  /**
   * Tells whether the item at the specified index has
   * been marked.
   */
  public boolean isMarked(int i) {
    return (marks.elementAt(i) == kTrue);
  }
  
  /**
   * Returns true if any of the items have been
   * marked.
   */
  public boolean hasMarks() {
    boolean hasMarks = false;
    for (int i = 0; i < size(); i++) {
      if (isMarked(i) == true) {
        hasMarks = true;
        break;
      }
    }
    return hasMarks;
  }

  /**
   * Adds a new item to the list.
   *
   * @param title as the title for the new item
   * @param link the link for the new item
   * @param description the description for the new item
   */
  public void addItem(String title, String link, String description) {
 
    ItemDetail item=new ItemDetail();
    item.setItemTitle(title);
    item.setItemLink(link);
    item.setItemDecription(description);
    tItems.addElement(item);
    marks.addElement(kFalse); 
    repaint();
  }
  
  /**
   * Shows the details screen for the current item.
   */
  public void details() {
      tDisplay=Display.getDisplay(midlet); 
      ItemDetail item = (ItemDetail)tItems.elementAt(tSelection);
      itemCanvas = new ItemCanvas(tDisplay, this, item);
      tDisplay.setCurrent(itemCanvas);
  }
 
  /**
   * Toggles the mark for the current item.
   */
  public void mark() {
    if(tListCanvas.equals("offListCanvas")) {
        if (markingAllowed == true) {
      if (marks.elementAt(tSelection) == kFalse)
        marks.setElementAt(kTrue, tSelection);
      else
        marks.setElementAt(kFalse, tSelection);
     }
        repaint();
    }else{
    if (markingAllowed == true) {
      if (marks.elementAt(tSelection) == kFalse)
        marks.setElementAt(kTrue, tSelection);
      else
        marks.setElementAt(kFalse, tSelection);
      repaint();
     }
    }
  }
  
 
  /**
   * Called internally.
   */
  public void run() {
    while (true) {
      try { Thread.sleep(100); }
      catch (InterruptedException ie) {}
   
    }
  }
  
  /**
   * Paints the item list.
   */
  public void paint(Graphics g) {
    // Clear the screen.
    g.setColor(247, 247, 238);
    g.fillRect(0, 0, width, height);
    g.drawImage(logoImage, width / 2, 0,
          Graphics.HCENTER | Graphics.TOP);

    // Set defaults.
    g.setColor(0, 0, 0);
    g.setFont(tFont);
    
    // Draw the header if appropriate.
    paintHeader(g);
    
    // Draw as many items as will fit between the header and the footer.
    paintBody(g);
    
    // Draw footer.
    paintFooter(g);
  }
  
  private void paintHeader(Graphics g) {
    if (tScroll != 0)
      g.drawImage(kUpImage, width / 2, 0,
          Graphics.HCENTER | Graphics.TOP);
  }
  
  private void paintBody(Graphics g) {
    int y = headerHeight;
    boolean trucking = true;
    int index = tScroll;
    while (trucking) {
      if (index >= tItems.size()) trucking = false;
      else if (y + lineHeight >= (height - footerHeight)) trucking = false;
      else {
          if (marks.elementAt(index) == kTrue){
              g.setColor(250, 0, 0);
          }else{
          if(index%2==1){
              g.setColor(128, 64, 0);
          }else{
              g.setColor(0, 0, 0);
          }
         }
        // Draw the item.
        ItemDetail item = (ItemDetail)tItems.elementAt(index);
        String title = item.getItemTitle();
         
        g.drawString(title, leftMargin, y,
            Graphics.TOP | Graphics.LEFT);
        // If it's too wide, show a right arrow.
        if (tFont.stringWidth(title) >
            (width - leftMargin)) {
          // Clear the area.
          int oldColor = g.getColor();
          g.setColor(255, 255, 255);
          g.fillRect(width - kRightImage.getWidth(), y,
              kRightImage.getWidth(), lineHeight);
          g.setColor(oldColor);
          // Draw the right arrow.
          g.drawImage(kRightImage, width, y + lineHeight / 2,
              Graphics.VCENTER | Graphics.RIGHT);
          
        }
           g.setColor(201, 201, 148);
           g.drawLine(leftMargin, y, width, y);
      
        // Draw the mark.
        if (marks.elementAt(index) == kTrue)
          g.drawImage(kMarkImage, leftMargin, y + lineHeight / 2,
              Graphics.VCENTER  | Graphics.RIGHT);

        // Draw the highlight.
        if (index == tSelection)
          g.drawImage(kSelectionImage, 0, y + lineHeight / 2,
              Graphics.VCENTER  | Graphics.LEFT);
        
        index++;
        y += lineHeight;
      }
    }
    
    // Set the last visible item index.
    lastVisible = index - 1;
  }
  
  private void paintFooter(Graphics g) {   
    
    // Draw down arrow.
    if (lastVisible < (tItems.size() - 1)) {
      g.drawImage(kDownImage, width / 2, height - footerHeight,
          Graphics.HCENTER | Graphics.TOP);
    }
  }
  
  /**
   * Responds to key presses.
   */
  protected void keyPressed(int keyCode) {
    int gameAction = getGameAction(keyCode);
    switch(gameAction) {
      case UP:
        if (tSelection > 0) {
          tSelection--;
          // Page up if necessary.
          if (tSelection < tScroll) {
            tScroll -= pageSize;
            if (tScroll < 0) tScroll = 0;
          }
          repaint();
        }
        break;
      case DOWN:
        if (tSelection < (tItems.size() - 1)) {
          tSelection++;
          // Page down if necessary.
          if (tSelection > lastVisible) {
            if (pageSize == 0) pageSize = lastVisible + 1;
            tScroll = tSelection;
          }
          repaint();
        }
        break;
      case RIGHT:
          details(); 
        break;
      case FIRE:
        mark();
        break;
    }
  }

   
 
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -