apply version 2.7 copyright
[jalview.git] / src / jalview / gui / TreePanel.java
index 3bb9108..d8253db 100755 (executable)
 /*
- * Jalview - A Sequence Alignment Editor and Viewer
- * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
- *
- * This program 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 2
- * of the License, or (at your option) any later version.
- *
- * This program 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 this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
+ * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
+ * 
+ * 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.
+ * 
+ * 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/>.
  */
 package jalview.gui;
 
-import jalview.analysis.*;
-
-import jalview.datamodel.*;
-
-import jalview.io.*;
-
-import jalview.jbgui.*;
-
-import org.jibble.epsgraphics.*;
+import java.beans.*;
+import java.io.*;
+import java.util.*;
+import javax.imageio.*;
 
 import java.awt.*;
 import java.awt.event.*;
 import java.awt.image.*;
-
-import java.io.*;
-
-import java.util.*;
-
 import javax.swing.*;
-import javax.imageio.*;
-
-import java.beans.PropertyChangeEvent;
 
+import org.jibble.epsgraphics.*;
+import jalview.analysis.*;
+import jalview.commands.CommandI;
+import jalview.commands.OrderCommand;
+import jalview.datamodel.*;
+import jalview.io.*;
+import jalview.jbgui.*;
 
 /**
  * DOCUMENT ME!
- *
+ * 
  * @author $author$
  * @version $Revision$
  */
 public class TreePanel extends GTreePanel
 {
-    String type;
-    String pwtype;
-    TreeCanvas treeCanvas;
-    NJTree tree;
-    AlignViewport av;
-
-    /**
-     * Creates a new TreePanel object.
-     *
-     * @param av DOCUMENT ME!
-     * @param seqVector DOCUMENT ME!
-     * @param type DOCUMENT ME!
-     * @param pwtype DOCUMENT ME!
-     * @param s DOCUMENT ME!
-     * @param e DOCUMENT ME!
-     */
-    public TreePanel(AlignmentPanel ap, String type, String pwtype)
+  String type;
+
+  String pwtype;
+
+  TreeCanvas treeCanvas;
+
+  NJTree tree;
+
+  AlignViewport av;
+
+  /**
+   * Creates a new TreePanel object.
+   * 
+   * @param av
+   *          DOCUMENT ME!
+   * @param seqVector
+   *          DOCUMENT ME!
+   * @param type
+   *          DOCUMENT ME!
+   * @param pwtype
+   *          DOCUMENT ME!
+   * @param s
+   *          DOCUMENT ME!
+   * @param e
+   *          DOCUMENT ME!
+   */
+  public TreePanel(AlignmentPanel ap, String type, String pwtype)
+  {
+    super();
+    initTreePanel(ap, type, pwtype, null, null);
+
+    // We know this tree has distances. JBPNote TODO: prolly should add this as
+    // a userdefined default
+    // showDistances(true);
+  }
+
+  /**
+   * Creates a new TreePanel object.
+   * 
+   * @param av
+   *          DOCUMENT ME!
+   * @param seqVector
+   *          DOCUMENT ME!
+   * @param newtree
+   *          DOCUMENT ME!
+   * @param type
+   *          DOCUMENT ME!
+   * @param pwtype
+   *          DOCUMENT ME!
+   */
+  public TreePanel(AlignmentPanel ap, String type, String pwtype,
+          NewickFile newtree)
+  {
+    super();
+    initTreePanel(ap, type, pwtype, newtree, null);
+  }
+
+  public TreePanel(AlignmentPanel av, String type, String pwtype,
+          NewickFile newtree, AlignmentView inputData)
+  {
+    super();
+    initTreePanel(av, type, pwtype, newtree, inputData);
+  }
+
+  public AlignmentI getAlignment()
+  {
+    return treeCanvas.av.getAlignment();
+  }
+
+  public AlignViewport getViewPort()
+  {
+    return treeCanvas.av;
+  }
+
+  void initTreePanel(AlignmentPanel ap, String type, String pwtype,
+          NewickFile newTree, AlignmentView inputData)
+  {
+
+    av = ap.av;
+    this.type = type;
+    this.pwtype = pwtype;
+
+    treeCanvas = new TreeCanvas(this, ap, scrollPane);
+    scrollPane.setViewportView(treeCanvas);
+
+    PaintRefresher.Register(this, ap.av.getSequenceSetId());
+
+    buildAssociatedViewMenu();
+
+    av.addPropertyChangeListener(new java.beans.PropertyChangeListener()
     {
-      super();
-      initTreePanel(ap, type, pwtype, null, null);
+      public void propertyChange(PropertyChangeEvent evt)
+      {
+        if (evt.getPropertyName().equals("alignment"))
+        {
+          if (tree == null)
+          {
+            System.out.println("tree is null");
+            // TODO: deal with case when a change event is received whilst a
+            // tree is still being calculated - should save reference for
+            // processing message later.
+            return;
+          }
+          if (evt.getNewValue() == null)
+          {
+            System.out
+                    .println("new alignment sequences vector value is null");
+          }
 
-      // We know this tree has distances. JBPNote TODO: prolly should add this as a userdefined default
-      // showDistances(true);
-    }
+          tree.UpdatePlaceHolders((Vector) evt.getNewValue());
+          treeCanvas.nameHash.clear(); // reset the mapping between canvas
+          // rectangles and leafnodes
+          repaint();
+        }
+      }
+    });
 
-    /**
-     * Creates a new TreePanel object.
-     *
-     * @param av DOCUMENT ME!
-     * @param seqVector DOCUMENT ME!
-     * @param newtree DOCUMENT ME!
-     * @param type DOCUMENT ME!
-     * @param pwtype DOCUMENT ME!
-     */
-    public TreePanel(AlignmentPanel ap,
-                     String type,
-                     String pwtype,
-                     NewickFile newtree)
+    TreeLoader tl = new TreeLoader(newTree);
+    if (inputData != null)
     {
-      super();
-      initTreePanel(ap, type, pwtype, newtree, null);
+      tl.odata = inputData;
     }
+    tl.start();
 
-    public TreePanel(AlignmentPanel av,
-        String type,
-        String pwtype,
-        NewickFile newtree, AlignmentView inputData) {
-     super();
-     initTreePanel(av,type,pwtype,newtree,inputData);
-    }
+  }
 
