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.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.getStartRes();
226 if (res > av.getEndRes())
229 res = av.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()) + av.getStartSeq(), av
266 .getAlignment().getHeight() - 1);
273 * When all of a sequence of edits are complete, put the resulting edit list
274 * on the history stack (undo list), and reset flags for editing in progress.
280 if (editCommand != null && editCommand.getSize() > 0)
282 ap.alignFrame.addHistoryItem(editCommand);
283 av.firePropertyChange("alignment", null, av.getAlignment()
289 * Tidy up come what may...
294 groupEditing = false;
303 seqCanvas.cursorY = getKeyboardNo1() - 1;
307 void setCursorColumn()
309 seqCanvas.cursorX = getKeyboardNo1() - 1;
313 void setCursorRowAndColumn()
315 if (keyboardNo2 == null)
317 keyboardNo2 = new StringBuffer();
321 seqCanvas.cursorX = getKeyboardNo1() - 1;
322 seqCanvas.cursorY = getKeyboardNo2() - 1;
327 void setCursorPosition()
329 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
331 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
335 void moveCursor(int dx, int dy)
337 seqCanvas.cursorX += dx;
338 seqCanvas.cursorY += dy;
339 if (av.hasHiddenColumns()
340 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
342 int original = seqCanvas.cursorX - dx;
343 int maxWidth = av.getAlignment().getWidth();
345 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
346 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
348 seqCanvas.cursorX += dx;
351 if (seqCanvas.cursorX >= maxWidth
352 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
354 seqCanvas.cursorX = original;
361 void scrollToVisible()
363 if (seqCanvas.cursorX < 0)
365 seqCanvas.cursorX = 0;
367 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
369 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
372 if (seqCanvas.cursorY < 0)
374 seqCanvas.cursorY = 0;
376 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
378 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
382 if (av.getWrapAlignment())
384 ap.scrollToWrappedVisible(seqCanvas.cursorX);
388 while (seqCanvas.cursorY < av.startSeq)
392 while (seqCanvas.cursorY + 1 > av.endSeq)
396 if (!av.getWrapAlignment())
398 while (seqCanvas.cursorX < av.getColumnSelection()
399 .adjustForHiddenColumns(av.startRes))
401 if (!ap.scrollRight(false))
406 while (seqCanvas.cursorX > av.getColumnSelection()
407 .adjustForHiddenColumns(av.endRes))
409 if (!ap.scrollRight(true))
416 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
417 seqCanvas.cursorX, seqCanvas.cursorY);
422 void setSelectionAreaAtCursor(boolean topLeft)
424 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
426 if (av.getSelectionGroup() != null)
428 SequenceGroup sg = av.getSelectionGroup();
429 // Find the top and bottom of this group
430 int min = av.getAlignment().getHeight(), max = 0;
431 for (int i = 0; i < sg.getSize(); i++)
433 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
448 sg.setStartRes(seqCanvas.cursorX);
449 if (sg.getEndRes() < seqCanvas.cursorX)
451 sg.setEndRes(seqCanvas.cursorX);
454 min = seqCanvas.cursorY;
458 sg.setEndRes(seqCanvas.cursorX);
459 if (sg.getStartRes() > seqCanvas.cursorX)
461 sg.setStartRes(seqCanvas.cursorX);
464 max = seqCanvas.cursorY + 1;
469 // Only the user can do this
470 av.setSelectionGroup(null);
474 // Now add any sequences between min and max
475 sg.getSequences(null).clear();
476 for (int i = min; i < max; i++)
478 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
483 if (av.getSelectionGroup() == null)
485 SequenceGroup sg = new SequenceGroup();
486 sg.setStartRes(seqCanvas.cursorX);
487 sg.setEndRes(seqCanvas.cursorX);
488 sg.addSequence(sequence, false);
489 av.setSelectionGroup(sg);
492 ap.paintAlignment(false);
496 void insertGapAtCursor(boolean group)
498 groupEditing = group;
499 startseq = seqCanvas.cursorY;
500 lastres = seqCanvas.cursorX;
501 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
505 void deleteGapAtCursor(boolean group)
507 groupEditing = group;
508 startseq = seqCanvas.cursorY;
509 lastres = seqCanvas.cursorX + getKeyboardNo1();
510 editSequence(false, false, seqCanvas.cursorX);
514 void insertNucAtCursor(boolean group, String nuc)
516 // TODO not called - delete?
517 groupEditing = group;
518 startseq = seqCanvas.cursorY;
519 lastres = seqCanvas.cursorX;
520 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
524 void numberPressed(char value)
526 if (keyboardNo1 == null)
528 keyboardNo1 = new StringBuffer();
531 if (keyboardNo2 != null)
533 keyboardNo2.append(value);
537 keyboardNo1.append(value);
545 if (keyboardNo1 != null)
547 int value = Integer.parseInt(keyboardNo1.toString());
551 } catch (Exception x)
562 if (keyboardNo2 != null)
564 int value = Integer.parseInt(keyboardNo2.toString());
568 } catch (Exception x)
582 public void mouseReleased(MouseEvent evt)
584 mouseDragging = false;
585 mouseWheelPressed = false;
587 if (evt.isPopupTrigger()) // Windows: mouseReleased
596 doMouseReleasedDefineMode(evt);
610 public void mousePressed(MouseEvent evt)
612 lastMousePress = evt.getPoint();
614 if (SwingUtilities.isMiddleMouseButton(evt))
616 mouseWheelPressed = true;
620 boolean isControlDown = Platform.isControlDown(evt);
621 if (evt.isShiftDown() || isControlDown)
631 doMousePressedDefineMode(evt);
635 int seq = findSeq(evt);
636 int res = findRes(evt);
638 if (seq < 0 || res < 0)
643 if ((seq < av.getAlignment().getHeight())
644 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
661 public void mouseOverSequence(SequenceI sequence, int index, int pos)
663 String tmp = sequence.hashCode() + " " + index + " " + pos;
665 if (lastMessage == null || !lastMessage.equals(tmp))
667 // System.err.println("mouseOver Sequence: "+tmp);
668 ssm.mouseOverSequence(sequence, index, pos, av);
674 * Highlight the mapped region described by the search results object (unless
675 * unchanged). This supports highlight of protein while mousing over linked
676 * cDNA and vice versa. The status bar is also updated to show the location of
677 * the start of the highlighted region.
680 public void highlightSequence(SearchResultsI results)
682 if (results == null || results.equals(lastSearchResults))
686 lastSearchResults = results;
688 if (av.isFollowHighlight())
691 * if scrollToPosition requires a scroll adjustment, this flag prevents
692 * another scroll event being propagated back to the originator
694 * @see AlignmentPanel#adjustmentValueChanged
696 ap.setDontScrollComplement(true);
697 if (ap.scrollToPosition(results, false))
699 seqCanvas.revalidate();
702 setStatusMessage(results);
703 seqCanvas.highlightSearchResults(results);
707 public VamsasSource getVamsasSource()
709 return this.ap == null ? null : this.ap.av;
713 public void updateColours(SequenceI seq, int index)
715 System.out.println("update the seqPanel colours");
726 public void mouseMoved(MouseEvent evt)
730 // This is because MacOSX creates a mouseMoved
731 // If control is down, other platforms will not.
735 int res = findRes(evt);
736 int seq = findSeq(evt);
738 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
743 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
745 if (res >= sequence.getLength())
750 pos = setStatusMessage(sequence, res, seq);
751 if (ssm != null && pos > -1)
753 mouseOverSequence(sequence, res, pos);
756 tooltipText.setLength(6); // Cuts the buffer back to <html>
758 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
761 for (int g = 0; g < groups.length; g++)
763 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
765 if (!groups[g].getName().startsWith("JTreeGroup")
766 && !groups[g].getName().startsWith("JGroup"))
768 tooltipText.append(groups[g].getName());
771 if (groups[g].getDescription() != null)
773 tooltipText.append(": " + groups[g].getDescription());
779 // use aa to see if the mouse pointer is on a
780 if (av.isShowSequenceFeatures())
783 List<SequenceFeature> features = ap.getFeatureRenderer()
784 .findFeaturesAtRes(sequence.getDatasetSequence(),
785 rpos = sequence.findPosition(res));
786 seqARep.appendFeatures(tooltipText, rpos, features,
787 this.ap.getSeqPanel().seqCanvas.fr.getMinMax());
789 if (tooltipText.length() == 6) // <html>
791 setToolTipText(null);
796 if (lastTooltip == null
797 || !lastTooltip.equals(tooltipText.toString()))
799 String formatedTooltipText = JvSwingUtils.wrapTooltip(true,
800 tooltipText.toString());
801 // String formatedTooltipText = tooltipText.toString();
802 setToolTipText(formatedTooltipText);
803 lastTooltip = tooltipText.toString();
810 private Point lastp = null;
815 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
818 public Point getToolTipLocation(MouseEvent event)
820 int x = event.getX(), w = getWidth();
821 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
824 if (!event.isShiftDown() || p == null)
826 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
827 event.getX() + wdth, event.getY() - 20) : null;
830 * TODO: try to modify position region is not obcured by tooltip
838 * set when the current UI interaction has resulted in a change that requires
839 * overview shading to be recalculated. this could be changed to something
840 * more expressive that indicates what actually has changed, so selective
841 * redraws can be applied
843 private boolean needOverviewUpdate = false; // TODO: refactor to avcontroller
846 * set if av.getSelectionGroup() refers to a group that is defined on the
847 * alignment view, rather than a transient selection
849 // private boolean editingDefinedGroup = false; // TODO: refactor to
850 // avcontroller or viewModel
853 * Set status message in alignment panel
856 * aligned sequence object
860 * index of sequence in alignment
861 * @return position of res in sequence
863 int setStatusMessage(SequenceI sequence, int res, int seq)
865 StringBuilder text = new StringBuilder(32);
868 * Sequence number (if known), and sequence name.
870 String seqno = seq == -1 ? "" : " " + (seq + 1);
871 text.append("Sequence").append(seqno).append(" ID: ")
872 .append(sequence.getName());
874 String residue = null;
876 * Try to translate the display character to residue name (null for gap).
878 final String displayChar = String.valueOf(sequence.getCharAt(res));
879 if (av.getAlignment().isNucleotide())
881 residue = ResidueProperties.nucleotideName.get(displayChar);
884 text.append(" Nucleotide: ").append(residue);
889 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
890 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
894 text.append(" Residue: ").append(residue);
901 pos = sequence.findPosition(res);
902 text.append(" (").append(Integer.toString(pos)).append(")");
904 ap.alignFrame.statusBar.setText(text.toString());
909 * Set the status bar message to highlight the first matched position in
914 private void setStatusMessage(SearchResultsI results)
916 AlignmentI al = this.av.getAlignment();
917 int sequenceIndex = al.findIndex(results);
918 if (sequenceIndex == -1)
922 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
923 for (SearchResultMatchI m : results.getResults())
925 SequenceI seq = m.getSequence();
926 if (seq.getDatasetSequence() != null)
928 seq = seq.getDatasetSequence();
934 * Convert position in sequence (base 1) to sequence character array
937 int start = m.getStart() - m.getSequence().getStart();
938 setStatusMessage(seq, start, sequenceIndex);
951 public void mouseDragged(MouseEvent evt)
953 if (mouseWheelPressed)
955 int oldWidth = av.getCharWidth();
957 // Which is bigger, left-right or up-down?
958 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
959 .getX() - lastMousePress.getX()))
961 int fontSize = av.font.getSize();
963 if (evt.getY() < lastMousePress.getY())
967 else if (evt.getY() > lastMousePress.getY())
978 new Font(av.font.getName(), av.font.getStyle(), fontSize),
980 av.setCharWidth(oldWidth);
985 if (evt.getX() < lastMousePress.getX() && av.getCharWidth() > 1)
987 av.setCharWidth(av.getCharWidth() - 1);
989 else if (evt.getX() > lastMousePress.getX())
991 av.setCharWidth(av.getCharWidth() + 1);
994 ap.paintAlignment(false);
997 FontMetrics fm = getFontMetrics(av.getFont());
998 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
1000 lastMousePress = evt.getPoint();
1007 doMouseDraggedDefineMode(evt);
1011 int res = findRes(evt);
1018 if ((lastres == -1) || (lastres == res))
1023 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1025 // dragLeft, delete gap
1026 editSequence(false, false, res);
1030 editSequence(true, false, res);
1033 mouseDragging = true;
1034 if (scrollThread != null)
1036 scrollThread.setEvent(evt);
1040 // TODO: Make it more clever than many booleans
1041 synchronized void editSequence(boolean insertGap, boolean editSeq,
1045 int fixedRight = -1;
1046 boolean fixedColumns = false;
1047 SequenceGroup sg = av.getSelectionGroup();
1049 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1051 // No group, but the sequence may represent a group
1052 if (!groupEditing && av.hasHiddenRows())
1054 if (av.isHiddenRepSequence(seq))
1056 sg = av.getRepresentedSequences(seq);
1057 groupEditing = true;
1061 StringBuilder message = new StringBuilder(64);
1064 message.append("Edit group:");
1065 if (editCommand == null)
1067 editCommand = new EditCommand(
1068 MessageManager.getString("action.edit_group"));
1073 message.append("Edit sequence: " + seq.getName());
1074 String label = seq.getName();
1075 if (label.length() > 10)
1077 label = label.substring(0, 10);
1079 if (editCommand == null)
1081 editCommand = new EditCommand(MessageManager.formatMessage(
1082 "label.edit_params", new String[] { label }));
1088 message.append(" insert ");
1092 message.append(" delete ");
1095 message.append(Math.abs(startres - lastres) + " gaps.");
1096 ap.alignFrame.statusBar.setText(message.toString());
1098 // Are we editing within a selection group?
1100 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1103 fixedColumns = true;
1105 // sg might be null as the user may only see 1 sequence,
1106 // but the sequence represents a group
1109 if (!av.isHiddenRepSequence(seq))
1114 sg = av.getRepresentedSequences(seq);
1117 fixedLeft = sg.getStartRes();
1118 fixedRight = sg.getEndRes();
1120 if ((startres < fixedLeft && lastres >= fixedLeft)
1121 || (startres >= fixedLeft && lastres < fixedLeft)
1122 || (startres > fixedRight && lastres <= fixedRight)
1123 || (startres <= fixedRight && lastres > fixedRight))
1129 if (fixedLeft > startres)
1131 fixedRight = fixedLeft - 1;
1134 else if (fixedRight < startres)
1136 fixedLeft = fixedRight;
1141 if (av.hasHiddenColumns())
1143 fixedColumns = true;
1144 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1145 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1147 if ((insertGap && startres > y1 && lastres < y1)
1148 || (!insertGap && startres < y2 && lastres > y2))
1154 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1155 // Selection spans a hidden region
1156 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1164 fixedRight = y2 - 1;
1171 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1172 int g, groupSize = vseqs.size();
1173 SequenceI[] groupSeqs = new SequenceI[groupSize];
1174 for (g = 0; g < groupSeqs.length; g++)
1176 groupSeqs[g] = vseqs.get(g);
1182 // If the user has selected the whole sequence, and is dragging to
1183 // the right, we can still extend the alignment and selectionGroup
1184 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1185 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1187 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1188 fixedRight = sg.getEndRes();
1191 // Is it valid with fixed columns??
1192 // Find the next gap before the end
1193 // of the visible region boundary
1194 boolean blank = false;
1195 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1199 for (g = 0; g < groupSize; g++)
1201 for (int j = 0; j < startres - lastres; j++)
1203 if (!Comparison.isGap(groupSeqs[g].getCharAt(fixedRight - j)))
1218 if (sg.getSize() == av.getAlignment().getHeight())
1220 if ((av.hasHiddenColumns() && startres < av
1221 .getColumnSelection().getHiddenBoundaryRight(startres)))
1227 int alWidth = av.getAlignment().getWidth();
1228 if (av.hasHiddenRows())
1230 int hwidth = av.getAlignment().getHiddenSequences()
1232 if (hwidth > alWidth)
1237 // We can still insert gaps if the selectionGroup
1238 // contains all the sequences
1239 sg.setEndRes(sg.getEndRes() + startres - lastres);
1240 fixedRight = alWidth + startres - lastres;
1251 else if (!insertGap)
1253 // / Are we able to delete?
1254 // ie are all columns blank?
1256 for (g = 0; g < groupSize; g++)
1258 for (int j = startres; j < lastres; j++)
1260 if (groupSeqs[g].getLength() <= j)
1265 if (!Comparison.isGap(groupSeqs[g].getCharAt(j)))
1267 // Not a gap, block edit not valid
1277 // dragging to the right
1278 if (fixedColumns && fixedRight != -1)
1280 for (int j = lastres; j < startres; j++)
1282 insertChar(j, groupSeqs, fixedRight);
1287 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1293 // dragging to the left
1294 if (fixedColumns && fixedRight != -1)
1296 for (int j = lastres; j > startres; j--)
1298 deleteChar(startres, groupSeqs, fixedRight);
1303 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1310 // ///Editing a single sequence///////////
1314 // dragging to the right
1315 if (fixedColumns && fixedRight != -1)
1317 for (int j = lastres; j < startres; j++)
1319 insertChar(j, new SequenceI[] { seq }, fixedRight);
1324 appendEdit(Action.INSERT_GAP, new SequenceI[] { seq }, lastres,
1325 startres - lastres);
1332 // dragging to the left
1333 if (fixedColumns && fixedRight != -1)
1335 for (int j = lastres; j > startres; j--)
1337 if (!Comparison.isGap(seq.getCharAt(startres)))
1342 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1347 // could be a keyboard edit trying to delete none gaps
1349 for (int m = startres; m < lastres; m++)
1351 if (!Comparison.isGap(seq.getCharAt(m)))
1360 appendEdit(Action.DELETE_GAP, new SequenceI[] { seq },
1366 {// insertGap==false AND editSeq==TRUE;
1367 if (fixedColumns && fixedRight != -1)
1369 for (int j = lastres; j < startres; j++)
1371 insertChar(j, new SequenceI[] { seq }, fixedRight);
1376 appendEdit(Action.INSERT_NUC, new SequenceI[] { seq }, lastres,
1377 startres - lastres);
1384 seqCanvas.repaint();
1387 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1389 int blankColumn = fixedColumn;
1390 for (int s = 0; s < seq.length; s++)
1392 // Find the next gap before the end of the visible region boundary
1393 // If lastCol > j, theres a boundary after the gap insertion
1395 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1397 if (Comparison.isGap(seq[s].getCharAt(blankColumn)))
1399 // Theres a space, so break and insert the gap
1404 if (blankColumn <= j)
1406 blankColumn = fixedColumn;
1412 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1414 appendEdit(Action.INSERT_GAP, seq, j, 1);
1419 * Helper method to add and perform one edit action.
1426 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1430 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1431 av.getAlignment().getGapCharacter());
1433 editCommand.appendEdit(edit, av.getAlignment(), true, null);
1436 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1439 appendEdit(Action.DELETE_GAP, seq, j, 1);
1441 appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1);
1451 public void mouseEntered(MouseEvent e)
1458 if (scrollThread != null)
1460 scrollThread.running = false;
1461 scrollThread = null;
1472 public void mouseExited(MouseEvent e)
1474 if (av.getWrapAlignment())
1481 scrollThread = new ScrollThread();
1486 public void mouseClicked(MouseEvent evt)
1488 SequenceGroup sg = null;
1489 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1490 if (evt.getClickCount() > 1)
1492 sg = av.getSelectionGroup();
1493 if (sg != null && sg.getSize() == 1
1494 && sg.getEndRes() - sg.getStartRes() < 2)
1496 av.setSelectionGroup(null);
1499 List<SequenceFeature> features = seqCanvas.getFeatureRenderer()
1500 .findFeaturesAtRes(sequence.getDatasetSequence(),
1501 sequence.findPosition(findRes(evt)));
1503 if (features != null && features.size() > 0)
1505 SearchResultsI highlight = new SearchResults();
1506 highlight.addResult(sequence, features.get(0).getBegin(), features
1508 seqCanvas.highlightSearchResults(highlight);
1510 if (features != null && features.size() > 0)
1512 seqCanvas.getFeatureRenderer().amendFeatures(
1513 new SequenceI[] { sequence },
1514 features.toArray(new SequenceFeature[features.size()]),
1517 seqCanvas.highlightSearchResults(null);
1523 public void mouseWheelMoved(MouseWheelEvent e)
1526 if (e.getWheelRotation() > 0)
1528 if (e.isShiftDown())
1530 ap.scrollRight(true);
1540 if (e.isShiftDown())
1542 ap.scrollRight(false);
1549 // TODO Update tooltip for new position.
1558 public void doMousePressedDefineMode(MouseEvent evt)
1560 final int res = findRes(evt);
1561 final int seq = findSeq(evt);
1563 needOverviewUpdate = false;
1565 startWrapBlock = wrappedBlock;
1567 if (av.getWrapAlignment() && seq > av.getAlignment().getHeight())
1569 JvOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1570 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1571 MessageManager.getString("label.wrapped_view_no_edit"),
1572 JvOptionPane.WARNING_MESSAGE);
1576 if (seq < 0 || res < 0)
1581 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1583 if ((sequence == null) || (res > sequence.getLength()))
1588 stretchGroup = av.getSelectionGroup();
1590 if (stretchGroup == null || !stretchGroup.contains(sequence, res))
1592 stretchGroup = av.getAlignment().findGroup(sequence, res);
1593 if (stretchGroup != null)
1595 // only update the current selection if the popup menu has a group to
1597 av.setSelectionGroup(stretchGroup);
1601 if (evt.isPopupTrigger()) // Mac: mousePressed
1608 * defer right-mouse click handling to mouseReleased on Windows
1609 * (where isPopupTrigger() will answer true)
1610 * NB isRightMouseButton is also true for Cmd-click on Mac
1612 if (SwingUtilities.isRightMouseButton(evt) && !Platform.isAMac())
1619 seqCanvas.cursorX = findRes(evt);
1620 seqCanvas.cursorY = findSeq(evt);
1621 seqCanvas.repaint();
1625 if (stretchGroup == null)
1627 // Only if left mouse button do we want to change group sizes
1629 // define a new group here
1630 SequenceGroup sg = new SequenceGroup();
1631 sg.setStartRes(res);
1633 sg.addSequence(sequence, false);
1634 av.setSelectionGroup(sg);
1637 if (av.getConservationSelected())
1639 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1643 if (av.getAbovePIDThreshold())
1645 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1648 // TODO: stretchGroup will always be not null. Is this a merge error ?
1649 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1651 // Edit end res position of selected group
1652 changeEndRes = true;
1654 else if ((stretchGroup != null)
1655 && (stretchGroup.getStartRes() == res))
1657 // Edit end res position of selected group
1658 changeStartRes = true;
1660 stretchGroup.getWidth();
1663 seqCanvas.repaint();
1667 * Build and show a pop-up menu at the right-click mouse position
1673 void showPopupMenu(MouseEvent evt)
1675 final int res = findRes(evt);
1676 final int seq = findSeq(evt);
1677 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1678 List<SequenceFeature> allFeatures = ap.getFeatureRenderer()
1679 .findFeaturesAtRes(sequence.getDatasetSequence(),
1680 sequence.findPosition(res));
1681 List<String> links = new ArrayList<String>();
1682 for (SequenceFeature sf : allFeatures)
1684 if (sf.links != null)
1686 for (String link : sf.links)
1693 PopupMenu pop = new PopupMenu(ap, null, links);
1694 pop.show(this, evt.getX(), evt.getY());
1703 public void doMouseReleasedDefineMode(MouseEvent evt)
1705 if (stretchGroup == null)
1709 // always do this - annotation has own state
1710 // but defer colourscheme update until hidden sequences are passed in
1711 boolean vischange = stretchGroup.recalcConservation(true);
1712 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup();
1713 if (stretchGroup.cs != null)
1715 stretchGroup.cs.alignmentChanged(stretchGroup,
1716 av.getHiddenRepSequences());
1718 ResidueShaderI groupColourScheme = stretchGroup.getGroupColourScheme();
1719 String name = stretchGroup.getName();
1720 if (stretchGroup.cs.conservationApplied())
1722 SliderPanel.setConservationSlider(ap, groupColourScheme, name);
1724 if (stretchGroup.cs.getThreshold() > 0)
1726 SliderPanel.setPIDSliderSource(ap, groupColourScheme, name);
1729 PaintRefresher.Refresh(this, av.getSequenceSetId());
1730 ap.paintAlignment(needOverviewUpdate);
1731 needOverviewUpdate = false;
1732 changeEndRes = false;
1733 changeStartRes = false;
1734 stretchGroup = null;
1744 public void doMouseDraggedDefineMode(MouseEvent evt)
1746 int res = findRes(evt);
1747 int y = findSeq(evt);
1749 if (wrappedBlock != startWrapBlock)
1754 if (stretchGroup == null)
1759 if (res >= av.getAlignment().getWidth())
1761 res = av.getAlignment().getWidth() - 1;
1764 if (stretchGroup.getEndRes() == res)
1766 // Edit end res position of selected group
1767 changeEndRes = true;
1769 else if (stretchGroup.getStartRes() == res)
1771 // Edit start res position of selected group
1772 changeStartRes = true;
1775 if (res < av.getStartRes())
1777 res = av.getStartRes();
1782 if (res > (stretchGroup.getStartRes() - 1))
1784 stretchGroup.setEndRes(res);
1785 needOverviewUpdate |= av.isSelectionDefinedGroup();
1788 else if (changeStartRes)
1790 if (res < (stretchGroup.getEndRes() + 1))
1792 stretchGroup.setStartRes(res);
1793 needOverviewUpdate |= av.isSelectionDefinedGroup();
1797 int dragDirection = 0;
1803 else if (y < oldSeq)
1808 while ((y != oldSeq) && (oldSeq > -1)
1809 && (y < av.getAlignment().getHeight()))
1811 // This routine ensures we don't skip any sequences, as the
1812 // selection is quite slow.
1813 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1815 oldSeq += dragDirection;
1822 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1824 if (stretchGroup.getSequences(null).contains(nextSeq))
1826 stretchGroup.deleteSequence(seq, false);
1827 needOverviewUpdate |= av.isSelectionDefinedGroup();
1833 stretchGroup.addSequence(seq, false);
1836 stretchGroup.addSequence(nextSeq, false);
1837 needOverviewUpdate |= av.isSelectionDefinedGroup();
1846 mouseDragging = true;
1848 if (scrollThread != null)
1850 scrollThread.setEvent(evt);
1853 seqCanvas.repaint();
1856 void scrollCanvas(MouseEvent evt)
1860 if (scrollThread != null)
1862 scrollThread.running = false;
1863 scrollThread = null;
1865 mouseDragging = false;
1869 if (scrollThread == null)
1871 scrollThread = new ScrollThread();
1874 mouseDragging = true;
1875 scrollThread.setEvent(evt);
1880 // this class allows scrolling off the bottom of the visible alignment
1881 class ScrollThread extends Thread
1885 boolean running = false;
1887 public ScrollThread()
1892 public void setEvent(MouseEvent e)
1897 public void stopScrolling()
1911 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1913 running = ap.scrollUp(true);
1916 if (mouseDragging && (evt.getY() >= getHeight())
1917 && (av.getAlignment().getHeight() > av.getEndSeq()))
1919 running = ap.scrollUp(false);
1922 if (mouseDragging && (evt.getX() < 0))
1924 running = ap.scrollRight(false);
1926 else if (mouseDragging && (evt.getX() >= getWidth()))
1928 running = ap.scrollRight(true);
1935 } catch (Exception ex)
1943 * modify current selection according to a received message.
1946 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1947 SelectionSource source)
1949 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1950 // handles selection messages...
1951 // TODO: extend config options to allow user to control if selections may be
1952 // shared between viewports.
1953 boolean iSentTheSelection = (av == source || (source instanceof AlignViewport && ((AlignmentViewport) source)
1954 .getSequenceSetId().equals(av.getSequenceSetId())));
1955 if (iSentTheSelection || !av.followSelection)
1961 * Ignore the selection if there is one of our own pending.
1963 if (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1969 * Check for selection in a view of which this one is a dna/protein
1972 if (selectionFromTranslation(seqsel, colsel, source))
1977 // do we want to thread this ? (contention with seqsel and colsel locks, I
1980 * only copy colsel if there is a real intersection between
1981 * sequence selection and this panel's alignment
1983 boolean repaint = false;
1984 boolean copycolsel = false;
1986 SequenceGroup sgroup = null;
1987 if (seqsel != null && seqsel.getSize() > 0)
1989 if (av.getAlignment() == null)
1991 Cache.log.warn("alignviewport av SeqSetId=" + av.getSequenceSetId()
1992 + " ViewId=" + av.getViewId()
1993 + " 's alignment is NULL! returning immediately.");
1996 sgroup = seqsel.intersect(av.getAlignment(),
1997 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1998 if ((sgroup != null && sgroup.getSize() > 0))
2003 if (sgroup != null && sgroup.getSize() > 0)
2005 av.setSelectionGroup(sgroup);
2009 av.setSelectionGroup(null);
2011 av.isSelectionGroupChanged(true);
2016 // the current selection is unset or from a previous message
2017 // so import the new colsel.
2018 if (colsel == null || colsel.isEmpty())
2020 if (av.getColumnSelection() != null)
2022 av.getColumnSelection().clear();
2028 // TODO: shift colSel according to the intersecting sequences
2029 if (av.getColumnSelection() == null)
2031 av.setColumnSelection(new ColumnSelection(colsel));
2035 av.getColumnSelection().setElementsFrom(colsel);
2038 av.isColSelChanged(true);
2043 && av.hasHiddenColumns()
2044 && (av.getColumnSelection() == null || av.getColumnSelection()
2045 .getHiddenColumns() == null))
2047 System.err.println("Bad things");
2049 if (repaint) // always true!
2051 // probably finessing with multiple redraws here
2052 PaintRefresher.Refresh(this, av.getSequenceSetId());
2053 // ap.paintAlignment(false);
2058 * If this panel is a cdna/protein translation view of the selection source,
2059 * tries to map the source selection to a local one, and returns true. Else
2066 protected boolean selectionFromTranslation(SequenceGroup seqsel,
2067 ColumnSelection colsel, SelectionSource source)
2069 if (!(source instanceof AlignViewportI))
2073 final AlignViewportI sourceAv = (AlignViewportI) source;
2074 if (sourceAv.getCodingComplement() != av
2075 && av.getCodingComplement() != sourceAv)
2081 * Map sequence selection
2083 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
2084 av.setSelectionGroup(sg);
2085 av.isSelectionGroupChanged(true);
2088 * Map column selection
2090 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
2092 av.setColumnSelection(cs);
2094 PaintRefresher.Refresh(this, av.getSequenceSetId());