JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / gui / PCAPanel.java
old mode 100755 (executable)
new mode 100644 (file)
index f423a43..57a14a0
@@ -1,34 +1,53 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
- * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
+ * Copyright (C) 2015 The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  * Jalview is free software: you can redistribute it and/or
  * modify it under the terms of the GNU General Public License 
- * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
  *  
  * Jalview is distributed in the hope that it will be useful, but 
  * WITHOUT ANY WARRANTY; without even the implied warranty 
  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
  * PURPOSE.  See the GNU General Public License for more details.
  * 
- * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
  */
 package jalview.gui;
 
-import java.util.*;
-
-import java.awt.*;
-import java.awt.event.*;
-import java.awt.print.*;
-import javax.swing.*;
-
-import jalview.analysis.*;
-import jalview.datamodel.*;
-import jalview.jbgui.*;
+import jalview.datamodel.Alignment;
+import jalview.datamodel.AlignmentView;
+import jalview.datamodel.ColumnSelection;
+import jalview.datamodel.SeqCigar;
+import jalview.datamodel.SequenceI;
+import jalview.jbgui.GPCAPanel;
+import jalview.schemes.ResidueProperties;
+import jalview.util.MessageManager;
+import jalview.viewmodel.AlignmentViewport;
 import jalview.viewmodel.PCAModel;
 
