JAL-3725 restrict mapped virtual feature location to mapped region
[jalview.git] / src / jalview / gui / PopupMenu.java
index 415054e..abe9835 100644 (file)
  */
 package jalview.gui;
 
+import java.awt.Color;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Hashtable;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.SortedMap;
+import java.util.TreeMap;
+import java.util.Vector;
+
+import javax.swing.ButtonGroup;
+import javax.swing.JCheckBoxMenuItem;
+import javax.swing.JColorChooser;
+import javax.swing.JMenu;
+import javax.swing.JMenuItem;
+import javax.swing.JPopupMenu;
+import javax.swing.JRadioButtonMenuItem;
+
 import jalview.analysis.AAFrequency;
 import jalview.analysis.AlignmentAnnotationUtils;
 import jalview.analysis.AlignmentUtils;
@@ -56,31 +81,6 @@ import jalview.util.StringUtils;
 import jalview.util.UrlLink;
 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
 
-import java.awt.Color;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.BitSet;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Hashtable;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.SortedMap;
-import java.util.TreeMap;
-import java.util.Vector;
-
-import javax.swing.ButtonGroup;
-import javax.swing.JCheckBoxMenuItem;
-import javax.swing.JColorChooser;
-import javax.swing.JMenu;
-import javax.swing.JMenuItem;
-import javax.swing.JPopupMenu;
-import javax.swing.JRadioButtonMenuItem;
-
 /**
  * The popup menu that is displayed on right-click on a sequence id, or in the
  * sequence alignment.
@@ -88,6 +88,11 @@ import javax.swing.JRadioButtonMenuItem;
 public class PopupMenu extends JPopupMenu implements ColourChangeListener
 {
   /*
+   * maximum length of feature description to include in popup menu item text
+   */
+  private static final int FEATURE_DESC_MAX = 40;
+
+  /*
    * true for ID Panel menu, false for alignment panel menu
    */
   private final boolean forIdPanel;
