JAL-3446 from applet, JAL-3584 tooltip fixes <br> and allows different
[jalview.git] / src / jalview / gui / JvSwingUtils.java
index f89b4e4..f23dcf8 100644 (file)
@@ -47,6 +47,7 @@ import javax.swing.border.Border;
 import javax.swing.border.TitledBorder;
 
 import jalview.util.MessageManager;
+import jalview.util.Platform;
 
 /**
  * useful functions for building Swing GUIs
@@ -56,7 +57,10 @@ import jalview.util.MessageManager;
  */
 public final class JvSwingUtils
 {
-  static final String HTML_PREFIX = "<html><div style=\"width:350px;white-space:pre-wrap;margin:2px;overflow-wrap:break-word;\">";
+  static final String HTML_PREFIX = (Platform.isJS() ? 
+          "<html><div style=\"max-width:350px;overflow-wrap:break-word;display:inline-block\">"
+          : "<html><div style=\"width:350; text-align: justify; word-wrap: break-word;\">"
+            );
 
   /**
    * wrap a bare html safe string to around 60 characters per line using a CSS
@@ -89,31 +93,24 @@ public final class JvSwingUtils
     boolean hasBR = ttext.contains("<br>");
     enclose |= isHTML || hasBR;
     if (hasBR)
-    {
-      
-// Too complex in HTML5 to mix <br> with word wrapping.
-//      
-//      String[] htmllines = ttext.split("<br>");
-//      for (String line : htmllines)
-//      {
-//        maxLengthExceeded = line.length() > 60;
-//        if (maxLengthExceeded)
-//        {
-//          break;
-//        }
-//      }
+    {  
+      int pt = -1, ptlast = -4;
+      while ((pt = ttext.indexOf("<br>", pt + 1)) >= 0) {
+        if (pt - ptlast - 4 > 60) {
+          maxLengthExceeded = true;
+          break;
+        }
+      }
     }
-    else
+    else  
     {
       maxLengthExceeded = ttext.length() > 60;
     }
 
-    if (!maxLengthExceeded)
-    {
-      return enclose ? "<html>" + ttext + "</html>" : ttext;
-    }
-    // BH 2018,2019
-    return (enclose ? HTML_PREFIX + ttext + "</div></html>" : ttext);
+    String ret = (!enclose ? ttext : maxLengthExceeded ? HTML_PREFIX + ttext + "</div></html>" :
+      "<html>" + ttext + "</html>");
+    //System.out.println("JvSwUtil " + enclose + " " + maxLengthExceeded + " " + ret);
+    return ret;
   }
 
   public static JButton makeButton(String label, String tooltip,