Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions table/src/main/uk/ac/starlink/table/gui/CustomLabelUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.awt.FontMetrics;
import java.awt.Rectangle;
import java.util.regex.Pattern;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JLabel;
Expand All @@ -23,6 +24,8 @@
public class CustomLabelUI extends BasicLabelUI {
private static final String ELLIPSES = "...";
private static final int ELLIPSES_LEN = ELLIPSES.length();
private static final Pattern EMBEDDED_WHITESPACE =
Pattern.compile( "[^\\s]\\s+[^\\s]" );
private String displayedText_;

/**
Expand Down Expand Up @@ -71,12 +74,12 @@ protected String layoutCL( JLabel label, FontMetrics fontMetrics,
return displayedText_;
}


/* Check if a truncation occurred, and if the text is long enough. If
* the text is too short, there is no point in truncating differently.*/
int displayedLength = displayedText_.length();
if ( !displayedText_.equals( text ) &&
displayedLength > 2 * ELLIPSES_LEN ) {
displayedLength > 2 * ELLIPSES_LEN &&
isCentralElisionPreferred( text ) ) {
int splitLength =
( displayedLength + ELLIPSES_LEN ) / 2 - ELLIPSES_LEN;
String beforeEllipses = displayedText_.substring( 0, splitLength );
Expand All @@ -94,4 +97,23 @@ protected String layoutCL( JLabel label, FontMetrics fontMetrics,

return displayedText_;
}

/**
* Indicates whether text should be truncated by eliding text in the
* middle, rather than at the end.
*
* @param txt text to test
* @return true if central elision is preferred
*/
private boolean isCentralElisionPreferred( String txt ) {
if ( txt == null ||
txt.length() == 0 ||
txt.endsWith( ELLIPSES ) ||
EMBEDDED_WHITESPACE.matcher( txt ).find() ) {
return false;
}
else {
return true;
}
}
}
4 changes: 2 additions & 2 deletions topcat/src/docs/sun253.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34926,8 +34926,8 @@ introduced since the last version:
<ref id="form-histogram">histogram</ref> plots.</li>
<li>Overlong text and array fields in the
<ref id="TableViewerWindow">Data Window</ref>
are now displayed with an ellipsis (...) in the middle
rather than at the end.</li>
are now in some cases displayed with an ellipsis (...)
in the middle rather than at the end.</li>
<li>Functions
<ref id="Coverage-uniqToOrder"><code>uniqToOrder</code></ref> and
<ref id="Coverage-uniqToIndex"><code>uniqToIndex</code></ref>
Expand Down
4 changes: 2 additions & 2 deletions ttools/src/docs/sun256.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16035,8 +16035,8 @@ eds. C. Gabriel et al., ASP Conf. Ser. 351, p. 666 (2006)
<ref id="layer-histogram">histogram</ref> plots.</li>
<li>Overlong text and array fields in the
<ref id="mode-gui"><code>gui</code></ref> output mode table
window are now displayed with an ellipsis (...) in the middle
rather than at the end.</li>
window are now in some cases displayed with an ellipsis (...)
in the middle rather than at the end.</li>
<li>Functions
<ref id="Coverage-uniqToOrder"><code>uniqToOrder</code></ref> and
<ref id="Coverage-uniqToIndex"><code>uniqToIndex</code></ref>
Expand Down