📄 tablecursor.java
字号:
int leadKey = (getStyle() & SWT.RIGHT_TO_LEFT) != 0 ? SWT.ARROW_RIGHT : SWT.ARROW_LEFT; if (event.keyCode == leadKey) { setRowColumn(rowIndex, Math.max(0, columnIndex - 1), true); } else { setRowColumn(rowIndex, Math.min(columnCount - 1, columnIndex + 1), true); } break; } case SWT.HOME : setRowColumn(0, columnIndex, true); break; case SWT.END : { int i = table.getItemCount() - 1; setRowColumn(i, columnIndex, true); break; } case SWT.PAGE_UP : { int index = table.getTopIndex(); if (index == rowIndex) { Rectangle rect = table.getClientArea(); TableItem item = table.getItem(index); Rectangle itemRect = item.getBounds(0); rect.height -= itemRect.y; int height = table.getItemHeight(); int page = Math.max(1, rect.height / height); index = Math.max(0, index - page + 1); } setRowColumn(index, columnIndex, true); break; } case SWT.PAGE_DOWN : { int index = table.getTopIndex(); Rectangle rect = table.getClientArea(); TableItem item = table.getItem(index); Rectangle itemRect = item.getBounds(0); rect.height -= itemRect.y; int height = table.getItemHeight(); int page = Math.max(1, rect.height / height); int end = table.getItemCount() - 1; index = Math.min(end, index + page - 1); if (index == rowIndex) { index = Math.min(end, index + page - 1); } setRowColumn(index, columnIndex, true); break; } }}void paint(Event event) { if (row == null) return; int columnIndex = column == null ? 0 : table.indexOf(column); GC gc = event.gc; Display display = getDisplay(); gc.setBackground(getBackground()); gc.setForeground(getForeground()); gc.fillRectangle(event.x, event.y, event.width, event.height); int x = 0; Point size = getSize(); Image image = row.getImage(columnIndex); if (image != null) { Rectangle imageSize = image.getBounds(); int imageY = (size.y - imageSize.height) / 2; gc.drawImage(image, x, imageY); x += imageSize.width; } String text = row.getText(columnIndex); if (text != "") { //$NON-NLS-1$ Rectangle bounds = row.getBounds(columnIndex); Point extent = gc.stringExtent(text); // Temporary code - need a better way to determine table trim String platform = SWT.getPlatform(); if ("win32".equals(platform)) { //$NON-NLS-1$ if (table.getColumnCount() == 0 || columnIndex == 0) { x += 2; } else { int alignmnent = column.getAlignment(); switch (alignmnent) { case SWT.LEFT: x += 6; break; case SWT.RIGHT: x = bounds.width - extent.x - 6; break; case SWT.CENTER: x += (bounds.width - x - extent.x) / 2; break; } } } else { if (table.getColumnCount() == 0) { x += 5; } else { int alignmnent = column.getAlignment(); switch (alignmnent) { case SWT.LEFT: x += 5; break; case SWT.RIGHT: x = bounds.width- extent.x - 2; break; case SWT.CENTER: x += (bounds.width - x - extent.x) / 2 + 2; break; } } } int textY = (size.y - extent.y) / 2; gc.drawString(text, x, textY); } if (isFocusControl()) { gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); gc.drawFocus(0, 0, size.x, size.y); }}void tableFocusIn(Event event) { if (isDisposed()) return; if (isVisible()) setFocus();}void tableMouseDown(Event event) { if (isDisposed() || !isVisible()) return; Point pt = new Point(event.x, event.y); Rectangle clientRect = table.getClientArea(); int columnCount = table.getColumnCount(); int maxColumnIndex = columnCount == 0 ? 0 : columnCount - 1; int start = table.getTopIndex(); int end = table.getItemCount(); for (int i = start; i < end; i++) { TableItem item = table.getItem(i); for (int j = 0; j <= maxColumnIndex; j++) { Rectangle rect = item.getBounds(j); if (rect.y > clientRect.y + clientRect.height) return; if (rect.contains(pt)) { setRowColumn(i, j, true); setFocus(); return; } } }}void traverse(Event event) { switch (event.detail) { case SWT.TRAVERSE_ARROW_NEXT : case SWT.TRAVERSE_ARROW_PREVIOUS : case SWT.TRAVERSE_RETURN : event.doit = false; return; } event.doit = true;}void setRowColumn(int row, int column, boolean notify) { TableItem item = row == -1 ? null : table.getItem(row); TableColumn col = column == -1 || table.getColumnCount() == 0 ? null : table.getColumn(column); setRowColumn(item, col, notify);}void setRowColumn(TableItem row, TableColumn column, boolean notify) { if (this.row == row && this.column == column) { return; } if (this.row != null && this.row != row) { this.row.removeListener(SWT.Dispose, disposeItemListener); this.row = null; } if (this.column != null && this.column != column) { this.column.removeListener(SWT.Dispose, disposeColumnListener); this.column.removeListener(SWT.Move, resizeListener); this.column.removeListener(SWT.Resize, resizeListener); this.column = null; } if (row != null) { if (this.row != row) { this.row = row; row.addListener(SWT.Dispose, disposeItemListener); table.showItem(row); } if (this.column != column && column != null) { this.column = column; column.addListener(SWT.Dispose, disposeColumnListener); column.addListener(SWT.Move, resizeListener); column.addListener(SWT.Resize, resizeListener); table.showColumn(column); } int columnIndex = column == null ? 0 : table.indexOf(column); setBounds(row.getBounds(columnIndex)); redraw(); if (notify) { notifyListeners(SWT.Selection, new Event()); } }}public void setVisible(boolean visible) { checkWidget(); if (visible) resize(); super.setVisible(visible);}/** * Removes the listener from the collection of listeners who will * be notified when the receiver's selection changes. * * @param listener the listener which should no longer be notified * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> * </ul> * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * * @see SelectionListener * @see #addSelectionListener(SelectionListener) * * @since 3.0 */public void removeSelectionListener(SelectionListener listener) { checkWidget(); if (listener == null) { SWT.error(SWT.ERROR_NULL_ARGUMENT); } removeListener(SWT.Selection, listener); removeListener(SWT.DefaultSelection, listener); }void resize() { if (row == null) { setBounds(-200, -200, 0, 0); } else { int columnIndex = column == null ? 0 : table.indexOf(column); setBounds(row.getBounds(columnIndex)); }}/** * Returns the column over which the TableCursor is positioned. * * @return the column for the current position * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> */public int getColumn() { checkWidget(); return column == null ? 0 : table.indexOf(column);}/** * Returns the row over which the TableCursor is positioned. * * @return the item for the current position * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> */public TableItem getRow() { checkWidget(); return row;}public void setBackground (Color color) { if (color == null) color = getDisplay().getSystemColor(BACKGROUND); super.setBackground(color); redraw();}public void setForeground (Color color) { if (color == null) color = getDisplay().getSystemColor(FOREGROUND); super.setForeground(color); redraw();}/** * Positions the TableCursor over the cell at the given row and column in the parent table. * * @param row the index of the row for the cell to select * @param column the index of column for the cell to select * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * */public void setSelection(int row, int column) { checkWidget(); int columnCount = table.getColumnCount(); int maxColumnIndex = columnCount == 0 ? 0 : columnCount - 1; if (row < 0 || row >= table.getItemCount() || column < 0 || column > maxColumnIndex) SWT.error(SWT.ERROR_INVALID_ARGUMENT); setRowColumn(row, column, false);}/** * Positions the TableCursor over the cell at the given row and column in the parent table. * * @param row the TableItem of the row for the cell to select * @param column the index of column for the cell to select * * @exception SWTException <ul> * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> * </ul> * */public void setSelection(TableItem row, int column) { checkWidget(); int columnCount = table.getColumnCount(); int maxColumnIndex = columnCount == 0 ? 0 : columnCount - 1; if (row == null || row.isDisposed() || column < 0 || column > maxColumnIndex) SWT.error(SWT.ERROR_INVALID_ARGUMENT); setRowColumn(table.indexOf(row), column, false);}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -