JAL-3949 Complete new abstracted logging framework in jalview.log. Updated log calls...
[jalview.git] / src / jalview / gui / PCAPanel.java
index caa2fc2..25f8363 100644 (file)
@@ -21,6 +21,7 @@
 package jalview.gui;
 
 import jalview.analysis.scoremodels.ScoreModels;
+import jalview.api.AlignViewportI;
 import jalview.api.analysis.ScoreModelI;
 import jalview.api.analysis.SimilarityParamsI;
 import jalview.bin.Cache;
@@ -29,6 +30,8 @@ import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AlignmentView;
 import jalview.datamodel.HiddenColumns;
 import jalview.datamodel.SequenceI;
+import jalview.gui.ImageExporter.ImageWriterI;
+import jalview.gui.JalviewColourChooser.ColourChooserListener;
 import jalview.jbgui.GPCAPanel;
 import jalview.math.RotatableMatrix.Axis;
 import jalview.util.ImageMaker;
@@ -48,7 +51,6 @@ import java.awt.print.PrinterException;
 import java.awt.print.PrinterJob;
 
 import javax.swing.ButtonGroup;
-import javax.swing.JColorChooser;
 import javax.swing.JMenuItem;
 import javax.swing.JRadioButtonMenuItem;
 import javax.swing.event.InternalFrameAdapter;
@@ -64,15 +66,15 @@ public class PCAPanel extends GPCAPanel
 
   private static final int MIN_HEIGHT = 250;
 
-  RotatableCanvas rc;
+  private RotatableCanvas rc;
 
   AlignmentPanel ap;
 
   AlignmentViewport av;
 
-  PCAModel pcaModel;
+  private PCAModel pcaModel;
 
-  int top = 0;
+  private int top = 0;
 
   private IProgressIndicator progressBar;
 
@@ -120,17 +122,15 @@ public class PCAPanel extends GPCAPanel
 
     ScoreModelI scoreModel = ScoreModels.getInstance()
             .getScoreModel(modelName, ap);
-    pcaModel = new PCAModel(seqstrings, seqs, nucleotide, scoreModel,
-            params);
+    setPcaModel(new PCAModel(seqstrings, seqs, nucleotide, scoreModel,
+            params));
     PaintRefresher.Register(this, av.getSequenceSetId());
 
-    rc = new RotatableCanvas(alignPanel);
-    this.getContentPane().add(rc, BorderLayout.CENTER);
+    setRotatableCanvas(new RotatableCanvas(alignPanel));
+    this.getContentPane().add(getRotatableCanvas(), BorderLayout.CENTER);
 
-    addKeyListener(rc);
+    addKeyListener(getRotatableCanvas());
     validate();
-
-    this.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
   }
 
   /**
@@ -139,21 +139,30 @@ public class PCAPanel extends GPCAPanel
    */
   protected void close_actionPerformed()
   {
-    pcaModel = null;
+    setPcaModel(null);
+    if (this.rc != null)
+    {
+      this.rc.sequencePoints = null;
+      this.rc.setAxisEndPoints(null);
+      this.rc = null;
+    }
   }
 
   @Override
   protected void bgcolour_actionPerformed()
   {
-    Color col = JColorChooser.showDialog(this,
-            MessageManager.getString("label.select_background_colour"),
-            rc.bgColour);
-
-    if (col != null)
+    String ttl = MessageManager.getString("label.select_background_colour");
+    ColourChooserListener listener = new ColourChooserListener()
     {
-      rc.bgColour = col;
-    }
-    rc.repaint();
+      @Override
+      public void colourSelected(Color c)
+      {
+        rc.setBgColour(c);
+        rc.repaint();
+      }
+    };
+    JalviewColourChooser.showColourChooser(this, ttl, rc.getBgColour(),
+            listener);
   }
 
   /**
@@ -162,6 +171,7 @@ public class PCAPanel extends GPCAPanel
   @Override
   public void run()
   {
+    working = true;
     long progId = System.currentTimeMillis();
     IProgressIndicator progress = this;
     String message = MessageManager.getString("label.pca_recalculating");
@@ -171,18 +181,17 @@ public class PCAPanel extends GPCAPanel
       message = MessageManager.getString("label.pca_calculating");
     }
     progress.setProgressBar(message, progId);
-    working = true;
     try
     {
-      pcaModel.calculate();
+      getPcaModel().calculate();
 
       xCombobox.setSelectedIndex(0);
       yCombobox.setSelectedIndex(1);
       zCombobox.setSelectedIndex(2);
 
-      pcaModel.updateRc(rc);
+      getPcaModel().updateRc(getRotatableCanvas());
       // rc.invalidate();
-      top = pcaModel.getTop();
+      setTop(getPcaModel().getTop());
 
     } catch (OutOfMemoryError er)
     {
@@ -199,8 +208,9 @@ public class PCAPanel extends GPCAPanel
     {
       Desktop.addInternalFrame(this,
               MessageManager.formatMessage("label.calc_title", "PCA",
-                      pcaModel.getScoreModelName()),
+                      getPcaModel().getScoreModelName()),
               475, 450);
+      this.setMinimumSize(new Dimension(MIN_WIDTH, MIN_HEIGHT));
     }
     working = false;
   }
@@ -212,16 +222,16 @@ public class PCAPanel extends GPCAPanel
   @Override
   protected void doDimensionChange()
   {
-    if (top == 0)
+    if (getTop() == 0)
     {
       return;
     }
 
-    int dim1 = top - xCombobox.getSelectedIndex();
-    int dim2 = top - yCombobox.getSelectedIndex();
-    int dim3 = top - zCombobox.getSelectedIndex();
-    pcaModel.updateRcView(dim1, dim2, dim3);
-    rc.resetView();
+    int dim1 = getTop() - xCombobox.getSelectedIndex();
+    int dim2 = getTop() - yCombobox.getSelectedIndex();
+    int dim3 = getTop() - zCombobox.getSelectedIndex();
+    getPcaModel().updateRcView(dim1, dim2, dim3);
+    getRotatableCanvas().resetView();
   }
 
   /**
@@ -254,7 +264,7 @@ public class PCAPanel extends GPCAPanel
     CutAndPasteTransfer cap = new CutAndPasteTransfer();
     try
     {
-      cap.setText(pcaModel.getDetails());
+      cap.setText(getPcaModel().getDetails());
       Desktop.addInternalFrame(cap,
               MessageManager.getString("label.pca_details"), 500, 500);
     } catch (OutOfMemoryError oom)
@@ -267,7 +277,7 @@ public class PCAPanel extends GPCAPanel
   @Override
   protected void showLabels_actionPerformed()
   {
-    rc.showLabels(showLabels.getState());
+    getRotatableCanvas().showLabels(showLabels.getState());
   }
 
   @Override
@@ -285,9 +295,9 @@ public class PCAPanel extends GPCAPanel
   public void originalSeqData_actionPerformed()
   {
     // JAL-2647 disabled after load from project (until save to project done)
-    if (pcaModel.getInputData() == null)
+    if (getPcaModel().getInputData() == null)
     {
-      Cache.log.info(
+      Cache.info(
               "Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
       return;
     }
@@ -307,7 +317,7 @@ public class PCAPanel extends GPCAPanel
     {
     }
 
-    Object[] alAndColsel = pcaModel.getInputData()
+    Object[] alAndColsel = getPcaModel().getInputData()
             .getAlignmentAndHiddenColumns(gc);
 
     if (alAndColsel != null && alAndColsel[0] != null)
@@ -389,11 +399,11 @@ public class PCAPanel extends GPCAPanel
     {
       pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
 
-      rc.drawBackground(pg);
-      rc.drawScene(pg);
-      if (rc.drawAxes)
+      getRotatableCanvas().drawBackground(pg);
+      getRotatableCanvas().drawScene(pg);
+      if (getRotatableCanvas().drawAxes)
       {
-        rc.drawAxes(pg);
+        getRotatableCanvas().drawAxes(pg);
       }
 
       if (pi == 0)
@@ -407,59 +417,27 @@ public class PCAPanel extends GPCAPanel
     }
   }
 
-  /**
-   * Handler for 'Save as EPS' option
-   */
-  @Override
-  protected void eps_actionPerformed()
-  {
-    makePCAImage(ImageMaker.TYPE.EPS);
-  }
-
-  /**
-   * Handler for 'Save as PNG' option
-   */
-  @Override
-  protected void png_actionPerformed()
-  {
-    makePCAImage(ImageMaker.TYPE.PNG);
-  }
-
-  void makePCAImage(ImageMaker.TYPE type)
+  public void makePCAImage(ImageMaker.TYPE type)
   {
-    int width = rc.getWidth();
-    int height = rc.getHeight();
-
-    ImageMaker im;
-
-    switch (type)
+    int width = getRotatableCanvas().getWidth();
+    int height = getRotatableCanvas().getHeight();
+    ImageWriterI writer = new ImageWriterI()
     {
-    case PNG:
-      im = new ImageMaker(this, ImageMaker.TYPE.PNG,
-              "Make PNG image from PCA", width, height, null, null, null, 0,
-              false);
-      break;
-    case EPS:
-      im = new ImageMaker(this, ImageMaker.TYPE.EPS,
-              "Make EPS file from PCA", width, height, null,
-              this.getTitle(), null, 0, false);
-      break;
-    default:
-      im = new ImageMaker(this, ImageMaker.TYPE.SVG,
-              "Make SVG file from PCA", width, height, null,
-              this.getTitle(), null, 0, false);
-    }
-
-    if (im.getGraphics() != null)
-    {
-      rc.drawBackground(im.getGraphics());
-      rc.drawScene(im.getGraphics());
-      if (rc.drawAxes)
+      @Override
+      public void exportImage(Graphics g) throws Exception
       {
-        rc.drawAxes(im.getGraphics());
+       RotatableCanvas canvas = getRotatableCanvas();
+       canvas.drawBackground(g);
+       canvas.drawScene(g);
+        if (canvas.drawAxes)
+        {
+          canvas.drawAxes(g);
+        }
       }
-      im.writeImage();
-    }
+    };
+    String pca = MessageManager.getString("label.pca");
+    ImageExporter exporter = new ImageExporter(writer, null, type, pca);
+    exporter.doExport(null, this, width, height, pca);
   }
 
   @Override
