Neeed to know protocol for annotation loading
[jalview.git] / src / jalview / gui / AlignFrame.java
index 4687b3c..bb722a5 100755 (executable)
@@ -33,10 +33,11 @@ import jalview.datamodel.*;
 import jalview.io.*;
 import jalview.jbgui.*;
 import jalview.schemes.*;
-import jalview.util.ShiftList;
 import jalview.commands.*;
 import jalview.ws.*;
 import java.awt.dnd.*;
+import javax.swing.event.ChangeListener;
+import javax.swing.event.ChangeEvent;
 
 /**
  * DOCUMENT ME!
@@ -52,17 +53,17 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
   /** DOCUMENT ME!! */
   public static final int DEFAULT_HEIGHT = 500;
   public AlignmentPanel alignPanel;
-  AlignViewport viewport;
 
+  AlignViewport viewport;
 
   Vector alignPanels = new Vector();
 
 
   /** DOCUMENT ME!! */
-  public String currentFileFormat = null;
-  Stack historyList = new Stack();
-  Stack redoList = new Stack();
-  private int treeCount = 0;
+  String currentFileFormat = null;
+
+  String fileName = null;
+
 
   /**
    * Creates a new AlignFrame object.
@@ -93,7 +94,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
       al.setDataset(null);
     }
 
-    addAlignmentPanel(alignPanel);
+    addAlignmentPanel(alignPanel, true);
     init();
   }
 
@@ -106,7 +107,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
   {
     viewport = ap.av;
     alignPanel = ap;
-    addAlignmentPanel(ap);
+    addAlignmentPanel(ap, false);
     init();
   }
 
@@ -137,18 +138,26 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
      setGUINucleotide(viewport.alignment.isNucleotide());
    }
 
-   if (jalview.bin.Cache.getDefault("WRAP_ALIGNMENT", false))
+   setMenusFromViewport(viewport);
+
+   if (viewport.wrapAlignment)
    {
-     wrapMenuItem.setSelected(true);
      wrapMenuItem_actionPerformed(null);
    }
 
    addKeyListener();
+
+  }
+
+  public void setFileName(String file, String format)
+  {
+     fileName = file;
+     currentFileFormat = format;
+     reload.setEnabled(true);
   }
 
   void addKeyListener()
   {
-      final AlignFrame af = this;
       addKeyListener(new KeyAdapter()
       {
         public void keyPressed(KeyEvent evt)
@@ -162,47 +171,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
 
           switch (evt.getKeyCode())
           {
-            case KeyEvent.VK_V:
-              if (!evt.isControlDown())
-              {
-                AlignmentI al;
-                if(viewport.hasHiddenRows)
-                {
-                  al = viewport.alignment.getHiddenSequences().getFullAlignment();
-                }
-                else
-                  al = new Alignment(viewport.alignment.getSequencesArray());
-
-                AlignViewport newview = new AlignViewport(al);
-
-                AlignmentPanel copy = new AlignmentPanel(af, newview);
-
-                if(viewport.viewName==null)
-                  viewport.viewName="View 1";
-
-                copy.av.sequenceSetID = viewport.getSequenceSetId();
-                copy.av.viewName = "View " +
-                    (Desktop.getViewCount(viewport.getSequenceSetId())+1);
-
-
-                addAlignmentPanel(copy);
-                PaintRefresher.Register(copy, viewport.getSequenceSetId());
-                PaintRefresher.Register(alignPanel,
-                                        viewport.getSequenceSetId());
-
-              }
-              break;
-            case KeyEvent.VK_G:
-              Desktop.instance.gatherViews(af);
-              break;
-
-            case KeyEvent.VK_X:
-              if (!evt.isControlDown())
-              {
-                Desktop.instance.explodeViews(af);
-              }
-              break;
-
 
             case 27: // escape key
               deselectAllSequenceMenuItem_actionPerformed(null);
@@ -380,21 +348,24 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
                                            viewport.startSeq
                                            + viewport.endSeq - viewport.startSeq);
               break;
-
-
           }
         }
       });
   }
 
 
-  public void addAlignmentPanel(final AlignmentPanel ap)
+  public void addAlignmentPanel(final AlignmentPanel ap,
+                                boolean newPanel)
   {
+    ap.alignFrame = this;
+
     alignPanels.addElement(ap);
 
+    PaintRefresher.Register(ap, ap.av.getSequenceSetId());
+
     int aSize = alignPanels.size();
 
-   tabbedPane.setVisible(aSize>1 || ap.av.viewName!=null);
+    tabbedPane.setVisible(aSize>1 || ap.av.viewName!=null);
 
     if (aSize == 1 && ap.av.viewName==null)
     {
@@ -404,34 +375,34 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
     {
       if (aSize == 2)
       {
-        AlignmentPanel first = (AlignmentPanel) alignPanels.firstElement();
-        tabbedPane.addTab(first.av.viewName==null?"Original":first.av.viewName,first);
-
-        this.getContentPane().add(tabbedPane, BorderLayout.CENTER);
+        setInitialTabVisible();
       }
 
-      tabbedPane.addTab(ap.av.viewName==null?"Original":ap.av.viewName, ap);
-    }
-
-    ap.av.updateConsensus(ap);
-    ap.av.updateConservation(ap);
+      expandViews.setEnabled(true);
+      gatherViews.setEnabled(true);
+      tabbedPane.addTab(ap.av.viewName, ap);
 
+      ap.setVisible(false);
+    }
 
-    ap.av.addPropertyChangeListener(new PropertyChangeListener()
+    if(newPanel)
     {
-     public void propertyChange(PropertyChangeEvent evt)
-     {
-       if (evt.getPropertyName().equals("alignment"))
-       {
-         PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
-         alignmentChanged();
-       }
-     }
-   });
-
+      if (ap.av.padGaps)
+        ap.av.alignment.padGaps();
+      ap.av.updateConservation(ap);
+      ap.av.updateConsensus(ap);
+    }
   }
 
-
+  public void setInitialTabVisible()
+  {
+    expandViews.setEnabled(true);
+    gatherViews.setEnabled(true);
+    tabbedPane.setVisible(true);
+    AlignmentPanel first = (AlignmentPanel) alignPanels.firstElement();
+    tabbedPane.addTab(first.av.viewName,first);
+    this.getContentPane().add(tabbedPane, BorderLayout.CENTER);
+  }
 
 
   public AlignViewport getViewport()
@@ -467,7 +438,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
       {
         // System.out.println("deregistering discoverer listener");
         Desktop.discoverer.removePropertyChangeListener(thisListener);
-        closeMenuItem_actionPerformed(null);
+        closeMenuItem_actionPerformed(true);
       }
       ;
     });
@@ -476,10 +447,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
   public void setGUINucleotide(boolean nucleotide)
   {
     showTranslation.setVisible( nucleotide );
-    //sequenceFeatures.setVisible(!nucleotide );
-    //featureSettings.setVisible( !nucleotide );
-    conservationMenuItem.setVisible( !nucleotide );
-    modifyConservation.setVisible(   !nucleotide );
+    conservationMenuItem.setEnabled( !nucleotide );
+    modifyConservation.setEnabled(   !nucleotide );
 
     //Remember AlignFrame always starts as protein
     if(!nucleotide)
@@ -488,6 +457,35 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
     }
   }
 
+  /**
+   * Need to call this method when tabs are selected for multiple views,
+   * or when loading from Jalview2XML.java
+   * @param av AlignViewport
+   */
+  void setMenusFromViewport(AlignViewport av)
+  {
+    padGapsMenuitem.setSelected(av.padGaps);
+    colourTextMenuItem.setSelected(av.showColourText);
+    abovePIDThreshold.setSelected(av.getAbovePIDThreshold());
+    conservationMenuItem.setSelected(av.getConservationSelected());
+    seqLimits.setSelected(av.getShowJVSuffix());
+    idRightAlign.setSelected(av.rightAlignIds);
+    renderGapsMenuItem.setSelected(av.renderGaps);
+    wrapMenuItem.setSelected(av.wrapAlignment);
+    annotationPanelMenuItem.setState(av.showAnnotation);
+    viewBoxesMenuItem.setSelected(av.showBoxes);
+    viewTextMenuItem.setSelected(av.showText);
+
+    setColourSelected(ColourSchemeProperty.
+                      getColourName(av.getGlobalColourScheme()));
+
+    showSeqFeatures.setSelected(av.showSequenceFeatures);
+    hiddenMarkers.setState(av.showHiddenMarkers);
+    applyToAllGroups.setState(av.colourAppliesToAllGroups);
+
+    updateEditMenuBar();
+  }
+
 
   Hashtable progressBars;
   public void setProgressBar(String message, long id)
