Merge branch 'develop' into feature/JAL-3390hideUnmappedStructure
[jalview.git] / src / jalview / gui / StructureViewerBase.java
index 33a122c..900252a 100644 (file)
@@ -33,6 +33,7 @@ import java.io.FileReader;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.Random;
 import java.util.Vector;
@@ -75,6 +76,8 @@ import jalview.ws.dbsources.Pdb;
 public abstract class StructureViewerBase extends GStructureViewer
         implements Runnable, ViewSetProvider
 {
+  private static final String UNMAPPED = "(unmapped)";
+
   /*
    * names for colour options (additional to Jalview colour schemes)
    */
@@ -171,9 +174,9 @@ public abstract class StructureViewerBase extends GStructureViewer
   }
 
   @Override
-  public boolean isUsedForColourBy(AlignmentViewPanel ap2)
+  public boolean isUsedForColourBy(AlignmentViewPanel avp)
   {
-    return (_colourwith != null) && _colourwith.contains(ap2);
+    return (_colourwith != null) && _colourwith.contains(avp);
   }
 
   /**
@@ -517,6 +520,10 @@ public abstract class StructureViewerBase extends GStructureViewer
     {
       return;
     }
+
+    /*
+     * add the 'All' menu item
+     */
     JMenuItem menuItem = new JMenuItem(
             MessageManager.getString("label.all"));
     menuItem.addActionListener(new ActionListener()
@@ -536,12 +543,27 @@ public abstract class StructureViewerBase extends GStructureViewer
         allChainsSelected = false;
       }
     });
-
     chainMenu.add(menuItem);
 
+    /*
+     * add a menu item for each structure and chain
+     */
+    Collections.sort(chainNames);
     for (String chain : chainNames)
     {
-      menuItem = new JCheckBoxMenuItem(chain, true);
+      String seqName = getSequenceNameForChain(chain);
+      if (seqName == null)
+      {
+        seqName = UNMAPPED;
+      }
+      int nameLength = seqName.length();
+      if (nameLength > 16)
+      {
+        seqName = seqName.substring(0, 8) + "..."
+                + seqName.substring(nameLength - 8, nameLength);
+      }
+      String text = chain + " " + seqName;
+      menuItem = new JCheckBoxMenuItem(text, true);
       menuItem.addItemListener(new ItemListener()
       {
         @Override
@@ -559,6 +581,26 @@ public abstract class StructureViewerBase extends GStructureViewer
   }
 
   /**
+   * Answers the name of the sequence mapped to the given chain (formatted as
+   * pdbId:chainId, e.g. 1A70:A). Answers null if no mapped sequence is found. If
+   * more than one sequence is matched, just answers the name of the first one
+   * found.
+   * 
+   * @param chain
+   * @return
+   */
+  private String getSequenceNameForChain(String chain)
+  {
+    String[] tokens = chain.split(":");
+    String pdbId = tokens[0];
+    String chainId = tokens[1];
+    List<StructureMapping> mappings = getBinding().getSsm()
+            .getMappingForChain(pdbId, chainId);
+    return mappings.isEmpty() ? null
+            : mappings.get(0).getSequence().getName();
+  }
+
+  /**
    * Action on selecting one of Jalview's registered colour schemes
    */
   @Override
@@ -714,6 +756,37 @@ public abstract class StructureViewerBase extends GStructureViewer
             });
     viewMenu.add(seqColourBy);
 
+    showAlignmentOnly = new JCheckBoxMenuItem(
+            MessageManager.getString("label.show_alignment_only"));
+    showAlignmentOnly.addActionListener(new ActionListener()
+    {
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        hideHiddenRegions.setEnabled(showAlignmentOnly.isSelected());
+        getBinding().setShowAlignmentOnly(showAlignmentOnly.isSelected());
+        getBinding().showStructures(getAlignmentPanel().getAlignViewport(),
+                true);
+      }
+    });
+    viewMenu.add(showAlignmentOnly);
+
+    hideHiddenRegions = new JCheckBoxMenuItem(
+            MessageManager.getString("label.hide_hidden_regions"));
+    hideHiddenRegions.setEnabled(false);
+    hideHiddenRegions.addActionListener(new ActionListener()
+    {
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        getBinding().setHideHiddenRegions(hideHiddenRegions.isSelected());
+        getBinding().showStructures(getAlignmentPanel().getAlignViewport(),
+                false);
+      }
+    });
+    viewMenu.add(hideHiddenRegions);
+    viewMenu.add(fitToWindow);
+
     final ItemListener handler = new ItemListener()
     {
       @Override
@@ -794,12 +867,12 @@ public abstract class StructureViewerBase extends GStructureViewer
       }
     } catch (Exception e)
     {
-      StringBuffer sp = new StringBuffer();
+      StringBuilder sb = new StringBuilder();
       for (AlignmentViewPanel alignPanel : _alignwith)
       {
-        sp.append("'" + alignPanel.getViewName() + "' ");
+        sb.append("'").append(alignPanel.getViewName()).append("' ");
       }
-      Cache.log.info("Couldn't align structures with the " + sp.toString()
+      Cache.log.info("Couldn't align structures with the " + sb.toString()
               + "associated alignment panels.", e);
     }
     return reply;
@@ -868,9 +941,9 @@ public abstract class StructureViewerBase extends GStructureViewer
         }
       }
       // Set the colour using the current view for the associated alignframe
-      for (AlignmentViewPanel alignPanel : _colourwith)
+      for (AlignmentViewPanel avp : _colourwith)
       {
-        binding.colourBySequence(alignPanel);
+        binding.updateStructureColours(avp);
       }
       seqColoursApplied = true;
     }
@@ -1045,6 +1118,23 @@ public abstract class StructureViewerBase extends GStructureViewer
   }
 
   @Override
+  public abstract AAStructureBindingModel getBinding();
+
+  /**
+   * Show only the selected chain(s) in the viewer
+   */
+  protected void showSelectedChains()
+  {
+    setChainsToHide();
+  
+    /*
+     * refresh display without resizing - easier to see what changed
+     */
+    getBinding().showStructures(getAlignmentPanel().getAlignViewport(),
+            false);
+  }
+
+  @Override
   public long startProgressBar(String msg)
   {
     // TODO would rather have startProgress/stopProgress as the
@@ -1091,26 +1181,6 @@ public abstract class StructureViewerBase extends GStructureViewer
   }
 
   /**
-   * Show only the selected chain(s) in the viewer
-   */
-  protected void showSelectedChains()
-  {
-    List<String> toshow = new ArrayList<>();
-    for (int i = 0; i < chainMenu.getItemCount(); i++)
-    {
-      if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
-      {
-        JCheckBoxMenuItem item = (JCheckBoxMenuItem) chainMenu.getItem(i);
-        if (item.isSelected())
-        {
-          toshow.add(item.getText());
-        }
-      }
-    }
-    getBinding().showChains(toshow);
-  }
-
-  /**
    * Tries to fetch a PDB file and save to a temporary local file. Returns the
    * saved file path if successful, or null if not.
    *