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

📄 tgrid.java

📁 gwt 开发 界面控件的封装
💻 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 + -