-    public AlignmentI getAlignment()
-    {
-      return treeCanvas.av.getAlignment();
-    }
-    public AlignViewport getViewPort() {
-      return treeCanvas.av;
-    }
+  public void viewMenu_menuSelected()
+  {
+    buildAssociatedViewMenu();
+  }
 
-    void initTreePanel(AlignmentPanel ap, String type,  String pwtype,
-                       NewickFile newTree, AlignmentView inputData)
+  void buildAssociatedViewMenu()
+  {
+    AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av
+            .getSequenceSetId());
+    if (aps.length == 1 && treeCanvas.ap == aps[0])
     {
+      associateLeavesMenu.setVisible(false);
+      return;
+    }
 
-      av = ap.av;
-      this.type = type;
-      this.pwtype = pwtype;
+    associateLeavesMenu.setVisible(true);
 
-      buildAssociatedViewMenu();
+    if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem))
+    {
+      viewMenu.insertSeparator(viewMenu.getItemCount() - 1);
+    }
 
-      treeCanvas = new TreeCanvas(ap, scrollPane);
-      scrollPane.setViewportView(treeCanvas);
+    associateLeavesMenu.removeAll();
 
-      av.addPropertyChangeListener(new java.beans.PropertyChangeListener()
+    JRadioButtonMenuItem item;
+    ButtonGroup buttonGroup = new ButtonGroup();
+    int i, iSize = aps.length;
+    final TreePanel thisTreePanel = this;
+    for (i = 0; i < iSize; i++)
+    {
+      final AlignmentPanel ap = aps[i];
+      item = new JRadioButtonMenuItem(ap.av.viewName, ap == treeCanvas.ap);
+      buttonGroup.add(item);
+      item.addActionListener(new ActionListener()
       {
-        public void propertyChange(PropertyChangeEvent evt)
+        public void actionPerformed(ActionEvent evt)
         {
-          if (evt.getPropertyName().equals("alignment"))
-          {
-            if(tree==null)
-              System.out.println("tree is null");
-            if(evt.getNewValue()==null)
-              System.out.println("new alignment sequences vector value is null");
-
-            tree.UpdatePlaceHolders( (Vector) evt.getNewValue());
-            treeCanvas.nameHash.clear(); // reset the mapping between canvas rectangles and leafnodes
-            repaint();
-          }
+          treeCanvas.applyToAllViews = false;
+          treeCanvas.ap = ap;
+          treeCanvas.av = ap.av;
+          PaintRefresher.Register(thisTreePanel, ap.av.getSequenceSetId());
         }
       });
 
-      TreeLoader tl = new TreeLoader(newTree);
-      if (inputData!=null) {
-        tl.odata=inputData;
-      }
-      tl.start();
-
+      associateLeavesMenu.add(item);
     }
 
-    void buildAssociatedViewMenu()
+    final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
+    buttonGroup.add(itemf);
+    itemf.setSelected(treeCanvas.applyToAllViews);
+    itemf.addActionListener(new ActionListener()
     {
-      AlignmentPanel [] aps = PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
-      if(aps.length<2)
+      public void actionPerformed(ActionEvent evt)
       {
-        associateLeavesMenu.setVisible(false);
-        return;
+        treeCanvas.applyToAllViews = itemf.isSelected();
       }
-      associateLeavesMenu.addSeparator();
+    });
+    associateLeavesMenu.add(itemf);
 
-      JRadioButtonMenuItem item;
-      ButtonGroup buttonGroup = new ButtonGroup();
-      int i, iSize = aps.length;
-      for(i=0; i<iSize; i++)
-      {
-          final AlignmentPanel ap = aps[i];
-          item = new JRadioButtonMenuItem(ap.av.viewName, ap.av==av );
-          buttonGroup.add(item);
-          item.addActionListener(new ActionListener()
-              {
-                public void actionPerformed(ActionEvent evt)
-                {
-                  treeCanvas.applyToAllViews = false;
-                  treeCanvas.ap = ap;
-                  treeCanvas.av = ap.av;
-                  PaintRefresher.RemoveComponent(treeCanvas);
-                  PaintRefresher.Register(treeCanvas, ap.av.getSequenceSetId());
-                }
-              });
+  }
 
-          associateLeavesMenu.add(item);
-      }
+  class TreeLoader extends Thread
+  {
+    NewickFile newtree;
 
-      final JRadioButtonMenuItem itemf = new JRadioButtonMenuItem("All Views");
-      buttonGroup.add(itemf);
-      itemf.addActionListener(new ActionListener()
-      {
-        public void actionPerformed(ActionEvent evt)
-        {
-          treeCanvas.applyToAllViews = itemf.isSelected();
-        }
-      });
-      associateLeavesMenu.add(itemf);
+    jalview.datamodel.AlignmentView odata = null;
 
+    public TreeLoader(NewickFile newtree)
+    {
+      this.newtree = newtree;
+      if (newtree != null)
+      {
+        // Must be outside run(), as Jalview2XML tries to
+        // update distance/bootstrap visibility at the same time
+        showBootstrap(newtree.HasBootstrap());
+        showDistances(newtree.HasDistances());
+      }
     }
 
-    class TreeLoader extends Thread
+    public void run()
     {
-      NewickFile newtree;
-      jalview.datamodel.AlignmentView odata=null;
-      public TreeLoader(NewickFile newtree)
+
+      if (newtree != null)
       {
-        this.newtree = newtree;
-        if (newtree != null)
+        if (odata == null)
         {
-          // Must be outside run(), as Jalview2XML tries to
-          // update distance/bootstrap visibility at the same time
-          showBootstrap(newtree.HasBootstrap());
-          showDistances(newtree.HasDistances());
+          tree = new NJTree(av.alignment.getSequencesArray(), newtree);
+        }
+        else
+        {
+          tree = new NJTree(av.alignment.getSequencesArray(), odata,
+                  newtree);
+        }
+        if (!tree.hasOriginalSequenceData())
+        {
+          allowOriginalSeqData(false);
         }
       }
-
-      public void run()
+      else
       {
-
-        if(newtree!=null)
+        int start, end;
+        SequenceI[] seqs;
+        AlignmentView seqStrings = av.getAlignmentView(av
+                .getSelectionGroup() != null);
+        if (av.getSelectionGroup() == null)
         {
-          if (odata==null) {
-            tree = new NJTree(av.alignment.getSequencesArray(),
-                              newtree);
-          } else {
-            tree = new NJTree(av.alignment.getSequencesArray(), odata, newtree);
-          }
-          if (!tree.hasOriginalSequenceData())
-            allowOriginalSeqData(false);
+          start = 0;
+          end = av.alignment.getWidth();
+          seqs = av.alignment.getSequencesArray();
         }
         else
         {
-          int start, end;
-          SequenceI [] seqs;
-          AlignmentView seqStrings = av.getAlignmentView(av.getSelectionGroup()!=null);
-          if(av.getSelectionGroup()==null)
-          {
-            start = 0;
-            end = av.alignment.getWidth();
-            seqs = av.alignment.getSequencesArray();
-          }
-          else
-          {
-            start = av.getSelectionGroup().getStartRes();
-            end = av.getSelectionGroup().getEndRes()+1;
-            seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment);
-          }
-
-          tree = new NJTree(seqs, seqStrings, type, pwtype, start, end);
-          showDistances(true);
+          start = av.getSelectionGroup().getStartRes();
+          end = av.getSelectionGroup().getEndRes() + 1;
+          seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment);
         }
 
+        tree = new NJTree(seqs, seqStrings, type, pwtype, start, end);
+        showDistances(true);
+      }
 
