|
|
Re: How show the Tooltip for part display cell(xxx...) [message #1745825 is a reply to message #1745802] |
Tue, 18 October 2016 04:11 |
neal zhang 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 #1802607 is a reply to message #1802592] |
Tue, 12 February 2019 17:34 |
Alexander Till 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
|
|
|
Powered by
FUDForum. Page generated in 0.06119 seconds