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;
45 import java.awt.BorderLayout;
47 import java.awt.FontMetrics;
48 import java.awt.Panel;
49 import java.awt.Point;
50 import java.awt.event.InputEvent;
51 import java.awt.event.MouseEvent;
52 import java.awt.event.MouseListener;
53 import java.awt.event.MouseMotionListener;
54 import java.util.Vector;
56 public class SeqPanel extends Panel implements MouseMotionListener,
57 MouseListener, SequenceListener, SelectionListener
60 public SeqCanvas seqCanvas;
62 public AlignmentPanel ap;
64 protected int lastres;
66 protected int startseq;
68 protected AlignViewport av;
70 // if character is inserted or deleted, we will need to recalculate the
72 boolean seqEditOccurred = false;
74 ScrollThread scrollThread = null;
76 boolean mouseDragging = false;
78 boolean editingSeqs = false;
80 boolean groupEditing = false;
84 boolean changeEndSeq = false;
86 boolean changeStartSeq = false;
88 boolean changeEndRes = false;
90 boolean changeStartRes = false;
92 SequenceGroup stretchGroup = null;
94 StringBuffer keyboardNo1;
96 StringBuffer keyboardNo2;
98 boolean mouseWheelPressed = false;
100 Point lastMousePress;
102 EditCommand editCommand;
104 StructureSelectionManager ssm;
106 public SeqPanel(AlignViewport avp, AlignmentPanel p)
110 seqCanvas = new SeqCanvas(avp);
111 setLayout(new BorderLayout());
116 seqCanvas.addMouseMotionListener(this);
117 seqCanvas.addMouseListener(this);
118 ssm = StructureSelectionManager.getStructureSelectionManager(av.applet);
119 ssm.addStructureViewerListener(this);
120 ssm.addSelectionListener(this);
127 if (editCommand != null && editCommand.getSize() > 0)
129 ap.alignFrame.addHistoryItem(editCommand);
130 av.firePropertyChange("alignment", null, av.getAlignment()
137 groupEditing = false;
145 seqCanvas.cursorY = getKeyboardNo1() - 1;
149 void setCursorColumn()
151 seqCanvas.cursorX = getKeyboardNo1() - 1;
155 void setCursorRowAndColumn()
157 if (keyboardNo2 == null)
159 keyboardNo2 = new StringBuffer();
163 seqCanvas.cursorX = getKeyboardNo1() - 1;
164 seqCanvas.cursorY = getKeyboardNo2() - 1;
169 void setCursorPosition()
171 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
173 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
177 void moveCursor(int dx, int dy)
179 seqCanvas.cursorX += dx;
180 seqCanvas.cursorY += dy;
181 if (av.hasHiddenColumns()
182 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
184 int original = seqCanvas.cursorX - dx;
185 int maxWidth = av.getAlignment().getWidth();
187 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
188 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
190 seqCanvas.cursorX += dx;
193 if (seqCanvas.cursorX >= maxWidth
194 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
196 seqCanvas.cursorX = original;
202 void scrollToVisible()
204 if (seqCanvas.cursorX < 0)
206 seqCanvas.cursorX = 0;
208 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
210 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
213 if (seqCanvas.cursorY < 0)
215 seqCanvas.cursorY = 0;
217 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
219 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
223 if (av.getWrapAlignment())
225 ap.scrollToWrappedVisible(seqCanvas.cursorX);
229 while (seqCanvas.cursorY < av.startSeq)
233 while (seqCanvas.cursorY + 1 > av.endSeq)
237 while (seqCanvas.cursorX < av.getColumnSelection()
238 .adjustForHiddenColumns(av.startRes))
241 if (!ap.scrollRight(false))
246 while (seqCanvas.cursorX > av.getColumnSelection()
247 .adjustForHiddenColumns(av.endRes))
249 if (!ap.scrollRight(true))
255 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
256 seqCanvas.cursorX, seqCanvas.cursorY);
261 void setSelectionAreaAtCursor(boolean topLeft)
263 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
265 if (av.getSelectionGroup() != null)
267 SequenceGroup sg = av.getSelectionGroup();
268 // Find the top and bottom of this group
269 int min = av.getAlignment().getHeight(), max = 0;
270 for (int i = 0; i < sg.getSize(); i++)
272 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
287 sg.setStartRes(seqCanvas.cursorX);
288 if (sg.getEndRes() < seqCanvas.cursorX)
290 sg.setEndRes(seqCanvas.cursorX);
293 min = seqCanvas.cursorY;
297 sg.setEndRes(seqCanvas.cursorX);
298 if (sg.getStartRes() > seqCanvas.cursorX)
300 sg.setStartRes(seqCanvas.cursorX);
303 max = seqCanvas.cursorY + 1;
308 // Only the user can do this
309 av.setSelectionGroup(null);
313 // Now add any sequences between min and max
315 for (int i = min; i < max; i++)
317 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
322 if (av.getSelectionGroup() == null)
324 SequenceGroup sg = new SequenceGroup();
325 sg.setStartRes(seqCanvas.cursorX);
326 sg.setEndRes(seqCanvas.cursorX);
327 sg.addSequence(sequence, false);
328 av.setSelectionGroup(sg);
330 ap.paintAlignment(false);
334 void insertGapAtCursor(boolean group)
336 groupEditing = group;
337 startseq = seqCanvas.cursorY;
338 lastres = seqCanvas.cursorX;
339 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
343 void deleteGapAtCursor(boolean group)
345 groupEditing = group;
346 startseq = seqCanvas.cursorY;
347 lastres = seqCanvas.cursorX + getKeyboardNo1();
348 editSequence(false, seqCanvas.cursorX);
352 void numberPressed(char value)
354 if (keyboardNo1 == null)
356 keyboardNo1 = new StringBuffer();
359 if (keyboardNo2 != null)
361 keyboardNo2.append(value);
365 keyboardNo1.append(value);
373 if (keyboardNo1 != null)
375 int value = Integer.parseInt(keyboardNo1.toString());
379 } catch (Exception x)
390 if (keyboardNo2 != null)
392 int value = Integer.parseInt(keyboardNo2.toString());
396 } catch (Exception x)
404 * Set status message in alignment panel
407 * aligned sequence object
411 * index of sequence in alignment
412 * @return position of res in sequence
414 void setStatusMessage(SequenceI sequence, int res, int seq)
416 // TODO remove duplication of identical gui method
417 StringBuilder text = new StringBuilder(32);
418 String seqno = seq == -1 ? "" : " " + (seq + 1);
419 text.append("Sequence" + seqno + " ID: " + sequence.getName());
421 String residue = null;
423 * Try to translate the display character to residue name (null for gap).
425 final String displayChar = String.valueOf(sequence.getCharAt(res));
426 if (av.getAlignment().isNucleotide())
428 residue = ResidueProperties.nucleotideName.get(displayChar);
431 text.append(" Nucleotide: ").append(residue);
436 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
437 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
441 text.append(" Residue: ").append(residue);
448 pos = sequence.findPosition(res);
449 text.append(" (").append(Integer.toString(pos)).append(")");
452 ap.alignFrame.statusBar.setText(text.toString());
456 * Set the status bar message to highlight the first matched position in
460 * @return true if results were matched, false if not
462 private boolean setStatusMessage(SearchResultsI results)
464 AlignmentI al = this.av.getAlignment();
465 int sequenceIndex = al.findIndex(results);
466 if (sequenceIndex == -1)
470 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
471 for (SearchResultMatchI m : results.getResults())
473 SequenceI seq = m.getSequence();
474 if (seq.getDatasetSequence() != null)
476 seq = seq.getDatasetSequence();
482 * Convert position in sequence (base 1) to sequence character array
485 int start = m.getStart() - m.getSequence().getStart();
486 setStatusMessage(seq, start, sequenceIndex);
494 public void mousePressed(MouseEvent evt)
496 lastMousePress = evt.getPoint();
498 // For now, ignore the mouseWheel font resizing on Macs
499 // As the Button2_mask always seems to be true
500 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
503 mouseWheelPressed = true;
507 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
509 if (evt.isControlDown() || evt.isAltDown())
517 doMousePressedDefineMode(evt);
521 int seq = findSeq(evt);
522 int res = findRes(evt);
524 if (seq < 0 || res < 0)
529 if ((seq < av.getAlignment().getHeight())
530 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
545 public void mouseClicked(MouseEvent evt)
547 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
548 if (evt.getClickCount() > 1)
550 if (av.getSelectionGroup() != null
551 && av.getSelectionGroup().getSize() == 1
552 && av.getSelectionGroup().getEndRes()
553 - av.getSelectionGroup().getStartRes() < 2)
555 av.setSelectionGroup(null);
558 SequenceFeature[] features = findFeaturesAtRes(sequence,
559 sequence.findPosition(findRes(evt)));
561 if (features != null && features.length > 0)
563 SearchResultsI highlight = new SearchResults();
564 highlight.addResult(sequence, features[0].getBegin(),
565 features[0].getEnd());
566 seqCanvas.highlightSearchResults(highlight);
568 if (features != null && features.length > 0)
570 seqCanvas.getFeatureRenderer().amendFeatures(
571 new SequenceI[] { sequence }, features, false, ap);
573 seqCanvas.highlightSearchResults(null);
579 public void mouseReleased(MouseEvent evt)
581 mouseDragging = false;
582 mouseWheelPressed = false;
583 ap.paintAlignment(true);
587 doMouseReleasedDefineMode(evt);
595 int startWrapBlock = -1;
597 int wrappedBlock = -1;
599 int findRes(MouseEvent evt)
604 if (av.getWrapAlignment())
607 int hgap = av.getCharHeight();
608 if (av.getScaleAboveWrapped())
610 hgap += av.getCharHeight();
613 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
614 + hgap + seqCanvas.getAnnotationHeight();
618 x -= seqCanvas.LABEL_WEST;
620 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
626 wrappedBlock = y / cHeight;
627 wrappedBlock += av.getStartRes() / cwidth;
629 res = wrappedBlock * cwidth + x / av.getCharWidth();
634 res = (x / av.getCharWidth()) + av.getStartRes();
637 if (av.hasHiddenColumns())
639 res = av.getColumnSelection().adjustForHiddenColumns(res);
646 int findSeq(MouseEvent evt)
648 final int sqnum = findAlRow(evt);
649 return (sqnum < 0) ? 0 : sqnum;
655 * @return row in alignment that was selected (or -1 for column selection)
657 private int findAlRow(MouseEvent evt)
662 if (av.getWrapAlignment())
664 int hgap = av.getCharHeight();
665 if (av.getScaleAboveWrapped())
667 hgap += av.getCharHeight();
670 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
671 + hgap + seqCanvas.getAnnotationHeight();
675 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
684 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
685 .getAlignment().getHeight() - 1);
695 public void doMousePressed(MouseEvent evt)
698 int seq = findSeq(evt);
699 int res = findRes(evt);
701 if (seq < av.getAlignment().getHeight()
702 && res < av.getAlignment().getSequenceAt(seq).getLength())
704 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
705 // Find the residue's position in the sequence (res is the position
723 public void mouseOverSequence(SequenceI sequence, int index, int pos)
725 String tmp = sequence.hashCode() + index + "";
726 if (lastMessage == null || !lastMessage.equals(tmp))
728 ssm.mouseOverSequence(sequence, index, pos, av);
735 public void highlightSequence(SearchResultsI results)
737 if (av.isFollowHighlight())
739 if (ap.scrollToPosition(results, true))
741 ap.alignFrame.repaint();
744 setStatusMessage(results);
745 seqCanvas.highlightSearchResults(results);
750 public VamsasSource getVamsasSource()
752 return this.ap == null ? null : this.ap.av;
756 public void updateColours(SequenceI seq, int index)
758 System.out.println("update the seqPanel colours");
763 public void mouseMoved(MouseEvent evt)
765 int res = findRes(evt);
766 int seq = findSeq(evt);
768 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
777 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
778 if (res > sequence.getLength())
787 int respos = sequence.findPosition(res);
790 mouseOverSequence(sequence, res, respos);
793 StringBuilder text = new StringBuilder();
794 text.append("Sequence ").append(Integer.toString(seq + 1))
795 .append(" ID: ").append(sequence.getName());
798 final String ch = String.valueOf(sequence.getCharAt(res));
799 if (av.getAlignment().isNucleotide())
801 obj = ResidueProperties.nucleotideName.get(ch);
804 text.append(" Nucleotide: ").append(obj);
809 obj = "X".equalsIgnoreCase(ch) ? "X" : ResidueProperties.aa2Triplet
813 text.append(" Residue: ").append(obj);
819 text.append(" (").append(Integer.toString(respos)).append(")");
822 ap.alignFrame.statusBar.setText(text.toString());
824 StringBuilder tooltipText = new StringBuilder();
825 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
828 for (int g = 0; g < groups.length; g++)
830 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
832 if (!groups[g].getName().startsWith("JTreeGroup")
833 && !groups[g].getName().startsWith("JGroup"))
835 tooltipText.append(groups[g].getName()).append(" ");
837 if (groups[g].getDescription() != null)
839 tooltipText.append(groups[g].getDescription());
841 tooltipText.append("\n");
846 // use aa to see if the mouse pointer is on a
847 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
848 sequence.findPosition(res));
851 while (index < allFeatures.length)
853 SequenceFeature sf = allFeatures[index];
855 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
857 if (sf.getDescription() != null)
859 tooltipText.append(" " + sf.getDescription());
862 if (sf.getValue("status") != null)
864 String status = sf.getValue("status").toString();
865 if (status.length() > 0)
867 tooltipText.append(" (" + sf.getValue("status") + ")");
870 tooltipText.append("\n");
877 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
881 tooltip.setTip(tooltipText.toString());
885 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
887 Vector tmp = new Vector();
888 SequenceFeature[] features = sequence.getSequenceFeatures();
889 if (features != null)
891 for (int i = 0; i < features.length; i++)
893 if (av.getFeaturesDisplayed() == null
894 || !av.getFeaturesDisplayed().isVisible(
895 features[i].getType()))
900 if (features[i].featureGroup != null
901 && !seqCanvas.fr.checkGroupVisibility(
902 features[i].featureGroup, false))
907 if ((features[i].getBegin() <= res)
908 && (features[i].getEnd() >= res))
910 tmp.addElement(features[i]);
915 features = new SequenceFeature[tmp.size()];
916 tmp.copyInto(features);
924 * set when the current UI interaction has resulted in a change that requires
925 * overview shading to be recalculated. this could be changed to something
926 * more expressive that indicates what actually has changed, so selective
927 * redraws can be applied
929 private boolean needOverviewUpdate; // TODO: refactor to avcontroller
932 public void mouseDragged(MouseEvent evt)
934 if (mouseWheelPressed)
936 int oldWidth = av.getCharWidth();
938 // Which is bigger, left-right or up-down?
939 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
942 int fontSize = av.font.getSize();
944 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
948 else if (evt.getY() > lastMousePress.y)
958 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
959 av.setCharWidth(oldWidth);
963 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
965 av.setCharWidth(av.getCharWidth() - 1);
967 else if (evt.getX() > lastMousePress.x)
969 av.setCharWidth(av.getCharWidth() + 1);
972 if (av.getCharWidth() < 1)
980 FontMetrics fm = getFontMetrics(av.getFont());
981 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
983 lastMousePress = evt.getPoint();
985 ap.paintAlignment(false);
986 ap.annotationPanel.image = null;
992 doMouseDraggedDefineMode(evt);
996 int res = findRes(evt);
1003 if ((lastres == -1) || (lastres == res))
1008 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1010 // dragLeft, delete gap
1011 editSequence(false, res);
1015 editSequence(true, res);
1018 mouseDragging = true;
1019 if (scrollThread != null)
1021 scrollThread.setEvent(evt);
1026 synchronized void editSequence(boolean insertGap, int startres)
1029 int fixedRight = -1;
1030 boolean fixedColumns = false;
1031 SequenceGroup sg = av.getSelectionGroup();
1033 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1035 if (!groupEditing && av.hasHiddenRows())
1037 if (av.isHiddenRepSequence(seq))
1039 sg = av.getRepresentedSequences(seq);
1040 groupEditing = true;
1044 StringBuffer message = new StringBuffer();
1047 message.append(MessageManager.getString("action.edit_group")).append(
1049 if (editCommand == null)
1051 editCommand = new EditCommand(
1052 MessageManager.getString("action.edit_group"));
1057 message.append(MessageManager.getString("label.edit_sequence"))
1058 .append(" " + seq.getName());
1059 String label = seq.getName();
1060 if (label.length() > 10)
1062 label = label.substring(0, 10);
1064 if (editCommand == null)
1066 editCommand = new EditCommand(MessageManager.formatMessage(
1067 "label.edit_params", new String[] { label }));
1073 message.append(" insert ");
1077 message.append(" delete ");
1080 message.append(Math.abs(startres - lastres) + " gaps.");
1081 ap.alignFrame.statusBar.setText(message.toString());
1083 // Are we editing within a selection group?
1085 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1088 fixedColumns = true;
1090 // sg might be null as the user may only see 1 sequence,
1091 // but the sequence represents a group
1094 if (!av.isHiddenRepSequence(seq))
1100 sg = av.getRepresentedSequences(seq);
1103 fixedLeft = sg.getStartRes();
1104 fixedRight = sg.getEndRes();
1106 if ((startres < fixedLeft && lastres >= fixedLeft)
1107 || (startres >= fixedLeft && lastres < fixedLeft)
1108 || (startres > fixedRight && lastres <= fixedRight)
1109 || (startres <= fixedRight && lastres > fixedRight))
1115 if (fixedLeft > startres)
1117 fixedRight = fixedLeft - 1;
1120 else if (fixedRight < startres)
1122 fixedLeft = fixedRight;
1127 if (av.hasHiddenColumns())
1129 fixedColumns = true;
1130 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1131 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1133 if ((insertGap && startres > y1 && lastres < y1)
1134 || (!insertGap && startres < y2 && lastres > y2))
1140 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1141 // Selection spans a hidden region
1142 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1150 fixedRight = y2 - 1;
1157 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1158 .toArray(new SequenceI[0]);
1163 // If the user has selected the whole sequence, and is dragging to
1164 // the right, we can still extend the alignment and selectionGroup
1165 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1166 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1168 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1169 fixedRight = sg.getEndRes();
1172 // Is it valid with fixed columns??
1173 // Find the next gap before the end
1174 // of the visible region boundary
1175 boolean blank = false;
1176 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1180 for (SequenceI gs : groupSeqs)
1182 for (int j = 0; j < startres - lastres; j++)
1184 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1200 if (sg.getSize() == av.getAlignment().getHeight())
1202 if ((av.hasHiddenColumns() && startres < av
1203 .getColumnSelection().getHiddenBoundaryRight(startres)))
1209 int alWidth = av.getAlignment().getWidth();
1210 if (av.hasHiddenRows())
1212 int hwidth = av.getAlignment().getHiddenSequences()
1214 if (hwidth > alWidth)
1219 // We can still insert gaps if the selectionGroup
1220 // contains all the sequences
1221 sg.setEndRes(sg.getEndRes() + startres - lastres);
1222 fixedRight = alWidth + startres - lastres;
1233 else if (!insertGap)
1235 // / Are we able to delete?
1236 // ie are all columns blank?
1238 for (SequenceI gs : groupSeqs)
1240 for (int j = startres; j < lastres; j++)
1242 if (gs.getLength() <= j)
1247 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1249 // Not a gap, block edit not valid
1259 // dragging to the right
1260 if (fixedColumns && fixedRight != -1)
1262 for (int j = lastres; j < startres; j++)
1264 insertChar(j, groupSeqs, fixedRight);
1269 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1270 startres - lastres, av.getAlignment(), true);
1275 // dragging to the left
1276 if (fixedColumns && fixedRight != -1)
1278 for (int j = lastres; j > startres; j--)
1280 deleteChar(startres, groupSeqs, fixedRight);
1285 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1286 lastres - startres, av.getAlignment(), true);
1292 // ///Editing a single sequence///////////
1296 // dragging to the right
1297 if (fixedColumns && fixedRight != -1)
1299 for (int j = lastres; j < startres; j++)
1301 insertChar(j, new SequenceI[] { seq }, fixedRight);
1306 editCommand.appendEdit(Action.INSERT_GAP,
1307 new SequenceI[] { seq }, lastres, startres - lastres,
1308 av.getAlignment(), true);
1313 // dragging to the left
1314 if (fixedColumns && fixedRight != -1)
1316 for (int j = lastres; j > startres; j--)
1318 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1323 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1328 // could be a keyboard edit trying to delete none gaps
1330 for (int m = startres; m < lastres; m++)
1332 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1341 editCommand.appendEdit(Action.DELETE_GAP,
1342 new SequenceI[] { seq }, startres, max,
1343 av.getAlignment(), true);
1350 seqCanvas.repaint();
1353 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1355 int blankColumn = fixedColumn;
1356 for (int s = 0; s < seq.length; s++)
1358 // Find the next gap before the end of the visible region boundary
1359 // If lastCol > j, theres a boundary after the gap insertion
1361 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1363 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1365 // Theres a space, so break and insert the gap
1370 if (blankColumn <= j)
1372 blankColumn = fixedColumn;
1378 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1379 av.getAlignment(), true);
1381 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1386 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1389 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1392 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1393 av.getAlignment(), true);
1396 // ////////////////////////////////////////
1397 // ///Everything below this is for defining the boundary of the rubberband
1398 // ////////////////////////////////////////
1399 public void doMousePressedDefineMode(MouseEvent evt)
1401 if (scrollThread != null)
1403 scrollThread.running = false;
1404 scrollThread = null;
1407 int res = findRes(evt);
1408 int seq = findSeq(evt);
1410 startWrapBlock = wrappedBlock;
1417 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1419 if (sequence == null || res > sequence.getLength())
1424 stretchGroup = av.getSelectionGroup();
1426 if (stretchGroup == null)
1428 stretchGroup = av.getAlignment().findGroup(sequence, res);
1429 av.setSelectionGroup(stretchGroup);
1432 if (stretchGroup == null
1433 || !stretchGroup.getSequences(null).contains(sequence)
1434 || stretchGroup.getStartRes() > res
1435 || stretchGroup.getEndRes() < res)
1437 stretchGroup = null;
1439 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1441 if (allGroups != null)
1443 for (int i = 0; i < allGroups.length; i++)
1445 if (allGroups[i].getStartRes() <= res
1446 && allGroups[i].getEndRes() >= res)
1448 stretchGroup = allGroups[i];
1453 av.setSelectionGroup(stretchGroup);
1456 // DETECT RIGHT MOUSE BUTTON IN AWT
1457 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1459 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1460 sequence.findPosition(res));
1462 Vector<String> links = null;
1463 if (allFeatures != null)
1465 for (int i = 0; i < allFeatures.length; i++)
1467 if (allFeatures[i].links != null)
1471 links = new Vector<String>();
1473 for (int j = 0; j < allFeatures[i].links.size(); j++)
1475 links.addElement(allFeatures[i].links.elementAt(j));
1480 APopupMenu popup = new APopupMenu(ap, null, links);
1482 popup.show(this, evt.getX(), evt.getY());
1488 seqCanvas.cursorX = findRes(evt);
1489 seqCanvas.cursorY = findSeq(evt);
1490 seqCanvas.repaint();
1494 // Only if left mouse button do we want to change group sizes
1496 if (stretchGroup == null)
1498 // define a new group here
1499 SequenceGroup sg = new SequenceGroup();
1500 sg.setStartRes(res);
1502 sg.addSequence(sequence, false);
1503 av.setSelectionGroup(sg);
1506 if (av.getConservationSelected())
1508 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1511 if (av.getAbovePIDThreshold())
1513 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1520 public void doMouseReleasedDefineMode(MouseEvent evt)
1522 if (stretchGroup == null)
1526 // always do this - annotation has own state
1527 // but defer colourscheme update until hidden sequences are passed in
1528 boolean vischange = stretchGroup.recalcConservation(true);
1529 // here we rely on stretchGroup == av.getSelection()
1530 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup();
1531 if (stretchGroup.cs != null)
1533 stretchGroup.cs.alignmentChanged(stretchGroup,
1534 av.getHiddenRepSequences());
1536 if (stretchGroup.cs.conservationApplied())
1538 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1539 stretchGroup.getName());
1541 if (stretchGroup.cs.getThreshold() > 0)
1543 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1544 stretchGroup.getName());
1547 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1548 ap.paintAlignment(needOverviewUpdate);
1549 needOverviewUpdate = false;
1550 changeEndRes = false;
1551 changeStartRes = false;
1552 stretchGroup = null;
1556 public void doMouseDraggedDefineMode(MouseEvent evt)
1558 int res = findRes(evt);
1559 int y = findSeq(evt);
1561 if (wrappedBlock != startWrapBlock)
1566 if (stretchGroup == null)
1571 mouseDragging = true;
1573 if (y > av.getAlignment().getHeight())
1575 y = av.getAlignment().getHeight() - 1;
1578 if (res >= av.getAlignment().getWidth())
1580 res = av.getAlignment().getWidth() - 1;
1583 if (stretchGroup.getEndRes() == res)
1585 // Edit end res position of selected group
1586 changeEndRes = true;
1588 else if (stretchGroup.getStartRes() == res)
1590 // Edit start res position of selected group
1591 changeStartRes = true;
1601 if (res > (stretchGroup.getStartRes() - 1))
1603 stretchGroup.setEndRes(res);
1604 needOverviewUpdate |= av.isSelectionDefinedGroup();
1607 else if (changeStartRes)
1609 if (res < (stretchGroup.getEndRes() + 1))
1611 stretchGroup.setStartRes(res);
1612 needOverviewUpdate |= av.isSelectionDefinedGroup();
1616 int dragDirection = 0;
1622 else if (y < oldSeq)
1627 while ((y != oldSeq) && (oldSeq > -1)
1628 && (y < av.getAlignment().getHeight()))
1630 // This routine ensures we don't skip any sequences, as the
1631 // selection is quite slow.
1632 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1634 oldSeq += dragDirection;
1641 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1643 if (stretchGroup.getSequences(null).contains(nextSeq))
1645 stretchGroup.deleteSequence(seq, false);
1646 needOverviewUpdate |= av.isSelectionDefinedGroup();
1652 stretchGroup.addSequence(seq, false);
1655 stretchGroup.addSequence(nextSeq, false);
1656 needOverviewUpdate |= av.isSelectionDefinedGroup();
1665 if (res > av.endRes || res < av.startRes || y < av.startSeq
1671 if (scrollThread != null)
1673 scrollThread.setEvent(evt);
1676 seqCanvas.repaint();
1680 public void mouseEntered(MouseEvent e)
1687 if (scrollThread != null)
1689 scrollThread.running = false;
1690 scrollThread = null;
1695 public void mouseExited(MouseEvent e)
1697 if (av.getWrapAlignment())
1702 if (mouseDragging && scrollThread == null)
1704 scrollThread = new ScrollThread();
1708 void scrollCanvas(MouseEvent evt)
1712 if (scrollThread != null)
1714 scrollThread.running = false;
1715 scrollThread = null;
1717 mouseDragging = false;
1721 if (scrollThread == null)
1723 scrollThread = new ScrollThread();
1726 mouseDragging = true;
1727 scrollThread.setEvent(evt);
1732 // this class allows scrolling off the bottom of the visible alignment
1733 class ScrollThread extends Thread
1737 boolean running = false;
1739 public ScrollThread()
1744 public void setEvent(MouseEvent e)
1749 public void stopScrolling()
1764 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1766 running = ap.scrollUp(true);
1769 if (mouseDragging && evt.getY() >= getSize().height
1770 && av.getAlignment().getHeight() > av.getEndSeq())
1772 running = ap.scrollUp(false);
1775 if (mouseDragging && evt.getX() < 0)
1777 running = ap.scrollRight(false);
1780 else if (mouseDragging && evt.getX() >= getSize().width)
1782 running = ap.scrollRight(true);
1789 } catch (Exception ex)
1797 * modify current selection according to a received message.
1800 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1801 SelectionSource source)
1803 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1804 // handles selection messages...
1805 // TODO: extend config options to allow user to control if selections may be
1806 // shared between viewports.
1808 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignmentViewport) source)
1809 .getSequenceSetId().equals(av.getSequenceSetId()))))
1815 * Check for selection in a view of which this one is a dna/protein
1818 if (selectionFromTranslation(seqsel, colsel, source))
1823 // do we want to thread this ? (contention with seqsel and colsel locks, I
1826 * only copy colsel if there is a real intersection between
1827 * sequence selection and this panel's alignment
1829 boolean repaint = false;
1830 boolean copycolsel = false;
1831 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1833 SequenceGroup sgroup = null;
1834 if (seqsel != null && seqsel.getSize() > 0)
1836 if (av.getAlignment() == null)
1839 .println("Selection message: alignviewport av SeqSetId="
1840 + av.getSequenceSetId() + " ViewId="
1842 + " 's alignment is NULL! returning immediatly.");
1845 sgroup = seqsel.intersect(av.getAlignment(),
1846 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1847 if ((sgroup != null && sgroup.getSize() > 0))
1852 if (sgroup != null && sgroup.getSize() > 0)
1854 av.setSelectionGroup(sgroup);
1858 av.setSelectionGroup(null);
1860 repaint = av.isSelectionGroupChanged(true);
1863 && (av.getColumnSelection() == null || !av
1864 .isColSelChanged(true)))
1866 // the current selection is unset or from a previous message
1867 // so import the new colsel.
1868 if (colsel == null || colsel.isEmpty())
1870 if (av.getColumnSelection() != null)
1872 av.getColumnSelection().clear();
1877 // TODO: shift colSel according to the intersecting sequences
1878 if (av.getColumnSelection() == null)
1880 av.setColumnSelection(new ColumnSelection(colsel));
1884 av.getColumnSelection().setElementsFrom(colsel);
1887 repaint |= av.isColSelChanged(true);
1890 && av.hasHiddenColumns()
1891 && (av.getColumnSelection() == null || av.getColumnSelection()
1892 .getHiddenColumns() == 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.startSeq : row;
1913 column = column < 0 ? ap.av.startRes : 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.startSeq : row;
1926 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1930 * scroll to the given column - or nearest visible location
1934 public void scrollToColumn(int column)
1937 column = column < 0 ? ap.av.startRes : column;
1938 ap.scrollTo(column, column, ap.av.startSeq, true, true);
1942 * If this panel is a cdna/protein translation view of the selection source,
1943 * tries to map the source selection to a local one, and returns true. Else
1950 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1951 ColumnSelection colsel, 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 av.setColumnSelection(cs);
1978 ap.scalePanelHolder.repaint();