| /******************************************************************************* |
| * Copyright (c) 2006, 2015 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 |
| * Jeanderson Candido <http://jeandersonbc.github.io> - Bug 414565 |
| * Simon Scholz <simon.scholz@vogella.com> - Bug 448143 |
| * Lars Vogel <Lars.Vogel@vogella.com> - Bug 475361 |
| *******************************************************************************/ |
| |
| package org.eclipse.jface.snippets.viewers; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| 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.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; |
| |
| /** |
| * Shows how to setup a Viewer to start cell editing on double click |
| * |
| * @author Tom Schindl <tom.schindl@bestsolution.at> |
| * |
| */ |
| public class Snippet052DoubleClickCellEditor { |
| |
| public static boolean flag = true; |
| |
| public static class MyModel { |
| public int counter; |
| |
| public MyModel(int counter) { |
| this.counter = counter; |
| } |
| |
| @Override |
| public String toString() { |
| return "Item " + this.counter; |
| } |
| } |
| |
| public Snippet052DoubleClickCellEditor(Shell shell) { |
| final TableViewer viewer = new TableViewer(shell, SWT.BORDER |
| | SWT.FULL_SELECTION); |
| viewer.setContentProvider(ArrayContentProvider.getInstance()); |
| |
| ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy( |
| viewer) { |
| @Override |
| protected boolean isEditorActivationEvent( |
| ColumnViewerEditorActivationEvent event) { |
| return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL |
| || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION |
| || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC; |
| } |
| }; |
| |
| int feature = ColumnViewerEditor.TABBING_HORIZONTAL |
| | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR |
| | ColumnViewerEditor.TABBING_VERTICAL |
| | ColumnViewerEditor.KEYBOARD_ACTIVATION; |
| |
| TableViewerEditor.create(viewer, actSupport, feature); |
| |
| String[] labels = { "Column 1", "Column 2", "Column 3" }; |
| String[] properties = new String[] { "1", "2", "3" }; |
| int i = 0; |
| for (String label : labels) { |
| createColumnFor(viewer, label, properties[i++]); |
| } |
| viewer.setInput(createModel()); |
| viewer.getTable().setLinesVisible(true); |
| viewer.getTable().setHeaderVisible(true); |
| } |
| |
| private TableViewerColumn createColumnFor(final TableViewer viewer, |
| String label, String property) { |
| TableViewerColumn column; |
| column = new TableViewerColumn(viewer, SWT.NONE); |
| column.getColumn().setWidth(200); |
| column.getColumn().setMoveable(true); |
| column.getColumn().setText(label); |
| column.setLabelProvider(new ColumnLabelProvider()); |
| column.setEditingSupport(new MyEditingSupport(viewer, property)); |
| return column; |
| } |
| |
| private List<MyModel> createModel() { |
| List<MyModel> elements = new ArrayList<>(); |
| |
| for (int i = 0; i < 10; i++) { |
| elements.add(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 Snippet052DoubleClickCellEditor(shell); |
| shell.open(); |
| |
| while (!shell.isDisposed()) { |
| if (!display.readAndDispatch()) |
| display.sleep(); |
| } |
| display.dispose(); |
| |
| } |
| |
| 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) { |
| |
| } |
| |
| } |
| |
| } |