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: 2624
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 message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2624
Registered: July 2012
Senior Member
Only if you are able to call getTextToDisplay() from the TextPainter somehow.
Previous Topic:Traversal and MultiLineTextCellEditor
Next Topic:Display column header diagonally
Goto Forum:
  


Current Time: Wed Nov 14 23:30:36 GMT 2018

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

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

Back to the top