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

📄 gridstoragerowhashtable.java

📁 PDBView是一个可以查看palmOS执行包*.pdb(类似于java包*.jar)结构的小工具
💻 JAVA
字号:
/* 
 * WExtLib - A class library for the Superwaba Virtual Machine
 * Copyright (C) 2005, Virgilio Alexandre Fornazin
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package wextlib.ui.grid;

import waba.util.*;

/**
 * This class implements an row hashtable-based storage model for the grid control.
 * Use this storage when you plan to add or remove rows in the grid.
 * 
 * @author Virgilio Alexandre Fornazin (mailto:virgiliofornazin@gmail.com)
 */
public final class GridStorageRowHashtable extends GridStorageModel 
{
	/** Model rows count */
	private int m_iRowCount;
	/** Model cols count */
	private int m_iColCount;
	/** Cell's Hashtable */
	private GridRowColHashtable m_mapRows;
	
	/**
	 * Default constructor
	 */
	public GridStorageRowHashtable()
	{
		eraseAllCells();
	}
	
	public GridCell createCell(GridCellID cellID) 
	{
		GridCellHashtable row = m_mapRows.get(cellID.Row);
		
		if (row == null)
		{
			row = new GridCellHashtable((int) m_iColCount / 2);
			
			m_mapRows.put(cellID.Row, row);
		}
		
		GridCell cell = row.get(cellID.Col);
		
		if (cell == null)
		{
			cell = new GridCell();
			
			row.put(cellID.Col, cell);
		}
		
		return cell;
	}

	public GridCell getCell(GridCellID cellID) 
	{
		try
		{
			return m_mapRows.get(cellID.Row).get(cellID.Col);
		}
		catch (Exception e)
		{
			return null;
		}
	}

	public void putCell(GridCellID cellID, GridCell cell) 
	{
		GridCellHashtable row = m_mapRows.get(cellID.Row);
		
		if (row == null)
		{
			row = new GridCellHashtable((int) m_iColCount / 2);
			
			m_mapRows.put(cellID.Row, row);
		}
		
		row.put(cellID.Col, cell);
	}

	public void eraseCell(GridCellID cellID) 
	{
		try
		{
			m_mapRows.get(cellID.Row).remove(cellID.Col);
		}
		catch (Exception e)
		{
		}
	}

	public void eraseAllCells()
	{
		m_iRowCount = 0;
		m_iColCount = 0;
		
		m_mapRows = new GridRowColHashtable(8);
	}

	public GridCellHashtable getRowCells(int iRow)
	{
		GridCellHashtable row = m_mapRows.get(iRow);
		
		if (row == null)
		{
			return new GridCellHashtable(1);
		}
		
		int iSize = row.size();
		GridCellHashtable gcht = new GridCellHashtable(iSize);
		IntVector keys = row.getKeys();
		int iKey;
		
		for (int i = 0; i < iSize; i++)
		{
			iKey = keys.items[i];			
			gcht.put(iKey, row.get(iKey));
		}
		
		return gcht;
	}
	
	public GridCellHashtable getColCells(int iCol)
	{
		GridCellHashtable gcht = new GridCellHashtable((int) (m_iRowCount / 4));
		GridCellHashtable row;
		GridCell cell;
		
		for (int i = 0; i < m_iRowCount; i++)
		{
			row = m_mapRows.get(i);
			
			if (row != null)
			{
				cell = row.get(iCol);
				
				if (cell != null)
				{
					gcht.put(i, cell);
				}
			}
		}
		
		return gcht;
	}
	
	public void setRowCount(int iRowCount) 
	{
		if (m_iRowCount == iRowCount)
		{
			return;
		}
		
		if (iRowCount < m_iRowCount)
		{
			_removePendingRows(iRowCount, m_iRowCount);
		}

		m_iRowCount = iRowCount;
	}
	
	public int getRowCount()
	{
		return m_iRowCount;
	}
	
	public int removeRows(int iRowStart, int iRowEnd)
	{
		if (iRowEnd == m_iRowCount - 1)
		{
			setRowCount(iRowStart);
			
			return m_iRowCount;
		}
		
		int iRowCount = m_iRowCount - ((iRowEnd - iRowStart) + 1);
		
		if ((iRowCount < 1) || (m_iColCount == 0))
		{
			iRowCount = 0;
			
			m_mapRows = new GridRowColHashtable(8);
		}
		else
		{
			GridCellHashtable row;

			int iReadID = iRowEnd + 1;
			int iWriteID = iRowStart;
			
			while (iReadID <= iRowCount)
			{
				row = m_mapRows.remove(iReadID);
				
				if (row != null)
				{
					m_mapRows.put(iWriteID, row);
				}
				else
				{
					m_mapRows.remove(iWriteID);
				}
				
				iReadID++;
				iWriteID++;
			}
		}
		
		if (iRowCount < m_iRowCount)
		{
			_removePendingRows(iRowCount, m_iRowCount);
		}
		
		m_iRowCount = iRowCount;
		
		return m_iRowCount;
	}

	public void setColCount(int iColCount) 
	{
		if (m_iColCount == iColCount)
		{
			return;
		}
		
		if (iColCount < m_iColCount)
		{
			_removePendingCols(iColCount, m_iColCount);
		}
	
		m_iColCount = iColCount;
	}

	public int getColCount()
	{
		return m_iColCount;
	}
	
	public int removeCols(int iColStart, int iColEnd)
	{
		if (iColEnd == m_iColCount - 1)
		{
			setColCount(iColStart);
			
			return m_iColCount;
		}
		
		int iColCount = m_iColCount - ((iColEnd - iColStart) + 1);
		
		if ((iColCount < 1) || (m_iRowCount == 0))
		{
			iColCount = 0;
			
			m_mapRows = new GridRowColHashtable(8);
		}
		else
		{
			GridCellHashtable row;
			GridCell cell;
			
			int iReadID;
			int iWriteID;
			
			for (int i = 0; i < m_iRowCount; i++)
			{
				row = m_mapRows.get(i);
				
				if (row != null)
				{
					iReadID = iColEnd + 1;
					iWriteID = iColStart;
					
					while (iReadID <= m_iColCount)
					{
						cell = row.remove(iReadID);
						
						if (cell != null)
						{
							row.put(iWriteID, cell);
						}
						else
						{
							row.remove(iWriteID);
						}
						
						iReadID++;
						iWriteID++;
					}
				}
			}
		}
	
		if (iColCount < m_iColCount)
		{
			_removePendingCols(iColCount, m_iColCount);
		}
		
		m_iColCount = iColCount;
		
		return m_iColCount;
	}
	
	private void _removePendingRows(int iRowStart, int iRowEnd)
	{
		for (int r = iRowStart; r < iRowEnd; r++)
		{
			m_mapRows.remove(r);
		}
	}
	
	private void _removePendingCols(int iColStart, int iColEnd)
	{
		GridCellHashtable row;
		
		for (int r = 0; r < m_iRowCount; r++)
		{
			row = m_mapRows.get(r);
			
			if (row != null)
			{
				for (int c = iColStart; c < iColEnd; c++)
				{
					row.remove(c);
				}
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -