2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import java.awt.BorderLayout;
24 import java.awt.Color;
26 import java.awt.FontMetrics;
27 import java.awt.Point;
28 import java.awt.event.MouseEvent;
29 import java.awt.event.MouseListener;
30 import java.awt.event.MouseMotionListener;
31 import java.awt.event.MouseWheelEvent;
32 import java.awt.event.MouseWheelListener;
33 import java.util.List;
34 import java.util.Vector;
36 import javax.swing.JOptionPane;
37 import javax.swing.JPanel;
38 import javax.swing.ToolTipManager;
40 import jalview.api.AlignViewportI;
41 import jalview.commands.EditCommand;
42 import jalview.commands.EditCommand.Action;
43 import jalview.commands.EditCommand.Edit;
44 import jalview.datamodel.ColumnSelection;
45 import jalview.datamodel.SearchResults;
46 import jalview.datamodel.SearchResults.Match;
47 import jalview.datamodel.Sequence;
48 import jalview.datamodel.SequenceFeature;
49 import jalview.datamodel.SequenceGroup;
50 import jalview.datamodel.SequenceI;
51 import jalview.io.SequenceAnnotationReport;
52 import jalview.schemes.ResidueProperties;
53 import jalview.structure.SelectionListener;
54 import jalview.structure.SelectionSource;
55 import jalview.structure.SequenceListener;
56 import jalview.structure.StructureSelectionManager;
57 import jalview.structure.VamsasSource;
58 import jalview.util.Comparison;
59 import jalview.util.MappingUtils;
60 import jalview.util.MessageManager;
61 import jalview.viewmodel.AlignmentViewport;
67 * @version $Revision: 1.130 $
69 public class SeqPanel extends JPanel implements MouseListener,
70 MouseMotionListener, MouseWheelListener, SequenceListener,
75 public SeqCanvas seqCanvas;
78 public AlignmentPanel ap;
80 protected int lastres;
82 protected int startseq;
84 protected AlignViewport av;
86 ScrollThread scrollThread = null;
88 boolean mouseDragging = false;
90 boolean editingSeqs = false;
92 boolean groupEditing = false;
94 // ////////////////////////////////////////
95 // ///Everything below this is for defining the boundary of the rubberband
96 // ////////////////////////////////////////
99 boolean changeEndSeq = false;
101 boolean changeStartSeq = false;
103 boolean changeEndRes = false;
105 boolean changeStartRes = false;
107 SequenceGroup stretchGroup = null;
109 boolean remove = false;
111 Point lastMousePress;
113 boolean mouseWheelPressed = false;
115 StringBuffer keyboardNo1;
117 StringBuffer keyboardNo2;
119 java.net.URL linkImageURL;
121 private final SequenceAnnotationReport seqARep;
123 StringBuffer tooltipText = new StringBuffer();
127 EditCommand editCommand;
129 StructureSelectionManager ssm;
132 * Creates a new SeqPanel object.
139 public SeqPanel(AlignViewport av, AlignmentPanel ap)
141 linkImageURL = getClass().getResource("/images/link.gif");
142 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
143 ToolTipManager.sharedInstance().registerComponent(this);
144 ToolTipManager.sharedInstance().setInitialDelay(0);
145 ToolTipManager.sharedInstance().setDismissDelay(10000);
147 setBackground(Color.white);
149 seqCanvas = new SeqCanvas(ap);
150 setLayout(new BorderLayout());
151 add(seqCanvas, BorderLayout.CENTER);
157 addMouseMotionListener(this);
158 addMouseListener(this);
159 addMouseWheelListener(this);
160 ssm = av.getStructureSelectionManager();
161 ssm.addStructureViewerListener(this);
162 ssm.addSelectionListener(this);
166 int startWrapBlock = -1;
168 int wrappedBlock = -1;
170 int findRes(MouseEvent evt)
175 if (av.getWrapAlignment())
178 int hgap = av.getCharHeight();
179 if (av.getScaleAboveWrapped())
181 hgap += av.getCharHeight();
184 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
186 + seqCanvas.getAnnotationHeight();
190 x -= seqCanvas.LABEL_WEST;
192 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
198 wrappedBlock = y / cHeight;
199 wrappedBlock += av.getStartRes() / cwidth;
201 res = wrappedBlock * cwidth + x / av.getCharWidth();
206 if (x > seqCanvas.getWidth() + seqCanvas.getWidth())
208 // make sure we calculate relative to visible alignment, rather than
210 x = seqCanvas.getX() + seqCanvas.getWidth();
212 res = (x / av.getCharWidth()) + av.getStartRes();
215 if (av.hasHiddenColumns())
217 res = av.getColumnSelection().adjustForHiddenColumns(res);
224 int findSeq(MouseEvent evt)
229 if (av.getWrapAlignment())
231 int hgap = av.getCharHeight();
232 if (av.getScaleAboveWrapped())
234 hgap += av.getCharHeight();
237 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
239 + seqCanvas.getAnnotationHeight();
243 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
248 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
249 .getAlignment().getHeight() - 1);
256 * When all of a sequence of edits are complete, put the resulting edit list
257 * on the history stack (undo list), and reset flags for editing in progress.
263 if (editCommand != null && editCommand.getSize() > 0)
265 ap.alignFrame.addHistoryItem(editCommand);
266 av.firePropertyChange("alignment", null, av.getAlignment()
272 * Tidy up come what may...
277 groupEditing = false;
286 seqCanvas.cursorY = getKeyboardNo1() - 1;
290 void setCursorColumn()
292 seqCanvas.cursorX = getKeyboardNo1() - 1;
296 void setCursorRowAndColumn()
298 if (keyboardNo2 == null)
300 keyboardNo2 = new StringBuffer();
304 seqCanvas.cursorX = getKeyboardNo1() - 1;
305 seqCanvas.cursorY = getKeyboardNo2() - 1;
310 void setCursorPosition()
312 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
314 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
318 void moveCursor(int dx, int dy)
320 seqCanvas.cursorX += dx;
321 seqCanvas.cursorY += dy;
322 if (av.hasHiddenColumns()
323 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
325 int original = seqCanvas.cursorX - dx;
326 int maxWidth = av.getAlignment().getWidth();
328 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
329 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
331 seqCanvas.cursorX += dx;
334 if (seqCanvas.cursorX >= maxWidth
335 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
337 seqCanvas.cursorX = original;
344 void scrollToVisible()
346 if (seqCanvas.cursorX < 0)
348 seqCanvas.cursorX = 0;
350 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
352 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
355 if (seqCanvas.cursorY < 0)
357 seqCanvas.cursorY = 0;
359 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
361 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
365 if (av.getWrapAlignment())
367 ap.scrollToWrappedVisible(seqCanvas.cursorX);
371 while (seqCanvas.cursorY < av.startSeq)
375 while (seqCanvas.cursorY + 1 > av.endSeq)
379 if (!av.getWrapAlignment())
381 while (seqCanvas.cursorX < av.getColumnSelection()
382 .adjustForHiddenColumns(av.startRes))
384 if (!ap.scrollRight(false))
389 while (seqCanvas.cursorX > av.getColumnSelection()
390 .adjustForHiddenColumns(av.endRes))
392 if (!ap.scrollRight(true))
399 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
400 seqCanvas.cursorX, seqCanvas.cursorY);
405 void setSelectionAreaAtCursor(boolean topLeft)
407 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
409 if (av.getSelectionGroup() != null)
411 SequenceGroup sg = av.getSelectionGroup();
412 // Find the top and bottom of this group
413 int min = av.getAlignment().getHeight(), max = 0;
414 for (int i = 0; i < sg.getSize(); i++)
416 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
431 sg.setStartRes(seqCanvas.cursorX);
432 if (sg.getEndRes() < seqCanvas.cursorX)
434 sg.setEndRes(seqCanvas.cursorX);
437 min = seqCanvas.cursorY;
441 sg.setEndRes(seqCanvas.cursorX);
442 if (sg.getStartRes() > seqCanvas.cursorX)
444 sg.setStartRes(seqCanvas.cursorX);
447 max = seqCanvas.cursorY + 1;
452 // Only the user can do this
453 av.setSelectionGroup(null);
457 // Now add any sequences between min and max
458 sg.getSequences(null).clear();
459 for (int i = min; i < max; i++)
461 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
466 if (av.getSelectionGroup() == null)
468 SequenceGroup sg = new SequenceGroup();
469 sg.setStartRes(seqCanvas.cursorX);
470 sg.setEndRes(seqCanvas.cursorX);
471 sg.addSequence(sequence, false);
472 av.setSelectionGroup(sg);
475 ap.paintAlignment(false);
479 void insertGapAtCursor(boolean group)
481 groupEditing = group;
482 startseq = seqCanvas.cursorY;
483 lastres = seqCanvas.cursorX;
484 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
488 void deleteGapAtCursor(boolean group)
490 groupEditing = group;
491 startseq = seqCanvas.cursorY;
492 lastres = seqCanvas.cursorX + getKeyboardNo1();
493 editSequence(false, false, seqCanvas.cursorX);
497 void insertNucAtCursor(boolean group, String nuc)
499 groupEditing = group;
500 startseq = seqCanvas.cursorY;
501 lastres = seqCanvas.cursorX;
502 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
506 void numberPressed(char value)
508 if (keyboardNo1 == null)
510 keyboardNo1 = new StringBuffer();
513 if (keyboardNo2 != null)
515 keyboardNo2.append(value);
519 keyboardNo1.append(value);
527 if (keyboardNo1 != null)
529 int value = Integer.parseInt(keyboardNo1.toString());
533 } catch (Exception x)
544 if (keyboardNo2 != null)
546 int value = Integer.parseInt(keyboardNo2.toString());
550 } catch (Exception x)
564 public void mouseReleased(MouseEvent evt)
566 mouseDragging = false;
567 mouseWheelPressed = false;
571 doMouseReleasedDefineMode(evt);
585 public void mousePressed(MouseEvent evt)
587 lastMousePress = evt.getPoint();
589 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
591 mouseWheelPressed = true;
595 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
597 if (evt.isAltDown() || evt.isControlDown())
605 doMousePressedDefineMode(evt);
609 int seq = findSeq(evt);
610 int res = findRes(evt);
612 if (seq < 0 || res < 0)
617 if ((seq < av.getAlignment().getHeight())
618 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
635 public void mouseOverSequence(SequenceI sequence, int index, int pos)
637 String tmp = sequence.hashCode() + " " + index + " " + pos;
639 if (lastMessage == null || !lastMessage.equals(tmp))
641 // System.err.println("mouseOver Sequence: "+tmp);
642 ssm.mouseOverSequence(sequence, index, pos, av);
648 public void highlightSequence(SearchResults results)
650 if (av.isFollowHighlight())
653 * if scrollToPosition requires scroll adjustment, this flag prevents
654 * another scroll event being propagated back to the originator
656 * @see AlignmentPanel#adjustmentValueChanged
658 ap.setFollowingComplementScroll(true);
659 if (ap.scrollToPosition(results, false))
661 seqCanvas.revalidate();
664 setStatusMessage(results);
665 seqCanvas.highlightSearchResults(results);
669 public VamsasSource getVamsasSource()
671 return this.ap == null ? null : this.ap.av;
674 public void updateColours(SequenceI seq, int index)
676 System.out.println("update the seqPanel colours");
687 public void mouseMoved(MouseEvent evt)
691 // This is because MacOSX creates a mouseMoved
692 // If control is down, other platforms will not.
696 int res = findRes(evt);
697 int seq = findSeq(evt);
699 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
704 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
706 if (res >= sequence.getLength())
711 pos = setStatusMessage(sequence, res, seq);
712 if (ssm != null && pos > -1)
714 mouseOverSequence(sequence, res, pos);
717 tooltipText.setLength(6); // Cuts the buffer back to <html>
719 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
722 for (int g = 0; g < groups.length; g++)
724 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
726 if (!groups[g].getName().startsWith("JTreeGroup")
727 && !groups[g].getName().startsWith("JGroup"))
729 tooltipText.append(groups[g].getName());
732 if (groups[g].getDescription() != null)
734 tooltipText.append(": " + groups[g].getDescription());
740 // use aa to see if the mouse pointer is on a
741 if (av.isShowSequenceFeatures())
744 List<SequenceFeature> features = ap.getFeatureRenderer().findFeaturesAtRes(
745 sequence.getDatasetSequence(),
746 rpos = sequence.findPosition(res));
747 seqARep.appendFeatures(tooltipText, rpos, features,
748 this.ap.getSeqPanel().seqCanvas.fr.getMinMax());
750 if (tooltipText.length() == 6) // <html></html>
752 setToolTipText(null);
757 if (lastTooltip == null
758 || !lastTooltip.equals(tooltipText.toString()))
760 String formatedTooltipText = JvSwingUtils.wrapTooltip(true,
761 tooltipText.toString());
762 // String formatedTooltipText = tooltipText.toString();
763 setToolTipText(formatedTooltipText);
764 lastTooltip = tooltipText.toString();
771 private Point lastp = null;
776 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
778 public Point getToolTipLocation(MouseEvent event)
780 int x = event.getX(), w = getWidth();
781 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
784 if (!event.isShiftDown() || p == null)
786 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
787 event.getX() + wdth, event.getY() - 20) : null;
790 * TODO: try to modify position region is not obcured by tooltip
798 * Set status message in alignment panel
801 * aligned sequence object
805 * index of sequence in alignment
806 * @return position of res in sequence
808 int setStatusMessage(SequenceI sequence, int res, int seq)
810 StringBuilder text = new StringBuilder(32);
813 * Sequence number (if known), and sequence name.
815 String seqno = seq == -1 ? "" : " " + (seq + 1);
816 text.append("Sequence" + seqno + " ID: " + sequence.getName());
818 String residue = null;
820 * Try to translate the display character to residue name (null for gap).
822 final String displayChar = String.valueOf(sequence.getCharAt(res));
823 if (av.getAlignment().isNucleotide())
825 residue = ResidueProperties.nucleotideName.get(displayChar);
828 text.append(" Nucleotide: ").append(residue);
833 residue = "X".equalsIgnoreCase(displayChar) ? "X"
834 : ResidueProperties.aa2Triplet.get(displayChar);
837 text.append(" Residue: ").append(residue);
844 pos = sequence.findPosition(res);
845 text.append(" (").append(Integer.toString(pos)).append(")");
847 ap.alignFrame.statusBar.setText(text.toString());
852 * Set the status bar message to highlight the first matched position in
857 private void setStatusMessage(SearchResults results)
859 List<Match> matches = results.getResults();
860 if (!matches.isEmpty())
862 Match m = matches.get(0);
863 SequenceI seq = m.getSequence();
864 int sequenceIndex = this.av.getAlignment().findIndex(seq);
867 * Convert position in sequence (base 1) to sequence character array index
870 int start = m.getStart() - 1;
871 setStatusMessage(seq, start, sequenceIndex);
882 public void mouseDragged(MouseEvent evt)
884 if (mouseWheelPressed)
886 int oldWidth = av.getCharWidth();
888 // Which is bigger, left-right or up-down?
889 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
890 .getX() - lastMousePress.getX()))
892 int fontSize = av.font.getSize();
894 if (evt.getY() < lastMousePress.getY())
898 else if (evt.getY() > lastMousePress.getY())
909 new Font(av.font.getName(), av.font.getStyle(), fontSize),
911 av.setCharWidth(oldWidth);
916 if (evt.getX() < lastMousePress.getX() && av.getCharWidth() > 1)
918 av.setCharWidth(av.getCharWidth() - 1);
920 else if (evt.getX() > lastMousePress.getX())
922 av.setCharWidth(av.getCharWidth() + 1);
925 ap.paintAlignment(false);
928 FontMetrics fm = getFontMetrics(av.getFont());
929 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
931 lastMousePress = evt.getPoint();
938 doMouseDraggedDefineMode(evt);
942 int res = findRes(evt);
949 if ((lastres == -1) || (lastres == res))
954 if ((res < av.getAlignment().getWidth()) && (res < lastres))
956 // dragLeft, delete gap
957 editSequence(false, false, res);
961 editSequence(true, false, res);
964 mouseDragging = true;
965 if (scrollThread != null)
967 scrollThread.setEvent(evt);
971 // TODO: Make it more clever than many booleans
972 synchronized void editSequence(boolean insertGap, boolean editSeq,
977 boolean fixedColumns = false;
978 SequenceGroup sg = av.getSelectionGroup();
980 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
982 // No group, but the sequence may represent a group
983 if (!groupEditing && av.hasHiddenRows())
985 if (av.isHiddenRepSequence(seq))
987 sg = av.getRepresentedSequences(seq);
992 StringBuilder message = new StringBuilder(64);
995 message.append("Edit group:");
996 if (editCommand == null)
998 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
1003 message.append("Edit sequence: " + seq.getName());
1004 String label = seq.getName();
1005 if (label.length() > 10)
1007 label = label.substring(0, 10);
1009 if (editCommand == null)
1011 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
1017 message.append(" insert ");
1021 message.append(" delete ");
1024 message.append(Math.abs(startres - lastres) + " gaps.");
1025 ap.alignFrame.statusBar.setText(message.toString());
1027 // Are we editing within a selection group?
1029 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1032 fixedColumns = true;
1034 // sg might be null as the user may only see 1 sequence,
1035 // but the sequence represents a group
1038 if (!av.isHiddenRepSequence(seq))
1043 sg = av.getRepresentedSequences(seq);
1046 fixedLeft = sg.getStartRes();
1047 fixedRight = sg.getEndRes();
1049 if ((startres < fixedLeft && lastres >= fixedLeft)
1050 || (startres >= fixedLeft && lastres < fixedLeft)
1051 || (startres > fixedRight && lastres <= fixedRight)
1052 || (startres <= fixedRight && lastres > fixedRight))
1058 if (fixedLeft > startres)
1060 fixedRight = fixedLeft - 1;
1063 else if (fixedRight < startres)
1065 fixedLeft = fixedRight;
1070 if (av.hasHiddenColumns())
1072 fixedColumns = true;
1073 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1074 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1076 if ((insertGap && startres > y1 && lastres < y1)
1077 || (!insertGap && startres < y2 && lastres > y2))
1083 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1084 // Selection spans a hidden region
1085 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1093 fixedRight = y2 - 1;
1100 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1101 int g, groupSize = vseqs.size();
1102 SequenceI[] groupSeqs = new SequenceI[groupSize];
1103 for (g = 0; g < groupSeqs.length; g++)
1105 groupSeqs[g] = vseqs.get(g);
1111 // If the user has selected the whole sequence, and is dragging to
1112 // the right, we can still extend the alignment and selectionGroup
1113 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1114 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1116 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1117 fixedRight = sg.getEndRes();
1120 // Is it valid with fixed columns??
1121 // Find the next gap before the end
1122 // of the visible region boundary
1123 boolean blank = false;
1124 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1128 for (g = 0; g < groupSize; g++)
1130 for (int j = 0; j < startres - lastres; j++)
1132 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1133 .getCharAt(fixedRight - j)))
1148 if (sg.getSize() == av.getAlignment().getHeight())
1150 if ((av.hasHiddenColumns() && startres < av
1151 .getColumnSelection().getHiddenBoundaryRight(startres)))
1157 int alWidth = av.getAlignment().getWidth();
1158 if (av.hasHiddenRows())
1160 int hwidth = av.getAlignment().getHiddenSequences()
1162 if (hwidth > alWidth)
1167 // We can still insert gaps if the selectionGroup
1168 // contains all the sequences
1169 sg.setEndRes(sg.getEndRes() + startres - lastres);
1170 fixedRight = alWidth + startres - lastres;
1181 else if (!insertGap)
1183 // / Are we able to delete?
1184 // ie are all columns blank?
1186 for (g = 0; g < groupSize; g++)
1188 for (int j = startres; j < lastres; j++)
1190 if (groupSeqs[g].getLength() <= j)
1195 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1197 // Not a gap, block edit not valid
1207 // dragging to the right
1208 if (fixedColumns && fixedRight != -1)
1210 for (int j = lastres; j < startres; j++)
1212 insertChar(j, groupSeqs, fixedRight);
1217 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1223 // dragging to the left
1224 if (fixedColumns && fixedRight != -1)
1226 for (int j = lastres; j > startres; j--)
1228 deleteChar(startres, groupSeqs, fixedRight);
1233 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1240 // ///Editing a single sequence///////////
1244 // dragging to the right
1245 if (fixedColumns && fixedRight != -1)
1247 for (int j = lastres; j < startres; j++)
1249 insertChar(j, new SequenceI[]
1250 { seq }, fixedRight);
1255 appendEdit(Action.INSERT_GAP, new SequenceI[]
1256 { seq }, lastres, startres - lastres);
1263 // dragging to the left
1264 if (fixedColumns && fixedRight != -1)
1266 for (int j = lastres; j > startres; j--)
1268 if (!Comparison.isGap(seq.getCharAt(startres)))
1273 deleteChar(startres, new SequenceI[]
1274 { seq }, fixedRight);
1279 // could be a keyboard edit trying to delete none gaps
1281 for (int m = startres; m < lastres; m++)
1283 if (!Comparison.isGap(seq.getCharAt(m)))
1292 appendEdit(Action.DELETE_GAP, new SequenceI[]
1293 { seq }, startres, max);
1298 {// insertGap==false AND editSeq==TRUE;
1299 if (fixedColumns && fixedRight != -1)
1301 for (int j = lastres; j < startres; j++)
1303 insertChar(j, new SequenceI[]
1304 { seq }, fixedRight);
1309 appendEdit(Action.INSERT_NUC, new SequenceI[]
1310 { seq }, lastres, startres - lastres);
1317 seqCanvas.repaint();
1320 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1322 int blankColumn = fixedColumn;
1323 for (int s = 0; s < seq.length; s++)
1325 // Find the next gap before the end of the visible region boundary
1326 // If lastCol > j, theres a boundary after the gap insertion
1328 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1330 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1332 // Theres a space, so break and insert the gap
1337 if (blankColumn <= j)
1339 blankColumn = fixedColumn;
1345 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1347 appendEdit(Action.INSERT_GAP, seq, j, 1);
1352 * Helper method to add and perform one edit action.
1359 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1363 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1364 av.getAlignment().getGapCharacter());
1366 editCommand.appendEdit(edit, av.getAlignment(),
1370 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1373 appendEdit(Action.DELETE_GAP, seq, j, 1);
1375 appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1);
1385 public void mouseEntered(MouseEvent e)
1392 if (scrollThread != null)
1394 scrollThread.running = false;
1395 scrollThread = null;
1406 public void mouseExited(MouseEvent e)
1408 if (av.getWrapAlignment())
1415 scrollThread = new ScrollThread();
1420 public void mouseClicked(MouseEvent evt)
1422 SequenceGroup sg = null;
1423 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1424 if (evt.getClickCount() > 1)
1426 sg = av.getSelectionGroup();
1427 if (sg != null && sg.getSize() == 1
1428 && sg.getEndRes() - sg.getStartRes() < 2)
1430 av.setSelectionGroup(null);
1433 List<SequenceFeature> features = seqCanvas.getFeatureRenderer().findFeaturesAtRes(
1434 sequence.getDatasetSequence(),
1435 sequence.findPosition(findRes(evt)));
1437 if (features != null && features.size()> 0)
1439 SearchResults highlight = new SearchResults();
1440 highlight.addResult(sequence, features.get(0).getBegin(),
1441 features.get(0).getEnd());
1442 seqCanvas.highlightSearchResults(highlight);
1444 if (features != null && features.size()> 0)
1446 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1447 { sequence }, features.toArray(new SequenceFeature[features.size()]), false, ap);
1449 seqCanvas.highlightSearchResults(null);
1455 public void mouseWheelMoved(MouseWheelEvent e)
1458 if (e.getWheelRotation() > 0)
1460 if (e.isShiftDown())
1462 ap.scrollRight(true);
1472 if (e.isShiftDown())
1474 ap.scrollRight(false);
1481 // TODO Update tooltip for new position.
1490 public void doMousePressedDefineMode(MouseEvent evt)
1492 int res = findRes(evt);
1493 int seq = findSeq(evt);
1496 startWrapBlock = wrappedBlock;
1498 if (av.getWrapAlignment() && seq > av.getAlignment().getHeight())
1500 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1501 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1502 MessageManager.getString("label.wrapped_view_no_edit"),
1503 JOptionPane.WARNING_MESSAGE);
1507 if (seq < 0 || res < 0)
1512 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1514 if ((sequence == null) || (res > sequence.getLength()))
1519 stretchGroup = av.getSelectionGroup();
1521 if (stretchGroup == null)
1523 stretchGroup = av.getAlignment().findGroup(sequence);
1525 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1526 && (res < stretchGroup.getEndRes()))
1528 av.setSelectionGroup(stretchGroup);
1532 stretchGroup = null;
1535 else if (!stretchGroup.getSequences(null).contains(sequence)
1536 || (stretchGroup.getStartRes() > res)
1537 || (stretchGroup.getEndRes() < res))
1539 stretchGroup = null;
1541 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1543 if (allGroups != null)
1545 for (int i = 0; i < allGroups.length; i++)
1547 if ((allGroups[i].getStartRes() <= res)
1548 && (allGroups[i].getEndRes() >= res))
1550 stretchGroup = allGroups[i];
1556 av.setSelectionGroup(stretchGroup);
1560 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1562 List<SequenceFeature> allFeatures = ap.getFeatureRenderer().findFeaturesAtRes(
1563 sequence.getDatasetSequence(), sequence.findPosition(res));
1564 Vector links = new Vector();
1565 for (SequenceFeature sf:allFeatures)
1567 if (sf.links != null)
1569 for (int j = 0; j < sf.links.size(); j++)
1571 links.addElement(sf.links.elementAt(j));
1576 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1577 pop.show(this, evt.getX(), evt.getY());
1583 seqCanvas.cursorX = findRes(evt);
1584 seqCanvas.cursorY = findSeq(evt);
1585 seqCanvas.repaint();
1589 if (stretchGroup == null)
1591 // Only if left mouse button do we want to change group sizes
1593 // define a new group here
1594 SequenceGroup sg = new SequenceGroup();
1595 sg.setStartRes(res);
1597 sg.addSequence(sequence, false);
1598 av.setSelectionGroup(sg);
1602 if (av.getConservationSelected())
1604 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1608 if (av.getAbovePIDThreshold())
1610 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1613 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1615 // Edit end res position of selected group
1616 changeEndRes = true;
1618 else if ((stretchGroup != null)
1619 && (stretchGroup.getStartRes() == res))
1621 // Edit end res position of selected group
1622 changeStartRes = true;
1624 stretchGroup.getWidth();
1627 seqCanvas.repaint();
1636 public void doMouseReleasedDefineMode(MouseEvent evt)
1638 if (stretchGroup == null)
1643 stretchGroup.recalcConservation(); // always do this - annotation has own
1645 if (stretchGroup.cs != null)
1647 stretchGroup.cs.alignmentChanged(stretchGroup,
1648 av.getHiddenRepSequences());
1650 if (stretchGroup.cs.conservationApplied())
1652 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1653 stretchGroup.getName());
1657 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1658 stretchGroup.getName());
1661 PaintRefresher.Refresh(this, av.getSequenceSetId());
1662 ap.paintAlignment(true);
1664 changeEndRes = false;
1665 changeStartRes = false;
1666 stretchGroup = null;
1676 public void doMouseDraggedDefineMode(MouseEvent evt)
1678 int res = findRes(evt);
1679 int y = findSeq(evt);
1681 if (wrappedBlock != startWrapBlock)
1686 if (stretchGroup == null)
1691 if (res >= av.getAlignment().getWidth())
1693 res = av.getAlignment().getWidth() - 1;
1696 if (stretchGroup.getEndRes() == res)
1698 // Edit end res position of selected group
1699 changeEndRes = true;
1701 else if (stretchGroup.getStartRes() == res)
1703 // Edit start res position of selected group
1704 changeStartRes = true;
1707 if (res < av.getStartRes())
1709 res = av.getStartRes();
1714 if (res > (stretchGroup.getStartRes() - 1))
1716 stretchGroup.setEndRes(res);
1719 else if (changeStartRes)
1721 if (res < (stretchGroup.getEndRes() + 1))
1723 stretchGroup.setStartRes(res);
1727 int dragDirection = 0;
1733 else if (y < oldSeq)
1738 while ((y != oldSeq) && (oldSeq > -1)
1739 && (y < av.getAlignment().getHeight()))
1741 // This routine ensures we don't skip any sequences, as the
1742 // selection is quite slow.
1743 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1745 oldSeq += dragDirection;
1752 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1754 if (stretchGroup.getSequences(null).contains(nextSeq))
1756 stretchGroup.deleteSequence(seq, false);
1762 stretchGroup.addSequence(seq, false);
1765 stretchGroup.addSequence(nextSeq, false);
1774 mouseDragging = true;
1776 if (scrollThread != null)
1778 scrollThread.setEvent(evt);
1781 seqCanvas.repaint();
1784 void scrollCanvas(MouseEvent evt)
1788 if (scrollThread != null)
1790 scrollThread.running = false;
1791 scrollThread = null;
1793 mouseDragging = false;
1797 if (scrollThread == null)
1799 scrollThread = new ScrollThread();
1802 mouseDragging = true;
1803 scrollThread.setEvent(evt);
1808 // this class allows scrolling off the bottom of the visible alignment
1809 class ScrollThread extends Thread
1813 boolean running = false;
1815 public ScrollThread()
1820 public void setEvent(MouseEvent e)
1825 public void stopScrolling()
1839 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1841 running = ap.scrollUp(true);
1844 if (mouseDragging && (evt.getY() >= getHeight())
1845 && (av.getAlignment().getHeight() > av.getEndSeq()))
1847 running = ap.scrollUp(false);
1850 if (mouseDragging && (evt.getX() < 0))
1852 running = ap.scrollRight(false);
1854 else if (mouseDragging && (evt.getX() >= getWidth()))
1856 running = ap.scrollRight(true);
1863 } catch (Exception ex)
1871 * modify current selection according to a received message.
1874 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1875 SelectionSource source)
1877 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1878 // handles selection messages...
1879 // TODO: extend config options to allow user to control if selections may be
1880 // shared between viewports.
1881 boolean iSentTheSelection = (av == source
1882 || (source instanceof AlignViewport && ((AlignmentViewport) source)
1883 .getSequenceSetId().equals(av.getSequenceSetId())));
1884 if (iSentTheSelection || !av.followSelection)
1890 * Ignore the selection if there is one of our own pending.
1892 if (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1898 * Check for selection in a view of which this one is a dna/protein
1901 if (selectionFromTranslation(seqsel, colsel, source))
1906 // do we want to thread this ? (contention with seqsel and colsel locks, I
1908 // rules are: colsel is copied if there is a real intersection between
1909 // sequence selection
1910 boolean repaint = false;
1911 boolean copycolsel = true;
1913 SequenceGroup sgroup = null;
1914 if (seqsel != null && seqsel.getSize() > 0)
1916 if (av.getAlignment() == null)
1918 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1919 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1920 + " 's alignment is NULL! returning immediately.");
1923 sgroup = seqsel.intersect(av.getAlignment(),
1924 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1925 if ((sgroup == null || sgroup.getSize() == 0)
1926 || (colsel == null || colsel.size() == 0))
1928 // don't copy columns if the region didn't intersect.
1932 if (sgroup != null && sgroup.getSize() > 0)
1934 av.setSelectionGroup(sgroup);
1938 av.setSelectionGroup(null);
1940 av.isSelectionGroupChanged(true);
1945 // the current selection is unset or from a previous message
1946 // so import the new colsel.
1947 if (colsel == null || colsel.size() == 0)
1949 if (av.getColumnSelection() != null)
1951 av.getColumnSelection().clear();
1957 // TODO: shift colSel according to the intersecting sequences
1958 if (av.getColumnSelection() == null)
1960 av.setColumnSelection(new ColumnSelection(colsel));
1964 av.getColumnSelection().setElementsFrom(colsel);
1967 av.isColSelChanged(true);
1972 && av.hasHiddenColumns()
1973 && (av.getColumnSelection() == null || av.getColumnSelection()
1974 .getHiddenColumns() == null))
1976 System.err.println("Bad things");
1978 if (repaint) // always true!
1980 // probably finessing with multiple redraws here
1981 PaintRefresher.Refresh(this, av.getSequenceSetId());
1982 // ap.paintAlignment(false);
1987 * If this panel is a cdna/protein translation view of the selection source,
1988 * tries to map the source selection to a local one, and returns true. Else
1995 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1996 ColumnSelection colsel, SelectionSource source)
1998 if (!(source instanceof AlignViewportI)) {
2001 final AlignViewportI sourceAv = (AlignViewportI) source;
2002 if (sourceAv.getCodingComplement() != av && av.getCodingComplement() != sourceAv)
2008 * Map sequence selection
2010 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
2011 av.setSelectionGroup(sg);
2012 av.isSelectionGroupChanged(true);
2015 * Map column selection
2017 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
2019 av.setColumnSelection(cs);
2020 av.isColSelChanged(true);
2022 PaintRefresher.Refresh(this, av.getSequenceSetId());