@@ -750,14 +755,16 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener
   }
 
   /**
-   * Add a link to show feature details for each sequence feature
+   * Add a menu item to show feature details for each sequence feature. Any
+   * linked 'virtual' features (CDS/protein) are also optionally found and
+   * included.
    * 
    * @param features
-   * @param column
    * @param seq
+   * @param column
    */
   protected void addFeatureDetails(List<SequenceFeature> features,
-          SequenceI seq, int column)
+          final SequenceI seq, final int column)
   {
     /*
      * add features in CDS/protein complement at the corresponding
@@ -792,88 +799,112 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener
     String name = seq.getName();
     for (final SequenceFeature sf : features)
     {
-      addFeatureDetailsMenuItem(details, name, sf);
+      addFeatureDetailsMenuItem(details, name, sf, null);
     }
 
     if (mf != null)
     {
-      name = mf.fromSeq == seq ? mf.mapping.getTo().getName()
-              : mf.fromSeq.getName();
       for (final SequenceFeature sf : mf.features)
       {
-        addFeatureDetailsMenuItem(details, name, sf);
+        addFeatureDetailsMenuItem(details, name, sf, mf);
       }
     }
   }
 
   /**
-   * A helper method to add one menu item whose action is to show details for one
-   * feature
+   * A helper method to add one menu item whose action is to show details for
+   * one feature. The menu text includes feature description, but this may be
+   * truncated.
    * 
    * @param details
    * @param seqName
    * @param sf
+   * @param mf
    */
   void addFeatureDetailsMenuItem(JMenu details, final String seqName,
-          final SequenceFeature sf)
+          final SequenceFeature sf, MappedFeatures mf)
   {
     int start = sf.getBegin();
     int end = sf.getEnd();
-    String desc = null;
-    if (start == end)
+    if (mf != null)
     {
-      desc = String.format("%s %d", sf.getType(), start);
+      /*
+       * show local rather than linked feature coordinates
+       */
+      int[] beginRange = mf.getMappedPositions(start, start);
+      int[] endRange = mf.getMappedPositions(end, end);
+      if (beginRange == null || endRange == null)
+      {
+        // e.g. variant extending to stop codon so not mappable
+        return;
+      }
+      start = beginRange[0];
+      end = endRange[endRange.length - 1];
+      int[] localRange = mf.getMappedPositions(start, end);
+      if (localRange == null)
+      {
+        return;
+      }
+      start = localRange[0];
+      end = localRange[localRange.length - 1];
     }
-    else
+    StringBuilder desc = new StringBuilder();
+    desc.append(sf.getType()).append(" ").append(String.valueOf(start));
+    if (start != end)
     {
-      desc = String.format("%s %d-%d", sf.getType(), start, end);
+      desc.append(sf.isContactFeature() ? ":" : "-");
+      desc.append(String.valueOf(end));
     }
-    String tooltip = desc;
     String description = sf.getDescription();
     if (description != null)
     {
+      desc.append(" ");
       description = StringUtils.stripHtmlTags(description);
-      if (description.length() > 12)
-      {
-        desc = desc + " " + description.substring(0, 12) + "..";
-      }
-      else
+
+      /*
+       * truncate overlong descriptions unless they contain an href
+       * (as truncation could leave corrupted html)
+       */
+      boolean hasLink = description.indexOf("a href") > -1;
+      if (description.length() > FEATURE_DESC_MAX && !hasLink)
       {
-        desc = desc + " " + description;
+        description = description.substring(0, FEATURE_DESC_MAX) + "...";
       }
-      tooltip = tooltip + " " + description;
+      desc.append(description);
     }
-    if (sf.getFeatureGroup() != null)
+    String featureGroup = sf.getFeatureGroup();
+    if (featureGroup != null)
     {
-      tooltip = tooltip + (" (" + sf.getFeatureGroup() + ")");
+      desc.append(" (").append(featureGroup).append(")");
     }
-    JMenuItem item = new JMenuItem(desc);
-    item.setToolTipText(tooltip);
+    String htmlText = JvSwingUtils.wrapTooltip(true, desc.toString());
+    JMenuItem item = new JMenuItem(htmlText);
     item.addActionListener(new ActionListener()
     {
       @Override
       public void actionPerformed(ActionEvent e)
       {
-        showFeatureDetails(seqName, sf);
+        showFeatureDetails(sf, seqName, mf);
       }
     });
     details.add(item);
   }
 
   /**
-   * Opens a panel showing a text report of feature dteails
-   * 
-   * @param seqName
+   * Opens a panel showing a text report of feature details
    * 
    * @param sf
+   * @param seqName
+   * @param mf
    */
-  protected void showFeatureDetails(String seqName, SequenceFeature sf)
+  protected void showFeatureDetails(SequenceFeature sf, String seqName,
+          MappedFeatures mf)
   {
     CutAndPasteHtmlTransfer cap = new CutAndPasteHtmlTransfer();
     // it appears Java's CSS does not support border-collapse :-(
     cap.addStylesheetRule("table { border-collapse: collapse;}");
     cap.addStylesheetRule("table, td, th {border: 1px solid black;}");
-    cap.setText(sf.getDetailsReport(seqName));
+    cap.setText(sf.getDetailsReport(seqName, mf));
 
     Desktop.addInternalFrame(cap,
             MessageManager.getString("label.feature_details"), 500, 500);
@@ -1757,7 +1788,7 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener
               "label.create_sequence_details_report_annotation_for",
               new Object[]
               { seq.getDisplayId(true) }) + "</h2></p><p>");
-      new SequenceAnnotationReport(null).createSequenceAnnotationReport(
+      new SequenceAnnotationReport(false).createSequenceAnnotationReport(
               contents, seq, true, true, ap.getSeqPanel().seqCanvas.fr);
       contents.append("</p>");
     }