f121d1158100d76f4ff2abdbf47fc5fe1f8decc1
[jalview.git] / src / jalview / appletgui / SeqPanel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.appletgui;
19
20 import java.util.*;
21
22 import java.awt.*;
23 import java.awt.event.*;
24
25 import jalview.commands.*;
26 import jalview.datamodel.*;
27 import jalview.schemes.*;
28 import jalview.structure.SelectionSource;
29 import jalview.structure.SequenceListener;
30 import jalview.structure.StructureSelectionManager;
31
32 public class SeqPanel extends Panel implements MouseMotionListener,
33         MouseListener, SequenceListener
34 {
35
36   public SeqCanvas seqCanvas;
37
38   public AlignmentPanel ap;
39
40   protected int lastres;
41
42   protected int startseq;
43
44   protected AlignViewport av;
45
46   // if character is inserted or deleted, we will need to recalculate the
47   // conservation
48   boolean seqEditOccurred = false;
49
50   ScrollThread scrollThread = null;
51
52   boolean mouseDragging = false;
53
54   boolean editingSeqs = false;
55
56   boolean groupEditing = false;
57
58   int oldSeq = -1;
59
60   boolean changeEndSeq = false;
61
62   boolean changeStartSeq = false;
63
64   boolean changeEndRes = false;
65
66   boolean changeStartRes = false;
67
68   SequenceGroup stretchGroup = null;
69
70   StringBuffer keyboardNo1;
71
72   StringBuffer keyboardNo2;
73
74   boolean mouseWheelPressed = false;
75
76   Point lastMousePress;
77
78   EditCommand editCommand;
79
80   StructureSelectionManager ssm;
81
82   public SeqPanel(AlignViewport avp, AlignmentPanel p)
83   {
84     this.av = avp;
85
86     seqCanvas = new SeqCanvas(avp);
87     setLayout(new BorderLayout());
88     add(seqCanvas);
89
90     ap = p;
91
92     seqCanvas.addMouseMotionListener(this);
93     seqCanvas.addMouseListener(this);
94     ssm = StructureSelectionManager.getStructureSelectionManager(av.applet);
95     ssm.addStructureViewerListener(this);
96
97     seqCanvas.repaint();
98   }
99
100   void endEditing()
101   {
102     if (editCommand != null && editCommand.getSize() > 0)
103     {
104       ap.alignFrame.addHistoryItem(editCommand);
105       av.firePropertyChange("alignment", null, av.getAlignment()
106               .getSequences());
107     }
108
109     startseq = -1;
110     lastres = -1;
111     editingSeqs = false;
112     groupEditing = false;
113     keyboardNo1 = null;
114     keyboardNo2 = null;
115     editCommand = null;
116   }
117
118   void setCursorRow()
119   {
120     seqCanvas.cursorY = getKeyboardNo1() - 1;
121     scrollToVisible();
122   }
123
124   void setCursorColumn()
125   {
126     seqCanvas.cursorX = getKeyboardNo1() - 1;
127     scrollToVisible();
128   }
129
130   void setCursorRowAndColumn()
131   {
132     if (keyboardNo2 == null)
133     {
134       keyboardNo2 = new StringBuffer();
135     }
136     else
137     {
138       seqCanvas.cursorX = getKeyboardNo1() - 1;
139       seqCanvas.cursorY = getKeyboardNo2() - 1;
140       scrollToVisible();
141     }
142   }
143
144   void setCursorPosition()
145   {
146     SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
147             seqCanvas.cursorY);
148
149     seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1() - 1);
150     scrollToVisible();
151   }
152
153   void moveCursor(int dx, int dy)
154   {
155     seqCanvas.cursorX += dx;
156     seqCanvas.cursorY += dy;
157     if (av.hasHiddenColumns() && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
158     {
159       int original = seqCanvas.cursorX - dx;
160       int maxWidth = av.getAlignment().getWidth();
161
162       while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
163               && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
164       {
165         seqCanvas.cursorX += dx;
166       }
167
168       if (seqCanvas.cursorX >= maxWidth
169               || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
170       {
171         seqCanvas.cursorX = original;
172       }
173     }
174     scrollToVisible();
175   }
176
177   void scrollToVisible()
178   {
179     if (seqCanvas.cursorX < 0)
180     {
181       seqCanvas.cursorX = 0;
182     }
183     else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
184     {
185       seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
186     }
187
188     if (seqCanvas.cursorY < 0)
189     {
190       seqCanvas.cursorY = 0;
191     }
192     else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
193     {
194       seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
195     }
196
197     endEditing();
198     if (av.wrapAlignment)
199     {
200       ap.scrollToWrappedVisible(seqCanvas.cursorX);
201     }
202     else
203     {
204       while (seqCanvas.cursorY < av.startSeq)
205       {
206         ap.scrollUp(true);
207       }
208       while (seqCanvas.cursorY + 1 > av.endSeq)
209       {
210         ap.scrollUp(false);
211       }
212       while (seqCanvas.cursorX < av.getColumnSelection()
213               .adjustForHiddenColumns(av.startRes))
214       {
215
216         if (!ap.scrollRight(false))
217         {
218           break;
219         }
220       }
221       while (seqCanvas.cursorX > av.getColumnSelection()
222               .adjustForHiddenColumns(av.endRes))
223       {
224         if (!ap.scrollRight(true))
225         {
226           break;
227         }
228       }
229     }
230     setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
231             seqCanvas.cursorX, seqCanvas.cursorY);
232
233     seqCanvas.repaint();
234   }
235
236   void setSelectionAreaAtCursor(boolean topLeft)
237   {
238     SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
239             seqCanvas.cursorY);
240
241     if (av.getSelectionGroup() != null)
242     {
243       SequenceGroup sg = av.getSelectionGroup();
244       // Find the top and bottom of this group
245       int min = av.getAlignment().getHeight(), max = 0;
246       for (int i = 0; i < sg.getSize(); i++)
247       {
248         int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
249         if (index > max)
250         {
251           max = index;
252         }
253         if (index < min)
254         {
255           min = index;
256         }
257       }
258
259       max++;
260
261       if (topLeft)
262       {
263         sg.setStartRes(seqCanvas.cursorX);
264         if (sg.getEndRes() < seqCanvas.cursorX)
265         {
266           sg.setEndRes(seqCanvas.cursorX);
267         }
268
269         min = seqCanvas.cursorY;
270       }
271       else
272       {
273         sg.setEndRes(seqCanvas.cursorX);
274         if (sg.getStartRes() > seqCanvas.cursorX)
275         {
276           sg.setStartRes(seqCanvas.cursorX);
277         }
278
279         max = seqCanvas.cursorY + 1;
280       }
281
282       if (min > max)
283       {
284         // Only the user can do this
285         av.setSelectionGroup(null);
286       }
287       else
288       {
289         // Now add any sequences between min and max
290         sg.getSequences(null).removeAllElements();
291         for (int i = min; i < max; i++)
292         {
293           sg.addSequence(av.getAlignment().getSequenceAt(i), false);
294         }
295       }
296     }
297
298     if (av.getSelectionGroup() == null)
299     {
300       SequenceGroup sg = new SequenceGroup();
301       sg.setStartRes(seqCanvas.cursorX);
302       sg.setEndRes(seqCanvas.cursorX);
303       sg.addSequence(sequence, false);
304       av.setSelectionGroup(sg);
305     }
306     ap.paintAlignment(false);
307     av.sendSelection();
308   }
309
310   void insertGapAtCursor(boolean group)
311   {
312     groupEditing = group;
313     startseq = seqCanvas.cursorY;
314     lastres = seqCanvas.cursorX;
315     editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
316     endEditing();
317   }
318
319   void deleteGapAtCursor(boolean group)
320   {
321     groupEditing = group;
322     startseq = seqCanvas.cursorY;
323     lastres = seqCanvas.cursorX + getKeyboardNo1();
324     editSequence(false, seqCanvas.cursorX);
325     endEditing();
326   }
327
328   void numberPressed(char value)
329   {
330     if (keyboardNo1 == null)
331     {
332       keyboardNo1 = new StringBuffer();
333     }
334
335     if (keyboardNo2 != null)
336     {
337       keyboardNo2.append(value);
338     }
339     else
340     {
341       keyboardNo1.append(value);
342     }
343   }
344
345   int getKeyboardNo1()
346   {
347     if (keyboardNo1 == null)
348       return 1;
349     else
350     {
351       int value = Integer.parseInt(keyboardNo1.toString());
352       keyboardNo1 = null;
353       return value;
354     }
355   }
356
357   int getKeyboardNo2()
358   {
359     if (keyboardNo2 == null)
360       return 1;
361     else
362     {
363       int value = Integer.parseInt(keyboardNo2.toString());
364       keyboardNo2 = null;
365       return value;
366     }
367   }
368
369   void setStatusMessage(SequenceI sequence, int res, int seq)
370   {
371     StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
372             + sequence.getName());
373
374     Object obj = null;
375     if (av.getAlignment().isNucleotide())
376     {
377       obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
378               + "");
379       if (obj != null)
380       {
381         text.append(" Nucleotide: ");
382       }
383     }
384     else
385     {
386       obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
387       if (obj != null)
388       {
389         text.append("  Residue: ");
390       }
391     }
392
393     if (obj != null)
394     {
395
396       if (obj != "")
397       {
398         text.append(obj + " (" + sequence.findPosition(res) + ")");
399       }
400     }
401
402     ap.alignFrame.statusBar.setText(text.toString());
403
404   }
405
406   public void mousePressed(MouseEvent evt)
407   {
408     lastMousePress = evt.getPoint();
409
410     // For now, ignore the mouseWheel font resizing on Macs
411     // As the Button2_mask always seems to be true
412     if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
413             && !av.MAC)
414     {
415       mouseWheelPressed = true;
416       return;
417     }
418
419     if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
420     {
421       if (evt.isControlDown() || evt.isAltDown())
422       {
423         groupEditing = true;
424       }
425       editingSeqs = true;
426     }
427     else
428     {
429       doMousePressedDefineMode(evt);
430       return;
431     }
432
433     int seq = findSeq(evt);
434     int res = findRes(evt);
435
436     if (seq < 0 || res < 0)
437     {
438       return;
439     }
440
441     if ((seq < av.getAlignment().getHeight())
442             && (res < av.getAlignment().getSequenceAt(seq).getLength()))
443     {
444       startseq = seq;
445       lastres = res;
446     }
447     else
448     {
449       startseq = -1;
450       lastres = -1;
451     }
452
453     return;
454   }
455
456   public void mouseClicked(MouseEvent evt)
457   {
458     SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
459     if (evt.getClickCount() > 1)
460     {
461       if (av.getSelectionGroup()!=null && av.getSelectionGroup().getSize() == 1
462               && av.getSelectionGroup().getEndRes()
463                       - av.getSelectionGroup().getStartRes() < 2)
464       {
465         av.setSelectionGroup(null);
466       }
467
468       SequenceFeature[] features = findFeaturesAtRes(sequence,
469               sequence.findPosition(findRes(evt)));
470
471       if (features != null && features.length > 0)
472       {
473         SearchResults highlight = new SearchResults();
474         highlight.addResult(sequence, features[0].getBegin(),
475                 features[0].getEnd());
476         seqCanvas.highlightSearchResults(highlight);
477       }
478       if (features != null && features.length > 0)
479       {
480         seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
481         { sequence }, features, false, ap);
482
483         seqCanvas.highlightSearchResults(null);
484       }
485     }
486   }
487
488   public void mouseReleased(MouseEvent evt)
489   {
490     mouseDragging = false;
491     mouseWheelPressed = false;
492     ap.paintAlignment(true);
493
494     if (!editingSeqs)
495     {
496       doMouseReleasedDefineMode(evt);
497       return;
498     }
499
500     endEditing();
501
502   }
503
504   int startWrapBlock = -1;
505
506   int wrappedBlock = -1;
507
508   int findRes(MouseEvent evt)
509   {
510     int res = 0;
511     int x = evt.getX();
512
513     if (av.wrapAlignment)
514     {
515
516       int hgap = av.charHeight;
517       if (av.scaleAboveWrapped)
518       {
519         hgap += av.charHeight;
520       }
521
522       int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
523               + seqCanvas.getAnnotationHeight();
524
525       int y = evt.getY();
526       y -= hgap;
527       x -= seqCanvas.LABEL_WEST;
528
529       int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
530       if (cwidth < 1)
531       {
532         return 0;
533       }
534
535       wrappedBlock = y / cHeight;
536       wrappedBlock += av.getStartRes() / cwidth;
537
538       res = wrappedBlock * cwidth + x / av.getCharWidth();
539
540     }
541     else
542     {
543       res = (x / av.getCharWidth()) + av.getStartRes();
544     }
545
546     if (av.hasHiddenColumns())
547     {
548       res = av.getColumnSelection().adjustForHiddenColumns(res);
549     }
550
551     return res;
552
553   }
554
555   int findSeq(MouseEvent evt)
556   {
557     final int sqnum = findAlRow(evt);
558     return (sqnum < 0) ? 0 : sqnum;
559   }
560
561   /**
562    * 
563    * @param evt
564    * @return row in alignment that was selected (or -1 for column selection)
565    */
566   private int findAlRow(MouseEvent evt)
567   {
568     int seq = 0;
569     int y = evt.getY();
570
571     if (av.wrapAlignment)
572     {
573       int hgap = av.charHeight;
574       if (av.scaleAboveWrapped)
575       {
576         hgap += av.charHeight;
577       }
578
579       int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
580               + seqCanvas.getAnnotationHeight();
581
582       y -= hgap;
583
584       seq = Math.min((y % cHeight) / av.getCharHeight(),
585               av.getAlignment().getHeight() - 1);
586       if (seq < 0)
587       {
588         seq = -1;
589       }
590     }
591     else
592     {
593       seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(),
594               av.getAlignment().getHeight() - 1);
595       if (seq < 0)
596       {
597         seq = -1;
598       }
599     }
600
601     return seq;
602   }
603
604   public void doMousePressed(MouseEvent evt)
605   {
606
607     int seq = findSeq(evt);
608     int res = findRes(evt);
609
610     if (seq < av.getAlignment().getHeight()
611             && res < av.getAlignment().getSequenceAt(seq).getLength())
612     {
613       // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
614       // Find the residue's position in the sequence (res is the position
615       // in the alignment
616
617       startseq = seq;
618       lastres = res;
619     }
620     else
621     {
622       startseq = -1;
623       lastres = -1;
624     }
625
626     return;
627   }
628
629   String lastMessage;
630
631   public void mouseOverSequence(SequenceI sequence, int index, int pos)
632   {
633     String tmp = sequence.hashCode() + index + "";
634     if (lastMessage == null || !lastMessage.equals(tmp))
635       ssm.mouseOverSequence(sequence, index, pos, av);
636
637     lastMessage = tmp;
638   }
639
640   public void highlightSequence(SearchResults results)
641   {
642     if (av.followHighlight)
643     {
644       if (ap.scrollToPosition(results, true))
645       {
646         ap.alignFrame.repaint();
647       }
648     }
649     seqCanvas.highlightSearchResults(results);
650
651   }
652
653   public void updateColours(SequenceI seq, int index)
654   {
655     System.out.println("update the seqPanel colours");
656     // repaint();
657   }
658
659   public void mouseMoved(MouseEvent evt)
660   {
661     int res = findRes(evt);
662     int seq = findSeq(evt);
663
664     if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
665     {
666       if (tooltip != null)
667       {
668         tooltip.setTip("");
669       }
670       return;
671     }
672
673     SequenceI sequence = av.getAlignment().getSequenceAt(seq);
674     if (res > sequence.getLength())
675     {
676       if (tooltip != null)
677       {
678         tooltip.setTip("");
679       }
680       return;
681     }
682
683     int respos = sequence.findPosition(res);
684     if (ssm != null)
685       mouseOverSequence(sequence, res, respos);
686
687     StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
688             + sequence.getName());
689
690     Object obj = null;
691     if (av.getAlignment().isNucleotide())
692     {
693       obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
694               + "");
695       if (obj != null)
696       {
697         text.append(" Nucleotide: ");
698       }
699     }
700     else
701     {
702       obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
703       if (obj != null)
704       {
705         text.append("  Residue: ");
706       }
707     }
708
709     if (obj != null)
710     {
711       if (obj != "")
712       {
713         text.append(obj + " (" + respos + ")");
714       }
715     }
716
717     ap.alignFrame.statusBar.setText(text.toString());
718
719     StringBuffer tooltipText = new StringBuffer();
720     SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
721     if (groups != null)
722     {
723       for (int g = 0; g < groups.length; g++)
724       {
725         if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
726         {
727           if (!groups[g].getName().startsWith("JTreeGroup")
728                   && !groups[g].getName().startsWith("JGroup"))
729           {
730             tooltipText.append(groups[g].getName() + " ");
731           }
732           if (groups[g].getDescription() != null)
733           {
734             tooltipText.append(groups[g].getDescription());
735           }
736           tooltipText.append("\n");
737         }
738       }
739     }
740
741     // use aa to see if the mouse pointer is on a
742     SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
743             sequence.findPosition(res));
744
745     int index = 0;
746     while (index < allFeatures.length)
747     {
748       SequenceFeature sf = allFeatures[index];
749
750       tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
751
752       if (sf.getDescription() != null)
753       {
754         tooltipText.append(" " + sf.getDescription());
755       }
756
757       if (sf.getValue("status") != null)
758       {
759         String status = sf.getValue("status").toString();
760         if (status.length() > 0)
761         {
762           tooltipText.append(" (" + sf.getValue("status") + ")");
763         }
764       }
765       tooltipText.append("\n");
766
767       index++;
768     }
769
770     if (tooltip == null)
771     {
772       tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
773     }
774     else
775     {
776       tooltip.setTip(tooltipText.toString());
777     }
778   }
779
780   SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
781   {
782     Vector tmp = new Vector();
783     SequenceFeature[] features = sequence.getSequenceFeatures();
784     if (features != null)
785     {
786       for (int i = 0; i < features.length; i++)
787       {
788         if (av.featuresDisplayed == null
789                 || !av.featuresDisplayed.containsKey(features[i].getType()))
790         {
791           continue;
792         }
793
794         if (features[i].featureGroup != null
795                 && seqCanvas.fr.featureGroups != null
796                 && seqCanvas.fr.featureGroups
797                         .containsKey(features[i].featureGroup)
798                 && !((Boolean) seqCanvas.fr.featureGroups
799                         .get(features[i].featureGroup)).booleanValue())
800           continue;
801
802         if ((features[i].getBegin() <= res)
803                 && (features[i].getEnd() >= res))
804         {
805           tmp.addElement(features[i]);
806         }
807       }
808     }
809
810     features = new SequenceFeature[tmp.size()];
811     tmp.copyInto(features);
812
813     return features;
814   }
815
816   Tooltip tooltip;
817
818   public void mouseDragged(MouseEvent evt)
819   {
820     if (mouseWheelPressed)
821     {
822       int oldWidth = av.charWidth;
823
824       // Which is bigger, left-right or up-down?
825       if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
826               - lastMousePress.x))
827       {
828         int fontSize = av.font.getSize();
829
830         if (evt.getY() < lastMousePress.y && av.charHeight > 1)
831         {
832           fontSize--;
833         }
834         else if (evt.getY() > lastMousePress.y)
835         {
836           fontSize++;
837         }
838
839         if (fontSize < 1)
840         {
841           fontSize = 1;
842         }
843
844         av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
845         av.charWidth = oldWidth;
846       }
847       else
848       {
849         if (evt.getX() < lastMousePress.x && av.charWidth > 1)
850         {
851           av.charWidth--;
852         }
853         else if (evt.getX() > lastMousePress.x)
854         {
855           av.charWidth++;
856         }
857
858         if (av.charWidth < 1)
859         {
860           av.charWidth = 1;
861         }
862       }
863
864       ap.fontChanged();
865
866       FontMetrics fm = getFontMetrics(av.getFont());
867       av.validCharWidth = fm.charWidth('M') <= av.charWidth;
868
869       lastMousePress = evt.getPoint();
870
871       ap.paintAlignment(false);
872       ap.annotationPanel.image = null;
873       return;
874     }
875
876     if (!editingSeqs)
877     {
878       doMouseDraggedDefineMode(evt);
879       return;
880     }
881
882     int res = findRes(evt);
883
884     if (res < 0)
885     {
886       res = 0;
887     }
888
889     if ((lastres == -1) || (lastres == res))
890     {
891       return;
892     }
893
894     if ((res < av.getAlignment().getWidth()) && (res < lastres))
895     {
896       // dragLeft, delete gap
897       editSequence(false, res);
898     }
899     else
900     {
901       editSequence(true, res);
902     }
903
904     mouseDragging = true;
905     if (scrollThread != null)
906     {
907       scrollThread.setEvent(evt);
908     }
909
910   }
911
912   synchronized void editSequence(boolean insertGap, int startres)
913   {
914     int fixedLeft = -1;
915     int fixedRight = -1;
916     boolean fixedColumns = false;
917     SequenceGroup sg = av.getSelectionGroup();
918
919     SequenceI seq = av.getAlignment().getSequenceAt(startseq);
920
921     if (!groupEditing && av.hasHiddenRows())
922     {
923       if (av.isHiddenRepSequence(seq))
924       {
925         sg = (SequenceGroup) av.getRepresentedSequences(seq);
926         groupEditing = true;
927       }
928     }
929
930     StringBuffer message = new StringBuffer();
931     if (groupEditing)
932     {
933       message.append("Edit group:");
934       if (editCommand == null)
935       {
936         editCommand = new EditCommand("Edit Group");
937       }
938     }
939     else
940     {
941       message.append("Edit sequence: " + seq.getName());
942       String label = seq.getName();
943       if (label.length() > 10)
944       {
945         label = label.substring(0, 10);
946       }
947       if (editCommand == null)
948       {
949         editCommand = new EditCommand("Edit " + label);
950       }
951     }
952
953     if (insertGap)
954     {
955       message.append(" insert ");
956     }
957     else
958     {
959       message.append(" delete ");
960     }
961
962     message.append(Math.abs(startres - lastres) + " gaps.");
963     ap.alignFrame.statusBar.setText(message.toString());
964
965     // Are we editing within a selection group?
966     if (groupEditing
967             || (sg != null && sg.getSequences(av.getHiddenRepSequences())
968                     .contains(seq)))
969     {
970       fixedColumns = true;
971
972       // sg might be null as the user may only see 1 sequence,
973       // but the sequence represents a group
974       if (sg == null)
975       {
976         if (!av.isHiddenRepSequence(seq))
977         {
978           endEditing();
979           return;
980         }
981
982         sg = av.getRepresentedSequences(seq);
983       }
984
985       fixedLeft = sg.getStartRes();
986       fixedRight = sg.getEndRes();
987
988       if ((startres < fixedLeft && lastres >= fixedLeft)
989               || (startres >= fixedLeft && lastres < fixedLeft)
990               || (startres > fixedRight && lastres <= fixedRight)
991               || (startres <= fixedRight && lastres > fixedRight))
992       {
993         endEditing();
994         return;
995       }
996
997       if (fixedLeft > startres)
998       {
999         fixedRight = fixedLeft - 1;
1000         fixedLeft = 0;
1001       }
1002       else if (fixedRight < startres)
1003       {
1004         fixedLeft = fixedRight;
1005         fixedRight = -1;
1006       }
1007     }
1008
1009     if (av.hasHiddenColumns())
1010     {
1011       fixedColumns = true;
1012       int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1013       int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1014
1015       if ((insertGap && startres > y1 && lastres < y1)
1016               || (!insertGap && startres < y2 && lastres > y2))
1017       {
1018         endEditing();
1019         return;
1020       }
1021
1022       // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1023       // Selection spans a hidden region
1024       if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1025       {
1026         if (startres >= y2)
1027         {
1028           fixedLeft = y2;
1029         }
1030         else
1031         {
1032           fixedRight = y2 - 1;
1033         }
1034       }
1035     }
1036
1037     if (groupEditing)
1038     {
1039       Vector vseqs = sg.getSequences(av.getHiddenRepSequences());
1040       int g, groupSize = vseqs.size();
1041       SequenceI[] groupSeqs = new SequenceI[groupSize];
1042       for (g = 0; g < groupSeqs.length; g++)
1043       {
1044         groupSeqs[g] = (SequenceI) vseqs.elementAt(g);
1045       }
1046
1047       // drag to right
1048       if (insertGap)
1049       {
1050         // If the user has selected the whole sequence, and is dragging to
1051         // the right, we can still extend the alignment and selectionGroup
1052         if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1053                 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1054         {
1055           sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1056           fixedRight = sg.getEndRes();
1057         }
1058
1059         // Is it valid with fixed columns??
1060         // Find the next gap before the end
1061         // of the visible region boundary
1062         boolean blank = false;
1063         for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1064         {
1065           blank = true;
1066
1067           for (g = 0; g < groupSize; g++)
1068           {
1069             for (int j = 0; j < startres - lastres; j++)
1070             {
1071               if (!jalview.util.Comparison.isGap(groupSeqs[g]
1072                       .getCharAt(fixedRight - j)))
1073               {
1074                 blank = false;
1075                 break;
1076               }
1077             }
1078           }
1079           if (blank)
1080           {
1081             break;
1082           }
1083         }
1084
1085         if (!blank)
1086         {
1087           if (sg.getSize() == av.getAlignment().getHeight())
1088           {
1089             if ((av.hasHiddenColumns() && startres < av.getColumnSelection()
1090                     .getHiddenBoundaryRight(startres)))
1091             {
1092               endEditing();
1093               return;
1094             }
1095
1096             int alWidth = av.getAlignment().getWidth();
1097             if (av.hasHiddenRows())
1098             {
1099               int hwidth = av.getAlignment().getHiddenSequences().getWidth();
1100               if (hwidth > alWidth)
1101               {
1102                 alWidth = hwidth;
1103               }
1104             }
1105             // We can still insert gaps if the selectionGroup
1106             // contains all the sequences
1107             sg.setEndRes(sg.getEndRes() + startres - lastres);
1108             fixedRight = alWidth + startres - lastres;
1109           }
1110           else
1111           {
1112             endEditing();
1113             return;
1114           }
1115         }
1116       }
1117
1118       // drag to left
1119       else if (!insertGap)
1120       {
1121         // / Are we able to delete?
1122         // ie are all columns blank?
1123
1124         for (g = 0; g < groupSize; g++)
1125         {
1126           for (int j = startres; j < lastres; j++)
1127           {
1128             if (groupSeqs[g].getLength() <= j)
1129             {
1130               continue;
1131             }
1132
1133             if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1134             {
1135               // Not a gap, block edit not valid
1136               endEditing();
1137               return;
1138             }
1139           }
1140         }
1141       }
1142
1143       if (insertGap)
1144       {
1145         // dragging to the right
1146         if (fixedColumns && fixedRight != -1)
1147         {
1148           for (int j = lastres; j < startres; j++)
1149           {
1150             insertChar(j, groupSeqs, fixedRight);
1151           }
1152         }
1153         else
1154         {
1155           editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1156                   startres, startres - lastres, av.getAlignment(), true);
1157         }
1158       }
1159       else
1160       {
1161         // dragging to the left
1162         if (fixedColumns && fixedRight != -1)
1163         {
1164           for (int j = lastres; j > startres; j--)
1165           {
1166             deleteChar(startres, groupSeqs, fixedRight);
1167           }
1168         }
1169         else
1170         {
1171           editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1172                   startres, lastres - startres, av.getAlignment(), true);
1173         }
1174
1175       }
1176     }
1177     else
1178     // ///Editing a single sequence///////////
1179     {
1180       if (insertGap)
1181       {
1182         // dragging to the right
1183         if (fixedColumns && fixedRight != -1)
1184         {
1185           for (int j = lastres; j < startres; j++)
1186           {
1187             insertChar(j, new SequenceI[]
1188             { seq }, fixedRight);
1189           }
1190         }
1191         else
1192         {
1193           editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1194           { seq }, lastres, startres - lastres, av.getAlignment(), true);
1195         }
1196       }
1197       else
1198       {
1199         // dragging to the left
1200         if (fixedColumns && fixedRight != -1)
1201         {
1202           for (int j = lastres; j > startres; j--)
1203           {
1204             if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1205             {
1206               endEditing();
1207               break;
1208             }
1209             deleteChar(startres, new SequenceI[]
1210             { seq }, fixedRight);
1211           }
1212         }
1213         else
1214         {
1215           // could be a keyboard edit trying to delete none gaps
1216           int max = 0;
1217           for (int m = startres; m < lastres; m++)
1218           {
1219             if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1220             {
1221               break;
1222             }
1223             max++;
1224           }
1225
1226           if (max > 0)
1227           {
1228             editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1229             { seq }, startres, max, av.getAlignment(), true);
1230           }
1231         }
1232       }
1233     }
1234
1235     lastres = startres;
1236     seqCanvas.repaint();
1237   }
1238
1239   void insertChar(int j, SequenceI[] seq, int fixedColumn)
1240   {
1241     int blankColumn = fixedColumn;
1242     for (int s = 0; s < seq.length; s++)
1243     {
1244       // Find the next gap before the end of the visible region boundary
1245       // If lastCol > j, theres a boundary after the gap insertion
1246
1247       for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1248       {
1249         if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1250         {
1251           // Theres a space, so break and insert the gap
1252           break;
1253         }
1254       }
1255
1256       if (blankColumn <= j)
1257       {
1258         blankColumn = fixedColumn;
1259         endEditing();
1260         return;
1261       }
1262     }
1263
1264     editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1265             av.getAlignment(), true);
1266
1267     editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1, av.getAlignment(),
1268             true);
1269
1270   }
1271
1272   void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1273   {
1274
1275     editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1, av.getAlignment(),
1276             true);
1277
1278     editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1279             av.getAlignment(), true);
1280   }
1281
1282   // ////////////////////////////////////////
1283   // ///Everything below this is for defining the boundary of the rubberband
1284   // ////////////////////////////////////////
1285   public void doMousePressedDefineMode(MouseEvent evt)
1286   {
1287     if (scrollThread != null)
1288     {
1289       scrollThread.running = false;
1290       scrollThread = null;
1291     }
1292
1293     int res = findRes(evt);
1294     int seq = findSeq(evt);
1295     oldSeq = seq;
1296     startWrapBlock = wrappedBlock;
1297
1298     if (seq == -1)
1299     {
1300       return;
1301     }
1302
1303     SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1304
1305     if (sequence == null || res > sequence.getLength())
1306     {
1307       return;
1308     }
1309
1310     stretchGroup = av.getSelectionGroup();
1311
1312     if (stretchGroup == null)
1313     {
1314       stretchGroup = av.getAlignment().findGroup(sequence);
1315       if (stretchGroup != null && res > stretchGroup.getStartRes()
1316               && res < stretchGroup.getEndRes())
1317       {
1318         av.setSelectionGroup(stretchGroup);
1319       }
1320       else
1321       {
1322         stretchGroup = null;
1323       }
1324     }
1325
1326     else if (!stretchGroup.getSequences(null).contains(sequence)
1327             || stretchGroup.getStartRes() > res
1328             || stretchGroup.getEndRes() < res)
1329     {
1330       stretchGroup = null;
1331
1332       SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1333
1334       if (allGroups != null)
1335       {
1336         for (int i = 0; i < allGroups.length; i++)
1337         {
1338           if (allGroups[i].getStartRes() <= res
1339                   && allGroups[i].getEndRes() >= res)
1340           {
1341             stretchGroup = allGroups[i];
1342             break;
1343           }
1344         }
1345       }
1346       av.setSelectionGroup(stretchGroup);
1347     }
1348
1349     // DETECT RIGHT MOUSE BUTTON IN AWT
1350     if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1351     {
1352       SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1353               sequence.findPosition(res));
1354
1355       Vector links = null;
1356       if (allFeatures != null)
1357       {
1358         for (int i = 0; i < allFeatures.length; i++)
1359         {
1360           if (allFeatures[i].links != null)
1361           {
1362             if (links == null)
1363             {
1364               links = new Vector();
1365             }
1366             for (int j = 0; j < allFeatures[i].links.size(); j++)
1367             {
1368               links.addElement(allFeatures[i].links.elementAt(j));
1369             }
1370           }
1371         }
1372       }
1373       APopupMenu popup = new APopupMenu(ap, null, links);
1374       this.add(popup);
1375       popup.show(this, evt.getX(), evt.getY());
1376       return;
1377     }
1378
1379     if (av.cursorMode)
1380     {
1381       seqCanvas.cursorX = findRes(evt);
1382       seqCanvas.cursorY = findSeq(evt);
1383       seqCanvas.repaint();
1384       return;
1385     }
1386
1387     // Only if left mouse button do we want to change group sizes
1388
1389     if (stretchGroup == null)
1390     {
1391       // define a new group here
1392       SequenceGroup sg = new SequenceGroup();
1393       sg.setStartRes(res);
1394       sg.setEndRes(res);
1395       sg.addSequence(sequence, false);
1396       av.setSelectionGroup(sg);
1397       stretchGroup = sg;
1398
1399       if (av.getConservationSelected())
1400       {
1401         SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1402                 "Background");
1403       }
1404       if (av.getAbovePIDThreshold())
1405       {
1406         SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1407                 "Background");
1408       }
1409
1410     }
1411   }
1412
1413   public void doMouseReleasedDefineMode(MouseEvent evt)
1414   {
1415     if (stretchGroup == null)
1416     {
1417       return;
1418     }
1419
1420     if (stretchGroup.cs != null)
1421     {
1422       if (stretchGroup.cs instanceof ClustalxColourScheme)
1423       {
1424         ((ClustalxColourScheme) stretchGroup.cs).resetClustalX(
1425                 stretchGroup.getSequences(av.getHiddenRepSequences()),
1426                 stretchGroup.getWidth());
1427       }
1428
1429       if (stretchGroup.cs instanceof Blosum62ColourScheme
1430               || stretchGroup.cs instanceof PIDColourScheme
1431               || stretchGroup.cs.conservationApplied()
1432               || stretchGroup.cs.getThreshold() > 0)
1433       {
1434         stretchGroup.recalcConservation();
1435       }
1436
1437       if (stretchGroup.cs.conservationApplied())
1438       {
1439         SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1440                 stretchGroup.getName());
1441         stretchGroup.recalcConservation();
1442       }
1443       else
1444       {
1445         SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1446                 stretchGroup.getName());
1447       }
1448     }
1449     changeEndRes = false;
1450     changeStartRes = false;
1451     stretchGroup = null;
1452     PaintRefresher.Refresh(ap, av.getSequenceSetId());
1453     ap.paintAlignment(true);
1454     av.sendSelection();
1455   }
1456
1457   public void doMouseDraggedDefineMode(MouseEvent evt)
1458   {
1459     int res = findRes(evt);
1460     int y = findSeq(evt);
1461
1462     if (wrappedBlock != startWrapBlock)
1463     {
1464       return;
1465     }
1466
1467     if (stretchGroup == null)
1468     {
1469       return;
1470     }
1471
1472     mouseDragging = true;
1473
1474     if (y > av.getAlignment().getHeight())
1475     {
1476       y = av.getAlignment().getHeight() - 1;
1477     }
1478
1479     if (res >= av.getAlignment().getWidth())
1480     {
1481       res = av.getAlignment().getWidth() - 1;
1482     }
1483
1484     if (stretchGroup.getEndRes() == res)
1485     {
1486       // Edit end res position of selected group
1487       changeEndRes = true;
1488     }
1489     else if (stretchGroup.getStartRes() == res)
1490     {
1491       // Edit start res position of selected group
1492       changeStartRes = true;
1493     }
1494
1495     if (res < 0)
1496     {
1497       res = 0;
1498     }
1499
1500     if (changeEndRes)
1501     {
1502       if (res > (stretchGroup.getStartRes() - 1))
1503       {
1504         stretchGroup.setEndRes(res);
1505       }
1506     }
1507     else if (changeStartRes)
1508     {
1509       if (res < (stretchGroup.getEndRes() + 1))
1510       {
1511         stretchGroup.setStartRes(res);
1512       }
1513     }
1514
1515     int dragDirection = 0;
1516
1517     if (y > oldSeq)
1518     {
1519       dragDirection = 1;
1520     }
1521     else if (y < oldSeq)
1522     {
1523       dragDirection = -1;
1524     }
1525
1526     while ((y != oldSeq) && (oldSeq > -1) && (y < av.getAlignment().getHeight()))
1527     {
1528       // This routine ensures we don't skip any sequences, as the
1529       // selection is quite slow.
1530       Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1531
1532       oldSeq += dragDirection;
1533
1534       if (oldSeq < 0)
1535       {
1536         break;
1537       }
1538
1539       Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1540
1541       if (stretchGroup.getSequences(null).contains(nextSeq))
1542       {
1543         stretchGroup.deleteSequence(seq, false);
1544       }
1545       else
1546       {
1547         if (seq != null)
1548         {
1549           stretchGroup.addSequence(seq, false);
1550         }
1551
1552         stretchGroup.addSequence(nextSeq, false);
1553       }
1554     }
1555
1556     if (oldSeq < 0)
1557     {
1558       oldSeq = -1;
1559     }
1560
1561     if (res > av.endRes || res < av.startRes || y < av.startSeq
1562             || y > av.endSeq)
1563     {
1564       mouseExited(evt);
1565     }
1566
1567     if (scrollThread != null)
1568     {
1569       scrollThread.setEvent(evt);
1570     }
1571
1572     seqCanvas.repaint();
1573   }
1574
1575   public void mouseEntered(MouseEvent e)
1576   {
1577     if (oldSeq < 0)
1578     {
1579       oldSeq = 0;
1580     }
1581
1582     if (scrollThread != null)
1583     {
1584       scrollThread.running = false;
1585       scrollThread = null;
1586     }
1587   }
1588
1589   public void mouseExited(MouseEvent e)
1590   {
1591     if (av.getWrapAlignment())
1592     {
1593       return;
1594     }
1595
1596     if (mouseDragging && scrollThread == null)
1597     {
1598       scrollThread = new ScrollThread();
1599     }
1600   }
1601
1602   void scrollCanvas(MouseEvent evt)
1603   {
1604     if (evt == null)
1605     {
1606       if (scrollThread != null)
1607       {
1608         scrollThread.running = false;
1609         scrollThread = null;
1610       }
1611       mouseDragging = false;
1612     }
1613     else
1614     {
1615       if (scrollThread == null)
1616       {
1617         scrollThread = new ScrollThread();
1618       }
1619
1620       mouseDragging = true;
1621       scrollThread.setEvent(evt);
1622     }
1623
1624   }
1625
1626   // this class allows scrolling off the bottom of the visible alignment
1627   class ScrollThread extends Thread
1628   {
1629     MouseEvent evt;
1630
1631     boolean running = false;
1632
1633     public ScrollThread()
1634     {
1635       start();
1636     }
1637
1638     public void setEvent(MouseEvent e)
1639     {
1640       evt = e;
1641     }
1642
1643     public void stopScrolling()
1644     {
1645       running = false;
1646     }
1647
1648     public void run()
1649     {
1650       running = true;
1651       while (running)
1652       {
1653
1654         if (evt != null)
1655         {
1656
1657           if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1658           {
1659             running = ap.scrollUp(true);
1660           }
1661
1662           if (mouseDragging && evt.getY() >= getSize().height
1663                   && av.getAlignment().getHeight() > av.getEndSeq())
1664           {
1665             running = ap.scrollUp(false);
1666           }
1667
1668           if (mouseDragging && evt.getX() < 0)
1669           {
1670             running = ap.scrollRight(false);
1671           }
1672
1673           else if (mouseDragging && evt.getX() >= getSize().width)
1674           {
1675             running = ap.scrollRight(true);
1676           }
1677         }
1678
1679         try
1680         {
1681           Thread.sleep(75);
1682         } catch (Exception ex)
1683         {
1684         }
1685       }
1686     }
1687   }
1688
1689   /**
1690    * modify current selection according to a received message.
1691    */
1692   public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1693           SelectionSource source)
1694   {
1695     // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1696     // handles selection messages...
1697     // TODO: extend config options to allow user to control if selections may be
1698     // shared between viewports.
1699     if (av != null
1700             && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignViewport) source)
1701                     .getSequenceSetId().equals(av.getSequenceSetId()))))
1702     {
1703       return;
1704     }
1705     // do we want to thread this ? (contention with seqsel and colsel locks, I
1706     // suspect)
1707     // rules are: colsel is copied if there is a real intersection between
1708     // sequence selection
1709     boolean repaint = false, copycolsel = true;
1710     if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1711     {
1712       SequenceGroup sgroup = null;
1713       if (seqsel != null && seqsel.getSize()>0)
1714       {
1715         if (av.getAlignment() == null)
1716         {
1717           System.out
1718                   .println("Selection message: alignviewport av SeqSetId="
1719                           + av.getSequenceSetId() + " ViewId="
1720                           + av.getViewId()
1721                           + " 's alignment is NULL! returning immediatly.");
1722           return;
1723         }
1724         sgroup = seqsel.intersect(av.getAlignment(),
1725                 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1726         if ((sgroup == null || sgroup.getSize() == 0)
1727                 && (colsel == null || colsel.size() == 0))
1728         {
1729           // don't copy columns if the region didn't intersect.
1730           copycolsel = false;
1731         }
1732       }
1733       if (sgroup != null && sgroup.getSize() > 0)
1734       {
1735         av.setSelectionGroup(sgroup);
1736       }
1737       else
1738       {
1739         av.setSelectionGroup(null);
1740       }
1741       repaint = av.isSelectionGroupChanged(true);
1742     }
1743     if (copycolsel && (av.getColumnSelection() == null || !av.isColSelChanged(true)))
1744     {
1745       // the current selection is unset or from a previous message
1746       // so import the new colsel.
1747       if (colsel == null || colsel.size() == 0)
1748       {
1749         if (av.getColumnSelection() != null)
1750         {
1751           av.getColumnSelection().clear();
1752         }
1753       }
1754       else
1755       {
1756         // TODO: shift colSel according to the intersecting sequences
1757         if (av.getColumnSelection() == null)
1758         {
1759           av.setColumnSelection(new ColumnSelection(colsel));
1760         }
1761         else
1762         {
1763           av.getColumnSelection().setElementsFrom(colsel);
1764         }
1765       }
1766       repaint |= av.isColSelChanged(true);
1767     }
1768     if (copycolsel && av.hasHiddenColumns()
1769             && (av.getColumnSelection() == null || av.getColumnSelection().getHiddenColumns() == null))
1770     {
1771       System.err.println("Bad things");
1772     }
1773     if (repaint)
1774     {
1775       ap.scalePanelHolder.repaint();
1776       ap.repaint();
1777     }
1778   }
1779
1780   /**
1781    * scroll to the given row/column - or nearest visible location
1782    * @param row
1783    * @param column
1784    */
1785   public void scrollTo(int row, int column)
1786   {
1787     
1788     row = row<0 ? ap.av.startSeq : row;
1789     column = column<0 ? ap.av.startRes : column;
1790     ap.scrollTo(row, row, column, true, true);
1791   }
1792   /**
1793    * scroll to the given row - or nearest visible location
1794    * @param row
1795    */
1796   public void scrollToRow(int row)
1797   {
1798     
1799     row = row<0 ? ap.av.startSeq : row;
1800     ap.scrollTo(row, row, ap.av.startRes, true, true);
1801   }
1802   /**
1803    * scroll to the given column - or nearest visible location
1804    * @param column
1805    */
1806   public void scrollToColumn(int column)
1807   {
1808     
1809     column = column<0 ? ap.av.startRes : column;
1810     ap.scrollTo(ap.av.startRes, ap.av.startRes, column, true, true);
1811   }
1812
1813 }