+import java.awt.BorderLayout;
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.print.PageFormat;
+import java.awt.print.Printable;
+import java.awt.print.PrinterException;
+import java.awt.print.PrinterJob;
+
+import javax.swing.ButtonGroup;
+import javax.swing.JCheckBoxMenuItem;
+import javax.swing.JColorChooser;
+import javax.swing.JMenuItem;
+import javax.swing.JOptionPane;
+import javax.swing.JRadioButtonMenuItem;
+
 /**
  * DOCUMENT ME!
  * 
@@ -39,11 +58,13 @@ public class PCAPanel extends GPCAPanel implements Runnable,
         IProgressIndicator
 {
 
+  private IProgressIndicator progressBar;
+
   RotatableCanvas rc;
 
   AlignmentPanel ap;
 
-  AlignViewport av;
+  AlignmentViewport av;
 
   PCAModel pcaModel;
 
@@ -62,6 +83,8 @@ public class PCAPanel extends GPCAPanel implements Runnable,
     this.av = ap.av;
     this.ap = ap;
 
+    progressBar = new ProgressBar(statusPanel, statusBar);
+
     boolean sameLength = true;
     boolean selected = av.getSelectionGroup() != null
             && av.getSelectionGroup().getSize() > 0;
@@ -90,13 +113,10 @@ public class PCAPanel extends GPCAPanel implements Runnable,
 
     if (!sameLength)
     {
-      JOptionPane
-              .showMessageDialog(
-                      Desktop.desktop,
-                      "The sequences must be aligned before calculating PCA.\n"
-                              + "Try using the Pad function in the edit menu,\n"
-                              + "or one of the multiple sequence alignment web services.",
-                      "Sequences not aligned", JOptionPane.WARNING_MESSAGE);
+      JOptionPane.showMessageDialog(Desktop.desktop,
+              MessageManager.getString("label.pca_sequences_not_aligned"),
+              MessageManager.getString("label.sequences_not_aligned"),
+              JOptionPane.WARNING_MESSAGE);
 
       return;
     }
@@ -109,9 +129,48 @@ public class PCAPanel extends GPCAPanel implements Runnable,
     worker.start();
   }
 
+  @Override
+  protected void scoreMatrix_menuSelected()
+  {
+    scoreMatrixMenu.removeAll();
+    for (final String sm : ResidueProperties.scoreMatrices.keySet())
+    {
+      if (ResidueProperties.getScoreMatrix(sm) != null)
+      {
+        // create an entry for this score matrix for use in PCA
+        JCheckBoxMenuItem jm = new JCheckBoxMenuItem();
+        jm.setText(MessageManager
+                .getStringOrReturn("label.score_model", sm));
+        jm.setSelected(pcaModel.getScore_matrix().equals(sm));
+        if ((ResidueProperties.scoreMatrices.get(sm).isDNA() && ResidueProperties.scoreMatrices
+                .get(sm).isProtein())
+                || pcaModel.isNucleotide() == ResidueProperties.scoreMatrices
+                        .get(sm).isDNA())
+        {
+          final PCAPanel us = this;
+          jm.addActionListener(new ActionListener()
+          {
+            @Override
+            public void actionPerformed(ActionEvent e)
+            {
+              if (!pcaModel.getScore_matrix().equals(sm))
+              {
+                pcaModel.setScore_matrix(sm);
+                Thread worker = new Thread(us);
+                worker.start();
+              }
+            }
+          });
+          scoreMatrixMenu.add(jm);
+        }
+      }
+    }
+  }
+
   public void bgcolour_actionPerformed(ActionEvent e)
   {
-    Color col = JColorChooser.showDialog(this, "Select Background Colour",
+    Color col = JColorChooser.showDialog(this,
+            MessageManager.getString("label.select_backgroud_colour"),
             rc.bgColour);
 
     if (col != null)
@@ -128,11 +187,11 @@ public class PCAPanel extends GPCAPanel implements Runnable,
   {
     long progId = System.currentTimeMillis();
     IProgressIndicator progress = this;
-    String message = "Recalculating PCA";
+    String message = MessageManager.getString("label.pca_recalculating");
     if (getParent() == null)
     {
       progress = ap.alignFrame;
-      message = "Calculating PCA";
+      message = MessageManager.getString("label.pca_calculating");
     }
     progress.setProgressBar(message, progId);
     try
@@ -164,8 +223,8 @@ public class PCAPanel extends GPCAPanel implements Runnable,
     if (getParent() == null)
     {
       addKeyListener(rc);
-      Desktop.addInternalFrame(this, "Principal component analysis", 475,
-              450);
+      Desktop.addInternalFrame(this, MessageManager
+              .getString("label.principal_component_analysis"), 475, 450);
     }
   }
 
@@ -175,6 +234,7 @@ public class PCAPanel extends GPCAPanel implements Runnable,
     if (!pcaModel.isNucleotide())
     {
       pcaModel.setNucleotide(true);
+      pcaModel.setScore_matrix("DNA");
       Thread worker = new Thread(this);
       worker.start();
     }
@@ -188,6 +248,7 @@ public class PCAPanel extends GPCAPanel implements Runnable,
     if (pcaModel.isNucleotide())
     {
       pcaModel.setNucleotide(false);
+      pcaModel.setScore_matrix("BLOSUM62");
       Thread worker = new Thread(this);
       worker.start();
     }
@@ -260,7 +321,8 @@ public class PCAPanel extends GPCAPanel implements Runnable,
     try
     {
       cap.setText(pcaModel.getDetails());
-      Desktop.addInternalFrame(cap, "PCA details", 500, 500);
+      Desktop.addInternalFrame(cap,
+              MessageManager.getString("label.pca_details"), 500, 500);
     } catch (OutOfMemoryError oom)
     {
       new OOMWarning("opening PCA details", oom);
@@ -334,8 +396,10 @@ public class PCAPanel extends GPCAPanel implements Runnable,
         // af.addSortByOrderMenuItem(ServiceName + " Ordering",
         // msaorder);
 
-        Desktop.addInternalFrame(af, "Original Data for " + this.title,
-                AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
+        Desktop.addInternalFrame(af, MessageManager.formatMessage(
+                "label.original_data_for_params",
+                new String[] { this.title }), AlignFrame.DEFAULT_WIDTH,
+                AlignFrame.DEFAULT_HEIGHT);
       }
     }
     /*
@@ -400,7 +464,7 @@ public class PCAPanel extends GPCAPanel implements Runnable,
    */
   public void eps_actionPerformed(ActionEvent e)
   {
-    makePCAImage(jalview.util.ImageMaker.EPS);
+    makePCAImage(jalview.util.ImageMaker.TYPE.EPS);
   }
 
   /**
@@ -411,26 +475,34 @@ public class PCAPanel extends GPCAPanel implements Runnable,
    */
   public void png_actionPerformed(ActionEvent e)
   {
-    makePCAImage(jalview.util.ImageMaker.PNG);
+    makePCAImage(jalview.util.ImageMaker.TYPE.PNG);
   }
 