@@ -554,6 +552,59 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
     Desktop.instance.inputLocalFileMenuItem_actionPerformed(viewport);
   }
 
+  public void reload_actionPerformed(ActionEvent e)
+  {
+    if(fileName!=null)
+    {
+      if(currentFileFormat.equals("Jalview"))
+      {
+        JInternalFrame [] frames = Desktop.desktop.getAllFrames();
+        for(int i=0; i<frames.length; i++)
+        {
+          if (frames[i] instanceof AlignFrame
+              && frames[i] != this
+              && ( (AlignFrame) frames[i]).fileName.equals(fileName))
+          {
+            try{
+              frames[i].setSelected(true);
+              Desktop.instance.closeAssociatedWindows();
+            }catch(java.beans.PropertyVetoException ex){}
+          }
+
+        }
+        Desktop.instance.closeAssociatedWindows();
+
+      }
+      else
+      {
+        viewport.alignment.deleteAllGroups();
+        viewport.sequenceColours=null;
+        while (viewport.alignment.getHeight() > 0)
+        {
+          viewport.alignment.deleteSequence(0);
+        }
+
+        viewport.historyList.clear();
+        viewport.redoList.clear();
+        Alignment dset = viewport.alignment.getDataset();
+        while (dset.getHeight() > 0)
+        {
+          dset.deleteSequence(0);
+        }
+
+        firePropertyChange("Alignment", null, viewport.alignment.getSequences());
+
+        updateEditMenuBar();
+      }
+
+      FileLoader loader = new FileLoader();
+      String protocol = fileName.startsWith("http:")? "URL":"File";
+      loader.LoadFile(viewport, fileName, protocol, currentFileFormat);
+
+    }
+  }
+
+
   public void addFromText_actionPerformed(ActionEvent e)
   {
     Desktop.instance.inputTextboxMenuItem_actionPerformed(viewport);
@@ -564,12 +615,24 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
     Desktop.instance.inputURLMenuItem_actionPerformed(viewport);
   }
 
