📄 gridcell.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.fx.*;
import waba.ui.*;
/**
* This class implements a default Grid Cell without editing support
*
* @author Virgilio Alexandre Fornazin (mailto:virgiliofornazin@gmail.com)
*/
public class GridCell extends GridCellAttributes
{
/** Cell text */
protected String m_strCellText;
/** Editing Grid */
protected Grid m_EditGrid;
/** Editing Control */
protected Control m_EditControl;
/** Editing CellID */
protected GridCellID m_EditCellID;
/** Editing Display Rect */
protected Rect m_EditRect;
/** Editing Foreground Color */
protected Color m_EditForeColor;
/** Editing Background Color */
protected Color m_EditBackColor;
/** Editing Font */
protected Font m_EditFont;
/**
* Default constructor
*/
public GridCell()
{
m_strCellText = "";
}
/**
* Get the cell text
*/
public String getCellText()
{
return m_strCellText;
}
/**
* Set the cell text
*/
public void setCellText(String strCellText)
{
m_strCellText = strCellText;
}
/**
* Clear the cell text
*/
public void clearCellText()
{
m_strCellText = "";
}
/**
* Copy contents from another instance
*/
public void copyContents(GridCell cell)
{
copyAttributes(cell);
m_strCellText = cell.m_strCellText;
}
/**
* This method tells grid that the cell wil support editing.
* Override this method returning TRUE to allow grid to invoke
* onStartEdit()
*/
public boolean supportEditing()
{
return false;
}
/**
* This event occurs when the grid request a cell for editing. You must
* return FALSE if you don磘 want to edit the cell contents. Override this
* method for handling start of editing.
*/
public boolean onStartEdit(Grid grid, GridCellID cellID, Rect cellRect, Color clrForeColor, Color clrBackColor, Font cellFont, int chrCode)
{
m_EditGrid = grid;
m_EditCellID = cellID;
m_EditRect = cellRect;
m_EditForeColor = clrForeColor;
m_EditBackColor = clrBackColor;
m_EditFont = cellFont;
// Code to prevent warnings
chrCode++;
chrCode--;
return false;
}
/**
* This event will be called by the cell editor when the editor loses input
* focus or the user finished entering cell data. Override this method for
* handling end of editing. You must return TRUE for notify user that editing
* was finished.
*/
public boolean onEndEdit()
{
return false;
}
/**
* Draw the cell background
*/
protected void drawCellBackground(GridCellDrawParams params)
{
params.graphics.setBackColor(params.clrCellBackColor);
params.graphics.fillRect(params.rcClip.x, params.rcClip.y,
params.rcClip.width - (params.bDrawHorizontalLine ? params.iColSeparatorWidth : 0),
params.rcClip.height - (params.bDrawVerticalLine ? params.iRowSeparatorHeight : 0));
}
/**
* Draw the cell borders
*/
protected void drawCellBorders(GridCellDrawParams params)
{
if (params.bDrawHorizontalLine || params.bDrawVerticalLine)
{
if (params.bDrawHorizontalLine)
{
if (params.iRowSeparatorHeight == 1)
{
params.graphics.setForeColor(params.clrGridCellSeparatorColor);
params.graphics.drawLine(
params.rcClip.x,
(params.rcClip.y + params.rcClip.height) - 1,
(params.rcClip.x + params.rcClip.width) - 1,
(params.rcClip.y + params.rcClip.height) - 1);
}
else
{
params.graphics.setBackColor(params.clrGridCellSeparatorColor);
params.graphics.fillRect(
params.rcClip.x,
(params.rcClip.y + params.rcClip.height) - params.iRowSeparatorHeight,
params.rcClip.width,
params.iRowSeparatorHeight);
}
}
if (params.bDrawVerticalLine)
{
if (params.iColSeparatorWidth == 1)
{
params.graphics.setForeColor(params.clrGridCellSeparatorColor);
params.graphics.drawLine(
(params.rcClip.x + params.rcClip.width) - 1,
params.rcClip.y,
(params.rcClip.x + params.rcClip.width) - 1,
(params.rcClip.y + params.rcClip.height) - 1);
}
else
{
params.graphics.setBackColor(params.clrGridCellSeparatorColor);
params.graphics.fillRect(
(params.rcClip.x + params.rcClip.width) - params.iColSeparatorWidth,
params.rcClip.y,
params.iColSeparatorWidth,
params.rcClip.height);
}
}
}
}
/**
* Draw the cell text
*/
protected void drawCellText(GridCellDrawParams params)
{
if (m_strCellText != null)
{
if ((m_strCellText.length() > 0) && (params.rcClip.width > 3) && (params.rcClip.height > 2))
{
Rect rcNewClip = new Rect();
Rect rcOldClip = new Rect();
rcNewClip.x = params.rcClip.x + 1;
rcNewClip.y = params.rcClip.y;
rcNewClip.width = params.rcClip.width - (params.iColSeparatorWidth + 2);
rcNewClip.height = params.rcClip.height - params.iRowSeparatorHeight;
int hAlign = (GridCellAttributes.HORIZONTAL_ALIGN_MASK & params.iCellAlignment);
int vAlign = (GridCellAttributes.VERTICAL_ALIGN_MASK & params.iCellAlignment);
int iTextX;
int iTextY;
switch (hAlign)
{
case GridCellAttributes.HORIZONTAL_ALIGN_LEFT:
{
iTextX = rcNewClip.x;
break;
}
case GridCellAttributes.HORIZONTAL_ALIGN_RIGHT:
{
iTextX = rcNewClip.x + (params.iCellWidth - params.fm.getTextWidth(m_strCellText)) - 2;
break;
}
case GridCellAttributes.HORIZONTAL_ALIGN_CENTER:
{
iTextX = rcNewClip.x + ((params.iCellWidth / 2) - (params.fm.getTextWidth(m_strCellText) / 2));
break;
}
default:
{
iTextX = 0;
break;
}
}
switch (vAlign)
{
case GridCellAttributes.VERTICAL_ALIGN_TOP:
{
iTextY = rcNewClip.y;
break;
}
case GridCellAttributes.VERTICAL_ALIGN_BOTTOM:
{
iTextY = rcNewClip.y + (params.iCellHeight - params.font.getSize());
break;
}
case GridCellAttributes.VERTICAL_ALIGN_CENTER:
{
iTextY = rcNewClip.y + ((params.iCellHeight / 2) - (params.font.getSize() / 2));
break;
}
default:
{
iTextY = 0;
break;
}
}
params.graphics.getClip(rcOldClip);
params.graphics.setClip(rcNewClip);
params.graphics.setFont(params.font);
params.graphics.setForeColor(params.clrCellForeColor);
params.graphics.setBackColor(params.clrCellBackColor);
params.graphics.drawText(m_strCellText, iTextX, iTextY);
params.graphics.setClip(rcOldClip);
}
}
}
/**
* This events draw the cell contents on the Grid Graphics surface. Override
* this to paint your own cell
*/
public void onPaint(GridCellDrawParams params)
{
if ((params.rcClip.width < 1) || (params.rcClip.height < 1))
{
return;
}
drawCellBackground(params);
drawCellBorders(params);
drawCellText(params);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -