📄 tableutils.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.ICellEditorListener;import org.eclipse.jface.viewers.ISelectionChangedListener;import org.eclipse.jface.viewers.SelectionChangedEvent;import org.eclipse.jface.viewers.StructuredSelection;import org.eclipse.jface.viewers.TableViewer;import org.eclipse.jface.viewers.TextCellEditor;import org.eclipse.swt.SWT;import org.eclipse.swt.custom.TableCursor;import org.eclipse.swt.events.KeyAdapter;import org.eclipse.swt.events.KeyEvent;import org.eclipse.swt.events.MouseAdapter;import org.eclipse.swt.events.MouseEvent;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.widgets.Table;import org.eclipse.swt.widgets.TableItem;import org.eclipse.swt.widgets.Text;public class TableUtils { /** * @param tableViewer * @param editors */ public static TableCursor installTableCursor(final TableViewer tableViewer) { CellEditor[] editors = tableViewer.getCellEditors(); final Table table = tableViewer.getTable(); final TableCursor cursor = new TableCursor(table, SWT.NONE); cursor.addMouseListener(new MouseAdapter() { public void mouseDown(MouseEvent e) { editCell(tableViewer, cursor); } }); cursor.addSelectionListener(new SelectionAdapter() { public void widgetDefaultSelected(SelectionEvent e) { editCell(tableViewer, cursor); } public void widgetSelected(SelectionEvent e) { CellEditor cellEditor = tableViewer.getCellEditors()[cursor.getColumn()]; if (cellEditor != null) { cellEditor.deactivate(); } // set cursor-selection to mark whole row tableViewer.setSelection(new StructuredSelection(cursor.getRow()), true); // set selection of table separatly; viewer does incorrectly. table.setSelection(new TableItem[] { cursor.getRow() }); } }); // KEY-LISTENER cursor.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { // Hide the TableCursor when the user hits the "CTRL" or "SHIFT" key. // This alows the user to select multiple items in the table. if ((e.keyCode == SWT.CTRL || e.keyCode == SWT.SHIFT) || (((e.stateMask & SWT.CONTROL) != 0 || (e.stateMask & SWT.SHIFT) != 0) && ((e.keyCode & SWT.ARROW) != 0))) { cursor.setVisible(false); } else if ((e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) && e.stateMask == 0) { editCell(tableViewer, cursor); } else if ((e.keyCode < 0x10000 || e.character != '\0') && e.keyCode > 0x1f && e.keyCode != 127 || e.keyCode == 0x00 && (e.stateMask == 0 || e.stateMask == SWT.SHIFT)) { editCell(tableViewer, cursor); if (tableViewer.getCellEditors()[cursor.getColumn()] instanceof TextCellEditor) { TextCellEditor editor = ((TextCellEditor)tableViewer.getCellEditors()[cursor.getColumn()]); editor.setValue(String.valueOf(e.character)); ((Text)editor.getControl()).setSelection(1); } } } }); // Show the TableCursor when the user releases the "SHIFT" or "CTRL" key. // This signals the end of the multiple selection task. table.addKeyListener(new KeyAdapter() { public void keyReleased(KeyEvent e) { if (e.keyCode == SWT.CONTROL && (e.stateMask & SWT.SHIFT) != 0) return; if (e.keyCode == SWT.SHIFT && (e.stateMask & SWT.CONTROL) != 0) return; if (e.keyCode != SWT.CONTROL && (e.stateMask & SWT.CONTROL) != 0) return; if ((e.keyCode == SWT.ARROW_DOWN || e.keyCode == SWT.ARROW_LEFT || e.keyCode == SWT.ARROW_UP || e.keyCode == SWT.ARROW_RIGHT) && ((e.stateMask & SWT.SHIFT) != 0 || (e.stateMask & SWT.CONTROL) != 0)) { return; } if ((e.keyCode < 0x10000 || e.character != '\0') && e.keyCode > 0x1f && e.keyCode != 127 || e.keyCode == 0x00 && (e.stateMask == 0 || e.stateMask == SWT.SHIFT)) { // The shift key is modifyer for typing not selection editCell(tableViewer, cursor); if (tableViewer.getCellEditors()[cursor.getColumn()] instanceof TextCellEditor) { TextCellEditor editor = ((TextCellEditor)tableViewer.getCellEditors()[cursor.getColumn()]); editor.setValue(String.valueOf(e.character)); ((Text)editor.getControl()).setSelection(1); } } else { // Selection just end TableItem[] selection = table.getSelection(); TableItem row; if (selection.length == 0) { if (table.getItemCount() != 0) { row = table.getItem(table.getTopIndex()); } else { return; } } else { row = selection[0]; } table.showItem(row); cursor.setSelection(row, 0); cursor.setVisible(true); cursor.setFocus(); } } }); if (editors != null) { CellEditorListener cellEditorListener = new CellEditorListener(tableViewer, cursor); for (int i = 0; i < editors.length; i++) { if (editors[i] != null) { editors[i].addListener(cellEditorListener); } } } tableViewer.addSelectionChangedListener(new ISelectionChangedListener() { public void selectionChanged(SelectionChangedEvent arg0) { cursor.redraw(); } }); return cursor; } /** * @param tableViewer * @param editors * @param cursor */ public static void installTabNavigation(TableViewer tableViewer, TableCursor cursor) { CellEditor[] editors = tableViewer.getCellEditors(); TableTabNavigation tabKeyListener = new TableTabNavigation(cursor, tableViewer); cursor.addTraverseListener(tabKeyListener); tableViewer.getTable().addTraverseListener(tabKeyListener); if (editors != null) { for (int i = 0; i < editors.length; i++) { if (editors[i] != null) { if (editors[i].getControl() != null) { editors[i].getControl().addTraverseListener(tabKeyListener); } } } } } private static final class CellEditorListener implements ICellEditorListener { TableViewer tableViewer; TableCursor cursor; public CellEditorListener(TableViewer tableViewer, TableCursor cursor) { this.tableViewer = tableViewer; this.cursor = cursor; } public void applyEditorValue() { makeCursorVisible(); //ask for viewer to refresh it cell since it have been modified. if (cursor.getRow() != null) { tableViewer.refresh(cursor.getRow().getData()); } } /** * */ private void makeCursorVisible() { if (cursor.getRow() != null) { tableViewer.setSelection(new StructuredSelection(cursor.getRow()), true); // set selection of table separatly; viewer does incorrectly. tableViewer.getTable().setSelection(new TableItem[] { cursor.getRow() }); cursor.setVisible(true); cursor.redraw(); } } public void cancelEditor() { makeCursorVisible(); } public void editorValueChanged(boolean oldValidState, boolean newValidState) { } } /** * @param tableViewer * @param cursor */ static void editCell(final TableViewer tableViewer, final TableCursor cursor) { if (cursor.getRow() != null) { tableViewer.editElement(cursor.getRow().getData(), cursor.getColumn()); // hide cursor only f there is an editor active on the cell cursor.setVisible(!tableViewer.isCellEditorActive()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -