normalised logo rendering (JAL-958)
[jalview.git] / src / jalview / gui / AnnotationLabels.java
index b8c81ce..7967ae6 100755 (executable)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
- * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
+ * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
  * 
  * This file is part of Jalview.
  * 
@@ -18,6 +18,7 @@
 package jalview.gui;
 
 import java.util.*;
+import java.util.regex.Pattern;
 
 import java.awt.*;
 import java.awt.datatransfer.*;
@@ -58,7 +59,7 @@ public class AnnotationLabels extends JPanel implements MouseListener,
   Image image;
 
   AlignmentPanel ap;
-  
+
   AlignViewport av;
 
   boolean resizing = false;
@@ -404,22 +405,43 @@ public class AnnotationLabels extends JPanel implements MouseListener,
             && ap.av.alignment.getAlignmentAnnotation().length > selectedRow)
     {
       AlignmentAnnotation aa = ap.av.alignment.getAlignmentAnnotation()[selectedRow];
-
-      StringBuffer desc = new StringBuffer("<html>");
-
+      
+      StringBuffer desc = new StringBuffer();
       if (aa.description != null
               && !aa.description.equals("New description"))
       {
-        desc.append(aa.getDescription(true));
-        if (aa.hasScore)
-          desc.append("<br>");
+        // TODO: we could refactor and merge this code with the code in jalview.gui.SeqPanel.mouseMoved(..) that formats sequence feature tooltips
+        desc.append(aa.getDescription(true).trim());
+        // check to see if the description is an html fragment.
+        if (desc.length()<6 || (desc.substring(0,6).toLowerCase().indexOf("<html>")<0))
+        {
+          // clean the description ready for embedding in html
+          desc = new StringBuffer(Pattern.compile("<").matcher(desc).replaceAll("&lt;"));        
+          desc.insert(0, "<html>");
+        } else {
+               // remove terminating html if any
+               int i=desc.substring(desc.length()-7).toLowerCase().lastIndexOf("</html>");
+               if (i>-1) {
+                 desc.setLength(desc.length()-7+i);
+               }
+        }
+        if (aa.hasScore())
+        {
+          desc.append("<br/>");
+        }
+        
+        
+      } else {
+        // begin the tooltip's html fragment
+        desc.append("<html>");
       }
       if (aa.hasScore())
       {
-        desc.append("Score: " + aa.score);
+        // TODO: limit precision of score to avoid noise from imprecise doubles (64.7 becomes 64.7+/some tiny value).
+        desc.append(" Score: " + aa.score);
       }
 
-      if (desc.length() != 6)
+      if (desc.length() > 6)
       {
         desc.append("</html>");
         this.setToolTipText(desc.toString());
@@ -439,7 +461,6 @@ public class AnnotationLabels extends JPanel implements MouseListener,
   public void mouseClicked(MouseEvent evt)
   {
     AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
-        
     if (SwingUtilities.isLeftMouseButton(evt))
     {
       if (selectedRow > -1 && selectedRow < aa.length)
@@ -495,7 +516,6 @@ public class AnnotationLabels extends JPanel implements MouseListener,
     JMenuItem item = new JMenuItem(ADDNEW);
     item.addActionListener(this);
     pop.add(item);
-        
     if (selectedRow < 0)
     {
       if (hasHiddenRows)
@@ -610,6 +630,28 @@ public class AnnotationLabels extends JPanel implements MouseListener,
             }
           });
           pop.add(cprofl);
+          final JCheckBoxMenuItem cproflnorm = new JCheckBoxMenuItem(
+                  "Normalise Group Logo",
+                  aa[selectedRow].groupRef.isNormaliseSequenceLogo());
+          cproflnorm.addActionListener(new ActionListener()
+          {
+            public void actionPerformed(ActionEvent e)
+            {
+              
+              // TODO: pass on reference
+              // to ap
+              // so the
+              // view
+              // can be
+              // updated.
+              aaa.groupRef.setNormaliseSequenceLogo(cproflnorm.getState());
+              // automatically enable logo display if we're clicked
+              aaa.groupRef.setshowSequenceLogo(true);
+              ap.repaint();
+              // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
+            }
+          });
+          pop.add(cproflnorm);
         }
         else
         {
@@ -626,6 +668,7 @@ public class AnnotationLabels extends JPanel implements MouseListener,
               // can be
               // updated.
               av.setShowConsensusHistogram(chist.getState());
+              ap.alignFrame.setMenusForViewport();
               ap.repaint();
               // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
             }
@@ -644,11 +687,32 @@ public class AnnotationLabels extends JPanel implements MouseListener,
               // can be
               // updated.
               av.setShowSequenceLogo(cprof.getState());
+              ap.alignFrame.setMenusForViewport();
               ap.repaint();
               // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
             }
           });
           pop.add(cprof);
+          final JCheckBoxMenuItem cprofnorm = new JCheckBoxMenuItem(
+                  "Normalise Logo", av.isNormaliseSequenceLogo());
+          cprofnorm.addActionListener(new ActionListener()
+          {
+            public void actionPerformed(ActionEvent e)
+            {
+              // TODO: pass on reference
+              // to ap
+              // so the
+              // view
+              // can be
+              // updated.
+              av.setShowSequenceLogo(true);
+              av.setNormaliseSequenceLogo(cprofnorm.getState());
+              ap.alignFrame.setMenusForViewport();
+              ap.repaint();
+              // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
+            }
+          });
+          pop.add(cprofnorm);
         }
         final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
         consclipbrd.addActionListener(this);