JAL-1015 refactor tooltip html generation code to jalview.io.SequenceAnnotationReport
[jalview.git] / src / jalview / gui / SeqPanel.java
index cdaa5b9..648c3a2 100644 (file)
@@ -27,6 +27,7 @@ import javax.swing.*;
 
 import jalview.commands.*;
 import jalview.datamodel.*;
+import jalview.io.SequenceAnnotationReport;
 import jalview.schemes.*;
 import jalview.structure.*;
 
@@ -87,7 +88,7 @@ public class SeqPanel extends JPanel implements MouseListener,
   StringBuffer keyboardNo2;
 
   java.net.URL linkImageURL;
-
+  private final SequenceAnnotationReport seqARep;
   StringBuffer tooltipText = new StringBuffer("<html>");
 
   String tmpString;
@@ -107,6 +108,7 @@ public class SeqPanel extends JPanel implements MouseListener,
   public SeqPanel(AlignViewport av, AlignmentPanel ap)
   {
     linkImageURL = getClass().getResource("/images/link.gif");
+    seqARep=new SequenceAnnotationReport(linkImageURL.toString());
     ToolTipManager.sharedInstance().registerComponent(this);
     ToolTipManager.sharedInstance().setInitialDelay(0);
     ToolTipManager.sharedInstance().setDismissDelay(10000);
@@ -713,7 +715,7 @@ public class SeqPanel extends JPanel implements MouseListener,
       SequenceFeature[] features = findFeaturesAtRes(
               sequence.getDatasetSequence(),
               rpos = sequence.findPosition(res));
-      appendFeatures(tooltipText, linkImageURL.toString(), rpos, features,
+      seqARep.appendFeatures(tooltipText,  rpos, features,
               this.ap.seqPanel.seqCanvas.fr.minmax);
     }
     if (tooltipText.length() == 6) // <html></html>
@@ -759,145 +761,6 @@ public class SeqPanel extends JPanel implements MouseListener,
     return lastp = p;
   }
 
-  /**
-   * appends the features at rpos to the given stringbuffer ready for display in
-   * a tooltip
-   *
-   * @param tooltipText2
-   * @param linkImageURL
-   * @param rpos
-   * @param features
-   *          TODO refactor to Jalview 'utilities' somehow.
-   */
-  public void appendFeatures(StringBuffer tooltipText2,
-          String linkImageURL, int rpos, SequenceFeature[] features)
-  {
-    appendFeatures(tooltipText2, linkImageURL, rpos, features, null);
-  }
-
-  public void appendFeatures(StringBuffer tooltipText2, String string,
-          int rpos, SequenceFeature[] features, Hashtable minmax)
-  {
-    String tmpString;
-    if (features != null)
-    {
-      for (int i = 0; i < features.length; i++)
-      {
-        if (features[i].getType().equals("disulfide bond"))
-        {
-          if (features[i].getBegin() == rpos
-                  || features[i].getEnd() == rpos)
-          {
-            if (tooltipText2.length() > 6)
-            {
-              tooltipText2.append("<br>");
-            }
-            tooltipText2.append("disulfide bond " + features[i].getBegin()
-                    + ":" + features[i].getEnd());
-            if (features[i].links != null)
-            {
-              tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
-            }
-          }
-        }
-        else
-        {
-          if (tooltipText2.length() > 6)
-          {
-            tooltipText2.append("<br>");
-          }
-          // TODO: remove this hack to display link only features
-          boolean linkOnly = features[i].getValue("linkonly") != null;
-          if (!linkOnly)
-          {
-            tooltipText2.append(features[i].getType() + " ");
-            if (rpos != 0)
-            {
-              // we are marking a positional feature
-              tooltipText2.append(features[i].begin);
-            }
-            if (features[i].begin != features[i].end)
-            {
-              tooltipText2.append(" " + features[i].end);
-            }
-
-            if (features[i].getDescription() != null
-                    && !features[i].description.equals(features[i]
-                            .getType()))
-            {
-              tmpString = features[i].getDescription();
-              String tmp2up=tmpString.toUpperCase();
-              int startTag = tmp2up.indexOf("<HTML>");
-              if (startTag > -1)
-              {
-                tmpString = tmpString.substring(startTag + 6);
-                tmp2up = tmp2up.substring(startTag+6);
-              }
-              int endTag = tmp2up.indexOf("</BODY>");
-              if (endTag > -1)
-              {
-                tmpString = tmpString.substring(0, endTag);
-                tmp2up = tmp2up.substring(0, endTag);
-              }
-              endTag = tmp2up.indexOf("</HTML>");
-              if (endTag > -1)
-              {
-                tmpString = tmpString.substring(0, endTag);
-              }
-
-              if (startTag > -1)
-              {
-                tooltipText2.append("; " + tmpString);
-              }
-              else
-              {
-                if (tmpString.indexOf("<") > -1
-                        || tmpString.indexOf(">") > -1)
-                {
-                  // The description does not specify html is to
-                  // be used, so we must remove < > symbols
-                  tmpString = tmpString.replaceAll("<", "&lt;");
-                  tmpString = tmpString.replaceAll(">", "&gt;");
-
-                  tooltipText2.append("; ");
-                  tooltipText2.append(tmpString);
-
-                }
-                else
-                {
-                  tooltipText2.append("; " + tmpString);
-                }
-              }
-            }
-            // check score should be shown
-            if (features[i].getScore() != Float.NaN)
-            {
-              float[][] rng = (minmax == null) ? null : ((float[][]) minmax
-                      .get(features[i].getType()));
-              if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
-              {
-                tooltipText2.append(" Score=" + features[i].getScore());
-              }
-            }
-            if (features[i].getValue("status") != null)
-            {
-              String status = features[i].getValue("status").toString();
-              if (status.length() > 0)
-              {
-                tooltipText2.append("; (" + features[i].getValue("status")
-                        + ")");
-              }
-            }
-          }
-          if (features[i].links != null)
-          {
-            tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
-          }
-
-        }
-      }
-    }
-  }
 
   String lastTooltip;