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.SearchResultMatchI;
29 import jalview.datamodel.SearchResults;
30 import jalview.datamodel.SearchResultsI;
31 import jalview.datamodel.Sequence;
32 import jalview.datamodel.SequenceFeature;
33 import jalview.datamodel.SequenceGroup;
34 import jalview.datamodel.SequenceI;
35 import jalview.schemes.ResidueProperties;
36 import jalview.structure.SelectionListener;
37 import jalview.structure.SelectionSource;
38 import jalview.structure.SequenceListener;
39 import jalview.structure.StructureSelectionManager;
40 import jalview.structure.VamsasSource;
41 import jalview.util.MappingUtils;
42 import jalview.util.MessageManager;
43 import jalview.viewmodel.AlignmentViewport;
44 import jalview.viewmodel.ViewportRanges;
46 import java.awt.BorderLayout;
48 import java.awt.FontMetrics;
49 import java.awt.Panel;
50 import java.awt.Point;
51 import java.awt.event.InputEvent;
52 import java.awt.event.MouseEvent;
53 import java.awt.event.MouseListener;
54 import java.awt.event.MouseMotionListener;
55 import java.util.List;
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.getColumnSelection().isVisible(seqCanvas.cursorX))
186 int original = seqCanvas.cursorX - dx;
187 int maxWidth = av.getAlignment().getWidth();
189 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
190 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
192 seqCanvas.cursorX += dx;
195 if (seqCanvas.cursorX >= maxWidth
196 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
198 seqCanvas.cursorX = original;
204 void scrollToVisible()
206 if (seqCanvas.cursorX < 0)
208 seqCanvas.cursorX = 0;
210 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
212 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
215 if (seqCanvas.cursorY < 0)
217 seqCanvas.cursorY = 0;
219 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
221 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
225 if (av.getWrapAlignment())
227 ap.scrollToWrappedVisible(seqCanvas.cursorX);
231 ViewportRanges ranges = av.getRanges();
232 while (seqCanvas.cursorY < ranges.getStartSeq())
236 while (seqCanvas.cursorY + 1 > ranges.getEndSeq())
240 while (seqCanvas.cursorX < av.getColumnSelection()
241 .adjustForHiddenColumns(ranges.getStartRes()))
244 if (!ap.scrollRight(false))
249 while (seqCanvas.cursorX > av.getColumnSelection()
250 .adjustForHiddenColumns(ranges.getEndRes()))
252 if (!ap.scrollRight(true))
258 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
259 seqCanvas.cursorX, seqCanvas.cursorY);
264 void setSelectionAreaAtCursor(boolean topLeft)
266 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
268 if (av.getSelectionGroup() != null)
270 SequenceGroup sg = av.getSelectionGroup();
271 // Find the top and bottom of this group
272 int min = av.getAlignment().getHeight(), max = 0;
273 for (int i = 0; i < sg.getSize(); i++)
275 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
290 sg.setStartRes(seqCanvas.cursorX);
291 if (sg.getEndRes() < seqCanvas.cursorX)
293 sg.setEndRes(seqCanvas.cursorX);
296 min = seqCanvas.cursorY;
300 sg.setEndRes(seqCanvas.cursorX);
301 if (sg.getStartRes() > seqCanvas.cursorX)
303 sg.setStartRes(seqCanvas.cursorX);
306 max = seqCanvas.cursorY + 1;
311 // Only the user can do this
312 av.setSelectionGroup(null);
316 // Now add any sequences between min and max
318 for (int i = min; i < max; i++)
320 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
325 if (av.getSelectionGroup() == null)
327 SequenceGroup sg = new SequenceGroup();
328 sg.setStartRes(seqCanvas.cursorX);
329 sg.setEndRes(seqCanvas.cursorX);
330 sg.addSequence(sequence, false);
331 av.setSelectionGroup(sg);
333 ap.paintAlignment(false);
337 void insertGapAtCursor(boolean group)
339 groupEditing = group;
340 startseq = seqCanvas.cursorY;
341 lastres = seqCanvas.cursorX;
342 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
346 void deleteGapAtCursor(boolean group)
348 groupEditing = group;
349 startseq = seqCanvas.cursorY;
350 lastres = seqCanvas.cursorX + getKeyboardNo1();
351 editSequence(false, seqCanvas.cursorX);
355 void numberPressed(char value)
357 if (keyboardNo1 == null)
359 keyboardNo1 = new StringBuffer();
362 if (keyboardNo2 != null)
364 keyboardNo2.append(value);
368 keyboardNo1.append(value);
376 if (keyboardNo1 != null)
378 int value = Integer.parseInt(keyboardNo1.toString());
382 } catch (Exception x)
393 if (keyboardNo2 != null)
395 int value = Integer.parseInt(keyboardNo2.toString());
399 } catch (Exception x)
407 * Set status message in alignment panel
410 * aligned sequence object
414 * index of sequence in alignment
415 * @return position of res in sequence
417 void setStatusMessage(SequenceI sequence, int res, int seq)
419 // TODO remove duplication of identical gui method
420 StringBuilder text = new StringBuilder(32);
421 String seqno = seq == -1 ? "" : " " + (seq + 1);
422 text.append("Sequence" + seqno + " ID: " + sequence.getName());
424 String residue = null;
426 * Try to translate the display character to residue name (null for gap).
428 final String displayChar = String.valueOf(sequence.getCharAt(res));
429 if (av.getAlignment().isNucleotide())
431 residue = ResidueProperties.nucleotideName.get(displayChar);
434 text.append(" Nucleotide: ").append(residue);
439 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
440 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
444 text.append(" Residue: ").append(residue);
451 pos = sequence.findPosition(res);
452 text.append(" (").append(Integer.toString(pos)).append(")");
455 ap.alignFrame.statusBar.setText(text.toString());
459 * Set the status bar message to highlight the first matched position in
463 * @return true if results were matched, false if not
465 private boolean setStatusMessage(SearchResultsI results)
467 AlignmentI al = this.av.getAlignment();
468 int sequenceIndex = al.findIndex(results);
469 if (sequenceIndex == -1)
473 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
474 for (SearchResultMatchI m : results.getResults())
476 SequenceI seq = m.getSequence();
477 if (seq.getDatasetSequence() != null)
479 seq = seq.getDatasetSequence();
485 * Convert position in sequence (base 1) to sequence character array
488 int start = m.getStart() - m.getSequence().getStart();
489 setStatusMessage(seq, start, sequenceIndex);
497 public void mousePressed(MouseEvent evt)
499 lastMousePress = evt.getPoint();
501 // For now, ignore the mouseWheel font resizing on Macs
502 // As the Button2_mask always seems to be true
503 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
506 mouseWheelPressed = true;
510 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
512 if (evt.isControlDown() || evt.isAltDown())
520 doMousePressedDefineMode(evt);
524 int seq = findSeq(evt);
525 int res = findRes(evt);
527 if (seq < 0 || res < 0)
532 if ((seq < av.getAlignment().getHeight())
533 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
548 public void mouseClicked(MouseEvent evt)
550 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
551 if (evt.getClickCount() > 1)
553 if (av.getSelectionGroup() != null
554 && av.getSelectionGroup().getSize() == 1
555 && av.getSelectionGroup().getEndRes()
556 - av.getSelectionGroup().getStartRes() < 2)
558 av.setSelectionGroup(null);
561 List<SequenceFeature> features = findFeaturesAtRes(sequence,
562 sequence.findPosition(findRes(evt)));
564 if (!features.isEmpty())
566 SearchResultsI highlight = new SearchResults();
567 highlight.addResult(sequence, features.get(0).getBegin(), features
569 seqCanvas.highlightSearchResults(highlight);
570 SequenceFeature[] featuresArray = features
571 .toArray(new SequenceFeature[features.size()]);
572 seqCanvas.getFeatureRenderer().amendFeatures(
573 new SequenceI[] { sequence }, featuresArray, false, ap,
576 seqCanvas.highlightSearchResults(null);
582 public void mouseReleased(MouseEvent evt)
584 mouseDragging = false;
585 mouseWheelPressed = false;
586 ap.paintAlignment(true);
590 doMouseReleasedDefineMode(evt);
598 int startWrapBlock = -1;
600 int wrappedBlock = -1;
602 int findRes(MouseEvent evt)
607 if (av.getWrapAlignment())
610 int hgap = av.getCharHeight();
611 if (av.getScaleAboveWrapped())
613 hgap += av.getCharHeight();
616 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
617 + hgap + seqCanvas.getAnnotationHeight();
621 x -= seqCanvas.LABEL_WEST;
623 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
629 wrappedBlock = y / cHeight;
630 wrappedBlock += av.getRanges().getStartRes() / cwidth;
632 res = wrappedBlock * cwidth + x / av.getCharWidth();
637 res = (x / av.getCharWidth()) + av.getRanges().getStartRes();
640 if (av.hasHiddenColumns())
642 res = av.getColumnSelection().adjustForHiddenColumns(res);
649 int findSeq(MouseEvent evt)
651 final int sqnum = findAlRow(evt);
652 return (sqnum < 0) ? 0 : sqnum;
658 * @return row in alignment that was selected (or -1 for column selection)
660 private int findAlRow(MouseEvent evt)
665 if (av.getWrapAlignment())
667 int hgap = av.getCharHeight();
668 if (av.getScaleAboveWrapped())
670 hgap += av.getCharHeight();
673 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
674 + hgap + seqCanvas.getAnnotationHeight();
678 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
687 seq = Math.min((y / av.getCharHeight())
688 + av.getRanges().getStartSeq(),
690 .getAlignment().getHeight() - 1);
700 public void doMousePressed(MouseEvent evt)
703 int seq = findSeq(evt);
704 int res = findRes(evt);
706 if (seq < av.getAlignment().getHeight()
707 && res < av.getAlignment().getSequenceAt(seq).getLength())
709 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
710 // Find the residue's position in the sequence (res is the position
728 public void mouseOverSequence(SequenceI sequence, int index, int pos)
730 String tmp = sequence.hashCode() + index + "";
731 if (lastMessage == null || !lastMessage.equals(tmp))
733 ssm.mouseOverSequence(sequence, index, pos, av);
740 public void highlightSequence(SearchResultsI results)
742 if (av.isFollowHighlight())
744 if (ap.scrollToPosition(results, true))
746 ap.alignFrame.repaint();
749 setStatusMessage(results);
750 seqCanvas.highlightSearchResults(results);
755 public VamsasSource getVamsasSource()
757 return this.ap == null ? null : this.ap.av;
761 public void updateColours(SequenceI seq, int index)
763 System.out.println("update the seqPanel colours");
768 public void mouseMoved(MouseEvent evt)
770 int res = findRes(evt);
771 int seq = findSeq(evt);
773 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
782 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
783 if (res > sequence.getLength())
792 int respos = sequence.findPosition(res);
795 mouseOverSequence(sequence, res, respos);
798 StringBuilder text = new StringBuilder();
799 text.append("Sequence ").append(Integer.toString(seq + 1))
800 .append(" ID: ").append(sequence.getName());
803 final String ch = String.valueOf(sequence.getCharAt(res));
804 if (av.getAlignment().isNucleotide())
806 obj = ResidueProperties.nucleotideName.get(ch);
809 text.append(" Nucleotide: ").append(obj);
814 obj = "X".equalsIgnoreCase(ch) ? "X" : ResidueProperties.aa2Triplet
818 text.append(" Residue: ").append(obj);
824 text.append(" (").append(Integer.toString(respos)).append(")");
827 ap.alignFrame.statusBar.setText(text.toString());
829 StringBuilder tooltipText = new StringBuilder();
830 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
833 for (int g = 0; g < groups.length; g++)
835 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
837 if (!groups[g].getName().startsWith("JTreeGroup")
838 && !groups[g].getName().startsWith("JGroup"))
840 tooltipText.append(groups[g].getName()).append(" ");
842 if (groups[g].getDescription() != null)
844 tooltipText.append(groups[g].getDescription());
846 tooltipText.append("\n");
851 // use aa to see if the mouse pointer is on a
852 List<SequenceFeature> allFeatures = findFeaturesAtRes(sequence,
853 sequence.findPosition(res));
856 while (index < allFeatures.size())
858 SequenceFeature sf = allFeatures.get(index);
860 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
862 if (sf.getDescription() != null)
864 tooltipText.append(" " + sf.getDescription());
867 if (sf.getValue("status") != null)
869 String status = sf.getValue("status").toString();
870 if (status.length() > 0)
872 tooltipText.append(" (" + sf.getValue("status") + ")");
875 tooltipText.append("\n");
882 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
886 tooltip.setTip(tooltipText.toString());
890 List<SequenceFeature> findFeaturesAtRes(SequenceI sequence, int res)
892 return seqCanvas.getFeatureRenderer().findFeaturesAtRes(sequence, res);
898 * set when the current UI interaction has resulted in a change that requires
899 * overview shading to be recalculated. this could be changed to something
900 * more expressive that indicates what actually has changed, so selective
901 * redraws can be applied
903 private boolean needOverviewUpdate; // TODO: refactor to avcontroller
906 public void mouseDragged(MouseEvent evt)
908 if (mouseWheelPressed)
910 int oldWidth = av.getCharWidth();
912 // Which is bigger, left-right or up-down?
913 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
916 int fontSize = av.font.getSize();
918 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
922 else if (evt.getY() > lastMousePress.y)
932 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
933 av.setCharWidth(oldWidth);
937 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
939 av.setCharWidth(av.getCharWidth() - 1);
941 else if (evt.getX() > lastMousePress.x)
943 av.setCharWidth(av.getCharWidth() + 1);
946 if (av.getCharWidth() < 1)
954 FontMetrics fm = getFontMetrics(av.getFont());
955 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
957 lastMousePress = evt.getPoint();
959 ap.paintAlignment(false);
960 ap.annotationPanel.image = null;
966 doMouseDraggedDefineMode(evt);
970 int res = findRes(evt);
977 if ((lastres == -1) || (lastres == res))
982 if ((res < av.getAlignment().getWidth()) && (res < lastres))
984 // dragLeft, delete gap
985 editSequence(false, res);
989 editSequence(true, res);
992 mouseDragging = true;
993 if (scrollThread != null)
995 scrollThread.setEvent(evt);
1000 synchronized void editSequence(boolean insertGap, int startres)
1003 int fixedRight = -1;
1004 boolean fixedColumns = false;
1005 SequenceGroup sg = av.getSelectionGroup();
1007 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1009 if (!groupEditing && av.hasHiddenRows())
1011 if (av.isHiddenRepSequence(seq))
1013 sg = av.getRepresentedSequences(seq);
1014 groupEditing = true;
1018 StringBuffer message = new StringBuffer();
1021 message.append(MessageManager.getString("action.edit_group")).append(
1023 if (editCommand == null)
1025 editCommand = new EditCommand(
1026 MessageManager.getString("action.edit_group"));
1031 message.append(MessageManager.getString("label.edit_sequence"))
1032 .append(" " + seq.getName());
1033 String label = seq.getName();
1034 if (label.length() > 10)
1036 label = label.substring(0, 10);
1038 if (editCommand == null)
1040 editCommand = new EditCommand(MessageManager.formatMessage(
1041 "label.edit_params", new String[] { label }));
1047 message.append(" insert ");
1051 message.append(" delete ");
1054 message.append(Math.abs(startres - lastres) + " gaps.");
1055 ap.alignFrame.statusBar.setText(message.toString());
1057 // Are we editing within a selection group?
1059 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1062 fixedColumns = true;
1064 // sg might be null as the user may only see 1 sequence,
1065 // but the sequence represents a group
1068 if (!av.isHiddenRepSequence(seq))
1074 sg = av.getRepresentedSequences(seq);
1077 fixedLeft = sg.getStartRes();
1078 fixedRight = sg.getEndRes();
1080 if ((startres < fixedLeft && lastres >= fixedLeft)
1081 || (startres >= fixedLeft && lastres < fixedLeft)
1082 || (startres > fixedRight && lastres <= fixedRight)
1083 || (startres <= fixedRight && lastres > fixedRight))
1089 if (fixedLeft > startres)
1091 fixedRight = fixedLeft - 1;
1094 else if (fixedRight < startres)
1096 fixedLeft = fixedRight;
1101 if (av.hasHiddenColumns())
1103 fixedColumns = true;
1104 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1105 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1107 if ((insertGap && startres > y1 && lastres < y1)
1108 || (!insertGap && startres < y2 && lastres > y2))
1114 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1115 // Selection spans a hidden region
1116 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1124 fixedRight = y2 - 1;
1131 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1132 .toArray(new SequenceI[0]);
1137 // If the user has selected the whole sequence, and is dragging to
1138 // the right, we can still extend the alignment and selectionGroup
1139 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1140 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1142 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1143 fixedRight = sg.getEndRes();
1146 // Is it valid with fixed columns??
1147 // Find the next gap before the end
1148 // of the visible region boundary
1149 boolean blank = false;
1150 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1154 for (SequenceI gs : groupSeqs)
1156 for (int j = 0; j < startres - lastres; j++)
1158 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1174 if (sg.getSize() == av.getAlignment().getHeight())
1176 if ((av.hasHiddenColumns() && startres < av
1177 .getColumnSelection().getHiddenBoundaryRight(startres)))
1183 int alWidth = av.getAlignment().getWidth();
1184 if (av.hasHiddenRows())
1186 int hwidth = av.getAlignment().getHiddenSequences()
1188 if (hwidth > alWidth)
1193 // We can still insert gaps if the selectionGroup
1194 // contains all the sequences
1195 sg.setEndRes(sg.getEndRes() + startres - lastres);
1196 fixedRight = alWidth + startres - lastres;
1207 else if (!insertGap)
1209 // / Are we able to delete?
1210 // ie are all columns blank?
1212 for (SequenceI gs : groupSeqs)
1214 for (int j = startres; j < lastres; j++)
1216 if (gs.getLength() <= j)
1221 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1223 // Not a gap, block edit not valid
1233 // dragging to the right
1234 if (fixedColumns && fixedRight != -1)
1236 for (int j = lastres; j < startres; j++)
1238 insertChar(j, groupSeqs, fixedRight);
1243 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1244 startres - lastres, av.getAlignment(), true);
1249 // dragging to the left
1250 if (fixedColumns && fixedRight != -1)
1252 for (int j = lastres; j > startres; j--)
1254 deleteChar(startres, groupSeqs, fixedRight);
1259 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1260 lastres - startres, av.getAlignment(), true);
1266 // ///Editing a single sequence///////////
1270 // dragging to the right
1271 if (fixedColumns && fixedRight != -1)
1273 for (int j = lastres; j < startres; j++)
1275 insertChar(j, new SequenceI[] { seq }, fixedRight);
1280 editCommand.appendEdit(Action.INSERT_GAP,
1281 new SequenceI[] { seq }, lastres, startres - lastres,
1282 av.getAlignment(), true);
1287 // dragging to the left
1288 if (fixedColumns && fixedRight != -1)
1290 for (int j = lastres; j > startres; j--)
1292 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1297 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1302 // could be a keyboard edit trying to delete none gaps
1304 for (int m = startres; m < lastres; m++)
1306 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1315 editCommand.appendEdit(Action.DELETE_GAP,
1316 new SequenceI[] { seq }, startres, max,
1317 av.getAlignment(), true);
1324 seqCanvas.repaint();
1327 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1329 int blankColumn = fixedColumn;
1330 for (int s = 0; s < seq.length; s++)
1332 // Find the next gap before the end of the visible region boundary
1333 // If lastCol > j, theres a boundary after the gap insertion
1335 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1337 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1339 // Theres a space, so break and insert the gap
1344 if (blankColumn <= j)
1346 blankColumn = fixedColumn;
1352 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1353 av.getAlignment(), true);
1355 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1360 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1363 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1366 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1367 av.getAlignment(), true);
1370 // ////////////////////////////////////////
1371 // ///Everything below this is for defining the boundary of the rubberband
1372 // ////////////////////////////////////////
1373 public void doMousePressedDefineMode(MouseEvent evt)
1375 if (scrollThread != null)
1377 scrollThread.running = false;
1378 scrollThread = null;
1381 int res = findRes(evt);
1382 int seq = findSeq(evt);
1384 startWrapBlock = wrappedBlock;
1391 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1393 if (sequence == null || res > sequence.getLength())
1398 stretchGroup = av.getSelectionGroup();
1400 if (stretchGroup == null || !stretchGroup.contains(sequence, res))
1402 stretchGroup = av.getAlignment().findGroup(sequence, res);
1403 if (stretchGroup != null)
1405 // only update the current selection if the popup menu has a group to
1407 av.setSelectionGroup(stretchGroup);
1411 // DETECT RIGHT MOUSE BUTTON IN AWT
1412 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1414 List<SequenceFeature> allFeatures = findFeaturesAtRes(sequence,
1415 sequence.findPosition(res));
1417 Vector<String> links = null;
1418 for (int i = 0; i < allFeatures.size(); i++)
1420 SequenceFeature sf = allFeatures.get(i);
1421 if (sf.links != null)
1425 links = new Vector<String>();
1427 links.addAll(sf.links);
1430 APopupMenu popup = new APopupMenu(ap, null, links);
1432 popup.show(this, evt.getX(), evt.getY());
1438 seqCanvas.cursorX = findRes(evt);
1439 seqCanvas.cursorY = findSeq(evt);
1440 seqCanvas.repaint();
1444 // Only if left mouse button do we want to change group sizes
1446 if (stretchGroup == null)
1448 // define a new group here
1449 SequenceGroup sg = new SequenceGroup();
1450 sg.setStartRes(res);
1452 sg.addSequence(sequence, false);
1453 av.setSelectionGroup(sg);
1456 if (av.getConservationSelected())
1458 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1461 if (av.getAbovePIDThreshold())
1463 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1470 public void doMouseReleasedDefineMode(MouseEvent evt)
1472 if (stretchGroup == null)
1476 // always do this - annotation has own state
1477 // but defer colourscheme update until hidden sequences are passed in
1478 boolean vischange = stretchGroup.recalcConservation(true);
1479 // here we rely on stretchGroup == av.getSelection()
1480 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup();
1481 if (stretchGroup.cs != null)
1483 stretchGroup.cs.alignmentChanged(stretchGroup,
1484 av.getHiddenRepSequences());
1486 if (stretchGroup.cs.conservationApplied())
1488 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1489 stretchGroup.getName());
1491 if (stretchGroup.cs.getThreshold() > 0)
1493 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1494 stretchGroup.getName());
1497 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1498 ap.paintAlignment(needOverviewUpdate);
1499 needOverviewUpdate = false;
1500 changeEndRes = false;
1501 changeStartRes = false;
1502 stretchGroup = null;
1506 public void doMouseDraggedDefineMode(MouseEvent evt)
1508 int res = findRes(evt);
1509 int y = findSeq(evt);
1511 if (wrappedBlock != startWrapBlock)
1516 if (stretchGroup == null)
1521 mouseDragging = true;
1523 if (y > av.getAlignment().getHeight())
1525 y = av.getAlignment().getHeight() - 1;
1528 if (res >= av.getAlignment().getWidth())
1530 res = av.getAlignment().getWidth() - 1;
1533 if (stretchGroup.getEndRes() == res)
1535 // Edit end res position of selected group
1536 changeEndRes = true;
1538 else if (stretchGroup.getStartRes() == res)
1540 // Edit start res position of selected group
1541 changeStartRes = true;
1551 if (res > (stretchGroup.getStartRes() - 1))
1553 stretchGroup.setEndRes(res);
1554 needOverviewUpdate |= av.isSelectionDefinedGroup();
1557 else if (changeStartRes)
1559 if (res < (stretchGroup.getEndRes() + 1))
1561 stretchGroup.setStartRes(res);
1562 needOverviewUpdate |= av.isSelectionDefinedGroup();
1566 int dragDirection = 0;
1572 else if (y < oldSeq)
1577 while ((y != oldSeq) && (oldSeq > -1)
1578 && (y < av.getAlignment().getHeight()))
1580 // This routine ensures we don't skip any sequences, as the
1581 // selection is quite slow.
1582 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1584 oldSeq += dragDirection;
1591 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1593 if (stretchGroup.getSequences(null).contains(nextSeq))
1595 stretchGroup.deleteSequence(seq, false);
1596 needOverviewUpdate |= av.isSelectionDefinedGroup();
1602 stretchGroup.addSequence(seq, false);
1605 stretchGroup.addSequence(nextSeq, false);
1606 needOverviewUpdate |= av.isSelectionDefinedGroup();
1615 if (res > av.getRanges().getEndRes()
1616 || res < av.getRanges().getStartRes()
1617 || y < av.getRanges().getStartSeq()
1618 || y > av.getRanges().getEndSeq())
1623 if (scrollThread != null)
1625 scrollThread.setEvent(evt);
1628 seqCanvas.repaint();
1632 public void mouseEntered(MouseEvent e)
1639 if (scrollThread != null)
1641 scrollThread.running = false;
1642 scrollThread = null;
1647 public void mouseExited(MouseEvent e)
1649 if (av.getWrapAlignment())
1654 if (mouseDragging && scrollThread == null)
1656 scrollThread = new ScrollThread();
1660 void scrollCanvas(MouseEvent evt)
1664 if (scrollThread != null)
1666 scrollThread.running = false;
1667 scrollThread = null;
1669 mouseDragging = false;
1673 if (scrollThread == null)
1675 scrollThread = new ScrollThread();
1678 mouseDragging = true;
1679 scrollThread.setEvent(evt);
1684 // this class allows scrolling off the bottom of the visible alignment
1685 class ScrollThread extends Thread
1689 boolean running = false;
1691 public ScrollThread()
1696 public void setEvent(MouseEvent e)
1701 public void stopScrolling()
1716 if (mouseDragging && evt.getY() < 0
1717 && av.getRanges().getStartSeq() > 0)
1719 running = ap.scrollUp(true);
1722 if (mouseDragging && evt.getY() >= getSize().height
1723 && av.getAlignment().getHeight() > av.getRanges()
1726 running = ap.scrollUp(false);
1729 if (mouseDragging && evt.getX() < 0)
1731 running = ap.scrollRight(false);
1734 else if (mouseDragging && evt.getX() >= getSize().width)
1736 running = ap.scrollRight(true);
1743 } catch (Exception ex)
1751 * modify current selection according to a received message.
1754 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1755 SelectionSource source)
1757 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1758 // handles selection messages...
1759 // TODO: extend config options to allow user to control if selections may be
1760 // shared between viewports.
1762 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignmentViewport) source)
1763 .getSequenceSetId().equals(av.getSequenceSetId()))))
1769 * Check for selection in a view of which this one is a dna/protein
1772 if (selectionFromTranslation(seqsel, colsel, source))
1777 // do we want to thread this ? (contention with seqsel and colsel locks, I
1780 * only copy colsel if there is a real intersection between
1781 * sequence selection and this panel's alignment
1783 boolean repaint = false;
1784 boolean copycolsel = false;
1785 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1787 SequenceGroup sgroup = null;
1788 if (seqsel != null && seqsel.getSize() > 0)
1790 if (av.getAlignment() == null)
1793 .println("Selection message: alignviewport av SeqSetId="
1794 + av.getSequenceSetId() + " ViewId="
1796 + " 's alignment is NULL! returning immediatly.");
1799 sgroup = seqsel.intersect(av.getAlignment(),
1800 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1801 if ((sgroup != null && sgroup.getSize() > 0))
1806 if (sgroup != null && sgroup.getSize() > 0)
1808 av.setSelectionGroup(sgroup);
1812 av.setSelectionGroup(null);
1814 repaint = av.isSelectionGroupChanged(true);
1817 && (av.getColumnSelection() == null || !av
1818 .isColSelChanged(true)))
1820 // the current selection is unset or from a previous message
1821 // so import the new colsel.
1822 if (colsel == null || colsel.isEmpty())
1824 if (av.getColumnSelection() != null)
1826 av.getColumnSelection().clear();
1831 // TODO: shift colSel according to the intersecting sequences
1832 if (av.getColumnSelection() == null)
1834 av.setColumnSelection(new ColumnSelection(colsel));
1838 av.getColumnSelection().setElementsFrom(colsel);
1841 repaint |= av.isColSelChanged(true);
1844 && av.hasHiddenColumns()
1845 && (av.getColumnSelection() == null || av.getColumnSelection()
1846 .getHiddenColumns() == null))
1848 System.err.println("Bad things");
1852 ap.scalePanelHolder.repaint();
1858 * scroll to the given row/column - or nearest visible location
1863 public void scrollTo(int row, int column)
1866 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1867 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1868 ap.scrollTo(column, column, row, true, true);
1872 * scroll to the given row - or nearest visible location
1876 public void scrollToRow(int row)
1879 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1880 ap.scrollTo(ap.av.getRanges().getStartRes(), ap.av.getRanges()
1881 .getStartRes(), row, true, true);
1885 * scroll to the given column - or nearest visible location
1889 public void scrollToColumn(int column)
1892 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1893 ap.scrollTo(column, column, ap.av.getRanges().getStartSeq(), true, true);
1897 * If this panel is a cdna/protein translation view of the selection source,
1898 * tries to map the source selection to a local one, and returns true. Else
1905 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1906 ColumnSelection colsel, SelectionSource source)
1908 if (!(source instanceof AlignViewportI))
1912 final AlignViewportI sourceAv = (AlignViewportI) source;
1913 if (sourceAv.getCodingComplement() != av
1914 && av.getCodingComplement() != sourceAv)
1920 * Map sequence selection
1922 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1923 av.setSelectionGroup(sg);
1924 av.isSelectionGroupChanged(true);
1927 * Map column selection
1929 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1931 av.setColumnSelection(cs);
1933 ap.scalePanelHolder.repaint();