Table.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:8k
源码类别:

J2ME

开发平台:

Java

  1. /*
  2.  * %W% %E%
  3.  *
  4.  * Copyright (c) 2000-2004 Sun Microsystems, Inc. All rights reserved. 
  5.  * PROPRIETARY/CONFIDENTIAL
  6.  * Use is subject to license terms
  7.  */
  8. package customitem;
  9. import javax.microedition.lcdui.*;
  10. /**
  11.  *
  12.  * @version 2.0
  13.  */
  14. public class Table
  15.     extends CustomItem
  16.     implements ItemCommandListener {
  17.     private final static Command CMD_EDIT = new Command("Edit", 
  18.                                                            Command.ITEM, 1);
  19.     private Display display;
  20.     private int rows = 5;
  21.     private int cols = 3;
  22.     private int dx = 50;
  23.     private int dy = 20;
  24.     private final static int UPPER = 0;
  25.     private final static int IN = 1;
  26.     private final static int LOWER = 2;
  27.     private int location = UPPER;
  28.     private int currentX = 0;
  29.     private int currentY = 0;
  30.     private String[][] data = new String[rows][cols];
  31.     
  32.     // Traversal stuff     
  33.     // indicating support of horizontal traversal internal to the CustomItem
  34.     boolean horz;
  35.     
  36.     // indicating support for vertical traversal internal to the CustomItem.
  37.     boolean vert;
  38.     public Table(String title, Display d) {
  39.         super(title);
  40.         display = d;
  41.         setDefaultCommand(CMD_EDIT);
  42.         setItemCommandListener(this);
  43.         int interactionMode = getInteractionModes();
  44.         horz = ((interactionMode & CustomItem.TRAVERSE_HORIZONTAL) != 0);
  45.         vert = ((interactionMode & CustomItem.TRAVERSE_VERTICAL) != 0);
  46.     }
  47.     protected int getMinContentHeight() {
  48.         return (rows * dy) + 1;
  49.     }
  50.     protected int getMinContentWidth() {
  51.         return (cols * dx) + 1;
  52.     }
  53.     protected int getPrefContentHeight(int width) {
  54.         return (rows * dy) + 1;
  55.     }
  56.     protected int getPrefContentWidth(int height) {
  57.         return (cols * dx) + 1;
  58.     }
  59.     protected void paint(Graphics g, int w, int h) {
  60.         for (int i = 0; i <= rows; i++) {
  61.             g.drawLine(0, i * dy, cols * dx, i * dy);
  62.         }
  63.         for (int i = 0; i <= cols; i++) {
  64.             g.drawLine(i * dx, 0, i * dx, rows * dy);
  65.         }
  66.         int oldColor = g.getColor();
  67.         g.setColor(0x00D0D0D0);
  68.         g.fillRect((currentX * dx) + 1, (currentY * dy) + 1, dx - 1, dy - 1);
  69.         g.setColor(oldColor);
  70.         for (int i = 0; i < rows; i++) {
  71.             for (int j = 0; j < cols; j++) {
  72.                 if (data[i][j] != null) {
  73.                     // store clipping properties
  74.                     int oldClipX = g.getClipX();
  75.                     int oldClipY = g.getClipY();
  76.                     int oldClipWidth = g.getClipWidth();
  77.                     int oldClipHeight = g.getClipHeight();
  78.                     g.setClip((j * dx) + 1, i * dy, dx - 1, dy - 1);
  79.                     g.drawString(data[i][j], (j * dx) + 2, ((i + 1) * dy) - 2, 
  80.                                  Graphics.BOTTOM | Graphics.LEFT);
  81.                     // restore clipping properties
  82.                     g.setClip(oldClipX, oldClipY, oldClipWidth, oldClipHeight);
  83.                 }
  84.             }
  85.         }
  86.     }
  87.     protected boolean traverse(int dir, int viewportWidth, int viewportHeight, 
  88.                                int[] visRect_inout) {
  89.         
  90.         if (horz && vert) {                                  
  91.             switch (dir) {
  92.     
  93.                 case Canvas.DOWN:
  94.     
  95.                     if (location == UPPER) {
  96.                         location = IN;
  97.                     } else {
  98.     
  99.                         if (currentY < (rows - 1)) {
  100.                             currentY++;
  101.                             repaint(currentX * dx, (currentY - 1) * dy, dx, dy);
  102.                             repaint(currentX * dx, currentY * dy, dx, dy);
  103.                         } else {
  104.                             location = LOWER;
  105.     
  106.                             return false;
  107.                         }
  108.                     }
  109.     
  110.                     break;
  111.     
  112.                 case Canvas.UP:
  113.     
  114.                     if (location == LOWER) {
  115.                         location = IN;
  116.                     } else {
  117.     
  118.                         if (currentY > 0) {
  119.                             currentY--;
  120.                             repaint(currentX * dx, (currentY + 1) * dy, dx, dy);
  121.                             repaint(currentX * dx, currentY * dy, dx, dy);
  122.                         } else {
  123.                             location = UPPER;
  124.     
  125.                             return false;
  126.                         }
  127.                     }
  128.     
  129.                     break;
  130.     
  131.                 case Canvas.LEFT:
  132.     
  133.                     if (currentX > 0) {
  134.                         currentX--;
  135.                         repaint((currentX + 1) * dx, currentY * dy, dx, dy);
  136.                         repaint(currentX * dx, currentY * dy, dx, dy);
  137.                     }
  138.     
  139.                     break;
  140.     
  141.                 case Canvas.RIGHT:
  142.     
  143.                     if (currentX < (cols - 1)) {
  144.                         currentX++;
  145.                         repaint((currentX - 1) * dx, currentY * dy, dx, dy);
  146.                         repaint(currentX * dx, currentY * dy, dx, dy);
  147.                     }
  148.             }
  149.         } else if (horz || vert) {
  150.             switch (dir) {
  151.          
  152.                 case Canvas.UP:
  153.                 case Canvas.LEFT:
  154.                     
  155.                     if (location == LOWER) {
  156.                         location = IN;
  157.                     } else {
  158.     
  159.                         if (currentX > 0) {
  160.                             currentX--;
  161.                             repaint((currentX + 1) * dx, currentY * dy, dx, dy);
  162.                             repaint(currentX * dx, currentY * dy, dx, dy);
  163.                         } else if (currentY > 0) {
  164.                             currentY--;
  165.                             repaint(currentX * dx, (currentY + 1) * dy, dx, dy);
  166.                             currentX = cols - 1;
  167.                             repaint(currentX * dx, currentY * dy, dx, dy);
  168.                         } else {
  169.                             location = UPPER;
  170.                             return false;
  171.                         }
  172.                     }
  173.     
  174.                     break;
  175.     
  176.                 case Canvas.DOWN:
  177.                 case Canvas.RIGHT:
  178.                     if (location == UPPER) {
  179.                         location = IN;
  180.                     } else {
  181.     
  182.                         if (currentX < (cols - 1)) {
  183.                             currentX++;
  184.                             repaint((currentX - 1) * dx, currentY * dy, dx, dy);
  185.                             repaint(currentX * dx, currentY * dy, dx, dy);
  186.                         } else if (currentY < (rows - 1)) {
  187.                             currentY++;                    
  188.                             repaint(currentX * dx, (currentY - 1) * dy, dx, dy);
  189.                             currentX = 0;
  190.                             repaint(currentX * dx, currentY * dy, dx, dy);
  191.                         } else {
  192.                             location = LOWER;
  193.                             return false;
  194.                         }
  195.                     }
  196.                         
  197.             }
  198.         } else { 
  199.             // In case of no Traversal at all: (horz|vert) == 0
  200.         }
  201.         visRect_inout[0] = currentX;
  202.         visRect_inout[1] = currentY;
  203.         visRect_inout[2] = dx;
  204.         visRect_inout[3] = dy;
  205.         return true;
  206.     }
  207.     public void setText(String text) {
  208.         data[currentY][currentX] = text;
  209.         repaint(currentY * dx, currentX * dy, dx, dy);
  210.     }
  211.     public void commandAction(Command c, Item i) {
  212.         if (c == CMD_EDIT) {
  213.             TextInput textInput = new TextInput(data[currentY][currentX], this, 
  214.                                                 display);
  215.             display.setCurrent(textInput);
  216.         }
  217.     }
  218. }