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 jalview.api.AlignViewportI;
24 import jalview.bin.Cache;
25 import jalview.commands.EditCommand;
26 import jalview.commands.EditCommand.Action;
27 import jalview.commands.EditCommand.Edit;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.ColumnSelection;
30 import jalview.datamodel.SearchResults;
31 import jalview.datamodel.SearchResults.Match;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceFeature;
34 import jalview.datamodel.SequenceGroup;
35 import jalview.datamodel.SequenceI;
36 import jalview.io.SequenceAnnotationReport;
37 import jalview.schemes.ResidueProperties;
38 import jalview.structure.SelectionListener;
39 import jalview.structure.SelectionSource;
40 import jalview.structure.SequenceListener;
41 import jalview.structure.StructureSelectionManager;
42 import jalview.structure.VamsasSource;
43 import jalview.util.Comparison;
44 import jalview.util.MappingUtils;
45 import jalview.util.MessageManager;
46 import jalview.util.Platform;
47 import jalview.viewmodel.AlignmentViewport;
49 import java.awt.BorderLayout;
50 import java.awt.Color;
52 import java.awt.FontMetrics;
53 import java.awt.Point;
54 import java.awt.event.MouseEvent;
55 import java.awt.event.MouseListener;
56 import java.awt.event.MouseMotionListener;
57 import java.awt.event.MouseWheelEvent;
58 import java.awt.event.MouseWheelListener;
59 import java.util.ArrayList;
60 import java.util.List;
62 import javax.swing.JOptionPane;
63 import javax.swing.JPanel;
64 import javax.swing.SwingUtilities;
65 import javax.swing.ToolTipManager;
71 * @version $Revision: 1.130 $
73 public class SeqPanel extends JPanel implements MouseListener,
74 MouseMotionListener, MouseWheelListener, SequenceListener,
79 public SeqCanvas seqCanvas;
82 public AlignmentPanel ap;
84 protected int lastres;
86 protected int startseq;
88 protected AlignViewport av;
90 ScrollThread scrollThread = null;
92 boolean mouseDragging = false;
94 boolean editingSeqs = false;
96 boolean groupEditing = false;
98 // ////////////////////////////////////////
99 // ///Everything below this is for defining the boundary of the rubberband
100 // ////////////////////////////////////////
103 boolean changeEndSeq = false;
105 boolean changeStartSeq = false;
107 boolean changeEndRes = false;
109 boolean changeStartRes = false;
111 SequenceGroup stretchGroup = null;
113 boolean remove = false;
115 Point lastMousePress;
117 boolean mouseWheelPressed = false;
119 StringBuffer keyboardNo1;
121 StringBuffer keyboardNo2;
123 java.net.URL linkImageURL;
125 private final SequenceAnnotationReport seqARep;
127 StringBuffer tooltipText = new StringBuffer();
131 EditCommand editCommand;
133 StructureSelectionManager ssm;
135 SearchResults lastSearchResults;
138 * Creates a new SeqPanel object.
145 public SeqPanel(AlignViewport av, AlignmentPanel ap)
147 linkImageURL = getClass().getResource("/images/link.gif");
148 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
149 ToolTipManager.sharedInstance().registerComponent(this);
150 ToolTipManager.sharedInstance().setInitialDelay(0);
151 ToolTipManager.sharedInstance().setDismissDelay(10000);
153 setBackground(Color.white);
155 seqCanvas = new SeqCanvas(ap);
156 setLayout(new BorderLayout());
157 add(seqCanvas, BorderLayout.CENTER);
163 addMouseMotionListener(this);
164 addMouseListener(this);
165 addMouseWheelListener(this);
166 ssm = av.getStructureSelectionManager();
167 ssm.addStructureViewerListener(this);
168 ssm.addSelectionListener(this);
172 int startWrapBlock = -1;
174 int wrappedBlock = -1;
177 * Returns the aligned sequence position (base 0) at the mouse position, or
178 * the closest visible one
183 int findRes(MouseEvent evt)
188 if (av.getWrapAlignment())
191 int hgap = av.getCharHeight();
192 if (av.getScaleAboveWrapped())
194 hgap += av.getCharHeight();
197 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
198 + hgap + seqCanvas.getAnnotationHeight();
202 x -= seqCanvas.LABEL_WEST;
204 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
210 wrappedBlock = y / cHeight;
211 wrappedBlock += av.getStartRes() / cwidth;
213 res = wrappedBlock * cwidth + x / av.getCharWidth();
218 if (x > seqCanvas.getX() + seqCanvas.getWidth())
220 // make sure we calculate relative to visible alignment, rather than
222 x = seqCanvas.getX() + seqCanvas.getWidth();
224 res = (x / av.getCharWidth()) + av.getStartRes();
225 if (res > av.getEndRes())
228 res = av.getEndRes();
232 if (av.hasHiddenColumns())
234 res = av.getColumnSelection().adjustForHiddenColumns(res);
241 int findSeq(MouseEvent evt)
246 if (av.getWrapAlignment())
248 int hgap = av.getCharHeight();
249 if (av.getScaleAboveWrapped())
251 hgap += av.getCharHeight();
254 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
255 + hgap + seqCanvas.getAnnotationHeight();
259 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
264 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
265 .getAlignment().getHeight() - 1);
272 * When all of a sequence of edits are complete, put the resulting edit list
273 * on the history stack (undo list), and reset flags for editing in progress.
279 if (editCommand != null && editCommand.getSize() > 0)
281 ap.alignFrame.addHistoryItem(editCommand);
282 av.firePropertyChange("alignment", null, av.getAlignment()
288 * Tidy up come what may...
293 groupEditing = false;
302 seqCanvas.cursorY = getKeyboardNo1() - 1;
306 void setCursorColumn()
308 seqCanvas.cursorX = getKeyboardNo1() - 1;
312 void setCursorRowAndColumn()
314 if (keyboardNo2 == null)
316 keyboardNo2 = new StringBuffer();
320 seqCanvas.cursorX = getKeyboardNo1() - 1;
321 seqCanvas.cursorY = getKeyboardNo2() - 1;
326 void setCursorPosition()
328 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
330 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
334 void moveCursor(int dx, int dy)
336 seqCanvas.cursorX += dx;
337 seqCanvas.cursorY += dy;
338 if (av.hasHiddenColumns()
339 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
341 int original = seqCanvas.cursorX - dx;
342 int maxWidth = av.getAlignment().getWidth();
344 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
345 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
347 seqCanvas.cursorX += dx;
350 if (seqCanvas.cursorX >= maxWidth
351 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
353 seqCanvas.cursorX = original;
360 void scrollToVisible()
362 if (seqCanvas.cursorX < 0)
364 seqCanvas.cursorX = 0;
366 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
368 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
371 if (seqCanvas.cursorY < 0)
373 seqCanvas.cursorY = 0;
375 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
377 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
381 if (av.getWrapAlignment())
383 ap.scrollToWrappedVisible(seqCanvas.cursorX);
387 while (seqCanvas.cursorY < av.startSeq)
391 while (seqCanvas.cursorY + 1 > av.endSeq)
395 if (!av.getWrapAlignment())
397 while (seqCanvas.cursorX < av.getColumnSelection()
398 .adjustForHiddenColumns(av.startRes))
400 if (!ap.scrollRight(false))
405 while (seqCanvas.cursorX > av.getColumnSelection()
406 .adjustForHiddenColumns(av.endRes))
408 if (!ap.scrollRight(true))
415 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
416 seqCanvas.cursorX, seqCanvas.cursorY);
421 void setSelectionAreaAtCursor(boolean topLeft)
423 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
425 if (av.getSelectionGroup() != null)
427 SequenceGroup sg = av.getSelectionGroup();
428 // Find the top and bottom of this group
429 int min = av.getAlignment().getHeight(), max = 0;
430 for (int i = 0; i < sg.getSize(); i++)
432 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
447 sg.setStartRes(seqCanvas.cursorX);
448 if (sg.getEndRes() < seqCanvas.cursorX)
450 sg.setEndRes(seqCanvas.cursorX);
453 min = seqCanvas.cursorY;
457 sg.setEndRes(seqCanvas.cursorX);
458 if (sg.getStartRes() > seqCanvas.cursorX)
460 sg.setStartRes(seqCanvas.cursorX);
463 max = seqCanvas.cursorY + 1;
468 // Only the user can do this
469 av.setSelectionGroup(null);
473 // Now add any sequences between min and max
474 sg.getSequences(null).clear();
475 for (int i = min; i < max; i++)
477 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
482 if (av.getSelectionGroup() == null)
484 SequenceGroup sg = new SequenceGroup();
485 sg.setStartRes(seqCanvas.cursorX);
486 sg.setEndRes(seqCanvas.cursorX);
487 sg.addSequence(sequence, false);
488 av.setSelectionGroup(sg);
491 ap.paintAlignment(false);
495 void insertGapAtCursor(boolean group)
497 groupEditing = group;
498 startseq = seqCanvas.cursorY;
499 lastres = seqCanvas.cursorX;
500 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
504 void deleteGapAtCursor(boolean group)
506 groupEditing = group;
507 startseq = seqCanvas.cursorY;
508 lastres = seqCanvas.cursorX + getKeyboardNo1();
509 editSequence(false, false, seqCanvas.cursorX);
513 void insertNucAtCursor(boolean group, String nuc)
515 // TODO not called - delete?
516 groupEditing = group;
517 startseq = seqCanvas.cursorY;
518 lastres = seqCanvas.cursorX;
519 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
523 void numberPressed(char value)
525 if (keyboardNo1 == null)
527 keyboardNo1 = new StringBuffer();
530 if (keyboardNo2 != null)
532 keyboardNo2.append(value);
536 keyboardNo1.append(value);
544 if (keyboardNo1 != null)
546 int value = Integer.parseInt(keyboardNo1.toString());
550 } catch (Exception x)
561 if (keyboardNo2 != null)
563 int value = Integer.parseInt(keyboardNo2.toString());
567 } catch (Exception x)
581 public void mouseReleased(MouseEvent evt)
583 mouseDragging = false;
584 mouseWheelPressed = false;
586 if (evt.isPopupTrigger()) // Windows: mouseReleased
595 doMouseReleasedDefineMode(evt);
609 public void mousePressed(MouseEvent evt)
611 lastMousePress = evt.getPoint();
613 if (SwingUtilities.isMiddleMouseButton(evt))
615 mouseWheelPressed = true;
619 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
621 if (evt.isAltDown() || evt.isControlDown())
629 doMousePressedDefineMode(evt);
633 int seq = findSeq(evt);
634 int res = findRes(evt);
636 if (seq < 0 || res < 0)
641 if ((seq < av.getAlignment().getHeight())
642 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
659 public void mouseOverSequence(SequenceI sequence, int index, int pos)
661 String tmp = sequence.hashCode() + " " + index + " " + pos;
663 if (lastMessage == null || !lastMessage.equals(tmp))
665 // System.err.println("mouseOver Sequence: "+tmp);
666 ssm.mouseOverSequence(sequence, index, pos, av);
672 * Highlight the mapped region described by the search results object (unless
673 * unchanged). This supports highlight of protein while mousing over linked
674 * cDNA and vice versa. The status bar is also updated to show the location of
675 * the start of the highlighted region.
678 public void highlightSequence(SearchResults results)
680 if (results == null || results.equals(lastSearchResults))
684 lastSearchResults = results;
686 if (av.isFollowHighlight())
689 * if scrollToPosition requires a scroll adjustment, this flag prevents
690 * another scroll event being propagated back to the originator
692 * @see AlignmentPanel#adjustmentValueChanged
694 ap.setDontScrollComplement(true);
695 if (ap.scrollToPosition(results, false))
697 seqCanvas.revalidate();
700 setStatusMessage(results);
701 seqCanvas.highlightSearchResults(results);
705 public VamsasSource getVamsasSource()
707 return this.ap == null ? null : this.ap.av;
711 public void updateColours(SequenceI seq, int index)
713 System.out.println("update the seqPanel colours");
724 public void mouseMoved(MouseEvent evt)
728 // This is because MacOSX creates a mouseMoved
729 // If control is down, other platforms will not.
733 int res = findRes(evt);
734 int seq = findSeq(evt);
736 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
741 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
743 if (res >= sequence.getLength())
748 pos = setStatusMessage(sequence, res, seq);
749 if (ssm != null && pos > -1)
751 mouseOverSequence(sequence, res, pos);
754 tooltipText.setLength(6); // Cuts the buffer back to <html>
756 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
759 for (int g = 0; g < groups.length; g++)
761 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
763 if (!groups[g].getName().startsWith("JTreeGroup")
764 && !groups[g].getName().startsWith("JGroup"))
766 tooltipText.append(groups[g].getName());
769 if (groups[g].getDescription() != null)
771 tooltipText.append(": " + groups[g].getDescription());
777 // use aa to see if the mouse pointer is on a
778 if (av.isShowSequenceFeatures())
781 List<SequenceFeature> features = ap.getFeatureRenderer()
782 .findFeaturesAtRes(sequence.getDatasetSequence(),
783 rpos = sequence.findPosition(res));
784 seqARep.appendFeatures(tooltipText, rpos, features,
785 this.ap.getSeqPanel().seqCanvas.fr.getMinMax());
787 if (tooltipText.length() == 6) // <html></html>
789 setToolTipText(null);
794 if (lastTooltip == null
795 || !lastTooltip.equals(tooltipText.toString()))
797 String formatedTooltipText = JvSwingUtils.wrapTooltip(true,
798 tooltipText.toString());
799 // String formatedTooltipText = tooltipText.toString();
800 setToolTipText(formatedTooltipText);
801 lastTooltip = tooltipText.toString();
808 private Point lastp = null;
813 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
816 public Point getToolTipLocation(MouseEvent event)
818 int x = event.getX(), w = getWidth();
819 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
822 if (!event.isShiftDown() || p == null)
824 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
825 event.getX() + wdth, event.getY() - 20) : null;
828 * TODO: try to modify position region is not obcured by tooltip
836 * set when the current UI interaction has resulted in a change that requires
837 * overview shading to be recalculated. this could be changed to something
838 * more expressive that indicates what actually has changed, so selective
839 * redraws can be applied
841 private boolean needOverviewUpdate = false; // TODO: refactor to avcontroller
844 * set if av.getSelectionGroup() refers to a group that is defined on the
845 * alignment view, rather than a transient selection
847 // private boolean editingDefinedGroup = false; // TODO: refactor to
848 // avcontroller or viewModel
851 * Set status message in alignment panel
854 * aligned sequence object
858 * index of sequence in alignment
859 * @return position of res in sequence
861 int setStatusMessage(SequenceI sequence, int res, int seq)
863 StringBuilder text = new StringBuilder(32);
866 * Sequence number (if known), and sequence name.
868 String seqno = seq == -1 ? "" : " " + (seq + 1);
869 text.append("Sequence").append(seqno).append(" ID: ")
870 .append(sequence.getName());
872 String residue = null;
874 * Try to translate the display character to residue name (null for gap).
876 final String displayChar = String.valueOf(sequence.getCharAt(res));
877 if (av.getAlignment().isNucleotide())
879 residue = ResidueProperties.nucleotideName.get(displayChar);
882 text.append(" Nucleotide: ").append(residue);
887 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
888 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
892 text.append(" Residue: ").append(residue);
899 pos = sequence.findPosition(res);
900 text.append(" (").append(Integer.toString(pos)).append(")");
902 ap.alignFrame.statusBar.setText(text.toString());
907 * Set the status bar message to highlight the first matched position in
912 private void setStatusMessage(SearchResults results)
914 AlignmentI al = this.av.getAlignment();
915 int sequenceIndex = al.findIndex(results);
916 if (sequenceIndex == -1)
920 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
921 for (Match m : results.getResults())
923 SequenceI seq = m.getSequence();
924 if (seq.getDatasetSequence() != null)
926 seq = seq.getDatasetSequence();
932 * Convert position in sequence (base 1) to sequence character array
935 int start = m.getStart() - m.getSequence().getStart();
936 setStatusMessage(seq, start, sequenceIndex);
949 public void mouseDragged(MouseEvent evt)
951 if (mouseWheelPressed)
953 int oldWidth = av.getCharWidth();
955 // Which is bigger, left-right or up-down?
956 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
957 .getX() - lastMousePress.getX()))
959 int fontSize = av.font.getSize();
961 if (evt.getY() < lastMousePress.getY())
965 else if (evt.getY() > lastMousePress.getY())
976 new Font(av.font.getName(), av.font.getStyle(), fontSize),
978 av.setCharWidth(oldWidth);
983 if (evt.getX() < lastMousePress.getX() && av.getCharWidth() > 1)
985 av.setCharWidth(av.getCharWidth() - 1);
987 else if (evt.getX() > lastMousePress.getX())
989 av.setCharWidth(av.getCharWidth() + 1);
992 ap.paintAlignment(false);
995 FontMetrics fm = getFontMetrics(av.getFont());
996 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
998 lastMousePress = evt.getPoint();
1005 doMouseDraggedDefineMode(evt);
1009 int res = findRes(evt);
1016 if ((lastres == -1) || (lastres == res))
1021 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1023 // dragLeft, delete gap
1024 editSequence(false, false, res);
1028 editSequence(true, false, res);
1031 mouseDragging = true;
1032 if (scrollThread != null)
1034 scrollThread.setEvent(evt);
1038 // TODO: Make it more clever than many booleans
1039 synchronized void editSequence(boolean insertGap, boolean editSeq,
1043 int fixedRight = -1;
1044 boolean fixedColumns = false;
1045 SequenceGroup sg = av.getSelectionGroup();
1047 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1049 // No group, but the sequence may represent a group
1050 if (!groupEditing && av.hasHiddenRows())
1052 if (av.isHiddenRepSequence(seq))
1054 sg = av.getRepresentedSequences(seq);
1055 groupEditing = true;
1059 StringBuilder message = new StringBuilder(64);
1062 message.append("Edit group:");
1063 if (editCommand == null)
1065 editCommand = new EditCommand(
1066 MessageManager.getString("action.edit_group"));
1071 message.append("Edit sequence: " + seq.getName());
1072 String label = seq.getName();
1073 if (label.length() > 10)
1075 label = label.substring(0, 10);
1077 if (editCommand == null)
1079 editCommand = new EditCommand(MessageManager.formatMessage(
1080 "label.edit_params", new String[] { label }));
1086 message.append(" insert ");
1090 message.append(" delete ");
1093 message.append(Math.abs(startres - lastres) + " gaps.");
1094 ap.alignFrame.statusBar.setText(message.toString());
1096 // Are we editing within a selection group?
1098 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1101 fixedColumns = true;
1103 // sg might be null as the user may only see 1 sequence,
1104 // but the sequence represents a group
1107 if (!av.isHiddenRepSequence(seq))
1112 sg = av.getRepresentedSequences(seq);
1115 fixedLeft = sg.getStartRes();
1116 fixedRight = sg.getEndRes();
1118 if ((startres < fixedLeft && lastres >= fixedLeft)
1119 || (startres >= fixedLeft && lastres < fixedLeft)
1120 || (startres > fixedRight && lastres <= fixedRight)
1121 || (startres <= fixedRight && lastres > fixedRight))
1127 if (fixedLeft > startres)
1129 fixedRight = fixedLeft - 1;
1132 else if (fixedRight < startres)
1134 fixedLeft = fixedRight;
1139 if (av.hasHiddenColumns())
1141 fixedColumns = true;
1142 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1143 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1145 if ((insertGap && startres > y1 && lastres < y1)
1146 || (!insertGap && startres < y2 && lastres > y2))
1152 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1153 // Selection spans a hidden region
1154 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1162 fixedRight = y2 - 1;
1169 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1170 int g, groupSize = vseqs.size();
1171 SequenceI[] groupSeqs = new SequenceI[groupSize];
1172 for (g = 0; g < groupSeqs.length; g++)
1174 groupSeqs[g] = vseqs.get(g);
1180 // If the user has selected the whole sequence, and is dragging to
1181 // the right, we can still extend the alignment and selectionGroup
1182 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1183 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1185 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1186 fixedRight = sg.getEndRes();
1189 // Is it valid with fixed columns??
1190 // Find the next gap before the end
1191 // of the visible region boundary
1192 boolean blank = false;
1193 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1197 for (g = 0; g < groupSize; g++)
1199 for (int j = 0; j < startres - lastres; j++)
1201 if (!Comparison.isGap(groupSeqs[g].getCharAt(fixedRight - j)))
1216 if (sg.getSize() == av.getAlignment().getHeight())
1218 if ((av.hasHiddenColumns() && startres < av
1219 .getColumnSelection().getHiddenBoundaryRight(startres)))
1225 int alWidth = av.getAlignment().getWidth();
1226 if (av.hasHiddenRows())
1228 int hwidth = av.getAlignment().getHiddenSequences()
1230 if (hwidth > alWidth)
1235 // We can still insert gaps if the selectionGroup
1236 // contains all the sequences
1237 sg.setEndRes(sg.getEndRes() + startres - lastres);
1238 fixedRight = alWidth + startres - lastres;
1249 else if (!insertGap)
1251 // / Are we able to delete?
1252 // ie are all columns blank?
1254 for (g = 0; g < groupSize; g++)
1256 for (int j = startres; j < lastres; j++)
1258 if (groupSeqs[g].getLength() <= j)
1263 if (!Comparison.isGap(groupSeqs[g].getCharAt(j)))
1265 // Not a gap, block edit not valid
1275 // dragging to the right
1276 if (fixedColumns && fixedRight != -1)
1278 for (int j = lastres; j < startres; j++)
1280 insertChar(j, groupSeqs, fixedRight);
1285 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1291 // dragging to the left
1292 if (fixedColumns && fixedRight != -1)
1294 for (int j = lastres; j > startres; j--)
1296 deleteChar(startres, groupSeqs, fixedRight);
1301 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1308 // ///Editing a single sequence///////////
1312 // dragging to the right
1313 if (fixedColumns && fixedRight != -1)
1315 for (int j = lastres; j < startres; j++)
1317 insertChar(j, new SequenceI[] { seq }, fixedRight);
1322 appendEdit(Action.INSERT_GAP, new SequenceI[] { seq }, lastres,
1323 startres - lastres);
1330 // dragging to the left
1331 if (fixedColumns && fixedRight != -1)
1333 for (int j = lastres; j > startres; j--)
1335 if (!Comparison.isGap(seq.getCharAt(startres)))
1340 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1345 // could be a keyboard edit trying to delete none gaps
1347 for (int m = startres; m < lastres; m++)
1349 if (!Comparison.isGap(seq.getCharAt(m)))
1358 appendEdit(Action.DELETE_GAP, new SequenceI[] { seq },
1364 {// insertGap==false AND editSeq==TRUE;
1365 if (fixedColumns && fixedRight != -1)
1367 for (int j = lastres; j < startres; j++)
1369 insertChar(j, new SequenceI[] { seq }, fixedRight);
1374 appendEdit(Action.INSERT_NUC, new SequenceI[] { seq }, lastres,
1375 startres - lastres);
1382 seqCanvas.repaint();
1385 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1387 int blankColumn = fixedColumn;
1388 for (int s = 0; s < seq.length; s++)
1390 // Find the next gap before the end of the visible region boundary
1391 // If lastCol > j, theres a boundary after the gap insertion
1393 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1395 if (Comparison.isGap(seq[s].getCharAt(blankColumn)))
1397 // Theres a space, so break and insert the gap
1402 if (blankColumn <= j)
1404 blankColumn = fixedColumn;
1410 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1412 appendEdit(Action.INSERT_GAP, seq, j, 1);
1417 * Helper method to add and perform one edit action.
1424 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1428 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1429 av.getAlignment().getGapCharacter());
1431 editCommand.appendEdit(edit, av.getAlignment(), true, null);
1434 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1437 appendEdit(Action.DELETE_GAP, seq, j, 1);
1439 appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1);
1449 public void mouseEntered(MouseEvent e)
1456 if (scrollThread != null)
1458 scrollThread.running = false;
1459 scrollThread = null;
1470 public void mouseExited(MouseEvent e)
1472 if (av.getWrapAlignment())
1479 scrollThread = new ScrollThread();
1484 public void mouseClicked(MouseEvent evt)
1486 SequenceGroup sg = null;
1487 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1488 if (evt.getClickCount() > 1)
1490 sg = av.getSelectionGroup();
1491 if (sg != null && sg.getSize() == 1
1492 && sg.getEndRes() - sg.getStartRes() < 2)
1494 av.setSelectionGroup(null);
1497 List<SequenceFeature> features = seqCanvas.getFeatureRenderer()
1498 .findFeaturesAtRes(sequence.getDatasetSequence(),
1499 sequence.findPosition(findRes(evt)));
1501 if (features != null && features.size() > 0)
1503 SearchResults highlight = new SearchResults();
1504 highlight.addResult(sequence, features.get(0).getBegin(), features
1506 seqCanvas.highlightSearchResults(highlight);
1508 if (features != null && features.size() > 0)
1510 seqCanvas.getFeatureRenderer().amendFeatures(
1511 new SequenceI[] { sequence },
1512 features.toArray(new SequenceFeature[features.size()]),
1515 seqCanvas.highlightSearchResults(null);
1521 public void mouseWheelMoved(MouseWheelEvent e)
1524 if (e.getWheelRotation() > 0)
1526 if (e.isShiftDown())
1528 ap.scrollRight(true);
1538 if (e.isShiftDown())
1540 ap.scrollRight(false);
1547 // TODO Update tooltip for new position.
1556 public void doMousePressedDefineMode(MouseEvent evt)
1558 final int res = findRes(evt);
1559 final int seq = findSeq(evt);
1561 needOverviewUpdate = false;
1563 startWrapBlock = wrappedBlock;
1565 if (av.getWrapAlignment() && seq > av.getAlignment().getHeight())
1567 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1568 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1569 MessageManager.getString("label.wrapped_view_no_edit"),
1570 JOptionPane.WARNING_MESSAGE);
1574 if (seq < 0 || res < 0)
1579 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1581 if ((sequence == null) || (res > sequence.getLength()))
1586 stretchGroup = av.getSelectionGroup();
1588 if (stretchGroup == null)
1590 stretchGroup = av.getAlignment().findGroup(sequence);
1592 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1593 && (res < stretchGroup.getEndRes()))
1595 av.setSelectionGroup(stretchGroup);
1599 stretchGroup = null;
1602 else if (!stretchGroup.getSequences(null).contains(sequence)
1603 || (stretchGroup.getStartRes() > res)
1604 || (stretchGroup.getEndRes() < res))
1606 stretchGroup = null;
1608 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1610 if (allGroups != null)
1612 for (int i = 0; i < allGroups.length; i++)
1614 if ((allGroups[i].getStartRes() <= res)
1615 && (allGroups[i].getEndRes() >= res))
1617 stretchGroup = allGroups[i];
1623 av.setSelectionGroup(stretchGroup);
1626 if (evt.isPopupTrigger()) // Mac: mousePressed
1633 * defer right-mouse click handling to mouseReleased on Windows
1634 * (where isPopupTrigger() will answer true)
1635 * NB isRightMouseButton is also true for Cmd-click on Mac
1637 if (SwingUtilities.isRightMouseButton(evt) && !Platform.isAMac())
1644 seqCanvas.cursorX = findRes(evt);
1645 seqCanvas.cursorY = findSeq(evt);
1646 seqCanvas.repaint();
1650 if (stretchGroup == null)
1652 // Only if left mouse button do we want to change group sizes
1654 // define a new group here
1655 SequenceGroup sg = new SequenceGroup();
1656 sg.setStartRes(res);
1658 sg.addSequence(sequence, false);
1659 av.setSelectionGroup(sg);
1662 if (av.getConservationSelected())
1664 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1668 if (av.getAbovePIDThreshold())
1670 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1673 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1675 // Edit end res position of selected group
1676 changeEndRes = true;
1678 else if ((stretchGroup != null)
1679 && (stretchGroup.getStartRes() == res))
1681 // Edit end res position of selected group
1682 changeStartRes = true;
1684 stretchGroup.getWidth();
1687 seqCanvas.repaint();
1691 * Build and show a pop-up menu at the right-click mouse position
1697 void showPopupMenu(MouseEvent evt)
1699 final int res = findRes(evt);
1700 final int seq = findSeq(evt);
1701 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1702 List<SequenceFeature> allFeatures = ap.getFeatureRenderer()
1703 .findFeaturesAtRes(sequence.getDatasetSequence(),
1704 sequence.findPosition(res));
1705 List<String> links = new ArrayList<String>();
1706 for (SequenceFeature sf : allFeatures)
1708 if (sf.links != null)
1710 for (String link : sf.links)
1717 PopupMenu pop = new PopupMenu(ap, null, links);
1718 pop.show(this, evt.getX(), evt.getY());
1727 public void doMouseReleasedDefineMode(MouseEvent evt)
1729 if (stretchGroup == null)
1733 // always do this - annotation has own state
1734 // but defer colourscheme update until hidden sequences are passed in
1735 boolean vischange = stretchGroup.recalcConservation(true);
1736 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup();
1737 if (stretchGroup.cs != null)
1739 stretchGroup.cs.alignmentChanged(stretchGroup,
1740 av.getHiddenRepSequences());
1742 if (stretchGroup.cs.conservationApplied())
1744 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1745 stretchGroup.getName());
1749 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1750 stretchGroup.getName());
1753 PaintRefresher.Refresh(this, av.getSequenceSetId());
1754 ap.paintAlignment(needOverviewUpdate);
1755 needOverviewUpdate =false;
1756 changeEndRes = false;
1757 changeStartRes = false;
1758 stretchGroup = null;
1768 public void doMouseDraggedDefineMode(MouseEvent evt)
1770 int res = findRes(evt);
1771 int y = findSeq(evt);
1773 if (wrappedBlock != startWrapBlock)
1778 if (stretchGroup == null)
1783 if (res >= av.getAlignment().getWidth())
1785 res = av.getAlignment().getWidth() - 1;
1788 if (stretchGroup.getEndRes() == res)
1790 // Edit end res position of selected group
1791 changeEndRes = true;
1793 else if (stretchGroup.getStartRes() == res)
1795 // Edit start res position of selected group
1796 changeStartRes = true;
1799 if (res < av.getStartRes())
1801 res = av.getStartRes();
1806 if (res > (stretchGroup.getStartRes() - 1))
1808 stretchGroup.setEndRes(res);
1809 needOverviewUpdate |= av.isSelectionDefinedGroup();
1812 else if (changeStartRes)
1814 if (res < (stretchGroup.getEndRes() + 1))
1816 stretchGroup.setStartRes(res);
1817 needOverviewUpdate |= av.isSelectionDefinedGroup();
1821 int dragDirection = 0;
1827 else if (y < oldSeq)
1832 while ((y != oldSeq) && (oldSeq > -1)
1833 && (y < av.getAlignment().getHeight()))
1835 // This routine ensures we don't skip any sequences, as the
1836 // selection is quite slow.
1837 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1839 oldSeq += dragDirection;
1846 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1848 if (stretchGroup.getSequences(null).contains(nextSeq))
1850 stretchGroup.deleteSequence(seq, false);
1851 needOverviewUpdate |= av.isSelectionDefinedGroup();
1857 stretchGroup.addSequence(seq, false);
1860 stretchGroup.addSequence(nextSeq, false);
1861 needOverviewUpdate |= av.isSelectionDefinedGroup();
1870 mouseDragging = true;
1872 if (scrollThread != null)
1874 scrollThread.setEvent(evt);
1877 seqCanvas.repaint();
1880 void scrollCanvas(MouseEvent evt)
1884 if (scrollThread != null)
1886 scrollThread.running = false;
1887 scrollThread = null;
1889 mouseDragging = false;
1893 if (scrollThread == null)
1895 scrollThread = new ScrollThread();
1898 mouseDragging = true;
1899 scrollThread.setEvent(evt);
1904 // this class allows scrolling off the bottom of the visible alignment
1905 class ScrollThread extends Thread
1909 boolean running = false;
1911 public ScrollThread()
1916 public void setEvent(MouseEvent e)
1921 public void stopScrolling()
1935 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1937 running = ap.scrollUp(true);
1940 if (mouseDragging && (evt.getY() >= getHeight())
1941 && (av.getAlignment().getHeight() > av.getEndSeq()))
1943 running = ap.scrollUp(false);
1946 if (mouseDragging && (evt.getX() < 0))
1948 running = ap.scrollRight(false);
1950 else if (mouseDragging && (evt.getX() >= getWidth()))
1952 running = ap.scrollRight(true);
1959 } catch (Exception ex)
1967 * modify current selection according to a received message.
1970 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1971 SelectionSource source)
1973 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1974 // handles selection messages...
1975 // TODO: extend config options to allow user to control if selections may be
1976 // shared between viewports.
1977 boolean iSentTheSelection = (av == source || (source instanceof AlignViewport && ((AlignmentViewport) source)
1978 .getSequenceSetId().equals(av.getSequenceSetId())));
1979 if (iSentTheSelection || !av.followSelection)
1985 * Ignore the selection if there is one of our own pending.
1987 if (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1993 * Check for selection in a view of which this one is a dna/protein
1996 if (selectionFromTranslation(seqsel, colsel, source))
2001 // do we want to thread this ? (contention with seqsel and colsel locks, I
2004 * only copy colsel if there is a real intersection between
2005 * sequence selection and this panel's alignment
2007 boolean repaint = false;
2008 boolean copycolsel = false;
2010 SequenceGroup sgroup = null;
2011 if (seqsel != null && seqsel.getSize() > 0)
2013 if (av.getAlignment() == null)
2015 Cache.log.warn("alignviewport av SeqSetId="
2016 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
2017 + " 's alignment is NULL! returning immediately.");
2020 sgroup = seqsel.intersect(av.getAlignment(),
2021 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
2022 if ((sgroup != null && sgroup.getSize() > 0))
2027 if (sgroup != null && sgroup.getSize() > 0)
2029 av.setSelectionGroup(sgroup);
2033 av.setSelectionGroup(null);
2035 av.isSelectionGroupChanged(true);
2040 // the current selection is unset or from a previous message
2041 // so import the new colsel.
2042 if (colsel == null || colsel.isEmpty())
2044 if (av.getColumnSelection() != null)
2046 av.getColumnSelection().clear();
2052 // TODO: shift colSel according to the intersecting sequences
2053 if (av.getColumnSelection() == null)
2055 av.setColumnSelection(new ColumnSelection(colsel));
2059 av.getColumnSelection().setElementsFrom(colsel);
2062 av.isColSelChanged(true);
2067 && av.hasHiddenColumns()
2068 && (av.getColumnSelection() == null || av.getColumnSelection()
2069 .getHiddenColumns() == null))
2071 System.err.println("Bad things");
2073 if (repaint) // always true!
2075 // probably finessing with multiple redraws here
2076 PaintRefresher.Refresh(this, av.getSequenceSetId());
2077 // ap.paintAlignment(false);
2082 * If this panel is a cdna/protein translation view of the selection source,
2083 * tries to map the source selection to a local one, and returns true. Else
2090 protected boolean selectionFromTranslation(SequenceGroup seqsel,
2091 ColumnSelection colsel, SelectionSource source)
2093 if (!(source instanceof AlignViewportI))
2097 final AlignViewportI sourceAv = (AlignViewportI) source;
2098 if (sourceAv.getCodingComplement() != av
2099 && av.getCodingComplement() != sourceAv)
2105 * Map sequence selection
2107 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
2108 av.setSelectionGroup(sg);
2109 av.isSelectionGroupChanged(true);
2112 * Map column selection
2114 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
2116 av.setColumnSelection(cs);
2118 PaintRefresher.Refresh(this, av.getSequenceSetId());