📄 ktable.java
字号:
y = getFixedHeight();
if (row<1) return 1;
for (int i=m_TopRow; i<row; i++)
y+=m_Model.getRowHeight(i);
}
return y;
}
private Rectangle getCellRect(int col, int row, int left_X) {
Point valid = getValidCell(col, row);
Rectangle bound = getCellRectIgnoreSpan(col, row, left_X);
// determine the cells that are contained:
int spanRow = 0, spanCol = 0;
// move over columns and add width:
while (getValidCell(col+spanCol+1, row).equals(valid) && col+spanCol+1<m_Model.getColumnCount()) {
int width = getColumnWidth(col+spanCol+1);
bound.width+=width;
spanCol++;
}
while (getValidCell(col, row+spanRow+1).equals(valid)) {
bound.height+=m_Model.getRowHeight(row+spanRow+1);
spanRow++;
}
return bound;
}
/**
* Returns the area that is occupied by the given cell.
* Respects cell span.
* @param col the column index
* @param row the row index
* @return returns the area the cell content is drawn at.
* @throws IllegalArgumentException if the given cell is not a cell
* that is visible, but overlapped by a spanning cell. Call getValidCell()
* first to ensure it is a visible cell.
*/
public Rectangle getCellRect(int col, int row) {
checkWidget();
// be sure that it is a valid cell that is not overlapped by some other:
Point valid = getValidCell(col, row);
if(valid.x!=col || row!=valid.y)
return new Rectangle(0,0,0,0);
//throw new IllegalArgumentException("The cell bounds of an overlapped, " +
// "non-visible cell were requested: "+col+", "+row);
Rectangle bound = getCellRectIgnoreSpan(col, row);
// determine the cells that are contained:
int spanRow = 0, spanCol = 0;
// move over columns and add width:
while (getValidCell(col+spanCol+1, row).equals(valid) && col+spanCol+1<m_Model.getColumnCount()) {
int width = getColumnWidth(col+spanCol+1);
bound.width+=width;
spanCol++;
}
while (getValidCell(col, row+spanRow+1).equals(valid)) {
bound.height+=m_Model.getRowHeight(row+spanRow+1);
spanRow++;
}
return bound;
}
protected boolean canDrawCell(Rectangle r, Rectangle clipRect) {
if (r.height==0 || r.width==0)
return false;
if (r.y + r.height < clipRect.y)
return false;
if (r.y > clipRect.y + clipRect.height)
return false;
if (r.x + r.width < clipRect.x)
return false;
if (r.x > clipRect.x + clipRect.width)
return false;
return true;
}
//////////////////////////////////////////////////////////////////////////////
// PAINTING & MORE
//////////////////////////////////////////////////////////////////////////////
// Paint-Result
protected void onPaint(PaintEvent event) {
Rectangle rect = getClientArea();
GC gc = event.gc;
doCalculations();
if (m_Model != null) {
// be sure that clipping cuts oft all the unneccessary part of a spanned cell:
Rectangle oldClipping = setContentAreaClipping(gc);
drawCells(gc, oldClipping, m_LeftColumn, m_Model
.getColumnCount(), m_TopRow, m_TopRow + m_RowsVisible);
// content has been drawn, so set back clipping:
setTopAreaClipping(gc, oldClipping);
drawCells(gc, gc.getClipping(), 0, getFixedColumnCount(),
m_TopRow, m_TopRow + m_RowsVisible);
setLeftAreaClipping(gc, oldClipping);
drawCells(gc, gc.getClipping(), m_LeftColumn, m_Model
.getColumnCount(), 0, getFixedRowCount());
gc.setClipping(oldClipping);
drawCells(gc, gc.getClipping() , 0, getFixedColumnCount(),
0, getFixedRowCount());
drawBottomSpace(gc);
} else {
gc.fillRectangle(rect);
}
}
private void setTopAreaClipping(GC gc, Rectangle oldClipping) {
Rectangle contentClip = getClientArea();
contentClip.x=1;
contentClip.y=1;
contentClip.width-=1;
contentClip.height-=1;
for (int i=0; i<getFixedRowCount(); i++) {
int height = getCellRectIgnoreSpan(0, i).height;
contentClip.y+=height+1;
contentClip.height-=height+1;
}
contentClip.intersect(oldClipping);
gc.setClipping(contentClip);
}
private void setLeftAreaClipping(GC gc, Rectangle oldClipping) {
Rectangle contentClip = getClientArea();
contentClip.x=1;
contentClip.y=1;
contentClip.width-=1;
contentClip.height-=1;
for (int i=0; i<getFixedColumnCount(); i++){
int width = getCellRectIgnoreSpan(i, 0).width;
contentClip.x+=width+1;
contentClip.width-=width+1;
}
contentClip.intersect(oldClipping);
gc.setClipping(contentClip);
}
/**
* This sets the clipping area of the GC to the content
* area of the table, ignoring all header cells. This
* has the result that fixed cells are not drawn by this gc!
* @param gc The gc to manipulate.
* @return The old clipping area. It you want to paint fixed cells
* with this GC, re-set this with gc.setClipping();
*/
private Rectangle setContentAreaClipping(GC gc) {
Rectangle oldClipping = gc.getClipping();
Rectangle contentClip = getClientArea();
contentClip.x=1;
contentClip.y=1;
contentClip.width-=1;
contentClip.height-=1;
for (int i=0; i<getFixedColumnCount(); i++){
int width = getCellRectIgnoreSpan(i, 0).width;
contentClip.x+=width+1;
contentClip.width-=width+1;
}
for (int i=0; i<getFixedRowCount(); i++) {
int height = getCellRectIgnoreSpan(0, i).height;
contentClip.y+=height+1;
contentClip.height-=height+1;
}
contentClip.intersect(oldClipping);
gc.setClipping(contentClip);
return oldClipping;
}
// Bottom-Space
protected void drawBottomSpace(GC gc) {
Rectangle r = getClientArea();
if (m_Model.getRowCount() > 0) {
r.y = getFixedHeight();
for (int i=0; i<m_RowsVisible; i++) {
r.y+= m_Model.getRowHeight(i+m_TopRow);
}
}
int lastColRight = getColumnRight(
Math.min(m_LeftColumn+m_ColumnsVisible, m_Model.getColumnCount()-1));
// implement the behavior that we fill the remaining space with an additional empty column.
if ((getStyle() & SWTX.FILL_WITH_DUMMYCOL)!=0) {
lastColRight--;
int lastCol = m_Model.getColumnCount()-1;
//int defaultrowheight = m_Model.getRowHeight()-1;
int ystart = 1;
for (int row=0; row<getFixedRowCount(); row++) {
Point last = getValidCell(lastCol, row);
KTableCellRenderer fixedRenderer = m_Model.getCellRenderer(last.x, last.y);
int rowheight=m_Model.getRowHeight(row);
fixedRenderer.drawCell(gc,
new Rectangle(lastColRight + 2, ystart, r.width-1, rowheight-1),
-1, -1, "", false, true, false, m_Model);
ystart += rowheight;
}
TextCellRenderer defaultRenderer = new TextCellRenderer(SWT.NONE);
for (int row=m_TopRow; row<m_TopRow+m_RowsVisible+1; row++) {
int rowHeight = m_Model.getRowHeight(row);
defaultRenderer.drawCell(gc,
new Rectangle(lastColRight + 2, ystart, r.width-1, rowHeight-1),
-1, -1, "", false, false, false, m_Model);
ystart += rowHeight;
}
// draw bottom areas
gc.setBackground(getBackground());
gc.fillRectangle(r);
// draw outer lines:
Rectangle clientArea = getClientArea();
gc.setForeground(m_Display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
gc.drawLine(1, r.y , clientArea.x+clientArea.width -1, r.y );
gc.drawLine(clientArea.x+clientArea.width -1, 0, clientArea.x+clientArea.width -1, r.y-1);
// draw left and top border line:
if (m_Model.getRowCount() > 0) {
if ((getStyle() & SWT.FLAT)==0)
gc.setForeground(m_Display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
else
gc.setForeground(m_Display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
gc.drawLine(0, 0, 0, clientArea.y+clientArea.height- 1);
gc.drawLine(0,0,clientArea.x+clientArea.width, 0);
if (m_LeftColumn+m_ColumnsVisible == m_Model.getColumnCount())
gc.drawLine(clientArea.x+clientArea.width-1,0,clientArea.x+clientArea.width-1, clientArea.y+clientArea.height-1);
}
} else if ((getStyle() & SWTX.FILL_WITH_LASTCOL) != 0) {
gc.setBackground(getBackground());
gc.fillRectangle(r);
// draw outer lines:
Rectangle clientArea = getClientArea();
gc.setForeground(m_Display.getSystemColor(SWT.COLOR_WHITE));
gc.drawLine(1, r.y , clientArea.x+clientArea.width -1, r.y );
if (m_LeftColumn+m_ColumnsVisible == m_Model.getColumnCount())
gc.drawLine(clientArea.x+clientArea.width -1, 0, clientArea.x+clientArea.width -1, r.y-1);
// draw left and top border line:
if (m_Model.getRowCount() > 0) {
if ((getStyle() & SWT.FLAT)==0)
gc.setForeground(m_Display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
else
gc.setForeground(m_Display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
gc.drawLine(0, 0, 0, clientArea.y+clientArea.height- 1);
gc.drawLine(0,0,clientArea.x+clientArea.width, 0);
if (m_LeftColumn+m_ColumnsVisible == m_Model.getColumnCount())
gc.drawLine(clientArea.x+clientArea.width-1,0,clientArea.x+clientArea.width-1, clientArea.y+clientArea.height);
}
} else {
// draw simple background colored areas
gc.setBackground(getBackground());
gc.fillRectangle(r);
gc.fillRectangle(lastColRight + 2, 0, r.width, r.height);
gc.setForeground(m_Display.getSystemColor(SWT.COLOR_WHITE));
gc.drawLine(1, r.y , lastColRight + 1, r.y);
gc.drawLine(lastColRight + 1, 0, lastColRight + 1, r.y-1);
// draw left and top border line:
if (m_Model.getRowCount() > 0) {
if ((getStyle() & SWT.FLAT)==0)
gc.setForeground(m_Display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
else
gc.setForeground(m_Display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
gc.drawLine(0, 0, 0, r.y - 1);
gc.drawLine(0,0,lastColRight, 0);
}
}
}
// Cells
/**
* Redraws the the cells only in the given area.
*
* @param cellsToRedraw
* Defines the area to redraw. The rectangles elements are not
* pixels but cell numbers.
*/
public void redraw(Rectangle cellsToRedraw) {
checkWidget();
redraw(cellsToRedraw.x, cellsToRedraw.y, cellsToRedraw.width,
cellsToRedraw.height);
}
/**
* Redraws the the cells only in the given area.
*
* @param firstCol the first column to draw.
* @param firstRow the first row to draw. (Map if you use a sorted model!)
* @param numOfCols the number of columns to draw.
* @param numOfRows the number of rows to draw.
*/
public void redraw(int firstCol, int firstRow, int numOfCols, int numOfRows) {
checkWidget();
boolean redrawFixedRows = false;
if (firstRow<getFixedRowCount()) {
firstRow=m_TopRow;
redrawFixedRows = true;
}
boolean redrawFixedCols = false;
if (firstCol<getFixedColumnCount()) {
firstCol=m_LeftColumn;
redrawFixedCols = true;
}
Rectangle clipRect = getClientArea();
GC gc = new GC(this);
Rectangle oldClip = setContentAreaClipping(gc);
drawCells(gc, clipRect, firstCol, firstCol + numOfCols,
firstRow, firstRow + numOfRows);
gc.setClipping(oldClip);
if (redrawFixedRows)
drawCells(gc, gc.getClipping(), m_LeftColumn, m_Model
.getColumnCount(), 0, getFixedRowCount());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -