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