[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Newsgroup Home]
|
[news.eclipse.platform.swt] Re: Default button and enter key cell activation
|
This would be done at the swt level by adding:
v.getTable().addTraverseListener(new TraverseListener() {
public void keyTraversed(TraverseEvent e) {
e.doit = e.keyCode != SWT.CR; // vetoes all CR traversals
}
});
Grant
"Andi Thomas" <athoma22@xxxxxxx> wrote in message
news:fvo79d$j2d$2@xxxxxxxxxxxxxxxxxxxx
> Andi Thomas wrote:
> > I have a table with a column viewer activation strategy with a CR key
> > pressed configuration, but in the same shell I have a default button.
> >
> > Navigating to a cell and pressing the enter key gets picked up by the
> > default button not the cell activation strategy.
> >
> > You might think that this is the correct behavior, but its not
> > unprecedented for a control to intercept the enter key press before the
> > default button kicks in. A Text control with the SWT.MULTI style but
> > for example.
> >
> > We have shells with and without default buttons and I would like the
> > table behavior to be consistent in all windows.
> >
> > Thanks (Tom!)
> >
> > - Andi
>
> Added an attachment that shows the problem. You can comment and
> uncomment line 37 to hide/show the problem.
>
----------------------------------------------------------------------------
----
> 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.ColumnViewerEditor;
> import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
> import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
> import org.eclipse.jface.viewers.EditingSupport;
> import org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter;
> 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.events.SelectionEvent;
> import org.eclipse.swt.events.SelectionListener;
> import org.eclipse.swt.layout.RowLayout;
> import org.eclipse.swt.widgets.Button;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Shell;
>
> /**
> * A simple TreeViewer to demonstrate usage
> *
> * @author Tom Schindl <tom.schindl@xxxxxxxxxxxxxxx>
> *
> */
> public class Snippet026TableViewerTabEditing {
> public Snippet026TableViewerTabEditing(final Shell shell) {
> final Button b = new Button(shell, SWT.PUSH);
> // Corrupts cell editor activation!!!
> shell.setDefaultButton(b);
>
> b.setText("Remove column");
> final TableViewer v = new TableViewer(shell, SWT.BORDER |
SWT.FULL_SELECTION);
> v.getTable().setLinesVisible(true);
> v.getTable().setHeaderVisible(true);
> b.addSelectionListener(new SelectionListener() {
>
> public void widgetDefaultSelected(final SelectionEvent e) {
>
> }
>
> public void widgetSelected(final SelectionEvent e) {
> v.getTable().getColumn(1).dispose();
> }
>
> });
>
> final TableViewerFocusCellManager focusCellManager = new
TableViewerFocusCellManager(v,
> new FocusCellOwnerDrawHighlighter(v));
> final ColumnViewerEditorActivationStrategy actSupport = new
ColumnViewerEditorActivationStrategy(v) {
> @Override
> protected boolean isEditorActivationEvent(final
ColumnViewerEditorActivationEvent event) {
> return event.eventType ==
ColumnViewerEditorActivationEvent.TRAVERSAL
> || event.eventType ==
ColumnViewerEditorActivationEvent.MOUSE_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);
>
> final TextCellEditor textCellEditor = new
TextCellEditor(v.getTable());
>
> TableViewerColumn column = new TableViewerColumn(v, SWT.NONE);
> column.getColumn().setWidth(200);
> column.getColumn().setMoveable(true);
> column.getColumn().setText("Column 1");
> column.setLabelProvider(new ColumnLabelProvider() {
>
> @Override
> public String getText(final Object element) {
> return "Column 1 => " + element.toString();
> }
>
> });
> column.setEditingSupport(new EditingSupport(v) {
> @Override
> protected boolean canEdit(final Object element) {
> return false;
> }
>
> @Override
> protected CellEditor getCellEditor(final Object element) {
> return textCellEditor;
> }
>
> @Override
> protected Object getValue(final Object element) {
> return ((MyModel) element).counter + "";
> }
>
> @Override
> protected void setValue(final Object element, final Object value) {
> ((MyModel) element).counter = Integer.parseInt(value.toString());
> v.update(element, null);
> }
> });
>
> column = new TableViewerColumn(v, SWT.NONE);
> column.getColumn().setWidth(200);
> column.getColumn().setMoveable(true);
> column.getColumn().setText("Column 2");
> column.setLabelProvider(new ColumnLabelProvider() {
>
> @Override
> public String getText(final Object element) {
> return "Column 2 => " + element.toString();
> }
>
> });
> column.setEditingSupport(new EditingSupport(v) {
> @Override
> protected boolean canEdit(final Object element) {
> return true;
> }
>
> @Override
> protected CellEditor getCellEditor(final Object element) {
> return textCellEditor;
> }
>
> @Override
> protected Object getValue(final Object element) {
> return ((MyModel) element).counter + "";
> }
>
> @Override
> protected void setValue(final Object element, final Object value) {
> ((MyModel) element).counter = Integer.parseInt(value.toString());
> v.update(element, null);
> }
> });
>
> column = new TableViewerColumn(v, SWT.NONE);
> column.getColumn().setWidth(200);
> column.getColumn().setMoveable(true);
> column.getColumn().setText("Column 3");
> column.setLabelProvider(new ColumnLabelProvider() {
>
> @Override
> public String getText(final Object element) {
> return "Column 3 => " + element.toString();
> }
>
> });
> column.setEditingSupport(new EditingSupport(v) {
> @Override
> protected boolean canEdit(final Object element) {
> return true;
> }
>
> @Override
> protected CellEditor getCellEditor(final Object element) {
> return textCellEditor;
> }
>
> @Override
> protected Object getValue(final Object element) {
> return ((MyModel) element).counter + "";
> }
>
> @Override
> protected void setValue(final Object element, final Object value) {
> ((MyModel) element).counter = Integer.parseInt(value.toString());
> v.update(element, null);
> }
> });
>
> v.setContentProvider(new ArrayContentProvider());
>
> v.setInput(createModel());
> }
>
> private List<MyModel> createModel() {
>
> final List<MyModel> items = new ArrayList<MyModel>();
> final MyModel root = new MyModel(0);
> root.counter = 0;
>
> for (int i = 1; i < 10; i++) {
> items.add(new MyModel(i));
> }
>
> return items;
> }
>
> public static void main(final String[] args) {
> final Display display = new Display();
> final Shell shell = new Shell(display);
> shell.setLayout(new RowLayout(SWT.VERTICAL));
> new Snippet026TableViewerTabEditing(shell);
> shell.pack();
> shell.open();
>
> while (!shell.isDisposed()) {
> if (!display.readAndDispatch())
> display.sleep();
> }
>
> display.dispose();
> }
>
> public class MyModel {
> public int counter;
>
> public MyModel(final int counter) {
> this.counter = counter;
> }
>
> @Override
> public String toString() {
> String rv = "Item ";
> rv += counter;
>
> return rv;
> }
> }
>
> }