- <br> indicates no additional line breaks.
- removal of unrealistic tests ("false" tests of jws2Discoverer HTML
messages)
- accepts either <br> or <br/> (as does JLabel)
*/
public final class JvSwingUtils
{
+ static final String HTML_PREFIX = "<html><div style=\"width:350px;white-space:pre-wrap;margin:2px;overflow-wrap:break-word;\">";
+
/**
* wrap a bare html safe string to around 60 characters per line using a CSS
* style class specifying word-wrap and break-word
*
* @param enclose
- * if true, add <html> wrapper tags
+ * if true, add <html> wrapper tags (currently false for only
+ * two references -- both in Jws2Discoverer --
* @param ttext
*
* @return
{
Objects.requireNonNull(ttext,
"Tootip text to format must not be null!");
- ttext = ttext.trim();
- boolean maxLengthExceeded = false;
+ ttext = ttext.trim().replaceAll("<br/>", "<br>");
- if (ttext.contains("<br>"))
+ boolean maxLengthExceeded = false;
+ boolean isHTML = ttext.startsWith("<html>");
+ if (isHTML)
+ {
+ ttext = ttext.substring(6);
+ }
+ if (ttext.endsWith("</html>"))
+ {
+ isHTML = true;
+ ttext = ttext.substring(0, ttext.length() - 7);
+ }
+ boolean hasBR = ttext.contains("<br>");
+ enclose |= isHTML || hasBR;
+ if (hasBR)
{
- maxLengthExceeded = false;
+
+// Too complex in HTML5 to mix <br> with word wrapping.
+//
// String[] htmllines = ttext.split("<br>");
// for (String line : htmllines)
// {
{
return enclose ? "<html>" + ttext + "</html>" : ttext;
}
-
- return (enclose ? "<html>" : "")
- // BH 2018
- + "<style> div.ttip {width:350px;white-space:pre-wrap;margin:2px;overflow-wrap:break-word;}</style><div class=\"ttip\">"
-// + "<style> p.ttip {width:350px;margin:-14px 0px -14px 0px;padding:2px;overflow-wrap:break-word;}"
-// + "</style><p class=\"ttip\">"
- + ttext
- + " </div>"
-// + "</p>"
- + ((enclose ? "</html>" : ""));
+ // BH 2018,2019
+ return (enclose ? HTML_PREFIX + ttext + "</div></html>" : ttext);
}
public static JButton makeButton(String label, String tooltip,
menuItem.setEnabled(true);
for (String calcId : tipEntries.keySet())
{
- tooltip.append("<br/>" + calcId + "/" + tipEntries.get(calcId));
+ tooltip.append("<br>" + calcId + "/" + tipEntries.get(calcId));
}
String tooltipText = JvSwingUtils.wrapTooltip(true,
tooltip.toString());
JvSwingUtils.wrapTooltip(true, tip));
tip = "0123456789012345678901234567890123456789012345678901234567890"; // 61
- assertFalse(tip.equals(JvSwingUtils.wrapTooltip(false, tip)));
+
+ // n/a -- message is too long for "false"
+//
+// assertFalse(tip.equals(JvSwingUtils.wrapTooltip(false, tip)));
+//
+
assertFalse(("<html>" + tip + "</html>").equals(JvSwingUtils
.wrapTooltip(true, tip)));
}
public void testWrapTooltip_multilineShortText()
{
String tip = "Now is the winter of our discontent<br>Made glorious summer by this sun of York";
- assertEquals(tip, JvSwingUtils.wrapTooltip(false, tip));
- assertEquals("<html>" + tip + "</html>",
- JvSwingUtils.wrapTooltip(true, tip));
+ String tip2 = "Now is the winter of our discontent<br/>Made glorious summer by this sun of York";
+
+// BH not applicable in Jalview; "false" is only for when no <br> and only for short j2s2Discover messages
+//
+// String s = JvSwingUtils.wrapTooltip(false, tip);
+// System.out.println("<html>" + tip + "</html>");
+// assertEquals(tip, s);
+
+ String s;
+ s = JvSwingUtils.wrapTooltip(true, tip);
+ System.out.println(s);
+ assertEquals("<html>" + tip + "</html>", s);
+ s = JvSwingUtils.wrapTooltip(true, tip2);
+ System.out.println(s);
+ assertEquals("<html>" + tip + "</html>", s);
}
/**
@Test(groups = { "Functional" })
public void testWrapTooltip_longText()
{
+ // BH should work in Java and JavaScript
String tip = "Now is the winter of our discontent made glorious summer by this sun of York";
- String expected = "<style> div.ttip {width:350px;white-space:pre-wrap;padding:2px;overflow-wrap:break-word;}</style>"
- + "<div class=\"ttip\">" + tip + " </div>";
- assertEquals("<html>" + expected + "</html>",
- JvSwingUtils.wrapTooltip(true, tip));
- assertEquals(expected, JvSwingUtils.wrapTooltip(false, tip));
+ String expected = JvSwingUtils.HTML_PREFIX + tip + "</div></html>";
+ String s = JvSwingUtils.wrapTooltip(true, tip);
+ assertEquals(expected, s);
+ // BH not applicable in Jalview; "false" is only for when no <br> and only for short j2s2Discover messages
+// s = JvSwingUtils.wrapTooltip(false, tip);
+// assertEquals(expected, s);
}
}
testee.configureReferenceAnnotationsMenu(menu, seqs);
assertTrue(menu.isEnabled());
String s = MessageManager.getString("label.add_annotations_for");
- String expected = "<html><style> div.ttip {width:350px;white-space:pre-wrap;padding:2px;overflow-wrap:break-word;}</style>"
- + "<div class=\"ttip\">" + s
- + "<br/>Jmol/secondary structure<br/>PDB/Temp </div></html>";
+// String expected = "<html><style> div.ttip {width:350px;white-space:pre-wrap;padding:2px;overflow-wrap:break-word;}</style>"
+// + "<div class=\"ttip\">" + s
+// + "<br/>Jmol/secondary structure<br/>PDB/Temp </div></html>";
+ String expected = "<html>" + s + "<br>Jmol/secondary structure<br>PDB/Temp</html>";
assertEquals(expected, menu.getToolTipText());
}
testee.configureReferenceAnnotationsMenu(menu, seqs);
assertTrue(menu.isEnabled());
String s = MessageManager.getString("label.add_annotations_for");
- String expected = "<html><style> div.ttip {width:350px;white-space:pre-wrap;padding:2px;overflow-wrap:break-word;}</style>"
- + "<div class=\"ttip\">" + s
- + "<br/>Jmol/secondary structure<br/>PDB/Temp </div></html>";
- assertEquals(expected, menu.getToolTipText());
+// String expected = "<html><style> div.ttip {width:350px;white-space:pre-wrap;padding:2px;overflow-wrap:break-word;}</style>"
+// + "<div class=\"ttip\">" + s
+// + "<br/>Jmol/secondary structure<br/>PDB/Temp</html>";
+ String expected = "<html>" + s
+ + "<br>Jmol/secondary structure<br>PDB/Temp</html>";
+ s = menu.getToolTipText();
+ assertEquals(expected, s);
}
/**