| /******************************************************************************* |
| * 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 |
| * Lars Vogel <Lars.Vogel@gmail.com> - Bug 414565 |
| * Simon Scholz <simon.scholz@vogella.com> - Bug 442343, 448143 |
| *******************************************************************************/ |
| |
| 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.EditingSupport; |
| import org.eclipse.jface.viewers.TableViewer; |
| import org.eclipse.jface.viewers.TableViewerColumn; |
| 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; |
| |
| /** |
| * Edit cell values in a table |
| * |
| */ |
| public class Snippet009CellEditors { |
| public class MyModel { |
| public int counter; |
| |
| public MyModel(int counter) { |
| this.counter = counter; |
| } |
| |
| @Override |
| public String toString() { |
| return "Item " + this.counter; |
| } |
| } |
| |
| public Snippet009CellEditors(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().setText("Column1"); |
| viewerColumn.getColumn().setWidth(300); |
| viewerColumn.setLabelProvider(new ColumnLabelProvider()); |
| viewerColumn.setEditingSupport(new EditingSupport(v) { |
| |
| @Override |
| protected void setValue(Object element, Object value) { |
| ((MyModel) element).counter = Integer.parseInt(value.toString()); |
| getViewer().update(element, null); |
| } |
| |
| @Override |
| protected Object getValue(Object element) { |
| return ((MyModel) element).counter + ""; |
| } |
| |
| @Override |
| protected CellEditor getCellEditor(Object element) { |
| return new TextCellEditor((Composite) getViewer().getControl()); |
| } |
| |
| @Override |
| protected boolean canEdit(Object element) { |
| return ((MyModel) element).counter % 2 == 0; |
| } |
| }); |
| |
| |
| MyModel[] model = createModel(); |
| v.setInput(model); |
| v.getTable().setLinesVisible(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 Snippet009CellEditors(shell); |
| shell.open (); |
| |
| while (!shell.isDisposed ()) { |
| if (!display.readAndDispatch ()) display.sleep (); |
| } |
| |
| display.dispose (); |
| |
| } |
| |
| } |