Native tooltips on JFace TableViewer not working flawlessly

旧时模样 提交于 2019-12-25 03:55:30

问题


I have a TableViewer with multiple columns where some columns have tooltips and some don't. Tooltip support is enabled through

ColumnViewerToolTipSupport.enableFor( tableViewer );

Each column has a CellLabelProvider assigned whose getToolTipText() method either returns null (no tooltip) or a tooltip text.

All columns use native toolstips, i.e. the respective useNativeToolTip() implementations return true.

While all this works at first sight, there is a flaw when moving the mouse pointer from a column without a tooltip to a column with a tooltip: the latter column does not show its tooltip.

Te described behavior can at least be observed on Windows 7.


回答1:


The workaround is to build a custom tooltip support that uses MouseMove and MouseHover listeners to update the tooltip of the underlying Table with the on from the current cell.

class NativeViewerToolTipSupport {

  private final ExtendedTableViewer tableViewer;
  private final Listener mouseListener;

  NativeViewerToolTipSupport( ExtendedTableViewer tableViewer ) {
    this.tableViewer = tableViewer;
    this.tableViewer.getTable().setToolTipText( "" );
    this.mouseListener = this::handleMouseEvent;
  }

  void install() {
    tableViewer.getTable().addListener( SWT.Dispose, event -> uninstall() );
    tableViewer.getTable().addListener( SWT.MouseMove, mouseListener );
    tableViewer.getTable().addListener( SWT.MouseHover, mouseListener );
  }

  void uninstall() {
    tableViewer.getTable().removeListener( SWT.MouseMove, mouseListener );
    tableViewer.getTable().removeListener( SWT.MouseHover, mouseListener );
  }

  private void handleMouseEvent( Event event ) {
    String toolTipText = getToolTipText( event );
    if( !Objects.equals( tableViewer.getTable().getToolTipText(), toolTipText ) ) {
      tableViewer.getControl().setToolTipText( toolTipText );
    }
  }

  private String getToolTipText( Event event ) {
    String result = "";
    Point point = new Point( event.x, event.y );
    ViewerRow row = tableViewer.getViewerRow( point );
    if( row != null ) {
      ViewerCell cell = row.getCell( point );
      if( cell != null ) {
        CellLabelProvider labelProvider = tableViewer.getLabelProvider( cell.getColumnIndex() );
        if( labelProvider != null ) {
          result = labelProvider.getToolTipText( cell.getElement() );
        }
      }
    }
    return result;
  }

}

The above class needs to find out the cell at the current mouse position and therefore requires access to getViewerRow(). Hence, it requires a TableViewer-derived class that makes getViewerRow() publicly accessible:

public class ExtendedTableViewer extends TableViewer {
  @Override
  public ViewerRow getViewerRow( Point point ) {
    return super.getViewerRow( point );
  }
}


来源:https://stackoverflow.com/questions/38105913/native-tooltips-on-jface-tableviewer-not-working-flawlessly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!