📄 grid.java
字号:
/*
* WExtLib - A Component 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 wextlib.util.*;
import waba.util.*;
import waba.sys.*;
import waba.ui.*;
import waba.fx.*;
/**
* This class implements a grid control with support for fonts, colors,
* editing, text alignment, etc. See javadocs for details.
*
* @author Virgilio Alexandre Fornazin (mailto:virgiliofornazin@gmail.com)
*/
public class Grid extends Container
{
/** Cell storage model */
private GridStorageModel m_Storage;
/** Number of rows in the grid */
private int m_iRowCount;
/** Number of columns in the grid */
private int m_iColCount;
/** Number of fixed rows in the grid */
private int m_iFixedRowCount;
/** Number of fixed columns in the grid */
private int m_iFixedColCount;
/** Grid磗 horizontal scrollbar */
private ScrollBar m_HorzScroll;
/** Grid磗 vertical scrollbar */
private ScrollBar m_VertScroll;
/** Grid磗 vertical scrollbar live scrolling */
private boolean m_bLiveScrollingHorizontal;
/** Grid磗 horizontal scrollbar live scrolling */
private boolean m_bLiveScrollingVertical;
/** Height of the vertical scroll bar */
private int m_iHorizontalScrollHeight;
/** Width of the vertical scroll bar */
private int m_iVerticalScrollWidth;
/** Indicator for updating grid display on operations */
private boolean m_bUpdating;
/** Indicator for displaying full row selection */
private boolean m_bFullRowSelectionDisplay;
/** Indicator for highlight selected cell in full row selection mode */
private boolean m_bHighlightSelectedCell;
/** Grid border color */
private Color m_clrGridBorderColor;
/** Grid background color */
private Color m_clrGridBackColor;
/** Grid line separators color */
private Color m_clrGridCellSeparatorColor;
/** Indicator for drawing cell row separators */
private boolean m_bDrawRowSeparators;
/** Indicator for drawing cell column separators */
private boolean m_bDrawColSeparators;
/** Size of the row separators */
private int m_iRowSeparatorHeight;
/** Size of the column separators */
private int m_iColSeparatorWidth;
/** Default row height (if not specified by setRowHeight() call) */
private int m_iRowHeightDefault;
/** Default column width (if not specified by setColumnWidth() call) */
private int m_iColWidthDefault;
/** Hashtable containing the user configured row heights */
private IntHashtable m_mapRowHeights;
/** Hashtable containing the user configured column widths */
private IntHashtable m_mapColWidths;
/** Hashtable containing the user configured row attributes. Note that row attributes take priority over column attributes */
private GridCellAttributesHashtable m_mapRowAttributes;
/** Hashtable containing the user configured column attributes. Note that row attributes take priority over column attributes */
private GridCellAttributesHashtable m_mapColAttributes;
/** Hashtable of default cell types for columns */
private IntClassHashtable m_mapColCellTypes;
/** Default grid cell (used for drawing) */
private GridCell m_cellAttributesDefault;
/** Default fixed grid cell (used for drawing) */
private GridCell m_cellAttributesFixed;
/** Selected grid cell (used for drawing) */
private GridCell m_cellAttributesSelected;
/** Number of the top row displayed in the grid */
private int m_iTopRow;
/** Number of the left column displayed in the grid */
private int m_iLeftCol;
/** Number of the bottom row displayed in the grid */
private int m_iBottomRow;
/** Number of the right column displayed in the grid */
private int m_iRightCol;
/** Y position of the top row */
private int m_iTopRowY;
/** X position of the left column */
private int m_iLeftColX;
/** Indicate if the last visible row was fully Draw on the screen */
private boolean m_bBottomRowFullDraw;
/** Indicate if the last visible column was fully Draw on the screen */
private boolean m_bRightColFullDraw;
/** Current cell being edited */
private GridCell m_cellEditing;
/** GridCellID of the selected row in the grid */
private GridCellID m_userSelectedCellID;
/** Last time when used clicked on a cell (used for editing cell磗) */
private Time m_lastTimeClicked;
/**
* Default constructor
*/
public Grid()
{
m_Storage = new GridStorageSingleHashtable();
_initGrid();
}
/**
* Storage constructor
*/
public Grid(GridStorageModel model)
{
m_Storage = model;
_initGrid();
}
private void _initGrid()
{
m_bUpdating = false;
clearGrid();
m_bUpdating = true;
}
/**
* Reset the grid contents
*/
public void clearGrid()
{
boolean bUpdating = m_bUpdating;
m_bUpdating = false;
m_Storage.eraseAllCells();
m_iRowCount = 0;
m_iColCount = 0;
m_iFixedRowCount = 0;
m_iFixedColCount = 0;
if (m_HorzScroll != null)
{
remove(m_HorzScroll);
}
m_HorzScroll = new ScrollBar(ScrollBar.HORIZONTAL);
m_HorzScroll.setVisibleItems(1);
add(m_HorzScroll);
if (m_VertScroll != null)
{
remove(m_VertScroll);
}
m_VertScroll = new ScrollBar(ScrollBar.VERTICAL);
m_VertScroll.setVisibleItems(1);
add(m_VertScroll);
setHorizontalScrollVisible(true);
setVerticalScrollVisible(true);
setHorizontalScrollLiveScrolling(false);
setVerticalScrollLiveScrolling(false);
m_iHorizontalScrollHeight = fm.height;
m_iVerticalScrollWidth = fm.height;
m_bFullRowSelectionDisplay = false;
m_bHighlightSelectedCell = true;
if (Settings.uiStyle == Settings.PalmOS)
{
if (Settings.isColor)
{
m_clrGridCellSeparatorColor = new Color(0, 0, 0);
m_clrGridBackColor = new Color(128, 128, 128);
m_clrGridBorderColor = new Color(0, 0, 0);
}
else
{
m_clrGridCellSeparatorColor = new Color(0, 0, 0);
m_clrGridBackColor = new Color(128, 128, 128);
m_clrGridBorderColor = new Color(0, 0, 0);
}
}
else // if (Settings.uiStyle == Settings.WinCE)
{
if (Settings.isColor)
{
m_clrGridCellSeparatorColor = new Color(64, 64, 64);
m_clrGridBackColor = new Color(128, 128, 128);
m_clrGridBorderColor = new Color(0, 0, 0);
}
else
{
m_clrGridCellSeparatorColor = new Color(0, 0, 0);
m_clrGridBackColor = new Color(128, 128, 128);
m_clrGridBorderColor = new Color(0, 0, 0);
}
}
m_bDrawRowSeparators = true;
m_bDrawColSeparators = true;
m_iRowSeparatorHeight = 1;
m_iColSeparatorWidth = 1;
m_iRowHeightDefault = fm.height + 2;
m_iColWidthDefault = (fm.height * 3) + 2;
m_mapRowHeights = new IntHashtable(4);
m_mapColWidths = new IntHashtable(8);
m_mapRowAttributes = new GridCellAttributesHashtable(4);
m_mapColAttributes = new GridCellAttributesHashtable(8);
m_mapColCellTypes = new IntClassHashtable(8);
m_cellAttributesDefault = new GridCell();
m_cellAttributesFixed = new GridCell();
m_cellAttributesSelected = new GridCell();
m_cellAttributesDefault.setCellAlignment(GridCellAttributes.HORIZONTAL_ALIGN_LEFT | GridCellAttributes.VERTICAL_ALIGN_CENTER);
m_cellAttributesDefault.setCellFont(getFont());
m_cellAttributesFixed.setCellAlignment(GridCellAttributes.HORIZONTAL_ALIGN_LEFT | GridCellAttributes.VERTICAL_ALIGN_CENTER);
m_cellAttributesFixed.setCellFont(getFont());
if (Settings.uiStyle == Settings.PalmOS)
{
if (Settings.isColor)
{
m_cellAttributesDefault.setCellBackColor(new Color(255, 255, 255));
m_cellAttributesDefault.setCellForeColor(new Color(0, 0, 0));
m_cellAttributesFixed.setCellBackColor(new Color(192, 192, 192));
m_cellAttributesFixed.setCellForeColor(new Color(0, 0, 0));
m_cellAttributesSelected.setCellBackColor(new Color(128, 128, 255));
m_cellAttributesSelected.setCellForeColor(new Color(255, 255, 255));
}
else
{
m_cellAttributesDefault.setCellBackColor(new Color(255, 255, 255));
m_cellAttributesDefault.setCellForeColor(new Color(0, 0, 0));
m_cellAttributesFixed.setCellBackColor(new Color(0, 0, 0));
m_cellAttributesFixed.setCellForeColor(new Color(255, 255, 255));
m_cellAttributesSelected.setCellBackColor(new Color(128, 128, 128));
m_cellAttributesSelected.setCellForeColor(new Color(255, 255, 255));
}
}
else // if (Settings.uiStyle == Settings.WinCE)
{
if (Settings.isColor)
{
m_cellAttributesDefault.setCellBackColor(new Color(255, 255, 255));
m_cellAttributesDefault.setCellForeColor(new Color(0, 0, 0));
m_cellAttributesFixed.setCellBackColor(new Color(192, 192, 192));
m_cellAttributesFixed.setCellForeColor(new Color(0, 0, 0));
m_cellAttributesSelected.setCellBackColor(new Color(0, 0, 255));
m_cellAttributesSelected.setCellForeColor(new Color(255, 255, 255));
}
else
{
m_cellAttributesDefault.setCellBackColor(new Color(255, 255, 255));
m_cellAttributesDefault.setCellForeColor(new Color(0, 0, 0));
m_cellAttributesFixed.setCellBackColor(new Color(0, 0, 0));
m_cellAttributesFixed.setCellForeColor(new Color(255, 255, 255));
m_cellAttributesSelected.setCellBackColor(new Color(128, 128, 128));
m_cellAttributesSelected.setCellForeColor(new Color(255, 255, 255));
}
}
m_iTopRow = 0;
m_iLeftCol = 0;
m_iBottomRow = 0;
m_iRightCol = 0;
m_iTopRowY = 0;
m_iLeftColX = 0;
m_bBottomRowFullDraw = true;
m_bRightColFullDraw = true;
m_cellEditing = null;
m_userSelectedCellID = new GridCellID();
m_lastTimeClicked = null;
m_bUpdating = bUpdating;
_repaintGridIfNeeded();
}
private void _recreateColumnCells(int iCol, Class cellType)
{
GridCellID cellID = new GridCellID(-1, iCol);
GridCell cellOld;
for (cellID.Row = m_iFixedRowCount; cellID.Row < m_iRowCount; cellID.Row++)
{
cellOld = m_Storage.getCell(cellID);
if (cellOld == null)
{
try
{
m_Storage.putCell(cellID, (GridCell) cellType.newInstance());
}
catch (Exception e)
{
}
}
else if (cellType.getName().compareTo(cellOld.getClass().getName()) != 0)
{
try
{
GridCell cellNew = (GridCell) cellType.newInstance();
cellNew.copyContents(cellOld);
m_Storage.putCell(cellID, cellNew);
}
catch (Exception e)
{
}
}
}
}
/**
* Get the grid row count
*/
public int getRowCount()
{
return m_iRowCount;
}
/**
* Set the grid row count
*/
public void setRowCount(int iRowCount)
{
if (iRowCount < 0)
{
return;
}
boolean bMustCreateCells = (iRowCount > m_iRowCount);
if (iRowCount < m_iRowCount)
{
for (int i = iRowCount; i < m_iRowCount; i++)
{
m_mapRowAttributes.remove(i);
m_mapRowHeights.remove(i);
}
}
if (m_iTopRow > (iRowCount - 1))
{
m_iTopRow = iRowCount - 1;
}
if (m_iFixedRowCount > iRowCount)
{
m_iFixedRowCount = iRowCount;
}
if (m_userSelectedCellID.Row > (iRowCount - 1))
{
m_userSelectedCellID.Row = -1;
m_userSelectedCellID.Col = -1;
}
m_iRowCount = iRowCount;
if ((m_iRowCount > 0) && (m_iTopRow < m_iFixedRowCount))
{
m_iTopRow = m_iFixedRowCount;
}
m_Storage.setRowCount(m_iRowCount);
if (bMustCreateCells)
{
IntVector keys;
int iCount;
int iCol;
keys = m_mapColCellTypes.getKeys();
iCount = m_mapColCellTypes.size();
for (int c = 0; c < iCount; c++)
{
iCol = keys.items[c];
_recreateColumnCells(iCol, m_mapColCellTypes.get(iCol));
}
}
m_VertScroll.setMinimum(0);
m_VertScroll.setMaximum(m_iRowCount - m_iFixedRowCount);
m_VertScroll.setValue(m_iTopRow);
m_VertScroll.setEnabled((m_iRowCount > m_iFixedRowCount));
_repaintGridIfNeeded();
}
/**
* Get the grid column count
*/
public int getColCount()
{
return m_iColCount;
}
/**
* Set the grid column count
*/
public void setColCount(int iColCount)
{
if (iColCount < 0)
{
return;
}
if (iColCount < m_iColCount)
{
for (int i = iColCount; i < m_iColCount; i++)
{
m_mapColCellTypes.remove(i);
m_mapColAttributes.remove(i);
m_mapColWidths.remove(i);
}
}
if (m_iLeftCol > (iColCount - 1))
{
m_iLeftCol = iColCount - 1;
}
if (m_iFixedColCount > iColCount)
{
m_iFixedColCount = iColCount;
}
if (m_userSelectedCellID.Col > (iColCount - 1))
{
m_userSelectedCellID.Row = -1;
m_userSelectedCellID.Col = -1;
}
m_iColCount = iColCount;
if ((m_iColCount > 0) && (m_iLeftCol < m_iFixedColCount))
{
m_iLeftCol = m_iFixedColCount;
}
m_Storage.setColCount(m_iColCount);
m_HorzScroll.setMinimum(0);
m_HorzScroll.setMaximum(m_iColCount - m_iFixedColCount);
m_HorzScroll.setValue(m_iLeftCol);
m_HorzScroll.setEnabled((m_iColCount > m_iFixedColCount));
_repaintGridIfNeeded();
}
/**
* Get the fixed row count
*/
public int getFixedRowCount()
{
return m_iFixedRowCount;
}
/**
* Set the fixed row count
*/
public void setFixedRowCount(int iFixedRowCount)
{
if (iFixedRowCount > m_iRowCount)
{
return;
}
if (m_iTopRow < iFixedRowCount)
{
m_iTopRow = iFixedRowCount;
}
m_iFixedRowCount = iFixedRowCount;
m_VertScroll.setMinimum(0);
m_VertScroll.setMaximum(m_iRowCount - m_iFixedRowCount);
m_VertScroll.setValue(m_iTopRow - iFixedRowCount);
m_VertScroll.setEnabled((m_iRowCount > m_iFixedRowCount));
_repaintGridIfNeeded();
}
/**
* Get the fixed column count
*/
public int getFixedColCount()
{
return m_iFixedColCount;
}
/**
* Set the fixed column count
*/
public void setFixedColCount(int iFixedColCount)
{
if (iFixedColCount > m_iColCount)
{
return;
}
if (m_iLeftCol < iFixedColCount)
{
m_iLeftCol = iFixedColCount;
}
m_iFixedColCount = iFixedColCount;
m_HorzScroll.setMinimum(0);
m_HorzScroll.setMaximum(m_iColCount - m_iFixedColCount);
m_HorzScroll.setValue(m_iLeftCol - m_iFixedColCount);
m_HorzScroll.setEnabled((m_iColCount > m_iFixedColCount));
_repaintGridIfNeeded();
}
/**
* Get the horizontal scrollbar visibility
*/
public boolean getHorizontalScrollVisible()
{
return m_HorzScroll.isVisible();
}
/**
* Set the horizontal scrollbar visibility
*/
public void setHorizontalScrollVisible(boolean bVisible)
{
m_HorzScroll.setVisible(bVisible);
_repaintGridIfNeeded();
}
/**
* Get the vertical scrollbar visibility
*/
public boolean getVerticalScrollVisible()
{
return m_VertScroll.isVisible();
}
/**
* Set the vertical scrollbar visibility
*/
public void setVerticalScrollVisible(boolean bVisible)
{
m_VertScroll.setVisible(bVisible);
_repaintGridIfNeeded();
}
/**
* Get the horizontal scrollbar live scrolling
*/
public boolean getHorizontalScrollLiveScrolling()
{
return m_bLiveScrollingHorizontal;
}
/**
* Set the horizontal scrollbar live scrolling
*/
public void setHorizontalScrollLiveScrolling(boolean bLiveScrolling)
{
m_bLiveScrollingHorizontal = bLiveScrolling;
m_HorzScroll.setLiveScrolling(m_bLiveScrollingHorizontal);
_repaintGridIfNeeded();
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -