| /******************************************************************************* |
| * Copyright (c) 2006, 2014 Tom Schindl and others. |
| * |
| * This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Tom Schindl - initial API and implementation |
| * Simon Scholz <simon.scholz@vogella.com> - Bug 442343, 442747 |
| *******************************************************************************/ |
| |
| package org.eclipse.jface.snippets.viewers; |
| |
| import org.eclipse.jface.viewers.ArrayContentProvider; |
| import org.eclipse.jface.viewers.CellEditor; |
| import org.eclipse.jface.viewers.ColumnLabelProvider; |
| import org.eclipse.jface.viewers.ColumnViewer; |
| import org.eclipse.jface.viewers.ColumnViewerEditor; |
| import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent; |
| import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy; |
| import org.eclipse.jface.viewers.EditingSupport; |
| import org.eclipse.jface.viewers.TableViewer; |
| import org.eclipse.jface.viewers.TableViewerColumn; |
| import org.eclipse.jface.viewers.TableViewerEditor; |
| import org.eclipse.jface.viewers.TableViewerFocusCellManager; |
| import org.eclipse.jface.viewers.TextCellEditor; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.layout.FillLayout; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Display; |
| import org.eclipse.swt.widgets.Shell; |
| import org.eclipse.swt.widgets.Table; |
| |
| /** |
| * Demonstrates how to use keyboard-editing support in a TableViewer |
| * |
| * @author Tom Schindl <tom.schindl@bestsolution.at> |
| * |
| */ |
| public class Snippet044TableViewerKeyboardEditing { |
| |
| private static class MyModel { |
| public int counter; |
| |
| public MyModel(int counter) { |
| this.counter = counter; |
| } |
| |
| @Override |
| public String toString() { |
| return "Item " + this.counter; |
| } |
| } |
| |
| private class MyColumnLabelProvider extends ColumnLabelProvider { |
| |
| private int columnIndex; |
| private Table table; |
| |
| public MyColumnLabelProvider(Table table, int columnIndex) { |
| this.table = table; |
| this.columnIndex = columnIndex; |
| } |
| |
| @Override |
| public String getText(Object element) { |
| return "Column " + table.getColumnOrder()[columnIndex] + " => " + element; |
| } |
| } |
| |
| private class MyEditingSupport extends EditingSupport { |
| |
| private String property; |
| |
| public MyEditingSupport(ColumnViewer viewer, String property) { |
| super(viewer); |
| this.property = property; |
| } |
| |
| @Override |
| protected CellEditor getCellEditor(Object element) { |
| return new TextCellEditor((Composite) getViewer().getControl()); |
| } |
| |
| @Override |
| protected boolean canEdit(Object element) { |
| return true; |
| } |
| |
| @Override |
| protected Object getValue(Object element) { |
| return "Column " + property + " => " + element; |
| } |
| |
| @Override |
| protected void setValue(Object element, Object value) { |
| |
| } |
| |
| } |
| |
| public Snippet044TableViewerKeyboardEditing(Shell shell) { |
| final TableViewer v = new TableViewer(shell, SWT.BORDER|SWT.FULL_SELECTION); |
| v.setContentProvider(ArrayContentProvider.getInstance()); |
| |
| TableViewerColumn viewerColumn = new TableViewerColumn(v, SWT.NONE); |
| viewerColumn.getColumn().setWidth(200); |
| viewerColumn.getColumn().setText("Column 0"); |
| |
| viewerColumn.setLabelProvider(new MyColumnLabelProvider(v.getTable(), 0)); |
| viewerColumn.setEditingSupport(new MyEditingSupport(v, "1")); |
| |
| TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(v,new FocusBorderCellHighlighter(v)); |
| ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(v) { |
| @Override |
| protected boolean isEditorActivationEvent( |
| ColumnViewerEditorActivationEvent event) { |
| return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL |
| || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION |
| || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR) |
| || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC; |
| } |
| }; |
| |
| TableViewerEditor.create(v, focusCellManager, actSupport, ColumnViewerEditor.TABBING_HORIZONTAL |
| | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR |
| | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION); |
| |
| |
| MyModel[] model = createModel(); |
| v.setInput(model); |
| v.getTable().setLinesVisible(true); |
| v.getTable().setHeaderVisible(true); |
| } |
| |
| private MyModel[] createModel() { |
| MyModel[] elements = new MyModel[10]; |
| |
| for (int i = 0; i < 10; i++) { |
| elements[i] = new MyModel(i); |
| } |
| |
| return elements; |
| } |
| |
| /** |
| * @param args |
| */ |
| public static void main(String[] args) { |
| Display display = new Display(); |
| |
| Shell shell = new Shell(display); |
| shell.setLayout(new FillLayout()); |
| new Snippet044TableViewerKeyboardEditing(shell); |
| shell.open(); |
| |
| while (!shell.isDisposed()) { |
| if (!display.readAndDispatch()) |
| display.sleep(); |
| } |
| |
| display.dispose(); |
| |
| } |
| |
| } |