JAL-3390 pull-up refactoring towards functional changes...
[jalview.git] / src / jalview / structures / models / AAStructureBindingModel.java
index a3c4beb..6159757 100644 (file)
@@ -20,6 +20,7 @@
  */
 package jalview.structures.models;
 
+import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
 import jalview.api.SequenceRenderer;
 import jalview.api.StructureSelectionManagerProvider;
@@ -94,6 +95,8 @@ public abstract class AAStructureBindingModel
 
   public String fileLoadingError;
 
+  private boolean showAlignmentOnly;
+
   /**
    * Data bean class to simplify parameterisation in superposeStructures
    */
@@ -162,6 +165,8 @@ public abstract class AAStructureBindingModel
      * final count of chain mappings discovered
      */
     int chainmaps = 0;
+    // JBPNote: JAL-2693 - this should be a list of chain mappings per
+    // [pdbentry][sequence]
     String[][] newchains = new String[pdbEntry.length][];
     int pe = 0;
     for (PDBEntry pdb : pdbEntry)
@@ -176,7 +181,8 @@ public abstract class AAStructureBindingModel
           String chain = (chains != null && chains[pe] != null)
                   ? chains[pe][se]
                   : null;
-          SequenceI sq = asq.getDatasetSequence();
+          SequenceI sq = (asq.getDatasetSequence() == null) ? asq
+                  : asq.getDatasetSequence();
           if (sq.getAllPDBEntries() != null)
           {
             for (PDBEntry pdbentry : sq.getAllPDBEntries())
@@ -352,8 +358,8 @@ public abstract class AAStructureBindingModel
               { Integer.valueOf(pe).toString() }));
     }
     final String nullChain = "TheNullChain";
-    List<SequenceI> s = new ArrayList<SequenceI>();
-    List<String> c = new ArrayList<String>();
+    List<SequenceI> s = new ArrayList<>();
+    List<String> c = new ArrayList<>();
     if (getChains() == null)
     {
       setChains(new String[getPdbCount()][]);
@@ -422,8 +428,8 @@ public abstract class AAStructureBindingModel
   public synchronized PDBEntry[] addSequenceAndChain(PDBEntry[] pdbe,
           SequenceI[][] seq, String[][] chns)
   {
-    List<PDBEntry> v = new ArrayList<PDBEntry>();
-    List<int[]> rtn = new ArrayList<int[]>();
+    List<PDBEntry> v = new ArrayList<>();
+    List<int[]> rtn = new ArrayList<>();
     for (int i = 0; i < getPdbCount(); i++)
     {
       v.add(getPdbEntry(i));
@@ -790,13 +796,20 @@ public abstract class AAStructureBindingModel
   public abstract void colourByCharge();
 
   /**
-   * colour any structures associated with sequences in the given alignment
-   * using the getFeatureRenderer() and getSequenceRenderer() renderers but only
-   * if colourBySequence is enabled.
+   * Recolours the displayed structures, if they are coloured by sequence, or
+   * 'show only visible alignment' is selected. This supports updating structure
+   * colours on either change of alignment colours, or change to the visible
+   * region of the alignment.
    */
   public void colourBySequence(AlignmentViewPanel alignmentv)
   {
-    if (!colourBySequence || !isLoadingFinished())
+    if (!isLoadingFinished())
+    {
+      return;
+    }
+    // todo: property change event for visibleAlignment
+    // to avoid unnecessary redraws here
+    if (!colourBySequence && !isShowAlignmentOnly())
     {
       return;
     }
@@ -820,4 +833,53 @@ public abstract class AAStructureBindingModel
 
   public abstract jalview.api.FeatureRenderer getFeatureRenderer(
           AlignmentViewPanel alignment);
+
+  /**
+   * Sets the flag for whether only mapped visible residues in the alignment
+   * should be visible in the structure viewer
+   * 
+   * @param b
+   */
+  public void setShowAlignmentOnly(boolean b)
+  {
+    showAlignmentOnly = b;
+  }
+
+  /**
+   * Answers true if only mapped visible residues in the alignment should be
+   * visible in the structure viewer, else false
+   * 
+   * @return
+   */
+  public boolean isShowAlignmentOnly()
+  {
+    return showAlignmentOnly;
+  }
+
+  /**
+   * Shows the structures in the viewer, without changing their colouring. This is
+   * to support toggling of whether the whole structure is shown, or only residues
+   * mapped to visible regions of the alignment.
+   * 
+   * @param alignViewportI
+   */
+  public void showStructures(AlignViewportI alignViewportI) 
+  {
+    // override with implementation
+  }
+
+  @Override
+  public void updateColours(Object source)
+  {
+    AlignmentViewPanel ap = (AlignmentViewPanel) source;
+    // ignore events from panels not used to colour this view
+    if (!getViewer().isUsedforcolourby(ap))
+    {
+      return;
+    }
+    if (!isLoadingFromArchive())
+    {
+      colourBySequence(ap);
+    }
+  }
 }