Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » NatTable » How show the Tooltip for part display cell(xxx...)
How show the Tooltip for part display cell(xxx...) [message #1745749] Mon, 17 October 2016 07:48 Go to next message
neal zhang is currently offline neal zhangFriend
Messages: 45
Registered: July 2012
Member
HI all,
I hope to show a tooltip function when cell only display part of content(end with ...). and this toolTip dialog show full of content. and i found TextPainter.java which have this function.
private String modifyTextToDisplay(String text, GC gc, int availableLength) {

if (lineLength > availableLength) {
it will splice a "..."

and there also have another function
protected String getTextToDisplay(ILayerCell cell, GC gc, int availableLength, String text) {

I tried to add a flag in this cell, but it is not possible to get this flag in outside.

Hi everyone, who have a good idea for this function? thank you very much.





Re: How show the Tooltip for part display cell(xxx...) [message #1745802 is a reply to message #1745749] Mon, 17 October 2016 15:28 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
That is already supported by
NatTableContentTooltip
you probably only need to customize the
getText()
method to only return a value in case the shown text is cutted.
Re: How show the Tooltip for part display cell(xxx...) [message #1745825 is a reply to message #1745802] Tue, 18 October 2016 04:11 Go to previous messageGo to next message
neal zhang is currently offline neal zhangFriend
Messages: 45
Registered: July 2012
Member
HI dirk,
thank you for your reply, I extend NatTableContentTooltip.java and override the getText function. but i can't get the flag for display part of content(...). nattable have many layout. this Cell is not same with TextPainter's function. do you have some idea? how implement this function, i only hope to show this tooltip when Cell can't show all of content.

i tried like this:

xxxxTextPainter extends TextPainter
private boolean isDisplayPart = false;

  

  /**
   * Checks if the given text is bigger than the available space. If not the
   * given text is simply returned without modification. If the text does not
   * fit into the available space, it will be modified by cutting and adding
   * three dots.
   *
   * @param text
   *            the text to compute
   * @param gc
   *            the current GC
   * @param availableLength
   *            the available space
   * @return the modified text if it is bigger than the available space or the
   *         text as it was given if it fits into the available space
   */
  private String modifyTextToDisplay(String text, GC gc, int availableLength) {
    // length of the text on GC taking new lines into account
    // this means the textLength is the value of the longest line
    int textLength = getLengthFromCache(gc, text);
    if (textLength > availableLength) {
      // as looking at the text length without taking new lines into
      // account we have to look at every line itself
      StringBuilder result = new StringBuilder();
      String[] lines = text.split(NEW_LINE_REGEX);
      for (String line : lines) {
        if (result.length() > 0) {
          result.append(LINE_SEPARATOR);
        }

        // now modify every line if it is longer than the available
        // space this way every line will get ... if it doesn't fit
        int lineLength = getLengthFromCache(gc, line);
        if (lineLength > availableLength) {
          int numExtraChars = 0;
          isDisplayPart = true;
          int newStringLength = line.length();
          String trialLabelText = line + DOT;
          int newTextExtent = getLengthFromCache(gc, trialLabelText);

          while (newTextExtent > availableLength + 1 && newStringLength > 0) {
            double avgWidthPerChar = (double)newTextExtent / trialLabelText.length();
            numExtraChars += 1 + (int)((newTextExtent - availableLength) / avgWidthPerChar);

            newStringLength = line.length() - numExtraChars;
            if (newStringLength > 0) {
              trialLabelText = line.substring(0, newStringLength) + DOT;
              newTextExtent = getLengthFromCache(gc, trialLabelText);
            }
          }

          if (numExtraChars > line.length()) {
            numExtraChars = line.length();
          }

          // now we have gone too short, lets add chars one at a time
          // to exceed the width...
          String testString = line;
          for (int i = 0; i < line.length(); i++) {
            testString = line.substring(0, line.length() + i - numExtraChars) + DOT;
            textLength = getLengthFromCache(gc, testString);

            if (textLength >= availableLength) {

              // now roll back one as this was the first number
              // that exceeded
              if (line.length() + i - numExtraChars < 1) {
                line = EMPTY;
              } else {
                line = line.substring(0, line.length() + i - numExtraChars - 1) + DOT;
              }
              break;
            }
          }
        } else {
          isDisplayPart = false;
        }
        result.append(line);
      }

      return result.toString();
    }
    return text;
  }

  protected String getTextToDisplay(ILayerCell cell, GC gc, int availableLength, String text) {
    StringBuilder output = new StringBuilder();
    isDisplayPart = false;
    text = text.trim();

    // take the whole width of the text
    int textLength = getLengthFromCache(gc, text);
    if (this.calculateByTextLength && this.wrapText) {
      if (availableLength < textLength) {
        // calculate length by finding the longest word in text
        textLength = (availableLength - (2 * this.spacing));

        String[] lines = text.split(NEW_LINE_REGEX);
        for (String line : lines) {
          if (output.length() > 0) {
            output.append(LINE_SEPARATOR);
          }

          String[] words = line.split("\\s"); //$NON-NLS-1$
          for (String word : words) {
            textLength = Math.max(textLength, getLengthFromCache(gc, word));
          }

          // concat the words with spaces and newlines to be always
          // smaller then available
          String computedText = ""; //$NON-NLS-1$
          for (String word : words) {
            computedText = computeTextToDisplay(computedText, word, gc, textLength);
          }
          output.append(computedText);
        }
      } else {
        output.append(text);
      }

      setNewMinLength(cell, textLength + calculatePadding(cell, availableLength) + (2 * this.spacing));
    } else if (this.calculateByTextLength && !this.wrapText) {
      output.append(modifyTextToDisplay(text, gc, textLength));

      // add padding and spacing to textLength because they are needed for
      // correct sizing
      // padding can occur on using decorators like the
      // BeveledBorderDecorator or the PaddingDecorator
      setNewMinLength(cell, textLength + calculatePadding(cell, availableLength) + (2 * this.spacing));
    } else if (!this.calculateByTextLength && this.wrapText) {
      String[] lines = text.split(NEW_LINE_REGEX);
      for (String line : lines) {
        if (output.length() > 0) {
          output.append(LINE_SEPARATOR);
        }

        String[] words = line.split("\\s"); //$NON-NLS-1$

        // concat the words with spaces and newlines
        String computedText = ""; //$NON-NLS-1$
        for (String word : words) {
          computedText = computeTextToDisplay(computedText, word, gc, availableLength);
        }

        output.append(computedText);
      }

    } else if (!this.calculateByTextLength && !this.wrapText) {
      output.append(modifyTextToDisplay(text, gc, availableLength + (2 * this.spacing)));
    }
    if(cell instanceof KiLayerCell) {
      ((KiLayerCell)cell).setDisplayPart(isDisplayPart);
    }
    return output.toString();
  }

  public boolean isDisplayPart() {
    return isDisplayPart;
  }



NattableContentTooptip

protected String getText(Event event) {
    int col = this.natTable.getColumnPositionByX(event.x);
    int row = this.natTable.getRowPositionByY(event.y);

    ILayerCell cell = natTable.getCellByPosition(col, row);
    if (cell != null) {
      
      if((cell instanceof KiLayerCell)  && ((KiLayerCell)cell).isDisplayPart()) {
        Object cellData = this.natTable.getDataValueByPosition(col, row);
        if (cellData != null) {
          return cellData.toString();
        } 
      }
    }
    return null;
  }


is there something wrong?
Re: How show the Tooltip for part display cell(xxx...) [message #1745826 is a reply to message #1745825] Tue, 18 October 2016 04:16 Go to previous messageGo to next message
neal zhang is currently offline neal zhangFriend
Messages: 45
Registered: July 2012
Member
Hi dirk,
is there a good way for " when the shown text is cutted." in NatTableContentTooltip.java
Re: How show the Tooltip for part display cell(xxx...) [message #1745830 is a reply to message #1745826] Tue, 18 October 2016 07:07 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Only if you are able to call getTextToDisplay() from the TextPainter somehow.
Re: How show the Tooltip for part display cell(xxx...) [message #1802588 is a reply to message #1745830] Tue, 12 February 2019 10:56 Go to previous messageGo to next message
Alexander Till is currently offline Alexander TillFriend
Messages: 4
Registered: August 2018
Junior Member
Hello Neal & Dirk
I came upon the same task: Display a tooltip with the full unmodified content for NatTable cells where the content was shortened and "..." was added.
Now I found it not easy to decide from within the TooltipRenderer when the content was shortened. One possibility would be to compute the available space the same was as the AbstractTextPainter does, compare it with the extend of the text value, but for this I need a GC with correct font metrics etc.
Another possibility would be to extend the TextPainter to make it add a label on a cell to mark it as "shortened" or a different kind of flag, but I am unsure about the performance impact as the NatTable is painted many times a second.

Has anyone found a good solution or is there a chance to get an API extension?
Re: How show the Tooltip for part display cell(xxx...) [message #1802592 is a reply to message #1802588] Tue, 12 February 2019 12:17 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
A good solution ... probably not ... but you could extend TextPainter and make getTextToDisplay() public (it is protected and the visibility therefore can be extended) and then call getTextToDisplay() and check on the result if it ends with ...

That a painter adds labels to the label stack is not a good idea as it would then introduce a cycling dependency as painters are typically dependent on labels and not labels on painters.
Re: How show the Tooltip for part display cell(xxx...) [message #1802607 is a reply to message #1802592] Tue, 12 February 2019 17:34 Go to previous message
Alexander Till is currently offline Alexander TillFriend
Messages: 4
Registered: August 2018
Junior Member
My problem is: where to call getTextToDisplay from. The caller (a special NatTableContentTooltip) has not the arguments GC and availableLength. Because the cellPainters form a chain of wrapping CellPainters and each CellPainter (might) reduces the clipping bounds, it is not useful to call getTextToDisplay on one of the cellPainters as the availableLength is unkown (not equal to cell.getBounds().width).
E.g. ImageCellPainter reduces the available width by the width of the image + padding.

My current solution looks like awful hack: traverse chain of wrapped cellPainters, compute pixels they clip off, compute the availableLength in the Tooltip controler, compute the text extend based on the current font metrics of the natTable and compare these two values. If the needed width >= availableLength => show tooltip.

[Updated on: Tue, 12 February 2019 17:37]

Report message to a moderator

Previous Topic:NatTableContentTooltip constructor for testing
Next Topic:NPE in SelectionLayer
Goto Forum:
  


Current Time: Thu Mar 28 18:41:43 GMT 2024

Powered by FUDForum. Page generated in 0.03657 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top