Gian Luca Spadafora
Gian Luca Spadafora

Reputation: 27

SWT how to have table that highlights entire row with single click, and edits cell with double click?

SWT newbie here. So, what I want is to be able to highlight a whole row, along with being able to select multiple rows, and make it so that double click edits the cells. Is a focusCellManager necessary? Relevant pieces of code:

EditorActivationEvent

final ColumnViewerEditorActivationStrategy actSupport = 
   new ColumnViewerEditorActivationStrategy(this)
   {
     @Override
     protected boolean isEditorActivationEvent
     (ColumnViewerEditorActivationEvent event)
       {
         return event.type == 
             ColumnViewerEditorActivationEvent.TRAVERSAL
             || event.eventType == 
         ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
             || event.eventType == 
             ColumnViewerEditorActivationEvent.KEY_PRESSED
             || event.eventType ==
             ColumnViewerEditorActivationEvent.PROGRAMMATIC;
       }
  };

creation of TableViewerEditor

TableViewerEditor.create(this,
    mgr,
    actSupport,
    ColumnViewerEditor.TABBING_HORIZONTAL|
    ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR|
    ColumnViewerEditor.TABBING_VERTICAL|
    ColumnViewerEditor.KEYBOARD_ACTIVATION);

code for mgr (focusCellManager):

focusCellOwnerDrawHighlighter drawHighlighter = new FocusCellOwnerDrawHighlighter(this);

final TableViewerFocusCellManager mgr = new TableViewerFocusCellManager(this, null);

The tableViewer (doesn't appear in the previous snippers as tableViewer is extended by another class and we use the other class, so I don't want to confuse you):

TableViewer vwr = new TableViewer(tableComposite,SWT.BORDER|SWT.FULL_SELECTION|SWT.MULTI);

Upvotes: 0

Views: 341

Answers (1)

greg-449
greg-449

Reputation: 111142

Using EditingSupport on the table columns combined with the following TableViewerEditor seems to work for me:

TableViewer viewer = new TableViewer(tableComp, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.FULL_SELECTION);

ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(viewer) {
  @Override
  protected boolean isEditorActivationEvent(final ColumnViewerEditorActivationEvent event) {
    return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
        || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
        || event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED
        || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
  }
};

int feature = ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR | ColumnViewerEditor.TABBING_HORIZONTAL
    | ColumnViewerEditor.KEYBOARD_ACTIVATION
    | ColumnViewerEditor.TABBING_CYCLE_IN_VIEWER;

TableViewerEditor.create(viewer, actSupport, feature);

Upvotes: 1

Related Questions