-        tree.reCount(tree.getTopNode());
-        tree.findHeight(tree.getTopNode());
-        treeCanvas.setTree(tree);
-        treeCanvas.repaint();
-        av.setCurrentTree(tree);
-
+      tree.reCount(tree.getTopNode());
+      tree.findHeight(tree.getTopNode());
+      treeCanvas.setTree(tree);
+      treeCanvas.repaint();
+      av.setCurrentTree(tree);
+      if (av.getSortByTree())
+      {
+        sortByTree_actionPerformed(null);
       }
     }
-
-    public void showDistances(boolean b)
+  }
+
+  public void showDistances(boolean b)
+  {
+    treeCanvas.setShowDistances(b);
+    distanceMenu.setSelected(b);
+  }
+
+  public void showBootstrap(boolean b)
+  {
+    treeCanvas.setShowBootstrap(b);
+    bootstrapMenu.setSelected(b);
+  }
+
+  public void showPlaceholders(boolean b)
+  {
+    placeholdersMenu.setState(b);
+    treeCanvas.setMarkPlaceholders(b);
+  }
+
+  private void allowOriginalSeqData(boolean b)
+  {
+    originalSeqData.setVisible(b);
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @return DOCUMENT ME!
+   */
+  public NJTree getTree()
+  {
+    return tree;
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @param e
+   *          DOCUMENT ME!
+   */
+  public void textbox_actionPerformed(ActionEvent e)
+  {
+    CutAndPasteTransfer cap = new CutAndPasteTransfer();
+
+    StringBuffer buffer = new StringBuffer();
+
+    if (type.equals("AV"))
     {
-      treeCanvas.setShowDistances(b);
-      distanceMenu.setSelected(b);
+      buffer.append("Average distance tree using ");
     }
-
-    public void showBootstrap(boolean b)
+    else
     {
-      treeCanvas.setShowBootstrap(b);
-      bootstrapMenu.setSelected(b);
+      buffer.append("Neighbour joining tree using ");
     }
 
-    public void showPlaceholders(boolean b)
+    if (pwtype.equals("BL"))
     {
-      placeholdersMenu.setState(b);
-      treeCanvas.setMarkPlaceholders(b);
+      buffer.append("BLOSUM62");
     }
-
-    private void allowOriginalSeqData(boolean b) {
-      originalSeqData.setVisible(b);
+    else
+    {
+      buffer.append("PID");
     }
 
-
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @return DOCUMENT ME!
-     */
-    public NJTree getTree()
+    jalview.io.NewickFile fout = new jalview.io.NewickFile(
+            tree.getTopNode());
+    try
     {
-        return tree;
+      cap.setText(fout.print(tree.isHasBootstrap(), tree.isHasDistances(),
+              tree.isHasRootDistance()));
+      Desktop.addInternalFrame(cap, buffer.toString(), 500, 100);
+    } catch (OutOfMemoryError oom)
+    {
+      new OOMWarning("generating newick tree file", oom);
+      cap.dispose();
     }
 
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @param e DOCUMENT ME!
-     */
-    public void textbox_actionPerformed(ActionEvent e)
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @param e
+   *          DOCUMENT ME!
+   */
+  public void saveAsNewick_actionPerformed(ActionEvent e)
+  {
+    JalviewFileChooser chooser = new JalviewFileChooser(
+            jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
+    chooser.setFileView(new JalviewFileView());
+    chooser.setDialogTitle("Save tree as newick file");
+    chooser.setToolTipText("Save");
+
+    int value = chooser.showSaveDialog(null);
+
+    if (value == JalviewFileChooser.APPROVE_OPTION)
     {
-        CutAndPasteTransfer cap = new CutAndPasteTransfer();
-
-        StringBuffer buffer = new StringBuffer();
-
-        if (type.equals("AV"))
-        {
-            buffer.append("Average distance tree using ");
-        }
-        else
-        {
-            buffer.append("Neighbour joining tree using ");
-        }
-
-        if (pwtype.equals("BL"))
-        {
-            buffer.append("BLOSUM62");
-        }
-        else
-        {
-            buffer.append("PID");
-        }
+      String choice = chooser.getSelectedFile().getPath();
+      jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
+              .getSelectedFile().getParent());
 
-        Desktop.addInternalFrame(cap, buffer.toString(), 500, 100);
-
-        jalview.io.NewickFile fout = new jalview.io.NewickFile(tree.getTopNode());
-        cap.setText(fout.print(tree.isHasBootstrap(), tree.isHasDistances(), tree.isHasRootDistance()));
+      try
+      {
+        jalview.io.NewickFile fout = new jalview.io.NewickFile(
+                tree.getTopNode());
+        String output = fout.print(tree.isHasBootstrap(),
+                tree.isHasDistances(), tree.isHasRootDistance());
+        java.io.PrintWriter out = new java.io.PrintWriter(
+                new java.io.FileWriter(choice));
+        out.println(output);
+        out.close();
+      } catch (Exception ex)
+      {
+        ex.printStackTrace();
+      }
     }
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @param e DOCUMENT ME!
-     */
-    public void saveAsNewick_actionPerformed(ActionEvent e)
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @param e
+   *          DOCUMENT ME!
+   */
+  public void printMenu_actionPerformed(ActionEvent e)
+  {
+    // Putting in a thread avoids Swing painting problems
+    treeCanvas.startPrinting();
+  }
+
+  public void originalSeqData_actionPerformed(ActionEvent e)
+  {
+    if (!tree.hasOriginalSequenceData())
     {
-        JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
-                    "LAST_DIRECTORY"));
-        chooser.setFileView(new JalviewFileView());
-        chooser.setDialogTitle("Save tree as newick file");
-        chooser.setToolTipText("Save");
-
-        int value = chooser.showSaveDialog(null);
-
-        if (value == JalviewFileChooser.APPROVE_OPTION)
-        {
-            String choice = chooser.getSelectedFile().getPath();
-            jalview.bin.Cache.setProperty("LAST_DIRECTORY",
-                chooser.getSelectedFile().getParent());
-
-            try
-            {
-                jalview.io.NewickFile fout = new jalview.io.NewickFile(tree.getTopNode());
-                String output = fout.print(tree.isHasBootstrap(), tree.isHasDistances(), tree.isHasRootDistance());
-                java.io.PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
-                            choice));
-                out.println(output);
-                out.close();
-            }
-            catch (Exception ex)
-            {
-                ex.printStackTrace();
-            }
-        }
+      jalview.bin.Cache.log
+              .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
+      return;
     }
+    // decide if av alignment is sufficiently different to original data to
+    // warrant a new window to be created
+    // create new alignmnt window with hidden regions (unhiding hidden regions
+    // yields unaligned seqs)
+    // or create a selection box around columns in alignment view
+    // test Alignment(SeqCigar[])
+    char gc = '-';
+    try
+    {
+      // we try to get the associated view's gap character
+      // but this may fail if the view was closed...
+      gc = av.getGapCharacter();
 
-    /**
-     * DOCUMENT ME!
-     *
-     * @param e DOCUMENT ME!
-     */
-    public void printMenu_actionPerformed(ActionEvent e)
+    } catch (Exception ex)
     {
-        //Putting in a thread avoids Swing painting problems
-        treeCanvas.startPrinting();
     }
+    ;
+    Object[] alAndColsel = tree.seqData.getAlignmentAndColumnSelection(gc);
 
-
-    public void originalSeqData_actionPerformed(ActionEvent e)
+    if (alAndColsel != null && alAndColsel[0] != null)
     {
-      if (!tree.hasOriginalSequenceData())
+      // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
+
+      Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
+      Alignment dataset = (av != null && av.getAlignment() != null) ? av
+              .getAlignment().getDataset() : null;
+      if (dataset != null)
       {
-        jalview.bin.Cache.log.info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
-        return;
+        al.setDataset(dataset);
       }
-      // decide if av alignment is sufficiently different to original data to warrant a new window to be created
-      // create new alignmnt window with hidden regions (unhiding hidden regions yields unaligned seqs)
-      // or create a selection box around columns in alignment view
-      // test Alignment(SeqCigar[])
-      Object[] alAndColsel = tree.seqData.getAlignmentAndColumnSelection(av.
-          getGapCharacter());
-
-
-      if (alAndColsel != null && alAndColsel[0]!=null)
-       {
-         // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
-
-         Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
-         Alignment dataset = av.getAlignment().getDataset();
-         if (dataset != null)
-         {
-           al.setDataset(dataset);
-         }
-
-         if (true)
-         {
-           // make a new frame!
-           AlignFrame af = new AlignFrame(al, (ColumnSelection) alAndColsel[1],
-                                           AlignFrame.DEFAULT_WIDTH,
-                                           AlignFrame.DEFAULT_HEIGHT
-);
-
-           //>>>This is a fix for the moment, until a better solution is found!!<<<
-           // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
-
-       //           af.addSortByOrderMenuItem(ServiceName + " Ordering",
-       //                                     msaorder);
-
-           Desktop.addInternalFrame(af, "Original Data for " + this.title,
-                                    AlignFrame.DEFAULT_WIDTH,
-                                    AlignFrame.DEFAULT_HEIGHT);
-         }
-       }
-    }
 
+      if (true)
+      {
+        // make a new frame!
+        AlignFrame af = new AlignFrame(al,
+                (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH,
+                AlignFrame.DEFAULT_HEIGHT);
 
-    /**
-     * DOCUMENT ME!
-     *
-     * @param e DOCUMENT ME!
-     */
-    public void fitToWindow_actionPerformed(ActionEvent e)
-    {
-        treeCanvas.fitToWindow = fitToWindow.isSelected();
-        repaint();
-    }
+        // >>>This is a fix for the moment, until a better solution is
+        // found!!<<<
+        // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
 
-    /**
-     * DOCUMENT ME!
-     *
-     * @param e DOCUMENT ME!
-     */
-    public void font_actionPerformed(ActionEvent e)
+        // af.addSortByOrderMenuItem(ServiceName + " Ordering",
+        // msaorder);
+
+        Desktop.addInternalFrame(af, "Original Data for " + this.title,
+                AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT);
+      }
+    }
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @param e
+   *          DOCUMENT ME!
+   */
+  public void fitToWindow_actionPerformed(ActionEvent e)
+  {
+    treeCanvas.fitToWindow = fitToWindow.isSelected();
+    repaint();
+  }
+
+  /**
+   * sort the associated alignment view by the current tree.
+   * 
+   * @param e
+   */
+  public void sortByTree_actionPerformed(ActionEvent e)
+  {
+    
+    if (treeCanvas.applyToAllViews)
     {
-        if (treeCanvas == null)
+      final ArrayList<CommandI> commands = new ArrayList<CommandI>();
+      for (AlignmentPanel ap: PaintRefresher.getAssociatedPanels(av
+              .getSequenceSetId()))
+      {
+        commands.add(sortAlignmentIn(ap.av.getAlignPanel()));
+      }
+      av.getAlignPanel().alignFrame.addHistoryItem(new CommandI()
+      {
+        
+        @Override
+        public void undoCommand(AlignmentI[] views)
         {
-            return;
+          for (CommandI tsort:commands)
+          {
+            tsort.undoCommand(views);
+          }          
+        }
+        
+        @Override
+        public int getSize()
+        {
+          return commands.size();
         }
+        
+        @Override
+        public String getDescription()
+        {
+          return "Tree Sort (many views)";
+        }
+        
+        @Override
+        public void doCommand(AlignmentI[] views)
+        {
 
-        new FontChooser(this);
+          for (CommandI tsort:commands)
+          {
+            tsort.doCommand(views);
+          }          
+        }
+      });
+      for (AlignmentPanel ap: PaintRefresher.getAssociatedPanels(av
+              .getSequenceSetId()))
+      {
+        // ensure all the alignFrames refresh their GI after adding an undo item
+        ap.alignFrame.updateEditMenuBar();
+      }
+    } else {
+      treeCanvas.ap.alignFrame.addHistoryItem(sortAlignmentIn(treeCanvas.ap));
     }
 
-    public Font getTreeFont()
+  }
+  public CommandI sortAlignmentIn(AlignmentPanel ap)
+  {
+    AlignViewport av = ap.av;
+    SequenceI[] oldOrder = av.getAlignment().getSequencesArray();
+    AlignmentSorter.sortByTree(av.getAlignment(), tree);
+    CommandI undo;
+    undo=new OrderCommand("Tree Sort", oldOrder,
+          av.alignment);
+
+    ap.paintAlignment(true);    
+    return undo;
+  }
+  /**
+   * DOCUMENT ME!
+   * 
+   * @param e
+   *          DOCUMENT ME!
+   */
+  public void font_actionPerformed(ActionEvent e)
+  {
+    if (treeCanvas == null)
     {
-        return treeCanvas.font;
+      return;
     }
 
-    public void setTreeFont(Font font)
+    new FontChooser(this);
+  }
+
+  public Font getTreeFont()
+  {
+    return treeCanvas.font;
+  }
+
+  public void setTreeFont(Font font)
+  {
+    if (treeCanvas != null)
     {
-      if(treeCanvas!=null)
       treeCanvas.setFont(font);
     }
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @param e
+   *          DOCUMENT ME!
+   */
+  public void distanceMenu_actionPerformed(ActionEvent e)
+  {
+    treeCanvas.setShowDistances(distanceMenu.isSelected());
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @param e
+   *          DOCUMENT ME!
+   */
+  public void bootstrapMenu_actionPerformed(ActionEvent e)
+  {
+    treeCanvas.setShowBootstrap(bootstrapMenu.isSelected());
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @param e
+   *          DOCUMENT ME!
+   */
+  public void placeholdersMenu_actionPerformed(ActionEvent e)
+  {
+    treeCanvas.setMarkPlaceholders(placeholdersMenu.isSelected());
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @param e
+   *          DOCUMENT ME!
+   */
+  public void epsTree_actionPerformed(ActionEvent e)
+  {
+    boolean accurateText = true;
+
+    String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
+            "Prompt each time");
 
-    /**
-     * DOCUMENT ME!
-     *
-     * @param e DOCUMENT ME!
-     */
-    public void distanceMenu_actionPerformed(ActionEvent e)
+    // If we need to prompt, and if the GUI is visible then
+    // Prompt for EPS rendering style
+    if (renderStyle.equalsIgnoreCase("Prompt each time")
+            && !(System.getProperty("java.awt.headless") != null && System
+                    .getProperty("java.awt.headless").equals("true")))
     {
-        treeCanvas.setShowDistances(distanceMenu.isSelected());
-    }
+      EPSOptions eps = new EPSOptions();
+      renderStyle = eps.getValue();
+
+      if (renderStyle == null || eps.cancelled)
+      {
+        return;
+      }
 
-    /**
-     * DOCUMENT ME!
-     *
-     * @param e DOCUMENT ME!
-     */
-    public void bootstrapMenu_actionPerformed(ActionEvent e)
-    {
-        treeCanvas.setShowBootstrap(bootstrapMenu.isSelected());
     }
 
-    /**
-     * DOCUMENT ME!
-     *
-     * @param e DOCUMENT ME!
-     */
-    public void placeholdersMenu_actionPerformed(ActionEvent e)
+    if (renderStyle.equalsIgnoreCase("text"))
     {
-        treeCanvas.setMarkPlaceholders(placeholdersMenu.isSelected());
+      accurateText = false;
     }
 
-    /**
-     * DOCUMENT ME!
-     *
-     * @param e DOCUMENT ME!
-     */
-    public void epsTree_actionPerformed(ActionEvent e)
+    int width = treeCanvas.getWidth();
+    int height = treeCanvas.getHeight();
+
+    try
     {
-      boolean accurateText = true;
+      jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(
+              jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
+              { "eps" }, new String[]
+              { "Encapsulated Postscript" }, "Encapsulated Postscript");
+      chooser.setFileView(new jalview.io.JalviewFileView());
+      chooser.setDialogTitle("Create EPS file from tree");
+      chooser.setToolTipText("Save");
 
-      String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
-          "Prompt each time");
+      int value = chooser.showSaveDialog(this);
 
-    // If we need to prompt, and if the GUI is visible then
-    // Prompt for EPS rendering style
-      if (renderStyle.equalsIgnoreCase("Prompt each time")
-          && !
-          (System.getProperty("java.awt.headless") != null
-           && System.getProperty("java.awt.headless").equals("true")))
+      if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION)
       {
-        EPSOptions eps = new EPSOptions();
-        renderStyle = eps.getValue();
+        return;
+      }
 
-        if (renderStyle==null || eps.cancelled)
-          return;
+      jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
+              .getSelectedFile().getParent());
 
+      FileOutputStream out = new FileOutputStream(chooser.getSelectedFile());
+      EpsGraphics2D pg = new EpsGraphics2D("Tree", out, 0, 0, width, height);
 
-      }
+      pg.setAccurateTextMode(accurateText);
 
-      if (renderStyle.equalsIgnoreCase("text"))
-      {
-        accurateText = false;
-      }
+      treeCanvas.draw(pg, width, height);
 
-        int width = treeCanvas.getWidth();
-        int height = treeCanvas.getHeight();
+      pg.flush();
+      pg.close();
+    } catch (Exception ex)
+    {
+      ex.printStackTrace();
+    }
+  }
+
+  /**
+   * DOCUMENT ME!
+   * 
+   * @param e
+   *          DOCUMENT ME!
+   */
+  public void pngTree_actionPerformed(ActionEvent e)
+  {
+    int width = treeCanvas.getWidth();
+    int height = treeCanvas.getHeight();
+
+    try
+    {
+      jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(
+              jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
+              { "png" }, new String[]
+              { "Portable network graphics" }, "Portable network graphics");
 
-        try
-        {
-            jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(
-                        "LAST_DIRECTORY"), new String[] { "eps" },
-                    new String[] { "Encapsulated Postscript" },
-                    "Encapsulated Postscript");
-            chooser.setFileView(new jalview.io.JalviewFileView());
-            chooser.setDialogTitle("Create EPS file from tree");
-            chooser.setToolTipText("Save");
+      chooser.setFileView(new jalview.io.JalviewFileView());
+      chooser.setDialogTitle("Create PNG image from tree");
+      chooser.setToolTipText("Save");
 
-            int value = chooser.showSaveDialog(this);
+      int value = chooser.showSaveDialog(this);
 
-            if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION)
-            {
-                return;
-            }
+      if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION)
+      {
+        return;
+      }
 
-            jalview.bin.Cache.setProperty("LAST_DIRECTORY",
-                                          chooser.getSelectedFile().getParent());
+      jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
+              .getSelectedFile().getParent());
 