-  void makePCAImage(int type)
+  void makePCAImage(jalview.util.ImageMaker.TYPE type)
   {
     int width = rc.getWidth();
     int height = rc.getHeight();
 
     jalview.util.ImageMaker im;
 
-    if (type == jalview.util.ImageMaker.PNG)
+    if (type == jalview.util.ImageMaker.TYPE.PNG)
     {
-      im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.PNG,
-              "Make PNG image from PCA", width, height, null, null);
+      im = new jalview.util.ImageMaker(this,
+              jalview.util.ImageMaker.TYPE.PNG, "Make PNG image from PCA",
+              width, height, null, null);
+    }
+    else if (type == jalview.util.ImageMaker.TYPE.EPS)
+    {
+      im = new jalview.util.ImageMaker(this,
+              jalview.util.ImageMaker.TYPE.EPS, "Make EPS file from PCA",
+              width, height, null, this.getTitle());
     }
     else
     {
-      im = new jalview.util.ImageMaker(this, jalview.util.ImageMaker.EPS,
-              "Make EPS file from PCA", width, height, null,
-              this.getTitle());
+      im = new jalview.util.ImageMaker(this,
+              jalview.util.ImageMaker.TYPE.SVG, "Make SVG file from PCA",
+              width, height, null, this.getTitle());
+
     }
 
     if (im.getGraphics() != null)
@@ -523,7 +595,9 @@ public class PCAPanel extends GPCAPanel implements Runnable,
       cap.setText(pcaModel.getPointsasCsv(false,
               xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
               zCombobox.getSelectedIndex()));
-      Desktop.addInternalFrame(cap, "Points for " + getTitle(), 500, 500);
+      Desktop.addInternalFrame(cap, MessageManager.formatMessage(
+              "label.points_for_params", new String[] { this.getTitle() }),
+              500, 500);
     } catch (OutOfMemoryError oom)
     {
       new OOMWarning("exporting PCA points", oom);
@@ -546,8 +620,9 @@ public class PCAPanel extends GPCAPanel implements Runnable,
       cap.setText(pcaModel.getPointsasCsv(true,
               xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
               zCombobox.getSelectedIndex()));
-      Desktop.addInternalFrame(cap, "Transformed points for " + getTitle(),
-              500, 500);
+      Desktop.addInternalFrame(cap, MessageManager.formatMessage(
+              "label.transformed_points_for_params",
+              new String[] { this.getTitle() }), 500, 500);
     } catch (OutOfMemoryError oom)
     {
       new OOMWarning("exporting transformed PCA points", oom);
@@ -555,10 +630,6 @@ public class PCAPanel extends GPCAPanel implements Runnable,
     }
   }
 
