1

hwo can I change the default selection behaviour of tables, I want to make a cell selected when user click it and make it editable when user double click it.

with @nonty 's help, I get what I want.
enter image description here
here is my cell highlighter implemention:

package com.amarsoft.rcputil;

import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;


public class DefaultCellFocusHighlighter extends FocusCellOwnerDrawHighlighter {

    public DefaultCellFocusHighlighter(ColumnViewer viewer) {
        super(viewer);
    }

    protected boolean onlyTextHighlighting(ViewerCell cell) {
        return false;
    }

    protected Color getSelectedCellBackgroundColor(ViewerCell cell) {
        return cell.getControl().getDisplay().getSystemColor(SWT.COLOR_DARK_BLUE);
    }

    protected Color getSelectedCellForegroundColor(ViewerCell cell) {
        return cell.getControl().getDisplay().getSystemColor(SWT.COLOR_WHITE);
    }

    protected Color getSelectedCellForegroundColorNoFocus(ViewerCell cell) {
        return cell.getControl().getDisplay().getSystemColor(SWT.COLOR_WHITE);
    }

    protected Color getSelectedCellBackgroundColorNoFocus(ViewerCell cell) {
        return cell.getControl().getDisplay().getSystemColor(SWT.COLOR_DARK_BLUE);
    }

    protected void focusCellChanged(ViewerCell newCell, ViewerCell oldCell) {
        super.focusCellChanged(newCell, oldCell);
    }



}

the code to use it :

TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(tv,new DefaultCellFocusHighlighter(tv));
        ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(tv) {
            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(tv, focusCellManager, actSupport, ColumnViewerEditor.TABBING_HORIZONTAL
                | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
                | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION);

but I got new problem : enter image description here
when I double click on cell to edit it's value, there is a little area at the left side of the cell is still highlighted with dark blue color

I know why :
When a text control is created with a border, the operating system includes a platform specific inset around the contents of the control.
still seeking for fixing...

CaiNiaoCoder
  • 3,269
  • 9
  • 52
  • 82

2 Answers2

1

Have a look at these two JFace Snippets:

Campa
  • 4,267
  • 3
  • 37
  • 42
Tonny Madsen
  • 12,628
  • 4
  • 31
  • 70
  • @nonty thanks. I have checked these two snippets, but I still don't understand it well, in the Snippet036FocusBorderCellHighlighter, when a cell is clicked, that cell is redrawed to remove the highlighted appearence, but other cells in same row is still being highlighted. I am thinking wether that possible to disable default selection behaviour, then write code to do it. I mean when you create a table with SWT.FULL_SELECTION but without any listener attached to it, you click on a row, the row is selected, there must be some Mouse Event listener was fired in the underlying SWT table, – CaiNiaoCoder Jun 28 '11 at 09:35
  • @nonty maybe I can remove the underlying lsitener and add a new one ? – CaiNiaoCoder Jun 28 '11 at 09:37
  • Not sure what you mean - when I run the example using `FocusCellOwnerDrawHighlighter` as the `FocusCellHighlighter` on line 158, then I get the intended behavior... – Tonny Madsen Jun 28 '11 at 09:46
  • @ I need some time to fully understand it...where is the FocusCellOwnerDrawHighlighter? – CaiNiaoCoder Jun 28 '11 at 10:04
  • `org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter` – Tonny Madsen Jun 28 '11 at 10:12
  • @nonty thanks again. I really got what I want, but I have new problem. I had edited my question.(I cant express my great appreciation because I dont know how to say it in ENGLISH!!!) – CaiNiaoCoder Jun 28 '11 at 12:30
0

After digging through the code, I found the following method in the ColumnViewer class:

/**
 * Hook up the editing support. Subclasses may override.
 * 
 * @param control
 *      the control you want to hook on
 */
protected void hookEditingSupport(Control control) {
    // Needed for backwards comp with AbstractTreeViewer and TableTreeViewer
    // who are not hooked this way others may already overwrite and provide
    // their
    // own impl
    if (viewerEditor != null) {
        control.addMouseListener(new MouseAdapter() {
            public void mouseDown(MouseEvent e) {
                // Workaround for bug 185817
                if (e.count != 2) {
                    handleMouseDown(e);
                }
            }

            public void mouseDoubleClick(MouseEvent e) {
                handleMouseDown(e);
            }
        });
    }
}

So, I overrode that function within my TableViewer subclass:

@Override protected void hookEditingSupport(Control control) {
    // We know there should be an editor avaiable
//  if (viewerEditor != null) {
        control.addMouseListener(new MouseAdapter() {
            public void mouseDown(MouseEvent e) {
                // Workaround for bug 185817
                if (e.count != 2) {
                    // We don't want to edit on single clicks
//                  handleMouseDown(e);
                }
            }

            public void mouseDoubleClick(MouseEvent e) {
                // This method is private, so copy the implementation
//              handleMouseDown(e);
                ViewerCell cell = getCell(new Point(e.x, e.y));
                e.count--; // A hack to make things work - pretend like it's a single click
                if (cell != null) {
                    triggerEditorActivationEvent(new ColumnViewerEditorActivationEvent(
                            cell, e));
                }
            }
        });
//  }
}

This works for me. Tell me if it works for you.

Ethan Reesor
  • 2,090
  • 1
  • 23
  • 40