JAL-2773 add new flag to paintAlignment(updateOverview,updateStructures) and first...
[jalview.git] / src / jalview / gui / StructureViewerBase.java
index 34ad659..c8854a7 100644 (file)
@@ -23,7 +23,7 @@ package jalview.gui;
 import jalview.bin.Cache;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
-import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceI;
 import jalview.gui.StructureViewer.ViewerType;
@@ -83,18 +83,18 @@ public abstract class StructureViewerBase extends GStructureViewer
   /**
    * list of sequenceSet ids associated with the view
    */
-  protected List<String> _aps = new ArrayList<String>();
+  protected List<String> _aps = new ArrayList<>();
 
   /**
    * list of alignment panels to use for superposition
    */
-  protected Vector<AlignmentPanel> _alignwith = new Vector<AlignmentPanel>();
+  protected Vector<AlignmentPanel> _alignwith = new Vector<>();
 
   /**
    * list of alignment panels that are used for colouring structures by aligned
    * sequences
    */
-  protected Vector<AlignmentPanel> _colourwith = new Vector<AlignmentPanel>();
+  protected Vector<AlignmentPanel> _colourwith = new Vector<>();
 
   private String viewId = null;
 
@@ -110,6 +110,8 @@ public abstract class StructureViewerBase extends GStructureViewer
 
   protected boolean allChainsSelected = false;
 
+  protected JMenu viewSelectionMenu;
+
   /**
    * Default constructor
    */
@@ -117,6 +119,7 @@ public abstract class StructureViewerBase extends GStructureViewer
   {
     super();
   }
+
   /**
    * 
    * @param ap2
@@ -167,7 +170,7 @@ public abstract class StructureViewerBase extends GStructureViewer
   {
     if (_alignwith == null)
     {
-      _alignwith = new Vector<AlignmentPanel>();
+      _alignwith = new Vector<>();
     }
     if (_alignwith.size() == 0 && ap != null)
     {
@@ -353,7 +356,8 @@ public abstract class StructureViewerBase extends GStructureViewer
     }
     // otherwise, start adding the structure.
     getBinding().addSequenceAndChain(new PDBEntry[] { pdbentry },
-            new SequenceI[][] { seqs }, new String[][] { chains });
+            new SequenceI[][]
+            { seqs }, new String[][] { chains });
     addingStructures = true;
     _started = false;
     alignAddedStructures = align;
@@ -375,7 +379,8 @@ public abstract class StructureViewerBase extends GStructureViewer
   {
     int option = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
             MessageManager.formatMessage("label.add_pdbentry_to_view",
-                    new Object[] { pdbId, view.getTitle() }),
+                    new Object[]
+                    { pdbId, view.getTitle() }),
             MessageManager
                     .getString("label.align_to_existing_structure_view"),
             JvOptionPane.YES_NO_CANCEL_OPTION);
@@ -463,7 +468,9 @@ public abstract class StructureViewerBase extends GStructureViewer
     if (apanel.getSeqPanel().seqCanvas.fr != null)
     {
       apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
-      apanel.paintAlignment(true);
+      // note - we don't do a refresh for structure here because we do it
+      // explicitly for all panels later on
+      apanel.paintAlignment(true, false);
     }
 
     /*
@@ -486,8 +493,8 @@ public abstract class StructureViewerBase extends GStructureViewer
            */
           viewer.useAlignmentPanelForColourbyseq(apanel);
           viewer.buildActionMenu();
-          apanel.getStructureSelectionManager().sequenceColoursChanged(
-                  apanel);
+          apanel.getStructureSelectionManager()
+                  .sequenceColoursChanged(apanel);
           break;
         }
       }
