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.datamodel.*;
\r
23 import jalview.schemes.*;
\r
26 import java.awt.event.*;
\r
30 import javax.swing.*;
\r
37 * @version $Revision$
\r
39 public class SeqPanel extends JPanel
\r
41 /** DOCUMENT ME!! */
\r
42 public SeqCanvas seqCanvas;
\r
44 /** DOCUMENT ME!! */
\r
45 public AlignmentPanel ap;
\r
46 protected int lastres;
\r
47 protected int startseq;
\r
50 protected AlignViewport av;
\r
52 // if character is inserted or deleted, we will need to recalculate the conservation
\r
53 int seqEditOccurred = -1;
\r
54 ScrollThread scrollThread = null;
\r
55 boolean mouseDragging = false;
\r
56 boolean editingSeqs = false;
\r
57 boolean groupEditing = false;
\r
59 //////////////////////////////////////////
\r
60 /////Everything below this is for defining the boundary of the rubberband
\r
61 //////////////////////////////////////////
\r
63 boolean changeEndSeq = false;
\r
64 boolean changeStartSeq = false;
\r
65 boolean changeEndRes = false;
\r
66 boolean changeStartRes = false;
\r
67 SequenceGroup stretchGroup = null;
\r
68 boolean remove = false;
\r
70 boolean mouseWheelPressed = 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
93 addMouseMotionListener(new MouseMotionAdapter()
\r
95 public void mouseMoved(MouseEvent evt)
\r
100 // This is because MacOSX creates a mouseMoved
\r
101 // If control is down
\r
102 if(!av.isDataset())
\r
103 doMouseDragged(evt);
\r
107 public void mouseDragged(MouseEvent evt)
\r
111 if(!av.isDataset())
\r
112 doMouseDragged(evt);
\r
116 if(!av.isDataset())
\r
117 doMouseDraggedDefineMode(evt);
\r
122 addMouseWheelListener(new MouseWheelListener()
\r
124 public void mouseWheelMoved(MouseWheelEvent e)
\r
127 if (mouseWheelPressed)
\r
129 Font font = av.getFont();
\r
130 int fontSize = font.getSize();
\r
131 if (e.getWheelRotation() > 0 && fontSize < 51)
\r
133 else if (fontSize > 1)
\r
136 av.setFont(new Font(font.getName(), font.getStyle(), fontSize));
\r
141 if (e.getWheelRotation() > 0)
\r
142 ap.scrollUp(false);
\r
151 if(!av.isDataset())
\r
153 addMouseListener(new MouseAdapter()
\r
155 public void mouseReleased(MouseEvent evt)
\r
157 mouseWheelPressed = false;
\r
161 doMouseReleased(evt);
\r
165 doMouseReleasedDefineMode(evt);
\r
169 public void mousePressed(MouseEvent evt)
\r
171 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
\r
173 mouseWheelPressed = true;
\r
177 if (evt.isShiftDown() || evt.isAltDown() ||
\r
178 evt.isControlDown())
\r
180 if (evt.isAltDown() || evt.isControlDown())
\r
182 groupEditing = true;
\r
186 editingSeqs = true;
\r
187 doMousePressed(evt);
\r
191 doMousePressedDefineMode(evt);
\r
195 public void mouseExited(MouseEvent evt)
\r
202 doMouseExitedDefineMode(evt);
\r
205 public void mouseEntered(MouseEvent evt)
\r
212 doMouseEnteredDefineMode(evt);
\r
218 int startWrapBlock=-1;
\r
219 int wrappedBlock=-1;
\r
220 int findRes(MouseEvent evt)
\r
223 int x = evt.getX();
\r
225 if (av.wrapAlignment)
\r
228 int hgap = av.charHeight;
\r
229 if (av.scaleAboveWrapped)
\r
230 hgap += av.charHeight;
\r
232 int cHeight = av.getAlignment().getHeight() * av.charHeight
\r
233 + hgap + seqCanvas.getAnnotationHeight();
\r
235 int y = evt.getY();
\r
237 x -= seqCanvas.LABEL_WEST;
\r
240 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
\r
242 wrappedBlock = y / cHeight;
\r
243 wrappedBlock += av.getStartRes() / cwidth;
\r
245 res = wrappedBlock * cwidth + x / av.getCharWidth();
\r
250 res = (x / av.getCharWidth()) + av.getStartRes();
\r
257 int findSeq(MouseEvent evt)
\r
261 int y = evt.getY();
\r
263 if (av.wrapAlignment)
\r
265 int hgap = av.charHeight;
\r
266 if (av.scaleAboveWrapped)
\r
267 hgap += av.charHeight;
\r
269 int cHeight = av.getAlignment().getHeight() * av.charHeight
\r
270 + hgap + seqCanvas.getAnnotationHeight();
\r
274 seq = ( (y % cHeight) / av.getCharHeight());
\r
278 seq = (y / av.getCharHeight()) + av.getStartSeq();
\r
288 * @param evt DOCUMENT ME!
\r
290 public void doMouseReleased(MouseEvent evt)
\r
292 if (seqEditOccurred > -1)
\r
294 editOccurred(seqEditOccurred);
\r
299 seqEditOccurred = -1;
\r
300 editingSeqs = false;
\r
301 groupEditing = false;
\r
309 * @param evt DOCUMENT ME!
\r
311 public void doMousePressed(MouseEvent evt)
\r
313 ap.alignFrame.addHistoryItem(new HistoryItem("Edit Sequence",
\r
314 av.alignment, HistoryItem.EDIT));
\r
316 int seq = findSeq(evt);
\r
317 int res = findRes(evt);
\r
322 if ((seq < av.getAlignment().getHeight()) &&
\r
323 (res < av.getAlignment().getSequenceAt(seq).getLength()))
\r
334 startEdit = lastres;
\r
343 * @param evt DOCUMENT ME!
\r
345 public void doMouseMoved(MouseEvent evt)
\r
347 int res = findRes(evt);
\r
348 int seq = findSeq(evt);
\r
350 if(res<0 || seq<0 || seq >= av.getAlignment().getHeight())
\r
353 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
\r
355 if (res > sequence.getLength())
\r
360 if(seqCanvas.pdbCanvas!=null && sequence==seqCanvas.pdbCanvas.sequence)
\r
362 seqCanvas.pdbCanvas.highlightRes(sequence.findPosition(res));
\r
366 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: " +
\r
367 sequence.getName());
\r
370 if (av.alignment.isNucleotide())
\r
372 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res) +
\r
375 text.append(" Nucleotide: ");
\r
379 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
\r
381 text.append(" Residue: ");
\r
389 text.append( obj + " (" +
\r
390 av.getAlignment().getSequenceAt(seq).findPosition(res) + ")");
\r
394 ap.alignFrame.statusBar.setText(text.toString());
\r
396 // use aa to see if the mouse pointer is on a
\r
397 if (av.showSequenceFeatures)
\r
399 Vector features = sequence.getDatasetSequence().getSequenceFeatures();
\r
402 StringBuffer sbuffer = new StringBuffer("<html>");
\r
404 for (int i = 0; i < features.size(); i++)
\r
406 SequenceFeature sf = (SequenceFeature) features.elementAt(i);
\r
408 if ( (sf.getBegin() <= sequence.findPosition(res)) &&
\r
409 (sf.getEnd() >= sequence.findPosition(res)))
\r
411 if (sf.getType().equals("disulfide bond"))
\r
413 if (sf.getBegin() == sequence.findPosition(res)
\r
414 || sf.getEnd() == sequence.findPosition(res))
\r
416 if (sbuffer.length() > 6)
\r
417 sbuffer.append("<br>");
\r
418 sbuffer.append("disulfide bond " + sf.getBegin() + ":" +
\r
424 if (sbuffer.length() > 6)
\r
425 sbuffer.append("<br>");
\r
426 sbuffer.append(sf.getType());
\r
427 if (sf.getDescription() != null)
\r
428 sbuffer.append(" " + sf.getDescription());
\r
430 if (sf.getStatus() != null)
\r
432 sbuffer.append(" (" + sf.getStatus() + ")");
\r
439 sbuffer.append("</html>");
\r
440 if(sbuffer.length()==13) // <html></html>
\r
441 setToolTipText("");
\r
443 setToolTipText(sbuffer.toString());
\r
446 setToolTipText("");
\r
453 * @param evt DOCUMENT ME!
\r
455 public void doMouseDragged(MouseEvent evt)
\r
457 // If we're dragging we're editing
\r
458 int res = findRes(evt);
\r
465 if ((lastres == -1) || (lastres == res))
\r
470 boolean dragRight = true;
\r
472 if ((res < av.getAlignment().getWidth()) && (res < lastres))
\r
477 if (res != lastres)
\r
482 SequenceGroup sg = av.getSelectionGroup();
\r
494 sg.setEndRes(sg.getEndRes() + (res - lastres));
\r
500 /// Are we able to delete?
\r
501 // ie are all columns blank?
\r
502 boolean deleteAllowed = false;
\r
504 for (int s = 0; s < sg.getSize(); s++)
\r
506 SequenceI seq = sg.getSequenceAt(s);
\r
508 for (int j = res; j < lastres; j++)
\r
510 if (seq.getSequence().length() <= j)
\r
515 if (!jalview.util.Comparison.isGap(
\r
516 seq.getSequence().charAt(j)))
\r
518 // Not a gap, block edit not valid
\r
520 deleteAllowed = false;
\r
525 deleteAllowed = true;
\r
529 if (!deleteAllowed)
\r
536 sg.setEndRes(sg.getEndRes() - (lastres - res));
\r
539 for (int i = 0; i < sg.getSize(); i++)
\r
541 SequenceI s = sg.getSequenceAt(i);
\r
542 int k = av.alignment.findIndex(s);
\r
547 for (int j = lastres; j < res; j++)
\r
556 for (int j = res; j < lastres; j++)
\r
558 if (s.getLength() > j)
\r
560 deleteChar(res, k);
\r
566 else /////Editing a single sequence///////////
\r
568 if ((res < av.getAlignment().getWidth()) && (res > lastres))
\r
570 // dragging to the right
\r
571 for (int j = lastres; j < res; j++)
\r
573 insertChar(j, startseq);
\r
576 else if ((res < av.getAlignment().getWidth()) &&
\r
579 // dragging to the left
\r
580 for (int j = lastres; j > res; j--)
\r
582 if (jalview.util.Comparison.isGap(
\r
583 av.alignment.getSequenceAt(startseq)
\r
584 .getSequence().charAt(res)))
\r
586 deleteChar(res, startseq);
\r
599 seqCanvas.repaint();
\r
605 * @param seqstart DOCUMENT ME!
\r
606 * @param seqend DOCUMENT ME!
\r
607 * @param start DOCUMENT ME!
\r
609 public void drawChars(int seqstart, int seqend, int start)
\r
611 seqCanvas.drawPanel(seqCanvas.gg, start, av.getEndRes(), seqstart,
\r
612 seqend, av.getStartRes(), av.getStartSeq(), 0);
\r
613 seqCanvas.repaint();
\r
619 * @param j DOCUMENT ME!
\r
620 * @param seq DOCUMENT ME!
\r
622 public void insertChar(int j, int seq)
\r
624 av.alignment.getSequenceAt(seq).insertCharAt(j, av.getGapCharacter());
\r
625 seqEditOccurred = seq;
\r
631 * @param j DOCUMENT ME!
\r
632 * @param seq DOCUMENT ME!
\r
634 public void deleteChar(int j, int seq)
\r
636 av.alignment.getSequenceAt(seq).deleteCharAt(j);
\r
637 seqEditOccurred = seq;
\r
639 av.alignment.getWidth();
\r
640 seqCanvas.repaint();
\r
646 * @param i DOCUMENT ME!
\r
648 void editOccurred(int i)
\r
650 if (endEdit == startEdit)
\r
652 ap.alignFrame.historyList.pop();
\r
653 ap.alignFrame.updateEditMenuBar();
\r
656 av.firePropertyChange("alignment", null,av.getAlignment().getSequences());
\r
663 * @param evt DOCUMENT ME!
\r
665 public void doMousePressedDefineMode(MouseEvent evt)
\r
667 int res = findRes(evt);
\r
668 int seq = findSeq(evt);
\r
671 startWrapBlock=wrappedBlock;
\r
673 if(av.wrapAlignment && seq>av.alignment.getHeight())
\r
675 JOptionPane.showInternalMessageDialog(Desktop.desktop,
\r
676 "Cannot edit annotations in wrapped view.",
\r
677 "Wrapped view - no edit",
\r
678 JOptionPane.WARNING_MESSAGE);
\r
686 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
\r
688 if ((sequence == null) || (res > sequence.getLength()))
\r
693 stretchGroup = av.getSelectionGroup();
\r
695 if (stretchGroup == null)
\r
697 stretchGroup = av.alignment.findGroup(sequence);
\r
699 if ((stretchGroup != null) && (res > stretchGroup.getStartRes()) &&
\r
700 (res < stretchGroup.getEndRes()))
\r
702 av.setSelectionGroup(stretchGroup);
\r
706 stretchGroup = null;
\r
709 else if (!stretchGroup.sequences.contains(sequence) ||
\r
710 (stretchGroup.getStartRes() > res) ||
\r
711 (stretchGroup.getEndRes() < res))
\r
713 stretchGroup = null;
\r
715 SequenceGroup[] allGroups = av.alignment.findAllGroups(sequence);
\r
717 if (allGroups != null)
\r
719 for (int i = 0; i < allGroups.length; i++)
\r
721 if ((allGroups[i].getStartRes() <= res) &&
\r
722 (allGroups[i].getEndRes() >= res))
\r
724 stretchGroup = allGroups[i];
\r
725 av.setSelectionGroup(stretchGroup);
\r
733 if (stretchGroup == null)
\r
735 // define a new group here
\r
736 SequenceGroup sg = new SequenceGroup();
\r
737 sg.setStartRes(res);
\r
739 sg.addSequence(sequence, false);
\r
740 av.setSelectionGroup(sg);
\r
743 if (av.getConservationSelected())
\r
745 SliderPanel.setConservationSlider(ap,
\r
746 av.getGlobalColourScheme(), "Background");
\r
749 if (av.getAbovePIDThreshold())
\r
751 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
\r
755 else if (javax.swing.SwingUtilities.isRightMouseButton(evt))
\r
757 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null);
\r
758 pop.show(this, evt.getX(), evt.getY());
\r
760 // edit the properties of existing group
\r
763 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
\r
765 // Edit end res position of selected group
\r
766 changeEndRes = true;
\r
768 else if ((stretchGroup != null) && (stretchGroup.getStartRes() == res))
\r
770 // Edit end res position of selected group
\r
771 changeStartRes = true;
\r
774 stretchGroup.getWidth();
\r
776 seqCanvas.repaint();
\r
782 * @param evt DOCUMENT ME!
\r
784 public void doMouseReleasedDefineMode(MouseEvent evt)
\r
788 stretchGroup.recalcConservation();
\r
789 mouseDragging = false;
\r
792 if (stretchGroup == null)
\r
797 if(stretchGroup.cs!=null)
\r
799 if (stretchGroup.cs instanceof ClustalxColourScheme)
\r
801 ( (ClustalxColourScheme) stretchGroup.cs).resetClustalX(stretchGroup.
\r
803 stretchGroup.getWidth());
\r
806 if (stretchGroup.cs.conservationApplied())
\r
808 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
\r
809 stretchGroup.getName());
\r
813 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
\r
814 stretchGroup.getName());
\r
817 changeEndRes = false;
\r
818 changeStartRes = false;
\r
819 stretchGroup = null;
\r
820 PaintRefresher.Refresh(av.alignment);
\r
826 * @param evt DOCUMENT ME!
\r
828 public void doMouseDraggedDefineMode(MouseEvent evt)
\r
830 int res = findRes(evt);
\r
831 int y = findSeq(evt);
\r
833 if(wrappedBlock!=startWrapBlock)
\r
836 if (stretchGroup == null)
\r
841 if (res > av.alignment.getWidth())
\r
843 res = av.alignment.getWidth() - 1;
\r
846 if (stretchGroup.getEndRes() == res)
\r
848 // Edit end res position of selected group
\r
849 changeEndRes = true;
\r
851 else if (stretchGroup.getStartRes() == res)
\r
853 // Edit start res position of selected group
\r
854 changeStartRes = true;
\r
857 if (res < av.getStartRes())
\r
859 res = av.getStartRes();
\r
861 else if (res > av.getEndRes() && !av.getWrapAlignment())
\r
863 res = av.getEndRes();
\r
868 if (res > (stretchGroup.getStartRes() - 1))
\r
870 stretchGroup.setEndRes(res);
\r
873 else if (changeStartRes)
\r
875 if (res < (stretchGroup.getEndRes() + 1))
\r
877 stretchGroup.setStartRes(res);
\r
881 int dragDirection = 0;
\r
887 else if (y < oldSeq)
\r
889 dragDirection = -1;
\r
892 while ((y != oldSeq) && (oldSeq > 0) && (y < av.alignment.getHeight()))
\r
894 // This routine ensures we don't skip any sequences, as the
\r
895 // selection is quite slow.
\r
896 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
\r
898 oldSeq += dragDirection;
\r
900 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
\r
902 if (stretchGroup.sequences.contains(nextSeq))
\r
904 stretchGroup.deleteSequence(seq, false);
\r
910 stretchGroup.addSequence(seq, false);
\r
913 stretchGroup.addSequence(nextSeq, false);
\r
918 mouseDragging = true;
\r
920 if (scrollThread != null)
\r
922 scrollThread.setEvent(evt);
\r
925 seqCanvas.repaint();
\r
931 * @param e DOCUMENT ME!
\r
933 public void doMouseEnteredDefineMode(MouseEvent e)
\r
935 if (scrollThread != null)
\r
937 scrollThread.running = false;
\r
944 * @param e DOCUMENT ME!
\r
946 public void doMouseExitedDefineMode(MouseEvent e)
\r
948 if (av.getWrapAlignment())
\r
955 scrollThread = new ScrollThread();
\r
959 // this class allows scrolling off the bottom of the visible alignment
\r
960 class ScrollThread extends Thread
\r
963 boolean running = false;
\r
965 public ScrollThread()
\r
970 public void setEvent(MouseEvent e)
\r
975 public void stopScrolling()
\r
988 if (mouseDragging && (evt.getY() < 0) &&
\r
989 (av.getStartSeq() > 0))
\r
991 running = ap.scrollUp(true);
\r
994 if (mouseDragging && (evt.getY() >= getHeight()) &&
\r
995 (av.alignment.getHeight() > av.getEndSeq()))
\r
997 running = ap.scrollUp(false);
\r
1000 if (mouseDragging && (evt.getX() < 0))
\r
1002 running = ap.scrollRight(true);
\r
1004 else if (mouseDragging && (evt.getX() >= getWidth()))
\r
1006 running = ap.scrollRight(false);
\r
1014 catch (Exception ex)
\r