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.
21 package jalview.appletgui;
23 import jalview.api.AlignViewportI;
24 import jalview.commands.EditCommand;
25 import jalview.commands.EditCommand.Action;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.ColumnSelection;
28 import jalview.datamodel.HiddenColumns;
29 import jalview.datamodel.SearchResultMatchI;
30 import jalview.datamodel.SearchResults;
31 import jalview.datamodel.SearchResultsI;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceFeature;
34 import jalview.datamodel.SequenceGroup;
35 import jalview.datamodel.SequenceI;
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.MappingUtils;
43 import jalview.util.MessageManager;
44 import jalview.viewmodel.AlignmentViewport;
45 import jalview.viewmodel.ViewportRanges;
47 import java.awt.BorderLayout;
49 import java.awt.FontMetrics;
50 import java.awt.Panel;
51 import java.awt.Point;
52 import java.awt.event.InputEvent;
53 import java.awt.event.MouseEvent;
54 import java.awt.event.MouseListener;
55 import java.awt.event.MouseMotionListener;
56 import java.util.Vector;
58 public class SeqPanel extends Panel implements MouseMotionListener,
59 MouseListener, SequenceListener, SelectionListener
62 public SeqCanvas seqCanvas;
64 public AlignmentPanel ap;
66 protected int lastres;
68 protected int startseq;
70 protected AlignViewport av;
72 // if character is inserted or deleted, we will need to recalculate the
74 boolean seqEditOccurred = false;
76 ScrollThread scrollThread = null;
78 boolean mouseDragging = false;
80 boolean editingSeqs = false;
82 boolean groupEditing = false;
86 boolean changeEndSeq = false;
88 boolean changeStartSeq = false;
90 boolean changeEndRes = false;
92 boolean changeStartRes = false;
94 SequenceGroup stretchGroup = null;
96 StringBuffer keyboardNo1;
98 StringBuffer keyboardNo2;
100 boolean mouseWheelPressed = false;
102 Point lastMousePress;
104 EditCommand editCommand;
106 StructureSelectionManager ssm;
108 public SeqPanel(AlignViewport avp, AlignmentPanel p)
112 seqCanvas = new SeqCanvas(avp);
113 setLayout(new BorderLayout());
118 seqCanvas.addMouseMotionListener(this);
119 seqCanvas.addMouseListener(this);
120 ssm = StructureSelectionManager.getStructureSelectionManager(av.applet);
121 ssm.addStructureViewerListener(this);
122 ssm.addSelectionListener(this);
129 if (editCommand != null && editCommand.getSize() > 0)
131 ap.alignFrame.addHistoryItem(editCommand);
132 av.firePropertyChange("alignment", null, av.getAlignment()
139 groupEditing = false;
147 seqCanvas.cursorY = getKeyboardNo1() - 1;
151 void setCursorColumn()
153 seqCanvas.cursorX = getKeyboardNo1() - 1;
157 void setCursorRowAndColumn()
159 if (keyboardNo2 == null)
161 keyboardNo2 = new StringBuffer();
165 seqCanvas.cursorX = getKeyboardNo1() - 1;
166 seqCanvas.cursorY = getKeyboardNo2() - 1;
171 void setCursorPosition()
173 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
175 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
179 void moveCursor(int dx, int dy)
181 seqCanvas.cursorX += dx;
182 seqCanvas.cursorY += dy;
183 if (av.hasHiddenColumns()
184 && !av.getAlignment().getHiddenColumns()
185 .isVisible(seqCanvas.cursorX))
187 int original = seqCanvas.cursorX - dx;
188 int maxWidth = av.getAlignment().getWidth();
190 while (!av.getAlignment().getHiddenColumns()
191 .isVisible(seqCanvas.cursorX)
192 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
194 seqCanvas.cursorX += dx;
197 if (seqCanvas.cursorX >= maxWidth
198 || !av.getAlignment().getHiddenColumns()
199 .isVisible(seqCanvas.cursorX))
201 seqCanvas.cursorX = original;
207 void scrollToVisible()
209 if (seqCanvas.cursorX < 0)
211 seqCanvas.cursorX = 0;
213 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
215 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
218 if (seqCanvas.cursorY < 0)
220 seqCanvas.cursorY = 0;
222 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
224 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
228 if (av.getWrapAlignment())
230 ap.scrollToWrappedVisible(seqCanvas.cursorX);
234 ViewportRanges ranges = av.getRanges();
235 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
236 while (seqCanvas.cursorY < ranges.getStartSeq())
240 while (seqCanvas.cursorY > ranges.getEndSeq())
244 while (seqCanvas.cursorX < hidden.adjustForHiddenColumns(ranges
248 if (!ap.scrollRight(false))
253 while (seqCanvas.cursorX > hidden.adjustForHiddenColumns(ranges
256 if (!ap.scrollRight(true))
262 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
263 seqCanvas.cursorX, seqCanvas.cursorY);
268 void setSelectionAreaAtCursor(boolean topLeft)
270 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
272 if (av.getSelectionGroup() != null)
274 SequenceGroup sg = av.getSelectionGroup();
275 // Find the top and bottom of this group
276 int min = av.getAlignment().getHeight(), max = 0;
277 for (int i = 0; i < sg.getSize(); i++)
279 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
294 sg.setStartRes(seqCanvas.cursorX);
295 if (sg.getEndRes() < seqCanvas.cursorX)
297 sg.setEndRes(seqCanvas.cursorX);
300 min = seqCanvas.cursorY;
304 sg.setEndRes(seqCanvas.cursorX);
305 if (sg.getStartRes() > seqCanvas.cursorX)
307 sg.setStartRes(seqCanvas.cursorX);
310 max = seqCanvas.cursorY + 1;
315 // Only the user can do this
316 av.setSelectionGroup(null);
320 // Now add any sequences between min and max
322 for (int i = min; i < max; i++)
324 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
329 if (av.getSelectionGroup() == null)
331 SequenceGroup sg = new SequenceGroup();
332 sg.setStartRes(seqCanvas.cursorX);
333 sg.setEndRes(seqCanvas.cursorX);
334 sg.addSequence(sequence, false);
335 av.setSelectionGroup(sg);
337 ap.paintAlignment(false);
341 void insertGapAtCursor(boolean group)
343 groupEditing = group;
344 startseq = seqCanvas.cursorY;
345 lastres = seqCanvas.cursorX;
346 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
350 void deleteGapAtCursor(boolean group)
352 groupEditing = group;
353 startseq = seqCanvas.cursorY;
354 lastres = seqCanvas.cursorX + getKeyboardNo1();
355 editSequence(false, seqCanvas.cursorX);
359 void numberPressed(char value)
361 if (keyboardNo1 == null)
363 keyboardNo1 = new StringBuffer();
366 if (keyboardNo2 != null)
368 keyboardNo2.append(value);
372 keyboardNo1.append(value);
380 if (keyboardNo1 != null)
382 int value = Integer.parseInt(keyboardNo1.toString());
386 } catch (Exception x)
397 if (keyboardNo2 != null)
399 int value = Integer.parseInt(keyboardNo2.toString());
403 } catch (Exception x)
411 * Set status message in alignment panel
414 * aligned sequence object
418 * index of sequence in alignment
419 * @return position of res in sequence
421 void setStatusMessage(SequenceI sequence, int res, int seq)
423 // TODO remove duplication of identical gui method
424 StringBuilder text = new StringBuilder(32);
425 String seqno = seq == -1 ? "" : " " + (seq + 1);
426 text.append("Sequence" + seqno + " ID: " + sequence.getName());
428 String residue = null;
430 * Try to translate the display character to residue name (null for gap).
432 final String displayChar = String.valueOf(sequence.getCharAt(res));
433 if (av.getAlignment().isNucleotide())
435 residue = ResidueProperties.nucleotideName.get(displayChar);
438 text.append(" Nucleotide: ").append(residue);
443 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
444 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
448 text.append(" Residue: ").append(residue);
455 pos = sequence.findPosition(res);
456 text.append(" (").append(Integer.toString(pos)).append(")");
459 ap.alignFrame.statusBar.setText(text.toString());
463 * Set the status bar message to highlight the first matched position in
467 * @return true if results were matched, false if not
469 private boolean setStatusMessage(SearchResultsI results)
471 AlignmentI al = this.av.getAlignment();
472 int sequenceIndex = al.findIndex(results);
473 if (sequenceIndex == -1)
477 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
478 for (SearchResultMatchI m : results.getResults())
480 SequenceI seq = m.getSequence();
481 if (seq.getDatasetSequence() != null)
483 seq = seq.getDatasetSequence();
489 * Convert position in sequence (base 1) to sequence character array
492 int start = m.getStart() - m.getSequence().getStart();
493 setStatusMessage(seq, start, sequenceIndex);
501 public void mousePressed(MouseEvent evt)
503 lastMousePress = evt.getPoint();
505 // For now, ignore the mouseWheel font resizing on Macs
506 // As the Button2_mask always seems to be true
507 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
510 mouseWheelPressed = true;
514 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
516 if (evt.isControlDown() || evt.isAltDown())
524 doMousePressedDefineMode(evt);
528 int seq = findSeq(evt);
529 int res = findRes(evt);
531 if (seq < 0 || res < 0)
536 if ((seq < av.getAlignment().getHeight())
537 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
552 public void mouseClicked(MouseEvent evt)
554 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
555 if (evt.getClickCount() > 1)
557 if (av.getSelectionGroup() != null
558 && av.getSelectionGroup().getSize() == 1
559 && av.getSelectionGroup().getEndRes()
560 - av.getSelectionGroup().getStartRes() < 2)
562 av.setSelectionGroup(null);
565 SequenceFeature[] features = findFeaturesAtRes(sequence,
566 sequence.findPosition(findRes(evt)));
568 if (features != null && features.length > 0)
570 SearchResultsI highlight = new SearchResults();
571 highlight.addResult(sequence, features[0].getBegin(),
572 features[0].getEnd());
573 seqCanvas.highlightSearchResults(highlight);
575 if (features != null && features.length > 0)
577 seqCanvas.getFeatureRenderer().amendFeatures(
578 new SequenceI[] { sequence }, features, false, ap, null);
580 seqCanvas.highlightSearchResults(null);
586 public void mouseReleased(MouseEvent evt)
588 mouseDragging = false;
589 mouseWheelPressed = false;
590 ap.paintAlignment(true);
594 doMouseReleasedDefineMode(evt);
602 int startWrapBlock = -1;
604 int wrappedBlock = -1;
606 int findRes(MouseEvent evt)
611 if (av.getWrapAlignment())
614 int hgap = av.getCharHeight();
615 if (av.getScaleAboveWrapped())
617 hgap += av.getCharHeight();
620 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
621 + hgap + seqCanvas.getAnnotationHeight();
625 x -= seqCanvas.LABEL_WEST;
627 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
633 wrappedBlock = y / cHeight;
634 wrappedBlock += av.getRanges().getStartRes() / cwidth;
636 res = wrappedBlock * cwidth + x / av.getCharWidth();
641 res = (x / av.getCharWidth()) + av.getRanges().getStartRes();
644 if (av.hasHiddenColumns())
646 res = av.getAlignment().getHiddenColumns()
647 .adjustForHiddenColumns(res);
654 int findSeq(MouseEvent evt)
656 final int sqnum = findAlRow(evt);
657 return (sqnum < 0) ? 0 : sqnum;
663 * @return row in alignment that was selected (or -1 for column selection)
665 private int findAlRow(MouseEvent evt)
670 if (av.getWrapAlignment())
672 int hgap = av.getCharHeight();
673 if (av.getScaleAboveWrapped())
675 hgap += av.getCharHeight();
678 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
679 + hgap + seqCanvas.getAnnotationHeight();
683 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
692 seq = Math.min((y / av.getCharHeight())
693 + av.getRanges().getStartSeq(),
695 .getAlignment().getHeight() - 1);
705 public void doMousePressed(MouseEvent evt)
708 int seq = findSeq(evt);
709 int res = findRes(evt);
711 if (seq < av.getAlignment().getHeight()
712 && res < av.getAlignment().getSequenceAt(seq).getLength())
714 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
715 // Find the residue's position in the sequence (res is the position
733 public void mouseOverSequence(SequenceI sequence, int index, int pos)
735 String tmp = sequence.hashCode() + index + "";
736 if (lastMessage == null || !lastMessage.equals(tmp))
738 ssm.mouseOverSequence(sequence, index, pos, av);
745 public void highlightSequence(SearchResultsI results)
747 if (av.isFollowHighlight())
749 if (ap.scrollToPosition(results, true))
751 ap.alignFrame.repaint();
754 setStatusMessage(results);
755 seqCanvas.highlightSearchResults(results);
760 public VamsasSource getVamsasSource()
762 return this.ap == null ? null : this.ap.av;
766 public void updateColours(SequenceI seq, int index)
768 System.out.println("update the seqPanel colours");
773 public void mouseMoved(MouseEvent evt)
775 int res = findRes(evt);
776 int seq = findSeq(evt);
778 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
787 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
788 if (res > sequence.getLength())
797 int respos = sequence.findPosition(res);
800 mouseOverSequence(sequence, res, respos);
803 StringBuilder text = new StringBuilder();
804 text.append("Sequence ").append(Integer.toString(seq + 1))
805 .append(" ID: ").append(sequence.getName());
808 final String ch = String.valueOf(sequence.getCharAt(res));
809 if (av.getAlignment().isNucleotide())
811 obj = ResidueProperties.nucleotideName.get(ch);
814 text.append(" Nucleotide: ").append(obj);
819 obj = "X".equalsIgnoreCase(ch) ? "X" : ResidueProperties.aa2Triplet
823 text.append(" Residue: ").append(obj);
829 text.append(" (").append(Integer.toString(respos)).append(")");
832 ap.alignFrame.statusBar.setText(text.toString());
834 StringBuilder tooltipText = new StringBuilder();
835 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
838 for (int g = 0; g < groups.length; g++)
840 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
842 if (!groups[g].getName().startsWith("JTreeGroup")
843 && !groups[g].getName().startsWith("JGroup"))
845 tooltipText.append(groups[g].getName()).append(" ");
847 if (groups[g].getDescription() != null)
849 tooltipText.append(groups[g].getDescription());
851 tooltipText.append("\n");
856 // use aa to see if the mouse pointer is on a
857 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
858 sequence.findPosition(res));
861 while (index < allFeatures.length)
863 SequenceFeature sf = allFeatures[index];
865 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
867 if (sf.getDescription() != null)
869 tooltipText.append(" " + sf.getDescription());
872 if (sf.getValue("status") != null)
874 String status = sf.getValue("status").toString();
875 if (status.length() > 0)
877 tooltipText.append(" (" + sf.getValue("status") + ")");
880 tooltipText.append("\n");
887 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
891 tooltip.setTip(tooltipText.toString());
895 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
897 Vector tmp = new Vector();
898 SequenceFeature[] features = sequence.getSequenceFeatures();
899 if (features != null)
901 for (int i = 0; i < features.length; i++)
903 if (av.getFeaturesDisplayed() == null
904 || !av.getFeaturesDisplayed().isVisible(
905 features[i].getType()))
910 if (features[i].featureGroup != null
911 && !seqCanvas.fr.checkGroupVisibility(
912 features[i].featureGroup, false))
917 if ((features[i].getBegin() <= res)
918 && (features[i].getEnd() >= res))
920 tmp.addElement(features[i]);
925 features = new SequenceFeature[tmp.size()];
926 tmp.copyInto(features);
934 * set when the current UI interaction has resulted in a change that requires
935 * overview shading to be recalculated. this could be changed to something
936 * more expressive that indicates what actually has changed, so selective
937 * redraws can be applied
939 private boolean needOverviewUpdate; // TODO: refactor to avcontroller
942 public void mouseDragged(MouseEvent evt)
944 if (mouseWheelPressed)
946 int oldWidth = av.getCharWidth();
948 // Which is bigger, left-right or up-down?
949 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
952 int fontSize = av.font.getSize();
954 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
958 else if (evt.getY() > lastMousePress.y)
969 new Font(av.font.getName(), av.font.getStyle(), fontSize),
971 av.setCharWidth(oldWidth);
975 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
977 av.setCharWidth(av.getCharWidth() - 1);
979 else if (evt.getX() > lastMousePress.x)
981 av.setCharWidth(av.getCharWidth() + 1);
984 if (av.getCharWidth() < 1)
992 FontMetrics fm = getFontMetrics(av.getFont());
993 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
995 lastMousePress = evt.getPoint();
997 ap.paintAlignment(false);
998 ap.annotationPanel.image = null;
1004 doMouseDraggedDefineMode(evt);
1008 int res = findRes(evt);
1015 if ((lastres == -1) || (lastres == res))
1020 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1022 // dragLeft, delete gap
1023 editSequence(false, res);
1027 editSequence(true, res);
1030 mouseDragging = true;
1031 if (scrollThread != null)
1033 scrollThread.setEvent(evt);
1038 synchronized void editSequence(boolean insertGap, int startres)
1041 int fixedRight = -1;
1042 boolean fixedColumns = false;
1043 SequenceGroup sg = av.getSelectionGroup();
1045 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1047 if (!groupEditing && av.hasHiddenRows())
1049 if (av.isHiddenRepSequence(seq))
1051 sg = av.getRepresentedSequences(seq);
1052 groupEditing = true;
1056 StringBuffer message = new StringBuffer();
1059 message.append(MessageManager.getString("action.edit_group")).append(
1061 if (editCommand == null)
1063 editCommand = new EditCommand(
1064 MessageManager.getString("action.edit_group"));
1069 message.append(MessageManager.getString("label.edit_sequence"))
1070 .append(" " + seq.getName());
1071 String label = seq.getName();
1072 if (label.length() > 10)
1074 label = label.substring(0, 10);
1076 if (editCommand == null)
1078 editCommand = new EditCommand(MessageManager.formatMessage(
1079 "label.edit_params", new String[] { label }));
1085 message.append(" insert ");
1089 message.append(" delete ");
1092 message.append(Math.abs(startres - lastres) + " gaps.");
1093 ap.alignFrame.statusBar.setText(message.toString());
1095 // Are we editing within a selection group?
1097 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1100 fixedColumns = true;
1102 // sg might be null as the user may only see 1 sequence,
1103 // but the sequence represents a group
1106 if (!av.isHiddenRepSequence(seq))
1112 sg = av.getRepresentedSequences(seq);
1115 fixedLeft = sg.getStartRes();
1116 fixedRight = sg.getEndRes();
1118 if ((startres < fixedLeft && lastres >= fixedLeft)
1119 || (startres >= fixedLeft && lastres < fixedLeft)
1120 || (startres > fixedRight && lastres <= fixedRight)
1121 || (startres <= fixedRight && lastres > fixedRight))
1127 if (fixedLeft > startres)
1129 fixedRight = fixedLeft - 1;
1132 else if (fixedRight < startres)
1134 fixedLeft = fixedRight;
1139 if (av.hasHiddenColumns())
1141 fixedColumns = true;
1142 int y1 = av.getAlignment().getHiddenColumns()
1143 .getHiddenBoundaryLeft(startres);
1144 int y2 = av.getAlignment().getHiddenColumns()
1145 .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 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1172 .toArray(new SequenceI[0]);
1177 // If the user has selected the whole sequence, and is dragging to
1178 // the right, we can still extend the alignment and selectionGroup
1179 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1180 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1182 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1183 fixedRight = sg.getEndRes();
1186 // Is it valid with fixed columns??
1187 // Find the next gap before the end
1188 // of the visible region boundary
1189 boolean blank = false;
1190 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1194 for (SequenceI gs : groupSeqs)
1196 for (int j = 0; j < startres - lastres; j++)
1198 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1214 if (sg.getSize() == av.getAlignment().getHeight())
1216 if ((av.hasHiddenColumns() && startres < av.getAlignment()
1217 .getHiddenColumns().getHiddenBoundaryRight(startres)))
1223 int alWidth = av.getAlignment().getWidth();
1224 if (av.hasHiddenRows())
1226 int hwidth = av.getAlignment().getHiddenSequences()
1228 if (hwidth > alWidth)
1233 // We can still insert gaps if the selectionGroup
1234 // contains all the sequences
1235 sg.setEndRes(sg.getEndRes() + startres - lastres);
1236 fixedRight = alWidth + startres - lastres;
1247 else if (!insertGap)
1249 // / Are we able to delete?
1250 // ie are all columns blank?
1252 for (SequenceI gs : groupSeqs)
1254 for (int j = startres; j < lastres; j++)
1256 if (gs.getLength() <= j)
1261 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1263 // Not a gap, block edit not valid
1273 // dragging to the right
1274 if (fixedColumns && fixedRight != -1)
1276 for (int j = lastres; j < startres; j++)
1278 insertChar(j, groupSeqs, fixedRight);
1283 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1284 startres - lastres, av.getAlignment(), true);
1289 // dragging to the left
1290 if (fixedColumns && fixedRight != -1)
1292 for (int j = lastres; j > startres; j--)
1294 deleteChar(startres, groupSeqs, fixedRight);
1299 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1300 lastres - startres, av.getAlignment(), true);
1306 // ///Editing a single sequence///////////
1310 // dragging to the right
1311 if (fixedColumns && fixedRight != -1)
1313 for (int j = lastres; j < startres; j++)
1315 insertChar(j, new SequenceI[] { seq }, fixedRight);
1320 editCommand.appendEdit(Action.INSERT_GAP,
1321 new SequenceI[] { seq }, lastres, startres - lastres,
1322 av.getAlignment(), true);
1327 // dragging to the left
1328 if (fixedColumns && fixedRight != -1)
1330 for (int j = lastres; j > startres; j--)
1332 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1337 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1342 // could be a keyboard edit trying to delete none gaps
1344 for (int m = startres; m < lastres; m++)
1346 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1355 editCommand.appendEdit(Action.DELETE_GAP,
1356 new SequenceI[] { seq }, startres, max,
1357 av.getAlignment(), true);
1364 seqCanvas.repaint();
1367 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1369 int blankColumn = fixedColumn;
1370 for (int s = 0; s < seq.length; s++)
1372 // Find the next gap before the end of the visible region boundary
1373 // If lastCol > j, theres a boundary after the gap insertion
1375 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1377 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1379 // Theres a space, so break and insert the gap
1384 if (blankColumn <= j)
1386 blankColumn = fixedColumn;
1392 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1393 av.getAlignment(), true);
1395 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1400 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1403 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1406 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1407 av.getAlignment(), true);
1410 // ////////////////////////////////////////
1411 // ///Everything below this is for defining the boundary of the rubberband
1412 // ////////////////////////////////////////
1413 public void doMousePressedDefineMode(MouseEvent evt)
1415 if (scrollThread != null)
1417 scrollThread.running = false;
1418 scrollThread = null;
1421 int res = findRes(evt);
1422 int seq = findSeq(evt);
1424 startWrapBlock = wrappedBlock;
1431 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1433 if (sequence == null || res > sequence.getLength())
1438 stretchGroup = av.getSelectionGroup();
1440 if (stretchGroup == null || !stretchGroup.contains(sequence, res))
1442 stretchGroup = av.getAlignment().findGroup(sequence, res);
1443 if (stretchGroup != null)
1445 // only update the current selection if the popup menu has a group to
1447 av.setSelectionGroup(stretchGroup);
1451 // DETECT RIGHT MOUSE BUTTON IN AWT
1452 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1454 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1455 sequence.findPosition(res));
1457 Vector<String> links = null;
1458 if (allFeatures != null)
1460 for (int i = 0; i < allFeatures.length; i++)
1462 if (allFeatures[i].links != null)
1466 links = new Vector<String>();
1468 for (int j = 0; j < allFeatures[i].links.size(); j++)
1470 links.addElement(allFeatures[i].links.elementAt(j));
1475 APopupMenu popup = new APopupMenu(ap, null, links);
1477 popup.show(this, evt.getX(), evt.getY());
1483 seqCanvas.cursorX = findRes(evt);
1484 seqCanvas.cursorY = findSeq(evt);
1485 seqCanvas.repaint();
1489 // Only if left mouse button do we want to change group sizes
1491 if (stretchGroup == null)
1493 // define a new group here
1494 SequenceGroup sg = new SequenceGroup();
1495 sg.setStartRes(res);
1497 sg.addSequence(sequence, false);
1498 av.setSelectionGroup(sg);
1501 if (av.getConservationSelected())
1503 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1506 if (av.getAbovePIDThreshold())
1508 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1515 public void doMouseReleasedDefineMode(MouseEvent evt)
1517 if (stretchGroup == null)
1521 // always do this - annotation has own state
1522 // but defer colourscheme update until hidden sequences are passed in
1523 boolean vischange = stretchGroup.recalcConservation(true);
1524 // here we rely on stretchGroup == av.getSelection()
1525 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup();
1526 if (stretchGroup.cs != null)
1528 stretchGroup.cs.alignmentChanged(stretchGroup,
1529 av.getHiddenRepSequences());
1531 if (stretchGroup.cs.conservationApplied())
1533 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1534 stretchGroup.getName());
1536 if (stretchGroup.cs.getThreshold() > 0)
1538 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1539 stretchGroup.getName());
1542 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1543 ap.paintAlignment(needOverviewUpdate);
1544 needOverviewUpdate = false;
1545 changeEndRes = false;
1546 changeStartRes = false;
1547 stretchGroup = null;
1551 public void doMouseDraggedDefineMode(MouseEvent evt)
1553 int res = findRes(evt);
1554 int y = findSeq(evt);
1556 if (wrappedBlock != startWrapBlock)
1561 if (stretchGroup == null)
1566 mouseDragging = true;
1568 if (y > av.getAlignment().getHeight())
1570 y = av.getAlignment().getHeight() - 1;
1573 if (res >= av.getAlignment().getWidth())
1575 res = av.getAlignment().getWidth() - 1;
1578 if (stretchGroup.getEndRes() == res)
1580 // Edit end res position of selected group
1581 changeEndRes = true;
1583 else if (stretchGroup.getStartRes() == res)
1585 // Edit start res position of selected group
1586 changeStartRes = true;
1596 if (res > (stretchGroup.getStartRes() - 1))
1598 stretchGroup.setEndRes(res);
1599 needOverviewUpdate |= av.isSelectionDefinedGroup();
1602 else if (changeStartRes)
1604 if (res < (stretchGroup.getEndRes() + 1))
1606 stretchGroup.setStartRes(res);
1607 needOverviewUpdate |= av.isSelectionDefinedGroup();
1611 int dragDirection = 0;
1617 else if (y < oldSeq)
1622 while ((y != oldSeq) && (oldSeq > -1)
1623 && (y < av.getAlignment().getHeight()))
1625 // This routine ensures we don't skip any sequences, as the
1626 // selection is quite slow.
1627 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1629 oldSeq += dragDirection;
1636 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1638 if (stretchGroup.getSequences(null).contains(nextSeq))
1640 stretchGroup.deleteSequence(seq, false);
1641 needOverviewUpdate |= av.isSelectionDefinedGroup();
1647 stretchGroup.addSequence(seq, false);
1650 stretchGroup.addSequence(nextSeq, false);
1651 needOverviewUpdate |= av.isSelectionDefinedGroup();
1660 if (res > av.getRanges().getEndRes()
1661 || res < av.getRanges().getStartRes()
1662 || y < av.getRanges().getStartSeq()
1663 || y > av.getRanges().getEndSeq())
1668 if (scrollThread != null)
1670 scrollThread.setEvent(evt);
1673 seqCanvas.repaint();
1677 public void mouseEntered(MouseEvent e)
1684 if (scrollThread != null)
1686 scrollThread.running = false;
1687 scrollThread = null;
1692 public void mouseExited(MouseEvent e)
1694 if (av.getWrapAlignment())
1699 if (mouseDragging && scrollThread == null)
1701 scrollThread = new ScrollThread();
1705 void scrollCanvas(MouseEvent evt)
1709 if (scrollThread != null)
1711 scrollThread.running = false;
1712 scrollThread = null;
1714 mouseDragging = false;
1718 if (scrollThread == null)
1720 scrollThread = new ScrollThread();
1723 mouseDragging = true;
1724 scrollThread.setEvent(evt);
1729 // this class allows scrolling off the bottom of the visible alignment
1730 class ScrollThread extends Thread
1734 boolean running = false;
1736 public ScrollThread()
1741 public void setEvent(MouseEvent e)
1746 public void stopScrolling()
1761 if (mouseDragging && evt.getY() < 0
1762 && av.getRanges().getStartSeq() > 0)
1764 running = ap.scrollUp(true);
1767 if (mouseDragging && evt.getY() >= getSize().height
1768 && av.getAlignment().getHeight() > av.getRanges()
1771 running = ap.scrollUp(false);
1774 if (mouseDragging && evt.getX() < 0)
1776 running = ap.scrollRight(false);
1779 else if (mouseDragging && evt.getX() >= getSize().width)
1781 running = ap.scrollRight(true);
1788 } catch (Exception ex)
1796 * modify current selection according to a received message.
1799 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1800 HiddenColumns hidden, SelectionSource source)
1802 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1803 // handles selection messages...
1804 // TODO: extend config options to allow user to control if selections may be
1805 // shared between viewports.
1807 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignmentViewport) source)
1808 .getSequenceSetId().equals(av.getSequenceSetId()))))
1814 * Check for selection in a view of which this one is a dna/protein
1817 if (selectionFromTranslation(seqsel, colsel, hidden, source))
1822 // do we want to thread this ? (contention with seqsel and colsel locks, I
1825 * only copy colsel if there is a real intersection between
1826 * sequence selection and this panel's alignment
1828 boolean repaint = false;
1829 boolean copycolsel = false;
1830 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1832 SequenceGroup sgroup = null;
1833 if (seqsel != null && seqsel.getSize() > 0)
1835 if (av.getAlignment() == null)
1838 .println("Selection message: alignviewport av SeqSetId="
1839 + av.getSequenceSetId() + " ViewId="
1841 + " 's alignment is NULL! returning immediatly.");
1844 sgroup = seqsel.intersect(av.getAlignment(),
1845 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1846 if ((sgroup != null && sgroup.getSize() > 0))
1851 if (sgroup != null && sgroup.getSize() > 0)
1853 av.setSelectionGroup(sgroup);
1857 av.setSelectionGroup(null);
1859 repaint = av.isSelectionGroupChanged(true);
1862 && (av.getColumnSelection() == null || !av
1863 .isColSelChanged(true)))
1865 // the current selection is unset or from a previous message
1866 // so import the new colsel.
1867 if (colsel == null || colsel.isEmpty())
1869 if (av.getColumnSelection() != null)
1871 av.getColumnSelection().clear();
1876 // TODO: shift colSel according to the intersecting sequences
1877 if (av.getColumnSelection() == null)
1879 av.setColumnSelection(new ColumnSelection(colsel));
1883 av.getColumnSelection().setElementsFrom(colsel,
1884 av.getAlignment().getHiddenColumns());
1887 repaint |= av.isColSelChanged(true);
1890 && av.hasHiddenColumns()
1891 && (av.getColumnSelection() == null || av.getAlignment()
1892 .getHiddenColumns().getHiddenRegions() == null))
1894 System.err.println("Bad things");
1898 ap.scalePanelHolder.repaint();
1904 * scroll to the given row/column - or nearest visible location
1909 public void scrollTo(int row, int column)
1912 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1913 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1914 ap.scrollTo(column, column, row, true, true);
1918 * scroll to the given row - or nearest visible location
1922 public void scrollToRow(int row)
1925 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1926 ap.scrollTo(ap.av.getRanges().getStartRes(), ap.av.getRanges()
1927 .getStartRes(), row, true, true);
1931 * scroll to the given column - or nearest visible location
1935 public void scrollToColumn(int column)
1938 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1939 ap.scrollTo(column, column, ap.av.getRanges().getStartSeq(), true, true);
1943 * If this panel is a cdna/protein translation view of the selection source,
1944 * tries to map the source selection to a local one, and returns true. Else
1951 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1952 ColumnSelection colsel, HiddenColumns hidden,
1953 SelectionSource source)
1955 if (!(source instanceof AlignViewportI))
1959 final AlignViewportI sourceAv = (AlignViewportI) source;
1960 if (sourceAv.getCodingComplement() != av
1961 && av.getCodingComplement() != sourceAv)
1967 * Map sequence selection
1969 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1970 av.setSelectionGroup(sg);
1971 av.isSelectionGroupChanged(true);
1974 * Map column selection
1976 // ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1978 ColumnSelection cs = new ColumnSelection();
1979 HiddenColumns hs = new HiddenColumns();
1980 MappingUtils.mapColumnSelection(colsel, hidden, sourceAv, av, cs, hs);
1981 av.setColumnSelection(cs);
1982 av.getAlignment().setHiddenColumns(hs);
1984 ap.scalePanelHolder.repaint();