@@ -476,7 +454,7 @@ public class PCAPanel extends GPCAPanel
   {
     AlignmentPanel[] aps = PaintRefresher
             .getAssociatedPanels(av.getSequenceSetId());
-    if (aps.length == 1 && rc.av == aps[0].av)
+    if (aps.length == 1 && getRotatableCanvas().av == aps[0].av)
     {
       associateViewsMenu.setVisible(false);
       return;
@@ -499,18 +477,15 @@ public class PCAPanel extends GPCAPanel
     for (int i = 0; i < iSize; i++)
     {
       final AlignmentPanel panel = aps[i];
-      item = new JRadioButtonMenuItem(panel.av.viewName, panel.av == rc.av);
+      item = new JRadioButtonMenuItem(panel.av.getViewName(),
+              panel.av == getRotatableCanvas().av);
       buttonGroup.add(item);
       item.addActionListener(new ActionListener()
       {
         @Override
         public void actionPerformed(ActionEvent evt)
         {
-          rc.applyToAllViews = false;
-          rc.av = panel.av;
-          rc.ap = panel;
-          PaintRefresher.Register(PCAPanel.this,
-                  panel.av.getSequenceSetId());
+          selectAssociatedView(panel);
         }
       });
 
@@ -522,13 +497,13 @@ public class PCAPanel extends GPCAPanel
 
     buttonGroup.add(itemf);
 
-    itemf.setSelected(rc.applyToAllViews);
+    itemf.setSelected(getRotatableCanvas().isApplyToAllViews());
     itemf.addActionListener(new ActionListener()
     {
       @Override
       public void actionPerformed(ActionEvent evt)
       {
-        rc.applyToAllViews = itemf.isSelected();
+        getRotatableCanvas().setApplyToAllViews(itemf.isSelected());
       }
     });
     associateViewsMenu.add(itemf);
@@ -548,7 +523,7 @@ public class PCAPanel extends GPCAPanel
     CutAndPasteTransfer cap = new CutAndPasteTransfer();
     try
     {
-      cap.setText(pcaModel.getPointsasCsv(false,
+      cap.setText(getPcaModel().getPointsasCsv(false,
               xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
               zCombobox.getSelectedIndex()));
       Desktop.addInternalFrame(cap, MessageManager
@@ -574,7 +549,7 @@ public class PCAPanel extends GPCAPanel
     CutAndPasteTransfer cap = new CutAndPasteTransfer();
     try
     {
-      cap.setText(pcaModel.getPointsasCsv(true,
+      cap.setText(getPcaModel().getPointsasCsv(true,
               xCombobox.getSelectedIndex(), yCombobox.getSelectedIndex(),
               zCombobox.getSelectedIndex()));
       Desktop.addInternalFrame(cap, MessageManager.formatMessage(
@@ -603,11 +578,11 @@ public class PCAPanel extends GPCAPanel
     // }
     //
     // JPanel progressPanel;
-    // Long lId = new Long(id);
+    // Long lId = Long.valueOf(id);
     // GridLayout layout = (GridLayout) statusPanel.getLayout();
     // if (progressBars.get(lId) != null)
     // {
-    // progressPanel = (JPanel) progressBars.get(new Long(id));
+    // progressPanel = (JPanel) progressBars.get(Long.valueOf(id));
     // statusPanel.remove(progressPanel);
     // progressBars.remove(lId);
     // progressPanel = null;
@@ -646,13 +621,13 @@ public class PCAPanel extends GPCAPanel
           final IProgressIndicatorHandler handler)
   {
     progressBar.registerHandler(id, handler);
-    // if (progressBarHandlers == null || !progressBars.contains(new Long(id)))
+    // if (progressBarHandlers == null || !progressBars.contains(Long.valueOf(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));
+    // progressBarHandlers.put(Long.valueOf(id), handler);
+    // final JPanel progressPanel = (JPanel) progressBars.get(Long.valueOf(id));
     // if (handler.canCancel())
     // {
     // JButton cancel = new JButton(
@@ -686,11 +661,11 @@ public class PCAPanel extends GPCAPanel
   @Override
   protected void resetButton_actionPerformed()
   {
-    int t = top;
-    top = 0; // ugly - prevents dimensionChanged events from being processed
+    int t = getTop();
+    setTop(0); // ugly - prevents dimensionChanged events from being processed
     xCombobox.setSelectedIndex(0);
     yCombobox.setSelectedIndex(1);
-    top = t;
+    setTop(t);
     zCombobox.setSelectedIndex(2);
   }
 
@@ -738,7 +713,59 @@ public class PCAPanel extends GPCAPanel
    */
   public void setInputData(AlignmentView data)
   {
-    pcaModel.setInputData(data);
+    getPcaModel().setInputData(data);
     originalSeqData.setVisible(data != null);
   }
+
+  public AlignViewportI getAlignViewport()
+  {
+    return av;
+  }
+
+  public PCAModel getPcaModel()
+  {
+    return pcaModel;
+  }
+
+  public void setPcaModel(PCAModel pcaModel)
+  {
+    this.pcaModel = pcaModel;
+  }
+
+  public RotatableCanvas getRotatableCanvas()
+  {
+    return rc;
+  }
+
+  public void setRotatableCanvas(RotatableCanvas rc)
+  {
+    this.rc = rc;
+  }
+
+  public int getTop()
+  {
+    return top;
+  }
+
+  public void setTop(int top)
+  {
+    this.top = top;
+  }
+
+  /**
+   * set the associated view for this PCA.
+   * 
+   * @param panel
+   */
+  public void selectAssociatedView(AlignmentPanel panel)
+  {
+    getRotatableCanvas().setApplyToAllViews(false);
+
+    ap = panel;
+    av = panel.av;
+
+    getRotatableCanvas().av = panel.av;
+    getRotatableCanvas().ap = panel;
+    PaintRefresher.Register(PCAPanel.this, panel.av.getSequenceSetId());
+  }
 }