Table.java
上传用户:haobig99
上传日期:2022-06-15
资源大小:369k
文件大小:10k
源码类别:

J2ME

开发平台:

Java

  1. /*
  2.  * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
  3.  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4.  *
  5.  * This code is free software; you can redistribute it and/or modify it
  6.  * under the terms of the GNU General Public License version 2 only, as
  7.  * published by the Free Software Foundation.  Sun designates this
  8.  * particular file as subject to the "Classpath" exception as provided
  9.  * by Sun in the LICENSE file that accompanied this code.
  10.  *
  11.  * This code is distributed in the hope that it will be useful, but WITHOUT
  12.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14.  * version 2 for more details (a copy is included in the LICENSE file that
  15.  * accompanied this code).
  16.  *
  17.  * You should have received a copy of the GNU General Public License version
  18.  * 2 along with this work; if not, write to the Free Software Foundation,
  19.  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20.  *
  21.  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22.  * CA 95054 USA or visit www.sun.com if you need additional information or
  23.  * have any questions.
  24.  */
  25. package com.sun.lwuit.table;
  26. import com.sun.lwuit.Component;
  27. import com.sun.lwuit.Container;
  28. import com.sun.lwuit.Form;
  29. import com.sun.lwuit.Graphics;
  30. import com.sun.lwuit.Label;
  31. import com.sun.lwuit.TextArea;
  32. import com.sun.lwuit.TextField;
  33. import com.sun.lwuit.events.ActionEvent;
  34. import com.sun.lwuit.events.ActionListener;
  35. import com.sun.lwuit.events.DataChangedListener;
  36. import com.sun.lwuit.plaf.Border;
  37. import com.sun.lwuit.plaf.Style;
  38. /**
  39.  * The table class represents a grid of data that can be used for rendering a grid
  40.  * of components/labels. The table reflects and updates the underlying model data.
  41.  *
  42.  * @author Shai Almog
  43.  */
  44. public class Table extends Container {
  45.     private TableModel model;
  46.     private Listener listener = new Listener();
  47.     private boolean drawBorder = true;
  48.     /**
  49.      * Indicates the alignment of the title see label alignment for details
  50.      * 
  51.      * @see com.sun.lwuit.Label#setAlignment(int) 
  52.      */
  53.     private int titleAlignment = Label.CENTER;
  54.     /**
  55.      * Indicates the alignment of the cells see label alignment for details
  56.      * 
  57.      * @see com.sun.lwuit.Label#setAlignment(int)
  58.      */
  59.     private int cellAlignment = Label.LEFT;
  60.     /**
  61.      * Create a table with a new model
  62.      *
  63.      * @param model the model underlying this table
  64.      */
  65.     public Table(TableModel model) {
  66.         this.model = model;
  67.         updateModel();
  68.         setUIID("Table");
  69.     }
  70.     private void updateModel() {
  71.         int selectionRow = -1, selectionColumn = -1;
  72.         Form f = getComponentForm();
  73.         if(f != null) {
  74.             Component c = f.getFocused();
  75.             if(c != null) {
  76.                 selectionRow = getCellRow(c);
  77.                 selectionColumn = getCellColumn(c);
  78.             }
  79.         }
  80.         removeAll();
  81.         int columnCount = model.getColumnCount();
  82.         // another row for the table header
  83.         setLayout(new TableLayout(model.getRowCount() + 1, columnCount));
  84.         for(int iter = 0 ; iter < columnCount ; iter++) {
  85.             Component header = createCellImpl(model.getColumnName(iter), -1, columnCount, false);
  86.             addComponent(null, header);
  87.         }
  88.         for(int r = 0 ; r < model.getRowCount() ; r++) {
  89.             for(int c = 0 ; c < columnCount ; c++) {
  90.                 Object value = model.getValueAt(r, c);
  91.                 boolean e = model.isCellEditable(r, c);
  92.                 Component cell = createCellImpl(value, r, c, e);
  93.                 addComponent(null, cell);
  94.                 cell.setFocusable(true);
  95.                 if(r == selectionRow && c == selectionColumn) {
  96.                     cell.requestFocus();
  97.                 }
  98.             }
  99.         }
  100.     }
  101.     private Component createCellImpl(Object value, final int row, final int column, boolean editable) {
  102.         Component c = createCell(value, row, column, editable);
  103.         c.putClientProperty("row", new Integer(row));
  104.         c.putClientProperty("column", new Integer(column));
  105.         
  106.         // we do this here to allow subclasses to return a text area or its subclass
  107.         if(c instanceof TextArea) {
  108.             ((TextArea)c).addActionListener(listener);
  109.         } 
  110.         Style s = c.getSelectedStyle();
  111.         s.setMargin(0, 0, 0, 0);
  112.         if(drawBorder) {
  113.             Border b = new Border() {
  114.                 public void paint(Graphics g, Component c) {
  115.                     g.setColor(getUnselectedStyle().getFgColor());
  116.                     if(row == getModel().getRowCount() - 1) {
  117.                         if(column == getModel().getColumnCount() - 1) { 
  118.                             g.drawRect(c.getX(), c.getY(), c.getWidth() - 1, c.getHeight() - 1);
  119.                         } else {
  120.                             g.drawRect(c.getX(), c.getY(), c.getWidth(), c.getHeight() - 1);
  121.                         }
  122.                     } else {
  123.                         if(column == getModel().getColumnCount() - 1) {
  124.                             g.drawRect(c.getX(), c.getY(), c.getWidth() - 1, c.getHeight());
  125.                         } else {
  126.                             g.drawRect(c.getX(), c.getY(), c.getWidth(), c.getHeight());
  127.                         }
  128.                     }
  129.                 }
  130.             };
  131.             s.setBorder(b);
  132.             s = c.getUnselectedStyle();
  133.             s.setBorder(b);
  134.         } else {
  135.             s = c.getUnselectedStyle();
  136.         }
  137.         s.setBgTransparency(0);
  138.         s.setMargin(0, 0, 0, 0);
  139.         return c;
  140.     }
  141.     /**
  142.      * Creates a cell based on the given value
  143.      *
  144.      * @param value the new value object
  145.      * @param row row number, -1 for the header rows
  146.      * @param column column number
  147.      * @param editable true if the cell is editable
  148.      * @return cell component instance
  149.      */
  150.     protected Component createCell(Object value, int row, int column, boolean editable) {
  151.         if(row == -1) {
  152.             Label header = new Label((String)value);
  153.             header.setUIID("TableHeader");
  154.             header.setAlignment(titleAlignment);
  155.             return header;
  156.         }
  157.         if(editable) {
  158.             TextField cell = new TextField(value.toString(), -1);
  159.             cell.setLeftAndRightEditingTrigger(false);
  160.             cell.setUIID("TableCell");
  161.             return cell;
  162.         }
  163.         Label cell = new Label(value.toString());
  164.         cell.setUIID("TableCell");
  165.         cell.setAlignment(cellAlignment);
  166.         return cell;
  167.     }
  168.     /**
  169.      * @inheritDoc
  170.      */
  171.     public void initComponent() {
  172.         model.addDataChangeListener(listener);
  173.     }
  174.     /**
  175.      * @inheritDoc
  176.      */
  177.     public void deinitialize() {
  178.         model.removeDataChangeListener(listener);
  179.     }
  180.     /**
  181.      * Replaces the underlying model
  182.      *
  183.      * @param model the new model
  184.      */
  185.     public void setModel(TableModel model) {
  186.         this.model = model;
  187.         updateModel();
  188.         revalidate();
  189.     }
  190.     /**
  191.      * Returns the model instance
  192.      *
  193.      * @return the model instance
  194.      */
  195.     public TableModel getModel() {
  196.         return model;
  197.     }
  198.     /**
  199.      * @return the drawBorder
  200.      */
  201.     public boolean isDrawBorder() {
  202.         return drawBorder;
  203.     }
  204.     /**
  205.      * @param drawBorder the drawBorder to set
  206.      */
  207.     public void setDrawBorder(boolean drawBorder) {
  208.         this.drawBorder = drawBorder;
  209.         repaint();
  210.     }
  211.     /**
  212.      * Indicates the alignment of the title see label alignment for details
  213.      *
  214.      * @return the title alignment
  215.      * @see com.sun.lwuit.Label#setAlignment(int)
  216.      */
  217.     public int getTitleAlignment() {
  218.         return titleAlignment;
  219.     }
  220.     /**
  221.      * Indicates the alignment of the title see label alignment for details
  222.      *
  223.      * @param titleAlignment the title alignment
  224.      * @see com.sun.lwuit.Label#setAlignment(int)
  225.      */
  226.     public void setTitleAlignment(int titleAlignment) {
  227.         this.titleAlignment = titleAlignment;
  228.         repaint();
  229.     }
  230.     /**
  231.      * Returns the column in which the given cell is placed
  232.      * 
  233.      * @param cell the component representing the cell placed in the table
  234.      * @return the column in which the cell was placed in the table
  235.      */
  236.     public int getCellColumn(Component cell) {
  237.         Integer i = ((Integer)cell.getClientProperty("column"));
  238.         if(i != null) {
  239.             return i.intValue();
  240.         }
  241.         return -1;
  242.     }
  243.     /**
  244.      * Returns the row in which the given cell is placed
  245.      * 
  246.      * @param cell the component representing the cell placed in the table
  247.      * @return the row in which the cell was placed in the table
  248.      */
  249.     public int getCellRow(Component cell) {
  250.         Integer i = ((Integer)cell.getClientProperty("row"));
  251.         if(i != null) {
  252.             return i.intValue();
  253.         }
  254.         return -1;
  255.     }
  256.     /**
  257.      * Indicates the alignment of the cells see label alignment for details
  258.      *
  259.      * @see com.sun.lwuit.Label#setAlignment(int)
  260.      * @return the cell alignment
  261.      */
  262.     public int getCellAlignment() {
  263.         return cellAlignment;
  264.     }
  265.     /**
  266.      * Indicates the alignment of the cells see label alignment for details
  267.      *
  268.      * @param cellAlignment the table cell alignment
  269.      * @see com.sun.lwuit.Label#setAlignment(int)
  270.      */
  271.     public void setCellAlignment(int cellAlignment) {
  272.         this.cellAlignment = cellAlignment;
  273.         repaint();
  274.     }
  275.     class Listener implements DataChangedListener, ActionListener {
  276.         /**
  277.          * @inheritDoc
  278.          */
  279.         public final void dataChanged(int row, int column) {
  280.             Object value = model.getValueAt(row, column);
  281.             boolean e = model.isCellEditable(row, column);
  282.             Component cell = createCellImpl(value, row, column, e);
  283.             TableLayout t = (TableLayout)getLayout();
  284.             removeComponent(t.getComponentAt(row + 1, column));
  285.             addComponent(t.createConstraint(row + 1, column), cell);
  286.             layoutContainer();
  287.             cell.setFocusable(true);
  288.             cell.requestFocus();
  289.             revalidate();
  290.         }
  291.         public void actionPerformed(ActionEvent evt) {
  292.             TextArea t = (TextArea)evt.getSource();
  293.             int row = getCellRow(t);
  294.             int column = getCellColumn(t);
  295.             getModel().setValueAt(row, column, t.getText());
  296.         }
  297.     }
  298. }