📄 tabletabnavigation.java
字号:
/* * BSD License for swtbinding * Copyright (c) 2005, JAYASOFT * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of JAYASOFT nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * version 20051212111010 */package net.sf.component.table;import org.eclipse.jface.viewers.CellEditor;import org.eclipse.jface.viewers.TableViewer;import org.eclipse.swt.SWT;import org.eclipse.swt.custom.TableCursor;import org.eclipse.swt.events.TraverseEvent;import org.eclipse.swt.events.TraverseListener;public class TableTabNavigation implements TraverseListener { public final static String END_TABLE_REACHED_HANDLER_KEY = "END_TABLE_REACHED_HANDLER"; final TableCursor cursor; final TableViewer tableViewer; public TableTabNavigation(TableCursor cursor, TableViewer tableViewer) { super(); this.cursor = cursor; this.tableViewer = tableViewer; } public void keyTraversed(TraverseEvent e) { if (e.keyCode == SWT.TAB || e.keyCode == SWT.CR) { if(!cursor.isVisible() && cursor.getRow() != null) { //if cursor is not visible it mean that we were editing the cell //ask for viewer to refresh it cell since it could have been modified. tableViewer.refresh(cursor.getRow().getData()); } if (e.stateMask == 0) { editNextCell(tableViewer, cursor); e.doit = false; } else if (e.stateMask == SWT.SHIFT) { editPreviousCell(tableViewer, cursor); e.doit = false; } } } static void editNextCell(final TableViewer tableViewer, final TableCursor cursor) { int col = cursor.getColumn(); if (cursor.getRow() != null) { int row = tableViewer.getTable().indexOf(cursor.getRow()); if (col < tableViewer.getColumnProperties().length - 1) { col += 1; } else { col = 0; row += 1; if (row >= tableViewer.getTable().getItemCount()) { row = handleTableEnd(tableViewer); } } //try to find next first editable column if any in the row CellEditor[] cellEditors = tableViewer.getCellEditors(); if(cellEditors != null && col < cellEditors.length) { for (int i = col; i < cellEditors.length; i++) { if(cellEditors[i] != null) { col = i; break; } } } editCell(tableViewer, cursor, row, col); } } /** * @return */ private static int handleTableEnd(TableViewer tableViewer) { TableEndHandler handler = (TableEndHandler)tableViewer.getTable().getData(END_TABLE_REACHED_HANDLER_KEY); if(handler != null) { return handler.getRowIndex(); } return 0; } static void editPreviousCell(final TableViewer tableViewer, final TableCursor cursor) { int col = cursor.getColumn(); if (cursor.getRow() != null) { int row = tableViewer.getTable().indexOf(cursor.getRow()); if (col > 0) { col -= 1; } else { col = tableViewer.getColumnProperties().length - 1; row -= 1; if (row < 0) { row = tableViewer.getTable().getItemCount() - 1; } } //have to handle line jump //try to first editable column if any// CellEditor[] cellEditors = tableViewer.getCellEditors();// if(cellEditors != null && col < cellEditors.length) {// for (int i = col; i >=0 ; i--) {// if(cellEditors[i] != null) {// col = i;// break;// }// }// } editCell(tableViewer, cursor, row, col); } } private static void editCell(final TableViewer tableViewer, final TableCursor cursor, int row, int col) { cursor.setSelection(row, col); cursor.notifyListeners(SWT.Selection, null); TableUtils.editCell(tableViewer, cursor); } public interface TableEndHandler { int getRowIndex(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -