2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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 jalview.api.AlignViewportI;
24 import jalview.commands.EditCommand;
25 import jalview.commands.EditCommand.Action;
26 import jalview.commands.EditCommand.Edit;
27 import jalview.datamodel.ColumnSelection;
28 import jalview.datamodel.SearchResults;
29 import jalview.datamodel.SearchResults.Match;
30 import jalview.datamodel.Sequence;
31 import jalview.datamodel.SequenceFeature;
32 import jalview.datamodel.SequenceGroup;
33 import jalview.datamodel.SequenceI;
34 import jalview.io.SequenceAnnotationReport;
35 import jalview.schemes.ResidueProperties;
36 import jalview.structure.SelectionListener;
37 import jalview.structure.SelectionSource;
38 import jalview.structure.SequenceListener;
39 import jalview.structure.StructureSelectionManager;
40 import jalview.structure.VamsasSource;
41 import jalview.util.Comparison;
42 import jalview.util.MappingUtils;
43 import jalview.util.MessageManager;
44 import jalview.viewmodel.AlignmentViewport;
46 import java.awt.BorderLayout;
47 import java.awt.Color;
49 import java.awt.FontMetrics;
50 import java.awt.Point;
51 import java.awt.event.MouseEvent;
52 import java.awt.event.MouseListener;
53 import java.awt.event.MouseMotionListener;
54 import java.awt.event.MouseWheelEvent;
55 import java.awt.event.MouseWheelListener;
56 import java.util.List;
57 import java.util.Vector;
59 import javax.swing.JOptionPane;
60 import javax.swing.JPanel;
61 import javax.swing.ToolTipManager;
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.followHighlight)
652 if (ap.scrollToPosition(results, false))
654 seqCanvas.revalidate();
657 setStatusMessage(results);
658 seqCanvas.highlightSearchResults(results);
662 public VamsasSource getVamsasSource()
664 return this.ap == null ? null : this.ap.av;
667 public void updateColours(SequenceI seq, int index)
669 System.out.println("update the seqPanel colours");
680 public void mouseMoved(MouseEvent evt)
684 // This is because MacOSX creates a mouseMoved
685 // If control is down, other platforms will not.
689 int res = findRes(evt);
690 int seq = findSeq(evt);
692 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
697 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
699 if (res >= sequence.getLength())
704 pos = setStatusMessage(sequence, res, seq);
705 if (ssm != null && pos > -1)
707 mouseOverSequence(sequence, res, pos);
710 tooltipText.setLength(6); // Cuts the buffer back to <html>
712 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
715 for (int g = 0; g < groups.length; g++)
717 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
719 if (!groups[g].getName().startsWith("JTreeGroup")
720 && !groups[g].getName().startsWith("JGroup"))
722 tooltipText.append(groups[g].getName());
725 if (groups[g].getDescription() != null)
727 tooltipText.append(": " + groups[g].getDescription());
733 // use aa to see if the mouse pointer is on a
734 if (av.isShowSequenceFeatures())
737 List<SequenceFeature> features = ap.getFeatureRenderer().findFeaturesAtRes(
738 sequence.getDatasetSequence(),
739 rpos = sequence.findPosition(res));
740 seqARep.appendFeatures(tooltipText, rpos, features,
741 this.ap.getSeqPanel().seqCanvas.fr.getMinMax());
743 if (tooltipText.length() == 6) // <html></html>
745 setToolTipText(null);
750 if (lastTooltip == null
751 || !lastTooltip.equals(tooltipText.toString()))
753 String formatedTooltipText = JvSwingUtils.wrapTooltip(true,
754 tooltipText.toString());
755 // String formatedTooltipText = tooltipText.toString();
756 setToolTipText(formatedTooltipText);
757 lastTooltip = tooltipText.toString();
764 private Point lastp = null;
769 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
771 public Point getToolTipLocation(MouseEvent event)
773 int x = event.getX(), w = getWidth();
774 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
777 if (!event.isShiftDown() || p == null)
779 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
780 event.getX() + wdth, event.getY() - 20) : null;
783 * TODO: try to modify position region is not obcured by tooltip
791 * Set status message in alignment panel
794 * aligned sequence object
798 * index of sequence in alignment
799 * @return position of res in sequence
801 int setStatusMessage(SequenceI sequence, int res, int seq)
803 StringBuilder text = new StringBuilder(32);
806 * Sequence number (if known), and sequence name.
808 String seqno = seq == -1 ? "" : " " + (seq + 1);
809 text.append("Sequence" + seqno + " ID: " + sequence.getName());
811 String residue = null;
813 * Try to translate the display character to residue name (null for gap).
815 final String displayChar = String.valueOf(sequence.getCharAt(res));
816 if (av.getAlignment().isNucleotide())
818 residue = ResidueProperties.nucleotideName.get(displayChar);
821 text.append(" Nucleotide: ").append(residue);
826 residue = "X".equalsIgnoreCase(displayChar) ? "X"
827 : ResidueProperties.aa2Triplet.get(displayChar);
830 text.append(" Residue: ").append(residue);
837 pos = sequence.findPosition(res);
838 text.append(" (").append(Integer.toString(pos)).append(")");
840 ap.alignFrame.statusBar.setText(text.toString());
845 * Set the status bar message to highlight the first matched position in
850 private void setStatusMessage(SearchResults results)
852 List<Match> matches = results.getResults();
853 if (!matches.isEmpty())
855 Match m = matches.get(0);
856 SequenceI seq = m.getSequence();
857 int sequenceIndex = this.av.getAlignment().findIndex(seq);
860 * Convert position in sequence (base 1) to sequence character array index
863 int start = m.getStart() - 1;
864 setStatusMessage(seq, start, sequenceIndex);
875 public void mouseDragged(MouseEvent evt)
877 if (mouseWheelPressed)
879 int oldWidth = av.getCharWidth();
881 // Which is bigger, left-right or up-down?
882 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
883 .getX() - lastMousePress.getX()))
885 int fontSize = av.font.getSize();
887 if (evt.getY() < lastMousePress.getY())
891 else if (evt.getY() > lastMousePress.getY())
902 new Font(av.font.getName(), av.font.getStyle(), fontSize),
904 av.setCharWidth(oldWidth);
909 if (evt.getX() < lastMousePress.getX() && av.getCharWidth() > 1)
911 av.setCharWidth(av.getCharWidth() - 1);
913 else if (evt.getX() > lastMousePress.getX())
915 av.setCharWidth(av.getCharWidth() + 1);
918 ap.paintAlignment(false);
921 FontMetrics fm = getFontMetrics(av.getFont());
922 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
924 lastMousePress = evt.getPoint();
931 doMouseDraggedDefineMode(evt);
935 int res = findRes(evt);
942 if ((lastres == -1) || (lastres == res))
947 if ((res < av.getAlignment().getWidth()) && (res < lastres))
949 // dragLeft, delete gap
950 editSequence(false, false, res);
954 editSequence(true, false, res);
957 mouseDragging = true;
958 if (scrollThread != null)
960 scrollThread.setEvent(evt);
964 // TODO: Make it more clever than many booleans
965 synchronized void editSequence(boolean insertGap, boolean editSeq,
970 boolean fixedColumns = false;
971 SequenceGroup sg = av.getSelectionGroup();
973 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
975 // No group, but the sequence may represent a group
976 if (!groupEditing && av.hasHiddenRows())
978 if (av.isHiddenRepSequence(seq))
980 sg = av.getRepresentedSequences(seq);
985 StringBuilder message = new StringBuilder(64);
988 message.append("Edit group:");
989 if (editCommand == null)
991 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
996 message.append("Edit sequence: " + seq.getName());
997 String label = seq.getName();
998 if (label.length() > 10)
1000 label = label.substring(0, 10);
1002 if (editCommand == null)
1004 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
1010 message.append(" insert ");
1014 message.append(" delete ");
1017 message.append(Math.abs(startres - lastres) + " gaps.");
1018 ap.alignFrame.statusBar.setText(message.toString());
1020 // Are we editing within a selection group?
1022 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1025 fixedColumns = true;
1027 // sg might be null as the user may only see 1 sequence,
1028 // but the sequence represents a group
1031 if (!av.isHiddenRepSequence(seq))
1036 sg = av.getRepresentedSequences(seq);
1039 fixedLeft = sg.getStartRes();
1040 fixedRight = sg.getEndRes();
1042 if ((startres < fixedLeft && lastres >= fixedLeft)
1043 || (startres >= fixedLeft && lastres < fixedLeft)
1044 || (startres > fixedRight && lastres <= fixedRight)
1045 || (startres <= fixedRight && lastres > fixedRight))
1051 if (fixedLeft > startres)
1053 fixedRight = fixedLeft - 1;
1056 else if (fixedRight < startres)
1058 fixedLeft = fixedRight;
1063 if (av.hasHiddenColumns())
1065 fixedColumns = true;
1066 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1067 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1069 if ((insertGap && startres > y1 && lastres < y1)
1070 || (!insertGap && startres < y2 && lastres > y2))
1076 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1077 // Selection spans a hidden region
1078 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1086 fixedRight = y2 - 1;
1093 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1094 int g, groupSize = vseqs.size();
1095 SequenceI[] groupSeqs = new SequenceI[groupSize];
1096 for (g = 0; g < groupSeqs.length; g++)
1098 groupSeqs[g] = vseqs.get(g);
1104 // If the user has selected the whole sequence, and is dragging to
1105 // the right, we can still extend the alignment and selectionGroup
1106 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1107 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1109 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1110 fixedRight = sg.getEndRes();
1113 // Is it valid with fixed columns??
1114 // Find the next gap before the end
1115 // of the visible region boundary
1116 boolean blank = false;
1117 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1121 for (g = 0; g < groupSize; g++)
1123 for (int j = 0; j < startres - lastres; j++)
1125 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1126 .getCharAt(fixedRight - j)))
1141 if (sg.getSize() == av.getAlignment().getHeight())
1143 if ((av.hasHiddenColumns() && startres < av
1144 .getColumnSelection().getHiddenBoundaryRight(startres)))
1150 int alWidth = av.getAlignment().getWidth();
1151 if (av.hasHiddenRows())
1153 int hwidth = av.getAlignment().getHiddenSequences()
1155 if (hwidth > alWidth)
1160 // We can still insert gaps if the selectionGroup
1161 // contains all the sequences
1162 sg.setEndRes(sg.getEndRes() + startres - lastres);
1163 fixedRight = alWidth + startres - lastres;
1174 else if (!insertGap)
1176 // / Are we able to delete?
1177 // ie are all columns blank?
1179 for (g = 0; g < groupSize; g++)
1181 for (int j = startres; j < lastres; j++)
1183 if (groupSeqs[g].getLength() <= j)
1188 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1190 // Not a gap, block edit not valid
1200 // dragging to the right
1201 if (fixedColumns && fixedRight != -1)
1203 for (int j = lastres; j < startres; j++)
1205 insertChar(j, groupSeqs, fixedRight);
1210 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1216 // dragging to the left
1217 if (fixedColumns && fixedRight != -1)
1219 for (int j = lastres; j > startres; j--)
1221 deleteChar(startres, groupSeqs, fixedRight);
1226 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1233 // ///Editing a single sequence///////////
1237 // dragging to the right
1238 if (fixedColumns && fixedRight != -1)
1240 for (int j = lastres; j < startres; j++)
1242 insertChar(j, new SequenceI[]
1243 { seq }, fixedRight);
1248 appendEdit(Action.INSERT_GAP, new SequenceI[]
1249 { seq }, lastres, startres - lastres);
1256 // dragging to the left
1257 if (fixedColumns && fixedRight != -1)
1259 for (int j = lastres; j > startres; j--)
1261 if (!Comparison.isGap(seq.getCharAt(startres)))
1266 deleteChar(startres, new SequenceI[]
1267 { seq }, fixedRight);
1272 // could be a keyboard edit trying to delete none gaps
1274 for (int m = startres; m < lastres; m++)
1276 if (!Comparison.isGap(seq.getCharAt(m)))
1285 appendEdit(Action.DELETE_GAP, new SequenceI[]
1286 { seq }, startres, max);
1291 {// insertGap==false AND editSeq==TRUE;
1292 if (fixedColumns && fixedRight != -1)
1294 for (int j = lastres; j < startres; j++)
1296 insertChar(j, new SequenceI[]
1297 { seq }, fixedRight);
1302 appendEdit(Action.INSERT_NUC, new SequenceI[]
1303 { seq }, lastres, startres - lastres);
1310 seqCanvas.repaint();
1313 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1315 int blankColumn = fixedColumn;
1316 for (int s = 0; s < seq.length; s++)
1318 // Find the next gap before the end of the visible region boundary
1319 // If lastCol > j, theres a boundary after the gap insertion
1321 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1323 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1325 // Theres a space, so break and insert the gap
1330 if (blankColumn <= j)
1332 blankColumn = fixedColumn;
1338 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1340 appendEdit(Action.INSERT_GAP, seq, j, 1);
1345 * Helper method to add and perform one edit action.
1352 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1356 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1357 av.getAlignment().getGapCharacter());
1359 editCommand.appendEdit(edit, av.getAlignment(),
1363 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1366 appendEdit(Action.DELETE_GAP, seq, j, 1);
1368 appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1);
1378 public void mouseEntered(MouseEvent e)
1385 if (scrollThread != null)
1387 scrollThread.running = false;
1388 scrollThread = null;
1399 public void mouseExited(MouseEvent e)
1401 if (av.getWrapAlignment())
1408 scrollThread = new ScrollThread();
1413 public void mouseClicked(MouseEvent evt)
1415 SequenceGroup sg = null;
1416 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1417 if (evt.getClickCount() > 1)
1419 sg = av.getSelectionGroup();
1420 if (sg != null && sg.getSize() == 1
1421 && sg.getEndRes() - sg.getStartRes() < 2)
1423 av.setSelectionGroup(null);
1426 List<SequenceFeature> features = seqCanvas.getFeatureRenderer().findFeaturesAtRes(
1427 sequence.getDatasetSequence(),
1428 sequence.findPosition(findRes(evt)));
1430 if (features != null && features.size()> 0)
1432 SearchResults highlight = new SearchResults();
1433 highlight.addResult(sequence, features.get(0).getBegin(),
1434 features.get(0).getEnd());
1435 seqCanvas.highlightSearchResults(highlight);
1437 if (features != null && features.size()> 0)
1439 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1440 { sequence }, features.toArray(new SequenceFeature[features.size()]), false, ap);
1442 seqCanvas.highlightSearchResults(null);
1448 public void mouseWheelMoved(MouseWheelEvent e)
1451 if (e.getWheelRotation() > 0)
1453 if (e.isShiftDown())
1455 ap.scrollRight(true);
1465 if (e.isShiftDown())
1467 ap.scrollRight(false);
1474 // TODO Update tooltip for new position.
1483 public void doMousePressedDefineMode(MouseEvent evt)
1485 int res = findRes(evt);
1486 int seq = findSeq(evt);
1489 startWrapBlock = wrappedBlock;
1491 if (av.getWrapAlignment() && seq > av.getAlignment().getHeight())
1493 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1494 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1495 MessageManager.getString("label.wrapped_view_no_edit"),
1496 JOptionPane.WARNING_MESSAGE);
1500 if (seq < 0 || res < 0)
1505 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1507 if ((sequence == null) || (res > sequence.getLength()))
1512 stretchGroup = av.getSelectionGroup();
1514 if (stretchGroup == null)
1516 stretchGroup = av.getAlignment().findGroup(sequence);
1518 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1519 && (res < stretchGroup.getEndRes()))
1521 av.setSelectionGroup(stretchGroup);
1525 stretchGroup = null;
1528 else if (!stretchGroup.getSequences(null).contains(sequence)
1529 || (stretchGroup.getStartRes() > res)
1530 || (stretchGroup.getEndRes() < res))
1532 stretchGroup = null;
1534 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1536 if (allGroups != null)
1538 for (int i = 0; i < allGroups.length; i++)
1540 if ((allGroups[i].getStartRes() <= res)
1541 && (allGroups[i].getEndRes() >= res))
1543 stretchGroup = allGroups[i];
1549 av.setSelectionGroup(stretchGroup);
1553 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1555 List<SequenceFeature> allFeatures = ap.getFeatureRenderer().findFeaturesAtRes(
1556 sequence.getDatasetSequence(), sequence.findPosition(res));
1557 Vector links = new Vector();
1558 for (SequenceFeature sf:allFeatures)
1560 if (sf.links != null)
1562 for (int j = 0; j < sf.links.size(); j++)
1564 links.addElement(sf.links.elementAt(j));
1569 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1570 pop.show(this, evt.getX(), evt.getY());
1576 seqCanvas.cursorX = findRes(evt);
1577 seqCanvas.cursorY = findSeq(evt);
1578 seqCanvas.repaint();
1582 if (stretchGroup == null)
1584 // Only if left mouse button do we want to change group sizes
1586 // define a new group here
1587 SequenceGroup sg = new SequenceGroup();
1588 sg.setStartRes(res);
1590 sg.addSequence(sequence, false);
1591 av.setSelectionGroup(sg);
1595 if (av.getConservationSelected())
1597 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1601 if (av.getAbovePIDThreshold())
1603 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1606 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1608 // Edit end res position of selected group
1609 changeEndRes = true;
1611 else if ((stretchGroup != null)
1612 && (stretchGroup.getStartRes() == res))
1614 // Edit end res position of selected group
1615 changeStartRes = true;
1617 stretchGroup.getWidth();
1620 seqCanvas.repaint();
1629 public void doMouseReleasedDefineMode(MouseEvent evt)
1631 if (stretchGroup == null)
1636 stretchGroup.recalcConservation(); // always do this - annotation has own
1638 if (stretchGroup.cs != null)
1640 stretchGroup.cs.alignmentChanged(stretchGroup,
1641 av.getHiddenRepSequences());
1643 if (stretchGroup.cs.conservationApplied())
1645 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1646 stretchGroup.getName());
1650 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1651 stretchGroup.getName());
1654 PaintRefresher.Refresh(this, av.getSequenceSetId());
1655 ap.paintAlignment(true);
1657 changeEndRes = false;
1658 changeStartRes = false;
1659 stretchGroup = null;
1669 public void doMouseDraggedDefineMode(MouseEvent evt)
1671 int res = findRes(evt);
1672 int y = findSeq(evt);
1674 if (wrappedBlock != startWrapBlock)
1679 if (stretchGroup == null)
1684 if (res >= av.getAlignment().getWidth())
1686 res = av.getAlignment().getWidth() - 1;
1689 if (stretchGroup.getEndRes() == res)
1691 // Edit end res position of selected group
1692 changeEndRes = true;
1694 else if (stretchGroup.getStartRes() == res)
1696 // Edit start res position of selected group
1697 changeStartRes = true;
1700 if (res < av.getStartRes())
1702 res = av.getStartRes();
1707 if (res > (stretchGroup.getStartRes() - 1))
1709 stretchGroup.setEndRes(res);
1712 else if (changeStartRes)
1714 if (res < (stretchGroup.getEndRes() + 1))
1716 stretchGroup.setStartRes(res);
1720 int dragDirection = 0;
1726 else if (y < oldSeq)
1731 while ((y != oldSeq) && (oldSeq > -1)
1732 && (y < av.getAlignment().getHeight()))
1734 // This routine ensures we don't skip any sequences, as the
1735 // selection is quite slow.
1736 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1738 oldSeq += dragDirection;
1745 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1747 if (stretchGroup.getSequences(null).contains(nextSeq))
1749 stretchGroup.deleteSequence(seq, false);
1755 stretchGroup.addSequence(seq, false);
1758 stretchGroup.addSequence(nextSeq, false);
1767 mouseDragging = true;
1769 if (scrollThread != null)
1771 scrollThread.setEvent(evt);
1774 seqCanvas.repaint();
1777 void scrollCanvas(MouseEvent evt)
1781 if (scrollThread != null)
1783 scrollThread.running = false;
1784 scrollThread = null;
1786 mouseDragging = false;
1790 if (scrollThread == null)
1792 scrollThread = new ScrollThread();
1795 mouseDragging = true;
1796 scrollThread.setEvent(evt);
1801 // this class allows scrolling off the bottom of the visible alignment
1802 class ScrollThread extends Thread
1806 boolean running = false;
1808 public ScrollThread()
1813 public void setEvent(MouseEvent e)
1818 public void stopScrolling()
1832 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1834 running = ap.scrollUp(true);
1837 if (mouseDragging && (evt.getY() >= getHeight())
1838 && (av.getAlignment().getHeight() > av.getEndSeq()))
1840 running = ap.scrollUp(false);
1843 if (mouseDragging && (evt.getX() < 0))
1845 running = ap.scrollRight(false);
1847 else if (mouseDragging && (evt.getX() >= getWidth()))
1849 running = ap.scrollRight(true);
1856 } catch (Exception ex)
1864 * modify current selection according to a received message.
1867 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1868 SelectionSource source)
1870 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1871 // handles selection messages...
1872 // TODO: extend config options to allow user to control if selections may be
1873 // shared between viewports.
1874 boolean iSentTheSelection = (av == source
1875 || (source instanceof AlignViewport && ((AlignmentViewport) source)
1876 .getSequenceSetId().equals(av.getSequenceSetId())));
1877 if (iSentTheSelection || !av.followSelection)
1883 * Ignore the selection if there is one of our own pending.
1885 if (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1891 * Check for selection in a view of which this one is a dna/protein
1894 if (selectionFromTranslation(seqsel, colsel, source))
1899 // do we want to thread this ? (contention with seqsel and colsel locks, I
1901 // rules are: colsel is copied if there is a real intersection between
1902 // sequence selection
1903 boolean repaint = false;
1904 boolean copycolsel = true;
1906 SequenceGroup sgroup = null;
1907 if (seqsel != null && seqsel.getSize() > 0)
1909 if (av.getAlignment() == null)
1911 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1912 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1913 + " 's alignment is NULL! returning immediately.");
1916 sgroup = seqsel.intersect(av.getAlignment(),
1917 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1918 if ((sgroup == null || sgroup.getSize() == 0)
1919 || (colsel == null || colsel.size() == 0))
1921 // don't copy columns if the region didn't intersect.
1925 if (sgroup != null && sgroup.getSize() > 0)
1927 av.setSelectionGroup(sgroup);
1931 av.setSelectionGroup(null);
1933 av.isSelectionGroupChanged(true);
1938 // the current selection is unset or from a previous message
1939 // so import the new colsel.
1940 if (colsel == null || colsel.size() == 0)
1942 if (av.getColumnSelection() != null)
1944 av.getColumnSelection().clear();
1950 // TODO: shift colSel according to the intersecting sequences
1951 if (av.getColumnSelection() == null)
1953 av.setColumnSelection(new ColumnSelection(colsel));
1957 av.getColumnSelection().setElementsFrom(colsel);
1960 av.isColSelChanged(true);
1965 && av.hasHiddenColumns()
1966 && (av.getColumnSelection() == null || av.getColumnSelection()
1967 .getHiddenColumns() == null))
1969 System.err.println("Bad things");
1971 if (repaint) // always true!
1973 // probably finessing with multiple redraws here
1974 PaintRefresher.Refresh(this, av.getSequenceSetId());
1975 // ap.paintAlignment(false);
1980 * If this panel is a cdna/protein translation view of the selection source,
1981 * tries to map the source selection to a local one, and returns true. Else
1988 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1989 ColumnSelection colsel, SelectionSource source)
1991 if (!(source instanceof AlignViewportI)) {
1994 final AlignViewportI sourceAv = (AlignViewportI) source;
1995 if (sourceAv.getCodingComplement() != av && av.getCodingComplement() != sourceAv)
2001 * Map sequence selection
2003 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
2004 av.setSelectionGroup(sg);
2005 av.isSelectionGroupChanged(true);
2008 * Map column selection
2010 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
2012 av.setColumnSelection(cs);
2013 av.isColSelChanged(true);
2015 PaintRefresher.Refresh(this, av.getSequenceSetId());