-  // methods for implementing IProgressIndicator
-  // need to refactor to a reusable stub class
-  Hashtable progressBars, progressBarHandlers;
-
   /*
    * (non-Javadoc)
    * 
@@ -567,81 +638,82 @@ public class PCAPanel extends GPCAPanel implements Runnable,
   @Override
   public void setProgressBar(String message, long id)
   {
-    if (progressBars == null)
-    {
-      progressBars = new Hashtable();
-      progressBarHandlers = new Hashtable();
-    }
-
-    JPanel progressPanel;
-    Long lId = new Long(id);
-    GridLayout layout = (GridLayout) statusPanel.getLayout();
-    if (progressBars.get(lId) != null)
-    {
-      progressPanel = (JPanel) progressBars.get(new Long(id));
-      statusPanel.remove(progressPanel);
-      progressBars.remove(lId);
-      progressPanel = null;
-      if (message != null)
-      {
-        statusBar.setText(message);
-      }
-      if (progressBarHandlers.contains(lId))
-      {
-        progressBarHandlers.remove(lId);
-      }
-      layout.setRows(layout.getRows() - 1);
-    }
-    else
-    {
-      progressPanel = new JPanel(new BorderLayout(10, 5));
-
-      JProgressBar progressBar = new JProgressBar();
-      progressBar.setIndeterminate(true);
-
-      progressPanel.add(new JLabel(message), BorderLayout.WEST);
-      progressPanel.add(progressBar, BorderLayout.CENTER);
-
-      layout.setRows(layout.getRows() + 1);
-      statusPanel.add(progressPanel);
-
-      progressBars.put(lId, progressPanel);
-    }
-    // update GUI
-    // setMenusForViewport();
-    validate();
+    progressBar.setProgressBar(message, id);
+    // if (progressBars == null)
+    // {
+    // progressBars = new Hashtable();
+    // progressBarHandlers = new Hashtable();
+    // }
+    //
+    // JPanel progressPanel;
+    // Long lId = new Long(id);
+    // GridLayout layout = (GridLayout) statusPanel.getLayout();
+    // if (progressBars.get(lId) != null)
+    // {
+    // progressPanel = (JPanel) progressBars.get(new Long(id));
+    // statusPanel.remove(progressPanel);
+    // progressBars.remove(lId);
+    // progressPanel = null;
+    // if (message != null)
+    // {
+    // statusBar.setText(message);
+    // }
+    // if (progressBarHandlers.contains(lId))
+    // {
+    // progressBarHandlers.remove(lId);
+    // }
+    // layout.setRows(layout.getRows() - 1);
+    // }
+    // else
+    // {
+    // progressPanel = new JPanel(new BorderLayout(10, 5));
+    //
+    // JProgressBar progressBar = new JProgressBar();
+    // progressBar.setIndeterminate(true);
+    //
+    // progressPanel.add(new JLabel(message), BorderLayout.WEST);
+    // progressPanel.add(progressBar, BorderLayout.CENTER);
+    //
+    // layout.setRows(layout.getRows() + 1);
+    // statusPanel.add(progressPanel);
+    //
+    // progressBars.put(lId, progressPanel);
+    // }
+    // // update GUI
+    // // setMenusForViewport();
+    // validate();
   }
 
   @Override
   public void registerHandler(final long id,
           final IProgressIndicatorHandler handler)
   {
-    if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
-    {
-      throw new Error(
-              "call setProgressBar before registering the progress bar's handler.");
-    }
-    progressBarHandlers.put(new Long(id), handler);
-    final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
-    if (handler.canCancel())
-    {
-      JButton cancel = new JButton("Cancel");
-      final IProgressIndicator us = this;
-      cancel.addActionListener(new ActionListener()
-      {
-
-        @Override
-        public void actionPerformed(ActionEvent e)
-        {
-          handler.cancelActivity(id);
-          us.setProgressBar(
-                  "Cancelled "
-                          + ((JLabel) progressPanel.getComponent(0))
-                                  .getText(), id);
-        }
-      });
-      progressPanel.add(cancel, BorderLayout.EAST);
-    }
+    progressBar.registerHandler(id, handler);
+    // if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
+    // {
+    // throw new
+    // Error(MessageManager.getString("error.call_setprogressbar_before_registering_handler"));
+    // }
+    // progressBarHandlers.put(new Long(id), handler);
+    // final JPanel progressPanel = (JPanel) progressBars.get(new Long(id));
+    // if (handler.canCancel())
+    // {
+    // JButton cancel = new JButton(
+    // MessageManager.getString("action.cancel"));
+    // final IProgressIndicator us = this;
+    // cancel.addActionListener(new ActionListener()
+    // {
+    //
+    // @Override
+    // public void actionPerformed(ActionEvent e)
+    // {
+    // handler.cancelActivity(id);
+    // us.setProgressBar(MessageManager.formatMessage("label.cancelled_params",
+    // new String[]{((JLabel) progressPanel.getComponent(0)).getText()}), id);
+    // }
+    // });
+    // progressPanel.add(cancel, BorderLayout.EAST);
+    // }
   }
 
   /**
@@ -651,11 +723,7 @@ public class PCAPanel extends GPCAPanel implements Runnable,
   @Override
   public boolean operationInProgress()
   {
-    if (progressBars != null && progressBars.size() > 0)
-    {
-      return true;
-    }
-    return false;
+    return progressBar.operationInProgress();
   }
 
   @Override