@@ -518,11 +525,11 @@ public abstract class StructureViewerBase extends GStructureViewer
        */
       int option = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
               MessageManager.formatMessage(
-                      "label.pdb_entry_is_already_displayed",
-                      new Object[] { pdbId }), MessageManager
-                      .formatMessage(
-                              "label.map_sequences_to_visible_window",
-                              new Object[] { pdbId }),
+                      "label.pdb_entry_is_already_displayed", new Object[]
+                      { pdbId }),
+              MessageManager.formatMessage(
+                      "label.map_sequences_to_visible_window", new Object[]
+                      { pdbId }),
               JvOptionPane.YES_NO_CANCEL_OPTION);
       if (option == JvOptionPane.CANCEL_OPTION)
       {
@@ -594,8 +601,8 @@ public abstract class StructureViewerBase extends GStructureViewer
   public void changeColour_actionPerformed(String colourSchemeName)
   {
     AlignmentI al = getAlignmentPanel().av.getAlignment();
-    ColourSchemeI cs = ColourSchemes.getInstance().getColourScheme(
-            colourSchemeName, al, null);
+    ColourSchemeI cs = ColourSchemes.getInstance()
+            .getColourScheme(colourSchemeName, al, null);
     getBinding().setJalviewColourScheme(cs);
   }
 
@@ -712,11 +719,11 @@ public abstract class StructureViewerBase extends GStructureViewer
 
     if (_colourwith == null)
     {
-      _colourwith = new Vector<AlignmentPanel>();
+      _colourwith = new Vector<>();
     }
     if (_alignwith == null)
     {
-      _alignwith = new Vector<AlignmentPanel>();
+      _alignwith = new Vector<>();
     }
 
     ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
@@ -744,17 +751,17 @@ public abstract class StructureViewerBase extends GStructureViewer
       @Override
       public void itemStateChanged(ItemEvent e)
       {
-        alignStructs.setEnabled(_alignwith.size() > 0);
+        alignStructs.setEnabled(!_alignwith.isEmpty());
         alignStructs.setToolTipText(MessageManager.formatMessage(
                 "label.align_structures_using_linked_alignment_views",
-                new String[] { String.valueOf(_alignwith.size()) }));
+                _alignwith.size()));
       }
     };
-    JMenu alpanels = new ViewSelectionMenu(
+    viewSelectionMenu = new ViewSelectionMenu(
             MessageManager.getString("label.superpose_with"), this,
             _alignwith, handler);
     handler.itemStateChanged(null);
-    viewerActionMenu.add(alpanels);
+    viewerActionMenu.add(viewSelectionMenu, 0);
     viewerActionMenu.addMenuListener(new MenuListener()
     {
       @Override
@@ -778,40 +785,55 @@ public abstract class StructureViewerBase extends GStructureViewer
   }
 
   @Override
-  public void setJalviewColourScheme(ColourSchemeI cs) {
+  public void setJalviewColourScheme(ColourSchemeI cs)
+  {
     getBinding().setJalviewColourScheme(cs);
   }
+
+  /**
+   * Sends commands to the structure viewer to superimpose structures based on
+   * currently associated alignments. May optionally return an error message for
+   * the operation.
+   */
   @Override
-  protected void alignStructs_actionPerformed(ActionEvent actionEvent)
+  protected String alignStructs_actionPerformed(ActionEvent actionEvent)
   {
-    alignStructs_withAllAlignPanels();
+    return alignStructs_withAllAlignPanels();
   }
-  protected void alignStructs_withAllAlignPanels()
+
+  protected String alignStructs_withAllAlignPanels()
   {
     if (getAlignmentPanel() == null)
     {
-      return;
+      return null;
     }
-  
+
     if (_alignwith.size() == 0)
     {
       _alignwith.add(getAlignmentPanel());
     }
-  
+
+    String reply = null;
     try
     {
       AlignmentI[] als = new Alignment[_alignwith.size()];
-      ColumnSelection[] alc = new ColumnSelection[_alignwith.size()];
+      HiddenColumns[] alc = new HiddenColumns[_alignwith.size()];
       int[] alm = new int[_alignwith.size()];
       int a = 0;
-  
+
       for (AlignmentPanel ap : _alignwith)
       {
         als[a] = ap.av.getAlignment();
         alm[a] = -1;
-        alc[a++] = ap.av.getColumnSelection();
+        alc[a++] = ap.av.getAlignment().getHiddenColumns();
+      }
+      reply = getBinding().superposeStructures(als, alm, alc);
+      if (reply != null)
+      {
+        String text = MessageManager
+                .formatMessage("error.superposition_failed", reply);
+        statusBar.setText(text);
       }
-      getBinding().superposeStructures(als, alm, alc);
     } catch (Exception e)
     {
       StringBuffer sp = new StringBuffer();
@@ -822,7 +844,9 @@ public abstract class StructureViewerBase extends GStructureViewer
       Cache.log.info("Couldn't align structures with the " + sp.toString()
               + "associated alignment panels.", e);
     }
+    return reply;
   }
+
   @Override
   public void background_actionPerformed(ActionEvent actionEvent)
   {
@@ -834,6 +858,7 @@ public abstract class StructureViewerBase extends GStructureViewer
       getBinding().setBackgroundColour(col);
     }
   }
+
   @Override
   public void viewerColour_actionPerformed(ActionEvent actionEvent)
   {
@@ -843,18 +868,21 @@ public abstract class StructureViewerBase extends GStructureViewer
       getBinding().setColourBySequence(false);
     }
   }
+
   @Override
   public void chainColour_actionPerformed(ActionEvent actionEvent)
   {
     chainColour.setSelected(true);
     getBinding().colourByChain();
   }
+
   @Override
   public void chargeColour_actionPerformed(ActionEvent actionEvent)
   {
     chargeColour.setSelected(true);
     getBinding().colourByCharge();
   }
+
   @Override
   public void seqColour_actionPerformed(ActionEvent actionEvent)
   {
@@ -862,7 +890,7 @@ public abstract class StructureViewerBase extends GStructureViewer
     binding.setColourBySequence(seqColour.isSelected());
     if (_colourwith == null)
     {
-      _colourwith = new Vector<AlignmentPanel>();
+      _colourwith = new Vector<>();
     }
     if (binding.isColourBySequence())
     {
@@ -881,18 +909,19 @@ public abstract class StructureViewerBase extends GStructureViewer
       }
     }
   }