+
+  public void save_actionPerformed(ActionEvent e)
+  {
+    if(fileName==null
+       || currentFileFormat==null
+       || fileName.startsWith("http")
+        )
+      saveAs_actionPerformed(null);
+    else
+      saveAlignment(fileName, currentFileFormat);
+  }
+
   /**
    * DOCUMENT ME!
    *
    * @param e DOCUMENT ME!
    */
-  public void saveAlignmentMenu_actionPerformed(ActionEvent e)
+  public void saveAs_actionPerformed(ActionEvent e)
   {
     JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.
         getProperty( "LAST_DIRECTORY"),
@@ -590,7 +653,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
     if (value == JalviewFileChooser.APPROVE_OPTION)
     {
         currentFileFormat = chooser.getSelectedFormat();
-
         if (currentFileFormat == null)
         {
           JOptionPane.showInternalMessageDialog(Desktop.desktop,
@@ -601,18 +663,21 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
           return;
         }
 
+        fileName = chooser.getSelectedFile().getPath();
+
       jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT",
                                     currentFileFormat);
 
-      String choice = chooser.getSelectedFile().getPath();
-      jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice);
+      jalview.bin.Cache.setProperty("LAST_DIRECTORY", fileName);
 
-      saveAlignment(choice, currentFileFormat);
+      saveAlignment(fileName, currentFileFormat);
     }
   }
 
   public boolean saveAlignment(String file, String format)
   {
+    boolean success = true;
+
     if (format.equalsIgnoreCase("Jalview"))
     {
       String shortName = title;
@@ -623,10 +688,12 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
             java.io.File.separatorChar) + 1);
       }
 
-      new Jalview2XML().SaveAlignment(this, file, shortName);
+      success = new Jalview2XML().SaveAlignment(this, file, shortName);
+
+      statusBar.setText("Successfully saved to file: "
+                          +fileName+" in "
+                          +format +" format.");
 
-      // USE Jalview2XML to save this file
-      return true;
     }
     else
     {
@@ -652,25 +719,37 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
 
       if (output == null)
       {
-        return false;
+        success = false;
       }
-
-      try
-      {
-        java.io.PrintWriter out = new java.io.PrintWriter(
-            new java.io.FileWriter(file));
-
-        out.print(output);
-        out.close();
-        this.setTitle(file);
-        return true;
-      }
-      catch (Exception ex)
+      else
       {
-        ex.printStackTrace();
+        try
+        {
+          java.io.PrintWriter out = new java.io.PrintWriter(
+              new java.io.FileWriter(file));
+
+          out.print(output);
+          out.close();
+          this.setTitle(file);
+          statusBar.setText("Successfully saved to file: "
+                            + fileName + " in "
+                            + format + " format.");
+        }
+        catch (Exception ex)
+        {
+          success = false;
+          ex.printStackTrace();
+        }
       }
     }
-    return false;
+
+    if (!success)
+      JOptionPane.showInternalMessageDialog(
+          this, "Couldn't save file: " + fileName,
+          "Error Saving File",
+          JOptionPane.WARNING_MESSAGE);
+
+    return success;
   }
 
   /**
@@ -697,6 +776,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
     }
 
     CutAndPasteTransfer cap = new CutAndPasteTransfer();
+    cap.setForInput(null);
     Desktop.addInternalFrame(cap,
                              "Alignment output - " + e.getActionCommand(), 600,
                              500);
@@ -799,22 +879,50 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
    *
    * @param e DOCUMENT ME!
    */
