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 boolean isControlDown = Platform.isControlDown(evt);
620 if (evt.isShiftDown() || isControlDown)
630 doMousePressedDefineMode(evt);
634 int seq = findSeq(evt);
635 int res = findRes(evt);
637 if (seq < 0 || res < 0)
642 if ((seq < av.getAlignment().getHeight())
643 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
660 public void mouseOverSequence(SequenceI sequence, int index, int pos)
662 String tmp = sequence.hashCode() + " " + index + " " + pos;
664 if (lastMessage == null || !lastMessage.equals(tmp))
666 // System.err.println("mouseOver Sequence: "+tmp);
667 ssm.mouseOverSequence(sequence, index, pos, av);
673 * Highlight the mapped region described by the search results object (unless
674 * unchanged). This supports highlight of protein while mousing over linked
675 * cDNA and vice versa. The status bar is also updated to show the location of
676 * the start of the highlighted region.
679 public void highlightSequence(SearchResults results)
681 if (results == null || results.equals(lastSearchResults))
685 lastSearchResults = results;
687 if (av.isFollowHighlight())
690 * if scrollToPosition requires a scroll adjustment, this flag prevents
691 * another scroll event being propagated back to the originator
693 * @see AlignmentPanel#adjustmentValueChanged
695 ap.setDontScrollComplement(true);
696 if (ap.scrollToPosition(results, false))
698 seqCanvas.revalidate();
701 setStatusMessage(results);
702 seqCanvas.highlightSearchResults(results);
706 public VamsasSource getVamsasSource()
708 return this.ap == null ? null : this.ap.av;
712 public void updateColours(SequenceI seq, int index)
714 System.out.println("update the seqPanel colours");
725 public void mouseMoved(MouseEvent evt)
729 // This is because MacOSX creates a mouseMoved
730 // If control is down, other platforms will not.
734 int res = findRes(evt);
735 int seq = findSeq(evt);
737 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
742 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
744 if (res >= sequence.getLength())
749 pos = setStatusMessage(sequence, res, seq);
750 if (ssm != null && pos > -1)
752 mouseOverSequence(sequence, res, pos);
755 tooltipText.setLength(6); // Cuts the buffer back to <html>
757 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
760 for (int g = 0; g < groups.length; g++)
762 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
764 if (!groups[g].getName().startsWith("JTreeGroup")
765 && !groups[g].getName().startsWith("JGroup"))
767 tooltipText.append(groups[g].getName());
770 if (groups[g].getDescription() != null)
772 tooltipText.append(": " + groups[g].getDescription());
778 // use aa to see if the mouse pointer is on a
779 if (av.isShowSequenceFeatures())
782 List<SequenceFeature> features = ap.getFeatureRenderer()
783 .findFeaturesAtRes(sequence.getDatasetSequence(),
784 rpos = sequence.findPosition(res));
785 seqARep.appendFeatures(tooltipText, rpos, features,
786 this.ap.getSeqPanel().seqCanvas.fr.getMinMax());
788 if (tooltipText.length() == 6) // <html></html>
790 setToolTipText(null);
795 if (lastTooltip == null
796 || !lastTooltip.equals(tooltipText.toString()))
798 String formatedTooltipText = JvSwingUtils.wrapTooltip(true,
799 tooltipText.toString());
800 // String formatedTooltipText = tooltipText.toString();
801 setToolTipText(formatedTooltipText);
802 lastTooltip = tooltipText.toString();
809 private Point lastp = null;
814 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
817 public Point getToolTipLocation(MouseEvent event)
819 int x = event.getX(), w = getWidth();
820 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
823 if (!event.isShiftDown() || p == null)
825 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
826 event.getX() + wdth, event.getY() - 20) : null;
829 * TODO: try to modify position region is not obcured by tooltip
837 * set when the current UI interaction has resulted in a change that requires
838 * overview shading to be recalculated. this could be changed to something
839 * more expressive that indicates what actually has changed, so selective
840 * redraws can be applied
842 private boolean needOverviewUpdate = false; // TODO: refactor to avcontroller
845 * set if av.getSelectionGroup() refers to a group that is defined on the
846 * alignment view, rather than a transient selection
848 // private boolean editingDefinedGroup = false; // TODO: refactor to
849 // avcontroller or viewModel
852 * Set status message in alignment panel
855 * aligned sequence object
859 * index of sequence in alignment
860 * @return position of res in sequence
862 int setStatusMessage(SequenceI sequence, int res, int seq)
864 StringBuilder text = new StringBuilder(32);
867 * Sequence number (if known), and sequence name.
869 String seqno = seq == -1 ? "" : " " + (seq + 1);
870 text.append("Sequence").append(seqno).append(" ID: ")
871 .append(sequence.getName());
873 String residue = null;
875 * Try to translate the display character to residue name (null for gap).
877 final String displayChar = String.valueOf(sequence.getCharAt(res));
878 if (av.getAlignment().isNucleotide())
880 residue = ResidueProperties.nucleotideName.get(displayChar);
883 text.append(" Nucleotide: ").append(residue);
888 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
889 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
893 text.append(" Residue: ").append(residue);
900 pos = sequence.findPosition(res);
901 text.append(" (").append(Integer.toString(pos)).append(")");
903 ap.alignFrame.statusBar.setText(text.toString());
908 * Set the status bar message to highlight the first matched position in
913 private void setStatusMessage(SearchResults results)
915 AlignmentI al = this.av.getAlignment();
916 int sequenceIndex = al.findIndex(results);
917 if (sequenceIndex == -1)
921 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
922 for (Match m : results.getResults())
924 SequenceI seq = m.getSequence();
925 if (seq.getDatasetSequence() != null)
927 seq = seq.getDatasetSequence();
933 * Convert position in sequence (base 1) to sequence character array
936 int start = m.getStart() - m.getSequence().getStart();
937 setStatusMessage(seq, start, sequenceIndex);
950 public void mouseDragged(MouseEvent evt)
952 if (mouseWheelPressed)
954 int oldWidth = av.getCharWidth();
956 // Which is bigger, left-right or up-down?
957 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
958 .getX() - lastMousePress.getX()))
960 int fontSize = av.font.getSize();
962 if (evt.getY() < lastMousePress.getY())
966 else if (evt.getY() > lastMousePress.getY())
977 new Font(av.font.getName(), av.font.getStyle(), fontSize),
979 av.setCharWidth(oldWidth);
984 if (evt.getX() < lastMousePress.getX() && av.getCharWidth() > 1)
986 av.setCharWidth(av.getCharWidth() - 1);
988 else if (evt.getX() > lastMousePress.getX())
990 av.setCharWidth(av.getCharWidth() + 1);
993 ap.paintAlignment(false);
996 FontMetrics fm = getFontMetrics(av.getFont());
997 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
999 lastMousePress = evt.getPoint();
1006 doMouseDraggedDefineMode(evt);
1010 int res = findRes(evt);
1017 if ((lastres == -1) || (lastres == res))
1022 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1024 // dragLeft, delete gap
1025 editSequence(false, false, res);
1029 editSequence(true, false, res);
1032 mouseDragging = true;
1033 if (scrollThread != null)
1035 scrollThread.setEvent(evt);
1039 // TODO: Make it more clever than many booleans
1040 synchronized void editSequence(boolean insertGap, boolean editSeq,
1044 int fixedRight = -1;
1045 boolean fixedColumns = false;
1046 SequenceGroup sg = av.getSelectionGroup();
1048 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1050 // No group, but the sequence may represent a group
1051 if (!groupEditing && av.hasHiddenRows())
1053 if (av.isHiddenRepSequence(seq))
1055 sg = av.getRepresentedSequences(seq);
1056 groupEditing = true;
1060 StringBuilder message = new StringBuilder(64);
1063 message.append("Edit group:");
1064 if (editCommand == null)
1066 editCommand = new EditCommand(
1067 MessageManager.getString("action.edit_group"));
1072 message.append("Edit sequence: " + seq.getName());
1073 String label = seq.getName();
1074 if (label.length() > 10)
1076 label = label.substring(0, 10);
1078 if (editCommand == null)
1080 editCommand = new EditCommand(MessageManager.formatMessage(
1081 "label.edit_params", new String[] { label }));
1087 message.append(" insert ");
1091 message.append(" delete ");
1094 message.append(Math.abs(startres - lastres) + " gaps.");
1095 ap.alignFrame.statusBar.setText(message.toString());
1097 // Are we editing within a selection group?
1099 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1102 fixedColumns = true;
1104 // sg might be null as the user may only see 1 sequence,
1105 // but the sequence represents a group
1108 if (!av.isHiddenRepSequence(seq))
1113 sg = av.getRepresentedSequences(seq);
1116 fixedLeft = sg.getStartRes();
1117 fixedRight = sg.getEndRes();
1119 if ((startres < fixedLeft && lastres >= fixedLeft)
1120 || (startres >= fixedLeft && lastres < fixedLeft)
1121 || (startres > fixedRight && lastres <= fixedRight)
1122 || (startres <= fixedRight && lastres > fixedRight))
1128 if (fixedLeft > startres)
1130 fixedRight = fixedLeft - 1;
1133 else if (fixedRight < startres)
1135 fixedLeft = fixedRight;
1140 if (av.hasHiddenColumns())
1142 fixedColumns = true;
1143 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1144 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1146 if ((insertGap && startres > y1 && lastres < y1)
1147 || (!insertGap && startres < y2 && lastres > y2))
1153 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1154 // Selection spans a hidden region
1155 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1163 fixedRight = y2 - 1;
1170 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1171 int g, groupSize = vseqs.size();
1172 SequenceI[] groupSeqs = new SequenceI[groupSize];
1173 for (g = 0; g < groupSeqs.length; g++)
1175 groupSeqs[g] = vseqs.get(g);
1181 // If the user has selected the whole sequence, and is dragging to
1182 // the right, we can still extend the alignment and selectionGroup
1183 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1184 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1186 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1187 fixedRight = sg.getEndRes();
1190 // Is it valid with fixed columns??
1191 // Find the next gap before the end
1192 // of the visible region boundary
1193 boolean blank = false;
1194 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1198 for (g = 0; g < groupSize; g++)
1200 for (int j = 0; j < startres - lastres; j++)
1202 if (!Comparison.isGap(groupSeqs[g].getCharAt(fixedRight - j)))
1217 if (sg.getSize() == av.getAlignment().getHeight())
1219 if ((av.hasHiddenColumns() && startres < av
1220 .getColumnSelection().getHiddenBoundaryRight(startres)))
1226 int alWidth = av.getAlignment().getWidth();
1227 if (av.hasHiddenRows())
1229 int hwidth = av.getAlignment().getHiddenSequences()
1231 if (hwidth > alWidth)
1236 // We can still insert gaps if the selectionGroup
1237 // contains all the sequences
1238 sg.setEndRes(sg.getEndRes() + startres - lastres);
1239 fixedRight = alWidth + startres - lastres;
1250 else if (!insertGap)
1252 // / Are we able to delete?
1253 // ie are all columns blank?
1255 for (g = 0; g < groupSize; g++)
1257 for (int j = startres; j < lastres; j++)
1259 if (groupSeqs[g].getLength() <= j)
1264 if (!Comparison.isGap(groupSeqs[g].getCharAt(j)))
1266 // Not a gap, block edit not valid
1276 // dragging to the right
1277 if (fixedColumns && fixedRight != -1)
1279 for (int j = lastres; j < startres; j++)
1281 insertChar(j, groupSeqs, fixedRight);
1286 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1292 // dragging to the left
1293 if (fixedColumns && fixedRight != -1)
1295 for (int j = lastres; j > startres; j--)
1297 deleteChar(startres, groupSeqs, fixedRight);
1302 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1309 // ///Editing a single sequence///////////
1313 // dragging to the right
1314 if (fixedColumns && fixedRight != -1)
1316 for (int j = lastres; j < startres; j++)
1318 insertChar(j, new SequenceI[] { seq }, fixedRight);
1323 appendEdit(Action.INSERT_GAP, new SequenceI[] { seq }, lastres,
1324 startres - lastres);
1331 // dragging to the left
1332 if (fixedColumns && fixedRight != -1)
1334 for (int j = lastres; j > startres; j--)
1336 if (!Comparison.isGap(seq.getCharAt(startres)))
1341 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1346 // could be a keyboard edit trying to delete none gaps
1348 for (int m = startres; m < lastres; m++)
1350 if (!Comparison.isGap(seq.getCharAt(m)))
1359 appendEdit(Action.DELETE_GAP, new SequenceI[] { seq },
1365 {// insertGap==false AND editSeq==TRUE;
1366 if (fixedColumns && fixedRight != -1)
1368 for (int j = lastres; j < startres; j++)
1370 insertChar(j, new SequenceI[] { seq }, fixedRight);
1375 appendEdit(Action.INSERT_NUC, new SequenceI[] { seq }, lastres,
1376 startres - lastres);
1383 seqCanvas.repaint();
1386 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1388 int blankColumn = fixedColumn;
1389 for (int s = 0; s < seq.length; s++)
1391 // Find the next gap before the end of the visible region boundary
1392 // If lastCol > j, theres a boundary after the gap insertion
1394 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1396 if (Comparison.isGap(seq[s].getCharAt(blankColumn)))
1398 // Theres a space, so break and insert the gap
1403 if (blankColumn <= j)
1405 blankColumn = fixedColumn;
1411 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1413 appendEdit(Action.INSERT_GAP, seq, j, 1);
1418 * Helper method to add and perform one edit action.
1425 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1429 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1430 av.getAlignment().getGapCharacter());
1432 editCommand.appendEdit(edit, av.getAlignment(), true, null);
1435 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1438 appendEdit(Action.DELETE_GAP, seq, j, 1);
1440 appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1);
1450 public void mouseEntered(MouseEvent e)
1457 if (scrollThread != null)
1459 scrollThread.running = false;
1460 scrollThread = null;
1471 public void mouseExited(MouseEvent e)
1473 if (av.getWrapAlignment())
1480 scrollThread = new ScrollThread();
1485 public void mouseClicked(MouseEvent evt)
1487 SequenceGroup sg = null;
1488 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1489 if (evt.getClickCount() > 1)
1491 sg = av.getSelectionGroup();
1492 if (sg != null && sg.getSize() == 1
1493 && sg.getEndRes() - sg.getStartRes() < 2)
1495 av.setSelectionGroup(null);
1498 List<SequenceFeature> features = seqCanvas.getFeatureRenderer()
1499 .findFeaturesAtRes(sequence.getDatasetSequence(),
1500 sequence.findPosition(findRes(evt)));
1502 if (features != null && features.size() > 0)
1504 SearchResults highlight = new SearchResults();
1505 highlight.addResult(sequence, features.get(0).getBegin(), features
1507 seqCanvas.highlightSearchResults(highlight);
1509 if (features != null && features.size() > 0)
1511 seqCanvas.getFeatureRenderer().amendFeatures(
1512 new SequenceI[] { sequence },
1513 features.toArray(new SequenceFeature[features.size()]),
1516 seqCanvas.highlightSearchResults(null);
1522 public void mouseWheelMoved(MouseWheelEvent e)
1525 if (e.getWheelRotation() > 0)
1527 if (e.isShiftDown())
1529 ap.scrollRight(true);
1539 if (e.isShiftDown())
1541 ap.scrollRight(false);
1548 // TODO Update tooltip for new position.
1557 public void doMousePressedDefineMode(MouseEvent evt)
1559 final int res = findRes(evt);
1560 final int seq = findSeq(evt);
1562 needOverviewUpdate = false;
1564 startWrapBlock = wrappedBlock;
1566 if (av.getWrapAlignment() && seq > av.getAlignment().getHeight())
1568 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1569 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1570 MessageManager.getString("label.wrapped_view_no_edit"),
1571 JOptionPane.WARNING_MESSAGE);
1575 if (seq < 0 || res < 0)
1580 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1582 if ((sequence == null) || (res > sequence.getLength()))
1587 stretchGroup = av.getSelectionGroup();
1589 if (stretchGroup == null)
1591 stretchGroup = av.getAlignment().findGroup(sequence);
1593 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1594 && (res < stretchGroup.getEndRes()))
1596 av.setSelectionGroup(stretchGroup);
1600 stretchGroup = null;
1603 else if (!stretchGroup.getSequences(null).contains(sequence)
1604 || (stretchGroup.getStartRes() > res)
1605 || (stretchGroup.getEndRes() < res))
1607 stretchGroup = null;
1609 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1611 if (allGroups != null)
1613 for (int i = 0; i < allGroups.length; i++)
1615 if ((allGroups[i].getStartRes() <= res)
1616 && (allGroups[i].getEndRes() >= res))
1618 stretchGroup = allGroups[i];
1624 av.setSelectionGroup(stretchGroup);
1627 if (evt.isPopupTrigger()) // Mac: mousePressed
1634 * defer right-mouse click handling to mouseReleased on Windows
1635 * (where isPopupTrigger() will answer true)
1636 * NB isRightMouseButton is also true for Cmd-click on Mac
1638 if (SwingUtilities.isRightMouseButton(evt) && !Platform.isAMac())
1645 seqCanvas.cursorX = findRes(evt);
1646 seqCanvas.cursorY = findSeq(evt);
1647 seqCanvas.repaint();
1651 if (stretchGroup == null)
1653 // Only if left mouse button do we want to change group sizes
1655 // define a new group here
1656 SequenceGroup sg = new SequenceGroup();
1657 sg.setStartRes(res);
1659 sg.addSequence(sequence, false);
1660 av.setSelectionGroup(sg);
1663 if (av.getConservationSelected())
1665 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1669 if (av.getAbovePIDThreshold())
1671 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1674 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1676 // Edit end res position of selected group
1677 changeEndRes = true;
1679 else if ((stretchGroup != null)
1680 && (stretchGroup.getStartRes() == res))
1682 // Edit end res position of selected group
1683 changeStartRes = true;
1685 stretchGroup.getWidth();
1688 seqCanvas.repaint();
1692 * Build and show a pop-up menu at the right-click mouse position
1698 void showPopupMenu(MouseEvent evt)
1700 final int res = findRes(evt);
1701 final int seq = findSeq(evt);
1702 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1703 List<SequenceFeature> allFeatures = ap.getFeatureRenderer()
1704 .findFeaturesAtRes(sequence.getDatasetSequence(),
1705 sequence.findPosition(res));
1706 List<String> links = new ArrayList<String>();
1707 for (SequenceFeature sf : allFeatures)
1709 if (sf.links != null)
1711 for (String link : sf.links)
1718 PopupMenu pop = new PopupMenu(ap, null, links);
1719 pop.show(this, evt.getX(), evt.getY());
1728 public void doMouseReleasedDefineMode(MouseEvent evt)
1730 if (stretchGroup == null)
1734 // always do this - annotation has own state
1735 // but defer colourscheme update until hidden sequences are passed in
1736 boolean vischange = stretchGroup.recalcConservation(true);
1737 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup();
1738 if (stretchGroup.cs != null)
1740 stretchGroup.cs.alignmentChanged(stretchGroup,
1741 av.getHiddenRepSequences());
1743 if (stretchGroup.cs.conservationApplied())
1745 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1746 stretchGroup.getName());
1750 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1751 stretchGroup.getName());
1754 PaintRefresher.Refresh(this, av.getSequenceSetId());
1755 ap.paintAlignment(needOverviewUpdate);
1756 needOverviewUpdate = false;
1757 changeEndRes = false;
1758 changeStartRes = false;
1759 stretchGroup = null;
1769 public void doMouseDraggedDefineMode(MouseEvent evt)
1771 int res = findRes(evt);
1772 int y = findSeq(evt);
1774 if (wrappedBlock != startWrapBlock)
1779 if (stretchGroup == null)
1784 if (res >= av.getAlignment().getWidth())
1786 res = av.getAlignment().getWidth() - 1;
1789 if (stretchGroup.getEndRes() == res)
1791 // Edit end res position of selected group
1792 changeEndRes = true;
1794 else if (stretchGroup.getStartRes() == res)
1796 // Edit start res position of selected group
1797 changeStartRes = true;
1800 if (res < av.getStartRes())
1802 res = av.getStartRes();
1807 if (res > (stretchGroup.getStartRes() - 1))
1809 stretchGroup.setEndRes(res);
1810 needOverviewUpdate |= av.isSelectionDefinedGroup();
1813 else if (changeStartRes)
1815 if (res < (stretchGroup.getEndRes() + 1))
1817 stretchGroup.setStartRes(res);
1818 needOverviewUpdate |= av.isSelectionDefinedGroup();
1822 int dragDirection = 0;
1828 else if (y < oldSeq)
1833 while ((y != oldSeq) && (oldSeq > -1)
1834 && (y < av.getAlignment().getHeight()))
1836 // This routine ensures we don't skip any sequences, as the
1837 // selection is quite slow.
1838 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1840 oldSeq += dragDirection;
1847 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1849 if (stretchGroup.getSequences(null).contains(nextSeq))
1851 stretchGroup.deleteSequence(seq, false);
1852 needOverviewUpdate |= av.isSelectionDefinedGroup();
1858 stretchGroup.addSequence(seq, false);
1861 stretchGroup.addSequence(nextSeq, false);
1862 needOverviewUpdate |= av.isSelectionDefinedGroup();
1871 mouseDragging = true;
1873 if (scrollThread != null)
1875 scrollThread.setEvent(evt);
1878 seqCanvas.repaint();
1881 void scrollCanvas(MouseEvent evt)
1885 if (scrollThread != null)
1887 scrollThread.running = false;
1888 scrollThread = null;
1890 mouseDragging = false;
1894 if (scrollThread == null)
1896 scrollThread = new ScrollThread();
1899 mouseDragging = true;
1900 scrollThread.setEvent(evt);
1905 // this class allows scrolling off the bottom of the visible alignment
1906 class ScrollThread extends Thread
1910 boolean running = false;
1912 public ScrollThread()
1917 public void setEvent(MouseEvent e)
1922 public void stopScrolling()
1936 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1938 running = ap.scrollUp(true);
1941 if (mouseDragging && (evt.getY() >= getHeight())
1942 && (av.getAlignment().getHeight() > av.getEndSeq()))
1944 running = ap.scrollUp(false);
1947 if (mouseDragging && (evt.getX() < 0))
1949 running = ap.scrollRight(false);
1951 else if (mouseDragging && (evt.getX() >= getWidth()))
1953 running = ap.scrollRight(true);
1960 } catch (Exception ex)
1968 * modify current selection according to a received message.
1971 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1972 SelectionSource source)
1974 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1975 // handles selection messages...
1976 // TODO: extend config options to allow user to control if selections may be
1977 // shared between viewports.
1978 boolean iSentTheSelection = (av == source || (source instanceof AlignViewport && ((AlignmentViewport) source)
1979 .getSequenceSetId().equals(av.getSequenceSetId())));
1980 if (iSentTheSelection || !av.followSelection)
1986 * Ignore the selection if there is one of our own pending.
1988 if (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1994 * Check for selection in a view of which this one is a dna/protein
1997 if (selectionFromTranslation(seqsel, colsel, source))
2002 // do we want to thread this ? (contention with seqsel and colsel locks, I
2005 * only copy colsel if there is a real intersection between
2006 * sequence selection and this panel's alignment
2008 boolean repaint = false;
2009 boolean copycolsel = false;
2011 SequenceGroup sgroup = null;
2012 if (seqsel != null && seqsel.getSize() > 0)
2014 if (av.getAlignment() == null)
2016 Cache.log.warn("alignviewport av SeqSetId=" + av.getSequenceSetId()
2017 + " ViewId=" + av.getViewId()
2018 + " 's alignment is NULL! returning immediately.");
2021 sgroup = seqsel.intersect(av.getAlignment(),
2022 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
2023 if ((sgroup != null && sgroup.getSize() > 0))
2028 if (sgroup != null && sgroup.getSize() > 0)
2030 av.setSelectionGroup(sgroup);
2034 av.setSelectionGroup(null);
2036 av.isSelectionGroupChanged(true);
2041 // the current selection is unset or from a previous message
2042 // so import the new colsel.
2043 if (colsel == null || colsel.isEmpty())
2045 if (av.getColumnSelection() != null)
2047 av.getColumnSelection().clear();
2053 // TODO: shift colSel according to the intersecting sequences
2054 if (av.getColumnSelection() == null)
2056 av.setColumnSelection(new ColumnSelection(colsel));
2060 av.getColumnSelection().setElementsFrom(colsel);
2063 av.isColSelChanged(true);
2068 && av.hasHiddenColumns()
2069 && (av.getColumnSelection() == null || av.getColumnSelection()
2070 .getHiddenColumns() == null))
2072 System.err.println("Bad things");
2074 if (repaint) // always true!
2076 // probably finessing with multiple redraws here
2077 PaintRefresher.Refresh(this, av.getSequenceSetId());
2078 // ap.paintAlignment(false);
2083 * If this panel is a cdna/protein translation view of the selection source,
2084 * tries to map the source selection to a local one, and returns true. Else
2091 protected boolean selectionFromTranslation(SequenceGroup seqsel,
2092 ColumnSelection colsel, SelectionSource source)
2094 if (!(source instanceof AlignViewportI))
2098 final AlignViewportI sourceAv = (AlignViewportI) source;
2099 if (sourceAv.getCodingComplement() != av
2100 && av.getCodingComplement() != sourceAv)
2106 * Map sequence selection
2108 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
2109 av.setSelectionGroup(sg);
2110 av.isSelectionGroupChanged(true);
2113 * Map column selection
2115 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
2117 av.setColumnSelection(cs);
2119 PaintRefresher.Refresh(this, av.getSequenceSetId());