swingx
  1. swingx
  2. SWINGX-1558

JXTableHeader: add extra getColumnToolTipText method

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Trivial Trivial
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.6.6
    • Component/s: Table
    • Labels:
      None

      Description

      The current signature of the getColumnToolTipText is

      protected String getColumnToolTipText(MouseEvent event)
      

      It would be nice if there was an extra method

      protected String getColumnToolTipText(TableColumnExt tableColumn)
      

      Typically if you want to override the tooltip you need access to the table column. This means you currently have to duplicate the code that is present in getColumnToolTipText

      My proposal is to adjust the current implementation of getColumnToolTipText to

          protected String getColumnToolTipText(MouseEvent event) {
              if (getXTable() == null) return null;
              int column = columnAtPoint(event.getPoint());
              if (column < 0) return null;
              TableColumnExt columnExt = getXTable().getColumnExt(column);
              return columnExt != null ? getColumnToolTipText( columnExt ) : null;
          }
          protected String getColumnToolTipText( TableColumnExt tableColumn ){
            return tableColumn.getToolTipText();
          }
      

      This should allow you to only override the getColumnToolTipText( TableColumnExt tableColumn ) method without having to duplicate the rest of the code in the getColumnToolTipText(MouseEvent event) method

        Activity

        Hide
        kleopatra added a comment -

        hmm .. can't quite think of a use-case (brain still a bit sluggish after vacation - why do you want to not return the column's tooltip? Could you elaborate a bit?

        But inclined to improve the factoring along your suggestion, maybe additionally separating the logic completely into a new method which returns the columnExt at the mouseEvent, need to think a bit.

        Thanks
        Jeanette

        Show
        kleopatra added a comment - hmm .. can't quite think of a use-case (brain still a bit sluggish after vacation - why do you want to not return the column's tooltip? Could you elaborate a bit? But inclined to improve the factoring along your suggestion, maybe additionally separating the logic completely into a new method which returns the columnExt at the mouseEvent, need to think a bit. Thanks Jeanette
        Hide
        robin_stevens added a comment -

        The use-case we have is that the tooltip is specified by the TableModel. Our TableModel is a sort of wrapper of our own "model" where the tooltip can be retrieved from our "model".

        We have one table where the TableModel is replaced at run-time based on some external parameters. As such, it is impossible to define the tooltips of the table in advance. Instead we retrieve them from our TableModel. In code, we have the following

          private static class TooltipTableHeader extends JXTableHeader {
        
            private final TableModel fTableModel;
        
            public TooltipTableHeader( TableColumnModel aColumnModel, TableModel aTableModel ) {
              super( aColumnModel );
              fTableModel = aTableModel;
            }
        
            @Override
            public String getToolTipText( MouseEvent event ) {
              int viewIndex = getColumnModel().getColumnIndexAtX( event.getX() );
              int modelIndex = getColumnModel().getColumn( viewIndex ).getModelIndex();
              if ( fTableModel instanceof IExtendedTableModel ) {
                return ( ( IExtendedTableModel ) fTableModel ).getColumnTooltipText( modelIndex );
              }
              return fTableModel.getColumnName( modelIndex );
            }
          }
        }
        

        As you can see, we need to duplicate the conversion from event to column. On the other hand, the workaround is so trivial and the use-case perhaps so limited that it does not justify to add API.

        Show
        robin_stevens added a comment - The use-case we have is that the tooltip is specified by the TableModel . Our TableModel is a sort of wrapper of our own "model" where the tooltip can be retrieved from our "model". We have one table where the TableModel is replaced at run-time based on some external parameters. As such, it is impossible to define the tooltips of the table in advance. Instead we retrieve them from our TableModel . In code, we have the following private static class TooltipTableHeader extends JXTableHeader { private final TableModel fTableModel; public TooltipTableHeader( TableColumnModel aColumnModel, TableModel aTableModel ) { super ( aColumnModel ); fTableModel = aTableModel; } @Override public String getToolTipText( MouseEvent event ) { int viewIndex = getColumnModel().getColumnIndexAtX( event.getX() ); int modelIndex = getColumnModel().getColumn( viewIndex ).getModelIndex(); if ( fTableModel instanceof IExtendedTableModel ) { return ( ( IExtendedTableModel ) fTableModel ).getColumnTooltipText( modelIndex ); } return fTableModel.getColumnName( modelIndex ); } } } As you can see, we need to duplicate the conversion from event to column. On the other hand, the workaround is so trivial and the use-case perhaps so limited that it does not justify to add API.
        Hide
        kleopatra added a comment -

        fixed revision #4296

        • added public api to get the tableColumn at a location
        • added protected api to get the tooltiptext at a column
        • changed getColumnToolTipText(MouseEvent) to use both
        • added visual tests

        Basically, went along your suggestion While doing so, discovered Issue SWINGX-1560 which is fixed also. JXTableHeaderVisualCheck has your use-case as an example.

        Thanks for the thorough report
        Jeanette

        Show
        kleopatra added a comment - fixed revision #4296 added public api to get the tableColumn at a location added protected api to get the tooltiptext at a column changed getColumnToolTipText(MouseEvent) to use both added visual tests Basically, went along your suggestion While doing so, discovered Issue SWINGX-1560 which is fixed also. JXTableHeaderVisualCheck has your use-case as an example. Thanks for the thorough report Jeanette

          People

          • Assignee:
            kleopatra
            Reporter:
            robin_stevens
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: