Merge branch 'develop' into feature/JAL-244_Automatically_adjust_left_margin_to_avoid...
[jalview.git] / src / jalview / gui / AlignFrame.java
index 42b5776..3e24e67 100644 (file)
@@ -59,7 +59,6 @@ import java.util.Hashtable;
 import java.util.List;
 import java.util.Locale;
 import java.util.Vector;
-import java.util.concurrent.Callable;
 
 import javax.swing.ButtonGroup;
 import javax.swing.JCheckBoxMenuItem;
@@ -112,6 +111,7 @@ import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AlignmentOrder;
 import jalview.datamodel.AlignmentView;
 import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.ContactMatrixI;
 import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SeqCigar;
@@ -140,6 +140,7 @@ import jalview.io.JnetAnnotationMaker;
 import jalview.io.NewickFile;
 import jalview.io.ScoreMatrixFile;
 import jalview.io.TCoffeeScoreFile;
+import jalview.io.exceptions.ImageOutputException;
 import jalview.io.vcf.VCFLoader;
 import jalview.jbgui.GAlignFrame;
 import jalview.project.Jalview2XML;
@@ -151,6 +152,7 @@ import jalview.util.HttpUtils;
 import jalview.util.ImageMaker.TYPE;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
+import jalview.util.imagemaker.BitmapImageSizing;
 import jalview.viewmodel.AlignmentViewport;
 import jalview.viewmodel.ViewportRanges;
 import jalview.ws.DBRefFetcher;
@@ -980,14 +982,16 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   @Override
   public void setProgressBar(String message, long id)
   {
-    progressBar.setProgressBar(message, id);
+    if (!Platform.isHeadless() && progressBar != null)
+      progressBar.setProgressBar(message, id);
   }
 
   @Override
   public void registerHandler(final long id,
           final IProgressIndicatorHandler handler)
   {
-    progressBar.registerHandler(id, handler);
+    if (progressBar != null)
+      progressBar.registerHandler(id, handler);
   }
 
   /**
@@ -997,7 +1001,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   @Override
   public boolean operationInProgress()
   {
-    return progressBar.operationInProgress();
+    return progressBar == null ? false : progressBar.operationInProgress();
   }
 
   /**
@@ -1275,11 +1279,10 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     }
 
     AlignExportSettingsI options = new AlignExportSettingsAdapter(false);
-    Callable<Void> cancelAction = () -> {
+    Runnable cancelAction = () -> {
       lastSaveSuccessful = false;
-      return null;
     };
-    Callable<Void> outputAction = () -> {
+    Runnable outputAction = () -> {
       // todo defer this to inside formatSequences (or later)
       AlignmentExportData exportData = viewport.getAlignExportData(options);
       String output = new FormatAdapter(alignPanel, options)
@@ -1357,7 +1360,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
           AlignFrame.this.getViewport().setSavedUpToDate(true);
         }
       }
-      return null;
     };
 
     /*
@@ -1375,7 +1377,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     {
       try
       {
-        outputAction.call();
+        outputAction.run();
       } catch (Exception e)
       {
         // TODO Auto-generated catch block
@@ -1397,7 +1399,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     FileFormatI fileFormat = FileFormats.getInstance()
             .forName(fileFormatName);
     AlignExportSettingsI options = new AlignExportSettingsAdapter(false);
-    Callable<Void> outputAction = () -> {
+    Runnable outputAction = () -> {
       // todo defer this to inside formatSequences (or later)
       AlignmentExportData exportData = viewport.getAlignExportData(options);
       CutAndPasteTransfer cap = new CutAndPasteTransfer();
@@ -1419,7 +1421,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
                 oom);
         cap.dispose();
       }
-      return null;
     };
 
     /*
@@ -1436,7 +1437,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     {
       try
       {
-        outputAction.call();
+        outputAction.run();
       } catch (Exception e)
       {
         e.printStackTrace();
@@ -1454,19 +1455,75 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   protected void htmlMenuItem_actionPerformed(ActionEvent e)
   {
     HtmlSvgOutput htmlSVG = new HtmlSvgOutput(alignPanel);
-    htmlSVG.exportHTML(null);
+    try
+    {
+      htmlSVG.exportHTML(null);
+    } catch (ImageOutputException x)
+    {
+      // report problem to console and raise dialog
+    }
   }
 
   @Override
   public void bioJSMenuItem_actionPerformed(ActionEvent e)
   {
     BioJsHTMLOutput bjs = new BioJsHTMLOutput(alignPanel);
-    bjs.exportHTML(null);
+    try
+    {
+      bjs.exportHTML(null);
+    } catch (ImageOutputException x)
+    {
+      // report problem to console and raise dialog
+    }
   }
 
   public void createImageMap(File file, String image)
   {
-    alignPanel.makePNGImageMap(file, image);
+    try
+    {
+      alignPanel.makePNGImageMap(file, image);
+    } catch (ImageOutputException x)
+    {
+      // report problem to console and raise dialog
+    }
+  }
+
+  @Override
+  public void createPNG_actionPerformed(ActionEvent e)
+  {
+    try
+    {
+      createPNG(null);
+    } catch (ImageOutputException ioex)
+    {
+      // raise dialog, and report via console
+    }
+  }
+
+  @Override
+  public void createEPS_actionPerformed(ActionEvent e)
+  {
+    try
+    {
+      createEPS(null);
+    } catch (ImageOutputException ioex)
+    {
+      // raise dialog, and report via console
+    }
+
+  }
+
+  @Override
+  public void createSVG_actionPerformed(ActionEvent e)
+  {
+    try
+    {
+      createSVG(null);
+    } catch (ImageOutputException ioex)
+    {
+      // raise dialog, and report via console
+    }
+
   }
 
   /**
@@ -1475,10 +1532,15 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * 
    * @param f
    */
-  @Override
-  public void createPNG(File f)
+  public void createPNG(File f) throws ImageOutputException
   {
-    alignPanel.makeAlignmentImage(TYPE.PNG, f);
+    createPNG(f, null, BitmapImageSizing.nullBitmapImageSizing());
+  }
+
+  public void createPNG(File f, String renderer, BitmapImageSizing userBis)
+          throws ImageOutputException
+  {
+    alignPanel.makeAlignmentImage(TYPE.PNG, f, renderer, userBis);
   }
 
   /**
@@ -1487,10 +1549,14 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * 
    * @param f
    */
-  @Override
-  public void createEPS(File f)
+  public void createEPS(File f) throws ImageOutputException
   {
-    alignPanel.makeAlignmentImage(TYPE.EPS, f);
+    createEPS(f, null);
+  }
+
+  public void createEPS(File f, String renderer) throws ImageOutputException
+  {
+    alignPanel.makeAlignmentImage(TYPE.EPS, f, renderer);
   }
 
   /**
@@ -1499,10 +1565,14 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    * 
    * @param f
    */
-  @Override
-  public void createSVG(File f)
+  public void createSVG(File f) throws ImageOutputException
   {
-    alignPanel.makeAlignmentImage(TYPE.SVG, f);
+    createSVG(f, null);
+  }
+
+  public void createSVG(File f, String renderer) throws ImageOutputException
+  {
+    alignPanel.makeAlignmentImage(TYPE.SVG, f, renderer);
   }
 
   @Override
@@ -1552,7 +1622,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       String choice = chooser.getSelectedFile().getPath();
       Cache.setProperty("LAST_DIRECTORY", choice);
       loadJalviewDataFile(chooser.getSelectedFile(), null, null, null);
-      return null;
     });
 
     chooser.showOpenDialog(this);
@@ -2284,10 +2353,17 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
                         .intValue();
               }
             }
-            alignment.addAnnotation(sequences[i].getAnnotation()[a]); // annotation
-            // was
-            // duplicated
-            // earlier
+            // annotation was duplicated earlier
+            alignment.addAnnotation(sequences[i].getAnnotation()[a]);
+            // take care of contact matrix too
+            ContactMatrixI cm = sequences[i]
+                    .getContactMatrixFor(sequences[i].getAnnotation()[a]);
+            if (cm != null)
+            {
+              alignment.addContactListFor(sequences[i].getAnnotation()[a],
+                      cm);
+            }
+
             alignment.setAnnotationIndex(sequences[i].getAnnotation()[a],
                     a);
           }
@@ -2474,7 +2550,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       return;
     }
 
-    Callable okAction = () -> {
+    Runnable okAction = () -> {
       SequenceI[] cut = sg.getSequences()
               .toArray(new SequenceI[sg.getSize()]);
 
@@ -2498,7 +2574,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
         {
         }
       }
-      return null;
     };
 
     /*
@@ -2524,7 +2599,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     {
       try
       {
-        okAction.call();
+        okAction.run();
       } catch (Exception e)
       {
         e.printStackTrace();
@@ -2634,6 +2709,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   {
     viewport.invertColumnSelection();
     alignPanel.paintAlignment(true, false);
+    PaintRefresher.Refresh(alignPanel, viewport.getSequenceSetId());
     viewport.sendSelection();
   }
 
@@ -3079,10 +3155,15 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   @Override
   public void wrapMenuItem_actionPerformed(ActionEvent e)
   {
-    scaleAbove.setVisible(wrapMenuItem.isSelected());
-    scaleLeft.setVisible(wrapMenuItem.isSelected());
-    scaleRight.setVisible(wrapMenuItem.isSelected());
-    viewport.setWrapAlignment(wrapMenuItem.isSelected());
+    setWrapFormat(wrapMenuItem.isSelected());
+  }
+
+  public void setWrapFormat(boolean b)
+  {
+    scaleAbove.setVisible(b);
+    scaleLeft.setVisible(b);
+    scaleRight.setVisible(b);
+    viewport.setWrapAlignment(b);
     alignPanel.updateLayout();
   }
 
@@ -3437,8 +3518,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   @Override
   public void overviewMenuItem_actionPerformed(ActionEvent e)
   {
-    boolean showHiddenRegions = Cache.getDefault(Preferences.SHOW_OV_HIDDEN_AT_START,
-                false);
+    boolean showHiddenRegions = Cache
+            .getDefault(Preferences.SHOW_OV_HIDDEN_AT_START, false);
     openOverviewPanel(showHiddenRegions);
   }
 
@@ -3449,14 +3530,15 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       return alignPanel.overviewPanel;
     }
     JInternalFrame frame = new JInternalFrame();
-    final OverviewPanel overview = new OverviewPanel(alignPanel, frame, showHidden);
+    final OverviewPanel overview = new OverviewPanel(alignPanel, frame,
+            showHidden);
     frame.setContentPane(overview);
-    Desktop.addInternalFrame(frame, "", true, frame.getWidth(), frame.getHeight(),
-            true, true);
+    Desktop.addInternalFrame(frame, "", true, frame.getWidth(),
+            frame.getHeight(), true, true);
     frame.setFrameIcon(null);
     frame.pack();
     frame.setLayer(JLayeredPane.PALETTE_LAYER);
-    final AlignmentPanel thePanel = this.alignPanel; 
+    final AlignmentPanel thePanel = this.alignPanel;
     frame.addInternalFrameListener(
             new javax.swing.event.InternalFrameAdapter()
             {
@@ -3475,7 +3557,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
     alignPanel.setOverviewPanel(overview);
     alignPanel.setOverviewTitle(this);
-    
+
     return overview;
   }
 
@@ -4114,7 +4196,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
                         .getString("label.possible_problem_with_tree_file"),
                 JvOptionPane.WARNING_MESSAGE);
       }
-      return null;
     });
     chooser.showOpenDialog(this);
   }
@@ -4180,6 +4261,57 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     return tp;
   }
 
+  public void showContactMapTree(AlignmentAnnotation aa, ContactMatrixI cm)
+  {
+    int x = 4, y = 5;
+    int w = 400, h = 500;
+
+    try
+    {
+      NewickFile fin = new NewickFile(
+              new FileParse(cm.getNewick(), DataSourceType.PASTE));
+      String title = aa.label + " "
+              + cm.getTreeMethod() + " tree" + (aa.sequenceRef != null
+                      ? (" for " + aa.sequenceRef.getDisplayId(false))
+                      : "");
+
+      showColumnWiseTree(fin, aa, title, w, h, x, y);
+    } catch (Throwable xx)
+    {
+      Console.error("Unexpected exception showing tree for contact matrix",
+              xx);
+    }
+  }
+
+  public TreePanel showColumnWiseTree(NewickFile nf, AlignmentAnnotation aa,
+          String treeTitle, int w, int h, int x, int y)
+  {
+    try
+    {
+      nf.parse();
+      if (nf.getTree() == null)
+      {
+        return null;
+      }
+      TreePanel tp = new TreePanel(alignPanel, nf, aa, treeTitle);
+
+      tp.setSize(w, h);
+
+      if (x > 0 && y > 0)
+      {
+        tp.setLocation(x, y);
+      }
+
+      Desktop.addInternalFrame(tp, treeTitle, w, h);
+      return tp;
+    } catch (Throwable xx)
+    {
+      Console.error("Unexpected exception showing tree for contact matrix",
+              xx);
+    }
+    return null;
+  }
+
   private boolean buildingMenu = false;
 
   /**
@@ -4973,7 +5105,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
        * to add view name "Original" if necessary
        */
       alignPanel.setOverviewTitle(this);
-      
+
       /*
        * switch panels and set Overview title (if there is one
        * because it was opened automatically)
@@ -5644,7 +5776,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
    *          update non-sequence-related annotations
    */
   @Override
-  protected void setAnnotationsVisibility(boolean visible,
+  public void setAnnotationsVisibility(boolean visible,
           boolean forSequences, boolean forAlignment)
   {
     AlignmentAnnotation[] anns = alignPanel.getAlignment()
@@ -5910,7 +6042,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       Cache.setProperty("LAST_DIRECTORY", choice);
       SequenceI[] seqs = viewport.getAlignment().getSequencesArray();
       new VCFLoader(choice).loadVCF(seqs, us);
-      return null;
     });
     chooser.showOpenDialog(null);
 
@@ -5929,6 +6060,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   {
     return lastFeatureSettingsBounds;
   }
+
 }
 
 class PrintThread extends Thread