2011-06-28 60 views
1

hwo是否可以更改表格的默認選擇行爲,我想在用戶單擊它時選中一個單元格,並在用戶雙擊它時使其可編輯。eclipse rcp:如何在tableviewer中選擇單個單元格?

與@nonty的幫助,我得到我想要的。
enter image description here
這裏是我的手機熒光筆FPGA實現:

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); 
    } 



} 

代碼使用它:

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); 

但我得到了新的問題: enter image description here
當我雙擊單元格以編輯它的價值,在細胞左側還有一點區域仍然用深藍色突出顯示

我知道爲什麼:
當使用邊框創建文本控件時,操作系統會在控件的內容周圍包含一個特定於平臺的插件。
仍然尋求固定...

回答

1

看一看這兩個JFace的片段:

+0

@nonty謝謝。我已經檢查了這兩個片段,但我仍然不明白,在Snippet036FocusBorderCellHighlighter中,單擊單元格時,該單元格將被重繪以移除突出顯示的外觀,但同一行中的其他單元格仍然被突出顯示。我在想,可以禁用默認選擇行爲,然後編寫代碼來完成它。我的意思是當你用SWT.FULL_SELECTION創建一個表,但沒有附加任何監聽器的時候,你點擊一行,該行被選中,必須有一些鼠標事件監聽器在底層SWT表中被觸發 – CaiNiaoCoder

+0

@nonty也許我可以刪除底層的lsitener並添加一個新的? – CaiNiaoCoder

+0

不確定你的意思 - 當我使用FocusCellOwnerDrawHighlighter作爲FocusCellHighlighter在線158上運行示例時,我得到了預期的行爲... –

0

通過代碼挖掘後,我發現下面的方法在ColumnViewer類:

/** 
* 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); 
      } 
     }); 
    } 
} 

於是,我不顧我的TableViewer子內的功能:

@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)); 
       } 
      } 
     }); 
// } 
} 

這對我的作品。告訴我它是否適合你。

相關問題