-  public void closeMenuItem_actionPerformed(ActionEvent e)
+  public void closeMenuItem_actionPerformed(boolean closeAllTabs)
   {
+    if(alignPanels!=null && alignPanels.size()<2)
+      closeAllTabs = true;
+
     try
     {
-      for(int i=0; i<alignPanels.size(); i++)
+      if(alignPanels!=null)
       {
-        AlignmentPanel ap = (AlignmentPanel)alignPanels.elementAt(i);
-        PaintRefresher.RemoveComponent(ap.seqPanel.seqCanvas);
-        PaintRefresher.RemoveComponent(ap.idPanel.idCanvas);
-        PaintRefresher.RemoveComponent(ap);
+        if (closeAllTabs)
+          for (int i = 0; i < alignPanels.size(); i++)
+          {
+            AlignmentPanel ap = (AlignmentPanel) alignPanels.elementAt(i);
+            PaintRefresher.RemoveComponent(ap.seqPanel.seqCanvas);
+            PaintRefresher.RemoveComponent(ap.idPanel.idCanvas);
+            PaintRefresher.RemoveComponent(ap);
+          }
+        else
+        {
+          int index = tabbedPane.getSelectedIndex();
+
+          alignPanels.removeElement(alignPanel);
+          PaintRefresher.RemoveComponent(alignPanel.seqPanel.seqCanvas);
+          PaintRefresher.RemoveComponent(alignPanel.idPanel.idCanvas);
+          PaintRefresher.RemoveComponent(alignPanel);
+          alignPanel = null;
+          viewport = null;
+
+          tabbedPane.removeTabAt(index);
+          tabbedPane.validate();
+
+          if(index==tabbedPane.getTabCount())
+            index --;
+
+          this.tabSelectionChanged(index);
+        }
       }
 
-      this.setClosed(true);
+      if (closeAllTabs)
+        this.setClosed(true);
     }
     catch (Exception ex)
     {
+      ex.printStackTrace();
     }
   }
 
@@ -824,11 +932,11 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
    */
   void updateEditMenuBar()
   {
-    if (historyList.size() > 0)
+
+    if (viewport.historyList.size() > 0)
     {
       undoMenuItem.setEnabled(true);
-
-      CommandI command = (CommandI) historyList.peek();
+      CommandI command = (CommandI) viewport.historyList.peek();
       undoMenuItem.setText("Undo " + command.getDescription());
     }
     else
@@ -837,11 +945,11 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
       undoMenuItem.setText("Undo");
     }
 
-    if (redoList.size() > 0)
+    if (viewport.redoList.size() > 0)
     {
       redoMenuItem.setEnabled(true);
 
-      CommandI command = (CommandI) redoList.peek();
+      CommandI command = (CommandI) viewport.redoList.peek();
       redoMenuItem.setText("Redo " + command.getDescription());
     }
     else
@@ -854,11 +962,13 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
 
   public void addHistoryItem(CommandI command)
   {
-    historyList.push(command);
-    redoList.clear();
-    updateEditMenuBar();
-
-    viewport.hasHiddenColumns = viewport.colSel.getHiddenColumns()!=null;
+    if(command.getSize()>0)
+    {
+      viewport.historyList.push(command);
+      viewport.redoList.clear();
+      updateEditMenuBar();
+      viewport.hasHiddenColumns = viewport.colSel.getHiddenColumns() != null;
+    }
   }
 
 
