?? tgrid.java
字號:
/*
* package com.mc.tables.client
*/
package com.mc.tables.client;
/*
* import lib
*/
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;
/*
* Class Hgrid public
* 對表格進行封裝 實現表格數據的整行操作, 從Grid 類繼承.
*/
public class TGrid extends Grid{
/**
* mflag 是否為選擇多行
* true 為多行/ false 為單行
* 默認情況為選擇多行
*
* clickmodel 選定行模式 true 為一直選中模式; false 為可以取消模式
*/
boolean mflag=false;
boolean clickmodel = true;
/*
* row 要創建的表格的行數
* thead 要創建的表格的表頭
*/
public TGrid (String [] thead){
super(1,thead.length);
int column = thead.length;
if(column>0 ){
for (int i=0; i<thead.length;i++){
this.setText(0, i, thead[i]);
}
this.numColumns = column;
this.setBorderWidth(1);
this.setCellPadding(0);
this.setCellSpacing(0);
this.addStyleName("table-Align");
this.getRowFormatter().setStyleName(0,"table-Header");
addListener();
}
}
/**
* 是否 為多行選中
* @return true/false
*/
public boolean mulLine()
{
return true;
}
/**
* 設置 為多行或者是單行選中
* @param mulline true / false
*/
public void setLine(boolean mulline)
{
mflag = mulline;
}
/*
* 添加列樣式
* col 列號
* cstyle 樣式名
*/
public void addColmnStyle(int col ,String cstyle)
{
if(col>=0 && col<= this.numColumns){
this.getColumnFormatter().addStyleName(col, cstyle);
}
}
/*
* 移除列樣式
* col 列號
* cstyle 樣式名
*/
public void removeColmnStyle(int col ,String cstyle)
{
if(col>=0 && col<= this.numColumns){
this.getColumnFormatter().removeStyleName(col, cstyle);
}
}
/*
* 添加行樣式
* row 行號
* rstyle 樣式名
*/
public void addRowStyle(int row ,String rstyle)
{
if(row>0 && row <this.numRows){
this.getRowFormatter().addStyleName(row, rstyle);
}
}
/*
* 移除行樣式
* row 行號
* rstyle 樣式名
*/
public void removeRowStyle(int row ,String rstyle)
{
if(row>0 && row <this.numRows){
this.getRowFormatter().removeStyleName(row, rstyle);
}
}
/*
* 更新表頭
* th 放置表頭的數據
*/
public void setHeadTitle(String [] th){
if(this.numColumns == th.length){
for (int i=0; i<th.length;i++){
this.setText(0, i, th[i]);
}
}
}
/*
* 獲取 表格的行數
*/
public int getRowsCount(){
return this.numRows;
}
/*
* 獲取 表格的列數
*/
public int getColumnsCount(){
return this.numColumns;
}
/*
* 向 表格插入一行新的數據
* rd 放置數據的數組
*/
public void addRowData(Object[] rd){
int preRow=this.numRows;
this.resizeRows(preRow+1);
int ln =rd.length>this.numColumns?this.numColumns:rd.length;
for (int i = 0; i<ln; i++){
this.setText(preRow, i, rd[i]+"");
}
}
/*
* 向 表格更新一行新的數據
* rd 放置數據的數組
* row 第幾行
*/
public void setRowData(int row,String [] rd){
if(row >0 && row < this.numRows){
int ln =rd.length>this.numColumns?this.numColumns:rd.length;
for (int i = 0; i<ln; i++){
this.setText(row, i, rd[i]);
}
}
}
/*
* 設置 表格中的列的寬度
*/
public void setColumnWidth(int column,String strwidth){
for(int i=0;i<this.numRows;i++){
Element td = getCellFormatter().getElement(i, column);
DOM.setAttribute(td, "width", strwidth);
}
}
/*
* 設置 表格中的行的高度
*/
public void setRowHeigh(int row,String strh){
if(row<this.numRows){
for(int i=0;i<this.numColumns;i++){
Element td = getCellFormatter().getElement(row, i);
//Element tr= getRowFormatter().getElement(row);
DOM.setAttribute(td, "height", strh);
}
}
}
/*
* 獲得 表格中的列的寬度
*/
public String getColumnWidth(int column,String attr){
String attrName="";
if(this.numRows>0)
{
Element td = getCellFormatter().getElement(0, column);
attrName = DOM.getAttribute(td, attr);
}
return attrName;
}
/*
* 添加 標題行樣式
* style 樣式名
*/
public void addHeadStyleName(String style){
this.getRowFormatter().setStyleName(0,style);
}
/*
* 獲取表格中的一行的數據
* row 第幾行
*/
public String [] getRowData(int row){
String [] result =null;
if(row >0 && row<this.numRows){
result = new String[this.numColumns];
for (int i = 0; i<this.numColumns; i++){
result[i] = this.getText(row, i);
}
}
return result;
}
/*
* 添加偵聽 以用來判斷是選中哪一行
* 或者選中的是哪一列
*/
private void addListener(){
this.addTableListener(new TableListener(){
public void onCellClicked(SourcesTableEvents arg0, int row, int column) {
if (row > 0) {
setSelectedRow(row,"table-SelectedRow");
}
}
});
}
/**
* 設置選中行
* @param row 行號
* @param rstyles 行樣式名
*/
public void setSelectedRow(int row,String rstyles){
if(row >0 && row <this.numRows){
if(mflag)
{
String rstyle = this.getRowFormatter().getStyleName(row).trim();
if(rstyle.equalsIgnoreCase(rstyles)){
this.getRowFormatter().removeStyleName(row, rstyles);
}
else
{
this.getRowFormatter().addStyleName(row, rstyles);
}
}
else
{
String rstyle ="";
String prestyle = this.getRowFormatter().getStyleName(row).trim();
for (int i=1; i <this.numRows;i++){
rstyle = this.getRowFormatter().getStyleName(i).trim();
if(rstyle.equalsIgnoreCase(rstyles)){
this.getRowFormatter().removeStyleName(i, rstyles);
}
}
if(clickmodel || prestyle.equalsIgnoreCase("")){
this.getRowFormatter().addStyleName(row, rstyles);
}
}
}
}
/**
* 獲得選中的行的個數
* @return 行的個數
*/
public int getSelectedRows()
{
int c = -1;
if(mflag){
c=0;
String rstyle ="";
for (int i=1; i <this.numRows;i++){
rstyle = this.getRowFormatter().getStyleName(i).trim();
if(rstyle.equalsIgnoreCase("table-SelectedColumn")){
c++;
}
}
}
else
{
String rstyle ="";
for (int i=1; i <this.numRows;i++){
rstyle = this.getRowFormatter().getStyleName(i).trim();
if(rstyle.equalsIgnoreCase("table-SelectedColumn")){
c= i;
}
}
}
return c ;
}
/**
* 刪除行 數據
* @param row 要刪除的行號
*/
public void deleteRowsData(int row){
if(row >0 && row <this.numRows){
this.removeRow(row);
this.numRows = this.numRows - 1;
}
}
/**
* 獲取單元格的值
* @param row 行號
* @param column 列號
* @return 單元格的值
*/
public String getCellText(int row ,int column){
String value = "";
if(row >0 && row <this.numRows && column>=0 &&column< this.numColumns){
value = this.getText(row, column);
}
return value;
}
/**
* 顯示出隱藏行數據
* @param row 行號
*/
public void showRow(int row){
if(row >0 && row <this.numRows){
this.getRowFormatter().setVisible(row,true);
}
}
/**
* 隱藏指定行號的數據
* @param row 行號
*/
public void hideRow(int row){
if(row >0 && row <this.numRows){
this.getRowFormatter().setVisible(row,false);
}
}
/**
* 設置頭的可見性
* @param hide true/false
*/
public void setHeadVisible(boolean hide){
if(this.numRows>0 ){
this.getRowFormatter().setVisible(0,false);
}
}
/**
* 隱藏指定列號的數據
* @param column 列號
*/
public void hideColumn(int column){
if(column >=0 && column <this.numColumns){
for(int i=0;i<this.numRows;i++){
this.getCellFormatter().setVisible(i, column, false);
}
}
}
/**
* 顯示指定列號的數據
* @param column 列號
*/
public void showColumn(int column){
if(column >=0 && column <this.numColumns){
for(int i=0;i<this.numRows;i++){
this.getCellFormatter().setVisible(i, column, true);
}
}
}
/**
* 設置行樣式
* @param row 行號
* @param hstyle 樣式名
*/
public void setRowStyle(int row,String hstyle)
{
if(row >0 && row <this.numRows){
this.getRowFormatter().setStyleName(row, hstyle);
}
}
/**
* 設置表頭的高度
* @param h 高度值
*/
public void setHeadHigh(String h){
if(this.numRows>0){
for(int i=0;i<this.numColumns;i++){
Element td = getCellFormatter().getElement(0, i);
//Element tr= getRowFormatter().getElement(row);
DOM.setAttribute(td, "height", h);
}
}
}
/**
* 獲得列標題
* @param column 列號
* @return 列標題
*/
public String getColumnTitle(int column){
String value= "";
if(column >=0 && column<this.numColumns){
value = this.getText(0, column);
}
return value;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -