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.commands.EditCommand;
25 import jalview.commands.EditCommand.Action;
26 import jalview.commands.EditCommand.Edit;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.ColumnSelection;
29 import jalview.datamodel.SearchResults;
30 import jalview.datamodel.SearchResults.Match;
31 import jalview.datamodel.Sequence;
32 import jalview.datamodel.SequenceFeature;
33 import jalview.datamodel.SequenceGroup;
34 import jalview.datamodel.SequenceI;
35 import jalview.io.SequenceAnnotationReport;
36 import jalview.schemes.ResidueProperties;
37 import jalview.structure.SelectionListener;
38 import jalview.structure.SelectionSource;
39 import jalview.structure.SequenceListener;
40 import jalview.structure.StructureSelectionManager;
41 import jalview.structure.VamsasSource;
42 import jalview.util.Comparison;
43 import jalview.util.MappingUtils;
44 import jalview.util.MessageManager;
45 import jalview.viewmodel.AlignmentViewport;
47 import java.awt.BorderLayout;
48 import java.awt.Color;
50 import java.awt.FontMetrics;
51 import java.awt.Point;
52 import java.awt.event.MouseEvent;
53 import java.awt.event.MouseListener;
54 import java.awt.event.MouseMotionListener;
55 import java.awt.event.MouseWheelEvent;
56 import java.awt.event.MouseWheelListener;
57 import java.util.List;
58 import java.util.Vector;
60 import javax.swing.JOptionPane;
61 import javax.swing.JPanel;
62 import javax.swing.ToolTipManager;
68 * @version $Revision: 1.130 $
70 public class SeqPanel extends JPanel implements MouseListener,
71 MouseMotionListener, MouseWheelListener, SequenceListener,
76 public SeqCanvas seqCanvas;
79 public AlignmentPanel ap;
81 protected int lastres;
83 protected int startseq;
85 protected AlignViewport av;
87 ScrollThread scrollThread = null;
89 boolean mouseDragging = false;
91 boolean editingSeqs = false;
93 boolean groupEditing = false;
95 // ////////////////////////////////////////
96 // ///Everything below this is for defining the boundary of the rubberband
97 // ////////////////////////////////////////
100 boolean changeEndSeq = false;
102 boolean changeStartSeq = false;
104 boolean changeEndRes = false;
106 boolean changeStartRes = false;
108 SequenceGroup stretchGroup = null;
110 boolean remove = false;
112 Point lastMousePress;
114 boolean mouseWheelPressed = false;
116 StringBuffer keyboardNo1;
118 StringBuffer keyboardNo2;
120 java.net.URL linkImageURL;
122 private final SequenceAnnotationReport seqARep;
124 StringBuffer tooltipText = new StringBuffer();
128 EditCommand editCommand;
130 StructureSelectionManager ssm;
132 SearchResults lastSearchResults;
135 * Creates a new SeqPanel object.
142 public SeqPanel(AlignViewport av, AlignmentPanel ap)
144 linkImageURL = getClass().getResource("/images/link.gif");
145 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
146 ToolTipManager.sharedInstance().registerComponent(this);
147 ToolTipManager.sharedInstance().setInitialDelay(0);
148 ToolTipManager.sharedInstance().setDismissDelay(10000);
150 setBackground(Color.white);
152 seqCanvas = new SeqCanvas(ap);
153 setLayout(new BorderLayout());
154 add(seqCanvas, BorderLayout.CENTER);
160 addMouseMotionListener(this);
161 addMouseListener(this);
162 addMouseWheelListener(this);
163 ssm = av.getStructureSelectionManager();
164 ssm.addStructureViewerListener(this);
165 ssm.addSelectionListener(this);
169 int startWrapBlock = -1;
171 int wrappedBlock = -1;
174 * Returns the aligned sequence position (base 0) at the mouse position, or
175 * the closest visible one
180 int findRes(MouseEvent evt)
185 if (av.getWrapAlignment())
188 int hgap = av.getCharHeight();
189 if (av.getScaleAboveWrapped())
191 hgap += av.getCharHeight();
194 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
195 + hgap + seqCanvas.getAnnotationHeight();
199 x -= seqCanvas.LABEL_WEST;
201 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
207 wrappedBlock = y / cHeight;
208 wrappedBlock += av.getStartRes() / cwidth;
210 res = wrappedBlock * cwidth + x / av.getCharWidth();
215 if (x > seqCanvas.getX() + seqCanvas.getWidth())
217 // make sure we calculate relative to visible alignment, rather than
219 x = seqCanvas.getX() + seqCanvas.getWidth();
221 res = (x / av.getCharWidth()) + av.getStartRes();
222 if (res > av.getEndRes())
225 res = av.getEndRes();
229 if (av.hasHiddenColumns())
231 res = av.getColumnSelection().adjustForHiddenColumns(res);
238 int findSeq(MouseEvent evt)
243 if (av.getWrapAlignment())
245 int hgap = av.getCharHeight();
246 if (av.getScaleAboveWrapped())
248 hgap += av.getCharHeight();
251 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
252 + hgap + seqCanvas.getAnnotationHeight();
256 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
261 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
262 .getAlignment().getHeight() - 1);
269 * When all of a sequence of edits are complete, put the resulting edit list
270 * on the history stack (undo list), and reset flags for editing in progress.
276 if (editCommand != null && editCommand.getSize() > 0)
278 ap.alignFrame.addHistoryItem(editCommand);
279 av.firePropertyChange("alignment", null, av.getAlignment()
285 * Tidy up come what may...
290 groupEditing = false;
299 seqCanvas.cursorY = getKeyboardNo1() - 1;
303 void setCursorColumn()
305 seqCanvas.cursorX = getKeyboardNo1() - 1;
309 void setCursorRowAndColumn()
311 if (keyboardNo2 == null)
313 keyboardNo2 = new StringBuffer();
317 seqCanvas.cursorX = getKeyboardNo1() - 1;
318 seqCanvas.cursorY = getKeyboardNo2() - 1;
323 void setCursorPosition()
325 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
327 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
331 void moveCursor(int dx, int dy)
333 seqCanvas.cursorX += dx;
334 seqCanvas.cursorY += dy;
335 if (av.hasHiddenColumns()
336 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
338 int original = seqCanvas.cursorX - dx;
339 int maxWidth = av.getAlignment().getWidth();
341 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
342 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
344 seqCanvas.cursorX += dx;
347 if (seqCanvas.cursorX >= maxWidth
348 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
350 seqCanvas.cursorX = original;
357 void scrollToVisible()
359 if (seqCanvas.cursorX < 0)
361 seqCanvas.cursorX = 0;
363 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
365 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
368 if (seqCanvas.cursorY < 0)
370 seqCanvas.cursorY = 0;
372 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
374 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
378 if (av.getWrapAlignment())
380 ap.scrollToWrappedVisible(seqCanvas.cursorX);
384 while (seqCanvas.cursorY < av.startSeq)
388 while (seqCanvas.cursorY + 1 > av.endSeq)
392 if (!av.getWrapAlignment())
394 while (seqCanvas.cursorX < av.getColumnSelection()
395 .adjustForHiddenColumns(av.startRes))
397 if (!ap.scrollRight(false))
402 while (seqCanvas.cursorX > av.getColumnSelection()
403 .adjustForHiddenColumns(av.endRes))
405 if (!ap.scrollRight(true))
412 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
413 seqCanvas.cursorX, seqCanvas.cursorY);
418 void setSelectionAreaAtCursor(boolean topLeft)
420 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
422 if (av.getSelectionGroup() != null)
424 SequenceGroup sg = av.getSelectionGroup();
425 // Find the top and bottom of this group
426 int min = av.getAlignment().getHeight(), max = 0;
427 for (int i = 0; i < sg.getSize(); i++)
429 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
444 sg.setStartRes(seqCanvas.cursorX);
445 if (sg.getEndRes() < seqCanvas.cursorX)
447 sg.setEndRes(seqCanvas.cursorX);
450 min = seqCanvas.cursorY;
454 sg.setEndRes(seqCanvas.cursorX);
455 if (sg.getStartRes() > seqCanvas.cursorX)
457 sg.setStartRes(seqCanvas.cursorX);
460 max = seqCanvas.cursorY + 1;
465 // Only the user can do this
466 av.setSelectionGroup(null);
470 // Now add any sequences between min and max
471 sg.getSequences(null).clear();
472 for (int i = min; i < max; i++)
474 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
479 if (av.getSelectionGroup() == null)
481 SequenceGroup sg = new SequenceGroup();
482 sg.setStartRes(seqCanvas.cursorX);
483 sg.setEndRes(seqCanvas.cursorX);
484 sg.addSequence(sequence, false);
485 av.setSelectionGroup(sg);
488 ap.paintAlignment(false);
492 void insertGapAtCursor(boolean group)
494 groupEditing = group;
495 startseq = seqCanvas.cursorY;
496 lastres = seqCanvas.cursorX;
497 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
501 void deleteGapAtCursor(boolean group)
503 groupEditing = group;
504 startseq = seqCanvas.cursorY;
505 lastres = seqCanvas.cursorX + getKeyboardNo1();
506 editSequence(false, false, seqCanvas.cursorX);
510 void insertNucAtCursor(boolean group, String nuc)
512 groupEditing = group;
513 startseq = seqCanvas.cursorY;
514 lastres = seqCanvas.cursorX;
515 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
519 void numberPressed(char value)
521 if (keyboardNo1 == null)
523 keyboardNo1 = new StringBuffer();
526 if (keyboardNo2 != null)
528 keyboardNo2.append(value);
532 keyboardNo1.append(value);
540 if (keyboardNo1 != null)
542 int value = Integer.parseInt(keyboardNo1.toString());
546 } catch (Exception x)
557 if (keyboardNo2 != null)
559 int value = Integer.parseInt(keyboardNo2.toString());
563 } catch (Exception x)
577 public void mouseReleased(MouseEvent evt)
579 mouseDragging = false;
580 mouseWheelPressed = false;
584 doMouseReleasedDefineMode(evt);
598 public void mousePressed(MouseEvent evt)
600 lastMousePress = evt.getPoint();
602 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
604 mouseWheelPressed = true;
608 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
610 if (evt.isAltDown() || evt.isControlDown())
618 doMousePressedDefineMode(evt);
622 int seq = findSeq(evt);
623 int res = findRes(evt);
625 if (seq < 0 || res < 0)
630 if ((seq < av.getAlignment().getHeight())
631 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
648 public void mouseOverSequence(SequenceI sequence, int index, int pos)
650 String tmp = sequence.hashCode() + " " + index + " " + pos;
652 if (lastMessage == null || !lastMessage.equals(tmp))
654 // System.err.println("mouseOver Sequence: "+tmp);
655 ssm.mouseOverSequence(sequence, index, pos, av);
661 * Highlight the mapped region described by the search results object (unless
662 * unchanged). This supports highlight of protein while mousing over linked
663 * cDNA and vice versa. The status bar is also updated to show the location of
664 * the start of the highlighted region.
667 public void highlightSequence(SearchResults results)
669 if (results == null || results.equals(lastSearchResults))
673 lastSearchResults = results;
675 if (av.isFollowHighlight())
678 * if scrollToPosition requires a scroll adjustment, this flag prevents
679 * another scroll event being propagated back to the originator
681 * @see AlignmentPanel#adjustmentValueChanged
683 ap.setDontScrollComplement(true);
684 if (ap.scrollToPosition(results, false))
686 seqCanvas.revalidate();
689 setStatusMessage(results);
690 seqCanvas.highlightSearchResults(results);
694 public VamsasSource getVamsasSource()
696 return this.ap == null ? null : this.ap.av;
700 public void updateColours(SequenceI seq, int index)
702 System.out.println("update the seqPanel colours");
713 public void mouseMoved(MouseEvent evt)
717 // This is because MacOSX creates a mouseMoved
718 // If control is down, other platforms will not.
722 int res = findRes(evt);
723 int seq = findSeq(evt);
725 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
730 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
732 if (res >= sequence.getLength())
737 pos = setStatusMessage(sequence, res, seq);
738 if (ssm != null && pos > -1)
740 mouseOverSequence(sequence, res, pos);
743 tooltipText.setLength(6); // Cuts the buffer back to <html>
745 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
748 for (int g = 0; g < groups.length; g++)
750 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
752 if (!groups[g].getName().startsWith("JTreeGroup")
753 && !groups[g].getName().startsWith("JGroup"))
755 tooltipText.append(groups[g].getName());
758 if (groups[g].getDescription() != null)
760 tooltipText.append(": " + groups[g].getDescription());
766 // use aa to see if the mouse pointer is on a
767 if (av.isShowSequenceFeatures())
770 List<SequenceFeature> features = ap.getFeatureRenderer()
771 .findFeaturesAtRes(sequence.getDatasetSequence(),
772 rpos = sequence.findPosition(res));
773 seqARep.appendFeatures(tooltipText, rpos, features,
774 this.ap.getSeqPanel().seqCanvas.fr.getMinMax());
776 if (tooltipText.length() == 6) // <html></html>
778 setToolTipText(null);
783 if (lastTooltip == null
784 || !lastTooltip.equals(tooltipText.toString()))
786 String formatedTooltipText = JvSwingUtils.wrapTooltip(true,
787 tooltipText.toString());
788 // String formatedTooltipText = tooltipText.toString();
789 setToolTipText(formatedTooltipText);
790 lastTooltip = tooltipText.toString();
797 private Point lastp = null;
802 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
805 public Point getToolTipLocation(MouseEvent event)
807 int x = event.getX(), w = getWidth();
808 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
811 if (!event.isShiftDown() || p == null)
813 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
814 event.getX() + wdth, event.getY() - 20) : null;
817 * TODO: try to modify position region is not obcured by tooltip
825 * Set status message in alignment panel
828 * aligned sequence object
832 * index of sequence in alignment
833 * @return position of res in sequence
835 int setStatusMessage(SequenceI sequence, int res, int seq)
837 StringBuilder text = new StringBuilder(32);
840 * Sequence number (if known), and sequence name.
842 String seqno = seq == -1 ? "" : " " + (seq + 1);
843 text.append("Sequence" + seqno + " ID: " + sequence.getName());
845 String residue = null;
847 * Try to translate the display character to residue name (null for gap).
849 final String displayChar = String.valueOf(sequence.getCharAt(res));
850 if (av.getAlignment().isNucleotide())
852 residue = ResidueProperties.nucleotideName.get(displayChar);
855 text.append(" Nucleotide: ").append(residue);
860 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
861 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
865 text.append(" Residue: ").append(residue);
872 pos = sequence.findPosition(res);
873 text.append(" (").append(Integer.toString(pos)).append(")");
875 ap.alignFrame.statusBar.setText(text.toString());
880 * Set the status bar message to highlight the first matched position in
885 private void setStatusMessage(SearchResults results)
887 AlignmentI al = this.av.getAlignment();
888 int sequenceIndex = al.findIndex(results);
889 if (sequenceIndex == -1)
893 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
894 for (Match m : results.getResults())
896 SequenceI seq = m.getSequence();
897 if (seq.getDatasetSequence() != null)
899 seq = seq.getDatasetSequence();
905 * Convert position in sequence (base 1) to sequence character array
908 int start = m.getStart() - m.getSequence().getStart();
909 setStatusMessage(seq, start, sequenceIndex);
922 public void mouseDragged(MouseEvent evt)
924 if (mouseWheelPressed)
926 int oldWidth = av.getCharWidth();
928 // Which is bigger, left-right or up-down?
929 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
930 .getX() - lastMousePress.getX()))
932 int fontSize = av.font.getSize();
934 if (evt.getY() < lastMousePress.getY())
938 else if (evt.getY() > lastMousePress.getY())
949 new Font(av.font.getName(), av.font.getStyle(), fontSize),
951 av.setCharWidth(oldWidth);
956 if (evt.getX() < lastMousePress.getX() && av.getCharWidth() > 1)
958 av.setCharWidth(av.getCharWidth() - 1);
960 else if (evt.getX() > lastMousePress.getX())
962 av.setCharWidth(av.getCharWidth() + 1);
965 ap.paintAlignment(false);
968 FontMetrics fm = getFontMetrics(av.getFont());
969 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
971 lastMousePress = evt.getPoint();
978 doMouseDraggedDefineMode(evt);
982 int res = findRes(evt);
989 if ((lastres == -1) || (lastres == res))
994 if ((res < av.getAlignment().getWidth()) && (res < lastres))
996 // dragLeft, delete gap
997 editSequence(false, false, res);
1001 editSequence(true, false, res);
1004 mouseDragging = true;
1005 if (scrollThread != null)
1007 scrollThread.setEvent(evt);
1011 // TODO: Make it more clever than many booleans
1012 synchronized void editSequence(boolean insertGap, boolean editSeq,
1016 int fixedRight = -1;
1017 boolean fixedColumns = false;
1018 SequenceGroup sg = av.getSelectionGroup();
1020 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1022 // No group, but the sequence may represent a group
1023 if (!groupEditing && av.hasHiddenRows())
1025 if (av.isHiddenRepSequence(seq))
1027 sg = av.getRepresentedSequences(seq);
1028 groupEditing = true;
1032 StringBuilder message = new StringBuilder(64);
1035 message.append("Edit group:");
1036 if (editCommand == null)
1038 editCommand = new EditCommand(
1039 MessageManager.getString("action.edit_group"));
1044 message.append("Edit sequence: " + seq.getName());
1045 String label = seq.getName();
1046 if (label.length() > 10)
1048 label = label.substring(0, 10);
1050 if (editCommand == null)
1052 editCommand = new EditCommand(MessageManager.formatMessage(
1053 "label.edit_params", new String[] { label }));
1059 message.append(" insert ");
1063 message.append(" delete ");
1066 message.append(Math.abs(startres - lastres) + " gaps.");
1067 ap.alignFrame.statusBar.setText(message.toString());
1069 // Are we editing within a selection group?
1071 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1074 fixedColumns = true;
1076 // sg might be null as the user may only see 1 sequence,
1077 // but the sequence represents a group
1080 if (!av.isHiddenRepSequence(seq))
1085 sg = av.getRepresentedSequences(seq);
1088 fixedLeft = sg.getStartRes();
1089 fixedRight = sg.getEndRes();
1091 if ((startres < fixedLeft && lastres >= fixedLeft)
1092 || (startres >= fixedLeft && lastres < fixedLeft)
1093 || (startres > fixedRight && lastres <= fixedRight)
1094 || (startres <= fixedRight && lastres > fixedRight))
1100 if (fixedLeft > startres)
1102 fixedRight = fixedLeft - 1;
1105 else if (fixedRight < startres)
1107 fixedLeft = fixedRight;
1112 if (av.hasHiddenColumns())
1114 fixedColumns = true;
1115 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1116 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1118 if ((insertGap && startres > y1 && lastres < y1)
1119 || (!insertGap && startres < y2 && lastres > y2))
1125 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1126 // Selection spans a hidden region
1127 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1135 fixedRight = y2 - 1;
1142 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1143 int g, groupSize = vseqs.size();
1144 SequenceI[] groupSeqs = new SequenceI[groupSize];
1145 for (g = 0; g < groupSeqs.length; g++)
1147 groupSeqs[g] = vseqs.get(g);
1153 // If the user has selected the whole sequence, and is dragging to
1154 // the right, we can still extend the alignment and selectionGroup
1155 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1156 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1158 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1159 fixedRight = sg.getEndRes();
1162 // Is it valid with fixed columns??
1163 // Find the next gap before the end
1164 // of the visible region boundary
1165 boolean blank = false;
1166 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1170 for (g = 0; g < groupSize; g++)
1172 for (int j = 0; j < startres - lastres; j++)
1174 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1175 .getCharAt(fixedRight - j)))
1190 if (sg.getSize() == av.getAlignment().getHeight())
1192 if ((av.hasHiddenColumns() && startres < av
1193 .getColumnSelection().getHiddenBoundaryRight(startres)))
1199 int alWidth = av.getAlignment().getWidth();
1200 if (av.hasHiddenRows())
1202 int hwidth = av.getAlignment().getHiddenSequences()
1204 if (hwidth > alWidth)
1209 // We can still insert gaps if the selectionGroup
1210 // contains all the sequences
1211 sg.setEndRes(sg.getEndRes() + startres - lastres);
1212 fixedRight = alWidth + startres - lastres;
1223 else if (!insertGap)
1225 // / Are we able to delete?
1226 // ie are all columns blank?
1228 for (g = 0; g < groupSize; g++)
1230 for (int j = startres; j < lastres; j++)
1232 if (groupSeqs[g].getLength() <= j)
1237 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1239 // Not a gap, block edit not valid
1249 // dragging to the right
1250 if (fixedColumns && fixedRight != -1)
1252 for (int j = lastres; j < startres; j++)
1254 insertChar(j, groupSeqs, fixedRight);
1259 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1265 // dragging to the left
1266 if (fixedColumns && fixedRight != -1)
1268 for (int j = lastres; j > startres; j--)
1270 deleteChar(startres, groupSeqs, fixedRight);
1275 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1282 // ///Editing a single sequence///////////
1286 // dragging to the right
1287 if (fixedColumns && fixedRight != -1)
1289 for (int j = lastres; j < startres; j++)
1291 insertChar(j, new SequenceI[] { seq }, fixedRight);
1296 appendEdit(Action.INSERT_GAP, new SequenceI[] { seq }, lastres,
1297 startres - lastres);
1304 // dragging to the left
1305 if (fixedColumns && fixedRight != -1)
1307 for (int j = lastres; j > startres; j--)
1309 if (!Comparison.isGap(seq.getCharAt(startres)))
1314 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1319 // could be a keyboard edit trying to delete none gaps
1321 for (int m = startres; m < lastres; m++)
1323 if (!Comparison.isGap(seq.getCharAt(m)))
1332 appendEdit(Action.DELETE_GAP, new SequenceI[] { seq },
1338 {// insertGap==false AND editSeq==TRUE;
1339 if (fixedColumns && fixedRight != -1)
1341 for (int j = lastres; j < startres; j++)
1343 insertChar(j, new SequenceI[] { seq }, fixedRight);
1348 appendEdit(Action.INSERT_NUC, new SequenceI[] { seq }, lastres,
1349 startres - lastres);
1356 seqCanvas.repaint();
1359 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1361 int blankColumn = fixedColumn;
1362 for (int s = 0; s < seq.length; s++)
1364 // Find the next gap before the end of the visible region boundary
1365 // If lastCol > j, theres a boundary after the gap insertion
1367 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1369 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1371 // Theres a space, so break and insert the gap
1376 if (blankColumn <= j)
1378 blankColumn = fixedColumn;
1384 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1386 appendEdit(Action.INSERT_GAP, seq, j, 1);
1391 * Helper method to add and perform one edit action.
1398 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1402 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1403 av.getAlignment().getGapCharacter());
1405 editCommand.appendEdit(edit, av.getAlignment(), true, null);
1408 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1411 appendEdit(Action.DELETE_GAP, seq, j, 1);
1413 appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1);
1423 public void mouseEntered(MouseEvent e)
1430 if (scrollThread != null)
1432 scrollThread.running = false;
1433 scrollThread = null;
1444 public void mouseExited(MouseEvent e)
1446 if (av.getWrapAlignment())
1453 scrollThread = new ScrollThread();
1458 public void mouseClicked(MouseEvent evt)
1460 SequenceGroup sg = null;
1461 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1462 if (evt.getClickCount() > 1)
1464 sg = av.getSelectionGroup();
1465 if (sg != null && sg.getSize() == 1
1466 && sg.getEndRes() - sg.getStartRes() < 2)
1468 av.setSelectionGroup(null);
1471 List<SequenceFeature> features = seqCanvas.getFeatureRenderer()
1472 .findFeaturesAtRes(sequence.getDatasetSequence(),
1473 sequence.findPosition(findRes(evt)));
1475 if (features != null && features.size() > 0)
1477 SearchResults highlight = new SearchResults();
1478 highlight.addResult(sequence, features.get(0).getBegin(), features
1480 seqCanvas.highlightSearchResults(highlight);
1482 if (features != null && features.size() > 0)
1484 seqCanvas.getFeatureRenderer().amendFeatures(
1485 new SequenceI[] { sequence },
1486 features.toArray(new SequenceFeature[features.size()]),
1489 seqCanvas.highlightSearchResults(null);
1495 public void mouseWheelMoved(MouseWheelEvent e)
1498 if (e.getWheelRotation() > 0)
1500 if (e.isShiftDown())
1502 ap.scrollRight(true);
1512 if (e.isShiftDown())
1514 ap.scrollRight(false);
1521 // TODO Update tooltip for new position.
1530 public void doMousePressedDefineMode(MouseEvent evt)
1532 int res = findRes(evt);
1533 int seq = findSeq(evt);
1536 startWrapBlock = wrappedBlock;
1538 if (av.getWrapAlignment() && seq > av.getAlignment().getHeight())
1540 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1541 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1542 MessageManager.getString("label.wrapped_view_no_edit"),
1543 JOptionPane.WARNING_MESSAGE);
1547 if (seq < 0 || res < 0)
1552 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1554 if ((sequence == null) || (res > sequence.getLength()))
1559 stretchGroup = av.getSelectionGroup();
1561 if (stretchGroup == null)
1563 stretchGroup = av.getAlignment().findGroup(sequence);
1565 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1566 && (res < stretchGroup.getEndRes()))
1568 av.setSelectionGroup(stretchGroup);
1572 stretchGroup = null;
1575 else if (!stretchGroup.getSequences(null).contains(sequence)
1576 || (stretchGroup.getStartRes() > res)
1577 || (stretchGroup.getEndRes() < res))
1579 stretchGroup = null;
1581 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1583 if (allGroups != null)
1585 for (int i = 0; i < allGroups.length; i++)
1587 if ((allGroups[i].getStartRes() <= res)
1588 && (allGroups[i].getEndRes() >= res))
1590 stretchGroup = allGroups[i];
1596 av.setSelectionGroup(stretchGroup);
1600 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1602 List<SequenceFeature> allFeatures = ap.getFeatureRenderer()
1603 .findFeaturesAtRes(sequence.getDatasetSequence(),
1604 sequence.findPosition(res));
1605 Vector links = new Vector();
1606 for (SequenceFeature sf : allFeatures)
1608 if (sf.links != null)
1610 for (int j = 0; j < sf.links.size(); j++)
1612 links.addElement(sf.links.elementAt(j));
1617 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1618 pop.show(this, evt.getX(), evt.getY());
1624 seqCanvas.cursorX = findRes(evt);
1625 seqCanvas.cursorY = findSeq(evt);
1626 seqCanvas.repaint();
1630 if (stretchGroup == null)
1632 // Only if left mouse button do we want to change group sizes
1634 // define a new group here
1635 SequenceGroup sg = new SequenceGroup();
1636 sg.setStartRes(res);
1638 sg.addSequence(sequence, false);
1639 av.setSelectionGroup(sg);
1643 if (av.getConservationSelected())
1645 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1649 if (av.getAbovePIDThreshold())
1651 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1654 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1656 // Edit end res position of selected group
1657 changeEndRes = true;
1659 else if ((stretchGroup != null)
1660 && (stretchGroup.getStartRes() == res))
1662 // Edit end res position of selected group
1663 changeStartRes = true;
1665 stretchGroup.getWidth();
1668 seqCanvas.repaint();
1677 public void doMouseReleasedDefineMode(MouseEvent evt)
1679 if (stretchGroup == null)
1684 stretchGroup.recalcConservation(); // always do this - annotation has own
1686 if (stretchGroup.cs != null)
1688 stretchGroup.cs.alignmentChanged(stretchGroup,
1689 av.getHiddenRepSequences());
1691 if (stretchGroup.cs.conservationApplied())
1693 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1694 stretchGroup.getName());
1698 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1699 stretchGroup.getName());
1702 PaintRefresher.Refresh(this, av.getSequenceSetId());
1703 ap.paintAlignment(true);
1705 changeEndRes = false;
1706 changeStartRes = false;
1707 stretchGroup = null;
1717 public void doMouseDraggedDefineMode(MouseEvent evt)
1719 int res = findRes(evt);
1720 int y = findSeq(evt);
1722 if (wrappedBlock != startWrapBlock)
1727 if (stretchGroup == null)
1732 if (res >= av.getAlignment().getWidth())
1734 res = av.getAlignment().getWidth() - 1;
1737 if (stretchGroup.getEndRes() == res)
1739 // Edit end res position of selected group
1740 changeEndRes = true;
1742 else if (stretchGroup.getStartRes() == res)
1744 // Edit start res position of selected group
1745 changeStartRes = true;
1748 if (res < av.getStartRes())
1750 res = av.getStartRes();
1755 if (res > (stretchGroup.getStartRes() - 1))
1757 stretchGroup.setEndRes(res);
1760 else if (changeStartRes)
1762 if (res < (stretchGroup.getEndRes() + 1))
1764 stretchGroup.setStartRes(res);
1768 int dragDirection = 0;
1774 else if (y < oldSeq)
1779 while ((y != oldSeq) && (oldSeq > -1)
1780 && (y < av.getAlignment().getHeight()))
1782 // This routine ensures we don't skip any sequences, as the
1783 // selection is quite slow.
1784 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1786 oldSeq += dragDirection;
1793 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1795 if (stretchGroup.getSequences(null).contains(nextSeq))
1797 stretchGroup.deleteSequence(seq, false);
1803 stretchGroup.addSequence(seq, false);
1806 stretchGroup.addSequence(nextSeq, false);
1815 mouseDragging = true;
1817 if (scrollThread != null)
1819 scrollThread.setEvent(evt);
1822 seqCanvas.repaint();
1825 void scrollCanvas(MouseEvent evt)
1829 if (scrollThread != null)
1831 scrollThread.running = false;
1832 scrollThread = null;
1834 mouseDragging = false;
1838 if (scrollThread == null)
1840 scrollThread = new ScrollThread();
1843 mouseDragging = true;
1844 scrollThread.setEvent(evt);
1849 // this class allows scrolling off the bottom of the visible alignment
1850 class ScrollThread extends Thread
1854 boolean running = false;
1856 public ScrollThread()
1861 public void setEvent(MouseEvent e)
1866 public void stopScrolling()
1880 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1882 running = ap.scrollUp(true);
1885 if (mouseDragging && (evt.getY() >= getHeight())
1886 && (av.getAlignment().getHeight() > av.getEndSeq()))
1888 running = ap.scrollUp(false);
1891 if (mouseDragging && (evt.getX() < 0))
1893 running = ap.scrollRight(false);
1895 else if (mouseDragging && (evt.getX() >= getWidth()))
1897 running = ap.scrollRight(true);
1904 } catch (Exception ex)
1912 * modify current selection according to a received message.
1915 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1916 SelectionSource source)
1918 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1919 // handles selection messages...
1920 // TODO: extend config options to allow user to control if selections may be
1921 // shared between viewports.
1922 boolean iSentTheSelection = (av == source || (source instanceof AlignViewport && ((AlignmentViewport) source)
1923 .getSequenceSetId().equals(av.getSequenceSetId())));
1924 if (iSentTheSelection || !av.followSelection)
1930 * Ignore the selection if there is one of our own pending.
1932 if (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1938 * Check for selection in a view of which this one is a dna/protein
1941 if (selectionFromTranslation(seqsel, colsel, source))
1946 // do we want to thread this ? (contention with seqsel and colsel locks, I
1948 // rules are: colsel is copied if there is a real intersection between
1949 // sequence selection
1950 boolean repaint = false;
1951 boolean copycolsel = true;
1953 SequenceGroup sgroup = null;
1954 if (seqsel != null && seqsel.getSize() > 0)
1956 if (av.getAlignment() == null)
1958 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1959 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1960 + " 's alignment is NULL! returning immediately.");
1963 sgroup = seqsel.intersect(av.getAlignment(),
1964 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1965 if ((sgroup == null || sgroup.getSize() == 0)
1966 || (colsel == null || colsel.isEmpty()))
1968 // don't copy columns if the region didn't intersect.
1972 if (sgroup != null && sgroup.getSize() > 0)
1974 av.setSelectionGroup(sgroup);
1978 av.setSelectionGroup(null);
1980 av.isSelectionGroupChanged(true);
1985 // the current selection is unset or from a previous message
1986 // so import the new colsel.
1987 if (colsel == null || colsel.isEmpty())
1989 if (av.getColumnSelection() != null)
1991 av.getColumnSelection().clear();
1997 // TODO: shift colSel according to the intersecting sequences
1998 if (av.getColumnSelection() == null)
2000 av.setColumnSelection(new ColumnSelection(colsel));
2004 av.getColumnSelection().setElementsFrom(colsel);
2007 av.isColSelChanged(true);
2012 && av.hasHiddenColumns()
2013 && (av.getColumnSelection() == null || av.getColumnSelection()
2014 .getHiddenColumns() == null))
2016 System.err.println("Bad things");
2018 if (repaint) // always true!
2020 // probably finessing with multiple redraws here
2021 PaintRefresher.Refresh(this, av.getSequenceSetId());
2022 // ap.paintAlignment(false);
2027 * If this panel is a cdna/protein translation view of the selection source,
2028 * tries to map the source selection to a local one, and returns true. Else
2035 protected boolean selectionFromTranslation(SequenceGroup seqsel,
2036 ColumnSelection colsel, SelectionSource source)
2038 if (!(source instanceof AlignViewportI))
2042 final AlignViewportI sourceAv = (AlignViewportI) source;
2043 if (sourceAv.getCodingComplement() != av
2044 && av.getCodingComplement() != sourceAv)
2050 * Map sequence selection
2052 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
2053 av.setSelectionGroup(sg);
2054 av.isSelectionGroupChanged(true);
2057 * Map column selection
2059 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
2061 av.setColumnSelection(cs);
2062 av.isColSelChanged(true);
2064 PaintRefresher.Refresh(this, av.getSequenceSetId());