@@ -870,19 +980,20 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
    */
   protected void undoMenuItem_actionPerformed(ActionEvent e)
   {
-    CommandI command = (CommandI)historyList.pop();
-    redoList.push(command);
+    CommandI command = (CommandI)viewport.historyList.pop();
+    viewport.redoList.push(command);
     command.undoCommand();
 
-   // HistoryItem nh,hi = (HistoryItem) historyList.pop();
-   // redoList.push(nh=new HistoryItem(hi.getDescription(), viewport.alignment,
-   //                               HistoryItem.HIDE));
-   // if (hi.alColumnChanges!=null)
-   //   nh.alColumnChanges = hi.alColumnChanges.getInverse();
-   // restoreHistoryItem(hi);
-   viewport.hasHiddenColumns = viewport.colSel.getHiddenColumns()!=null;
-   updateEditMenuBar();
-    viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences());
+    AlignViewport originalSource = getOriginatingSource(command);
+    updateEditMenuBar();
+
+    if(originalSource!=null)
+    {
+      originalSource.hasHiddenColumns = viewport.colSel.getHiddenColumns() != null;
+      originalSource.firePropertyChange("alignment",
+                                        null,
+                                        originalSource.alignment.getSequences());
+    }
   }
 
   /**
@@ -892,21 +1003,64 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
    */
   protected void redoMenuItem_actionPerformed(ActionEvent e)
   {
-    CommandI command = (CommandI) redoList.pop();
-    historyList.push(command);
+    if(viewport.redoList.size()<1)
+      return;
+
+    CommandI command = (CommandI) viewport.redoList.pop();
+    viewport.historyList.push(command);
     command.doCommand();
-    viewport.hasHiddenColumns = viewport.colSel.getHiddenColumns()!=null;
-
-   // HistoryItem nh,hi = (HistoryItem) redoList.pop();
-  //  historyList.push(nh=new HistoryItem(hi.getDescription(), viewport.alignment,
-  //      HistoryItem.HIDE));
-  //  if (hi.alColumnChanges!=null)
-  //    nh.alColumnChanges=hi.alColumnChanges.getInverse();
-  //  restoreHistoryItem(hi);
+
+    AlignViewport originalSource = getOriginatingSource(command);
     updateEditMenuBar();
-    viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences());
+
+    if(originalSource!=null)
+    {
+      originalSource.hasHiddenColumns = viewport.colSel.getHiddenColumns() != null;
+      originalSource.firePropertyChange("alignment",
+                                        null,
+                                        originalSource.alignment.getSequences());
+    }
   }
 
+  AlignViewport getOriginatingSource(CommandI command)
+  {
+    AlignViewport originalSource = null;
+    //For sequence removal and addition, we need to fire
+   //the property change event FROM the viewport where the
+   //original alignment was altered
+    AlignmentI al=null;
+    if (command instanceof EditCommand)
+    {
+      EditCommand editCommand = (EditCommand) command;
+      al = editCommand.getAlignment();
+      Vector comps = (Vector) PaintRefresher.components
+          .get(viewport.getSequenceSetId());
+
+      for (int i = 0; i < comps.size(); i++)
+      {
+        if (comps.elementAt(i) instanceof AlignmentPanel)
+        {
+          if (al == ( (AlignmentPanel) comps.elementAt(i)).av.alignment)
+          {
+            originalSource = ( (AlignmentPanel) comps.elementAt(i)).av;
+            break;
+          }
+        }
+      }
+    }
+
+    if (originalSource == null)
+    {
+      //The original view is closed, we must validate
+      //the current view against the closed view first
+      if (al != null)
+        PaintRefresher.validateSequences(al, viewport.alignment);
+
+      originalSource = viewport;
+    }
+
+    return originalSource;
+  }
 
   /**
    * DOCUMENT ME!
@@ -1129,7 +1283,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
      }
      else
      {
-       sequences = new FormatAdapter().readFile(str, "Paste", format);
+       sequences = new FormatAdapter().readFile(str, "Paste", format).getSequencesArray();
      }
 
      AlignmentI alignment = null;
@@ -1156,12 +1310,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
               sequences[i].getEnd());
 
           alignment.addSequence(newseqs[i]);
-          PaintRefresher.Refresh(alignPanel,
-                                 alignPanel.av.getSequenceSetId(),
-                                 null,newseqs[i]);
         }
 
-
         /*
          //ADD HISTORY ITEM
          */
@@ -1274,7 +1424,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
    *
    * @param e DOCUMENT ME!
    */
-  protected void delete_actionPerformed(ActionEvent e)
+  protected void delete_actionPerformed(ActionEvent evt)
   {
 
     SequenceGroup sg = viewport.getSelectionGroup();
@@ -1284,19 +1434,14 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
     }
 
     Vector seqs = new Vector();
-    int cutLength = sg.getEndRes()-sg.getStartRes()+1;
     SequenceI seq;
     for (int i = 0; i < sg.getSize(false); i++)
     {
       seq = sg.getSequenceAt(i);
       seqs.addElement(seq);
-      if(seq.getLength()<=cutLength)
-        PaintRefresher.Refresh(alignPanel,
-                       alignPanel.av.getSequenceSetId(),
-                       seq,
-                       null);
     }
 
+
    // If the cut affects all sequences, remove highlighted columns
    if (sg.getSize(false) == viewport.alignment.getHeight())
    {
@@ -1320,6 +1465,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
                                       sg.getEndRes()-sg.getStartRes()+1,
                                       viewport.alignment));
 
+
     viewport.setSelectionGroup(null);
     viewport.alignment.deleteGroup(sg);
 
@@ -1346,6 +1492,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
   protected void deleteGroups_actionPerformed(ActionEvent e)
   {
     viewport.alignment.deleteAllGroups();
+    viewport.sequenceColours = null;
     viewport.setSelectionGroup(null);
     PaintRefresher.Refresh(this, viewport.getSequenceSetId());
     alignPanel.repaint();
@@ -1390,7 +1537,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
     alignPanel.seqPanel.seqCanvas.highlightSearchResults(null);
     alignPanel.idPanel.idCanvas.searchResults = null;
     alignPanel.repaint();
-    PaintRefresher.Refresh(this, viewport.getSequenceSetId());
+    PaintRefresher.Refresh(alignPanel, viewport.getSequenceSetId());
   }
 
   /**
@@ -1415,7 +1562,9 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
       sg.addOrRemove(viewport.getAlignment().getSequenceAt(i), false);
     }
 
-    PaintRefresher.Refresh(this, viewport.getSequenceSetId());
+    alignPanel.repaint();
+
+    PaintRefresher.Refresh(alignPanel, viewport.getSequenceSetId());
   }
 
   public void invertColSel_actionPerformed(ActionEvent e)
@@ -1474,6 +1623,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
                                     viewport.alignment,
                                     viewport.colSel,
                                     viewport.selectionGroup);
+        viewport.setStartRes(0);
       }
      else
      {
@@ -1486,6 +1636,9 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
                                    viewport.selectionGroup);
      }
 
+     statusBar.setText("Removed "+trimRegion.getSize()+" columns.");
+
+
       addHistoryItem(trimRegion);
 
       Vector groups = viewport.alignment.getGroups();
@@ -1501,7 +1654,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
         }
       }
 
-      viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences());
+      viewport.firePropertyChange("alignment", null,
+                                  viewport.getAlignment().getSequences());
     }
   }
 
@@ -1512,7 +1666,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
    */
   public void removeGappedColumnMenuItem_actionPerformed(ActionEvent e)
   {
-    int start = 0, end = viewport.alignment.getWidth();
+    int start = 0, end = viewport.alignment.getWidth()-1;
 
     SequenceI[] seqs;
     if (viewport.getSelectionGroup() != null)
@@ -1525,10 +1679,15 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
       seqs = viewport.alignment.getSequencesArray();
 
 
-    addHistoryItem(new RemoveGapColCommand("Remove Gapped Columns",
-                                           seqs,
-                                           start, end,
-                   viewport.getGapCharacter()));
+    RemoveGapColCommand removeGapCols =
+        new RemoveGapColCommand("Remove Gapped Columns",
+                                seqs,
+                                start, end,
+                                viewport.getGapCharacter());
+
+    addHistoryItem(removeGapCols);
+
+    statusBar.setText("Removed "+removeGapCols.getSize()+" empty columns.");
 
     //This is to maintain viewport position on first residue
     //of first sequence
@@ -1551,7 +1710,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
    */
   public void removeAllGapsMenuItem_actionPerformed(ActionEvent e)
   {
-    int start = 0, end = viewport.alignment.getWidth();
+    int start = 0, end = viewport.alignment.getWidth()-1;
 
     SequenceI[] seqs;
     if (viewport.getSelectionGroup() != null)
@@ -1579,77 +1738,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
 
   }
 
- public void alignmentChanged()
- {
-   AlignViewport av;
-   AlignmentPanel ap;
-
-   for(int i=0; i<alignPanels.size(); i++)
-   {
-     ap = (AlignmentPanel) alignPanels.elementAt(i);
-     av = ap.av;
-
-     if (av.padGaps)
-       av.getAlignment().padGaps();
-
-     if (av.hconsensus != null && av.autoCalculateConsensus)
-     {
-       av.updateConsensus(ap);
-       av.updateConservation(ap);
-       ap.annotationPanel.repaint();
-     }
-
-     resetAllColourSchemes();
-
-     av.alignment.adjustSequenceAnnotations();
-
-     if (ap.overviewPanel != null)
-       ap.overviewPanel.updateOverviewImage();
-
-     ap.repaint();
-   }
- }
-
-  void resetAllColourSchemes()
-  {
-    ColourSchemeI cs = viewport.globalColourScheme;
-    if(cs!=null)
-    {
-      if (cs instanceof ClustalxColourScheme)
-      {
-        ( (ClustalxColourScheme) viewport.getGlobalColourScheme()).
-            resetClustalX(viewport.alignment.getSequences(),
-                          viewport.alignment.getWidth());
-      }
-
-      cs.setConsensus(viewport.hconsensus);
-      if (cs.conservationApplied())
-      {
-        Alignment al = (Alignment) viewport.alignment;
-        Conservation c = new Conservation("All",
-                                          ResidueProperties.propHash, 3,
-                                          al.getSequences(), 0,
-                                          al.getWidth() - 1);
-        c.calculate();
-        c.verdict(false, viewport.ConsPercGaps);
-
-        cs.setConservation(c);
-      }
-    }
-
-    int s, sSize = viewport.alignment.getGroups().size();
-    for(s=0; s<sSize; s++)
-    {
-      SequenceGroup sg = (SequenceGroup)viewport.alignment.getGroups().elementAt(s);
-      if(sg.cs!=null && sg.cs instanceof ClustalxColourScheme)
-      {
-        ((ClustalxColourScheme)sg.cs).resetClustalX(
-            sg.getSequences(true), sg.getWidth());
-      }
-      sg.recalcConservation();
-    }
-  }
-
   /**
    * DOCUMENT ME!
    *
@@ -1657,9 +1745,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
    */
   public void padGapsMenuitem_actionPerformed(ActionEvent e)
   {
-  //  addHistoryItem(new HistoryItem("Pad Gaps", viewport.alignment,
-   //                                HistoryItem.HIDE));
-
     viewport.padGaps = padGapsMenuitem.isSelected();
 
     viewport.firePropertyChange("alignment",
@@ -1674,13 +1759,63 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
    */
   public void findMenuItem_actionPerformed(ActionEvent e)
   {
-    JInternalFrame frame = new JInternalFrame();
-    Finder finder = new Finder(viewport, alignPanel, frame);
-    frame.setContentPane(finder);
-    frame.setLayer(JLayeredPane.PALETTE_LAYER);
-    Desktop.addInternalFrame(frame, "Find", 340, 110);
+    new Finder();
   }
 
+  public void newView_actionPerformed(ActionEvent e)
+  {
+    AlignmentPanel newap =
+        new Jalview2XML().copyAlignPanel(alignPanel, true);
+
+    if (viewport.viewName == null)
+      viewport.viewName = "Original";
+
+    newap.av.historyList = viewport.historyList;
+    newap.av.redoList = viewport.redoList;
+
+    int index = Desktop.getViewCount(viewport.getSequenceSetId());
+    String newViewName = "View " +index;
+
+    Vector comps = (Vector)PaintRefresher.components.get(viewport.getSequenceSetId());
+    Vector existingNames = new Vector();
+    for(int i=0; i<comps.size(); i++)
+    {
+      if(comps.elementAt(i) instanceof AlignmentPanel)
+      {
+        AlignmentPanel ap = (AlignmentPanel)comps.elementAt(i);
+        if(!existingNames.contains(ap.av.viewName))
+          existingNames.addElement(ap.av.viewName);
+      }
+    }
+
+    while(existingNames.contains(newViewName))
+    {
+      newViewName = "View "+ (++index);
+    }
+
+    newap.av.viewName = newViewName;
+
+    addAlignmentPanel(newap, false);
+
+    if(alignPanels.size()==2)
+    {
+      viewport.gatherViewsHere = true;
+    }
+    tabbedPane.setSelectedIndex(tabbedPane.getTabCount() - 1);
+  }
+
+  public void expandViews_actionPerformed(ActionEvent e)
+  {
+        Desktop.instance.explodeViews(this);
+  }
+
+  public void gatherViews_actionPerformed(ActionEvent e)
+  {
+    Desktop.instance.gatherViews(this);
+  }
+
+
+
   /**
    * DOCUMENT ME!
    *
@@ -1691,13 +1826,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
     new FontChooser(alignPanel);
   }
 
-  public void smoothFont_actionPerformed(ActionEvent e)
-  {
-    viewport.antiAlias = smoothFont.isSelected();
-    alignPanel.annotationPanel.image = null;
-    alignPanel.repaint();
-  }
-
 
   /**
    * DOCUMENT ME!
@@ -1712,6 +1840,13 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
     alignPanel.repaint();
   }
 
+  public void idRightAlign_actionPerformed(ActionEvent e)
+  {
+    viewport.rightAlignIds = idRightAlign.isSelected();
+    alignPanel.repaint();
+  }
+
+
 
   /**
    * DOCUMENT ME!
@@ -1903,6 +2038,11 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
     alignPanel.setOverviewPanel(overview);
   }
 
+  public void textColour_actionPerformed(ActionEvent e)
+  {
+    new TextColourChooser().chooseColour(alignPanel, null);
+  }
+
   /**
    * DOCUMENT ME!
    *
@@ -2115,8 +2255,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
                 viewport.getIgnoreGapsConsensus());
 
          sg.cs.setConsensus(AAFrequency.calculate(
-             sg.getSequences(true), 0,
-             sg.getWidth()));
+             sg.getSequences(true), sg.getStartRes(),
+             sg.getEndRes()+1));
        }
         else
           sg.cs.setThreshold(0, viewport.getIgnoreGapsConsensus());
@@ -2126,8 +2266,9 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
         {
           Conservation c = new Conservation("Group",
                                             ResidueProperties.propHash, 3,
-                                            sg.getSequences(true), 0,
-                                            viewport.alignment.getWidth() - 1);
+                                            sg.getSequences(true),
+                                            sg.getStartRes(),
+                                            sg.getEndRes()+1);
           c.calculate();
           c.verdict(false, viewport.ConsPercGaps);
           sg.cs.setConservation(c);
@@ -2413,7 +2554,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
       return;
     }
 
-     new PCAPanel(viewport);
+     new PCAPanel(alignPanel);
   }
 
 
@@ -2533,9 +2674,14 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
       tp = new TreePanel(alignPanel, type, pwType);
     }
 
-    addTreeMenuItem(tp, title);
+    title += " from ";
+
+    if(viewport.viewName!=null)
+      title+= viewport.viewName+" of ";
+
+    title += this.title;
 
-    Desktop.addInternalFrame(tp, title + " from " + this.title, 600, 500);
+    Desktop.addInternalFrame(tp, title, 600, 500);
   }
 
   /**
@@ -2573,51 +2719,53 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
    * @param treePanel Displayed tree window.
    * @param title SortBy menu item title.
    */
-  void addTreeMenuItem(final TreePanel treePanel, String title)
+  public void buildTreeMenu()
   {
-    final JMenuItem item = new JMenuItem(title);
+    sortByTreeMenu.removeAll();
 
-    treeCount++;
-
-    if (treeCount == 1)
-    {
-      sort.add(sortByTreeMenu);
-    }
-
-    sortByTreeMenu.add(item);
-    item.addActionListener(new java.awt.event.ActionListener()
+    Vector comps = (Vector)PaintRefresher.components.get(viewport.getSequenceSetId());
+    Vector treePanels = new Vector();
+    int i, iSize = comps.size();
+    for(i=0; i<iSize; i++)
     {
-      public void actionPerformed(ActionEvent e)
+      if(comps.elementAt(i) instanceof TreePanel)
       {
-        SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray();
-        AlignmentSorter.sortByTree(viewport.getAlignment(),
-                                   treePanel.getTree());
+        treePanels.add(comps.elementAt(i));
+      }
+    }
 
-        addHistoryItem(new OrderCommand("Tree Sort",
-                                        oldOrder,
-                                        viewport.alignment));
+    iSize = treePanels.size();
 
+    if(iSize<1)
+    {
+      sortByTreeMenu.setVisible(false);
+      return;
+    }
 
-        alignPanel.repaint();
-      }
-    });
+    sortByTreeMenu.setVisible(true);
 
-    treePanel.addInternalFrameListener(new javax.swing.event.
-                                       InternalFrameAdapter()
+    for(i=0; i<treePanels.size(); i++)
     {
-      public void internalFrameClosed(
-          javax.swing.event.InternalFrameEvent evt)
+      TreePanel tp = (TreePanel)treePanels.elementAt(i);
+      final JMenuItem item = new JMenuItem(tp.getTitle());
+      final NJTree tree = ((TreePanel)treePanels.elementAt(i)).getTree();
+      item.addActionListener(new java.awt.event.ActionListener()
       {
-        treeCount--;
-        sortByTreeMenu.remove(item);
-
-        if (treeCount == 0)
+        public void actionPerformed(ActionEvent e)
         {
-          sort.remove(sortByTreeMenu);
+          SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray();
+          AlignmentSorter.sortByTree(viewport.getAlignment(), tree);
+
+          addHistoryItem(new OrderCommand("Tree Sort",
+                                          oldOrder,
+                                          viewport.alignment));
+
+          alignPanel.repaint();
         }
-      }
-      ;
-    });
+      });
+
+      sortByTreeMenu.add(item);
+    }
   }
 
   /**
@@ -2773,7 +2921,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener
 
 
         Desktop.addInternalFrame(tp, title, w, h);
-        addTreeMenuItem(tp, title);
       }
     }
     catch (Exception ex)
@@ -3140,7 +3287,7 @@ public void drop(DropTargetDropEvent evt)
       }
 
       boolean isAnnotation = new AnnotationFile().readAnnotationFile(viewport.
-          alignment, file);
+          alignment, file, protocol);
 
       if (!isAnnotation)
       {
@@ -3181,6 +3328,7 @@ public void drop(DropTargetDropEvent evt)
     {
       alignPanel = (AlignmentPanel) alignPanels.elementAt(index);
       viewport = alignPanel.av;
+      setMenusFromViewport(viewport);
     }
   }