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 java.awt.BorderLayout;
25 import java.awt.FontMetrics;
26 import java.awt.Panel;
27 import java.awt.Point;
28 import java.awt.event.InputEvent;
29 import java.awt.event.MouseEvent;
30 import java.awt.event.MouseListener;
31 import java.awt.event.MouseMotionListener;
32 import java.util.List;
33 import java.util.Vector;
35 import jalview.api.AlignViewportI;
36 import jalview.commands.EditCommand;
37 import jalview.commands.EditCommand.Action;
38 import jalview.datamodel.ColumnSelection;
39 import jalview.datamodel.SearchResults;
40 import jalview.datamodel.SearchResults.Match;
41 import jalview.datamodel.Sequence;
42 import jalview.datamodel.SequenceFeature;
43 import jalview.datamodel.SequenceGroup;
44 import jalview.datamodel.SequenceI;
45 import jalview.schemes.ResidueProperties;
46 import jalview.structure.SelectionListener;
47 import jalview.structure.SelectionSource;
48 import jalview.structure.SequenceListener;
49 import jalview.structure.StructureSelectionManager;
50 import jalview.structure.VamsasSource;
51 import jalview.util.MappingUtils;
52 import jalview.util.MessageManager;
53 import jalview.viewmodel.AlignmentViewport;
55 public class SeqPanel extends Panel implements MouseMotionListener,
56 MouseListener, SequenceListener, SelectionListener
59 public SeqCanvas seqCanvas;
61 public AlignmentPanel ap;
63 protected int lastres;
65 protected int startseq;
67 protected AlignViewport av;
69 // if character is inserted or deleted, we will need to recalculate the
71 boolean seqEditOccurred = false;
73 ScrollThread scrollThread = null;
75 boolean mouseDragging = false;
77 boolean editingSeqs = false;
79 boolean groupEditing = false;
83 boolean changeEndSeq = false;
85 boolean changeStartSeq = false;
87 boolean changeEndRes = false;
89 boolean changeStartRes = false;
91 SequenceGroup stretchGroup = null;
93 StringBuffer keyboardNo1;
95 StringBuffer keyboardNo2;
97 boolean mouseWheelPressed = false;
101 EditCommand editCommand;
103 StructureSelectionManager ssm;
105 public SeqPanel(AlignViewport avp, AlignmentPanel p)
109 seqCanvas = new SeqCanvas(avp);
110 setLayout(new BorderLayout());
115 seqCanvas.addMouseMotionListener(this);
116 seqCanvas.addMouseListener(this);
117 ssm = StructureSelectionManager.getStructureSelectionManager(av.applet);
118 ssm.addStructureViewerListener(this);
119 ssm.addSelectionListener(this);
126 if (editCommand != null && editCommand.getSize() > 0)
128 ap.alignFrame.addHistoryItem(editCommand);
129 av.firePropertyChange("alignment", null, av.getAlignment()
136 groupEditing = false;
144 seqCanvas.cursorY = getKeyboardNo1() - 1;
148 void setCursorColumn()
150 seqCanvas.cursorX = getKeyboardNo1() - 1;
154 void setCursorRowAndColumn()
156 if (keyboardNo2 == null)
158 keyboardNo2 = new StringBuffer();
162 seqCanvas.cursorX = getKeyboardNo1() - 1;
163 seqCanvas.cursorY = getKeyboardNo2() - 1;
168 void setCursorPosition()
170 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
172 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
176 void moveCursor(int dx, int dy)
178 seqCanvas.cursorX += dx;
179 seqCanvas.cursorY += dy;
180 if (av.hasHiddenColumns()
181 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
183 int original = seqCanvas.cursorX - dx;
184 int maxWidth = av.getAlignment().getWidth();
186 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
187 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
189 seqCanvas.cursorX += dx;
192 if (seqCanvas.cursorX >= maxWidth
193 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
195 seqCanvas.cursorX = original;
201 void scrollToVisible()
203 if (seqCanvas.cursorX < 0)
205 seqCanvas.cursorX = 0;
207 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
209 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
212 if (seqCanvas.cursorY < 0)
214 seqCanvas.cursorY = 0;
216 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
218 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
222 if (av.getWrapAlignment())
224 ap.scrollToWrappedVisible(seqCanvas.cursorX);
228 while (seqCanvas.cursorY < av.startSeq)
232 while (seqCanvas.cursorY + 1 > av.endSeq)
236 while (seqCanvas.cursorX < av.getColumnSelection()
237 .adjustForHiddenColumns(av.startRes))
240 if (!ap.scrollRight(false))
245 while (seqCanvas.cursorX > av.getColumnSelection()
246 .adjustForHiddenColumns(av.endRes))
248 if (!ap.scrollRight(true))
254 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
255 seqCanvas.cursorX, seqCanvas.cursorY);
260 void setSelectionAreaAtCursor(boolean topLeft)
262 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
264 if (av.getSelectionGroup() != null)
266 SequenceGroup sg = av.getSelectionGroup();
267 // Find the top and bottom of this group
268 int min = av.getAlignment().getHeight(), max = 0;
269 for (int i = 0; i < sg.getSize(); i++)
271 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
286 sg.setStartRes(seqCanvas.cursorX);
287 if (sg.getEndRes() < seqCanvas.cursorX)
289 sg.setEndRes(seqCanvas.cursorX);
292 min = seqCanvas.cursorY;
296 sg.setEndRes(seqCanvas.cursorX);
297 if (sg.getStartRes() > seqCanvas.cursorX)
299 sg.setStartRes(seqCanvas.cursorX);
302 max = seqCanvas.cursorY + 1;
307 // Only the user can do this
308 av.setSelectionGroup(null);
312 // Now add any sequences between min and max
314 for (int i = min; i < max; i++)
316 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
321 if (av.getSelectionGroup() == null)
323 SequenceGroup sg = new SequenceGroup();
324 sg.setStartRes(seqCanvas.cursorX);
325 sg.setEndRes(seqCanvas.cursorX);
326 sg.addSequence(sequence, false);
327 av.setSelectionGroup(sg);
329 ap.paintAlignment(false);
333 void insertGapAtCursor(boolean group)
335 groupEditing = group;
336 startseq = seqCanvas.cursorY;
337 lastres = seqCanvas.cursorX;
338 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
342 void deleteGapAtCursor(boolean group)
344 groupEditing = group;
345 startseq = seqCanvas.cursorY;
346 lastres = seqCanvas.cursorX + getKeyboardNo1();
347 editSequence(false, seqCanvas.cursorX);
351 void numberPressed(char value)
353 if (keyboardNo1 == null)
355 keyboardNo1 = new StringBuffer();
358 if (keyboardNo2 != null)
360 keyboardNo2.append(value);
364 keyboardNo1.append(value);
372 if (keyboardNo1 != null)
374 int value = Integer.parseInt(keyboardNo1.toString());
378 } catch (Exception x)
389 if (keyboardNo2 != null)
391 int value = Integer.parseInt(keyboardNo2.toString());
395 } catch (Exception x)
403 * Set status message in alignment panel
406 * aligned sequence object
410 * index of sequence in alignment
411 * @return position of res in sequence
413 void setStatusMessage(SequenceI sequence, int res, int seq)
415 // TODO remove duplication of identical gui method
416 StringBuilder text = new StringBuilder(32);
417 String seqno = seq == -1 ? "" : " " + (seq + 1);
418 text.append("Sequence" + seqno + " ID: " + sequence.getName());
420 String residue = null;
422 * Try to translate the display character to residue name (null for gap).
424 final String displayChar = String.valueOf(sequence.getCharAt(res));
425 if (av.getAlignment().isNucleotide())
427 residue = ResidueProperties.nucleotideName.get(displayChar);
430 text.append(" Nucleotide: ").append(residue);
435 residue = "X".equalsIgnoreCase(displayChar) ? "X"
436 : ResidueProperties.aa2Triplet.get(displayChar);
439 text.append(" Residue: ").append(residue);
446 pos = sequence.findPosition(res);
447 text.append(" (").append(Integer.toString(pos)).append(")");
449 // Object obj = null;
450 // if (av.getAlignment().isNucleotide())
452 // obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
456 // text.append(" Nucleotide: ");
461 // obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
464 // text.append(" Residue: ");
473 // text.append(obj + " (" + sequence.findPosition(res) + ")");
477 ap.alignFrame.statusBar.setText(text.toString());
482 * Set the status bar message to highlight the first matched position in
487 private void setStatusMessage(SearchResults results)
489 List<Match> matches = results.getResults();
490 if (!matches.isEmpty())
492 Match m = matches.get(0);
493 SequenceI seq = m.getSequence();
494 int sequenceIndex = this.av.getAlignment().findIndex(seq);
497 * Convert position in sequence (base 1) to sequence character array index
500 int start = m.getStart() - 1;
501 setStatusMessage(seq, start, sequenceIndex);
505 public void mousePressed(MouseEvent evt)
507 lastMousePress = evt.getPoint();
509 // For now, ignore the mouseWheel font resizing on Macs
510 // As the Button2_mask always seems to be true
511 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
514 mouseWheelPressed = true;
518 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
520 if (evt.isControlDown() || evt.isAltDown())
528 doMousePressedDefineMode(evt);
532 int seq = findSeq(evt);
533 int res = findRes(evt);
535 if (seq < 0 || res < 0)
540 if ((seq < av.getAlignment().getHeight())
541 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
555 public void mouseClicked(MouseEvent evt)
557 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
558 if (evt.getClickCount() > 1)
560 if (av.getSelectionGroup() != null
561 && av.getSelectionGroup().getSize() == 1
562 && av.getSelectionGroup().getEndRes()
563 - av.getSelectionGroup().getStartRes() < 2)
565 av.setSelectionGroup(null);
568 SequenceFeature[] features = findFeaturesAtRes(sequence,
569 sequence.findPosition(findRes(evt)));
571 if (features != null && features.length > 0)
573 SearchResults highlight = new SearchResults();
574 highlight.addResult(sequence, features[0].getBegin(),
575 features[0].getEnd());
576 seqCanvas.highlightSearchResults(highlight);
578 if (features != null && features.length > 0)
580 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
581 { sequence }, features, false, ap);
583 seqCanvas.highlightSearchResults(null);
588 public void mouseReleased(MouseEvent evt)
590 mouseDragging = false;
591 mouseWheelPressed = false;
592 ap.paintAlignment(true);
596 doMouseReleasedDefineMode(evt);
604 int startWrapBlock = -1;
606 int wrappedBlock = -1;
608 int findRes(MouseEvent evt)
613 if (av.getWrapAlignment())
616 int hgap = av.getCharHeight();
617 if (av.getScaleAboveWrapped())
619 hgap += av.getCharHeight();
622 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
624 + seqCanvas.getAnnotationHeight();
628 x -= seqCanvas.LABEL_WEST;
630 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
636 wrappedBlock = y / cHeight;
637 wrappedBlock += av.getStartRes() / cwidth;
639 res = wrappedBlock * cwidth + x / av.getCharWidth();
644 res = (x / av.getCharWidth()) + av.getStartRes();
647 if (av.hasHiddenColumns())
649 res = av.getColumnSelection().adjustForHiddenColumns(res);
656 int findSeq(MouseEvent evt)
658 final int sqnum = findAlRow(evt);
659 return (sqnum < 0) ? 0 : sqnum;
665 * @return row in alignment that was selected (or -1 for column selection)
667 private int findAlRow(MouseEvent evt)
672 if (av.getWrapAlignment())
674 int hgap = av.getCharHeight();
675 if (av.getScaleAboveWrapped())
677 hgap += av.getCharHeight();
680 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
682 + seqCanvas.getAnnotationHeight();
686 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
695 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
696 .getAlignment().getHeight() - 1);
706 public void doMousePressed(MouseEvent evt)
709 int seq = findSeq(evt);
710 int res = findRes(evt);
712 if (seq < av.getAlignment().getHeight()
713 && res < av.getAlignment().getSequenceAt(seq).getLength())
715 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
716 // 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);
744 public void highlightSequence(SearchResults results)
746 if (av.isFollowHighlight())
748 if (ap.scrollToPosition(results, true))
750 ap.alignFrame.repaint();
753 setStatusMessage(results);
754 seqCanvas.highlightSearchResults(results);
759 public VamsasSource getVamsasSource()
761 return this.ap == null ? null : this.ap.av;
764 public void updateColours(SequenceI seq, int index)
766 System.out.println("update the seqPanel colours");
770 public void mouseMoved(MouseEvent evt)
772 int res = findRes(evt);
773 int seq = findSeq(evt);
775 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
784 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
785 if (res > sequence.getLength())
794 int respos = sequence.findPosition(res);
797 mouseOverSequence(sequence, res, respos);
800 StringBuilder text = new StringBuilder();
801 text.append("Sequence ").append(Integer.toString(seq + 1))
802 .append(" ID: ").append(sequence.getName());
805 final String ch = String.valueOf(sequence.getCharAt(res));
806 if (av.getAlignment().isNucleotide())
808 obj = ResidueProperties.nucleotideName.get(ch);
811 text.append(" Nucleotide: ").append(obj);
816 obj = "X".equalsIgnoreCase(ch) ? "X"
817 : ResidueProperties.aa2Triplet.get(ch);
820 text.append(" Residue: ").append(obj);
826 text.append(" (").append(Integer.toString(respos)).append(")");
829 ap.alignFrame.statusBar.setText(text.toString());
831 StringBuilder tooltipText = new StringBuilder();
832 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
835 for (int g = 0; g < groups.length; g++)
837 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
839 if (!groups[g].getName().startsWith("JTreeGroup")
840 && !groups[g].getName().startsWith("JGroup"))
842 tooltipText.append(groups[g].getName()).append(" ");
844 if (groups[g].getDescription() != null)
846 tooltipText.append(groups[g].getDescription());
848 tooltipText.append("\n");
853 // use aa to see if the mouse pointer is on a
854 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
855 sequence.findPosition(res));
858 while (index < allFeatures.length)
860 SequenceFeature sf = allFeatures[index];
862 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
864 if (sf.getDescription() != null)
866 tooltipText.append(" " + sf.getDescription());
869 if (sf.getValue("status") != null)
871 String status = sf.getValue("status").toString();
872 if (status.length() > 0)
874 tooltipText.append(" (" + sf.getValue("status") + ")");
877 tooltipText.append("\n");
884 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
888 tooltip.setTip(tooltipText.toString());
892 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
894 Vector tmp = new Vector();
895 SequenceFeature[] features = sequence.getSequenceFeatures();
896 if (features != null)
898 for (int i = 0; i < features.length; i++)
900 if (av.getFeaturesDisplayed() == null
901 || !av.getFeaturesDisplayed().isVisible(features[i].getType()))
906 if (features[i].featureGroup != null
907 && !seqCanvas.fr.checkGroupVisibility(features[i].featureGroup,false))
912 if ((features[i].getBegin() <= res)
913 && (features[i].getEnd() >= res))
915 tmp.addElement(features[i]);
920 features = new SequenceFeature[tmp.size()];
921 tmp.copyInto(features);
928 public void mouseDragged(MouseEvent evt)
930 if (mouseWheelPressed)
932 int oldWidth = av.getCharWidth();
934 // Which is bigger, left-right or up-down?
935 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
938 int fontSize = av.font.getSize();
940 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
944 else if (evt.getY() > lastMousePress.y)
954 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
955 av.setCharWidth(oldWidth);
959 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
961 av.setCharWidth(av.getCharWidth() - 1);
963 else if (evt.getX() > lastMousePress.x)
965 av.setCharWidth(av.getCharWidth() + 1);
968 if (av.getCharWidth() < 1)
976 FontMetrics fm = getFontMetrics(av.getFont());
977 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
979 lastMousePress = evt.getPoint();
981 ap.paintAlignment(false);
982 ap.annotationPanel.image = null;
988 doMouseDraggedDefineMode(evt);
992 int res = findRes(evt);
999 if ((lastres == -1) || (lastres == res))
1004 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1006 // dragLeft, delete gap
1007 editSequence(false, res);
1011 editSequence(true, res);
1014 mouseDragging = true;
1015 if (scrollThread != null)
1017 scrollThread.setEvent(evt);
1022 synchronized void editSequence(boolean insertGap, int startres)
1025 int fixedRight = -1;
1026 boolean fixedColumns = false;
1027 SequenceGroup sg = av.getSelectionGroup();
1029 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1031 if (!groupEditing && av.hasHiddenRows())
1033 if (av.isHiddenRepSequence(seq))
1035 sg = av.getRepresentedSequences(seq);
1036 groupEditing = true;
1040 StringBuffer message = new StringBuffer();
1043 message.append(MessageManager.getString("action.edit_group")).append(
1045 if (editCommand == null)
1047 editCommand = new EditCommand(
1048 MessageManager.getString("action.edit_group"));
1053 message.append(MessageManager.getString("label.edit_sequence"))
1054 .append(" " + seq.getName());
1055 String label = seq.getName();
1056 if (label.length() > 10)
1058 label = label.substring(0, 10);
1060 if (editCommand == null)
1062 editCommand = new EditCommand(MessageManager.formatMessage(
1063 "label.edit_params", new String[]
1070 message.append(" insert ");
1074 message.append(" delete ");
1077 message.append(Math.abs(startres - lastres) + " gaps.");
1078 ap.alignFrame.statusBar.setText(message.toString());
1080 // Are we editing within a selection group?
1082 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1085 fixedColumns = true;
1087 // sg might be null as the user may only see 1 sequence,
1088 // but the sequence represents a group
1091 if (!av.isHiddenRepSequence(seq))
1097 sg = av.getRepresentedSequences(seq);
1100 fixedLeft = sg.getStartRes();
1101 fixedRight = sg.getEndRes();
1103 if ((startres < fixedLeft && lastres >= fixedLeft)
1104 || (startres >= fixedLeft && lastres < fixedLeft)
1105 || (startres > fixedRight && lastres <= fixedRight)
1106 || (startres <= fixedRight && lastres > fixedRight))
1112 if (fixedLeft > startres)
1114 fixedRight = fixedLeft - 1;
1117 else if (fixedRight < startres)
1119 fixedLeft = fixedRight;
1124 if (av.hasHiddenColumns())
1126 fixedColumns = true;
1127 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1128 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1130 if ((insertGap && startres > y1 && lastres < y1)
1131 || (!insertGap && startres < y2 && lastres > y2))
1137 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1138 // Selection spans a hidden region
1139 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1147 fixedRight = y2 - 1;
1154 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1155 .toArray(new SequenceI[0]);
1160 // If the user has selected the whole sequence, and is dragging to
1161 // the right, we can still extend the alignment and selectionGroup
1162 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1163 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1165 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1166 fixedRight = sg.getEndRes();
1169 // Is it valid with fixed columns??
1170 // Find the next gap before the end
1171 // of the visible region boundary
1172 boolean blank = false;
1173 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1177 for (SequenceI gs : groupSeqs)
1179 for (int j = 0; j < startres - lastres; j++)
1181 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1197 if (sg.getSize() == av.getAlignment().getHeight())
1199 if ((av.hasHiddenColumns() && startres < av
1200 .getColumnSelection().getHiddenBoundaryRight(startres)))
1206 int alWidth = av.getAlignment().getWidth();
1207 if (av.hasHiddenRows())
1209 int hwidth = av.getAlignment().getHiddenSequences()
1211 if (hwidth > alWidth)
1216 // We can still insert gaps if the selectionGroup
1217 // contains all the sequences
1218 sg.setEndRes(sg.getEndRes() + startres - lastres);
1219 fixedRight = alWidth + startres - lastres;
1230 else if (!insertGap)
1232 // / Are we able to delete?
1233 // ie are all columns blank?
1235 for (SequenceI gs : groupSeqs)
1237 for (int j = startres; j < lastres; j++)
1239 if (gs.getLength() <= j)
1244 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1246 // Not a gap, block edit not valid
1256 // dragging to the right
1257 if (fixedColumns && fixedRight != -1)
1259 for (int j = lastres; j < startres; j++)
1261 insertChar(j, groupSeqs, fixedRight);
1266 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1267 startres - lastres, av.getAlignment(), true);
1272 // dragging to the left
1273 if (fixedColumns && fixedRight != -1)
1275 for (int j = lastres; j > startres; j--)
1277 deleteChar(startres, groupSeqs, fixedRight);
1282 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1283 lastres - startres, av.getAlignment(), true);
1289 // ///Editing a single sequence///////////
1293 // dragging to the right
1294 if (fixedColumns && fixedRight != -1)
1296 for (int j = lastres; j < startres; j++)
1298 insertChar(j, new SequenceI[]
1299 { seq }, fixedRight);
1304 editCommand.appendEdit(Action.INSERT_GAP, new SequenceI[]
1305 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1310 // dragging to the left
1311 if (fixedColumns && fixedRight != -1)
1313 for (int j = lastres; j > startres; j--)
1315 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1320 deleteChar(startres, new SequenceI[]
1321 { seq }, fixedRight);
1326 // could be a keyboard edit trying to delete none gaps
1328 for (int m = startres; m < lastres; m++)
1330 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1339 editCommand.appendEdit(Action.DELETE_GAP, new SequenceI[]
1340 { seq }, startres, max, av.getAlignment(), true);
1347 seqCanvas.repaint();
1350 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1352 int blankColumn = fixedColumn;
1353 for (int s = 0; s < seq.length; s++)
1355 // Find the next gap before the end of the visible region boundary
1356 // If lastCol > j, theres a boundary after the gap insertion
1358 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1360 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1362 // Theres a space, so break and insert the gap
1367 if (blankColumn <= j)
1369 blankColumn = fixedColumn;
1375 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1376 av.getAlignment(), true);
1378 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1383 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1386 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1389 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1390 av.getAlignment(), true);
1393 // ////////////////////////////////////////
1394 // ///Everything below this is for defining the boundary of the rubberband
1395 // ////////////////////////////////////////
1396 public void doMousePressedDefineMode(MouseEvent evt)
1398 if (scrollThread != null)
1400 scrollThread.running = false;
1401 scrollThread = null;
1404 int res = findRes(evt);
1405 int seq = findSeq(evt);
1407 startWrapBlock = wrappedBlock;
1414 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1416 if (sequence == null || res > sequence.getLength())
1421 stretchGroup = av.getSelectionGroup();
1423 if (stretchGroup == null)
1425 stretchGroup = av.getAlignment().findGroup(sequence);
1426 if (stretchGroup != null && res > stretchGroup.getStartRes()
1427 && res < stretchGroup.getEndRes())
1429 av.setSelectionGroup(stretchGroup);
1433 stretchGroup = null;
1437 else if (!stretchGroup.getSequences(null).contains(sequence)
1438 || stretchGroup.getStartRes() > res
1439 || stretchGroup.getEndRes() < res)
1441 stretchGroup = null;
1443 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1445 if (allGroups != null)
1447 for (int i = 0; i < allGroups.length; i++)
1449 if (allGroups[i].getStartRes() <= res
1450 && allGroups[i].getEndRes() >= res)
1452 stretchGroup = allGroups[i];
1457 av.setSelectionGroup(stretchGroup);
1460 // DETECT RIGHT MOUSE BUTTON IN AWT
1461 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1463 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1464 sequence.findPosition(res));
1466 Vector<String> links = null;
1467 if (allFeatures != null)
1469 for (int i = 0; i < allFeatures.length; i++)
1471 if (allFeatures[i].links != null)
1475 links = new Vector<String>();
1477 for (int j = 0; j < allFeatures[i].links.size(); j++)
1479 links.addElement(allFeatures[i].links.elementAt(j));
1484 APopupMenu popup = new APopupMenu(ap, null, links);
1486 popup.show(this, evt.getX(), evt.getY());
1492 seqCanvas.cursorX = findRes(evt);
1493 seqCanvas.cursorY = findSeq(evt);
1494 seqCanvas.repaint();
1498 // Only if left mouse button do we want to change group sizes
1500 if (stretchGroup == null)
1502 // define a new group here
1503 SequenceGroup sg = new SequenceGroup();
1504 sg.setStartRes(res);
1506 sg.addSequence(sequence, false);
1507 av.setSelectionGroup(sg);
1510 if (av.getConservationSelected())
1512 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1515 if (av.getAbovePIDThreshold())
1517 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1524 public void doMouseReleasedDefineMode(MouseEvent evt)
1526 if (stretchGroup == null)
1531 stretchGroup.recalcConservation(); // always do this - annotation has own
1533 if (stretchGroup.cs != null)
1535 stretchGroup.cs.alignmentChanged(stretchGroup,
1536 av.getHiddenRepSequences());
1538 if (stretchGroup.cs.conservationApplied())
1540 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1541 stretchGroup.getName());
1545 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1546 stretchGroup.getName());
1549 changeEndRes = false;
1550 changeStartRes = false;
1551 stretchGroup = null;
1552 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1553 ap.paintAlignment(true);
1557 public void doMouseDraggedDefineMode(MouseEvent evt)
1559 int res = findRes(evt);
1560 int y = findSeq(evt);
1562 if (wrappedBlock != startWrapBlock)
1567 if (stretchGroup == null)
1572 mouseDragging = true;
1574 if (y > av.getAlignment().getHeight())
1576 y = av.getAlignment().getHeight() - 1;
1579 if (res >= av.getAlignment().getWidth())
1581 res = av.getAlignment().getWidth() - 1;
1584 if (stretchGroup.getEndRes() == res)
1586 // Edit end res position of selected group
1587 changeEndRes = true;
1589 else if (stretchGroup.getStartRes() == res)
1591 // Edit start res position of selected group
1592 changeStartRes = true;
1602 if (res > (stretchGroup.getStartRes() - 1))
1604 stretchGroup.setEndRes(res);
1607 else if (changeStartRes)
1609 if (res < (stretchGroup.getEndRes() + 1))
1611 stretchGroup.setStartRes(res);
1615 int dragDirection = 0;
1621 else if (y < oldSeq)
1626 while ((y != oldSeq) && (oldSeq > -1)
1627 && (y < av.getAlignment().getHeight()))
1629 // This routine ensures we don't skip any sequences, as the
1630 // selection is quite slow.
1631 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1633 oldSeq += dragDirection;
1640 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1642 if (stretchGroup.getSequences(null).contains(nextSeq))
1644 stretchGroup.deleteSequence(seq, false);
1650 stretchGroup.addSequence(seq, false);
1653 stretchGroup.addSequence(nextSeq, false);
1662 if (res > av.endRes || res < av.startRes || y < av.startSeq
1668 if (scrollThread != null)
1670 scrollThread.setEvent(evt);
1673 seqCanvas.repaint();
1676 public void mouseEntered(MouseEvent e)
1683 if (scrollThread != null)
1685 scrollThread.running = false;
1686 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()
1758 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1760 running = ap.scrollUp(true);
1763 if (mouseDragging && evt.getY() >= getSize().height
1764 && av.getAlignment().getHeight() > av.getEndSeq())
1766 running = ap.scrollUp(false);
1769 if (mouseDragging && evt.getX() < 0)
1771 running = ap.scrollRight(false);
1774 else if (mouseDragging && evt.getX() >= getSize().width)
1776 running = ap.scrollRight(true);
1783 } catch (Exception ex)
1791 * modify current selection according to a received message.
1793 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1794 SelectionSource source)
1796 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1797 // handles selection messages...
1798 // TODO: extend config options to allow user to control if selections may be
1799 // shared between viewports.
1801 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignmentViewport) source)
1802 .getSequenceSetId().equals(av.getSequenceSetId()))))
1808 * Check for selection in a view of which this one is a dna/protein
1811 if (selectionFromTranslation(seqsel, colsel, source))
1816 // do we want to thread this ? (contention with seqsel and colsel locks, I
1818 // rules are: colsel is copied if there is a real intersection between
1819 // sequence selection
1820 boolean repaint = false, copycolsel = true;
1821 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1823 SequenceGroup sgroup = null;
1824 if (seqsel != null && seqsel.getSize() > 0)
1826 if (av.getAlignment() == null)
1829 .println("Selection message: alignviewport av SeqSetId="
1830 + av.getSequenceSetId() + " ViewId="
1832 + " 's alignment is NULL! returning immediatly.");
1835 sgroup = seqsel.intersect(av.getAlignment(),
1836 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1837 if ((sgroup == null || sgroup.getSize() == 0)
1838 && (colsel == null || colsel.size() == 0))
1840 // don't copy columns if the region didn't intersect.
1844 if (sgroup != null && sgroup.getSize() > 0)
1846 av.setSelectionGroup(sgroup);
1850 av.setSelectionGroup(null);
1852 repaint = av.isSelectionGroupChanged(true);
1855 && (av.getColumnSelection() == null || !av
1856 .isColSelChanged(true)))
1858 // the current selection is unset or from a previous message
1859 // so import the new colsel.
1860 if (colsel == null || colsel.size() == 0)
1862 if (av.getColumnSelection() != null)
1864 av.getColumnSelection().clear();
1869 // TODO: shift colSel according to the intersecting sequences
1870 if (av.getColumnSelection() == null)
1872 av.setColumnSelection(new ColumnSelection(colsel));
1876 av.getColumnSelection().setElementsFrom(colsel);
1879 repaint |= av.isColSelChanged(true);
1882 && av.hasHiddenColumns()
1883 && (av.getColumnSelection() == null || av.getColumnSelection()
1884 .getHiddenColumns() == null))
1886 System.err.println("Bad things");
1890 ap.scalePanelHolder.repaint();
1896 * scroll to the given row/column - or nearest visible location
1901 public void scrollTo(int row, int column)
1904 row = row < 0 ? ap.av.startSeq : row;
1905 column = column < 0 ? ap.av.startRes : column;
1906 ap.scrollTo(column, column, row, true, true);
1910 * scroll to the given row - or nearest visible location
1914 public void scrollToRow(int row)
1917 row = row < 0 ? ap.av.startSeq : row;
1918 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1922 * scroll to the given column - or nearest visible location
1926 public void scrollToColumn(int column)
1929 column = column < 0 ? ap.av.startRes : column;
1930 ap.scrollTo(column, column, ap.av.startSeq, true, true);
1934 * If this panel is a cdna/protein translation view of the selection source,
1935 * tries to map the source selection to a local one, and returns true. Else
1942 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1943 ColumnSelection colsel, SelectionSource source)
1945 if (!(source instanceof AlignViewportI)) {
1948 final AlignViewportI sourceAv = (AlignViewportI) source;
1949 if (sourceAv.getCodingComplement() != av && av.getCodingComplement() != sourceAv)
1955 * Map sequence selection
1957 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1958 av.setSelectionGroup(sg);
1959 av.isSelectionGroupChanged(true);
1962 * Map column selection
1964 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1966 av.setColumnSelection(cs);
1967 av.isColSelChanged(true);
1969 ap.scalePanelHolder.repaint();