Package com.google.gwt.topspin.ui.client

Source Code of com.google.gwt.topspin.ui.client.Table

/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.topspin.ui.client;

import com.google.gwt.dom.client.TableCellElement;
import com.google.gwt.dom.client.TableElement;
import com.google.gwt.dom.client.TableRowElement;
import com.google.gwt.dom.client.TableSectionElement;

/**
* A widget that wraps a <table> element.
*/
public class Table extends Widget {
 
  /**
   * Lazily initialized <code><thead></code> if we want one.
   */
  private TableSectionElement thead;
 
  /**
   * Lazily initialized <code><tfoot></code> if we want one.
   */
  private TableSectionElement tfoot;
 
  /**
   * Always have a <code><tbody></code>.
   */
  private final TableSectionElement tbody;

  /**
   * Creates a Table widget in the given container.
   *
   * @param container
   */
  public Table(Container container) {
    super(container.getDocument().createTableElement(), container);

    tbody = container.getDocument().createTBodyElement();
    getElement().appendChild(tbody);
  }

  /**
   * Appends a new cell to a row in the table.
   *
   * @param rowIndex the index of the row to which the new cell will be appended
   * @return the cell element that was just appended to the specified row
   */
  public TableCellElement appendCell(int rowIndex) {
    return insertCell(rowIndex, -1);
  }

  /**
   * Appends a new row to the table.
   * @return the row element appended to the <code><tbody></code>
   */
  public TableRowElement appendRow() {
    return insertRow(-1);
  }

  /**
   * Deletes an existing cell from a row in the table.
   *
   * @param rowIndex the index of the row from which the cell will be deleted
   * @param cellIndex the index of the cell to be deleted
   */
  public void deleteCell(int rowIndex, int cellIndex) {
    TableRowElement tr = tbody.getRows().getItem(rowIndex);
    tr.deleteCell(cellIndex);
  }

  /**
   * Deletes an existing row from the table.
   *
   * @param rowIndex the index of the row to be deleted
   */
  public void deleteRow(int rowIndex) {
    tbody.deleteRow(rowIndex);
  }

  /**
   * Gets the number of cells on the given row.
   *
   * @param row the row index
   * @return the cell count
   */
  public int getCellCount(int row) {
    return getTableRowElement(row).getCells().getLength();
  }

  /**
   * Gets the number of rows in the table.
   *
   * @return the row count
   */
  public int getRowCount() {
    return tbody.getRows().getLength();
  }

  /**
   * Gets the {@link TableSectionElement} corresponding to the
   * <code><tbody></code> element for this Table.
   * 
   * @return the table body element
   */
  public TableSectionElement getTableBody() {
    return tbody;
  }
 
  /**
   * Gets the {@link TableCellElement} representing the specified cell.
   *
   * @param row the cell's row index
   * @param col the cell's column index
   * @return the cell element
   */
  public TableCellElement getTableCellElement(int row, int col) {
    TableRowElement tr = getTableRowElement(row);
    return tr.getCells().getItem(col);
  }

  /**
   * Gets the {@link TableRowElement} representing the specified row.
   *
   * @param row the row's index
   * @return the {@link TableRowElement} representing the specified row.
   */
  public TableRowElement getTableRowElement(int row) {
    return tbody.getRows().getItem(row);
  }

  /**
   * Inserts a new cell into a row in the table.
   *
   * @param rowIndex the index of the row into which the new cell will be
   *          inserted
   * @param colIndex the index of the cell before which the new cell will be
   *          inserted (<code>-1</code> will append the new cell to the end)
   * @return the table cell element that was inserted at the specified row
   *          and column indices
   */
  public TableCellElement insertCell(int rowIndex, int colIndex) {
    TableRowElement tr = tbody.getRows().getItem(rowIndex);
    return tr.insertCell(colIndex);
  }

  /**
   * Inserts a new row into the table.
   *
   * @param rowIndex the index of the row before which the new row will be
   *          inserted (<code>-1</code> will append the new row to the end)
   * @return the row element that was inserted at the specified index
   */
  public TableRowElement insertRow(int rowIndex) {
    return tbody.insertRow(rowIndex);
  }

  /**
   * Sets the table's cell padding, in pixels.
   *
   * @param padding the cell padding
   */
  public void setCellPadding(int padding) {
    getTableElement().setCellPadding(padding);
  }

  /**
   * Sets the table's cell spacing, in pixels.
   *
   * @param spacing the cell spacing
   */
  public void setCellSpacing(int spacing) {
    getTableElement().setCellSpacing(spacing);
  }

  /**
   * Causes the table to use a fixed layout, as defined by the "table-layout"
   * CSS property.
   */
  public void setFixedLayout(boolean fixed) {
    getElement().getStyle().setProperty("tableLayout", fixed ? "fixed" : "");
  }

  /**
   * Sets the HTML within the given cell. This will remove any existing contents
   * within the cell.
   *
   * WARNING: Use care when setting an object's HTML; it is an easy way to
   * expose script-based security problems. Consider using
   * {@link #setText(int, int, String)} whenever possible.
   *
   * @param row the cell's row index
   * @param col the cell's column index
   * @param html the HTML to be set
   */
  public void setHtml(int row, int col, String html) {
    getTableCellElement(row, col).setInnerHTML(html);
  }

  /**
   * Sets the text within the given cell. This will remove any existing contents
   * within the cell.
   *
   * @param row the cell's row index
   * @param col the cell's column index
   * @param text the text to be set
   */
  public void setText(int row, int col, String text) {
    getTableCellElement(row, col).setInnerText(text);
  }

  /**
   * Simple getter for the <code><thead></code>.
   *
   * @return the table head element
   */
  public TableSectionElement getTableHead() {
    if (thead == null) {
      thead = getTableElement().createTHead();
    }
   
    return thead;
  }
 
  /**
   * Simple getter for the <code><tfoot></code>.
   *
   * @return the table foot element
   */
  public TableSectionElement getTableFoot() {
    if (tfoot == null) {
      tfoot = getTableElement().createTFoot();
    }
   
    return tfoot;
  }
 
  /**
   * Gets this widget's element, down-cast to the correct type.
   *
   * @return the widget's element
   */
  protected TableElement getTableElement() {
    return (TableElement) getElement();
  }
}
TOP

Related Classes of com.google.gwt.topspin.ui.client.Table

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.