JAL-3446 from applet, JAL-3584 tooltip fixes <br> and allows different
[jalview.git] / src / jalview / gui / JvSwingUtils.java
index 7a5dde6..c561457 100644 (file)
@@ -48,6 +48,9 @@ import javax.swing.SwingConstants;
 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,6 +59,12 @@ import javax.swing.border.TitledBorder;
  */
 public final class JvSwingUtils
 {
+  // JBPMerge note: Old Non-JS prefix
+  // 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 = "<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
    * style class specifying word-wrap and break-word
@@ -72,33 +81,37 @@ public final class JvSwingUtils
             "Tootip text to format must not be null!");
     ttext = ttext.trim();
     boolean maxLengthExceeded = false;
-
-    if (ttext.contains("<br>"))
+    boolean isHTML = ttext.startsWith("<html>");
+    if (isHTML)
     {
-      String[] htmllines = ttext.split("<br>");
-      for (String line : htmllines)
-      {
-        maxLengthExceeded = line.length() > 60;
-        if (maxLengthExceeded)
-        {
+      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)
+    {  
+      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;
-    }
-
-    return (enclose ? "<html>" : "")
-            + "<style> p.ttip {width: 350; text-align: left; word-wrap: break-word;}</style><p class=\"ttip\">"
-            + ttext + "</p>" + ((enclose ? "</html>" : ""));
-
+    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,