📄 gridstoragecolumnhashtable.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 columns in the grid.
*
* @author Virgilio Alexandre Fornazin (mailto:virgiliofornazin@gmail.com)
*/
public final class GridStorageColumnHashtable extends GridStorageModel
{
/** Model rows count */
private int m_iRowCount;
/** Model cols count */
private int m_iColCount;
/** Cell's Hashtable */
private GridRowColHashtable m_mapCols;
/**
* Default constructor
*/
public GridStorageColumnHashtable()
{
eraseAllCells();
}
public GridCell createCell(GridCellID cellID)
{
GridCellHashtable col = m_mapCols.get(cellID.Col);
if (col == null)
{
col = new GridCellHashtable((int) m_iRowCount / 2);
m_mapCols.put(cellID.Col, col);
}
GridCell cell = col.get(cellID.Row);
if (cell == null)
{
cell = new GridCell();
col.put(cellID.Row, cell);
}
return cell;
}
public GridCell getCell(GridCellID cellID)
{
try
{
return m_mapCols.get(cellID.Col).get(cellID.Row);
}
catch (Exception e)
{
return null;
}
}
public void putCell(GridCellID cellID, GridCell cell)
{
GridCellHashtable col = m_mapCols.get(cellID.Col);
if (col == null)
{
col = new GridCellHashtable((int) m_iRowCount / 2);
m_mapCols.put(cellID.Col, col);
}
col.put(cellID.Row, cell);
}
public void eraseCell(GridCellID cellID)
{
try
{
m_mapCols.get(cellID.Col).remove(cellID.Row);
}
catch (Exception e)
{
}
}
public void eraseAllCells()
{
m_iRowCount = 0;
m_iColCount = 0;
m_mapCols = new GridRowColHashtable(8);
}
public GridCellHashtable getRowCells(int iRow)
{
GridCellHashtable gcht = new GridCellHashtable((int) (m_iColCount / 4));
GridCellHashtable col;
GridCell cell;
for (int i = 0; i < m_iColCount; i++)
{
col = m_mapCols.get(i);
if (col != null)
{
cell = col.get(iRow);
if (cell != null)
{
gcht.put(i, cell);
}
}
}
return gcht;
}
public GridCellHashtable getColCells(int iCol)
{
GridCellHashtable col = m_mapCols.get(iCol);
if (col == null)
{
return new GridCellHashtable(1);
}
int iSize = col.size();
GridCellHashtable gcht = new GridCellHashtable(iSize);
IntVector keys = col.getKeys();
int iKey;
for (int i = 0; i < iSize; i++)
{
iKey = keys.items[i];
gcht.put(iKey, col.get(iKey));
}
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_mapCols = new GridRowColHashtable(8);
}
else
{
GridCellHashtable col;
GridCell cell;
int iReadID;
int iWriteID;
for (int i = 0; i < m_iColCount; i++)
{
col = m_mapCols.get(i);
if (col != null)
{
iReadID = iRowEnd + 1;
iWriteID = iRowStart;
while (iReadID <= m_iRowCount)
{
cell = col.remove(iReadID);
if (cell != null)
{
col.put(iWriteID, cell);
}
else
{
col.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_mapCols = new GridRowColHashtable(8);
}
else
{
GridCellHashtable col;
int iReadID = iColEnd + 1;
int iWriteID = iColStart;
while (iReadID <= iColCount)
{
col = m_mapCols.remove(iReadID);
if (col != null)
{
m_mapCols.put(iWriteID, col);
}
else
{
m_mapCols.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)
{
GridCellHashtable col;
for (int c = 0; c < m_iColCount; c++)
{
col = m_mapCols.get(c);
if (col != null)
{
for (int r = iRowStart; r < iRowEnd; r++)
{
col.remove(r);
}
}
}
}
private void _removePendingCols(int iColStart, int iColEnd)
{
for (int c = iColStart; c < iColEnd; c++)
{
m_mapCols.remove(c);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -