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 avcontroller or viewModel
850 * Set status message in alignment panel
853 * aligned sequence object
857 * index of sequence in alignment
858 * @return position of res in sequence
860 int setStatusMessage(SequenceI sequence, int res, int seq)
862 StringBuilder text = new StringBuilder(32);
865 * Sequence number (if known), and sequence name.
867 String seqno = seq == -1 ? "" : " " + (seq + 1);
868 text.append("Sequence").append(seqno).append(" ID: ")
869 .append(sequence.getName());
871 String residue = null;
873 * Try to translate the display character to residue name (null for gap).
875 final String displayChar = String.valueOf(sequence.getCharAt(res));
876 if (av.getAlignment().isNucleotide())
878 residue = ResidueProperties.nucleotideName.get(displayChar);
881 text.append(" Nucleotide: ").append(residue);
886 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
887 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
891 text.append(" Residue: ").append(residue);
898 pos = sequence.findPosition(res);
899 text.append(" (").append(Integer.toString(pos)).append(")");
901 ap.alignFrame.statusBar.setText(text.toString());
906 * Set the status bar message to highlight the first matched position in
911 private void setStatusMessage(SearchResults results)
913 AlignmentI al = this.av.getAlignment();
914 int sequenceIndex = al.findIndex(results);
915 if (sequenceIndex == -1)
919 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
920 for (Match m : results.getResults())
922 SequenceI seq = m.getSequence();
923 if (seq.getDatasetSequence() != null)
925 seq = seq.getDatasetSequence();
931 * Convert position in sequence (base 1) to sequence character array
934 int start = m.getStart() - m.getSequence().getStart();
935 setStatusMessage(seq, start, sequenceIndex);
948 public void mouseDragged(MouseEvent evt)
950 if (mouseWheelPressed)
952 int oldWidth = av.getCharWidth();
954 // Which is bigger, left-right or up-down?
955 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
956 .getX() - lastMousePress.getX()))
958 int fontSize = av.font.getSize();
960 if (evt.getY() < lastMousePress.getY())
964 else if (evt.getY() > lastMousePress.getY())
975 new Font(av.font.getName(), av.font.getStyle(), fontSize),
977 av.setCharWidth(oldWidth);
982 if (evt.getX() < lastMousePress.getX() && av.getCharWidth() > 1)
984 av.setCharWidth(av.getCharWidth() - 1);
986 else if (evt.getX() > lastMousePress.getX())
988 av.setCharWidth(av.getCharWidth() + 1);
991 ap.paintAlignment(false);
994 FontMetrics fm = getFontMetrics(av.getFont());
995 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
997 lastMousePress = evt.getPoint();
1004 doMouseDraggedDefineMode(evt);
1008 int res = findRes(evt);
1015 if ((lastres == -1) || (lastres == res))
1020 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1022 // dragLeft, delete gap
1023 editSequence(false, false, res);
1027 editSequence(true, false, res);
1030 mouseDragging = true;
1031 if (scrollThread != null)
1033 scrollThread.setEvent(evt);
1037 // TODO: Make it more clever than many booleans
1038 synchronized void editSequence(boolean insertGap, boolean editSeq,
1042 int fixedRight = -1;
1043 boolean fixedColumns = false;
1044 SequenceGroup sg = av.getSelectionGroup();
1046 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1048 // No group, but the sequence may represent a group
1049 if (!groupEditing && av.hasHiddenRows())
1051 if (av.isHiddenRepSequence(seq))
1053 sg = av.getRepresentedSequences(seq);
1054 groupEditing = true;
1058 StringBuilder message = new StringBuilder(64);
1061 message.append("Edit group:");
1062 if (editCommand == null)
1064 editCommand = new EditCommand(
1065 MessageManager.getString("action.edit_group"));
1070 message.append("Edit sequence: " + seq.getName());
1071 String label = seq.getName();
1072 if (label.length() > 10)
1074 label = label.substring(0, 10);
1076 if (editCommand == null)
1078 editCommand = new EditCommand(MessageManager.formatMessage(
1079 "label.edit_params", new String[] { label }));
1085 message.append(" insert ");
1089 message.append(" delete ");
1092 message.append(Math.abs(startres - lastres) + " gaps.");
1093 ap.alignFrame.statusBar.setText(message.toString());
1095 // Are we editing within a selection group?
1097 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1100 fixedColumns = true;
1102 // sg might be null as the user may only see 1 sequence,
1103 // but the sequence represents a group
1106 if (!av.isHiddenRepSequence(seq))
1111 sg = av.getRepresentedSequences(seq);
1114 fixedLeft = sg.getStartRes();
1115 fixedRight = sg.getEndRes();
1117 if ((startres < fixedLeft && lastres >= fixedLeft)
1118 || (startres >= fixedLeft && lastres < fixedLeft)
1119 || (startres > fixedRight && lastres <= fixedRight)
1120 || (startres <= fixedRight && lastres > fixedRight))
1126 if (fixedLeft > startres)
1128 fixedRight = fixedLeft - 1;
1131 else if (fixedRight < startres)
1133 fixedLeft = fixedRight;
1138 if (av.hasHiddenColumns())
1140 fixedColumns = true;
1141 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1142 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1144 if ((insertGap && startres > y1 && lastres < y1)
1145 || (!insertGap && startres < y2 && lastres > y2))
1151 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1152 // Selection spans a hidden region
1153 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1161 fixedRight = y2 - 1;
1168 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1169 int g, groupSize = vseqs.size();
1170 SequenceI[] groupSeqs = new SequenceI[groupSize];
1171 for (g = 0; g < groupSeqs.length; g++)
1173 groupSeqs[g] = vseqs.get(g);
1179 // If the user has selected the whole sequence, and is dragging to
1180 // the right, we can still extend the alignment and selectionGroup
1181 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1182 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1184 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1185 fixedRight = sg.getEndRes();
1188 // Is it valid with fixed columns??
1189 // Find the next gap before the end
1190 // of the visible region boundary
1191 boolean blank = false;
1192 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1196 for (g = 0; g < groupSize; g++)
1198 for (int j = 0; j < startres - lastres; j++)
1200 if (!Comparison.isGap(groupSeqs[g].getCharAt(fixedRight - j)))
1215 if (sg.getSize() == av.getAlignment().getHeight())
1217 if ((av.hasHiddenColumns() && startres < av
1218 .getColumnSelection().getHiddenBoundaryRight(startres)))
1224 int alWidth = av.getAlignment().getWidth();
1225 if (av.hasHiddenRows())
1227 int hwidth = av.getAlignment().getHiddenSequences()
1229 if (hwidth > alWidth)
1234 // We can still insert gaps if the selectionGroup
1235 // contains all the sequences
1236 sg.setEndRes(sg.getEndRes() + startres - lastres);
1237 fixedRight = alWidth + startres - lastres;
1248 else if (!insertGap)
1250 // / Are we able to delete?
1251 // ie are all columns blank?
1253 for (g = 0; g < groupSize; g++)
1255 for (int j = startres; j < lastres; j++)
1257 if (groupSeqs[g].getLength() <= j)
1262 if (!Comparison.isGap(groupSeqs[g].getCharAt(j)))
1264 // Not a gap, block edit not valid
1274 // dragging to the right
1275 if (fixedColumns && fixedRight != -1)
1277 for (int j = lastres; j < startres; j++)
1279 insertChar(j, groupSeqs, fixedRight);
1284 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1290 // dragging to the left
1291 if (fixedColumns && fixedRight != -1)
1293 for (int j = lastres; j > startres; j--)
1295 deleteChar(startres, groupSeqs, fixedRight);
1300 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1307 // ///Editing a single sequence///////////
1311 // dragging to the right
1312 if (fixedColumns && fixedRight != -1)
1314 for (int j = lastres; j < startres; j++)
1316 insertChar(j, new SequenceI[] { seq }, fixedRight);
1321 appendEdit(Action.INSERT_GAP, new SequenceI[] { seq }, lastres,
1322 startres - lastres);
1329 // dragging to the left
1330 if (fixedColumns && fixedRight != -1)
1332 for (int j = lastres; j > startres; j--)
1334 if (!Comparison.isGap(seq.getCharAt(startres)))
1339 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1344 // could be a keyboard edit trying to delete none gaps
1346 for (int m = startres; m < lastres; m++)
1348 if (!Comparison.isGap(seq.getCharAt(m)))
1357 appendEdit(Action.DELETE_GAP, new SequenceI[] { seq },
1363 {// insertGap==false AND editSeq==TRUE;
1364 if (fixedColumns && fixedRight != -1)
1366 for (int j = lastres; j < startres; j++)
1368 insertChar(j, new SequenceI[] { seq }, fixedRight);
1373 appendEdit(Action.INSERT_NUC, new SequenceI[] { seq }, lastres,
1374 startres - lastres);
1381 seqCanvas.repaint();
1384 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1386 int blankColumn = fixedColumn;
1387 for (int s = 0; s < seq.length; s++)
1389 // Find the next gap before the end of the visible region boundary
1390 // If lastCol > j, theres a boundary after the gap insertion
1392 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1394 if (Comparison.isGap(seq[s].getCharAt(blankColumn)))
1396 // Theres a space, so break and insert the gap
1401 if (blankColumn <= j)
1403 blankColumn = fixedColumn;
1409 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1411 appendEdit(Action.INSERT_GAP, seq, j, 1);
1416 * Helper method to add and perform one edit action.
1423 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1427 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1428 av.getAlignment().getGapCharacter());
1430 editCommand.appendEdit(edit, av.getAlignment(), true, null);
1433 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1436 appendEdit(Action.DELETE_GAP, seq, j, 1);
1438 appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1);
1448 public void mouseEntered(MouseEvent e)
1455 if (scrollThread != null)
1457 scrollThread.running = false;
1458 scrollThread = null;
1469 public void mouseExited(MouseEvent e)
1471 if (av.getWrapAlignment())
1478 scrollThread = new ScrollThread();
1483 public void mouseClicked(MouseEvent evt)
1485 SequenceGroup sg = null;
1486 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1487 if (evt.getClickCount() > 1)
1489 sg = av.getSelectionGroup();
1490 if (sg != null && sg.getSize() == 1
1491 && sg.getEndRes() - sg.getStartRes() < 2)
1493 av.setSelectionGroup(null);
1496 List<SequenceFeature> features = seqCanvas.getFeatureRenderer()
1497 .findFeaturesAtRes(sequence.getDatasetSequence(),
1498 sequence.findPosition(findRes(evt)));
1500 if (features != null && features.size() > 0)
1502 SearchResults highlight = new SearchResults();
1503 highlight.addResult(sequence, features.get(0).getBegin(), features
1505 seqCanvas.highlightSearchResults(highlight);
1507 if (features != null && features.size() > 0)
1509 seqCanvas.getFeatureRenderer().amendFeatures(
1510 new SequenceI[] { sequence },
1511 features.toArray(new SequenceFeature[features.size()]),
1514 seqCanvas.highlightSearchResults(null);
1520 public void mouseWheelMoved(MouseWheelEvent e)
1523 if (e.getWheelRotation() > 0)
1525 if (e.isShiftDown())
1527 ap.scrollRight(true);
1537 if (e.isShiftDown())
1539 ap.scrollRight(false);
1546 // TODO Update tooltip for new position.
1555 public void doMousePressedDefineMode(MouseEvent evt)
1557 final int res = findRes(evt);
1558 final int seq = findSeq(evt);
1561 startWrapBlock = wrappedBlock;
1563 if (av.getWrapAlignment() && seq > av.getAlignment().getHeight())
1565 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1566 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1567 MessageManager.getString("label.wrapped_view_no_edit"),
1568 JOptionPane.WARNING_MESSAGE);
1572 if (seq < 0 || res < 0)
1577 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1579 if ((sequence == null) || (res > sequence.getLength()))
1584 stretchGroup = av.getSelectionGroup();
1586 if (stretchGroup == null)
1588 stretchGroup = av.getAlignment().findGroup(sequence);
1590 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1591 && (res < stretchGroup.getEndRes()))
1593 av.setSelectionGroup(stretchGroup);
1594 editingDefinedGroup = true;
1598 stretchGroup = null;
1599 editingDefinedGroup = false;
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];
1618 editingDefinedGroup = true;
1624 av.setSelectionGroup(stretchGroup);
1628 if (evt.isPopupTrigger()) // Mac: mousePressed
1635 * defer right-mouse click handling to mouseReleased on Windows
1636 * (where isPopupTrigger() will answer true)
1637 * NB isRightMouseButton is also true for Cmd-click on Mac
1639 if (SwingUtilities.isRightMouseButton(evt) && !Platform.isAMac())
1646 seqCanvas.cursorX = findRes(evt);
1647 seqCanvas.cursorY = findSeq(evt);
1648 seqCanvas.repaint();
1652 if (stretchGroup == null)
1654 // Only if left mouse button do we want to change group sizes
1656 // define a new group here
1657 SequenceGroup sg = new SequenceGroup();
1658 sg.setStartRes(res);
1660 sg.addSequence(sequence, false);
1661 av.setSelectionGroup(sg);
1662 editingDefinedGroup = false;
1665 if (av.getConservationSelected())
1667 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1671 if (av.getAbovePIDThreshold())
1673 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1676 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1678 // Edit end res position of selected group
1679 changeEndRes = true;
1681 else if ((stretchGroup != null)
1682 && (stretchGroup.getStartRes() == res))
1684 // Edit end res position of selected group
1685 changeStartRes = true;
1687 stretchGroup.getWidth();
1690 seqCanvas.repaint();
1694 * Build and show a pop-up menu at the right-click mouse position
1700 void showPopupMenu(MouseEvent evt)
1702 final int res = findRes(evt);
1703 final int seq = findSeq(evt);
1704 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1705 List<SequenceFeature> allFeatures = ap.getFeatureRenderer()
1706 .findFeaturesAtRes(sequence.getDatasetSequence(),
1707 sequence.findPosition(res));
1708 List<String> links = new ArrayList<String>();
1709 for (SequenceFeature sf : allFeatures)
1711 if (sf.links != null)
1713 for (String link : sf.links)
1720 PopupMenu pop = new PopupMenu(ap, null, links);
1721 pop.show(this, evt.getX(), evt.getY());
1730 public void doMouseReleasedDefineMode(MouseEvent evt)
1732 if (stretchGroup == null)
1736 // always do this - annotation has own state
1737 // but defer colourscheme update until hidden sequences are passed in
1738 boolean vischange = stretchGroup.recalcConservation(true);
1739 needOverviewUpdate |= vischange && editingDefinedGroup;
1740 if (stretchGroup.cs != null)
1742 stretchGroup.cs.alignmentChanged(stretchGroup,
1743 av.getHiddenRepSequences());
1745 if (stretchGroup.cs.conservationApplied())
1747 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1748 stretchGroup.getName());
1752 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1753 stretchGroup.getName());
1756 PaintRefresher.Refresh(this, av.getSequenceSetId());
1757 ap.paintAlignment(needOverviewUpdate);
1758 needOverviewUpdate =false;
1759 editingDefinedGroup = false;
1760 changeEndRes = false;
1761 changeStartRes = false;
1762 stretchGroup = null;
1772 public void doMouseDraggedDefineMode(MouseEvent evt)
1774 int res = findRes(evt);
1775 int y = findSeq(evt);
1777 if (wrappedBlock != startWrapBlock)
1782 if (stretchGroup == null)
1787 if (res >= av.getAlignment().getWidth())
1789 res = av.getAlignment().getWidth() - 1;
1792 if (stretchGroup.getEndRes() == res)
1794 // Edit end res position of selected group
1795 changeEndRes = true;
1797 else if (stretchGroup.getStartRes() == res)
1799 // Edit start res position of selected group
1800 changeStartRes = true;
1803 if (res < av.getStartRes())
1805 res = av.getStartRes();
1810 if (res > (stretchGroup.getStartRes() - 1))
1812 stretchGroup.setEndRes(res);
1813 needOverviewUpdate |= editingDefinedGroup;
1816 else if (changeStartRes)
1818 if (res < (stretchGroup.getEndRes() + 1))
1820 stretchGroup.setStartRes(res);
1821 needOverviewUpdate |= editingDefinedGroup;
1825 int dragDirection = 0;
1831 else if (y < oldSeq)
1836 while ((y != oldSeq) && (oldSeq > -1)
1837 && (y < av.getAlignment().getHeight()))
1839 // This routine ensures we don't skip any sequences, as the
1840 // selection is quite slow.
1841 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1843 oldSeq += dragDirection;
1850 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1852 if (stretchGroup.getSequences(null).contains(nextSeq))
1854 stretchGroup.deleteSequence(seq, false);
1855 needOverviewUpdate |= editingDefinedGroup;
1861 stretchGroup.addSequence(seq, false);
1864 stretchGroup.addSequence(nextSeq, false);
1865 needOverviewUpdate |= editingDefinedGroup;
1874 mouseDragging = true;
1876 if (scrollThread != null)
1878 scrollThread.setEvent(evt);
1881 seqCanvas.repaint();
1884 void scrollCanvas(MouseEvent evt)
1888 if (scrollThread != null)
1890 scrollThread.running = false;
1891 scrollThread = null;
1893 mouseDragging = false;
1897 if (scrollThread == null)
1899 scrollThread = new ScrollThread();
1902 mouseDragging = true;
1903 scrollThread.setEvent(evt);
1908 // this class allows scrolling off the bottom of the visible alignment
1909 class ScrollThread extends Thread
1913 boolean running = false;
1915 public ScrollThread()
1920 public void setEvent(MouseEvent e)
1925 public void stopScrolling()
1939 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1941 running = ap.scrollUp(true);
1944 if (mouseDragging && (evt.getY() >= getHeight())
1945 && (av.getAlignment().getHeight() > av.getEndSeq()))
1947 running = ap.scrollUp(false);
1950 if (mouseDragging && (evt.getX() < 0))
1952 running = ap.scrollRight(false);
1954 else if (mouseDragging && (evt.getX() >= getWidth()))
1956 running = ap.scrollRight(true);
1963 } catch (Exception ex)
1971 * modify current selection according to a received message.
1974 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1975 SelectionSource source)
1977 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1978 // handles selection messages...
1979 // TODO: extend config options to allow user to control if selections may be
1980 // shared between viewports.
1981 boolean iSentTheSelection = (av == source || (source instanceof AlignViewport && ((AlignmentViewport) source)
1982 .getSequenceSetId().equals(av.getSequenceSetId())));
1983 if (iSentTheSelection || !av.followSelection)
1989 * Ignore the selection if there is one of our own pending.
1991 if (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1997 * Check for selection in a view of which this one is a dna/protein
2000 if (selectionFromTranslation(seqsel, colsel, source))
2005 // do we want to thread this ? (contention with seqsel and colsel locks, I
2008 * only copy colsel if there is a real intersection between
2009 * sequence selection and this panel's alignment
2011 boolean repaint = false;
2012 boolean copycolsel = false;
2014 SequenceGroup sgroup = null;
2015 if (seqsel != null && seqsel.getSize() > 0)
2017 if (av.getAlignment() == null)
2019 Cache.log.warn("alignviewport av SeqSetId="
2020 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
2021 + " 's alignment is NULL! returning immediately.");
2024 sgroup = seqsel.intersect(av.getAlignment(),
2025 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
2026 if ((sgroup != null && sgroup.getSize() > 0))
2031 if (sgroup != null && sgroup.getSize() > 0)
2033 av.setSelectionGroup(sgroup);
2037 av.setSelectionGroup(null);
2039 av.isSelectionGroupChanged(true);
2044 // the current selection is unset or from a previous message
2045 // so import the new colsel.
2046 if (colsel == null || colsel.isEmpty())
2048 if (av.getColumnSelection() != null)
2050 av.getColumnSelection().clear();
2056 // TODO: shift colSel according to the intersecting sequences
2057 if (av.getColumnSelection() == null)
2059 av.setColumnSelection(new ColumnSelection(colsel));
2063 av.getColumnSelection().setElementsFrom(colsel);
2066 av.isColSelChanged(true);
2071 && av.hasHiddenColumns()
2072 && (av.getColumnSelection() == null || av.getColumnSelection()
2073 .getHiddenColumns() == null))
2075 System.err.println("Bad things");
2077 if (repaint) // always true!
2079 // probably finessing with multiple redraws here
2080 PaintRefresher.Refresh(this, av.getSequenceSetId());
2081 // ap.paintAlignment(false);
2086 * If this panel is a cdna/protein translation view of the selection source,
2087 * tries to map the source selection to a local one, and returns true. Else
2094 protected boolean selectionFromTranslation(SequenceGroup seqsel,
2095 ColumnSelection colsel, SelectionSource source)
2097 if (!(source instanceof AlignViewportI))
2101 final AlignViewportI sourceAv = (AlignViewportI) source;
2102 if (sourceAv.getCodingComplement() != av
2103 && av.getCodingComplement() != sourceAv)
2109 * Map sequence selection
2111 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
2112 av.setSelectionGroup(sg);
2113 av.isSelectionGroupChanged(true);
2116 * Map column selection
2118 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
2120 av.setColumnSelection(cs);
2122 PaintRefresher.Refresh(this, av.getSequenceSetId());