mouseOver highlighting and broadcasting efficiency
authorjprocter <Jim Procter>
Sun, 17 Jun 2007 12:51:27 +0000 (12:51 +0000)
committerjprocter <Jim Procter>
Sun, 17 Jun 2007 12:51:27 +0000 (12:51 +0000)
src/jalview/gui/SeqPanel.java

index 37262bb..dc326de 100755 (executable)
-/*\r
- * Jalview - A Sequence Alignment Editor and Viewer\r
- * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
- *\r
- * This program is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU General Public License\r
- * as published by the Free Software Foundation; either version 2\r
- * of the License, or (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program; if not, write to the Free Software\r
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
- */\r
-package jalview.gui;\r
-\r
-import java.util.*;\r
-\r
-import java.awt.*;\r
-import java.awt.event.*;\r
-import javax.swing.*;\r
-\r
-import jalview.commands.*;\r
-import jalview.datamodel.*;\r
-import jalview.schemes.*;\r
-import jalview.structure.*;\r
-\r
-/**\r
- * DOCUMENT ME!\r
- *\r
- * @author $author$\r
- * @version $Revision$\r
- */\r
-public class SeqPanel\r
-    extends JPanel implements MouseListener,\r
-    MouseMotionListener, MouseWheelListener, SequenceListener\r
-\r
-{\r
-  /** DOCUMENT ME!! */\r
-  public SeqCanvas seqCanvas;\r
-\r
-  /** DOCUMENT ME!! */\r
-  public AlignmentPanel ap;\r
-  protected int lastres;\r
-  protected int startseq;\r
-  protected AlignViewport av;\r
-\r
-  ScrollThread scrollThread = null;\r
-  boolean mouseDragging = false;\r
-  boolean editingSeqs = false;\r
-  boolean groupEditing = false;\r
-\r
-  //////////////////////////////////////////\r
-  /////Everything below this is for defining the boundary of the rubberband\r
-  //////////////////////////////////////////\r
-  int oldSeq = -1;\r
-  boolean changeEndSeq = false;\r
-  boolean changeStartSeq = false;\r
-  boolean changeEndRes = false;\r
-  boolean changeStartRes = false;\r
-  SequenceGroup stretchGroup = null;\r
-  boolean remove = false;\r
-\r
-  Point lastMousePress;\r
-  boolean mouseWheelPressed = false;\r
-  StringBuffer keyboardNo1;\r
-  StringBuffer keyboardNo2;\r
-\r
-  java.net.URL linkImageURL;\r
-\r
-  StringBuffer tooltipText = new StringBuffer("<html>");\r
-  String tmpString;\r
-\r
-  EditCommand editCommand;\r
-\r
-  StructureSelectionManager ssm;\r
-\r
-\r
-  /**\r
-   * Creates a new SeqPanel object.\r
-   *\r
-   * @param avp DOCUMENT ME!\r
-   * @param p DOCUMENT ME!\r
-   */\r
-  public SeqPanel(AlignViewport av, AlignmentPanel ap)\r
-  {\r
-    linkImageURL = getClass().getResource("/images/link.gif");\r
-    ToolTipManager.sharedInstance().registerComponent(this);\r
-    ToolTipManager.sharedInstance().setInitialDelay(0);\r
-    ToolTipManager.sharedInstance().setDismissDelay(10000);\r
-    this.av = av;\r
-    setBackground(Color.white);\r
-\r
-    seqCanvas = new SeqCanvas(ap);\r
-    setLayout(new BorderLayout());\r
-    add(seqCanvas, BorderLayout.CENTER);\r
-\r
-    this.ap = ap;\r
-\r
-    if (!av.isDataset())\r
-    {\r
-      addMouseMotionListener(this);\r
-      addMouseListener(this);\r
-      addMouseWheelListener(this);\r
-      ssm = StructureSelectionManager.getStructureSelectionManager();\r
-      ssm.addStructureViewerListener(this);\r
-    }\r
-  }\r
-\r
-  int startWrapBlock = -1;\r
-  int wrappedBlock = -1;\r
-  int findRes(MouseEvent evt)\r
-  {\r
-    int res = 0;\r
-    int x = evt.getX();\r
-\r
-    if (av.wrapAlignment)\r
-    {\r
-\r
-      int hgap = av.charHeight;\r
-      if (av.scaleAboveWrapped)\r
-      {\r
-        hgap += av.charHeight;\r
-      }\r
-\r
-      int cHeight = av.getAlignment().getHeight() * av.charHeight\r
-          + hgap + seqCanvas.getAnnotationHeight();\r
-\r
-      int y = evt.getY();\r
-      y -= hgap;\r
-      x -= seqCanvas.LABEL_WEST;\r
-\r
-      int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());\r
-      if (cwidth < 1)\r
-      {\r
-        return 0;\r
-      }\r
-\r
-      wrappedBlock = y / cHeight;\r
-      wrappedBlock += av.getStartRes() / cwidth;\r
-\r
-      res = wrappedBlock * cwidth + x / av.getCharWidth();\r
-\r
-    }\r
-    else\r
-    {\r
-      res = (x / av.getCharWidth()) + av.getStartRes();\r
-    }\r
-\r
-    if (av.hasHiddenColumns)\r
-    {\r
-      res = av.getColumnSelection().adjustForHiddenColumns(res);\r
-    }\r
-\r
-    return res;\r
-\r
-  }\r
-\r
-  int findSeq(MouseEvent evt)\r
-  {\r
-    int seq = 0;\r
-    int y = evt.getY();\r
-\r
-    if (av.wrapAlignment)\r
-    {\r
-      int hgap = av.charHeight;\r
-      if (av.scaleAboveWrapped)\r
-      {\r
-        hgap += av.charHeight;\r
-      }\r
-\r
-      int cHeight = av.getAlignment().getHeight() * av.charHeight\r
-          + hgap + seqCanvas.getAnnotationHeight();\r
-\r
-      y -= hgap;\r
-\r
-      seq = Math.min( (y % cHeight) / av.getCharHeight(),\r
-                     av.alignment.getHeight() - 1);\r
-    }\r
-    else\r
-    {\r
-      seq = Math.min( (y / av.getCharHeight()) + av.getStartSeq(),\r
-                     av.alignment.getHeight() - 1);\r
-    }\r
-\r
-    return seq;\r
-  }\r
-\r
-  SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)\r
-  {\r
-    Vector tmp = new Vector();\r
-    SequenceFeature[] features = sequence.getSequenceFeatures();\r
-    if (features != null)\r
-    {\r
-      for (int i = 0; i < features.length; i++)\r
-      {\r
-        if (av.featuresDisplayed == null\r
-            || !av.featuresDisplayed.containsKey(features[i].getType()))\r
-        {\r
-          continue;\r
-        }\r
-\r
-        if (features[i].featureGroup != null\r
-           && seqCanvas.fr.featureGroups!=null\r
-            && seqCanvas.fr.featureGroups.containsKey(features[i].featureGroup)\r
-            && !((Boolean)seqCanvas.fr.featureGroups.get(features[i].featureGroup)).booleanValue())\r
-          continue;\r
-\r
-\r
-        if ( (features[i].getBegin() <= res) &&\r
-            (features[i].getEnd() >= res))\r
-        {\r
-          tmp.addElement(features[i]);\r
-        }\r
-      }\r
-    }\r
-\r
-    features = new SequenceFeature[tmp.size()];\r
-    tmp.copyInto(features);\r
-\r
-    return features;\r
-  }\r
-\r
-\r
-  void endEditing()\r
-  {\r
-    if (editCommand != null && editCommand.getSize() > 0)\r
-    {\r
-      ap.alignFrame.addHistoryItem(editCommand);\r
-      av.firePropertyChange("alignment", null,\r
-                            av.getAlignment().getSequences());\r
-    }\r
-\r
-    startseq = -1;\r
-    lastres = -1;\r
-    editingSeqs = false;\r
-    groupEditing = false;\r
-    keyboardNo1 = null;\r
-    keyboardNo2 = null;\r
-    editCommand = null;\r
-  }\r
-\r
-  void setCursorRow()\r
-  {\r
-    seqCanvas.cursorY = getKeyboardNo1() - 1;\r
-    scrollToVisible();\r
-  }\r
-\r
-  void setCursorColumn()\r
-  {\r
-    seqCanvas.cursorX = getKeyboardNo1() - 1;\r
-    scrollToVisible();\r
-  }\r
-\r
-  void setCursorRowAndColumn()\r
-  {\r
-    if (keyboardNo2 == null)\r
-    {\r
-      keyboardNo2 = new StringBuffer();\r
-    }\r
-    else\r
-    {\r
-      seqCanvas.cursorX = getKeyboardNo1() - 1;\r
-      seqCanvas.cursorY = getKeyboardNo2() - 1;\r
-      scrollToVisible();\r
-    }\r
-  }\r
-\r
-  void setCursorPosition()\r
-  {\r
-    SequenceI sequence =\r
-        (Sequence) av.getAlignment().getSequenceAt(seqCanvas.cursorY);\r
-\r
-    seqCanvas.cursorX = sequence.findIndex(\r
-        getKeyboardNo1() - 1\r
-        );\r
-    scrollToVisible();\r
-  }\r
-\r
-  void moveCursor(int dx, int dy)\r
-  {\r
-    seqCanvas.cursorX += dx;\r
-    seqCanvas.cursorY += dy;\r
-    if (av.hasHiddenColumns && !av.colSel.isVisible(seqCanvas.cursorX))\r
-    {\r
-      int original = seqCanvas.cursorX - dx;\r
-      int maxWidth = av.alignment.getWidth();\r
-\r
-      while (!av.colSel.isVisible(seqCanvas.cursorX)\r
-             && seqCanvas.cursorX < maxWidth\r
-             && seqCanvas.cursorX > 0)\r
-      {\r
-        seqCanvas.cursorX += dx;\r
-      }\r
-\r
-      if (seqCanvas.cursorX >= maxWidth\r
-          || !av.colSel.isVisible(seqCanvas.cursorX))\r
-      {\r
-        seqCanvas.cursorX = original;\r
-      }\r
-    }\r
-\r
-    scrollToVisible();\r
-  }\r
-\r
-  void scrollToVisible()\r
-  {\r
-    if (seqCanvas.cursorX < 0)\r
-    {\r
-      seqCanvas.cursorX = 0;\r
-    }\r
-    else if (seqCanvas.cursorX > av.alignment.getWidth() - 1)\r
-    {\r
-      seqCanvas.cursorX = av.alignment.getWidth() - 1;\r
-    }\r
-\r
-    if (seqCanvas.cursorY < 0)\r
-    {\r
-      seqCanvas.cursorY = 0;\r
-    }\r
-    else if (seqCanvas.cursorY > av.alignment.getHeight() - 1)\r
-    {\r
-      seqCanvas.cursorY = av.alignment.getHeight() - 1;\r
-    }\r
-\r
-    endEditing();\r
-    if (av.wrapAlignment)\r
-    {\r
-      ap.scrollToWrappedVisible(seqCanvas.cursorX);\r
-    }\r
-    else\r
-    {\r
-      while (seqCanvas.cursorY < av.startSeq)\r
-      {\r
-        ap.scrollUp(true);\r
-      }\r
-      while (seqCanvas.cursorY + 1 > av.endSeq)\r
-      {\r
-        ap.scrollUp(false);\r
-      }\r
-      if (!av.wrapAlignment)\r
-      {\r
-        while (seqCanvas.cursorX < av.colSel.adjustForHiddenColumns(av.startRes))\r
-        {\r
-          if (!ap.scrollRight(false))\r
-          {\r
-            break;\r
-          }\r
-        }\r
-        while (seqCanvas.cursorX > av.colSel.adjustForHiddenColumns(av.endRes))\r
-        {\r
-          if (!ap.scrollRight(true))\r
-          {\r
-            break;\r
-          }\r
-        }\r
-      }\r
-    }\r
-    setStatusMessage(av.alignment.getSequenceAt(seqCanvas.cursorY),\r
-                     seqCanvas.cursorX, seqCanvas.cursorY);\r
-\r
-    seqCanvas.repaint();\r
-  }\r
-\r
-  void setSelectionAreaAtCursor(boolean topLeft)\r
-  {\r
-    SequenceI sequence =\r
-        (Sequence) av.getAlignment().getSequenceAt(seqCanvas.cursorY);\r
-\r
-    if (av.getSelectionGroup() != null)\r
-    {\r
-      SequenceGroup sg = av.selectionGroup;\r
-      //Find the top and bottom of this group\r
-      int min = av.alignment.getHeight(), max = 0;\r
-      for (int i = 0; i < sg.getSize(); i++)\r
-      {\r
-        int index = av.alignment.findIndex(sg.getSequenceAt(i));\r
-        if (index > max)\r
-        {\r
-          max = index;\r
-        }\r
-        if (index < min)\r
-        {\r
-          min = index;\r
-        }\r
-      }\r
-\r
-      max++;\r
-\r
-      if (topLeft)\r
-      {\r
-        sg.setStartRes(seqCanvas.cursorX);\r
-        if (sg.getEndRes() < seqCanvas.cursorX)\r
-        {\r
-          sg.setEndRes(seqCanvas.cursorX);\r
-        }\r
-\r
-        min = seqCanvas.cursorY;\r
-      }\r
-      else\r
-      {\r
-        sg.setEndRes(seqCanvas.cursorX);\r
-        if (sg.getStartRes() > seqCanvas.cursorX)\r
-        {\r
-          sg.setStartRes(seqCanvas.cursorX);\r
-        }\r
-\r
-        max = seqCanvas.cursorY + 1;\r
-      }\r
-\r
-      if (min > max)\r
-      {\r
-        // Only the user can do this\r
-        av.setSelectionGroup(null);\r
-      }\r
-      else\r
-      {\r
-        // Now add any sequences between min and max\r
-        sg.getSequences(null).clear();\r
-        for (int i = min; i < max; i++)\r
-        {\r
-          sg.addSequence(av.alignment.getSequenceAt(i), false);\r
-        }\r
-      }\r
-    }\r
-\r
-    if (av.getSelectionGroup() == null)\r
-    {\r
-      SequenceGroup sg = new SequenceGroup();\r
-      sg.setStartRes(seqCanvas.cursorX);\r
-      sg.setEndRes(seqCanvas.cursorX);\r
-      sg.addSequence(sequence, false);\r
-      av.setSelectionGroup(sg);\r
-    }\r
-\r
-    ap.paintAlignment(false);\r
-  }\r
-\r
-  void insertGapAtCursor(boolean group)\r
-  {\r
-    groupEditing = group;\r
-    startseq = seqCanvas.cursorY;\r
-    lastres = seqCanvas.cursorX;\r
-    editSequence(true, seqCanvas.cursorX + getKeyboardNo1());\r
-    endEditing();\r
-  }\r
-\r
-  void deleteGapAtCursor(boolean group)\r
-  {\r
-    groupEditing = group;\r
-    startseq = seqCanvas.cursorY;\r
-    lastres = seqCanvas.cursorX + getKeyboardNo1();\r
-    editSequence(false, seqCanvas.cursorX);\r
-    endEditing();\r
-  }\r
-\r
-  void numberPressed(char value)\r
-  {\r
-    if (keyboardNo1 == null)\r
-    {\r
-      keyboardNo1 = new StringBuffer();\r
-    }\r
-\r
-    if (keyboardNo2 != null)\r
-    {\r
-      keyboardNo2.append(value);\r
-    }\r
-    else\r
-    {\r
-      keyboardNo1.append(value);\r
-    }\r
-  }\r
-\r
-  int getKeyboardNo1()\r
-  {\r
-    if (keyboardNo1 == null)\r
-      return 1;\r
-    else\r
-    {\r
-      int value = Integer.parseInt(keyboardNo1.toString());\r
-      keyboardNo1 = null;\r
-      return value;\r
-    }\r
-  }\r
-\r
-  int getKeyboardNo2()\r
-  {\r
-    if (keyboardNo2 == null)\r
-      return 1;\r
-    else\r
-    {\r
-      int value = Integer.parseInt(keyboardNo2.toString());\r
-      keyboardNo2 = null;\r
-      return value;\r
-    }\r
-  }\r
-\r
-  /**\r
-   * DOCUMENT ME!\r
-   *\r
-   * @param evt DOCUMENT ME!\r
-   */\r
-  public void mouseReleased(MouseEvent evt)\r
-  {\r
-    mouseDragging = false;\r
-    mouseWheelPressed = false;\r
-\r
-    if (!editingSeqs)\r
-    {\r
-      doMouseReleasedDefineMode(evt);\r
-      return;\r
-    }\r
-\r
-    endEditing();\r
-  }\r
-\r
-  /**\r
-   * DOCUMENT ME!\r
-   *\r
-   * @param evt DOCUMENT ME!\r
-   */\r
-  public void mousePressed(MouseEvent evt)\r
-  {\r
-    lastMousePress = evt.getPoint();\r
-\r
-    if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))\r
-    {\r
-      mouseWheelPressed = true;\r
-      return;\r
-    }\r
-\r
-    if (evt.isShiftDown() || evt.isAltDown() ||\r
-        evt.isControlDown())\r
-    {\r
-      if (evt.isAltDown() || evt.isControlDown())\r
-      {\r
-        groupEditing = true;\r
-      }\r
-      editingSeqs = true;\r
-    }\r
-    else\r
-    {\r
-      doMousePressedDefineMode(evt);\r
-      return;\r
-    }\r
-\r
-    int seq = findSeq(evt);\r
-    int res = findRes(evt);\r
-\r
-    if (seq < 0 || res < 0)\r
-    {\r
-      return;\r
-    }\r
-\r
-    if ( (seq < av.getAlignment().getHeight()) &&\r
-        (res < av.getAlignment().getSequenceAt(seq).getLength()))\r
-    {\r
-      startseq = seq;\r
-      lastres = res;\r
-    }\r
-    else\r
-    {\r
-      startseq = -1;\r
-      lastres = -1;\r
-    }\r
-\r
-    return;\r
-  }\r
-\r
-  String lastMessage;\r
-  public void mouseOverSequence(SequenceI sequence, int index)\r
-  {\r
-    String tmp = sequence.hashCode()+index+"";\r
-    if (lastMessage == null || !lastMessage.equals(tmp))\r
-      ssm.mouseOverSequence(sequence, index);\r
-\r
-    lastMessage = tmp;\r
-  }\r
-\r
-\r
-  public void highlightSequence(SearchResults results)\r
-  {\r
-      seqCanvas.highlightSearchResults(results);\r
-  }\r
-\r
-  public void updateColours(SequenceI seq, int index)\r
-  {\r
-    System.out.println("update the seqPanel colours");\r
-    //repaint();\r
-  }\r
-\r
-  /**\r
-   * DOCUMENT ME!\r
-   *\r
-   * @param evt DOCUMENT ME!\r
-   */\r
-  public void mouseMoved(MouseEvent evt)\r
-  {\r
-    if (editingSeqs)\r
-    {\r
-      // This is because MacOSX creates a mouseMoved\r
-      // If control is down, other platforms will not.\r
-      mouseDragged(evt);\r
-    }\r
-\r
-    int res = findRes(evt);\r
-    int seq = findSeq(evt);\r
-\r
-    if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())\r
-    {\r
-      return;\r
-    }\r
-\r
-    SequenceI sequence = av.getAlignment().getSequenceAt(seq);\r
-\r
-    if (res > sequence.getLength())\r
-    {\r
-      return;\r
-    }\r
-\r
-    if (ssm != null)\r
-      mouseOverSequence(sequence, sequence.findPosition(res));\r
-\r
-    setStatusMessage(sequence, res, seq);\r
-\r
-    tooltipText.setLength(6); // Cuts the buffer back to <html>\r
-\r
-    SequenceGroup[] groups = av.alignment.findAllGroups(sequence);\r
-    if (groups != null)\r
-    {\r
-      for (int g = 0; g < groups.length; g++)\r
-      {\r
-        if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)\r
-        {\r
-          if (tooltipText.length() > 6)\r
-          {\r
-            tooltipText.append("<br>");\r
-          }\r
-\r
-          if (!groups[g].getName().startsWith("JTreeGroup") &&\r
-              !groups[g].getName().startsWith("JGroup"))\r
-          {\r
-            tooltipText.append(groups[g].getName());\r
-          }\r
-\r
-          if (groups[g].getDescription() != null)\r
-          {\r
-            tooltipText.append(": " + groups[g].getDescription());\r
-          }\r
-        }\r
-      }\r
-    }\r
-\r
-    // use aa to see if the mouse pointer is on a\r
-    if (av.showSequenceFeatures)\r
-    {\r
-      SequenceFeature[] features = findFeaturesAtRes(\r
-          sequence.getDatasetSequence(), sequence.findPosition(res));\r
-\r
-      if (features != null)\r
-      {\r
-        for (int i = 0; i < features.length; i++)\r
-        {\r
-          if (features[i].getType().equals("disulfide bond"))\r
-          {\r
-            if (features[i].getBegin() == sequence.findPosition(res)\r
-                || features[i].getEnd() == sequence.findPosition(res))\r
-            {\r
-              if (tooltipText.length() > 6)\r
-              {\r
-                tooltipText.append("<br>");\r
-              }\r
-              tooltipText.append("disulfide bond " + features[i].getBegin() +\r
-                                 ":" +\r
-                                 features[i].getEnd());\r
-              if (features[i].links != null)\r
-              {\r
-                tooltipText.append(" <img src=\"" + linkImageURL + "\">");\r
-              }\r
-            }\r
-          }\r
-          else\r
-          {\r
-            if (tooltipText.length() > 6)\r
-            {\r
-              tooltipText.append("<br>");\r
-            }\r
-\r
-            tooltipText.append(features[i].getType() + " " +\r
-                               features[i].begin);\r
-            if (features[i].begin != features[i].end)\r
-            {\r
-              tooltipText.append(" " + features[i].end);\r
-            }\r
-\r
-            if (features[i].getDescription() != null\r
-                && !features[i].description.equals(features[i].getType()))\r
-            {\r
-              tmpString = features[i].getDescription();\r
-              int startTag = tmpString.toUpperCase().indexOf("<HTML>");\r
-              if (startTag > -1)\r
-              {\r
-                tmpString = tmpString.substring(startTag + 6);\r
-              }\r
-              int endTag = tmpString.toUpperCase().indexOf("</BODY>");\r
-              if (endTag > -1)\r
-              {\r
-                tmpString = tmpString.substring(0, endTag);\r
-              }\r
-              endTag = tmpString.toUpperCase().indexOf("</HTML>");\r
-              if (endTag > -1)\r
-              {\r
-                tmpString = tmpString.substring(0, endTag);\r
-              }\r
-\r
-              if (startTag > -1)\r
-              {\r
-                tooltipText.append("; " + tmpString);\r
-              }\r
-              else\r
-              {\r
-                if (tmpString.indexOf("<") > -1 || tmpString.indexOf(">") > -1)\r
-                {\r
-                  //The description does not specify html is to\r
-                  //be used, so we must remove < > symbols\r
-                  tmpString = tmpString.replaceAll("<", "&lt;");\r
-                  tmpString = tmpString.replaceAll(">", "&gt;");\r
-\r
-                  tooltipText.append("; ");\r
-                  tooltipText.append(tmpString);\r
-\r
-                }\r
-                else\r
-                {\r
-                  tooltipText.append("; " + tmpString);\r
-                }\r
-              }\r
-            }\r
-            if (features[i].getValue("status") != null)\r
-            {\r
-              String status = features[i].getValue("status").toString();\r
-              if (status.length() > 0)\r
-              {\r
-                tooltipText.append("; (" + features[i].getValue("status") + ")");\r
-              }\r
-            }\r
-\r
-            if (features[i].links != null)\r
-            {\r
-              tooltipText.append(" <img src=\"" + linkImageURL + "\">");\r
-            }\r
-\r
-          }\r
-        }\r
-      }\r
-    }\r
-\r
-    if (tooltipText.length() == 6) // <html></html>\r
-    {\r
-      setToolTipText(null);\r
-    }\r
-    else\r
-    {\r
-      tooltipText.append("</html>");\r
-      if (lastTooltip == null || !lastTooltip.equals(tooltipText.toString()))\r
-        setToolTipText(tooltipText.toString());\r
-\r
-      lastTooltip = tooltipText.toString();\r
-    }\r
-\r
-  }\r
-  String lastTooltip;\r
-\r
-  void setStatusMessage(SequenceI sequence, int res, int seq)\r
-  {\r
-    StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: " +\r
-                                         sequence.getName());\r
-\r
-    Object obj = null;\r
-    if (av.alignment.isNucleotide())\r
-    {\r
-      obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res) +\r
-                                                 "");\r
-      if (obj != null)\r
-      {\r
-        text.append(" Nucleotide: ");\r
-      }\r
-    }\r
-    else\r
-    {\r
-      obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");\r
-      if (obj != null)\r
-      {\r
-        text.append("  Residue: ");\r
-      }\r
-    }\r
-\r
-    if (obj != null)\r
-    {\r
-\r
-      if (obj != "")\r
-      {\r
-        text.append(obj + " (" + sequence.findPosition(res) +\r
-                    ")");\r
-      }\r
-    }\r
-    ap.alignFrame.statusBar.setText(text.toString());\r
-\r
-  }\r
-\r
-  /**\r
-   * DOCUMENT ME!\r
-   *\r
-   * @param evt DOCUMENT ME!\r
-   */\r
-  public void mouseDragged(MouseEvent evt)\r
-  {\r
-    if (mouseWheelPressed)\r
-    {\r
-      int oldWidth = av.charWidth;\r
-\r
-      //Which is bigger, left-right or up-down?\r
-      if (Math.abs(evt.getY() - lastMousePress.getY())\r
-          > Math.abs(evt.getX() - lastMousePress.getX()))\r
-      {\r
-        int fontSize = av.font.getSize();\r
-\r
-        if (evt.getY() < lastMousePress.getY())\r
-        {\r
-          fontSize--;\r
-        }\r
-        else if (evt.getY() > lastMousePress.getY())\r
-        {\r
-          fontSize++;\r
-        }\r
-\r
-        if (fontSize < 1)\r
-        {\r
-          fontSize = 1;\r
-        }\r
-\r
-        av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));\r
-        av.charWidth = oldWidth;\r
-        ap.fontChanged();\r
-      }\r
-      else\r
-      {\r
-        if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)\r
-        {\r
-          av.charWidth--;\r
-        }\r
-        else if (evt.getX() > lastMousePress.getX())\r
-        {\r
-          av.charWidth++;\r
-        }\r
-\r
-        ap.paintAlignment(false);\r
-      }\r
-\r
-      FontMetrics fm = getFontMetrics(av.getFont());\r
-      av.validCharWidth = fm.charWidth('M') <= av.charWidth;\r
-\r
-      lastMousePress = evt.getPoint();\r
-\r
-      return;\r
-    }\r
-\r
-    if (!editingSeqs)\r
-    {\r
-      doMouseDraggedDefineMode(evt);\r
-      return;\r
-    }\r
-\r
-    int res = findRes(evt);\r
-\r
-    if (res < 0)\r
-    {\r
-      res = 0;\r
-    }\r
-\r
-    if ( (lastres == -1) || (lastres == res))\r
-    {\r
-      return;\r
-    }\r
-\r
-    if ( (res < av.getAlignment().getWidth()) && (res < lastres))\r
-    {\r
-      // dragLeft, delete gap\r
-      editSequence(false, res);\r
-    }\r
-    else\r
-    {\r
-      editSequence(true, res);\r
-    }\r
-\r
-    mouseDragging = true;\r
-    if (scrollThread != null)\r
-    {\r
-      scrollThread.setEvent(evt);\r
-    }\r
-  }\r
-\r
-\r
-\r
-  synchronized void editSequence(boolean insertGap, int startres)\r
-  {\r
-    int fixedLeft = -1;\r
-    int fixedRight = -1;\r
-    boolean fixedColumns = false;\r
-    SequenceGroup sg = av.getSelectionGroup();\r
-\r
-    SequenceI seq = av.alignment.getSequenceAt(startseq);\r
-\r
-    //No group, but the sequence may represent a group\r
-    if (!groupEditing && av.hasHiddenRows)\r
-    {\r
-      if (av.hiddenRepSequences != null\r
-          && av.hiddenRepSequences.containsKey(seq))\r
-      {\r
-        sg = (SequenceGroup) av.hiddenRepSequences.get(seq);\r
-        groupEditing = true;\r
-      }\r
-    }\r
-\r
-    StringBuffer message = new StringBuffer();\r
-    if (groupEditing)\r
-    {\r
-      message.append("Edit group:");\r
-      if (editCommand == null)\r
-      {\r
-        editCommand = new EditCommand("Edit Group");\r
-      }\r
-    }\r
-    else\r
-    {\r
-      message.append("Edit sequence: " + seq.getName());\r
-      String label = seq.getName();\r
-      if (label.length() > 10)\r
-      {\r
-        label = label.substring(0, 10);\r
-      }\r
-      if (editCommand == null)\r
-      {\r
-        editCommand = new EditCommand("Edit " + label);\r
-      }\r
-    }\r
-\r
-    if (insertGap)\r
-    {\r
-      message.append(" insert ");\r
-    }\r
-    else\r
-    {\r
-      message.append(" delete ");\r
-    }\r
-\r
-    message.append(Math.abs(startres - lastres) + " gaps.");\r
-    ap.alignFrame.statusBar.setText(message.toString());\r
-\r
-    //Are we editing within a selection group?\r
-    if (groupEditing\r
-        || (sg != null && sg.getSequences(av.hiddenRepSequences).contains(seq)))\r
-    {\r
-      fixedColumns = true;\r
-\r
-      //sg might be null as the user may only see 1 sequence,\r
-      //but the sequence represents a group\r
-      if (sg == null)\r
-      {\r
-        if (av.hiddenRepSequences == null\r
-            || !av.hiddenRepSequences.containsKey(seq))\r
-        {\r
-          endEditing();\r
-          return;\r
-        }\r
-        sg = (SequenceGroup) av.hiddenRepSequences.get(seq);\r
-      }\r
-\r
-      fixedLeft = sg.getStartRes();\r
-      fixedRight = sg.getEndRes();\r
-\r
-      if ( (startres < fixedLeft && lastres >= fixedLeft)\r
-          || (startres >= fixedLeft && lastres < fixedLeft)\r
-          || (startres > fixedRight && lastres <= fixedRight)\r
-          || (startres <= fixedRight && lastres > fixedRight))\r
-      {\r
-        endEditing();\r
-        return;\r
-      }\r
-\r
-      if (fixedLeft > startres)\r
-      {\r
-        fixedRight = fixedLeft - 1;\r
-        fixedLeft = 0;\r
-      }\r
-      else if (fixedRight < startres)\r
-      {\r
-        fixedLeft = fixedRight;\r
-        fixedRight = -1;\r
-      }\r
-    }\r
-\r
-    if (av.hasHiddenColumns)\r
-    {\r
-      fixedColumns = true;\r
-      int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);\r
-      int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);\r
-\r
-      if ( (insertGap && startres > y1 && lastres < y1)\r
-          || (!insertGap && startres < y2 && lastres > y2))\r
-      {\r
-        endEditing();\r
-        return;\r
-      }\r
-\r
-      //System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");\r
-      //Selection spans a hidden region\r
-      if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))\r
-      {\r
-        if (startres >= y2)\r
-        {\r
-          fixedLeft = y2;\r
-        }\r
-        else\r
-        {\r
-          fixedRight = y2 - 1;\r
-        }\r
-      }\r
-    }\r
-\r
-    if (groupEditing)\r
-    {\r
-      Vector vseqs = sg.getSequences(av.hiddenRepSequences);\r
-      int g, groupSize = vseqs.size();\r
-      SequenceI[] groupSeqs = new SequenceI[groupSize];\r
-      for (g = 0; g < groupSeqs.length; g++)\r
-      {\r
-        groupSeqs[g] = (SequenceI) vseqs.elementAt(g);\r
-      }\r
-\r
-      // drag to right\r
-      if (insertGap)\r
-      {\r
-        //If the user has selected the whole sequence, and is dragging to\r
-        // the right, we can still extend the alignment and selectionGroup\r
-        if (sg.getStartRes() == 0\r
-            && sg.getEndRes() == fixedRight\r
-            && sg.getEndRes() == av.alignment.getWidth() - 1\r
-            )\r
-        {\r
-          sg.setEndRes(av.alignment.getWidth() + startres - lastres);\r
-          fixedRight = sg.getEndRes();\r
-        }\r
-\r
-        // Is it valid with fixed columns??\r
-        // Find the next gap before the end\r
-        // of the visible region boundary\r
-        boolean blank = false;\r
-        for (fixedRight = fixedRight;\r
-             fixedRight > lastres;\r
-             fixedRight--)\r
-        {\r
-          blank = true;\r
-\r
-          for (g = 0; g < groupSize; g++)\r
-          {\r
-            for (int j = 0; j < startres - lastres; j++)\r
-            {\r
-              if (!jalview.util.Comparison.isGap(\r
-                  groupSeqs[g].getCharAt(fixedRight - j)))\r
-              {\r
-                blank = false;\r
-                break;\r
-              }\r
-            }\r
-          }\r
-          if (blank)\r
-          {\r
-            break;\r
-          }\r
-        }\r
-\r
-        if (!blank)\r
-        {\r
-          if (sg.getSize() == av.alignment.getHeight())\r
-          {\r
-            if ( (av.hasHiddenColumns\r
-                  &&\r
-                  startres < av.getColumnSelection().getHiddenBoundaryRight(startres)))\r
-            {\r
-              endEditing();\r
-              return;\r
-            }\r
-\r
-            int alWidth = av.alignment.getWidth();\r
-            if (av.hasHiddenRows)\r
-            {\r
-              int hwidth = av.alignment.getHiddenSequences().getWidth();\r
-              if (hwidth > alWidth)\r
-              {\r
-                alWidth = hwidth;\r
-              }\r
-            }\r
-            //We can still insert gaps if the selectionGroup\r
-            //contains all the sequences\r
-            sg.setEndRes(sg.getEndRes() + startres - lastres);\r
-            fixedRight = alWidth + startres - lastres;\r
-          }\r
-          else\r
-          {\r
-            endEditing();\r
-            return;\r
-          }\r
-        }\r
-      }\r
-\r
-      // drag to left\r
-      else if (!insertGap)\r
-      {\r
-        /// Are we able to delete?\r
-        // ie are all columns blank?\r
-\r
-        for (g = 0; g < groupSize; g++)\r
-        {\r
-          for (int j = startres; j < lastres; j++)\r
-          {\r
-            if (groupSeqs[g].getLength() <= j)\r
-            {\r
-              continue;\r
-            }\r
-\r
-            if (!jalview.util.Comparison.isGap(\r
-                groupSeqs[g].getCharAt(j)))\r
-            {\r
-              // Not a gap, block edit not valid\r
-              endEditing();\r
-              return;\r
-            }\r
-          }\r
-        }\r
-      }\r
-\r
-      if (insertGap)\r
-      {\r
-        // dragging to the right\r
-        if (fixedColumns && fixedRight != -1)\r
-        {\r
-          for (int j = lastres; j < startres; j++)\r
-          {\r
-            insertChar(j, groupSeqs, fixedRight);\r
-          }\r
-        }\r
-        else\r
-        {\r
-          editCommand.appendEdit(EditCommand.INSERT_GAP,\r
-                                 groupSeqs,\r
-                                 startres, startres - lastres,\r
-                                 av.alignment,\r
-                                 true);\r
-        }\r
-      }\r
-      else\r
-      {\r
-        // dragging to the left\r
-        if (fixedColumns && fixedRight != -1)\r
-        {\r
-          for (int j = lastres; j > startres; j--)\r
-          {\r
-            deleteChar(startres, groupSeqs, fixedRight);\r
-          }\r
-        }\r
-        else\r
-        {\r
-          editCommand.appendEdit(EditCommand.DELETE_GAP,\r
-                                 groupSeqs,\r
-                                 startres, lastres - startres,\r
-                                 av.alignment,\r
-                                 true);\r
-        }\r
-\r
-      }\r
-    }\r
-    else /////Editing a single sequence///////////\r
-    {\r
-      if (insertGap)\r
-      {\r
-        // dragging to the right\r
-        if (fixedColumns && fixedRight != -1)\r
-        {\r
-          for (int j = lastres; j < startres; j++)\r
-          {\r
-            insertChar(j, new SequenceI[]\r
-                       {seq}, fixedRight);\r
-          }\r
-        }\r
-        else\r
-        {\r
-          editCommand.appendEdit(EditCommand.INSERT_GAP,\r
-                                 new SequenceI[]\r
-                                 {seq},\r
-                                 lastres, startres - lastres,\r
-                                 av.alignment,\r
-                                 true);\r
-        }\r
-      }\r
-      else\r
-      {\r
-        // dragging to the left\r
-        if (fixedColumns && fixedRight != -1)\r
-        {\r
-          for (int j = lastres; j > startres; j--)\r
-          {\r
-            if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))\r
-            {\r
-              endEditing();\r
-              break;\r
-            }\r
-            deleteChar(startres, new SequenceI[]\r
-                       {seq}, fixedRight);\r
-          }\r
-        }\r
-        else\r
-        {\r
-          //could be a keyboard edit trying to delete none gaps\r
-          int max = 0;\r
-          for (int m = startres; m < lastres; m++)\r
-          {\r
-            if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))\r
-            {\r
-              break;\r
-            }\r
-            max++;\r
-          }\r
-\r
-          if (max > 0)\r
-          {\r
-            editCommand.appendEdit(EditCommand.DELETE_GAP,\r
-                                   new SequenceI[]\r
-                                   {seq},\r
-                                   startres, max,\r
-                                   av.alignment,\r
-                                   true);\r
-          }\r
-        }\r
-      }\r
-    }\r
-\r
-    lastres = startres;\r
-    seqCanvas.repaint();\r
-  }\r
-\r
-  void insertChar(int j, SequenceI[] seq, int fixedColumn)\r
-  {\r
-    int blankColumn = fixedColumn;\r
-    for (int s = 0; s < seq.length; s++)\r
-    {\r
-      //Find the next gap before the end of the visible region boundary\r
-      //If lastCol > j, theres a boundary after the gap insertion\r
-\r
-      for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)\r
-      {\r
-        if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))\r
-        {\r
-          //Theres a space, so break and insert the gap\r
-          break;\r
-        }\r
-      }\r
-\r
-      if (blankColumn <= j)\r
-      {\r
-        blankColumn = fixedColumn;\r
-        endEditing();\r
-        return;\r
-      }\r
-    }\r
-\r
-    editCommand.appendEdit(EditCommand.DELETE_GAP,\r
-                           seq,\r
-                           blankColumn, 1, av.alignment, true);\r
-\r
-    editCommand.appendEdit(EditCommand.INSERT_GAP,\r
-                           seq,\r
-                           j, 1, av.alignment,\r
-                           true);\r
-\r
-  }\r
-\r
-  void deleteChar(int j, SequenceI[] seq, int fixedColumn)\r
-  {\r
-\r
-    editCommand.appendEdit(EditCommand.DELETE_GAP,\r
-                           seq,\r
-                           j, 1, av.alignment, true);\r
-\r
-    editCommand.appendEdit(EditCommand.INSERT_GAP,\r
-                           seq,\r
-                           fixedColumn, 1, av.alignment, true);\r
-  }\r
-\r
-  /**\r
-   * DOCUMENT ME!\r
-   *\r
-   * @param e DOCUMENT ME!\r
-   */\r
-  public void mouseEntered(MouseEvent e)\r
-  {\r
-    if (oldSeq < 0)\r
-    {\r
-      oldSeq = 0;\r
-    }\r
-\r
-    if (scrollThread != null)\r
-    {\r
-      scrollThread.running = false;\r
-      scrollThread = null;\r
-    }\r
-  }\r
-\r
-  /**\r
-   * DOCUMENT ME!\r
-   *\r
-   * @param e DOCUMENT ME!\r
-   */\r
-  public void mouseExited(MouseEvent e)\r
-  {\r
-    if (av.getWrapAlignment())\r
-    {\r
-      return;\r
-    }\r
-\r
-    if (mouseDragging)\r
-    {\r
-      scrollThread = new ScrollThread();\r
-    }\r
-  }\r
-\r
-  public void mouseClicked(MouseEvent evt)\r
-  {\r
-    SequenceI sequence = av.alignment.getSequenceAt(findSeq(evt));\r
-    if (evt.getClickCount() > 1)\r
-    {\r
-      if (av.getSelectionGroup().getSize() == 1\r
-          && av.getSelectionGroup().getEndRes()\r
-          - av.getSelectionGroup().getStartRes() < 2)\r
-      {\r
-        av.setSelectionGroup(null);\r
-      }\r
-\r
-      SequenceFeature[] features = findFeaturesAtRes(\r
-          sequence.getDatasetSequence(),\r
-          sequence.findPosition(findRes(evt))\r
-          );\r
-\r
-      if (features != null && features.length > 0)\r
-      {\r
-        SearchResults highlight = new SearchResults();\r
-        highlight.addResult(sequence,\r
-                            features[0].getBegin(),\r
-                            features[0].getEnd());\r
-        seqCanvas.highlightSearchResults(highlight);\r
-      }\r
-      if (features != null && features.length > 0)\r
-      {\r
-        seqCanvas.getFeatureRenderer().amendFeatures(\r
-            new SequenceI[]\r
-            {sequence}, features, false, ap);\r
-\r
-        seqCanvas.highlightSearchResults(null);\r
-      }\r
-    }\r
-  }\r
-\r
-  public void mouseWheelMoved(MouseWheelEvent e)\r
-  {\r
-    e.consume();\r
-      if (e.getWheelRotation() > 0)\r
-      {\r
-        ap.scrollUp(false);\r
-      }\r
-      else\r
-      {\r
-        ap.scrollUp(true);\r
-      }\r
-  }\r
-\r
-  /**\r
-   * DOCUMENT ME!\r
-   *\r
-   * @param evt DOCUMENT ME!\r
-   */\r
-  public void doMousePressedDefineMode(MouseEvent evt)\r
-  {\r
-    int res = findRes(evt);\r
-    int seq = findSeq(evt);\r
-    oldSeq = seq;\r
-\r
-    startWrapBlock = wrappedBlock;\r
-\r
-    if (av.wrapAlignment && seq > av.alignment.getHeight())\r
-    {\r
-      JOptionPane.showInternalMessageDialog(Desktop.desktop,\r
-                                            "Cannot edit annotations in wrapped view.",\r
-                                            "Wrapped view - no edit",\r
-                                            JOptionPane.WARNING_MESSAGE);\r
-      return;\r
-    }\r
-\r
-    if (seq < 0 || res < 0)\r
-    {\r
-      return;\r
-    }\r
-\r
-    SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);\r
-\r
-    if ( (sequence == null) || (res > sequence.getLength()))\r
-    {\r
-      return;\r
-    }\r
-\r
-    stretchGroup = av.getSelectionGroup();\r
-\r
-    if (stretchGroup == null)\r
-    {\r
-      stretchGroup = av.alignment.findGroup(sequence);\r
-\r
-      if ( (stretchGroup != null) && (res > stretchGroup.getStartRes()) &&\r
-          (res < stretchGroup.getEndRes()))\r
-      {\r
-        av.setSelectionGroup(stretchGroup);\r
-      }\r
-      else\r
-      {\r
-        stretchGroup = null;\r
-      }\r
-    }\r
-    else if (!stretchGroup.getSequences(null).contains(sequence) ||\r
-             (stretchGroup.getStartRes() > res) ||\r
-             (stretchGroup.getEndRes() < res))\r
-    {\r
-      stretchGroup = null;\r
-\r
-      SequenceGroup[] allGroups = av.alignment.findAllGroups(sequence);\r
-\r
-      if (allGroups != null)\r
-      {\r
-        for (int i = 0; i < allGroups.length; i++)\r
-        {\r
-          if ( (allGroups[i].getStartRes() <= res) &&\r
-              (allGroups[i].getEndRes() >= res))\r
-          {\r
-            stretchGroup = allGroups[i];\r
-            break;\r
-          }\r
-        }\r
-      }\r
-\r
-      av.setSelectionGroup(stretchGroup);\r
-\r
-    }\r
-\r
-    if (javax.swing.SwingUtilities.isRightMouseButton(evt))\r
-    {\r
-      SequenceFeature [] allFeatures = findFeaturesAtRes(sequence.getDatasetSequence(),\r
-                                               sequence.findPosition(res));\r
-      Vector links = new Vector();\r
-      for (int i = 0; i < allFeatures.length; i++)\r
-      {\r
-        if (allFeatures[i].links != null)\r
-        {\r
-          for (int j = 0; j < allFeatures[i].links.size(); j++)\r
-          {\r
-            links.addElement(allFeatures[i].links.elementAt(j));\r
-          }\r
-        }\r
-      }\r
-\r
-      jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);\r
-      pop.show(this, evt.getX(), evt.getY());\r
-      return;\r
-    }\r
-\r
-    if (av.cursorMode)\r
-    {\r
-      seqCanvas.cursorX = findRes(evt);\r
-      seqCanvas.cursorY = findSeq(evt);\r
-      seqCanvas.repaint();\r
-      return;\r
-    }\r
-\r
-    if (stretchGroup == null)\r
-    {\r
-      //Only if left mouse button do we want to change group sizes\r
-\r
-      // define a new group here\r
-      SequenceGroup sg = new SequenceGroup();\r
-      sg.setStartRes(res);\r
-      sg.setEndRes(res);\r
-      sg.addSequence(sequence, false);\r
-      av.setSelectionGroup(sg);\r
-      stretchGroup = sg;\r
-\r
-      if (av.getConservationSelected())\r
-      {\r
-        SliderPanel.setConservationSlider(ap,\r
-                                          av.getGlobalColourScheme(),\r
-                                          "Background");\r
-      }\r
-\r
-      if (av.getAbovePIDThreshold())\r
-      {\r
-        SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),\r
-                                       "Background");\r
-      }\r
-      if ( (stretchGroup != null) && (stretchGroup.getEndRes() == res))\r
-      {\r
-        // Edit end res position of selected group\r
-        changeEndRes = true;\r
-      }\r
-      else if ( (stretchGroup != null) &&\r
-               (stretchGroup.getStartRes() == res))\r
-      {\r
-        // Edit end res position of selected group\r
-        changeStartRes = true;\r
-      }\r
-      stretchGroup.getWidth();\r
-    }\r
-\r
-    seqCanvas.repaint();\r
-  }\r
-\r
-  /**\r
-   * DOCUMENT ME!\r
-   *\r
-   * @param evt DOCUMENT ME!\r
-   */\r
-  public void doMouseReleasedDefineMode(MouseEvent evt)\r
-  {\r
-    if (stretchGroup == null)\r
-    {\r
-      return;\r
-    }\r
-\r
-    if (stretchGroup.cs != null)\r
-    {\r
-      if (stretchGroup.cs instanceof ClustalxColourScheme)\r
-      {\r
-        ( (ClustalxColourScheme) stretchGroup.cs).resetClustalX(\r
-            stretchGroup.getSequences(av.hiddenRepSequences),\r
-            stretchGroup.getWidth());\r
-      }\r
-\r
-      if (stretchGroup.cs instanceof Blosum62ColourScheme\r
-          || stretchGroup.cs instanceof PIDColourScheme\r
-          || stretchGroup.cs.conservationApplied()\r
-          || stretchGroup.cs.getThreshold() > 0)\r
-      {\r
-        stretchGroup.recalcConservation();\r
-      }\r
-\r
-      if (stretchGroup.cs.conservationApplied())\r
-      {\r
-        SliderPanel.setConservationSlider(ap, stretchGroup.cs,\r
-                                          stretchGroup.getName());\r
-      }\r
-      else\r
-      {\r
-        SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,\r
-                                       stretchGroup.getName());\r
-      }\r
-      PaintRefresher.Refresh(this, av.getSequenceSetId());\r
-      ap.paintAlignment(true);\r
-    }\r
-\r
-    changeEndRes = false;\r
-    changeStartRes = false;\r
-    stretchGroup = null;\r
-\r
-  }\r
-\r
-  /**\r
-   * DOCUMENT ME!\r
-   *\r
-   * @param evt DOCUMENT ME!\r
-   */\r
-  public void doMouseDraggedDefineMode(MouseEvent evt)\r
-  {\r
-    int res = findRes(evt);\r
-    int y = findSeq(evt);\r
-\r
-    if (wrappedBlock != startWrapBlock)\r
-    {\r
-      return;\r
-    }\r
-\r
-    if (stretchGroup == null)\r
-    {\r
-      return;\r
-    }\r
-\r
-    if (res >= av.alignment.getWidth())\r
-    {\r
-      res = av.alignment.getWidth() - 1;\r
-    }\r
-\r
-    if (stretchGroup.getEndRes() == res)\r
-    {\r
-      // Edit end res position of selected group\r
-      changeEndRes = true;\r
-    }\r
-    else if (stretchGroup.getStartRes() == res)\r
-    {\r
-      // Edit start res position of selected group\r
-      changeStartRes = true;\r
-    }\r
-\r
-    if (res < av.getStartRes())\r
-    {\r
-      res = av.getStartRes();\r
-    }\r
-\r
-    if (changeEndRes)\r
-    {\r
-      if (res > (stretchGroup.getStartRes() - 1))\r
-      {\r
-        stretchGroup.setEndRes(res);\r
-      }\r
-    }\r
-    else if (changeStartRes)\r
-    {\r
-      if (res < (stretchGroup.getEndRes() + 1))\r
-      {\r
-        stretchGroup.setStartRes(res);\r
-      }\r
-    }\r
-\r
-    int dragDirection = 0;\r
-\r
-    if (y > oldSeq)\r
-    {\r
-      dragDirection = 1;\r
-    }\r
-    else if (y < oldSeq)\r
-    {\r
-      dragDirection = -1;\r
-    }\r
-\r
-    while ( (y != oldSeq) && (oldSeq > -1) && (y < av.alignment.getHeight()))\r
-    {\r
-      // This routine ensures we don't skip any sequences, as the\r
-      // selection is quite slow.\r
-      Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);\r
-\r
-      oldSeq += dragDirection;\r
-\r
-      if (oldSeq < 0)\r
-      {\r
-        break;\r
-      }\r
-\r
-      Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);\r
-\r
-      if (stretchGroup.getSequences(null).contains(nextSeq))\r
-      {\r
-        stretchGroup.deleteSequence(seq, false);\r
-      }\r
-      else\r
-      {\r
-        if (seq != null)\r
-        {\r
-          stretchGroup.addSequence(seq, false);\r
-        }\r
-\r
-        stretchGroup.addSequence(nextSeq, false);\r
-      }\r
-    }\r
-\r
-    if (oldSeq < 0)\r
-    {\r
-      oldSeq = -1;\r
-    }\r
-\r
-    mouseDragging = true;\r
-\r
-    if (scrollThread != null)\r
-    {\r
-      scrollThread.setEvent(evt);\r
-    }\r
-\r
-    seqCanvas.repaint();\r
-  }\r
-\r
-  void scrollCanvas(MouseEvent evt)\r
-  {\r
-    if (evt == null)\r
-    {\r
-      if (scrollThread != null)\r
-      {\r
-        scrollThread.running = false;\r
-        scrollThread = null;\r
-      }\r
-      mouseDragging = false;\r
-    }\r
-    else\r
-    {\r
-      if (scrollThread == null)\r
-      {\r
-        scrollThread = new ScrollThread();\r
-      }\r
-\r
-      mouseDragging = true;\r
-      scrollThread.setEvent(evt);\r
-    }\r
-\r
-  }\r
-\r
-  // this class allows scrolling off the bottom of the visible alignment\r
-  class ScrollThread\r
-      extends Thread\r
-  {\r
-    MouseEvent evt;\r
-    boolean running = false;\r
-\r
-    public ScrollThread()\r
-    {\r
-      start();\r
-    }\r
-\r
-    public void setEvent(MouseEvent e)\r
-    {\r
-      evt = e;\r
-    }\r
-\r
-    public void stopScrolling()\r
-    {\r
-      running = false;\r
-    }\r
-\r
-    public void run()\r
-    {\r
-      running = true;\r
-\r
-      while (running)\r
-      {\r
-        if (evt != null)\r
-        {\r
-          if (mouseDragging && (evt.getY() < 0) &&\r
-              (av.getStartSeq() > 0))\r
-          {\r
-            running = ap.scrollUp(true);\r
-          }\r
-\r
-          if (mouseDragging && (evt.getY() >= getHeight()) &&\r
-              (av.alignment.getHeight() > av.getEndSeq()))\r
-          {\r
-            running = ap.scrollUp(false);\r
-          }\r
-\r
-          if (mouseDragging && (evt.getX() < 0))\r
-          {\r
-            running = ap.scrollRight(false);\r
-          }\r
-          else if (mouseDragging && (evt.getX() >= getWidth()))\r
-          {\r
-            running = ap.scrollRight(true);\r
-          }\r
-        }\r
-\r
-        try\r
-        {\r
-          Thread.sleep(20);\r
-        }\r
-        catch (Exception ex)\r
-        {\r
-        }\r
-      }\r
-    }\r
-  }\r
-}\r
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer
+ * Copyright (C) 2007 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
+ */
+package jalview.gui;
+
+import java.util.*;
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+
+import jalview.commands.*;
+import jalview.datamodel.*;
+import jalview.schemes.*;
+import jalview.structure.*;
+
+/**
+ * DOCUMENT ME!
+ *
+ * @author $author$
+ * @version $Revision$
+ */
+public class SeqPanel
+    extends JPanel implements MouseListener,
+    MouseMotionListener, MouseWheelListener, SequenceListener
+
+{
+  /** DOCUMENT ME!! */
+  public SeqCanvas seqCanvas;
+
+  /** DOCUMENT ME!! */
+  public AlignmentPanel ap;
+  protected int lastres;
+  protected int startseq;
+  protected AlignViewport av;
+
+  ScrollThread scrollThread = null;
+  boolean mouseDragging = false;
+  boolean editingSeqs = false;
+  boolean groupEditing = false;
+
+  //////////////////////////////////////////
+  /////Everything below this is for defining the boundary of the rubberband
+  //////////////////////////////////////////
+  int oldSeq = -1;
+  boolean changeEndSeq = false;
+  boolean changeStartSeq = false;
+  boolean changeEndRes = false;
+  boolean changeStartRes = false;
+  SequenceGroup stretchGroup = null;
+  boolean remove = false;
+
+  Point lastMousePress;
+  boolean mouseWheelPressed = false;
+  StringBuffer keyboardNo1;
+  StringBuffer keyboardNo2;
+
+  java.net.URL linkImageURL;
+
+  StringBuffer tooltipText = new StringBuffer("<html>");
+  String tmpString;
+
+  EditCommand editCommand;
+
+  StructureSelectionManager ssm;
+
+
+  /**
+   * Creates a new SeqPanel object.
+   *
+   * @param avp DOCUMENT ME!
+   * @param p DOCUMENT ME!
+   */
+  public SeqPanel(AlignViewport av, AlignmentPanel ap)
+  {
+    linkImageURL = getClass().getResource("/images/link.gif");
+    ToolTipManager.sharedInstance().registerComponent(this);
+    ToolTipManager.sharedInstance().setInitialDelay(0);
+    ToolTipManager.sharedInstance().setDismissDelay(10000);
+    this.av = av;
+    setBackground(Color.white);
+
+    seqCanvas = new SeqCanvas(ap);
+    setLayout(new BorderLayout());
+    add(seqCanvas, BorderLayout.CENTER);
+
+    this.ap = ap;
+
+    if (!av.isDataset())
+    {
+      addMouseMotionListener(this);
+      addMouseListener(this);
+      addMouseWheelListener(this);
+      ssm = StructureSelectionManager.getStructureSelectionManager();
+      ssm.addStructureViewerListener(this);
+    }
+  }
+
+  int startWrapBlock = -1;
+  int wrappedBlock = -1;
+  int findRes(MouseEvent evt)
+  {
+    int res = 0;
+    int x = evt.getX();
+
+    if (av.wrapAlignment)
+    {
+
+      int hgap = av.charHeight;
+      if (av.scaleAboveWrapped)
+      {
+        hgap += av.charHeight;
+      }
+
+      int cHeight = av.getAlignment().getHeight() * av.charHeight
+          + hgap + seqCanvas.getAnnotationHeight();
+
+      int y = evt.getY();
+      y -= hgap;
+      x -= seqCanvas.LABEL_WEST;
+
+      int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
+      if (cwidth < 1)
+      {
+        return 0;
+      }
+
+      wrappedBlock = y / cHeight;
+      wrappedBlock += av.getStartRes() / cwidth;
+
+      res = wrappedBlock * cwidth + x / av.getCharWidth();
+
+    }
+    else
+    {
+      res = (x / av.getCharWidth()) + av.getStartRes();
+    }
+
+    if (av.hasHiddenColumns)
+    {
+      res = av.getColumnSelection().adjustForHiddenColumns(res);
+    }
+
+    return res;
+
+  }
+
+  int findSeq(MouseEvent evt)
+  {
+    int seq = 0;
+    int y = evt.getY();
+
+    if (av.wrapAlignment)
+    {
+      int hgap = av.charHeight;
+      if (av.scaleAboveWrapped)
+      {
+        hgap += av.charHeight;
+      }
+
+      int cHeight = av.getAlignment().getHeight() * av.charHeight
+          + hgap + seqCanvas.getAnnotationHeight();
+
+      y -= hgap;
+
+      seq = Math.min( (y % cHeight) / av.getCharHeight(),
+                     av.alignment.getHeight() - 1);
+    }
+    else
+    {
+      seq = Math.min( (y / av.getCharHeight()) + av.getStartSeq(),
+                     av.alignment.getHeight() - 1);
+    }
+
+    return seq;
+  }
+
+  SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
+  {
+    Vector tmp = new Vector();
+    SequenceFeature[] features = sequence.getSequenceFeatures();
+    if (features != null)
+    {
+      for (int i = 0; i < features.length; i++)
+      {
+        if (av.featuresDisplayed == null
+            || !av.featuresDisplayed.containsKey(features[i].getType()))
+        {
+          continue;
+        }
+
+        if (features[i].featureGroup != null
+           && seqCanvas.fr.featureGroups!=null
+            && seqCanvas.fr.featureGroups.containsKey(features[i].featureGroup)
+            && !((Boolean)seqCanvas.fr.featureGroups.get(features[i].featureGroup)).booleanValue())
+          continue;
+
+
+        if ( (features[i].getBegin() <= res) &&
+            (features[i].getEnd() >= res))
+        {
+          tmp.addElement(features[i]);
+        }
+      }
+    }
+
+    features = new SequenceFeature[tmp.size()];
+    tmp.copyInto(features);
+
+    return features;
+  }
+
+
+  void endEditing()
+  {
+    if (editCommand != null && editCommand.getSize() > 0)
+    {
+      ap.alignFrame.addHistoryItem(editCommand);
+      av.firePropertyChange("alignment", null,
+                            av.getAlignment().getSequences());
+    }
+
+    startseq = -1;
+    lastres = -1;
+    editingSeqs = false;
+    groupEditing = false;
+    keyboardNo1 = null;
+    keyboardNo2 = null;
+    editCommand = null;
+  }
+
+  void setCursorRow()
+  {
+    seqCanvas.cursorY = getKeyboardNo1() - 1;
+    scrollToVisible();
+  }
+
+  void setCursorColumn()
+  {
+    seqCanvas.cursorX = getKeyboardNo1() - 1;
+    scrollToVisible();
+  }
+
+  void setCursorRowAndColumn()
+  {
+    if (keyboardNo2 == null)
+    {
+      keyboardNo2 = new StringBuffer();
+    }
+    else
+    {
+      seqCanvas.cursorX = getKeyboardNo1() - 1;
+      seqCanvas.cursorY = getKeyboardNo2() - 1;
+      scrollToVisible();
+    }
+  }
+
+  void setCursorPosition()
+  {
+    SequenceI sequence =
+        (Sequence) av.getAlignment().getSequenceAt(seqCanvas.cursorY);
+
+    seqCanvas.cursorX = sequence.findIndex(
+        getKeyboardNo1() - 1
+        );
+    scrollToVisible();
+  }
+
+  void moveCursor(int dx, int dy)
+  {
+    seqCanvas.cursorX += dx;
+    seqCanvas.cursorY += dy;
+    if (av.hasHiddenColumns && !av.colSel.isVisible(seqCanvas.cursorX))
+    {
+      int original = seqCanvas.cursorX - dx;
+      int maxWidth = av.alignment.getWidth();
+
+      while (!av.colSel.isVisible(seqCanvas.cursorX)
+             && seqCanvas.cursorX < maxWidth
+             && seqCanvas.cursorX > 0)
+      {
+        seqCanvas.cursorX += dx;
+      }
+
+      if (seqCanvas.cursorX >= maxWidth
+          || !av.colSel.isVisible(seqCanvas.cursorX))
+      {
+        seqCanvas.cursorX = original;
+      }
+    }
+
+    scrollToVisible();
+  }
+
+  void scrollToVisible()
+  {
+    if (seqCanvas.cursorX < 0)
+    {
+      seqCanvas.cursorX = 0;
+    }
+    else if (seqCanvas.cursorX > av.alignment.getWidth() - 1)
+    {
+      seqCanvas.cursorX = av.alignment.getWidth() - 1;
+    }
+
+    if (seqCanvas.cursorY < 0)
+    {
+      seqCanvas.cursorY = 0;
+    }
+    else if (seqCanvas.cursorY > av.alignment.getHeight() - 1)
+    {
+      seqCanvas.cursorY = av.alignment.getHeight() - 1;
+    }
+
+    endEditing();
+    if (av.wrapAlignment)
+    {
+      ap.scrollToWrappedVisible(seqCanvas.cursorX);
+    }
+    else
+    {
+      while (seqCanvas.cursorY < av.startSeq)
+      {
+        ap.scrollUp(true);
+      }
+      while (seqCanvas.cursorY + 1 > av.endSeq)
+      {
+        ap.scrollUp(false);
+      }
+      if (!av.wrapAlignment)
+      {
+        while (seqCanvas.cursorX < av.colSel.adjustForHiddenColumns(av.startRes))
+        {
+          if (!ap.scrollRight(false))
+          {
+            break;
+          }
+        }
+        while (seqCanvas.cursorX > av.colSel.adjustForHiddenColumns(av.endRes))
+        {
+          if (!ap.scrollRight(true))
+          {
+            break;
+          }
+        }
+      }
+    }
+    setStatusMessage(av.alignment.getSequenceAt(seqCanvas.cursorY),
+                     seqCanvas.cursorX, seqCanvas.cursorY);
+
+    seqCanvas.repaint();
+  }
+
+  void setSelectionAreaAtCursor(boolean topLeft)
+  {
+    SequenceI sequence =
+        (Sequence) av.getAlignment().getSequenceAt(seqCanvas.cursorY);
+
+    if (av.getSelectionGroup() != null)
+    {
+      SequenceGroup sg = av.selectionGroup;
+      //Find the top and bottom of this group
+      int min = av.alignment.getHeight(), max = 0;
+      for (int i = 0; i < sg.getSize(); i++)
+      {
+        int index = av.alignment.findIndex(sg.getSequenceAt(i));
+        if (index > max)
+        {
+          max = index;
+        }
+        if (index < min)
+        {
+          min = index;
+        }
+      }
+
+      max++;
+
+      if (topLeft)
+      {
+        sg.setStartRes(seqCanvas.cursorX);
+        if (sg.getEndRes() < seqCanvas.cursorX)
+        {
+          sg.setEndRes(seqCanvas.cursorX);
+        }
+
+        min = seqCanvas.cursorY;
+      }
+      else
+      {
+        sg.setEndRes(seqCanvas.cursorX);
+        if (sg.getStartRes() > seqCanvas.cursorX)
+        {
+          sg.setStartRes(seqCanvas.cursorX);
+        }
+
+        max = seqCanvas.cursorY + 1;
+      }
+
+      if (min > max)
+      {
+        // Only the user can do this
+        av.setSelectionGroup(null);
+      }
+      else
+      {
+        // Now add any sequences between min and max
+        sg.getSequences(null).clear();
+        for (int i = min; i < max; i++)
+        {
+          sg.addSequence(av.alignment.getSequenceAt(i), false);
+        }
+      }
+    }
+
+    if (av.getSelectionGroup() == null)
+    {
+      SequenceGroup sg = new SequenceGroup();
+      sg.setStartRes(seqCanvas.cursorX);
+      sg.setEndRes(seqCanvas.cursorX);
+      sg.addSequence(sequence, false);
+      av.setSelectionGroup(sg);
+    }
+
+    ap.paintAlignment(false);
+  }
+
+  void insertGapAtCursor(boolean group)
+  {
+    groupEditing = group;
+    startseq = seqCanvas.cursorY;
+    lastres = seqCanvas.cursorX;
+    editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
+    endEditing();
+  }
+
+  void deleteGapAtCursor(boolean group)
+  {
+    groupEditing = group;
+    startseq = seqCanvas.cursorY;
+    lastres = seqCanvas.cursorX + getKeyboardNo1();
+    editSequence(false, seqCanvas.cursorX);
+    endEditing();
+  }
+
+  void numberPressed(char value)
+  {
+    if (keyboardNo1 == null)
+    {
+      keyboardNo1 = new StringBuffer();
+    }
+
+    if (keyboardNo2 != null)
+    {
+      keyboardNo2.append(value);
+    }
+    else
+    {
+      keyboardNo1.append(value);
+    }
+  }
+
+  int getKeyboardNo1()
+  {
+    if (keyboardNo1 == null)
+      return 1;
+    else
+    {
+      int value = Integer.parseInt(keyboardNo1.toString());
+      keyboardNo1 = null;
+      return value;
+    }
+  }
+
+  int getKeyboardNo2()
+  {
+    if (keyboardNo2 == null)
+      return 1;
+    else
+    {
+      int value = Integer.parseInt(keyboardNo2.toString());
+      keyboardNo2 = null;
+      return value;
+    }
+  }
+
+  /**
+   * DOCUMENT ME!
+   *
+   * @param evt DOCUMENT ME!
+   */
+  public void mouseReleased(MouseEvent evt)
+  {
+    mouseDragging = false;
+    mouseWheelPressed = false;
+
+    if (!editingSeqs)
+    {
+      doMouseReleasedDefineMode(evt);
+      return;
+    }
+
+    endEditing();
+  }
+
+  /**
+   * DOCUMENT ME!
+   *
+   * @param evt DOCUMENT ME!
+   */
+  public void mousePressed(MouseEvent evt)
+  {
+    lastMousePress = evt.getPoint();
+
+    if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
+    {
+      mouseWheelPressed = true;
+      return;
+    }
+
+    if (evt.isShiftDown() || evt.isAltDown() ||
+        evt.isControlDown())
+    {
+      if (evt.isAltDown() || evt.isControlDown())
+      {
+        groupEditing = true;
+      }
+      editingSeqs = true;
+    }
+    else
+    {
+      doMousePressedDefineMode(evt);
+      return;
+    }
+
+    int seq = findSeq(evt);
+    int res = findRes(evt);
+
+    if (seq < 0 || res < 0)
+    {
+      return;
+    }
+
+    if ( (seq < av.getAlignment().getHeight()) &&
+        (res < av.getAlignment().getSequenceAt(seq).getLength()))
+    {
+      startseq = seq;
+      lastres = res;
+    }
+    else
+    {
+      startseq = -1;
+      lastres = -1;
+    }
+
+    return;
+  }
+
+  String lastMessage;
+  public void mouseOverSequence(SequenceI sequence, int index)
+  {
+    String tmp = sequence.hashCode()+" "+index+"";
+    
+    if (lastMessage == null || !lastMessage.equals(tmp))
+    {
+      // System.err.println("mouseOver Sequence: "+tmp);
+      ssm.mouseOverSequence(sequence, index);
+    }
+    lastMessage = tmp;
+  }
+
+
+  public void highlightSequence(SearchResults results)
+  {
+      seqCanvas.highlightSearchResults(results);
+  }
+
+  public void updateColours(SequenceI seq, int index)
+  {
+    System.out.println("update the seqPanel colours");
+    //repaint();
+  }
+
+  /**
+   * DOCUMENT ME!
+   *
+   * @param evt DOCUMENT ME!
+   */
+  public void mouseMoved(MouseEvent evt)
+  {
+    if (editingSeqs)
+    {
+      // This is because MacOSX creates a mouseMoved
+      // If control is down, other platforms will not.
+      mouseDragged(evt);
+    }
+
+    int res = findRes(evt);
+    int seq = findSeq(evt);
+    int pos;
+    if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
+    {
+      return;
+    }
+
+    SequenceI sequence = av.getAlignment().getSequenceAt(seq);
+
+    if (res >= sequence.getLength())
+    {
+      return;
+    }
+
+    pos = setStatusMessage(sequence, res, seq);
+    if (ssm != null && pos>-1)
+      mouseOverSequence(sequence, pos);
+
+
+    tooltipText.setLength(6); // Cuts the buffer back to <html>
+
+    SequenceGroup[] groups = av.alignment.findAllGroups(sequence);
+    if (groups != null)
+    {
+      for (int g = 0; g < groups.length; g++)
+      {
+        if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
+        {
+          if (tooltipText.length() > 6)
+          {
+            tooltipText.append("<br>");
+          }
+
+          if (!groups[g].getName().startsWith("JTreeGroup") &&
+              !groups[g].getName().startsWith("JGroup"))
+          {
+            tooltipText.append(groups[g].getName());
+          }
+
+          if (groups[g].getDescription() != null)
+          {
+            tooltipText.append(": " + groups[g].getDescription());
+          }
+        }
+      }
+    }
+
+    // use aa to see if the mouse pointer is on a
+    if (av.showSequenceFeatures)
+    {
+      SequenceFeature[] features = findFeaturesAtRes(
+          sequence.getDatasetSequence(), sequence.findPosition(res));
+
+      if (features != null)
+      {
+        for (int i = 0; i < features.length; i++)
+        {
+          if (features[i].getType().equals("disulfide bond"))
+          {
+            if (features[i].getBegin() == sequence.findPosition(res)
+                || features[i].getEnd() == sequence.findPosition(res))
+            {
+              if (tooltipText.length() > 6)
+              {
+                tooltipText.append("<br>");
+              }
+              tooltipText.append("disulfide bond " + features[i].getBegin() +
+                                 ":" +
+                                 features[i].getEnd());
+              if (features[i].links != null)
+              {
+                tooltipText.append(" <img src=\"" + linkImageURL + "\">");
+              }
+            }
+          }
+          else
+          {
+            if (tooltipText.length() > 6)
+            {
+              tooltipText.append("<br>");
+            }
+
+            tooltipText.append(features[i].getType() + " " +
+                               features[i].begin);
+            if (features[i].begin != features[i].end)
+            {
+              tooltipText.append(" " + features[i].end);
+            }
+
+            if (features[i].getDescription() != null
+                && !features[i].description.equals(features[i].getType()))
+            {
+              tmpString = features[i].getDescription();
+              int startTag = tmpString.toUpperCase().indexOf("<HTML>");
+              if (startTag > -1)
+              {
+                tmpString = tmpString.substring(startTag + 6);
+              }
+              int endTag = tmpString.toUpperCase().indexOf("</BODY>");
+              if (endTag > -1)
+              {
+                tmpString = tmpString.substring(0, endTag);
+              }
+              endTag = tmpString.toUpperCase().indexOf("</HTML>");
+              if (endTag > -1)
+              {
+                tmpString = tmpString.substring(0, endTag);
+              }
+
+              if (startTag > -1)
+              {
+                tooltipText.append("; " + tmpString);
+              }
+              else
+              {
+                if (tmpString.indexOf("<") > -1 || tmpString.indexOf(">") > -1)
+                {
+                  //The description does not specify html is to
+                  //be used, so we must remove < > symbols
+                  tmpString = tmpString.replaceAll("<", "&lt;");
+                  tmpString = tmpString.replaceAll(">", "&gt;");
+
+                  tooltipText.append("; ");
+                  tooltipText.append(tmpString);
+
+                }
+                else
+                {
+                  tooltipText.append("; " + tmpString);
+                }
+              }
+            }
+            if (features[i].getValue("status") != null)
+            {
+              String status = features[i].getValue("status").toString();
+              if (status.length() > 0)
+              {
+                tooltipText.append("; (" + features[i].getValue("status") + ")");
+              }
+            }
+
+            if (features[i].links != null)
+            {
+              tooltipText.append(" <img src=\"" + linkImageURL + "\">");
+            }
+
+          }
+        }
+      }
+    }
+
+    if (tooltipText.length() == 6) // <html></html>
+    {
+      setToolTipText(null);
+    }
+    else
+    {
+      tooltipText.append("</html>");
+      if (lastTooltip == null || !lastTooltip.equals(tooltipText.toString()))
+        setToolTipText(tooltipText.toString());
+
+      lastTooltip = tooltipText.toString();
+    }
+
+  }
+  String lastTooltip;
+
+  /**
+   * Set status message in alignment panel
+   * @param sequence aligned sequence object
+   * @param res alignment column
+   * @param seq index of sequence in alignment
+   * @return position of res in sequence
+   */
+  int setStatusMessage(SequenceI sequence, int res, int seq)
+  {
+    int pos=-1;
+    StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: " +
+                                         sequence.getName());
+
+    Object obj = null;
+    if (av.alignment.isNucleotide())
+    {
+      obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res) +
+                                                 "");
+      if (obj != null)
+      {
+        text.append(" Nucleotide: ");
+      }
+    }
+    else
+    {
+      obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
+      if (obj != null)
+      {
+        text.append("  Residue: ");
+      }
+    }
+
+    if (obj != null)
+    {
+      pos=sequence.findPosition(res);
+      if (obj != "")
+      {
+        text.append(obj + " (" + pos +
+                    ")");
+      }
+    }
+    ap.alignFrame.statusBar.setText(text.toString());
+    return pos;
+  }
+
+  /**
+   * DOCUMENT ME!
+   *
+   * @param evt DOCUMENT ME!
+   */
+  public void mouseDragged(MouseEvent evt)
+  {
+    if (mouseWheelPressed)
+    {
+      int oldWidth = av.charWidth;
+
+      //Which is bigger, left-right or up-down?
+      if (Math.abs(evt.getY() - lastMousePress.getY())
+          > Math.abs(evt.getX() - lastMousePress.getX()))
+      {
+        int fontSize = av.font.getSize();
+
+        if (evt.getY() < lastMousePress.getY())
+        {
+          fontSize--;
+        }
+        else if (evt.getY() > lastMousePress.getY())
+        {
+          fontSize++;
+        }
+
+        if (fontSize < 1)
+        {
+          fontSize = 1;
+        }
+
+        av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
+        av.charWidth = oldWidth;
+        ap.fontChanged();
+      }
+      else
+      {
+        if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
+        {
+          av.charWidth--;
+        }
+        else if (evt.getX() > lastMousePress.getX())
+        {
+          av.charWidth++;
+        }
+
+        ap.paintAlignment(false);
+      }
+
+      FontMetrics fm = getFontMetrics(av.getFont());
+      av.validCharWidth = fm.charWidth('M') <= av.charWidth;
+
+      lastMousePress = evt.getPoint();
+
+      return;
+    }
+
+    if (!editingSeqs)
+    {
+      doMouseDraggedDefineMode(evt);
+      return;
+    }
+
+    int res = findRes(evt);
+
+    if (res < 0)
+    {
+      res = 0;
+    }
+
+    if ( (lastres == -1) || (lastres == res))
+    {
+      return;
+    }
+
+    if ( (res < av.getAlignment().getWidth()) && (res < lastres))
+    {
+      // dragLeft, delete gap
+      editSequence(false, res);
+    }
+    else
+    {
+      editSequence(true, res);
+    }
+
+    mouseDragging = true;
+    if (scrollThread != null)
+    {
+      scrollThread.setEvent(evt);
+    }
+  }
+
+
+
+  synchronized void editSequence(boolean insertGap, int startres)
+  {
+    int fixedLeft = -1;
+    int fixedRight = -1;
+    boolean fixedColumns = false;
+    SequenceGroup sg = av.getSelectionGroup();
+
+    SequenceI seq = av.alignment.getSequenceAt(startseq);
+
+    //No group, but the sequence may represent a group
+    if (!groupEditing && av.hasHiddenRows)
+    {
+      if (av.hiddenRepSequences != null
+          && av.hiddenRepSequences.containsKey(seq))
+      {
+        sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
+        groupEditing = true;
+      }
+    }
+
+    StringBuffer message = new StringBuffer();
+    if (groupEditing)
+    {
+      message.append("Edit group:");
+      if (editCommand == null)
+      {
+        editCommand = new EditCommand("Edit Group");
+      }
+    }
+    else
+    {
+      message.append("Edit sequence: " + seq.getName());
+      String label = seq.getName();
+      if (label.length() > 10)
+      {
+        label = label.substring(0, 10);
+      }
+      if (editCommand == null)
+      {
+        editCommand = new EditCommand("Edit " + label);
+      }
+    }
+
+    if (insertGap)
+    {
+      message.append(" insert ");
+    }
+    else
+    {
+      message.append(" delete ");
+    }
+
+    message.append(Math.abs(startres - lastres) + " gaps.");
+    ap.alignFrame.statusBar.setText(message.toString());
+
+    //Are we editing within a selection group?
+    if (groupEditing
+        || (sg != null && sg.getSequences(av.hiddenRepSequences).contains(seq)))
+    {
+      fixedColumns = true;
+
+      //sg might be null as the user may only see 1 sequence,
+      //but the sequence represents a group
+      if (sg == null)
+      {
+        if (av.hiddenRepSequences == null
+            || !av.hiddenRepSequences.containsKey(seq))
+        {
+          endEditing();
+          return;
+        }
+        sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
+      }
+
+      fixedLeft = sg.getStartRes();
+      fixedRight = sg.getEndRes();
+
+      if ( (startres < fixedLeft && lastres >= fixedLeft)
+          || (startres >= fixedLeft && lastres < fixedLeft)
+          || (startres > fixedRight && lastres <= fixedRight)
+          || (startres <= fixedRight && lastres > fixedRight))
+      {
+        endEditing();
+        return;
+      }
+
+      if (fixedLeft > startres)
+      {
+        fixedRight = fixedLeft - 1;
+        fixedLeft = 0;
+      }
+      else if (fixedRight < startres)
+      {
+        fixedLeft = fixedRight;
+        fixedRight = -1;
+      }
+    }
+
+    if (av.hasHiddenColumns)
+    {
+      fixedColumns = true;
+      int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
+      int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
+
+      if ( (insertGap && startres > y1 && lastres < y1)
+          || (!insertGap && startres < y2 && lastres > y2))
+      {
+        endEditing();
+        return;
+      }
+
+      //System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
+      //Selection spans a hidden region
+      if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
+      {
+        if (startres >= y2)
+        {
+          fixedLeft = y2;
+        }
+        else
+        {
+          fixedRight = y2 - 1;
+        }
+      }
+    }
+
+    if (groupEditing)
+    {
+      Vector vseqs = sg.getSequences(av.hiddenRepSequences);
+      int g, groupSize = vseqs.size();
+      SequenceI[] groupSeqs = new SequenceI[groupSize];
+      for (g = 0; g < groupSeqs.length; g++)
+      {
+        groupSeqs[g] = (SequenceI) vseqs.elementAt(g);
+      }
+
+      // drag to right
+      if (insertGap)
+      {
+        //If the user has selected the whole sequence, and is dragging to
+        // the right, we can still extend the alignment and selectionGroup
+        if (sg.getStartRes() == 0
+            && sg.getEndRes() == fixedRight
+            && sg.getEndRes() == av.alignment.getWidth() - 1
+            )
+        {
+          sg.setEndRes(av.alignment.getWidth() + startres - lastres);
+          fixedRight = sg.getEndRes();
+        }
+
+        // Is it valid with fixed columns??
+        // Find the next gap before the end
+        // of the visible region boundary
+        boolean blank = false;
+        for (fixedRight = fixedRight;
+             fixedRight > lastres;
+             fixedRight--)
+        {
+          blank = true;
+
+          for (g = 0; g < groupSize; g++)
+          {
+            for (int j = 0; j < startres - lastres; j++)
+            {
+              if (!jalview.util.Comparison.isGap(
+                  groupSeqs[g].getCharAt(fixedRight - j)))
+              {
+                blank = false;
+                break;
+              }
+            }
+          }
+          if (blank)
+          {
+            break;
+          }
+        }
+
+        if (!blank)
+        {
+          if (sg.getSize() == av.alignment.getHeight())
+          {
+            if ( (av.hasHiddenColumns
+                  &&
+                  startres < av.getColumnSelection().getHiddenBoundaryRight(startres)))
+            {
+              endEditing();
+              return;
+            }
+
+            int alWidth = av.alignment.getWidth();
+            if (av.hasHiddenRows)
+            {
+              int hwidth = av.alignment.getHiddenSequences().getWidth();
+              if (hwidth > alWidth)
+              {
+                alWidth = hwidth;
+              }
+            }
+            //We can still insert gaps if the selectionGroup
+            //contains all the sequences
+            sg.setEndRes(sg.getEndRes() + startres - lastres);
+            fixedRight = alWidth + startres - lastres;
+          }
+          else
+          {
+            endEditing();
+            return;
+          }
+        }
+      }
+
+      // drag to left
+      else if (!insertGap)
+      {
+        /// Are we able to delete?
+        // ie are all columns blank?
+
+        for (g = 0; g < groupSize; g++)
+        {
+          for (int j = startres; j < lastres; j++)
+          {
+            if (groupSeqs[g].getLength() <= j)
+            {
+              continue;
+            }
+
+            if (!jalview.util.Comparison.isGap(
+                groupSeqs[g].getCharAt(j)))
+            {
+              // Not a gap, block edit not valid
+              endEditing();
+              return;
+            }
+          }
+        }
+      }
+
+      if (insertGap)
+      {
+        // dragging to the right
+        if (fixedColumns && fixedRight != -1)
+        {
+          for (int j = lastres; j < startres; j++)
+          {
+            insertChar(j, groupSeqs, fixedRight);
+          }
+        }
+        else
+        {
+          editCommand.appendEdit(EditCommand.INSERT_GAP,
+                                 groupSeqs,
+                                 startres, startres - lastres,
+                                 av.alignment,
+                                 true);
+        }
+      }
+      else
+      {
+        // dragging to the left
+        if (fixedColumns && fixedRight != -1)
+        {
+          for (int j = lastres; j > startres; j--)
+          {
+            deleteChar(startres, groupSeqs, fixedRight);
+          }
+        }
+        else
+        {
+          editCommand.appendEdit(EditCommand.DELETE_GAP,
+                                 groupSeqs,
+                                 startres, lastres - startres,
+                                 av.alignment,
+                                 true);
+        }
+
+      }
+    }
+    else /////Editing a single sequence///////////
+    {
+      if (insertGap)
+      {
+        // dragging to the right
+        if (fixedColumns && fixedRight != -1)
+        {
+          for (int j = lastres; j < startres; j++)
+          {
+            insertChar(j, new SequenceI[]
+                       {seq}, fixedRight);
+          }
+        }
+        else
+        {
+          editCommand.appendEdit(EditCommand.INSERT_GAP,
+                                 new SequenceI[]
+                                 {seq},
+                                 lastres, startres - lastres,
+                                 av.alignment,
+                                 true);
+        }
+      }
+      else
+      {
+        // dragging to the left
+        if (fixedColumns && fixedRight != -1)
+        {
+          for (int j = lastres; j > startres; j--)
+          {
+            if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
+            {
+              endEditing();
+              break;
+            }
+            deleteChar(startres, new SequenceI[]
+                       {seq}, fixedRight);
+          }
+        }
+        else
+        {
+          //could be a keyboard edit trying to delete none gaps
+          int max = 0;
+          for (int m = startres; m < lastres; m++)
+          {
+            if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
+            {
+              break;
+            }
+            max++;
+          }
+
+          if (max > 0)
+          {
+            editCommand.appendEdit(EditCommand.DELETE_GAP,
+                                   new SequenceI[]
+                                   {seq},
+                                   startres, max,
+                                   av.alignment,
+                                   true);
+          }
+        }
+      }
+    }
+
+    lastres = startres;
+    seqCanvas.repaint();
+  }
+
+  void insertChar(int j, SequenceI[] seq, int fixedColumn)
+  {
+    int blankColumn = fixedColumn;
+    for (int s = 0; s < seq.length; s++)
+    {
+      //Find the next gap before the end of the visible region boundary
+      //If lastCol > j, theres a boundary after the gap insertion
+
+      for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
+      {
+        if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
+        {
+          //Theres a space, so break and insert the gap
+          break;
+        }
+      }
+
+      if (blankColumn <= j)
+      {
+        blankColumn = fixedColumn;
+        endEditing();
+        return;
+      }
+    }
+
+    editCommand.appendEdit(EditCommand.DELETE_GAP,
+                           seq,
+                           blankColumn, 1, av.alignment, true);
+
+    editCommand.appendEdit(EditCommand.INSERT_GAP,
+                           seq,
+                           j, 1, av.alignment,
+                           true);
+
+  }
+
+  void deleteChar(int j, SequenceI[] seq, int fixedColumn)
+  {
+
+    editCommand.appendEdit(EditCommand.DELETE_GAP,
+                           seq,
+                           j, 1, av.alignment, true);
+
+    editCommand.appendEdit(EditCommand.INSERT_GAP,
+                           seq,
+                           fixedColumn, 1, av.alignment, true);
+  }
+
+  /**
+   * DOCUMENT ME!
+   *
+   * @param e DOCUMENT ME!
+   */
+  public void mouseEntered(MouseEvent e)
+  {
+    if (oldSeq < 0)
+    {
+      oldSeq = 0;
+    }
+
+    if (scrollThread != null)
+    {
+      scrollThread.running = false;
+      scrollThread = null;
+    }
+  }
+
+  /**
+   * DOCUMENT ME!
+   *
+   * @param e DOCUMENT ME!
+   */
+  public void mouseExited(MouseEvent e)
+  {
+    if (av.getWrapAlignment())
+    {
+      return;
+    }
+
+    if (mouseDragging)
+    {
+      scrollThread = new ScrollThread();
+    }
+  }
+
+  public void mouseClicked(MouseEvent evt)
+  {
+    SequenceI sequence = av.alignment.getSequenceAt(findSeq(evt));
+    if (evt.getClickCount() > 1)
+    {
+      if (av.getSelectionGroup().getSize() == 1
+          && av.getSelectionGroup().getEndRes()
+          - av.getSelectionGroup().getStartRes() < 2)
+      {
+        av.setSelectionGroup(null);
+      }
+
+      SequenceFeature[] features = findFeaturesAtRes(
+          sequence.getDatasetSequence(),
+          sequence.findPosition(findRes(evt))
+          );
+
+      if (features != null && features.length > 0)
+      {
+        SearchResults highlight = new SearchResults();
+        highlight.addResult(sequence,
+                            features[0].getBegin(),
+                            features[0].getEnd());
+        seqCanvas.highlightSearchResults(highlight);
+      }
+      if (features != null && features.length > 0)
+      {
+        seqCanvas.getFeatureRenderer().amendFeatures(
+            new SequenceI[]
+            {sequence}, features, false, ap);
+
+        seqCanvas.highlightSearchResults(null);
+      }
+    }
+  }
+
+  public void mouseWheelMoved(MouseWheelEvent e)
+  {
+    e.consume();
+      if (e.getWheelRotation() > 0)
+      {
+        ap.scrollUp(false);
+      }
+      else
+      {
+        ap.scrollUp(true);
+      }
+  }
+
+  /**
+   * DOCUMENT ME!
+   *
+   * @param evt DOCUMENT ME!
+   */
+  public void doMousePressedDefineMode(MouseEvent evt)
+  {
+    int res = findRes(evt);
+    int seq = findSeq(evt);
+    oldSeq = seq;
+
+    startWrapBlock = wrappedBlock;
+
+    if (av.wrapAlignment && seq > av.alignment.getHeight())
+    {
+      JOptionPane.showInternalMessageDialog(Desktop.desktop,
+                                            "Cannot edit annotations in wrapped view.",
+                                            "Wrapped view - no edit",
+                                            JOptionPane.WARNING_MESSAGE);
+      return;
+    }
+
+    if (seq < 0 || res < 0)
+    {
+      return;
+    }
+
+    SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
+
+    if ( (sequence == null) || (res > sequence.getLength()))
+    {
+      return;
+    }
+
+    stretchGroup = av.getSelectionGroup();
+
+    if (stretchGroup == null)
+    {
+      stretchGroup = av.alignment.findGroup(sequence);
+
+      if ( (stretchGroup != null) && (res > stretchGroup.getStartRes()) &&
+          (res < stretchGroup.getEndRes()))
+      {
+        av.setSelectionGroup(stretchGroup);
+      }
+      else
+      {
+        stretchGroup = null;
+      }
+    }
+    else if (!stretchGroup.getSequences(null).contains(sequence) ||
+             (stretchGroup.getStartRes() > res) ||
+             (stretchGroup.getEndRes() < res))
+    {
+      stretchGroup = null;
+
+      SequenceGroup[] allGroups = av.alignment.findAllGroups(sequence);
+
+      if (allGroups != null)
+      {
+        for (int i = 0; i < allGroups.length; i++)
+        {
+          if ( (allGroups[i].getStartRes() <= res) &&
+              (allGroups[i].getEndRes() >= res))
+          {
+            stretchGroup = allGroups[i];
+            break;
+          }
+        }
+      }
+
+      av.setSelectionGroup(stretchGroup);
+
+    }
+
+    if (javax.swing.SwingUtilities.isRightMouseButton(evt))
+    {
+      SequenceFeature [] allFeatures = findFeaturesAtRes(sequence.getDatasetSequence(),
+                                               sequence.findPosition(res));
+      Vector links = new Vector();
+      for (int i = 0; i < allFeatures.length; i++)
+      {
+        if (allFeatures[i].links != null)
+        {
+          for (int j = 0; j < allFeatures[i].links.size(); j++)
+          {
+            links.addElement(allFeatures[i].links.elementAt(j));
+          }
+        }
+      }
+
+      jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
+      pop.show(this, evt.getX(), evt.getY());
+      return;
+    }
+
+    if (av.cursorMode)
+    {
+      seqCanvas.cursorX = findRes(evt);
+      seqCanvas.cursorY = findSeq(evt);
+      seqCanvas.repaint();
+      return;
+    }
+
+    if (stretchGroup == null)
+    {
+      //Only if left mouse button do we want to change group sizes
+
+      // define a new group here
+      SequenceGroup sg = new SequenceGroup();
+      sg.setStartRes(res);
+      sg.setEndRes(res);
+      sg.addSequence(sequence, false);
+      av.setSelectionGroup(sg);
+      stretchGroup = sg;
+
+      if (av.getConservationSelected())
+      {
+        SliderPanel.setConservationSlider(ap,
+                                          av.getGlobalColourScheme(),
+                                          "Background");
+      }
+
+      if (av.getAbovePIDThreshold())
+      {
+        SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
+                                       "Background");
+      }
+      if ( (stretchGroup != null) && (stretchGroup.getEndRes() == res))
+      {
+        // Edit end res position of selected group
+        changeEndRes = true;
+      }
+      else if ( (stretchGroup != null) &&
+               (stretchGroup.getStartRes() == res))
+      {
+        // Edit end res position of selected group
+        changeStartRes = true;
+      }
+      stretchGroup.getWidth();
+    }
+
+    seqCanvas.repaint();
+  }
+
+  /**
+   * DOCUMENT ME!
+   *
+   * @param evt DOCUMENT ME!
+   */
+  public void doMouseReleasedDefineMode(MouseEvent evt)
+  {
+    if (stretchGroup == null)
+    {
+      return;
+    }
+
+    if (stretchGroup.cs != null)
+    {
+      if (stretchGroup.cs instanceof ClustalxColourScheme)
+      {
+        ( (ClustalxColourScheme) stretchGroup.cs).resetClustalX(
+            stretchGroup.getSequences(av.hiddenRepSequences),
+            stretchGroup.getWidth());
+      }
+
+      if (stretchGroup.cs instanceof Blosum62ColourScheme
+          || stretchGroup.cs instanceof PIDColourScheme
+          || stretchGroup.cs.conservationApplied()
+          || stretchGroup.cs.getThreshold() > 0)
+      {
+        stretchGroup.recalcConservation();
+      }
+
+      if (stretchGroup.cs.conservationApplied())
+      {
+        SliderPanel.setConservationSlider(ap, stretchGroup.cs,
+                                          stretchGroup.getName());
+      }
+      else
+      {
+        SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
+                                       stretchGroup.getName());
+      }
+      PaintRefresher.Refresh(this, av.getSequenceSetId());
+      ap.paintAlignment(true);
+    }
+
+    changeEndRes = false;
+    changeStartRes = false;
+    stretchGroup = null;
+
+  }
+
+  /**
+   * DOCUMENT ME!
+   *
+   * @param evt DOCUMENT ME!
+   */
+  public void doMouseDraggedDefineMode(MouseEvent evt)
+  {
+    int res = findRes(evt);
+    int y = findSeq(evt);
+
+    if (wrappedBlock != startWrapBlock)
+    {
+      return;
+    }
+
+    if (stretchGroup == null)
+    {
+      return;
+    }
+
+    if (res >= av.alignment.getWidth())
+    {
+      res = av.alignment.getWidth() - 1;
+    }
+
+    if (stretchGroup.getEndRes() == res)
+    {
+      // Edit end res position of selected group
+      changeEndRes = true;
+    }
+    else if (stretchGroup.getStartRes() == res)
+    {
+      // Edit start res position of selected group
+      changeStartRes = true;
+    }
+
+    if (res < av.getStartRes())
+    {
+      res = av.getStartRes();
+    }
+
+    if (changeEndRes)
+    {
+      if (res > (stretchGroup.getStartRes() - 1))
+      {
+        stretchGroup.setEndRes(res);
+      }
+    }
+    else if (changeStartRes)
+    {
+      if (res < (stretchGroup.getEndRes() + 1))
+      {
+        stretchGroup.setStartRes(res);
+      }
+    }
+
+    int dragDirection = 0;
+
+    if (y > oldSeq)
+    {
+      dragDirection = 1;
+    }
+    else if (y < oldSeq)
+    {
+      dragDirection = -1;
+    }
+
+    while ( (y != oldSeq) && (oldSeq > -1) && (y < av.alignment.getHeight()))
+    {
+      // This routine ensures we don't skip any sequences, as the
+      // selection is quite slow.
+      Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
+
+      oldSeq += dragDirection;
+
+      if (oldSeq < 0)
+      {
+        break;
+      }
+
+      Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
+
+      if (stretchGroup.getSequences(null).contains(nextSeq))
+      {
+        stretchGroup.deleteSequence(seq, false);
+      }
+      else
+      {
+        if (seq != null)
+        {
+          stretchGroup.addSequence(seq, false);
+        }
+
+        stretchGroup.addSequence(nextSeq, false);
+      }
+    }
+
+    if (oldSeq < 0)
+    {
+      oldSeq = -1;
+    }
+
+    mouseDragging = true;
+
+    if (scrollThread != null)
+    {
+      scrollThread.setEvent(evt);
+    }
+
+    seqCanvas.repaint();
+  }
+
+  void scrollCanvas(MouseEvent evt)
+  {
+    if (evt == null)
+    {
+      if (scrollThread != null)
+      {
+        scrollThread.running = false;
+        scrollThread = null;
+      }
+      mouseDragging = false;
+    }
+    else
+    {
+      if (scrollThread == null)
+      {
+        scrollThread = new ScrollThread();
+      }
+
+      mouseDragging = true;
+      scrollThread.setEvent(evt);
+    }
+
+  }
+
+  // this class allows scrolling off the bottom of the visible alignment
+  class ScrollThread
+      extends Thread
+  {
+    MouseEvent evt;
+    boolean running = false;
+
+    public ScrollThread()
+    {
+      start();
+    }
+
+    public void setEvent(MouseEvent e)
+    {
+      evt = e;
+    }
+
+    public void stopScrolling()
+    {
+      running = false;
+    }
+
+    public void run()
+    {
+      running = true;
+
+      while (running)
+      {
+        if (evt != null)
+        {
+          if (mouseDragging && (evt.getY() < 0) &&
+              (av.getStartSeq() > 0))
+          {
+            running = ap.scrollUp(true);
+          }
+
+          if (mouseDragging && (evt.getY() >= getHeight()) &&
+              (av.alignment.getHeight() > av.getEndSeq()))
+          {
+            running = ap.scrollUp(false);
+          }
+
+          if (mouseDragging && (evt.getX() < 0))
+          {
+            running = ap.scrollRight(false);
+          }
+          else if (mouseDragging && (evt.getX() >= getWidth()))
+          {
+            running = ap.scrollRight(true);
+          }
+        }
+
+        try
+        {
+          Thread.sleep(20);
+        }
+        catch (Exception ex)
+        {
+        }
+      }
+    }
+  }
+}