-            FileOutputStream out = new FileOutputStream(chooser.getSelectedFile());
-            EpsGraphics2D pg = new EpsGraphics2D("Tree", out, 0, 0, width,
-                                                 height);
+      FileOutputStream out = new FileOutputStream(chooser.getSelectedFile());
 
-            pg.setAccurateTextMode(accurateText);
+      BufferedImage bi = new BufferedImage(width, height,
+              BufferedImage.TYPE_INT_RGB);
+      Graphics png = bi.getGraphics();
 
-            treeCanvas.draw(pg, width, height);
+      treeCanvas.draw(png, width, height);
 
-            pg.flush();
-            pg.close();
-        }
-        catch (Exception ex)
-        {
-            ex.printStackTrace();
-        }
+      ImageIO.write(bi, "png", out);
+      out.close();
+    } catch (Exception ex)
+    {
+      ex.printStackTrace();
     }
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @param e DOCUMENT ME!
-     */
-    public void pngTree_actionPerformed(ActionEvent e)
+  }
+
+  /**
+   * change node labels to the annotation referred to by labelClass TODO:
+   * promote to a datamodel modification that can be undone TODO: make argument
+   * one case of a generic transformation function ie { undoStep = apply(Tree,
+   * TransformFunction)};
+   * 
+   * @param labelClass
+   */
+  public void changeNames(final String labelClass)
+  {
+    tree.applyToNodes(new NodeTransformI()
     {
-        int width = treeCanvas.getWidth();
-        int height = treeCanvas.getHeight();
 
-        try
+      public void transform(BinaryNode node)
+      {
+        if (node instanceof SequenceNode
+                && !((SequenceNode) node).isPlaceholder()
+                && !((SequenceNode) node).isDummy())
         {
-            jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(
-                        "LAST_DIRECTORY"), new String[] { "png" },
-                    new String[] { "Portable network graphics" },
-                    "Portable network graphics");
-
-            chooser.setFileView(new jalview.io.JalviewFileView());
-            chooser.setDialogTitle("Create PNG image from tree");
-            chooser.setToolTipText("Save");
-
-            int value = chooser.showSaveDialog(this);
-
-            if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION)
+          String newname = null;
+          SequenceI sq = (SequenceI) ((SequenceNode) node).element();
+          if (sq != null)
+          {
+            // search dbrefs, features and annotation
+            DBRefEntry[] refs = jalview.util.DBRefUtils.selectRefs(
+                    sq.getDBRef(), new String[]
+                    { labelClass.toUpperCase() });
+            if (refs != null)
             {
-                return;
+              for (int i = 0; i < refs.length; i++)
+              {
+                if (newname == null)
+                {
+                  newname = new String(refs[i].getAccessionId());
+                }
+                else
+                {
+                  newname = newname + "; " + refs[i].getAccessionId();
+                }
+              }
             }
-
-            jalview.bin.Cache.setProperty("LAST_DIRECTORY",
-                chooser.getSelectedFile().getParent());
-
-            FileOutputStream out = new FileOutputStream(chooser.getSelectedFile());
-
-            BufferedImage bi = new BufferedImage(width, height,
-                    BufferedImage.TYPE_INT_RGB);
-            Graphics png = bi.getGraphics();
-
-            treeCanvas.draw(png, width, height);
-
-            ImageIO.write(bi, "png", out);
-            out.close();
-        }
-        catch (Exception ex)
-        {
-            ex.printStackTrace();
+            if (newname == null)
+            {
+              SequenceFeature sf[] = sq.getSequenceFeatures();
+              for (int i = 0; sf != null && i < sf.length; i++)
+              {
+                if (sf[i].getType().equals(labelClass))
+                {
+                  if (newname == null)
+                  {
+                    newname = new String(sf[i].getDescription());
+                  }
+                  else
+                  {
+                    newname = newname + "; " + sf[i].getDescription();
+                  }
+                }
+              }
+            }
+          }
+          if (newname != null)
+          {
+            String oldname = ((SequenceNode) node).getName();
+            // TODO : save in the undo object for this modification.
+            ((SequenceNode) node).setName(newname);
+          }
         }
-    }
+      }
+    });
+  }
 }