2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\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
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
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
19 package jalview.gui;
\r
21 import jalview.analysis.*;
\r
23 import jalview.datamodel.*;
\r
25 import jalview.schemes.*;
\r
28 import java.awt.event.*;
\r
32 import javax.swing.*;
\r
39 * @version $Revision$
\r
41 public class SeqPanel extends JPanel
\r
43 /** DOCUMENT ME!! */
\r
44 public SeqCanvas seqCanvas;
\r
46 /** DOCUMENT ME!! */
\r
47 public AlignmentPanel ap;
\r
48 protected int lastres;
\r
49 protected int startseq;
\r
52 protected AlignViewport av;
\r
54 // if character is inserted or deleted, we will need to recalculate the conservation
\r
55 int seqEditOccurred = -1;
\r
56 ScrollThread scrollThread = null;
\r
57 boolean mouseDragging = false;
\r
58 boolean editingSeqs = false;
\r
59 boolean groupEditing = false;
\r
61 //////////////////////////////////////////
\r
62 /////Everything below this is for defining the boundary of the rubberband
\r
63 //////////////////////////////////////////
\r
65 boolean changeEndSeq = false;
\r
66 boolean changeStartSeq = false;
\r
67 boolean changeEndRes = false;
\r
68 boolean changeStartRes = false;
\r
69 SequenceGroup stretchGroup = null;
\r
70 boolean remove = false;
\r
73 * Creates a new SeqPanel object.
\r
75 * @param avp DOCUMENT ME!
\r
76 * @param p DOCUMENT ME!
\r
78 public SeqPanel(AlignViewport avp, AlignmentPanel p)
\r
80 ToolTipManager.sharedInstance().registerComponent(this);
\r
81 ToolTipManager.sharedInstance().setInitialDelay(0);
\r
82 ToolTipManager.sharedInstance().setDismissDelay(10000);
\r
84 setBackground(Color.white);
\r
86 seqCanvas = new SeqCanvas(avp);
\r
87 setLayout(new BorderLayout());
\r
88 add(seqCanvas, BorderLayout.CENTER);
\r
92 addMouseMotionListener(new MouseMotionAdapter()
\r
94 public void mouseMoved(MouseEvent evt)
\r
96 if (av.getWrapAlignment())
\r
104 public void mouseDragged(MouseEvent evt)
\r
106 if (av.getWrapAlignment())
\r
113 doMouseDragged(evt);
\r
117 doMouseDraggedDefineMode(evt);
\r
122 addMouseListener(new MouseAdapter()
\r
124 public void mouseReleased(MouseEvent evt)
\r
126 if (av.getWrapAlignment())
\r
133 doMouseReleased(evt);
\r
137 doMouseReleasedDefineMode(evt);
\r
141 public void mousePressed(MouseEvent evt)
\r
143 if (av.getWrapAlignment())
\r
148 if (evt.isShiftDown() || evt.isAltDown() ||
\r
149 evt.isControlDown())
\r
151 if (evt.isAltDown() || evt.isControlDown())
\r
153 groupEditing = true;
\r
156 editingSeqs = true;
\r
157 doMousePressed(evt);
\r
161 doMousePressedDefineMode(evt);
\r
165 public void mouseExited(MouseEvent evt)
\r
167 if (av.getWrapAlignment() || editingSeqs)
\r
172 doMouseExitedDefineMode(evt);
\r
175 public void mouseEntered(MouseEvent evt)
\r
177 if (av.getWrapAlignment() || editingSeqs)
\r
182 doMouseEnteredDefineMode(evt);
\r
190 * @param evt DOCUMENT ME!
\r
192 public void doMouseReleased(MouseEvent evt)
\r
194 if (seqEditOccurred > -1)
\r
196 editOccurred(seqEditOccurred);
\r
201 seqEditOccurred = -1;
\r
202 editingSeqs = false;
\r
203 groupEditing = false;
\r
211 * @param evt DOCUMENT ME!
\r
213 public void doMousePressed(MouseEvent evt)
\r
215 ap.alignFrame.addHistoryItem(new HistoryItem("Edit Sequence",
\r
216 av.alignment, HistoryItem.EDIT));
\r
221 int x = evt.getX();
\r
222 int y = evt.getY();
\r
224 res = (x / av.getCharWidth()) + av.getStartRes();
\r
225 seq = (y / av.getCharHeight()) + av.getStartSeq();
\r
227 if ((seq < av.getAlignment().getHeight()) &&
\r
228 (res < av.getAlignment().getSequenceAt(seq).getLength()))
\r
239 startEdit = lastres;
\r
248 * @param evt DOCUMENT ME!
\r
250 public void doMouseMoved(MouseEvent evt)
\r
254 int x = evt.getX();
\r
255 int y = evt.getY();
\r
257 if (av.wrapAlignment)
\r
259 y -= (2 * av.charHeight);
\r
261 int chunkHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
263 res = (int) ((y / chunkHeight) * (getWidth() / av.charWidth)) +
\r
264 (x / av.getCharWidth()) + av.getStartRes();
\r
267 seq = (y / av.getCharHeight()) + av.getStartSeq();
\r
271 res = (x / av.getCharWidth()) + av.getStartRes();
\r
272 seq = (y / av.getCharHeight()) + av.getStartSeq();
\r
275 if (seq >= av.getAlignment().getHeight())
\r
280 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
\r
282 if (res > sequence.getLength())
\r
287 Object obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) +
\r
293 aa = obj.toString();
\r
296 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: " +
\r
297 sequence.getName());
\r
301 text.append(" Residue: " + aa + " (" +
\r
302 av.getAlignment().getSequenceAt(seq).findPosition(res) + ")");
\r
305 ap.alignFrame.statusBar.setText(text.toString());
\r
307 // use aa to see if the mouse pointer is on a
\r
308 if (av.showSequenceFeatures)
\r
310 Vector features = sequence.getSequenceFeatures();
\r
311 Enumeration e = features.elements();
\r
312 StringBuffer sbuffer = new StringBuffer();
\r
314 while (e.hasMoreElements())
\r
316 SequenceFeature sf = (SequenceFeature) e.nextElement();
\r
318 if ((sf.getStart() <= sequence.findPosition(res)) &&
\r
319 (sf.getEnd() >= sequence.findPosition(res)))
\r
321 if (sbuffer.length() > 0)
\r
323 sbuffer.append("; ");
\r
326 sbuffer.append(sf.getType() + " " + sf.getDescription());
\r
328 if (sf.getStatus().length() > 0)
\r
330 sbuffer.append(" (" + sf.getStatus() + ")");
\r
335 this.setToolTipText(sbuffer.toString());
\r
342 * @param evt DOCUMENT ME!
\r
344 public void doMouseDragged(MouseEvent evt)
\r
346 // If we're dragging we're editing
\r
347 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
\r
354 if ((lastres == -1) || (lastres == res))
\r
359 boolean dragRight = true;
\r
361 if ((res < av.getAlignment().getWidth()) && (res < lastres))
\r
366 if (res != lastres)
\r
371 SequenceGroup sg = av.getSelectionGroup();
\r
383 sg.setEndRes(sg.getEndRes() + (res - lastres));
\r
389 /// Are we able to delete?
\r
390 // ie are all columns blank?
\r
391 boolean deleteAllowed = false;
\r
393 for (int s = 0; s < sg.getSize(); s++)
\r
395 SequenceI seq = sg.getSequenceAt(s);
\r
397 for (int j = res; j < lastres; j++)
\r
399 if (seq.getSequence().length() <= j)
\r
404 if (!jalview.util.Comparison.isGap(
\r
405 seq.getSequence().charAt(j)))
\r
407 // Not a gap, block edit not valid
\r
409 deleteAllowed = false;
\r
414 deleteAllowed = true;
\r
418 if (!deleteAllowed)
\r
425 sg.setEndRes(sg.getEndRes() - (lastres - res));
\r
428 for (int i = 0; i < sg.getSize(); i++)
\r
430 SequenceI s = sg.getSequenceAt(i);
\r
431 int k = av.alignment.findIndex(s);
\r
436 for (int j = lastres; j < res; j++)
\r
445 for (int j = res; j < lastres; j++)
\r
447 if (s.getLength() > j)
\r
449 deleteChar(res, k);
\r
455 else /////Editing a single sequence///////////
\r
457 if ((res < av.getAlignment().getWidth()) && (res > lastres))
\r
459 // dragging to the right
\r
460 for (int j = lastres; j < res; j++)
\r
462 insertChar(j, startseq);
\r
465 else if ((res < av.getAlignment().getWidth()) &&
\r
468 // dragging to the left
\r
469 for (int j = lastres; j > res; j--)
\r
471 if (jalview.util.Comparison.isGap(
\r
472 av.alignment.getSequenceAt(startseq)
\r
473 .getSequence().charAt(res)))
\r
475 deleteChar(res, startseq);
\r
488 seqCanvas.repaint();
\r
494 * @param seqstart DOCUMENT ME!
\r
495 * @param seqend DOCUMENT ME!
\r
496 * @param start DOCUMENT ME!
\r
498 public void drawChars(int seqstart, int seqend, int start)
\r
500 seqCanvas.drawPanel(seqCanvas.gg, start, av.getEndRes(), seqstart,
\r
501 seqend, av.getStartRes(), av.getStartSeq(), 0);
\r
502 seqCanvas.repaint();
\r
508 * @param j DOCUMENT ME!
\r
509 * @param seq DOCUMENT ME!
\r
511 public void insertChar(int j, int seq)
\r
513 av.alignment.getSequenceAt(seq).insertCharAt(j, av.getGapCharacter());
\r
514 seqEditOccurred = seq;
\r
520 * @param j DOCUMENT ME!
\r
521 * @param seq DOCUMENT ME!
\r
523 public void deleteChar(int j, int seq)
\r
525 av.alignment.getSequenceAt(seq).deleteCharAt(j);
\r
526 seqEditOccurred = seq;
\r
528 av.alignment.getWidth();
\r
529 seqCanvas.repaint();
\r
535 * @param i DOCUMENT ME!
\r
537 void editOccurred(int i)
\r
539 if (endEdit == startEdit)
\r
541 ap.alignFrame.historyList.pop();
\r
542 ap.alignFrame.updateEditMenuBar();
\r
545 av.updateConservation();
\r
546 av.updateConsensus();
\r
549 ColourSchemeI cs = av.getGlobalColourScheme();
\r
551 if (av.getSelectionGroup() != null)
\r
553 av.getSelectionGroup().recalcConservation();
\r
556 if (cs instanceof ConservationColourScheme)
\r
558 ConservationColourScheme ccs = (ConservationColourScheme) cs;
\r
559 Conservation c = new Conservation("All",
\r
560 ResidueProperties.propHash, 3, av.alignment.getSequences(),
\r
561 0, av.alignment.getWidth() - 1);
\r
563 c.verdict(false, av.ConsPercGaps);
\r
565 if (ccs.cs instanceof ClustalxColourScheme)
\r
567 ClustalxColourScheme cxs = (ClustalxColourScheme) ccs.cs;
\r
568 cxs.resetClustalX(av.alignment.getSequences(),
\r
569 av.alignment.getWidth());
\r
570 ccs = new ConservationColourScheme(c, cxs);
\r
571 av.setGlobalColourScheme(ccs);
\r
575 ccs = new ConservationColourScheme(c, ccs.cs);
\r
576 av.setGlobalColourScheme(ccs);
\r
580 if (cs instanceof ClustalxColourScheme)
\r
582 ((ClustalxColourScheme) cs).resetClustalX(av.alignment.getSequences(),
\r
583 av.alignment.getWidth());
\r
584 av.setGlobalColourScheme(cs);
\r
591 * @param evt DOCUMENT ME!
\r
593 public void doMousePressedDefineMode(MouseEvent evt)
\r
595 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
\r
596 int seq = (evt.getY() / av.getCharHeight()) + av.getStartSeq();
\r
599 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
\r
601 if ((sequence == null) || (res > sequence.getLength()))
\r
606 stretchGroup = av.getSelectionGroup();
\r
608 if (stretchGroup == null)
\r
610 stretchGroup = av.alignment.findGroup(sequence);
\r
612 if ((stretchGroup != null) && (res > stretchGroup.getStartRes()) &&
\r
613 (res < stretchGroup.getEndRes()))
\r
615 av.setSelectionGroup(stretchGroup);
\r
619 stretchGroup = null;
\r
622 else if (!stretchGroup.sequences.contains(sequence) ||
\r
623 (stretchGroup.getStartRes() > res) ||
\r
624 (stretchGroup.getEndRes() < res))
\r
626 stretchGroup = null;
\r
628 SequenceGroup[] allGroups = av.alignment.findAllGroups(sequence);
\r
630 if (allGroups != null)
\r
632 for (int i = 0; i < allGroups.length; i++)
\r
634 if ((allGroups[i].getStartRes() <= res) &&
\r
635 (allGroups[i].getEndRes() >= res))
\r
637 stretchGroup = allGroups[i];
\r
638 av.setSelectionGroup(stretchGroup);
\r
646 if (stretchGroup == null)
\r
648 // define a new group here
\r
649 SequenceGroup sg = new SequenceGroup();
\r
650 sg.setStartRes(res);
\r
652 sg.addSequence(sequence, false);
\r
653 av.setSelectionGroup(sg);
\r
656 if (av.getConservationSelected())
\r
658 SliderPanel.setConservationSlider(ap,
\r
659 av.getGlobalColourScheme(), "Background");
\r
662 if (av.getAbovePIDThreshold())
\r
664 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
\r
668 else if (javax.swing.SwingUtilities.isRightMouseButton(evt))
\r
670 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null);
\r
671 pop.show(this, evt.getX(), evt.getY());
\r
673 // edit the properties of existing group
\r
676 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
\r
678 // Edit end res position of selected group
\r
679 changeEndRes = true;
\r
681 else if ((stretchGroup != null) && (stretchGroup.getStartRes() == res))
\r
683 // Edit end res position of selected group
\r
684 changeStartRes = true;
\r
687 stretchGroup.getWidth();
\r
689 seqCanvas.repaint();
\r
695 * @param evt DOCUMENT ME!
\r
697 public void doMouseReleasedDefineMode(MouseEvent evt)
\r
701 stretchGroup.recalcConservation();
\r
702 mouseDragging = false;
\r
705 if (stretchGroup == null)
\r
710 if (stretchGroup.cs instanceof ClustalxColourScheme)
\r
712 ((ClustalxColourScheme) stretchGroup.cs).resetClustalX(stretchGroup.sequences,
\r
713 stretchGroup.getWidth());
\r
716 if (stretchGroup.cs instanceof ConservationColourScheme)
\r
718 ConservationColourScheme ccs = (ConservationColourScheme) stretchGroup.cs;
\r
719 stretchGroup.cs = ccs;
\r
720 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
\r
721 stretchGroup.getName());
\r
725 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
\r
726 stretchGroup.getName());
\r
729 changeEndRes = false;
\r
730 changeStartRes = false;
\r
731 stretchGroup = null;
\r
732 PaintRefresher.Refresh(av.alignment);
\r
738 * @param evt DOCUMENT ME!
\r
740 public void doMouseDraggedDefineMode(MouseEvent evt)
\r
742 int res = (evt.getX() / av.getCharWidth()) + av.getStartRes();
\r
743 int y = (evt.getY() / av.getCharHeight()) + av.getStartSeq();
\r
745 if (stretchGroup == null)
\r
750 if (res > av.alignment.getWidth())
\r
752 res = av.alignment.getWidth() - 1;
\r
755 if (stretchGroup.getEndRes() == res)
\r
757 // Edit end res position of selected group
\r
758 changeEndRes = true;
\r
760 else if (stretchGroup.getStartRes() == res)
\r
762 // Edit start res position of selected group
\r
763 changeStartRes = true;
\r
766 if (res < av.getStartRes())
\r
768 res = av.getStartRes();
\r
770 else if (res > av.getEndRes())
\r
772 res = av.getEndRes();
\r
777 if (res > (stretchGroup.getStartRes() - 1))
\r
779 stretchGroup.setEndRes(res);
\r
782 else if (changeStartRes)
\r
784 if (res < (stretchGroup.getEndRes() + 1))
\r
786 stretchGroup.setStartRes(res);
\r
790 int dragDirection = 0;
\r
796 else if (y < oldSeq)
\r
798 dragDirection = -1;
\r
801 while ((y != oldSeq) && (oldSeq > 0) && (y < av.alignment.getHeight()))
\r
803 // This routine ensures we don't skip any sequences, as the
\r
804 // selection is quite slow.
\r
805 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
\r
807 oldSeq += dragDirection;
\r
809 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
\r
811 if (stretchGroup.sequences.contains(nextSeq))
\r
813 stretchGroup.deleteSequence(seq, false);
\r
819 stretchGroup.addSequence(seq, false);
\r
822 stretchGroup.addSequence(nextSeq, false);
\r
827 mouseDragging = true;
\r
829 if (scrollThread != null)
\r
831 scrollThread.setEvent(evt);
\r
834 seqCanvas.repaint();
\r
840 * @param e DOCUMENT ME!
\r
842 public void doMouseEnteredDefineMode(MouseEvent e)
\r
844 if (scrollThread != null)
\r
846 scrollThread.running = false;
\r
853 * @param e DOCUMENT ME!
\r
855 public void doMouseExitedDefineMode(MouseEvent e)
\r
857 if (av.getWrapAlignment())
\r
864 scrollThread = new ScrollThread();
\r
868 // this class allows scrolling off the bottom of the visible alignment
\r
869 class ScrollThread extends Thread
\r
872 boolean running = false;
\r
874 public ScrollThread()
\r
879 public void setEvent(MouseEvent e)
\r
884 public void stopScrolling()
\r
897 if (mouseDragging && (evt.getY() < 0) &&
\r
898 (av.getStartSeq() > 0))
\r
900 running = ap.scrollUp(true);
\r
903 if (mouseDragging && (evt.getY() >= getHeight()) &&
\r
904 (av.alignment.getHeight() > av.getEndSeq()))
\r
906 running = ap.scrollUp(false);
\r
909 if (mouseDragging && (evt.getX() < 0))
\r
911 running = ap.scrollRight(true);
\r
913 else if (mouseDragging && (evt.getX() >= getWidth()))
\r
915 running = ap.scrollRight(false);
\r
923 catch (Exception ex)
\r