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.SearchResultMatchI;
31 import jalview.datamodel.SearchResults;
32 import jalview.datamodel.SearchResultsI;
33 import jalview.datamodel.Sequence;
34 import jalview.datamodel.SequenceFeature;
35 import jalview.datamodel.SequenceGroup;
36 import jalview.datamodel.SequenceI;
37 import jalview.io.SequenceAnnotationReport;
38 import jalview.renderer.ResidueShaderI;
39 import jalview.schemes.ResidueProperties;
40 import jalview.structure.SelectionListener;
41 import jalview.structure.SelectionSource;
42 import jalview.structure.SequenceListener;
43 import jalview.structure.StructureSelectionManager;
44 import jalview.structure.VamsasSource;
45 import jalview.util.Comparison;
46 import jalview.util.MappingUtils;
47 import jalview.util.MessageManager;
48 import jalview.util.Platform;
49 import jalview.viewmodel.AlignmentViewport;
51 import java.awt.BorderLayout;
52 import java.awt.Color;
54 import java.awt.FontMetrics;
55 import java.awt.Point;
56 import java.awt.event.MouseEvent;
57 import java.awt.event.MouseListener;
58 import java.awt.event.MouseMotionListener;
59 import java.awt.event.MouseWheelEvent;
60 import java.awt.event.MouseWheelListener;
61 import java.util.ArrayList;
62 import java.util.List;
64 import javax.swing.JPanel;
65 import javax.swing.SwingUtilities;
66 import javax.swing.ToolTipManager;
72 * @version $Revision: 1.130 $
74 public class SeqPanel extends JPanel implements MouseListener,
75 MouseMotionListener, MouseWheelListener, SequenceListener,
80 public SeqCanvas seqCanvas;
83 public AlignmentPanel ap;
85 protected int lastres;
87 protected int startseq;
89 protected AlignViewport av;
91 ScrollThread scrollThread = null;
93 boolean mouseDragging = false;
95 boolean editingSeqs = false;
97 boolean groupEditing = false;
99 // ////////////////////////////////////////
100 // ///Everything below this is for defining the boundary of the rubberband
101 // ////////////////////////////////////////
104 boolean changeEndSeq = false;
106 boolean changeStartSeq = false;
108 boolean changeEndRes = false;
110 boolean changeStartRes = false;
112 SequenceGroup stretchGroup = null;
114 boolean remove = false;
116 Point lastMousePress;
118 boolean mouseWheelPressed = false;
120 StringBuffer keyboardNo1;
122 StringBuffer keyboardNo2;
124 java.net.URL linkImageURL;
126 private final SequenceAnnotationReport seqARep;
128 StringBuilder tooltipText = new StringBuilder();
132 EditCommand editCommand;
134 StructureSelectionManager ssm;
136 SearchResultsI lastSearchResults;
139 * Creates a new SeqPanel object.
146 public SeqPanel(AlignViewport av, AlignmentPanel ap)
148 linkImageURL = getClass().getResource("/images/link.gif");
149 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
150 ToolTipManager.sharedInstance().registerComponent(this);
151 ToolTipManager.sharedInstance().setInitialDelay(0);
152 ToolTipManager.sharedInstance().setDismissDelay(10000);
154 setBackground(Color.white);
156 seqCanvas = new SeqCanvas(ap);
157 setLayout(new BorderLayout());
158 add(seqCanvas, BorderLayout.CENTER);
164 addMouseMotionListener(this);
165 addMouseListener(this);
166 addMouseWheelListener(this);
167 ssm = av.getStructureSelectionManager();
168 ssm.addStructureViewerListener(this);
169 ssm.addSelectionListener(this);
173 int startWrapBlock = -1;
175 int wrappedBlock = -1;
178 * Returns the aligned sequence position (base 0) at the mouse position, or
179 * the closest visible one
184 int findRes(MouseEvent evt)
189 if (av.getWrapAlignment())
192 int hgap = av.getCharHeight();
193 if (av.getScaleAboveWrapped())
195 hgap += av.getCharHeight();
198 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
199 + hgap + seqCanvas.getAnnotationHeight();
203 x -= seqCanvas.LABEL_WEST;
205 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
211 wrappedBlock = y / cHeight;
212 wrappedBlock += av.getRanges().getStartRes() / cwidth;
214 res = wrappedBlock * cwidth + x / av.getCharWidth();
219 if (x > seqCanvas.getX() + seqCanvas.getWidth())
221 // make sure we calculate relative to visible alignment, rather than
223 x = seqCanvas.getX() + seqCanvas.getWidth();
225 res = (x / av.getCharWidth()) + av.getRanges().getStartRes();
226 if (res > av.getRanges().getEndRes())
229 res = av.getRanges().getEndRes();
233 if (av.hasHiddenColumns())
235 res = av.getColumnSelection().adjustForHiddenColumns(res);
242 int findSeq(MouseEvent evt)
247 if (av.getWrapAlignment())
249 int hgap = av.getCharHeight();
250 if (av.getScaleAboveWrapped())
252 hgap += av.getCharHeight();
255 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
256 + hgap + seqCanvas.getAnnotationHeight();
260 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
265 seq = Math.min((y / av.getCharHeight())
266 + av.getRanges().getStartSeq(),
268 .getAlignment().getHeight() - 1);
275 * When all of a sequence of edits are complete, put the resulting edit list
276 * on the history stack (undo list), and reset flags for editing in progress.
282 if (editCommand != null && editCommand.getSize() > 0)
284 ap.alignFrame.addHistoryItem(editCommand);
285 av.firePropertyChange("alignment", null, av.getAlignment()
291 * Tidy up come what may...
296 groupEditing = false;
305 seqCanvas.cursorY = getKeyboardNo1() - 1;
309 void setCursorColumn()
311 seqCanvas.cursorX = getKeyboardNo1() - 1;
315 void setCursorRowAndColumn()
317 if (keyboardNo2 == null)
319 keyboardNo2 = new StringBuffer();
323 seqCanvas.cursorX = getKeyboardNo1() - 1;
324 seqCanvas.cursorY = getKeyboardNo2() - 1;
329 void setCursorPosition()
331 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
333 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
337 void moveCursor(int dx, int dy)
339 seqCanvas.cursorX += dx;
340 seqCanvas.cursorY += dy;
341 if (av.hasHiddenColumns()
342 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
344 int original = seqCanvas.cursorX - dx;
345 int maxWidth = av.getAlignment().getWidth();
347 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
348 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
350 seqCanvas.cursorX += dx;
353 if (seqCanvas.cursorX >= maxWidth
354 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
356 seqCanvas.cursorX = original;
363 void scrollToVisible()
365 if (seqCanvas.cursorX < 0)
367 seqCanvas.cursorX = 0;
369 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
371 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
374 if (seqCanvas.cursorY < 0)
376 seqCanvas.cursorY = 0;
378 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
380 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
384 if (av.getWrapAlignment())
386 ap.scrollToWrappedVisible(seqCanvas.cursorX);
390 while (seqCanvas.cursorY < av.getRanges().getStartSeq())
394 while (seqCanvas.cursorY + 1 > av.getRanges().getEndSeq())
398 if (!av.getWrapAlignment())
400 while (seqCanvas.cursorX < av.getColumnSelection()
401 .adjustForHiddenColumns(av.getRanges().getStartRes()))
403 if (!ap.scrollRight(false))
408 while (seqCanvas.cursorX > av.getColumnSelection()
409 .adjustForHiddenColumns(av.getRanges().getEndRes()))
411 if (!ap.scrollRight(true))
418 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
419 seqCanvas.cursorX, seqCanvas.cursorY);
424 void setSelectionAreaAtCursor(boolean topLeft)
426 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
428 if (av.getSelectionGroup() != null)
430 SequenceGroup sg = av.getSelectionGroup();
431 // Find the top and bottom of this group
432 int min = av.getAlignment().getHeight(), max = 0;
433 for (int i = 0; i < sg.getSize(); i++)
435 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
450 sg.setStartRes(seqCanvas.cursorX);
451 if (sg.getEndRes() < seqCanvas.cursorX)
453 sg.setEndRes(seqCanvas.cursorX);
456 min = seqCanvas.cursorY;
460 sg.setEndRes(seqCanvas.cursorX);
461 if (sg.getStartRes() > seqCanvas.cursorX)
463 sg.setStartRes(seqCanvas.cursorX);
466 max = seqCanvas.cursorY + 1;
471 // Only the user can do this
472 av.setSelectionGroup(null);
476 // Now add any sequences between min and max
477 sg.getSequences(null).clear();
478 for (int i = min; i < max; i++)
480 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
485 if (av.getSelectionGroup() == null)
487 SequenceGroup sg = new SequenceGroup();
488 sg.setStartRes(seqCanvas.cursorX);
489 sg.setEndRes(seqCanvas.cursorX);
490 sg.addSequence(sequence, false);
491 av.setSelectionGroup(sg);
494 ap.paintAlignment(false);
498 void insertGapAtCursor(boolean group)
500 groupEditing = group;
501 startseq = seqCanvas.cursorY;
502 lastres = seqCanvas.cursorX;
503 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
507 void deleteGapAtCursor(boolean group)
509 groupEditing = group;
510 startseq = seqCanvas.cursorY;
511 lastres = seqCanvas.cursorX + getKeyboardNo1();
512 editSequence(false, false, seqCanvas.cursorX);
516 void insertNucAtCursor(boolean group, String nuc)
518 // TODO not called - delete?
519 groupEditing = group;
520 startseq = seqCanvas.cursorY;
521 lastres = seqCanvas.cursorX;
522 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
526 void numberPressed(char value)
528 if (keyboardNo1 == null)
530 keyboardNo1 = new StringBuffer();
533 if (keyboardNo2 != null)
535 keyboardNo2.append(value);
539 keyboardNo1.append(value);
547 if (keyboardNo1 != null)
549 int value = Integer.parseInt(keyboardNo1.toString());
553 } catch (Exception x)
564 if (keyboardNo2 != null)
566 int value = Integer.parseInt(keyboardNo2.toString());
570 } catch (Exception x)
584 public void mouseReleased(MouseEvent evt)
586 mouseDragging = false;
587 mouseWheelPressed = false;
589 if (evt.isPopupTrigger()) // Windows: mouseReleased
598 doMouseReleasedDefineMode(evt);
612 public void mousePressed(MouseEvent evt)
614 lastMousePress = evt.getPoint();
616 if (SwingUtilities.isMiddleMouseButton(evt))
618 mouseWheelPressed = true;
622 boolean isControlDown = Platform.isControlDown(evt);
623 if (evt.isShiftDown() || isControlDown)
633 doMousePressedDefineMode(evt);
637 int seq = findSeq(evt);
638 int res = findRes(evt);
640 if (seq < 0 || res < 0)
645 if ((seq < av.getAlignment().getHeight())
646 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
663 public void mouseOverSequence(SequenceI sequence, int index, int pos)
665 String tmp = sequence.hashCode() + " " + index + " " + pos;
667 if (lastMessage == null || !lastMessage.equals(tmp))
669 // System.err.println("mouseOver Sequence: "+tmp);
670 ssm.mouseOverSequence(sequence, index, pos, av);
676 * Highlight the mapped region described by the search results object (unless
677 * unchanged). This supports highlight of protein while mousing over linked
678 * cDNA and vice versa. The status bar is also updated to show the location of
679 * the start of the highlighted region.
682 public void highlightSequence(SearchResultsI results)
684 if (results == null || results.equals(lastSearchResults))
688 lastSearchResults = results;
690 if (av.isFollowHighlight())
693 * if scrollToPosition requires a scroll adjustment, this flag prevents
694 * another scroll event being propagated back to the originator
696 * @see AlignmentPanel#adjustmentValueChanged
698 ap.setDontScrollComplement(true);
699 if (ap.scrollToPosition(results, false))
701 seqCanvas.revalidate();
704 setStatusMessage(results);
705 seqCanvas.highlightSearchResults(results);
709 public VamsasSource getVamsasSource()
711 return this.ap == null ? null : this.ap.av;
715 public void updateColours(SequenceI seq, int index)
717 System.out.println("update the seqPanel colours");
728 public void mouseMoved(MouseEvent evt)
732 // This is because MacOSX creates a mouseMoved
733 // If control is down, other platforms will not.
737 int res = findRes(evt);
738 int seq = findSeq(evt);
740 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
745 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
747 if (res >= sequence.getLength())
752 pos = setStatusMessage(sequence, res, seq);
753 if (ssm != null && pos > -1)
755 mouseOverSequence(sequence, res, pos);
758 tooltipText.setLength(6); // Cuts the buffer back to <html>
760 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
763 for (int g = 0; g < groups.length; g++)
765 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
767 if (!groups[g].getName().startsWith("JTreeGroup")
768 && !groups[g].getName().startsWith("JGroup"))
770 tooltipText.append(groups[g].getName());
773 if (groups[g].getDescription() != null)
775 tooltipText.append(": " + groups[g].getDescription());
781 // use aa to see if the mouse pointer is on a
782 if (av.isShowSequenceFeatures())
785 List<SequenceFeature> features = ap.getFeatureRenderer()
786 .findFeaturesAtRes(sequence.getDatasetSequence(),
787 rpos = sequence.findPosition(res));
788 seqARep.appendFeatures(tooltipText, rpos, features,
789 this.ap.getSeqPanel().seqCanvas.fr.getMinMax());
791 if (tooltipText.length() == 6) // <html>
793 setToolTipText(null);
798 if (lastTooltip == null
799 || !lastTooltip.equals(tooltipText.toString()))
801 String formatedTooltipText = JvSwingUtils.wrapTooltip(true,
802 tooltipText.toString());
803 // String formatedTooltipText = tooltipText.toString();
804 setToolTipText(formatedTooltipText);
805 lastTooltip = tooltipText.toString();
812 private Point lastp = null;
817 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
820 public Point getToolTipLocation(MouseEvent event)
822 int x = event.getX(), w = getWidth();
823 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
826 if (!event.isShiftDown() || p == null)
828 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
829 event.getX() + wdth, event.getY() - 20) : null;
832 * TODO: try to modify position region is not obcured by tooltip
840 * set when the current UI interaction has resulted in a change that requires
841 * overview shading to be recalculated. this could be changed to something
842 * more expressive that indicates what actually has changed, so selective
843 * redraws can be applied
845 private boolean needOverviewUpdate = false; // TODO: refactor to avcontroller
848 * set if av.getSelectionGroup() refers to a group that is defined on the
849 * alignment view, rather than a transient selection
851 // private boolean editingDefinedGroup = false; // TODO: refactor to
852 // avcontroller or viewModel
855 * Set status message in alignment panel
858 * aligned sequence object
862 * index of sequence in alignment
863 * @return position of res in sequence
865 int setStatusMessage(SequenceI sequence, int res, int seq)
867 StringBuilder text = new StringBuilder(32);
870 * Sequence number (if known), and sequence name.
872 String seqno = seq == -1 ? "" : " " + (seq + 1);
873 text.append("Sequence").append(seqno).append(" ID: ")
874 .append(sequence.getName());
876 String residue = null;
878 * Try to translate the display character to residue name (null for gap).
880 final String displayChar = String.valueOf(sequence.getCharAt(res));
881 if (av.getAlignment().isNucleotide())
883 residue = ResidueProperties.nucleotideName.get(displayChar);
886 text.append(" Nucleotide: ").append(residue);
891 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
892 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
896 text.append(" Residue: ").append(residue);
903 pos = sequence.findPosition(res);
904 text.append(" (").append(Integer.toString(pos)).append(")");
906 ap.alignFrame.statusBar.setText(text.toString());
911 * Set the status bar message to highlight the first matched position in
916 private void setStatusMessage(SearchResultsI results)
918 AlignmentI al = this.av.getAlignment();
919 int sequenceIndex = al.findIndex(results);
920 if (sequenceIndex == -1)
924 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
925 for (SearchResultMatchI m : results.getResults())
927 SequenceI seq = m.getSequence();
928 if (seq.getDatasetSequence() != null)
930 seq = seq.getDatasetSequence();
936 * Convert position in sequence (base 1) to sequence character array
939 int start = m.getStart() - m.getSequence().getStart();
940 setStatusMessage(seq, start, sequenceIndex);
953 public void mouseDragged(MouseEvent evt)
955 if (mouseWheelPressed)
957 int oldWidth = av.getCharWidth();
959 // Which is bigger, left-right or up-down?
960 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
961 .getX() - lastMousePress.getX()))
963 int fontSize = av.font.getSize();
965 if (evt.getY() < lastMousePress.getY())
969 else if (evt.getY() > lastMousePress.getY())
980 new Font(av.font.getName(), av.font.getStyle(), fontSize),
982 av.setCharWidth(oldWidth);
987 if (evt.getX() < lastMousePress.getX() && av.getCharWidth() > 1)
989 av.setCharWidth(av.getCharWidth() - 1);
991 else if (evt.getX() > lastMousePress.getX())
993 av.setCharWidth(av.getCharWidth() + 1);
996 ap.paintAlignment(false);
999 FontMetrics fm = getFontMetrics(av.getFont());
1000 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
1002 lastMousePress = evt.getPoint();
1009 doMouseDraggedDefineMode(evt);
1013 int res = findRes(evt);
1020 if ((lastres == -1) || (lastres == res))
1025 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1027 // dragLeft, delete gap
1028 editSequence(false, false, res);
1032 editSequence(true, false, res);
1035 mouseDragging = true;
1036 if (scrollThread != null)
1038 scrollThread.setEvent(evt);
1042 // TODO: Make it more clever than many booleans
1043 synchronized void editSequence(boolean insertGap, boolean editSeq,
1047 int fixedRight = -1;
1048 boolean fixedColumns = false;
1049 SequenceGroup sg = av.getSelectionGroup();
1051 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1053 // No group, but the sequence may represent a group
1054 if (!groupEditing && av.hasHiddenRows())
1056 if (av.isHiddenRepSequence(seq))
1058 sg = av.getRepresentedSequences(seq);
1059 groupEditing = true;
1063 StringBuilder message = new StringBuilder(64);
1066 message.append("Edit group:");
1067 if (editCommand == null)
1069 editCommand = new EditCommand(
1070 MessageManager.getString("action.edit_group"));
1075 message.append("Edit sequence: " + seq.getName());
1076 String label = seq.getName();
1077 if (label.length() > 10)
1079 label = label.substring(0, 10);
1081 if (editCommand == null)
1083 editCommand = new EditCommand(MessageManager.formatMessage(
1084 "label.edit_params", new String[] { label }));
1090 message.append(" insert ");
1094 message.append(" delete ");
1097 message.append(Math.abs(startres - lastres) + " gaps.");
1098 ap.alignFrame.statusBar.setText(message.toString());
1100 // Are we editing within a selection group?
1102 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1105 fixedColumns = true;
1107 // sg might be null as the user may only see 1 sequence,
1108 // but the sequence represents a group
1111 if (!av.isHiddenRepSequence(seq))
1116 sg = av.getRepresentedSequences(seq);
1119 fixedLeft = sg.getStartRes();
1120 fixedRight = sg.getEndRes();
1122 if ((startres < fixedLeft && lastres >= fixedLeft)
1123 || (startres >= fixedLeft && lastres < fixedLeft)
1124 || (startres > fixedRight && lastres <= fixedRight)
1125 || (startres <= fixedRight && lastres > fixedRight))
1131 if (fixedLeft > startres)
1133 fixedRight = fixedLeft - 1;
1136 else if (fixedRight < startres)
1138 fixedLeft = fixedRight;
1143 if (av.hasHiddenColumns())
1145 fixedColumns = true;
1146 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1147 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1149 if ((insertGap && startres > y1 && lastres < y1)
1150 || (!insertGap && startres < y2 && lastres > y2))
1156 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1157 // Selection spans a hidden region
1158 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1166 fixedRight = y2 - 1;
1173 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1174 int g, groupSize = vseqs.size();
1175 SequenceI[] groupSeqs = new SequenceI[groupSize];
1176 for (g = 0; g < groupSeqs.length; g++)
1178 groupSeqs[g] = vseqs.get(g);
1184 // If the user has selected the whole sequence, and is dragging to
1185 // the right, we can still extend the alignment and selectionGroup
1186 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1187 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1189 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1190 fixedRight = sg.getEndRes();
1193 // Is it valid with fixed columns??
1194 // Find the next gap before the end
1195 // of the visible region boundary
1196 boolean blank = false;
1197 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1201 for (g = 0; g < groupSize; g++)
1203 for (int j = 0; j < startres - lastres; j++)
1205 if (!Comparison.isGap(groupSeqs[g].getCharAt(fixedRight - j)))
1220 if (sg.getSize() == av.getAlignment().getHeight())
1222 if ((av.hasHiddenColumns() && startres < av
1223 .getColumnSelection().getHiddenBoundaryRight(startres)))
1229 int alWidth = av.getAlignment().getWidth();
1230 if (av.hasHiddenRows())
1232 int hwidth = av.getAlignment().getHiddenSequences()
1234 if (hwidth > alWidth)
1239 // We can still insert gaps if the selectionGroup
1240 // contains all the sequences
1241 sg.setEndRes(sg.getEndRes() + startres - lastres);
1242 fixedRight = alWidth + startres - lastres;
1253 else if (!insertGap)
1255 // / Are we able to delete?
1256 // ie are all columns blank?
1258 for (g = 0; g < groupSize; g++)
1260 for (int j = startres; j < lastres; j++)
1262 if (groupSeqs[g].getLength() <= j)
1267 if (!Comparison.isGap(groupSeqs[g].getCharAt(j)))
1269 // Not a gap, block edit not valid
1279 // dragging to the right
1280 if (fixedColumns && fixedRight != -1)
1282 for (int j = lastres; j < startres; j++)
1284 insertChar(j, groupSeqs, fixedRight);
1289 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1295 // dragging to the left
1296 if (fixedColumns && fixedRight != -1)
1298 for (int j = lastres; j > startres; j--)
1300 deleteChar(startres, groupSeqs, fixedRight);
1305 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1312 // ///Editing a single sequence///////////
1316 // dragging to the right
1317 if (fixedColumns && fixedRight != -1)
1319 for (int j = lastres; j < startres; j++)
1321 insertChar(j, new SequenceI[] { seq }, fixedRight);
1326 appendEdit(Action.INSERT_GAP, new SequenceI[] { seq }, lastres,
1327 startres - lastres);
1334 // dragging to the left
1335 if (fixedColumns && fixedRight != -1)
1337 for (int j = lastres; j > startres; j--)
1339 if (!Comparison.isGap(seq.getCharAt(startres)))
1344 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1349 // could be a keyboard edit trying to delete none gaps
1351 for (int m = startres; m < lastres; m++)
1353 if (!Comparison.isGap(seq.getCharAt(m)))
1362 appendEdit(Action.DELETE_GAP, new SequenceI[] { seq },
1368 {// insertGap==false AND editSeq==TRUE;
1369 if (fixedColumns && fixedRight != -1)
1371 for (int j = lastres; j < startres; j++)
1373 insertChar(j, new SequenceI[] { seq }, fixedRight);
1378 appendEdit(Action.INSERT_NUC, new SequenceI[] { seq }, lastres,
1379 startres - lastres);
1386 seqCanvas.repaint();
1389 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1391 int blankColumn = fixedColumn;
1392 for (int s = 0; s < seq.length; s++)
1394 // Find the next gap before the end of the visible region boundary
1395 // If lastCol > j, theres a boundary after the gap insertion
1397 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1399 if (Comparison.isGap(seq[s].getCharAt(blankColumn)))
1401 // Theres a space, so break and insert the gap
1406 if (blankColumn <= j)
1408 blankColumn = fixedColumn;
1414 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1416 appendEdit(Action.INSERT_GAP, seq, j, 1);
1421 * Helper method to add and perform one edit action.
1428 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1432 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1433 av.getAlignment().getGapCharacter());
1435 editCommand.appendEdit(edit, av.getAlignment(), true, null);
1438 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1441 appendEdit(Action.DELETE_GAP, seq, j, 1);
1443 appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1);
1453 public void mouseEntered(MouseEvent e)
1460 if (scrollThread != null)
1462 scrollThread.running = false;
1463 scrollThread = null;
1474 public void mouseExited(MouseEvent e)
1476 if (av.getWrapAlignment())
1483 scrollThread = new ScrollThread();
1488 public void mouseClicked(MouseEvent evt)
1490 SequenceGroup sg = null;
1491 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1492 if (evt.getClickCount() > 1)
1494 sg = av.getSelectionGroup();
1495 if (sg != null && sg.getSize() == 1
1496 && sg.getEndRes() - sg.getStartRes() < 2)
1498 av.setSelectionGroup(null);
1501 List<SequenceFeature> features = seqCanvas.getFeatureRenderer()
1502 .findFeaturesAtRes(sequence.getDatasetSequence(),
1503 sequence.findPosition(findRes(evt)));
1505 if (features != null && features.size() > 0)
1507 SearchResultsI highlight = new SearchResults();
1508 highlight.addResult(sequence, features.get(0).getBegin(), features
1510 seqCanvas.highlightSearchResults(highlight);
1512 if (features != null && features.size() > 0)
1514 seqCanvas.getFeatureRenderer().amendFeatures(
1515 new SequenceI[] { sequence },
1516 features.toArray(new SequenceFeature[features.size()]),
1519 seqCanvas.highlightSearchResults(null);
1525 public void mouseWheelMoved(MouseWheelEvent e)
1528 if (e.getWheelRotation() > 0)
1530 if (e.isShiftDown())
1532 ap.scrollRight(true);
1542 if (e.isShiftDown())
1544 ap.scrollRight(false);
1551 // TODO Update tooltip for new position.
1560 public void doMousePressedDefineMode(MouseEvent evt)
1562 final int res = findRes(evt);
1563 final int seq = findSeq(evt);
1565 needOverviewUpdate = false;
1567 startWrapBlock = wrappedBlock;
1569 if (av.getWrapAlignment() && seq > av.getAlignment().getHeight())
1571 JvOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1572 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1573 MessageManager.getString("label.wrapped_view_no_edit"),
1574 JvOptionPane.WARNING_MESSAGE);
1578 if (seq < 0 || res < 0)
1583 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1585 if ((sequence == null) || (res > sequence.getLength()))
1590 stretchGroup = av.getSelectionGroup();
1592 if (stretchGroup == null || !stretchGroup.contains(sequence, res))
1594 stretchGroup = av.getAlignment().findGroup(sequence, res);
1595 if (stretchGroup != null)
1597 // only update the current selection if the popup menu has a group to
1599 av.setSelectionGroup(stretchGroup);
1603 if (evt.isPopupTrigger()) // Mac: mousePressed
1610 * defer right-mouse click handling to mouseReleased on Windows
1611 * (where isPopupTrigger() will answer true)
1612 * NB isRightMouseButton is also true for Cmd-click on Mac
1614 if (SwingUtilities.isRightMouseButton(evt) && !Platform.isAMac())
1621 seqCanvas.cursorX = findRes(evt);
1622 seqCanvas.cursorY = findSeq(evt);
1623 seqCanvas.repaint();
1627 if (stretchGroup == null)
1629 // Only if left mouse button do we want to change group sizes
1631 // define a new group here
1632 SequenceGroup sg = new SequenceGroup();
1633 sg.setStartRes(res);
1635 sg.addSequence(sequence, false);
1636 av.setSelectionGroup(sg);
1639 if (av.getConservationSelected())
1641 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1645 if (av.getAbovePIDThreshold())
1647 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1650 // TODO: stretchGroup will always be not null. Is this a merge error ?
1651 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1653 // Edit end res position of selected group
1654 changeEndRes = true;
1656 else if ((stretchGroup != null)
1657 && (stretchGroup.getStartRes() == res))
1659 // Edit end res position of selected group
1660 changeStartRes = true;
1662 stretchGroup.getWidth();
1665 seqCanvas.repaint();
1669 * Build and show a pop-up menu at the right-click mouse position
1675 void showPopupMenu(MouseEvent evt)
1677 final int res = findRes(evt);
1678 final int seq = findSeq(evt);
1679 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1680 List<SequenceFeature> allFeatures = ap.getFeatureRenderer()
1681 .findFeaturesAtRes(sequence.getDatasetSequence(),
1682 sequence.findPosition(res));
1683 List<String> links = new ArrayList<String>();
1684 for (SequenceFeature sf : allFeatures)
1686 if (sf.links != null)
1688 for (String link : sf.links)
1695 PopupMenu pop = new PopupMenu(ap, null, links);
1696 pop.show(this, evt.getX(), evt.getY());
1705 public void doMouseReleasedDefineMode(MouseEvent evt)
1707 if (stretchGroup == null)
1711 // always do this - annotation has own state
1712 // but defer colourscheme update until hidden sequences are passed in
1713 boolean vischange = stretchGroup.recalcConservation(true);
1714 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup();
1715 if (stretchGroup.cs != null)
1717 stretchGroup.cs.alignmentChanged(stretchGroup,
1718 av.getHiddenRepSequences());
1720 ResidueShaderI groupColourScheme = stretchGroup.getGroupColourScheme();
1721 String name = stretchGroup.getName();
1722 if (stretchGroup.cs.conservationApplied())
1724 SliderPanel.setConservationSlider(ap, groupColourScheme, name);
1726 if (stretchGroup.cs.getThreshold() > 0)
1728 SliderPanel.setPIDSliderSource(ap, groupColourScheme, name);
1731 PaintRefresher.Refresh(this, av.getSequenceSetId());
1732 ap.paintAlignment(needOverviewUpdate);
1733 needOverviewUpdate = false;
1734 changeEndRes = false;
1735 changeStartRes = false;
1736 stretchGroup = null;
1746 public void doMouseDraggedDefineMode(MouseEvent evt)
1748 int res = findRes(evt);
1749 int y = findSeq(evt);
1751 if (wrappedBlock != startWrapBlock)
1756 if (stretchGroup == null)
1761 if (res >= av.getAlignment().getWidth())
1763 res = av.getAlignment().getWidth() - 1;
1766 if (stretchGroup.getEndRes() == res)
1768 // Edit end res position of selected group
1769 changeEndRes = true;
1771 else if (stretchGroup.getStartRes() == res)
1773 // Edit start res position of selected group
1774 changeStartRes = true;
1777 if (res < av.getRanges().getStartRes())
1779 res = av.getRanges().getStartRes();
1784 if (res > (stretchGroup.getStartRes() - 1))
1786 stretchGroup.setEndRes(res);
1787 needOverviewUpdate |= av.isSelectionDefinedGroup();
1790 else if (changeStartRes)
1792 if (res < (stretchGroup.getEndRes() + 1))
1794 stretchGroup.setStartRes(res);
1795 needOverviewUpdate |= av.isSelectionDefinedGroup();
1799 int dragDirection = 0;
1805 else if (y < oldSeq)
1810 while ((y != oldSeq) && (oldSeq > -1)
1811 && (y < av.getAlignment().getHeight()))
1813 // This routine ensures we don't skip any sequences, as the
1814 // selection is quite slow.
1815 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1817 oldSeq += dragDirection;
1824 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1826 if (stretchGroup.getSequences(null).contains(nextSeq))
1828 stretchGroup.deleteSequence(seq, false);
1829 needOverviewUpdate |= av.isSelectionDefinedGroup();
1835 stretchGroup.addSequence(seq, false);
1838 stretchGroup.addSequence(nextSeq, false);
1839 needOverviewUpdate |= av.isSelectionDefinedGroup();
1848 mouseDragging = true;
1850 if (scrollThread != null)
1852 scrollThread.setEvent(evt);
1855 seqCanvas.repaint();
1858 void scrollCanvas(MouseEvent evt)
1862 if (scrollThread != null)
1864 scrollThread.running = false;
1865 scrollThread = null;
1867 mouseDragging = false;
1871 if (scrollThread == null)
1873 scrollThread = new ScrollThread();
1876 mouseDragging = true;
1877 scrollThread.setEvent(evt);
1882 // this class allows scrolling off the bottom of the visible alignment
1883 class ScrollThread extends Thread
1887 boolean running = false;
1889 public ScrollThread()
1894 public void setEvent(MouseEvent e)
1899 public void stopScrolling()
1913 if (mouseDragging && (evt.getY() < 0)
1914 && (av.getRanges().getStartSeq() > 0))
1916 running = ap.scrollUp(true);
1919 if (mouseDragging && (evt.getY() >= getHeight())
1920 && (av.getAlignment().getHeight() > av.getRanges()
1923 running = ap.scrollUp(false);
1926 if (mouseDragging && (evt.getX() < 0))
1928 running = ap.scrollRight(false);
1930 else if (mouseDragging && (evt.getX() >= getWidth()))
1932 running = ap.scrollRight(true);
1939 } catch (Exception ex)
1947 * modify current selection according to a received message.
1950 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1951 SelectionSource source)
1953 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1954 // handles selection messages...
1955 // TODO: extend config options to allow user to control if selections may be
1956 // shared between viewports.
1957 boolean iSentTheSelection = (av == source || (source instanceof AlignViewport && ((AlignmentViewport) source)
1958 .getSequenceSetId().equals(av.getSequenceSetId())));
1960 if (iSentTheSelection)
1962 // respond to our own event by updating dependent dialogs
1963 if (ap.getCalculationDialog() != null)
1965 ap.getCalculationDialog().validateCalcTypes();
1968 // process further ?
1969 if (!av.followSelection)
1976 * Ignore the selection if there is one of our own pending.
1978 if (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1984 * Check for selection in a view of which this one is a dna/protein
1987 if (selectionFromTranslation(seqsel, colsel, source))
1992 // do we want to thread this ? (contention with seqsel and colsel locks, I
1995 * only copy colsel if there is a real intersection between
1996 * sequence selection and this panel's alignment
1998 boolean repaint = false;
1999 boolean copycolsel = false;
2001 SequenceGroup sgroup = null;
2002 if (seqsel != null && seqsel.getSize() > 0)
2004 if (av.getAlignment() == null)
2006 Cache.log.warn("alignviewport av SeqSetId=" + av.getSequenceSetId()
2007 + " ViewId=" + av.getViewId()
2008 + " 's alignment is NULL! returning immediately.");
2011 sgroup = seqsel.intersect(av.getAlignment(),
2012 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
2013 if ((sgroup != null && sgroup.getSize() > 0))
2018 if (sgroup != null && sgroup.getSize() > 0)
2020 av.setSelectionGroup(sgroup);
2024 av.setSelectionGroup(null);
2026 av.isSelectionGroupChanged(true);
2031 // the current selection is unset or from a previous message
2032 // so import the new colsel.
2033 if (colsel == null || colsel.isEmpty())
2035 if (av.getColumnSelection() != null)
2037 av.getColumnSelection().clear();
2043 // TODO: shift colSel according to the intersecting sequences
2044 if (av.getColumnSelection() == null)
2046 av.setColumnSelection(new ColumnSelection(colsel));
2050 av.getColumnSelection().setElementsFrom(colsel);
2053 av.isColSelChanged(true);
2058 && av.hasHiddenColumns()
2059 && (av.getColumnSelection() == null || av.getColumnSelection()
2060 .getHiddenColumns() == null))
2062 System.err.println("Bad things");
2064 if (repaint) // always true!
2066 // probably finessing with multiple redraws here
2067 PaintRefresher.Refresh(this, av.getSequenceSetId());
2068 // ap.paintAlignment(false);
2071 // lastly, update dependent dialogs
2072 if (ap.getCalculationDialog() != null)
2074 ap.getCalculationDialog().validateCalcTypes();
2080 * If this panel is a cdna/protein translation view of the selection source,
2081 * tries to map the source selection to a local one, and returns true. Else
2088 protected boolean selectionFromTranslation(SequenceGroup seqsel,
2089 ColumnSelection colsel, SelectionSource source)
2091 if (!(source instanceof AlignViewportI))
2095 final AlignViewportI sourceAv = (AlignViewportI) source;
2096 if (sourceAv.getCodingComplement() != av
2097 && av.getCodingComplement() != sourceAv)
2103 * Map sequence selection
2105 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
2106 av.setSelectionGroup(sg);
2107 av.isSelectionGroupChanged(true);
2110 * Map column selection
2112 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
2114 av.setColumnSelection(cs);
2116 // lastly, update any dependent dialogs
2117 if (ap.getCalculationDialog() != null)
2119 ap.getCalculationDialog().validateCalcTypes();
2122 PaintRefresher.Refresh(this, av.getSequenceSetId());