📄 ktable.java
字号:
}
for (int j=0; j<getModel().getFixedHeaderRowCount(); j++) {
Point valid = getValidCell(col, j);
// allow painting of GC only on rows, not on cols:
Rectangle rowClip = new Rectangle(1+fixedWidth,1,bounds.width-1-fixedWidth,fixedHeight);
rowClip.intersect(oldClipping);
gc.setClipping(rowClip);
drawCell(gc, valid.x, valid.y);
}
gc.setClipping(oldClipping);
}
}
});
addFocusListener(new FocusListener() {
private Point[] oldSelection;
public void focusGained(FocusEvent e) {
if (!isShowSelectionWithoutFocus() &&
oldSelection!=null) {
setSelection(oldSelection, false);
for (int i=0; i<oldSelection.length; i++)
updateCell(oldSelection[i].x, oldSelection[i].y);
oldSelection=null;
}
}
public void focusLost(FocusEvent e) {
if (!isShowSelectionWithoutFocus()) {
oldSelection = getCellSelection();
clearSelection();
if (oldSelection!=null)
for (int i=0; i<oldSelection.length; i++)
updateCell(oldSelection[i].x, oldSelection[i].y);
}
}
});
TooltipListener tooltipListener = new TooltipListener();
addListener (SWT.Dispose, tooltipListener);
addListener (SWT.KeyDown, tooltipListener);
addListener (SWT.MouseDown, tooltipListener);
addListener (SWT.MouseDoubleClick, tooltipListener);
addListener (SWT.MouseMove, tooltipListener);
addListener (SWT.MouseHover, tooltipListener);
addListener (SWT.MouseExit, tooltipListener);
if (getVerticalBar() != null) {
getVerticalBar().addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
int oldTopRow = m_TopRow;
m_TopRow = getVerticalBar().getSelection();
if (oldTopRow!=m_TopRow)
redraw();
}
});
getVerticalBar().addListener(SWT.Selection, tooltipListener);
}
if (getHorizontalBar() != null) {
getHorizontalBar().addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
int oldLeftCol = m_LeftColumn;
m_LeftColumn = getHorizontalBar().getSelection();
if (oldLeftCol!=m_LeftColumn)
redraw();
}
});
getHorizontalBar().addListener(SWT.Selection, tooltipListener);
}
}
//////////////////////////////////////////////////////////////////////////////
// CALCULATIONS
//////////////////////////////////////////////////////////////////////////////
protected int getFixedWidth() {
int width = 0;
for (int i = 0; i < getFixedColumnCount(); i++)
width += getColumnWidth(i);
return width;
}
protected int getHeaderWidth() {
int width = 0;
for (int i = 0; i < m_Model.getFixedHeaderColumnCount(); i++)
width += getColumnWidth(i);
return width;
}
protected int getColumnLeft(int index) {
if (index < getFixedColumnCount()) {
int x = 0;
for (int i = 0; i < index; i++) {
x += getColumnWidth(i);
}
return x;
}
// regular data area:
if (index < m_LeftColumn ||
index> m_LeftColumn+m_ColumnsVisible)
return -1;
int x = getFixedWidth();
for (int i = m_LeftColumn; i < index; i++) {
x += getColumnWidth(i);
}
return x;
}
protected int getColumnRight(int index) {
if (index < 0)
return 0;
return getColumnLeft(index) + getColumnWidth(index);
}
protected int getRowBottom(int index) {
if ( index < 0)
return 0;
int y = getFixedHeight();
for ( int i = m_TopRow; i <= index; i++) {
y += m_Model.getRowHeight(i);
}
return y;
}
private int getFixedHeight() {
int height = 1;
for (int i=0; i<getFixedRowCount(); i++)
height+=m_Model.getRowHeight(i);
return height;
}
/**
* @return Returns the number of visible rows in the table.
* This always contains the fixed rows, and adds the number of
* rows that fit in the rest based on the currently shown top-row.
*/
public int getVisibleRowCount() {
if (m_Model==null)
return 0;
return getFixedRowCount()+getFullyVisibleRowCount(getFixedHeight());
}
private int getFullyVisibleRowCount(int fixedHeight) {
Rectangle rect = getClientArea();
ScrollBar sb = getHorizontalBar();
if (sb != null)
rect.height-=sb.getSize().y;
int count = 0;
int heightSum = fixedHeight;
for (int i=m_TopRow; heightSum<rect.height; i++) {
count++;
heightSum+=m_Model.getRowHeight(i);
}
return count;
}
protected void doCalculations() {
if (m_Model == null) {
ScrollBar sb = getHorizontalBar();
if (sb != null) {
sb.setMinimum(0);
sb.setMaximum(1);
sb.setPageIncrement(1);
sb.setThumb(1);
sb.setSelection(1);
}
sb = getVerticalBar();
if (sb != null) {
sb.setMinimum(0);
sb.setMaximum(1);
sb.setPageIncrement(1);
sb.setThumb(1);
sb.setSelection(1);
}
return;
}
Rectangle rect = getClientArea();
if (m_LeftColumn < getFixedColumnCount()) {
m_LeftColumn = getFixedColumnCount();
}
if (m_LeftColumn > m_Model.getColumnCount())
m_LeftColumn=0;
if (m_TopRow < getFixedRowCount()) {
m_TopRow = getFixedRowCount();
}
if (m_TopRow > m_Model.getRowCount())
m_TopRow = 0;
int fixedHeight = getFixedHeight();
m_ColumnsVisible = 0;
m_ColumnsFullyVisible = 0;
if (m_Model.getColumnCount() > getFixedColumnCount()) {
int runningWidth = getColumnLeft(m_LeftColumn);
for (int col = m_LeftColumn; col < m_Model.getColumnCount(); col++) {
if (runningWidth < rect.width + rect.x)
m_ColumnsVisible++;
runningWidth += getColumnWidth(col);
if (runningWidth < rect.width + rect.x)
m_ColumnsFullyVisible++;
else
break;
}
}
ScrollBar sb = getHorizontalBar();
if (sb != null) {
if (m_Model.getColumnCount() <= getFixedColumnCount()) {
sb.setMinimum(0);
sb.setMaximum(1);
sb.setPageIncrement(1);
sb.setThumb(1);
sb.setSelection(1);
} else {
sb.setMinimum(getFixedColumnCount());
sb.setMaximum(m_Model.getColumnCount());
sb.setIncrement(1);
sb.setPageIncrement(1);
sb.setThumb(m_ColumnsFullyVisible);
sb.setSelection(m_LeftColumn);
}
}
m_RowsFullyVisible = getFullyVisibleRowCount(fixedHeight);
m_RowsFullyVisible = Math.min(m_RowsFullyVisible, m_Model.getRowCount()
- getFixedRowCount());
m_RowsFullyVisible = Math.max(0, m_RowsFullyVisible);
m_RowsVisible = m_RowsFullyVisible + 1;
if (m_TopRow + m_RowsFullyVisible > m_Model.getRowCount()) {
m_TopRow = Math.max(getFixedRowCount(),
m_Model.getRowCount()- m_RowsFullyVisible);
}
if (m_TopRow + m_RowsFullyVisible >= m_Model.getRowCount()) {
m_RowsVisible--;
}
sb = getVerticalBar();
if (sb != null) {
if (m_Model.getRowCount() <= getFixedRowCount()) {
sb.setMinimum(0);
sb.setMaximum(1);
sb.setPageIncrement(1);
sb.setThumb(1);
sb.setSelection(1);
} else {
sb.setMinimum(getFixedRowCount());
sb.setMaximum(m_Model.getRowCount());
sb.setPageIncrement(m_RowsVisible - getFixedRowCount());
sb.setIncrement(1);
sb.setThumb(m_RowsFullyVisible);
sb.setSelection(m_TopRow);
}
}
}
/**
* Returns the area that is occupied by the given cell. Does not
* take into account any cell span.
* @param col
* @param row
* @return Rectangle
*/
protected Rectangle getCellRectIgnoreSpan(int col, int row) {
return getCellRectIgnoreSpan(col, row, getColumnLeft(col) + 1);
}
/**
* Returns the area that is occupied by the given cell. Does not
* take into account any cell span.<p>
* This version is an optimization if the x value is known. Use the
* version with just col and row as parameter if this is not known.
* @param col The column index
* @param row The row index
* @param x_startValue The right horizontal start value - if this is not
* known, there is a version without this parameter! Note that this is the
* end value of the last cell + 1 px border.
* @return Returns the area of a cell
*/
protected Rectangle getCellRectIgnoreSpan(int col, int row, int x_startValue) {
if ((col < 0) || (col >= m_Model.getColumnCount()))
return new Rectangle(-1, -1, 0, 0);
int x = x_startValue;
int y;
y = getYforRow(row);
if (row>=getFixedRowCount() && row<m_TopRow) {
for (int i = row; i<m_TopRow; i++)
y-=m_Model.getRowHeight(i);
}
// take into account 1px bottom and right border that
// belongs to the cell and is rendered by the cellrenderer
// TODO: Make lines seperately configured and seperately drawn items
int width = getColumnWidth(col) - 1;
int height = m_Model.getRowHeight(row) - 1;
return new Rectangle(x, y, width, height);
}
private int getRowForY(int y) {
int rowSum = 1;
for (int i=0; i<getFixedRowCount(); i++) {
int height =m_Model.getRowHeight(i);
if (rowSum<y && rowSum+height>=y) return i;
rowSum+=height;
}
for (int i=m_TopRow; i<m_Model.getRowCount(); i++) {
int height =m_Model.getRowHeight(i);
if (rowSum<y && rowSum+height>=y) return i;
rowSum+=height;
}
return -1;
}
private int getYforRow(int row) {
if (row == 0)
return 1;
int y = 1+m_Model.getRowHeight(0);
if (row < m_Model.getFixedHeaderRowCount()) {
for (int i=1; i<row; i++)
y += m_Model.getRowHeight(row);
}
else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -