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