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 + 1 > 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);
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)
968 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
969 av.setCharWidth(oldWidth);
973 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
975 av.setCharWidth(av.getCharWidth() - 1);
977 else if (evt.getX() > lastMousePress.x)
979 av.setCharWidth(av.getCharWidth() + 1);
982 if (av.getCharWidth() < 1)
990 FontMetrics fm = getFontMetrics(av.getFont());
991 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
993 lastMousePress = evt.getPoint();
995 ap.paintAlignment(false);
996 ap.annotationPanel.image = null;
1002 doMouseDraggedDefineMode(evt);
1006 int res = findRes(evt);
1013 if ((lastres == -1) || (lastres == res))
1018 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1020 // dragLeft, delete gap
1021 editSequence(false, res);
1025 editSequence(true, res);
1028 mouseDragging = true;
1029 if (scrollThread != null)
1031 scrollThread.setEvent(evt);
1036 synchronized void editSequence(boolean insertGap, int startres)
1039 int fixedRight = -1;
1040 boolean fixedColumns = false;
1041 SequenceGroup sg = av.getSelectionGroup();
1043 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1045 if (!groupEditing && av.hasHiddenRows())
1047 if (av.isHiddenRepSequence(seq))
1049 sg = av.getRepresentedSequences(seq);
1050 groupEditing = true;
1054 StringBuffer message = new StringBuffer();
1057 message.append(MessageManager.getString("action.edit_group")).append(
1059 if (editCommand == null)
1061 editCommand = new EditCommand(
1062 MessageManager.getString("action.edit_group"));
1067 message.append(MessageManager.getString("label.edit_sequence"))
1068 .append(" " + seq.getName());
1069 String label = seq.getName();
1070 if (label.length() > 10)
1072 label = label.substring(0, 10);
1074 if (editCommand == null)
1076 editCommand = new EditCommand(MessageManager.formatMessage(
1077 "label.edit_params", new String[] { label }));
1083 message.append(" insert ");
1087 message.append(" delete ");
1090 message.append(Math.abs(startres - lastres) + " gaps.");
1091 ap.alignFrame.statusBar.setText(message.toString());
1093 // Are we editing within a selection group?
1095 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1098 fixedColumns = true;
1100 // sg might be null as the user may only see 1 sequence,
1101 // but the sequence represents a group
1104 if (!av.isHiddenRepSequence(seq))
1110 sg = av.getRepresentedSequences(seq);
1113 fixedLeft = sg.getStartRes();
1114 fixedRight = sg.getEndRes();
1116 if ((startres < fixedLeft && lastres >= fixedLeft)
1117 || (startres >= fixedLeft && lastres < fixedLeft)
1118 || (startres > fixedRight && lastres <= fixedRight)
1119 || (startres <= fixedRight && lastres > fixedRight))
1125 if (fixedLeft > startres)
1127 fixedRight = fixedLeft - 1;
1130 else if (fixedRight < startres)
1132 fixedLeft = fixedRight;
1137 if (av.hasHiddenColumns())
1139 fixedColumns = true;
1140 int y1 = av.getAlignment().getHiddenColumns()
1141 .getHiddenBoundaryLeft(startres);
1142 int y2 = av.getAlignment().getHiddenColumns()
1143 .getHiddenBoundaryRight(startres);
1145 if ((insertGap && startres > y1 && lastres < y1)
1146 || (!insertGap && startres < y2 && lastres > y2))
1152 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1153 // Selection spans a hidden region
1154 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1162 fixedRight = y2 - 1;
1169 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1170 .toArray(new SequenceI[0]);
1175 // If the user has selected the whole sequence, and is dragging to
1176 // the right, we can still extend the alignment and selectionGroup
1177 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1178 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1180 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1181 fixedRight = sg.getEndRes();
1184 // Is it valid with fixed columns??
1185 // Find the next gap before the end
1186 // of the visible region boundary
1187 boolean blank = false;
1188 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1192 for (SequenceI gs : groupSeqs)
1194 for (int j = 0; j < startres - lastres; j++)
1196 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1212 if (sg.getSize() == av.getAlignment().getHeight())
1214 if ((av.hasHiddenColumns() && startres < av.getAlignment()
1215 .getHiddenColumns().getHiddenBoundaryRight(startres)))
1221 int alWidth = av.getAlignment().getWidth();
1222 if (av.hasHiddenRows())
1224 int hwidth = av.getAlignment().getHiddenSequences()
1226 if (hwidth > alWidth)
1231 // We can still insert gaps if the selectionGroup
1232 // contains all the sequences
1233 sg.setEndRes(sg.getEndRes() + startres - lastres);
1234 fixedRight = alWidth + startres - lastres;
1245 else if (!insertGap)
1247 // / Are we able to delete?
1248 // ie are all columns blank?
1250 for (SequenceI gs : groupSeqs)
1252 for (int j = startres; j < lastres; j++)
1254 if (gs.getLength() <= j)
1259 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1261 // Not a gap, block edit not valid
1271 // dragging to the right
1272 if (fixedColumns && fixedRight != -1)
1274 for (int j = lastres; j < startres; j++)
1276 insertChar(j, groupSeqs, fixedRight);
1281 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1282 startres - lastres, av.getAlignment(), true);
1287 // dragging to the left
1288 if (fixedColumns && fixedRight != -1)
1290 for (int j = lastres; j > startres; j--)
1292 deleteChar(startres, groupSeqs, fixedRight);
1297 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1298 lastres - startres, av.getAlignment(), true);
1304 // ///Editing a single sequence///////////
1308 // dragging to the right
1309 if (fixedColumns && fixedRight != -1)
1311 for (int j = lastres; j < startres; j++)
1313 insertChar(j, new SequenceI[] { seq }, fixedRight);
1318 editCommand.appendEdit(Action.INSERT_GAP,
1319 new SequenceI[] { seq }, lastres, startres - lastres,
1320 av.getAlignment(), true);
1325 // dragging to the left
1326 if (fixedColumns && fixedRight != -1)
1328 for (int j = lastres; j > startres; j--)
1330 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1335 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1340 // could be a keyboard edit trying to delete none gaps
1342 for (int m = startres; m < lastres; m++)
1344 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1353 editCommand.appendEdit(Action.DELETE_GAP,
1354 new SequenceI[] { seq }, startres, max,
1355 av.getAlignment(), true);
1362 seqCanvas.repaint();
1365 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1367 int blankColumn = fixedColumn;
1368 for (int s = 0; s < seq.length; s++)
1370 // Find the next gap before the end of the visible region boundary
1371 // If lastCol > j, theres a boundary after the gap insertion
1373 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1375 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1377 // Theres a space, so break and insert the gap
1382 if (blankColumn <= j)
1384 blankColumn = fixedColumn;
1390 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1391 av.getAlignment(), true);
1393 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1398 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1401 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1404 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1405 av.getAlignment(), true);
1408 // ////////////////////////////////////////
1409 // ///Everything below this is for defining the boundary of the rubberband
1410 // ////////////////////////////////////////
1411 public void doMousePressedDefineMode(MouseEvent evt)
1413 if (scrollThread != null)
1415 scrollThread.running = false;
1416 scrollThread = null;
1419 int res = findRes(evt);
1420 int seq = findSeq(evt);
1422 startWrapBlock = wrappedBlock;
1429 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1431 if (sequence == null || res > sequence.getLength())
1436 stretchGroup = av.getSelectionGroup();
1438 if (stretchGroup == null || !stretchGroup.contains(sequence, res))
1440 stretchGroup = av.getAlignment().findGroup(sequence, res);
1441 if (stretchGroup != null)
1443 // only update the current selection if the popup menu has a group to
1445 av.setSelectionGroup(stretchGroup);
1449 // DETECT RIGHT MOUSE BUTTON IN AWT
1450 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1452 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1453 sequence.findPosition(res));
1455 Vector<String> links = null;
1456 if (allFeatures != null)
1458 for (int i = 0; i < allFeatures.length; i++)
1460 if (allFeatures[i].links != null)
1464 links = new Vector<String>();
1466 for (int j = 0; j < allFeatures[i].links.size(); j++)
1468 links.addElement(allFeatures[i].links.elementAt(j));
1473 APopupMenu popup = new APopupMenu(ap, null, links);
1475 popup.show(this, evt.getX(), evt.getY());
1481 seqCanvas.cursorX = findRes(evt);
1482 seqCanvas.cursorY = findSeq(evt);
1483 seqCanvas.repaint();
1487 // Only if left mouse button do we want to change group sizes
1489 if (stretchGroup == null)
1491 // define a new group here
1492 SequenceGroup sg = new SequenceGroup();
1493 sg.setStartRes(res);
1495 sg.addSequence(sequence, false);
1496 av.setSelectionGroup(sg);
1499 if (av.getConservationSelected())
1501 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1504 if (av.getAbovePIDThreshold())
1506 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1513 public void doMouseReleasedDefineMode(MouseEvent evt)
1515 if (stretchGroup == null)
1519 // always do this - annotation has own state
1520 // but defer colourscheme update until hidden sequences are passed in
1521 boolean vischange = stretchGroup.recalcConservation(true);
1522 // here we rely on stretchGroup == av.getSelection()
1523 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup();
1524 if (stretchGroup.cs != null)
1526 stretchGroup.cs.alignmentChanged(stretchGroup,
1527 av.getHiddenRepSequences());
1529 if (stretchGroup.cs.conservationApplied())
1531 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1532 stretchGroup.getName());
1534 if (stretchGroup.cs.getThreshold() > 0)
1536 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1537 stretchGroup.getName());
1540 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1541 ap.paintAlignment(needOverviewUpdate);
1542 needOverviewUpdate = false;
1543 changeEndRes = false;
1544 changeStartRes = false;
1545 stretchGroup = null;
1549 public void doMouseDraggedDefineMode(MouseEvent evt)
1551 int res = findRes(evt);
1552 int y = findSeq(evt);
1554 if (wrappedBlock != startWrapBlock)
1559 if (stretchGroup == null)
1564 mouseDragging = true;
1566 if (y > av.getAlignment().getHeight())
1568 y = av.getAlignment().getHeight() - 1;
1571 if (res >= av.getAlignment().getWidth())
1573 res = av.getAlignment().getWidth() - 1;
1576 if (stretchGroup.getEndRes() == res)
1578 // Edit end res position of selected group
1579 changeEndRes = true;
1581 else if (stretchGroup.getStartRes() == res)
1583 // Edit start res position of selected group
1584 changeStartRes = true;
1594 if (res > (stretchGroup.getStartRes() - 1))
1596 stretchGroup.setEndRes(res);
1597 needOverviewUpdate |= av.isSelectionDefinedGroup();
1600 else if (changeStartRes)
1602 if (res < (stretchGroup.getEndRes() + 1))
1604 stretchGroup.setStartRes(res);
1605 needOverviewUpdate |= av.isSelectionDefinedGroup();
1609 int dragDirection = 0;
1615 else if (y < oldSeq)
1620 while ((y != oldSeq) && (oldSeq > -1)
1621 && (y < av.getAlignment().getHeight()))
1623 // This routine ensures we don't skip any sequences, as the
1624 // selection is quite slow.
1625 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1627 oldSeq += dragDirection;
1634 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1636 if (stretchGroup.getSequences(null).contains(nextSeq))
1638 stretchGroup.deleteSequence(seq, false);
1639 needOverviewUpdate |= av.isSelectionDefinedGroup();
1645 stretchGroup.addSequence(seq, false);
1648 stretchGroup.addSequence(nextSeq, false);
1649 needOverviewUpdate |= av.isSelectionDefinedGroup();
1658 if (res > av.getRanges().getEndRes()
1659 || res < av.getRanges().getStartRes()
1660 || y < av.getRanges().getStartSeq()
1661 || y > av.getRanges().getEndSeq())
1666 if (scrollThread != null)
1668 scrollThread.setEvent(evt);
1671 seqCanvas.repaint();
1675 public void mouseEntered(MouseEvent e)
1682 if (scrollThread != null)
1684 scrollThread.running = false;
1685 scrollThread = null;
1690 public void mouseExited(MouseEvent e)
1692 if (av.getWrapAlignment())
1697 if (mouseDragging && scrollThread == null)
1699 scrollThread = new ScrollThread();
1703 void scrollCanvas(MouseEvent evt)
1707 if (scrollThread != null)
1709 scrollThread.running = false;
1710 scrollThread = null;
1712 mouseDragging = false;
1716 if (scrollThread == null)
1718 scrollThread = new ScrollThread();
1721 mouseDragging = true;
1722 scrollThread.setEvent(evt);
1727 // this class allows scrolling off the bottom of the visible alignment
1728 class ScrollThread extends Thread
1732 boolean running = false;
1734 public ScrollThread()
1739 public void setEvent(MouseEvent e)
1744 public void stopScrolling()
1759 if (mouseDragging && evt.getY() < 0
1760 && av.getRanges().getStartSeq() > 0)
1762 running = ap.scrollUp(true);
1765 if (mouseDragging && evt.getY() >= getSize().height
1766 && av.getAlignment().getHeight() > av.getRanges()
1769 running = ap.scrollUp(false);
1772 if (mouseDragging && evt.getX() < 0)
1774 running = ap.scrollRight(false);
1777 else if (mouseDragging && evt.getX() >= getSize().width)
1779 running = ap.scrollRight(true);
1786 } catch (Exception ex)
1794 * modify current selection according to a received message.
1797 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1798 HiddenColumns hidden, SelectionSource source)
1800 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1801 // handles selection messages...
1802 // TODO: extend config options to allow user to control if selections may be
1803 // shared between viewports.
1805 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignmentViewport) source)
1806 .getSequenceSetId().equals(av.getSequenceSetId()))))
1812 * Check for selection in a view of which this one is a dna/protein
1815 if (selectionFromTranslation(seqsel, colsel, hidden, source))
1820 // do we want to thread this ? (contention with seqsel and colsel locks, I
1823 * only copy colsel if there is a real intersection between
1824 * sequence selection and this panel's alignment
1826 boolean repaint = false;
1827 boolean copycolsel = false;
1828 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1830 SequenceGroup sgroup = null;
1831 if (seqsel != null && seqsel.getSize() > 0)
1833 if (av.getAlignment() == null)
1836 .println("Selection message: alignviewport av SeqSetId="
1837 + av.getSequenceSetId() + " ViewId="
1839 + " 's alignment is NULL! returning immediatly.");
1842 sgroup = seqsel.intersect(av.getAlignment(),
1843 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1844 if ((sgroup != null && sgroup.getSize() > 0))
1849 if (sgroup != null && sgroup.getSize() > 0)
1851 av.setSelectionGroup(sgroup);
1855 av.setSelectionGroup(null);
1857 repaint = av.isSelectionGroupChanged(true);
1860 && (av.getColumnSelection() == null || !av
1861 .isColSelChanged(true)))
1863 // the current selection is unset or from a previous message
1864 // so import the new colsel.
1865 if (colsel == null || colsel.isEmpty())
1867 if (av.getColumnSelection() != null)
1869 av.getColumnSelection().clear();
1874 // TODO: shift colSel according to the intersecting sequences
1875 if (av.getColumnSelection() == null)
1877 av.setColumnSelection(new ColumnSelection(colsel));
1881 av.getColumnSelection().setElementsFrom(colsel,
1882 av.getAlignment().getHiddenColumns());
1885 repaint |= av.isColSelChanged(true);
1888 && av.hasHiddenColumns()
1889 && (av.getColumnSelection() == null || av.getAlignment()
1890 .getHiddenColumns().getListOfCols() == null))
1892 System.err.println("Bad things");
1896 ap.scalePanelHolder.repaint();
1902 * scroll to the given row/column - or nearest visible location
1907 public void scrollTo(int row, int column)
1910 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1911 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1912 ap.scrollTo(column, column, row, true, true);
1916 * scroll to the given row - or nearest visible location
1920 public void scrollToRow(int row)
1923 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1924 ap.scrollTo(ap.av.getRanges().getStartRes(), ap.av.getRanges()
1925 .getStartRes(), row, true, true);
1929 * scroll to the given column - or nearest visible location
1933 public void scrollToColumn(int column)
1936 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1937 ap.scrollTo(column, column, ap.av.getRanges().getStartSeq(), true, true);
1941 * If this panel is a cdna/protein translation view of the selection source,
1942 * tries to map the source selection to a local one, and returns true. Else
1949 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1950 ColumnSelection colsel, HiddenColumns hidden,
1951 SelectionSource source)
1953 if (!(source instanceof AlignViewportI))
1957 final AlignViewportI sourceAv = (AlignViewportI) source;
1958 if (sourceAv.getCodingComplement() != av
1959 && av.getCodingComplement() != sourceAv)
1965 * Map sequence selection
1967 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1968 av.setSelectionGroup(sg);
1969 av.isSelectionGroupChanged(true);
1972 * Map column selection
1974 // ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1976 ColumnSelection cs = new ColumnSelection();
1977 HiddenColumns hs = new HiddenColumns();
1978 MappingUtils.mapColumnSelection(colsel, hidden, sourceAv, av, cs, hs);
1979 av.setColumnSelection(cs);
1980 av.getAlignment().setHiddenColumns(hs);
1982 ap.scalePanelHolder.repaint();