JAL-4047 - proof of concept for JAL-4048 - display columns of info in sequence ID...
[jalview.git] / src / jalview / gui / IdPanel.java
index 6b312ad..fdca723 100755 (executable)
  */
 package jalview.gui;
 
-import jalview.datamodel.AlignmentAnnotation;
-import jalview.datamodel.Sequence;
-import jalview.datamodel.SequenceFeature;
-import jalview.datamodel.SequenceGroup;
-import jalview.datamodel.SequenceI;
-import jalview.gui.SeqPanel.MousePos;
-import jalview.io.SequenceAnnotationReport;
-import jalview.util.MessageManager;
-import jalview.util.Platform;
-import jalview.viewmodel.AlignmentViewport;
-import jalview.viewmodel.ViewportRanges;
-
 import java.awt.BorderLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
@@ -48,6 +36,20 @@ import javax.swing.SwingUtilities;
 import javax.swing.Timer;
 import javax.swing.ToolTipManager;
 
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.Sequence;
+import jalview.datamodel.SequenceGroup;
+import jalview.datamodel.SequenceI;
+import jalview.gui.SeqPanel.MousePos;
+import jalview.io.SequenceAnnotationReport;
+import jalview.util.MessageManager;
+import jalview.util.Platform;
+import jalview.viewmodel.AlignmentViewport;
+import jalview.viewmodel.ViewportRanges;
+import jalview.viewmodel.seqfeatures.IdColumn;
+import jalview.viewmodel.seqfeatures.IdColumns;
+import jalview.viewmodel.seqfeatures.IdColumns.ColumnCell;
+
 /**
  * This panel hosts alignment sequence ids and responds to mouse clicks on them,
  * as well as highlighting ids matched by a search from the Find menu.
@@ -66,8 +68,6 @@ public class IdPanel extends JPanel
 
   ScrollThread scrollThread = null;
 
-  String linkImageURL;
-
   int offy;
 
   // int width;
@@ -88,8 +88,7 @@ public class IdPanel extends JPanel
     this.av = av;
     alignPanel = parent;
     setIdCanvas(new IdCanvas(av));
-    linkImageURL = getClass().getResource("/images/link.gif").toString();
-    seqAnnotReport = new SequenceAnnotationReport(linkImageURL);
+    seqAnnotReport = new SequenceAnnotationReport(true);
     setLayout(new BorderLayout());
     add(getIdCanvas(), BorderLayout.CENTER);
     addMouseListener(this);
@@ -129,19 +128,61 @@ public class IdPanel extends JPanel
         SequenceI sequence = av.getAlignment().getSequenceAt(seq);
         StringBuilder tip = new StringBuilder(64);
         tip.append(sequence.getDisplayId(true)).append(" ");
-        seqAnnotReport.createTooltipAnnotationReport(tip, sequence,
-                av.isShowDBRefs(), av.isShowNPFeats(), sp.seqCanvas.fr);
+        IdColumn col = locateColumnFor(e);
+        if (col != null)
+        {
+          // tooltip for column
+          tip.append(getTooltipFor(col, sequence));
+        }
+        else
+        {
+          seqAnnotReport.createTooltipAnnotationReport(tip, sequence,
+                  av.isShowDBRefs(), av.isShowNPFeats(), sp.seqCanvas.fr);
+        }
         setToolTipText(JvSwingUtils.wrapTooltip(true, tip.toString()));
 
         StringBuilder text = new StringBuilder();
         text.append("Sequence ").append(String.valueOf(seq + 1))
-                .append(" ID: ")
-                .append(sequence.getName());
+                .append(" ID: ").append(sequence.getName());
         alignPanel.alignFrame.setStatus(text.toString());
       }
     }
   }
 
+  private Object getTooltipFor(IdColumn col, SequenceI seq)
+  {
+    ColumnCell cell = av.getIdColumns().getCellFor(seq, col);
+    if (cell != null)
+    {
+      return "" + col.getLabel() + ": " + cell.label;
+    }
+    return "";
+  }
+
+  private IdColumn locateColumnFor(MouseEvent e)
+  {
+    // TODO COMBINE SAME CODE IN IDCANVAS!!!
+
+    IdColumns id_cols = av.getIdColumns();
+    List<IdColumn> visible = id_cols.getVisible();
+    /**
+     * width of an idColumn
+     */
+    int colWid = 20;
+    int panelWidth = Math.max(idCanvas.getWidth() / 2,
+            idCanvas.getWidth() - (colWid * visible.size()));
+    int p = 0;
+    while (panelWidth < idCanvas.getWidth() && p < visible.size())
+    {
+
+      if (e.getX() >= panelWidth && e.getX() < panelWidth + colWid)
+        return visible.get(p);
+      p++;
+      panelWidth += colWid;
+    }
+    return null;
+  }
+
   /**
    * Responds to a mouse drag by selecting the sequences under the dragged
    * region.
@@ -382,7 +423,7 @@ public class IdPanel extends JPanel
     }
 
     MousePos pos = alignPanel.getSeqPanel().findMousePosition(e);
-    
+
     if (e.isPopupTrigger()) // Mac reports this in mousePressed
     {
       showPopupMenu(e, pos);
@@ -436,28 +477,12 @@ public class IdPanel extends JPanel
     }
 
     Sequence sq = (Sequence) av.getAlignment().getSequenceAt(pos.seqIndex);
-
-    /*
-     *  build a new links menu based on the current links
-     *  and any non-positional features
-     */
-    List<SequenceFeature> features = null;
     if (sq != null)
     {
-    List<String> nlinks = Preferences.sequenceUrlLinks.getLinksForMenu();
-      features = sq.getFeatures().getNonPositionalFeatures();
-    for (SequenceFeature sf : features)
-    {
-      if (sf.links != null)
-      {
-        nlinks.addAll(sf.links);
-      }
+      PopupMenu pop = new PopupMenu(alignPanel, sq,
+              Preferences.getGroupURLLinks());
+      pop.show(this, e.getX(), e.getY());
     }
-    }
-
-    PopupMenu pop = new PopupMenu(alignPanel, sq, features,
-            Preferences.getGroupURLLinks());
-    pop.show(this, e.getX(), e.getY());
   }
 
   /**