+
   @Override
   public void pdbFile_actionPerformed(ActionEvent actionEvent)
   {
     JalviewFileChooser chooser = new JalviewFileChooser(
             Cache.getProperty("LAST_DIRECTORY"));
-  
+
     chooser.setFileView(new JalviewFileView());
     chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
     chooser.setToolTipText(MessageManager.getString("action.save"));
-  
+
     int value = chooser.showSaveDialog(this);
-  
+
     if (value == JalviewFileChooser.APPROVE_OPTION)
     {
       BufferedReader in = null;
@@ -900,9 +929,9 @@ public abstract class StructureViewerBase extends GStructureViewer
       {
         // TODO: cope with multiple PDB files in view
         in = new BufferedReader(
-                new FileReader(getBinding().getPdbFile()[0]));
+                new FileReader(getBinding().getStructureFiles()[0]));
         File outFile = chooser.getSelectedFile();
-  
+
         PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
         String data;
         while ((data = in.readLine()) != null)
@@ -931,6 +960,7 @@ public abstract class StructureViewerBase extends GStructureViewer
       }
     }
   }
+
   @Override
   public void viewMapping_actionPerformed(ActionEvent actionEvent)
   {
@@ -952,6 +982,10 @@ public abstract class StructureViewerBase extends GStructureViewer
   }
 
   protected abstract String getViewerName();
+
+  /**
+   * Configures the title and menu items of the viewer panel.
+   */
   public void updateTitleAndMenus()
   {
     AAStructureBindingModel binding = getBinding();
@@ -961,12 +995,32 @@ public abstract class StructureViewerBase extends GStructureViewer
       return;
     }
     setChainMenuItems(binding.getChainNames());
-  
+
     this.setTitle(binding.getViewerTitle(getViewerName(), true));
-    if (binding.getPdbFile().length > 1 && binding.getSequence().length > 1)
+
+    /*
+     * enable 'Superpose with' if more than one mapped structure
+     */
+    viewSelectionMenu.setEnabled(false);
+    if (getBinding().getStructureFiles().length > 1
+            && getBinding().getSequence().length > 1)
+    {
+      viewSelectionMenu.setEnabled(true);
+    }
+
+    /*
+     * Show action menu if it has any enabled items
+     */
+    viewerActionMenu.setVisible(false);
+    for (int i = 0; i < viewerActionMenu.getItemCount(); i++)
     {
-      viewerActionMenu.setVisible(true);
+      if (viewerActionMenu.getItem(i).isEnabled())
+      {
+        viewerActionMenu.setVisible(true);
+        break;
+      }
     }
+
     if (!binding.isLoadingFromArchive())
     {
       seqColour_actionPerformed(null);