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()
196 + seqCanvas.getAnnotationHeight();
200 x -= seqCanvas.LABEL_WEST;
202 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
208 wrappedBlock = y / cHeight;
209 wrappedBlock += av.getStartRes() / cwidth;
211 res = wrappedBlock * cwidth + x / av.getCharWidth();
216 if (x > seqCanvas.getX() + seqCanvas.getWidth())
218 // make sure we calculate relative to visible alignment, rather than
220 x = seqCanvas.getX() + seqCanvas.getWidth();
222 res = (x / av.getCharWidth()) + av.getStartRes();
223 if (res > av.getEndRes())
226 res = av.getEndRes();
230 if (av.hasHiddenColumns())
232 res = av.getColumnSelection().adjustForHiddenColumns(res);
239 int findSeq(MouseEvent evt)
244 if (av.getWrapAlignment())
246 int hgap = av.getCharHeight();
247 if (av.getScaleAboveWrapped())
249 hgap += av.getCharHeight();
252 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
254 + seqCanvas.getAnnotationHeight();
258 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
263 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
264 .getAlignment().getHeight() - 1);
271 * When all of a sequence of edits are complete, put the resulting edit list
272 * on the history stack (undo list), and reset flags for editing in progress.
278 if (editCommand != null && editCommand.getSize() > 0)
280 ap.alignFrame.addHistoryItem(editCommand);
281 av.firePropertyChange("alignment", null, av.getAlignment()
287 * Tidy up come what may...
292 groupEditing = false;
301 seqCanvas.cursorY = getKeyboardNo1() - 1;
305 void setCursorColumn()
307 seqCanvas.cursorX = getKeyboardNo1() - 1;
311 void setCursorRowAndColumn()
313 if (keyboardNo2 == null)
315 keyboardNo2 = new StringBuffer();
319 seqCanvas.cursorX = getKeyboardNo1() - 1;
320 seqCanvas.cursorY = getKeyboardNo2() - 1;
325 void setCursorPosition()
327 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
329 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
333 void moveCursor(int dx, int dy)
335 seqCanvas.cursorX += dx;
336 seqCanvas.cursorY += dy;
337 if (av.hasHiddenColumns()
338 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
340 int original = seqCanvas.cursorX - dx;
341 int maxWidth = av.getAlignment().getWidth();
343 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
344 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
346 seqCanvas.cursorX += dx;
349 if (seqCanvas.cursorX >= maxWidth
350 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
352 seqCanvas.cursorX = original;
359 void scrollToVisible()
361 if (seqCanvas.cursorX < 0)
363 seqCanvas.cursorX = 0;
365 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
367 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
370 if (seqCanvas.cursorY < 0)
372 seqCanvas.cursorY = 0;
374 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
376 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
380 if (av.getWrapAlignment())
382 ap.scrollToWrappedVisible(seqCanvas.cursorX);
386 while (seqCanvas.cursorY < av.startSeq)
390 while (seqCanvas.cursorY + 1 > av.endSeq)
394 if (!av.getWrapAlignment())
396 while (seqCanvas.cursorX < av.getColumnSelection()
397 .adjustForHiddenColumns(av.startRes))
399 if (!ap.scrollRight(false))
404 while (seqCanvas.cursorX > av.getColumnSelection()
405 .adjustForHiddenColumns(av.endRes))
407 if (!ap.scrollRight(true))
414 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
415 seqCanvas.cursorX, seqCanvas.cursorY);
420 void setSelectionAreaAtCursor(boolean topLeft)
422 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
424 if (av.getSelectionGroup() != null)
426 SequenceGroup sg = av.getSelectionGroup();
427 // Find the top and bottom of this group
428 int min = av.getAlignment().getHeight(), max = 0;
429 for (int i = 0; i < sg.getSize(); i++)
431 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
446 sg.setStartRes(seqCanvas.cursorX);
447 if (sg.getEndRes() < seqCanvas.cursorX)
449 sg.setEndRes(seqCanvas.cursorX);
452 min = seqCanvas.cursorY;
456 sg.setEndRes(seqCanvas.cursorX);
457 if (sg.getStartRes() > seqCanvas.cursorX)
459 sg.setStartRes(seqCanvas.cursorX);
462 max = seqCanvas.cursorY + 1;
467 // Only the user can do this
468 av.setSelectionGroup(null);
472 // Now add any sequences between min and max
473 sg.getSequences(null).clear();
474 for (int i = min; i < max; i++)
476 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
481 if (av.getSelectionGroup() == null)
483 SequenceGroup sg = new SequenceGroup();
484 sg.setStartRes(seqCanvas.cursorX);
485 sg.setEndRes(seqCanvas.cursorX);
486 sg.addSequence(sequence, false);
487 av.setSelectionGroup(sg);
490 ap.paintAlignment(false);
494 void insertGapAtCursor(boolean group)
496 groupEditing = group;
497 startseq = seqCanvas.cursorY;
498 lastres = seqCanvas.cursorX;
499 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
503 void deleteGapAtCursor(boolean group)
505 groupEditing = group;
506 startseq = seqCanvas.cursorY;
507 lastres = seqCanvas.cursorX + getKeyboardNo1();
508 editSequence(false, false, seqCanvas.cursorX);
512 void insertNucAtCursor(boolean group, String nuc)
514 groupEditing = group;
515 startseq = seqCanvas.cursorY;
516 lastres = seqCanvas.cursorX;
517 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
521 void numberPressed(char value)
523 if (keyboardNo1 == null)
525 keyboardNo1 = new StringBuffer();
528 if (keyboardNo2 != null)
530 keyboardNo2.append(value);
534 keyboardNo1.append(value);
542 if (keyboardNo1 != null)
544 int value = Integer.parseInt(keyboardNo1.toString());
548 } catch (Exception x)
559 if (keyboardNo2 != null)
561 int value = Integer.parseInt(keyboardNo2.toString());
565 } catch (Exception x)
579 public void mouseReleased(MouseEvent evt)
581 mouseDragging = false;
582 mouseWheelPressed = false;
586 doMouseReleasedDefineMode(evt);
600 public void mousePressed(MouseEvent evt)
602 lastMousePress = evt.getPoint();
604 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
606 mouseWheelPressed = true;
610 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
612 if (evt.isAltDown() || evt.isControlDown())
620 doMousePressedDefineMode(evt);
624 int seq = findSeq(evt);
625 int res = findRes(evt);
627 if (seq < 0 || res < 0)
632 if ((seq < av.getAlignment().getHeight())
633 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
650 public void mouseOverSequence(SequenceI sequence, int index, int pos)
652 String tmp = sequence.hashCode() + " " + index + " " + pos;
654 if (lastMessage == null || !lastMessage.equals(tmp))
656 // System.err.println("mouseOver Sequence: "+tmp);
657 ssm.mouseOverSequence(sequence, index, pos, av);
663 * Highlight the mapped region described by the search results object (unless
664 * unchanged). This supports highlight of protein while mousing over linked
665 * cDNA and vice versa. The status bar is also updated to show the location of
666 * the start of the highlighted region.
669 public void highlightSequence(SearchResults results)
671 if (results == null || results.equals(lastSearchResults)) {
674 lastSearchResults = results;
676 if (av.isFollowHighlight())
679 * if scrollToPosition requires a scroll adjustment, this flag prevents
680 * another scroll event being propagated back to the originator
682 * @see AlignmentPanel#adjustmentValueChanged
684 ap.setFollowingComplementScroll(true);
685 if (ap.scrollToPosition(results, false))
687 seqCanvas.revalidate();
690 setStatusMessage(results);
691 seqCanvas.highlightSearchResults(results);
695 public VamsasSource getVamsasSource()
697 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().findFeaturesAtRes(
771 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)
804 public Point getToolTipLocation(MouseEvent event)
806 int x = event.getX(), w = getWidth();
807 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
810 if (!event.isShiftDown() || p == null)
812 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
813 event.getX() + wdth, event.getY() - 20) : null;
816 * TODO: try to modify position region is not obcured by tooltip
824 * Set status message in alignment panel
827 * aligned sequence object
831 * index of sequence in alignment
832 * @return position of res in sequence
834 int setStatusMessage(SequenceI sequence, int res, int seq)
836 StringBuilder text = new StringBuilder(32);
839 * Sequence number (if known), and sequence name.
841 String seqno = seq == -1 ? "" : " " + (seq + 1);
842 text.append("Sequence" + seqno + " ID: " + sequence.getName());
844 String residue = null;
846 * Try to translate the display character to residue name (null for gap).
848 final String displayChar = String.valueOf(sequence.getCharAt(res));
849 if (av.getAlignment().isNucleotide())
851 residue = ResidueProperties.nucleotideName.get(displayChar);
854 text.append(" Nucleotide: ").append(residue);
859 residue = "X".equalsIgnoreCase(displayChar) ? "X"
860 : ResidueProperties.aa2Triplet.get(displayChar);
863 text.append(" Residue: ").append(residue);
870 pos = sequence.findPosition(res);
871 text.append(" (").append(Integer.toString(pos)).append(")");
873 ap.alignFrame.statusBar.setText(text.toString());
878 * Set the status bar message to highlight the first matched position in
883 private void setStatusMessage(SearchResults results)
885 AlignmentI al = this.av.getAlignment();
886 int sequenceIndex = al.findIndex(results);
887 if (sequenceIndex == -1)
891 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
892 for (Match m : results.getResults())
894 SequenceI seq = m.getSequence();
895 if (seq.getDatasetSequence() != null)
897 seq = seq.getDatasetSequence();
903 * Convert position in sequence (base 1) to sequence character array
906 int start = m.getStart() - 1;
907 setStatusMessage(seq, start, sequenceIndex);
920 public void mouseDragged(MouseEvent evt)
922 if (mouseWheelPressed)
924 int oldWidth = av.getCharWidth();
926 // Which is bigger, left-right or up-down?
927 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
928 .getX() - lastMousePress.getX()))
930 int fontSize = av.font.getSize();
932 if (evt.getY() < lastMousePress.getY())
936 else if (evt.getY() > lastMousePress.getY())
947 new Font(av.font.getName(), av.font.getStyle(), fontSize),
949 av.setCharWidth(oldWidth);
954 if (evt.getX() < lastMousePress.getX() && av.getCharWidth() > 1)
956 av.setCharWidth(av.getCharWidth() - 1);
958 else if (evt.getX() > lastMousePress.getX())
960 av.setCharWidth(av.getCharWidth() + 1);
963 ap.paintAlignment(false);
966 FontMetrics fm = getFontMetrics(av.getFont());
967 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
969 lastMousePress = evt.getPoint();
976 doMouseDraggedDefineMode(evt);
980 int res = findRes(evt);
987 if ((lastres == -1) || (lastres == res))
992 if ((res < av.getAlignment().getWidth()) && (res < lastres))
994 // dragLeft, delete gap
995 editSequence(false, false, res);
999 editSequence(true, false, res);
1002 mouseDragging = true;
1003 if (scrollThread != null)
1005 scrollThread.setEvent(evt);
1009 // TODO: Make it more clever than many booleans
1010 synchronized void editSequence(boolean insertGap, boolean editSeq,
1014 int fixedRight = -1;
1015 boolean fixedColumns = false;
1016 SequenceGroup sg = av.getSelectionGroup();
1018 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1020 // No group, but the sequence may represent a group
1021 if (!groupEditing && av.hasHiddenRows())
1023 if (av.isHiddenRepSequence(seq))
1025 sg = av.getRepresentedSequences(seq);
1026 groupEditing = true;
1030 StringBuilder message = new StringBuilder(64);
1033 message.append("Edit group:");
1034 if (editCommand == null)
1036 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
1041 message.append("Edit sequence: " + seq.getName());
1042 String label = seq.getName();
1043 if (label.length() > 10)
1045 label = label.substring(0, 10);
1047 if (editCommand == null)
1049 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
1055 message.append(" insert ");
1059 message.append(" delete ");
1062 message.append(Math.abs(startres - lastres) + " gaps.");
1063 ap.alignFrame.statusBar.setText(message.toString());
1065 // Are we editing within a selection group?
1067 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1070 fixedColumns = true;
1072 // sg might be null as the user may only see 1 sequence,
1073 // but the sequence represents a group
1076 if (!av.isHiddenRepSequence(seq))
1081 sg = av.getRepresentedSequences(seq);
1084 fixedLeft = sg.getStartRes();
1085 fixedRight = sg.getEndRes();
1087 if ((startres < fixedLeft && lastres >= fixedLeft)
1088 || (startres >= fixedLeft && lastres < fixedLeft)
1089 || (startres > fixedRight && lastres <= fixedRight)
1090 || (startres <= fixedRight && lastres > fixedRight))
1096 if (fixedLeft > startres)
1098 fixedRight = fixedLeft - 1;
1101 else if (fixedRight < startres)
1103 fixedLeft = fixedRight;
1108 if (av.hasHiddenColumns())
1110 fixedColumns = true;
1111 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1112 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1114 if ((insertGap && startres > y1 && lastres < y1)
1115 || (!insertGap && startres < y2 && lastres > y2))
1121 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1122 // Selection spans a hidden region
1123 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1131 fixedRight = y2 - 1;
1138 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1139 int g, groupSize = vseqs.size();
1140 SequenceI[] groupSeqs = new SequenceI[groupSize];
1141 for (g = 0; g < groupSeqs.length; g++)
1143 groupSeqs[g] = vseqs.get(g);
1149 // If the user has selected the whole sequence, and is dragging to
1150 // the right, we can still extend the alignment and selectionGroup
1151 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1152 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1154 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1155 fixedRight = sg.getEndRes();
1158 // Is it valid with fixed columns??
1159 // Find the next gap before the end
1160 // of the visible region boundary
1161 boolean blank = false;
1162 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1166 for (g = 0; g < groupSize; g++)
1168 for (int j = 0; j < startres - lastres; j++)
1170 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1171 .getCharAt(fixedRight - j)))
1186 if (sg.getSize() == av.getAlignment().getHeight())
1188 if ((av.hasHiddenColumns() && startres < av
1189 .getColumnSelection().getHiddenBoundaryRight(startres)))
1195 int alWidth = av.getAlignment().getWidth();
1196 if (av.hasHiddenRows())
1198 int hwidth = av.getAlignment().getHiddenSequences()
1200 if (hwidth > alWidth)
1205 // We can still insert gaps if the selectionGroup
1206 // contains all the sequences
1207 sg.setEndRes(sg.getEndRes() + startres - lastres);
1208 fixedRight = alWidth + startres - lastres;
1219 else if (!insertGap)
1221 // / Are we able to delete?
1222 // ie are all columns blank?
1224 for (g = 0; g < groupSize; g++)
1226 for (int j = startres; j < lastres; j++)
1228 if (groupSeqs[g].getLength() <= j)
1233 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1235 // Not a gap, block edit not valid
1245 // dragging to the right
1246 if (fixedColumns && fixedRight != -1)
1248 for (int j = lastres; j < startres; j++)
1250 insertChar(j, groupSeqs, fixedRight);
1255 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1261 // dragging to the left
1262 if (fixedColumns && fixedRight != -1)
1264 for (int j = lastres; j > startres; j--)
1266 deleteChar(startres, groupSeqs, fixedRight);
1271 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1278 // ///Editing a single sequence///////////
1282 // dragging to the right
1283 if (fixedColumns && fixedRight != -1)
1285 for (int j = lastres; j < startres; j++)
1287 insertChar(j, new SequenceI[]
1288 { seq }, fixedRight);
1293 appendEdit(Action.INSERT_GAP, new SequenceI[]
1294 { seq }, lastres, startres - lastres);
1301 // dragging to the left
1302 if (fixedColumns && fixedRight != -1)
1304 for (int j = lastres; j > startres; j--)
1306 if (!Comparison.isGap(seq.getCharAt(startres)))
1311 deleteChar(startres, new SequenceI[]
1312 { seq }, fixedRight);
1317 // could be a keyboard edit trying to delete none gaps
1319 for (int m = startres; m < lastres; m++)
1321 if (!Comparison.isGap(seq.getCharAt(m)))
1330 appendEdit(Action.DELETE_GAP, new SequenceI[]
1331 { seq }, startres, max);
1336 {// insertGap==false AND editSeq==TRUE;
1337 if (fixedColumns && fixedRight != -1)
1339 for (int j = lastres; j < startres; j++)
1341 insertChar(j, new SequenceI[]
1342 { seq }, fixedRight);
1347 appendEdit(Action.INSERT_NUC, new SequenceI[]
1348 { seq }, lastres, startres - lastres);
1355 seqCanvas.repaint();
1358 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1360 int blankColumn = fixedColumn;
1361 for (int s = 0; s < seq.length; s++)
1363 // Find the next gap before the end of the visible region boundary
1364 // If lastCol > j, theres a boundary after the gap insertion
1366 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1368 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1370 // Theres a space, so break and insert the gap
1375 if (blankColumn <= j)
1377 blankColumn = fixedColumn;
1383 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1385 appendEdit(Action.INSERT_GAP, seq, j, 1);
1390 * Helper method to add and perform one edit action.
1397 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1401 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1402 av.getAlignment().getGapCharacter());
1404 editCommand.appendEdit(edit, av.getAlignment(),
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().findFeaturesAtRes(
1472 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(),
1479 features.get(0).getEnd());
1480 seqCanvas.highlightSearchResults(highlight);
1482 if (features != null && features.size()> 0)
1484 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1485 { sequence }, features.toArray(new SequenceFeature[features.size()]), false, ap);
1487 seqCanvas.highlightSearchResults(null);
1493 public void mouseWheelMoved(MouseWheelEvent e)
1496 if (e.getWheelRotation() > 0)
1498 if (e.isShiftDown())
1500 ap.scrollRight(true);
1510 if (e.isShiftDown())
1512 ap.scrollRight(false);
1519 // TODO Update tooltip for new position.
1528 public void doMousePressedDefineMode(MouseEvent evt)
1530 int res = findRes(evt);
1531 int seq = findSeq(evt);
1534 startWrapBlock = wrappedBlock;
1536 if (av.getWrapAlignment() && seq > av.getAlignment().getHeight())
1538 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1539 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1540 MessageManager.getString("label.wrapped_view_no_edit"),
1541 JOptionPane.WARNING_MESSAGE);
1545 if (seq < 0 || res < 0)
1550 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1552 if ((sequence == null) || (res > sequence.getLength()))
1557 stretchGroup = av.getSelectionGroup();
1559 if (stretchGroup == null)
1561 stretchGroup = av.getAlignment().findGroup(sequence);
1563 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1564 && (res < stretchGroup.getEndRes()))
1566 av.setSelectionGroup(stretchGroup);
1570 stretchGroup = null;
1573 else if (!stretchGroup.getSequences(null).contains(sequence)
1574 || (stretchGroup.getStartRes() > res)
1575 || (stretchGroup.getEndRes() < res))
1577 stretchGroup = null;
1579 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1581 if (allGroups != null)
1583 for (int i = 0; i < allGroups.length; i++)
1585 if ((allGroups[i].getStartRes() <= res)
1586 && (allGroups[i].getEndRes() >= res))
1588 stretchGroup = allGroups[i];
1594 av.setSelectionGroup(stretchGroup);
1598 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1600 List<SequenceFeature> allFeatures = ap.getFeatureRenderer().findFeaturesAtRes(
1601 sequence.getDatasetSequence(), sequence.findPosition(res));
1602 Vector links = new Vector();
1603 for (SequenceFeature sf:allFeatures)
1605 if (sf.links != null)
1607 for (int j = 0; j < sf.links.size(); j++)
1609 links.addElement(sf.links.elementAt(j));
1614 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1615 pop.show(this, evt.getX(), evt.getY());
1621 seqCanvas.cursorX = findRes(evt);
1622 seqCanvas.cursorY = findSeq(evt);
1623 seqCanvas.repaint();
1627 if (stretchGroup == null)
1629 // Only if left mouse button do we want to change group sizes
1631 // define a new group here
1632 SequenceGroup sg = new SequenceGroup();
1633 sg.setStartRes(res);
1635 sg.addSequence(sequence, false);
1636 av.setSelectionGroup(sg);
1640 if (av.getConservationSelected())
1642 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1646 if (av.getAbovePIDThreshold())
1648 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1651 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1653 // Edit end res position of selected group
1654 changeEndRes = true;
1656 else if ((stretchGroup != null)
1657 && (stretchGroup.getStartRes() == res))
1659 // Edit end res position of selected group
1660 changeStartRes = true;
1662 stretchGroup.getWidth();
1665 seqCanvas.repaint();
1674 public void doMouseReleasedDefineMode(MouseEvent evt)
1676 if (stretchGroup == null)
1681 stretchGroup.recalcConservation(); // always do this - annotation has own
1683 if (stretchGroup.cs != null)
1685 stretchGroup.cs.alignmentChanged(stretchGroup,
1686 av.getHiddenRepSequences());
1688 if (stretchGroup.cs.conservationApplied())
1690 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1691 stretchGroup.getName());
1695 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1696 stretchGroup.getName());
1699 PaintRefresher.Refresh(this, av.getSequenceSetId());
1700 ap.paintAlignment(true);
1702 changeEndRes = false;
1703 changeStartRes = false;
1704 stretchGroup = null;
1714 public void doMouseDraggedDefineMode(MouseEvent evt)
1716 int res = findRes(evt);
1717 int y = findSeq(evt);
1719 if (wrappedBlock != startWrapBlock)
1724 if (stretchGroup == null)
1729 if (res >= av.getAlignment().getWidth())
1731 res = av.getAlignment().getWidth() - 1;
1734 if (stretchGroup.getEndRes() == res)
1736 // Edit end res position of selected group
1737 changeEndRes = true;
1739 else if (stretchGroup.getStartRes() == res)
1741 // Edit start res position of selected group
1742 changeStartRes = true;
1745 if (res < av.getStartRes())
1747 res = av.getStartRes();
1752 if (res > (stretchGroup.getStartRes() - 1))
1754 stretchGroup.setEndRes(res);
1757 else if (changeStartRes)
1759 if (res < (stretchGroup.getEndRes() + 1))
1761 stretchGroup.setStartRes(res);
1765 int dragDirection = 0;
1771 else if (y < oldSeq)
1776 while ((y != oldSeq) && (oldSeq > -1)
1777 && (y < av.getAlignment().getHeight()))
1779 // This routine ensures we don't skip any sequences, as the
1780 // selection is quite slow.
1781 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1783 oldSeq += dragDirection;
1790 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1792 if (stretchGroup.getSequences(null).contains(nextSeq))
1794 stretchGroup.deleteSequence(seq, false);
1800 stretchGroup.addSequence(seq, false);
1803 stretchGroup.addSequence(nextSeq, false);
1812 mouseDragging = true;
1814 if (scrollThread != null)
1816 scrollThread.setEvent(evt);
1819 seqCanvas.repaint();
1822 void scrollCanvas(MouseEvent evt)
1826 if (scrollThread != null)
1828 scrollThread.running = false;
1829 scrollThread = null;
1831 mouseDragging = false;
1835 if (scrollThread == null)
1837 scrollThread = new ScrollThread();
1840 mouseDragging = true;
1841 scrollThread.setEvent(evt);
1846 // this class allows scrolling off the bottom of the visible alignment
1847 class ScrollThread extends Thread
1851 boolean running = false;
1853 public ScrollThread()
1858 public void setEvent(MouseEvent e)
1863 public void stopScrolling()
1877 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1879 running = ap.scrollUp(true);
1882 if (mouseDragging && (evt.getY() >= getHeight())
1883 && (av.getAlignment().getHeight() > av.getEndSeq()))
1885 running = ap.scrollUp(false);
1888 if (mouseDragging && (evt.getX() < 0))
1890 running = ap.scrollRight(false);
1892 else if (mouseDragging && (evt.getX() >= getWidth()))
1894 running = ap.scrollRight(true);
1901 } catch (Exception ex)
1909 * modify current selection according to a received message.
1912 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1913 SelectionSource source)
1915 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1916 // handles selection messages...
1917 // TODO: extend config options to allow user to control if selections may be
1918 // shared between viewports.
1919 boolean iSentTheSelection = (av == source
1920 || (source instanceof AlignViewport && ((AlignmentViewport) source)
1921 .getSequenceSetId().equals(av.getSequenceSetId())));
1922 if (iSentTheSelection || !av.followSelection)
1928 * Ignore the selection if there is one of our own pending.
1930 if (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1936 * Check for selection in a view of which this one is a dna/protein
1939 if (selectionFromTranslation(seqsel, colsel, source))
1944 // do we want to thread this ? (contention with seqsel and colsel locks, I
1946 // rules are: colsel is copied if there is a real intersection between
1947 // sequence selection
1948 boolean repaint = false;
1949 boolean copycolsel = true;
1951 SequenceGroup sgroup = null;
1952 if (seqsel != null && seqsel.getSize() > 0)
1954 if (av.getAlignment() == null)
1956 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1957 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1958 + " 's alignment is NULL! returning immediately.");
1961 sgroup = seqsel.intersect(av.getAlignment(),
1962 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1963 if ((sgroup == null || sgroup.getSize() == 0)
1964 || (colsel == null || colsel.size() == 0))
1966 // don't copy columns if the region didn't intersect.
1970 if (sgroup != null && sgroup.getSize() > 0)
1972 av.setSelectionGroup(sgroup);
1976 av.setSelectionGroup(null);
1978 av.isSelectionGroupChanged(true);
1983 // the current selection is unset or from a previous message
1984 // so import the new colsel.
1985 if (colsel == null || colsel.size() == 0)
1987 if (av.getColumnSelection() != null)
1989 av.getColumnSelection().clear();
1995 // TODO: shift colSel according to the intersecting sequences
1996 if (av.getColumnSelection() == null)
1998 av.setColumnSelection(new ColumnSelection(colsel));
2002 av.getColumnSelection().setElementsFrom(colsel);
2005 av.isColSelChanged(true);
2010 && av.hasHiddenColumns()
2011 && (av.getColumnSelection() == null || av.getColumnSelection()
2012 .getHiddenColumns() == null))
2014 System.err.println("Bad things");
2016 if (repaint) // always true!
2018 // probably finessing with multiple redraws here
2019 PaintRefresher.Refresh(this, av.getSequenceSetId());
2020 // ap.paintAlignment(false);
2025 * If this panel is a cdna/protein translation view of the selection source,
2026 * tries to map the source selection to a local one, and returns true. Else
2033 protected boolean selectionFromTranslation(SequenceGroup seqsel,
2034 ColumnSelection colsel, SelectionSource source)
2036 if (!(source instanceof AlignViewportI)) {
2039 final AlignViewportI sourceAv = (AlignViewportI) source;
2040 if (sourceAv.getCodingComplement() != av && av.getCodingComplement() != sourceAv)
2046 * Map sequence selection
2048 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
2049 av.setSelectionGroup(sg);
2050 av.isSelectionGroupChanged(true);
2053 * Map column selection
2055 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
2057 av.setColumnSelection(cs);
2058 av.isColSelChanged(true);
2060 PaintRefresher.Refresh(this, av.getSequenceSetId());