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.ColumnSelection;
27 import jalview.datamodel.SearchResults;
28 import jalview.datamodel.SearchResults.Match;
29 import jalview.datamodel.Sequence;
30 import jalview.datamodel.SequenceFeature;
31 import jalview.datamodel.SequenceGroup;
32 import jalview.datamodel.SequenceI;
33 import jalview.schemes.ResidueProperties;
34 import jalview.structure.SelectionListener;
35 import jalview.structure.SelectionSource;
36 import jalview.structure.SequenceListener;
37 import jalview.structure.StructureSelectionManager;
38 import jalview.structure.VamsasSource;
39 import jalview.util.Comparison;
40 import jalview.util.MappingUtils;
41 import jalview.util.MessageManager;
42 import jalview.viewmodel.AlignmentViewport;
44 import java.awt.BorderLayout;
46 import java.awt.FontMetrics;
47 import java.awt.Point;
48 import java.awt.event.InputEvent;
49 import java.awt.event.MouseEvent;
50 import java.awt.event.MouseListener;
51 import java.awt.event.MouseMotionListener;
52 import java.util.List;
53 import java.util.Vector;
55 import javax.swing.JPanel;
57 public class SeqPanel extends JPanel implements MouseMotionListener,
58 MouseListener, SequenceListener, SelectionListener
61 public SeqCanvas seqCanvas;
63 public AlignmentPanel ap;
65 protected int lastres;
67 protected int startseq;
69 protected AlignViewport av;
71 // if character is inserted or deleted, we will need to recalculate the
73 boolean seqEditOccurred = false;
75 ScrollThread scrollThread = null;
77 boolean mouseDragging = false;
79 boolean editingSeqs = false;
81 boolean groupEditing = false;
85 boolean changeEndSeq = false;
87 boolean changeStartSeq = false;
89 boolean changeEndRes = false;
91 boolean changeStartRes = false;
93 SequenceGroup stretchGroup = null;
95 StringBuffer keyboardNo1;
97 StringBuffer keyboardNo2;
99 boolean mouseWheelPressed = false;
101 Point lastMousePress;
103 EditCommand editCommand;
105 StructureSelectionManager ssm;
107 public SeqPanel(AlignViewport avp, AlignmentPanel p)
111 seqCanvas = new SeqCanvas(avp);
112 setLayout(new BorderLayout());
117 seqCanvas.addMouseMotionListener(this);
118 seqCanvas.addMouseListener(this);
119 ssm = StructureSelectionManager.getStructureSelectionManager(av.applet);
120 ssm.addStructureViewerListener(this);
121 ssm.addSelectionListener(this);
128 if (editCommand != null && editCommand.getSize() > 0)
130 ap.alignFrame.addHistoryItem(editCommand);
131 av.firePropertyChange("alignment", null, av.getAlignment()
138 groupEditing = false;
146 seqCanvas.cursorY = getKeyboardNo1() - 1;
150 void setCursorColumn()
152 seqCanvas.cursorX = getKeyboardNo1() - 1;
156 void setCursorRowAndColumn()
158 if (keyboardNo2 == null)
160 keyboardNo2 = new StringBuffer();
164 seqCanvas.cursorX = getKeyboardNo1() - 1;
165 seqCanvas.cursorY = getKeyboardNo2() - 1;
170 void setCursorPosition()
172 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
174 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
178 void moveCursor(int dx, int dy)
180 seqCanvas.cursorX += dx;
181 seqCanvas.cursorY += dy;
182 if (av.hasHiddenColumns()
183 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
185 int original = seqCanvas.cursorX - dx;
186 int maxWidth = av.getAlignment().getWidth();
188 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
189 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
191 seqCanvas.cursorX += dx;
194 if (seqCanvas.cursorX >= maxWidth
195 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
197 seqCanvas.cursorX = original;
203 void scrollToVisible()
205 if (seqCanvas.cursorX < 0)
207 seqCanvas.cursorX = 0;
209 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
211 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
214 if (seqCanvas.cursorY < 0)
216 seqCanvas.cursorY = 0;
218 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
220 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
224 if (av.getWrapAlignment())
226 ap.scrollToWrappedVisible(seqCanvas.cursorX);
230 while (seqCanvas.cursorY < av.startSeq)
234 while (seqCanvas.cursorY + 1 > av.endSeq)
238 while (seqCanvas.cursorX < av.getColumnSelection()
239 .adjustForHiddenColumns(av.startRes))
242 if (!ap.scrollRight(false))
247 while (seqCanvas.cursorX > av.getColumnSelection()
248 .adjustForHiddenColumns(av.endRes))
250 if (!ap.scrollRight(true))
256 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
257 seqCanvas.cursorX, seqCanvas.cursorY);
262 void setSelectionAreaAtCursor(boolean topLeft)
264 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
266 if (av.getSelectionGroup() != null)
268 SequenceGroup sg = av.getSelectionGroup();
269 // Find the top and bottom of this group
270 int min = av.getAlignment().getHeight(), max = 0;
271 for (int i = 0; i < sg.getSize(); i++)
273 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
288 sg.setStartRes(seqCanvas.cursorX);
289 if (sg.getEndRes() < seqCanvas.cursorX)
291 sg.setEndRes(seqCanvas.cursorX);
294 min = seqCanvas.cursorY;
298 sg.setEndRes(seqCanvas.cursorX);
299 if (sg.getStartRes() > seqCanvas.cursorX)
301 sg.setStartRes(seqCanvas.cursorX);
304 max = seqCanvas.cursorY + 1;
309 // Only the user can do this
310 av.setSelectionGroup(null);
314 // Now add any sequences between min and max
316 for (int i = min; i < max; i++)
318 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
323 if (av.getSelectionGroup() == null)
325 SequenceGroup sg = new SequenceGroup();
326 sg.setStartRes(seqCanvas.cursorX);
327 sg.setEndRes(seqCanvas.cursorX);
328 sg.addSequence(sequence, false);
329 av.setSelectionGroup(sg);
331 ap.paintAlignment(false);
335 void insertGapAtCursor(boolean group)
337 groupEditing = group;
338 startseq = seqCanvas.cursorY;
339 lastres = seqCanvas.cursorX;
340 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
344 void deleteGapAtCursor(boolean group)
346 groupEditing = group;
347 startseq = seqCanvas.cursorY;
348 lastres = seqCanvas.cursorX + getKeyboardNo1();
349 editSequence(false, seqCanvas.cursorX);
353 void numberPressed(char value)
355 if (keyboardNo1 == null)
357 keyboardNo1 = new StringBuffer();
360 if (keyboardNo2 != null)
362 keyboardNo2.append(value);
366 keyboardNo1.append(value);
374 if (keyboardNo1 != null)
376 int value = Integer.parseInt(keyboardNo1.toString());
380 } catch (Exception x)
391 if (keyboardNo2 != null)
393 int value = Integer.parseInt(keyboardNo2.toString());
397 } catch (Exception x)
405 * Set status message in alignment panel
408 * aligned sequence object
412 * index of sequence in alignment
413 * @return position of res in sequence
415 void setStatusMessage(SequenceI sequence, int res, int seq)
417 // TODO remove duplication of identical gui method
418 StringBuilder text = new StringBuilder(32);
419 String seqno = seq == -1 ? "" : " " + (seq + 1);
420 text.append("Sequence" + seqno + " ID: " + sequence.getName());
422 String residue = null;
424 * Try to translate the display character to residue name (null for gap).
426 final String displayChar = String.valueOf(sequence.getCharAt(res));
427 if (av.getAlignment().isNucleotide())
429 residue = ResidueProperties.nucleotideName.get(displayChar);
432 text.append(" Nucleotide: ").append(residue);
437 residue = "X".equalsIgnoreCase(displayChar) ? "X"
438 : ResidueProperties.aa2Triplet.get(displayChar);
441 text.append(" Residue: ").append(residue);
448 pos = sequence.findPosition(res);
449 text.append(" (").append(Integer.toString(pos)).append(")");
451 // Object obj = null;
452 // if (av.getAlignment().isNucleotide())
454 // obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
458 // text.append(" Nucleotide: ");
463 // obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
466 // text.append(" Residue: ");
475 // text.append(obj + " (" + sequence.findPosition(res) + ")");
479 ap.alignFrame.statusBar.setText(text.toString());
484 * Set the status bar message to highlight the first matched position in
489 private void setStatusMessage(SearchResults results)
491 List<Match> matches = results.getResults();
492 if (!matches.isEmpty())
494 Match m = matches.get(0);
495 SequenceI seq = m.getSequence();
496 int sequenceIndex = this.av.getAlignment().findIndex(seq);
499 * Convert position in sequence (base 1) to sequence character array index
502 int start = m.getStart() - 1;
503 setStatusMessage(seq, start, sequenceIndex);
507 public void mousePressed(MouseEvent evt)
509 lastMousePress = evt.getPoint();
511 // For now, ignore the mouseWheel font resizing on Macs
512 // As the Button2_mask always seems to be true
513 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
516 mouseWheelPressed = true;
520 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
522 if (evt.isControlDown() || evt.isAltDown())
530 doMousePressedDefineMode(evt);
534 int seq = findSeq(evt);
535 int res = findRes(evt);
537 if (seq < 0 || res < 0)
542 if ((seq < av.getAlignment().getHeight())
543 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
557 public void mouseClicked(MouseEvent evt)
559 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
560 if (evt.getClickCount() > 1)
562 if (av.getSelectionGroup() != null
563 && av.getSelectionGroup().getSize() == 1
564 && av.getSelectionGroup().getEndRes()
565 - av.getSelectionGroup().getStartRes() < 2)
567 av.setSelectionGroup(null);
570 SequenceFeature[] features = findFeaturesAtRes(sequence,
571 sequence.findPosition(findRes(evt)));
573 if (features != null && features.length > 0)
575 SearchResults highlight = new SearchResults();
576 highlight.addResult(sequence, features[0].getBegin(),
577 features[0].getEnd());
578 seqCanvas.highlightSearchResults(highlight);
580 if (features != null && features.length > 0)
582 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
583 { sequence }, features, false, ap);
585 seqCanvas.highlightSearchResults(null);
590 public void mouseReleased(MouseEvent evt)
592 mouseDragging = false;
593 mouseWheelPressed = false;
594 ap.paintAlignment(true);
598 doMouseReleasedDefineMode(evt);
606 int startWrapBlock = -1;
608 int wrappedBlock = -1;
610 int findRes(MouseEvent evt)
615 if (av.getWrapAlignment())
618 int hgap = av.getCharHeight();
619 if (av.getScaleAboveWrapped())
621 hgap += av.getCharHeight();
624 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
626 + seqCanvas.getAnnotationHeight();
630 x -= seqCanvas.LABEL_WEST;
632 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
638 wrappedBlock = y / cHeight;
639 wrappedBlock += av.getStartRes() / cwidth;
641 res = wrappedBlock * cwidth + x / av.getCharWidth();
646 res = (x / av.getCharWidth()) + av.getStartRes();
649 if (av.hasHiddenColumns())
651 res = av.getColumnSelection().adjustForHiddenColumns(res);
658 int findSeq(MouseEvent evt)
660 final int sqnum = findAlRow(evt);
661 return (sqnum < 0) ? 0 : sqnum;
667 * @return row in alignment that was selected (or -1 for column selection)
669 private int findAlRow(MouseEvent evt)
674 if (av.getWrapAlignment())
676 int hgap = av.getCharHeight();
677 if (av.getScaleAboveWrapped())
679 hgap += av.getCharHeight();
682 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
684 + seqCanvas.getAnnotationHeight();
688 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
697 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
698 .getAlignment().getHeight() - 1);
708 public void doMousePressed(MouseEvent evt)
711 int seq = findSeq(evt);
712 int res = findRes(evt);
714 if (seq < av.getAlignment().getHeight()
715 && res < av.getAlignment().getSequenceAt(seq).getLength())
717 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
718 // Find the residue's position in the sequence (res is the position
735 public void mouseOverSequence(SequenceI sequence, int index, int pos)
737 String tmp = sequence.hashCode() + index + "";
738 if (lastMessage == null || !lastMessage.equals(tmp))
740 ssm.mouseOverSequence(sequence, index, pos, av);
746 public void highlightSequence(SearchResults results)
748 if (av.isFollowHighlight())
750 if (ap.scrollToPosition(results, true))
752 ap.alignFrame.repaint();
755 setStatusMessage(results);
756 seqCanvas.highlightSearchResults(results);
761 public VamsasSource getVamsasSource()
763 return this.ap == null ? null : this.ap.av;
766 public void updateColours(SequenceI seq, int index)
768 System.out.println("update the seqPanel colours");
772 public void mouseMoved(MouseEvent evt)
774 int res = findRes(evt);
775 int seq = findSeq(evt);
777 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
786 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
787 if (res > sequence.getLength())
796 int respos = sequence.findPosition(res);
799 mouseOverSequence(sequence, res, respos);
802 StringBuilder text = new StringBuilder();
803 text.append("Sequence ").append(Integer.toString(seq + 1))
804 .append(" ID: ").append(sequence.getName());
807 final String ch = String.valueOf(sequence.getCharAt(res));
808 if (av.getAlignment().isNucleotide())
810 obj = ResidueProperties.nucleotideName.get(ch);
813 text.append(" Nucleotide: ").append(obj);
818 obj = "X".equalsIgnoreCase(ch) ? "X"
819 : ResidueProperties.aa2Triplet.get(ch);
822 text.append(" Residue: ").append(obj);
828 text.append(" (").append(Integer.toString(respos)).append(")");
831 ap.alignFrame.statusBar.setText(text.toString());
833 StringBuilder tooltipText = new StringBuilder();
834 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
837 for (int g = 0; g < groups.length; g++)
839 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
841 if (!groups[g].getName().startsWith("JTreeGroup")
842 && !groups[g].getName().startsWith("JGroup"))
844 tooltipText.append(groups[g].getName()).append(" ");
846 if (groups[g].getDescription() != null)
848 tooltipText.append(groups[g].getDescription());
850 tooltipText.append("\n");
855 // use aa to see if the mouse pointer is on a
856 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
857 sequence.findPosition(res));
860 while (index < allFeatures.length)
862 SequenceFeature sf = allFeatures[index];
864 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
866 if (sf.getDescription() != null)
868 tooltipText.append(" " + sf.getDescription());
871 if (sf.getValue("status") != null)
873 String status = sf.getValue("status").toString();
874 if (status.length() > 0)
876 tooltipText.append(" (" + sf.getValue("status") + ")");
879 tooltipText.append("\n");
886 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
890 tooltip.setTip(tooltipText.toString());
894 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
896 Vector tmp = new Vector();
897 SequenceFeature[] features = sequence.getSequenceFeatures();
898 if (features != null)
900 for (int i = 0; i < features.length; i++)
902 if (av.getFeaturesDisplayed() == null
903 || !av.getFeaturesDisplayed().isVisible(features[i].getType()))
908 if (features[i].featureGroup != null
909 && !seqCanvas.fr.checkGroupVisibility(features[i].featureGroup,false))
914 if ((features[i].getBegin() <= res)
915 && (features[i].getEnd() >= res))
917 tmp.addElement(features[i]);
922 features = new SequenceFeature[tmp.size()];
923 tmp.copyInto(features);
930 public void mouseDragged(MouseEvent evt)
932 if (mouseWheelPressed)
934 int oldWidth = av.getCharWidth();
936 // Which is bigger, left-right or up-down?
937 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
940 int fontSize = av.font.getSize();
942 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
946 else if (evt.getY() > lastMousePress.y)
956 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
957 av.setCharWidth(oldWidth);
961 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
963 av.setCharWidth(av.getCharWidth() - 1);
965 else if (evt.getX() > lastMousePress.x)
967 av.setCharWidth(av.getCharWidth() + 1);
970 if (av.getCharWidth() < 1)
978 FontMetrics fm = getFontMetrics(av.getFont());
979 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
981 lastMousePress = evt.getPoint();
983 ap.paintAlignment(false);
984 ap.annotationPanel.image = null;
990 doMouseDraggedDefineMode(evt);
994 int res = findRes(evt);
1001 if ((lastres == -1) || (lastres == res))
1006 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1008 // dragLeft, delete gap
1009 editSequence(false, res);
1013 editSequence(true, res);
1016 mouseDragging = true;
1017 if (scrollThread != null)
1019 scrollThread.setEvent(evt);
1024 synchronized void editSequence(boolean insertGap, int startres)
1027 int fixedRight = -1;
1028 boolean fixedColumns = false;
1029 SequenceGroup sg = av.getSelectionGroup();
1031 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1033 if (!groupEditing && av.hasHiddenRows())
1035 if (av.isHiddenRepSequence(seq))
1037 sg = av.getRepresentedSequences(seq);
1038 groupEditing = true;
1042 StringBuffer message = new StringBuffer();
1045 message.append(MessageManager.getString("action.edit_group")).append(
1047 if (editCommand == null)
1049 editCommand = new EditCommand(
1050 MessageManager.getString("action.edit_group"));
1055 message.append(MessageManager.getString("label.edit_sequence"))
1056 .append(" " + seq.getName());
1057 String label = seq.getName();
1058 if (label.length() > 10)
1060 label = label.substring(0, 10);
1062 if (editCommand == null)
1064 editCommand = new EditCommand(MessageManager.formatMessage(
1065 "label.edit_params", new String[]
1072 message.append(" insert ");
1076 message.append(" delete ");
1079 message.append(Math.abs(startres - lastres) + " gaps.");
1080 ap.alignFrame.statusBar.setText(message.toString());
1082 // Are we editing within a selection group?
1084 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1087 fixedColumns = true;
1089 // sg might be null as the user may only see 1 sequence,
1090 // but the sequence represents a group
1093 if (!av.isHiddenRepSequence(seq))
1099 sg = av.getRepresentedSequences(seq);
1102 fixedLeft = sg.getStartRes();
1103 fixedRight = sg.getEndRes();
1105 if ((startres < fixedLeft && lastres >= fixedLeft)
1106 || (startres >= fixedLeft && lastres < fixedLeft)
1107 || (startres > fixedRight && lastres <= fixedRight)
1108 || (startres <= fixedRight && lastres > fixedRight))
1114 if (fixedLeft > startres)
1116 fixedRight = fixedLeft - 1;
1119 else if (fixedRight < startres)
1121 fixedLeft = fixedRight;
1126 if (av.hasHiddenColumns())
1128 fixedColumns = true;
1129 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1130 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1132 if ((insertGap && startres > y1 && lastres < y1)
1133 || (!insertGap && startres < y2 && lastres > y2))
1139 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1140 // Selection spans a hidden region
1141 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1149 fixedRight = y2 - 1;
1156 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1157 .toArray(new SequenceI[0]);
1162 // If the user has selected the whole sequence, and is dragging to
1163 // the right, we can still extend the alignment and selectionGroup
1164 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1165 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1167 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1168 fixedRight = sg.getEndRes();
1171 // Is it valid with fixed columns??
1172 // Find the next gap before the end
1173 // of the visible region boundary
1174 boolean blank = false;
1175 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1179 for (SequenceI gs : groupSeqs)
1181 for (int j = 0; j < startres - lastres; j++)
1183 if (!Comparison.isGap(gs.getCharAt(fixedRight
1199 if (sg.getSize() == av.getAlignment().getHeight())
1201 if ((av.hasHiddenColumns() && startres < av
1202 .getColumnSelection().getHiddenBoundaryRight(startres)))
1208 int alWidth = av.getAlignment().getWidth();
1209 if (av.hasHiddenRows())
1211 int hwidth = av.getAlignment().getHiddenSequences()
1213 if (hwidth > alWidth)
1218 // We can still insert gaps if the selectionGroup
1219 // contains all the sequences
1220 sg.setEndRes(sg.getEndRes() + startres - lastres);
1221 fixedRight = alWidth + startres - lastres;
1232 else if (!insertGap)
1234 // / Are we able to delete?
1235 // ie are all columns blank?
1237 for (SequenceI gs : groupSeqs)
1239 for (int j = startres; j < lastres; j++)
1241 if (gs.getLength() <= j)
1246 if (!Comparison.isGap(gs.getCharAt(j)))
1248 // Not a gap, block edit not valid
1258 // dragging to the right
1259 if (fixedColumns && fixedRight != -1)
1261 for (int j = lastres; j < startres; j++)
1263 insertChar(j, groupSeqs, fixedRight);
1268 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1269 startres - lastres, av.getAlignment(), true);
1274 // dragging to the left
1275 if (fixedColumns && fixedRight != -1)
1277 for (int j = lastres; j > startres; j--)
1279 deleteChar(startres, groupSeqs, fixedRight);
1284 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1285 lastres - startres, av.getAlignment(), true);
1291 // ///Editing a single sequence///////////
1295 // dragging to the right
1296 if (fixedColumns && fixedRight != -1)
1298 for (int j = lastres; j < startres; j++)
1300 insertChar(j, new SequenceI[]
1301 { seq }, fixedRight);
1306 editCommand.appendEdit(Action.INSERT_GAP, new SequenceI[]
1307 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1312 // dragging to the left
1313 if (fixedColumns && fixedRight != -1)
1315 for (int j = lastres; j > startres; j--)
1317 if (!Comparison.isGap(seq.getCharAt(startres)))
1322 deleteChar(startres, new SequenceI[]
1323 { seq }, fixedRight);
1328 // could be a keyboard edit trying to delete none gaps
1330 for (int m = startres; m < lastres; m++)
1332 if (!Comparison.isGap(seq.getCharAt(m)))
1341 editCommand.appendEdit(Action.DELETE_GAP, new SequenceI[]
1342 { seq }, startres, max, av.getAlignment(), true);
1349 seqCanvas.repaint();
1352 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1354 int blankColumn = fixedColumn;
1355 for (int s = 0; s < seq.length; s++)
1357 // Find the next gap before the end of the visible region boundary
1358 // If lastCol > j, theres a boundary after the gap insertion
1360 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1362 if (Comparison.isGap(seq[s].getCharAt(blankColumn)))
1364 // Theres a space, so break and insert the gap
1369 if (blankColumn <= j)
1371 blankColumn = fixedColumn;
1377 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1378 av.getAlignment(), true);
1380 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1385 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1388 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1391 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1392 av.getAlignment(), true);
1395 // ////////////////////////////////////////
1396 // ///Everything below this is for defining the boundary of the rubberband
1397 // ////////////////////////////////////////
1398 public void doMousePressedDefineMode(MouseEvent evt)
1400 if (scrollThread != null)
1402 scrollThread.running = false;
1403 scrollThread = null;
1406 int res = findRes(evt);
1407 int seq = findSeq(evt);
1409 startWrapBlock = wrappedBlock;
1416 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1418 if (sequence == null || res > sequence.getLength())
1423 stretchGroup = av.getSelectionGroup();
1425 if (stretchGroup == null)
1427 stretchGroup = av.getAlignment().findGroup(sequence);
1428 if (stretchGroup != null && res > stretchGroup.getStartRes()
1429 && res < stretchGroup.getEndRes())
1431 av.setSelectionGroup(stretchGroup);
1435 stretchGroup = null;
1439 else if (!stretchGroup.getSequences(null).contains(sequence)
1440 || stretchGroup.getStartRes() > res
1441 || stretchGroup.getEndRes() < res)
1443 stretchGroup = null;
1445 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1447 if (allGroups != null)
1449 for (int i = 0; i < allGroups.length; i++)
1451 if (allGroups[i].getStartRes() <= res
1452 && allGroups[i].getEndRes() >= res)
1454 stretchGroup = allGroups[i];
1459 av.setSelectionGroup(stretchGroup);
1462 // DETECT RIGHT MOUSE BUTTON IN AWT
1463 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1465 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1466 sequence.findPosition(res));
1468 Vector<String> links = null;
1469 if (allFeatures != null)
1471 for (int i = 0; i < allFeatures.length; i++)
1473 if (allFeatures[i].links != null)
1477 links = new Vector<String>();
1479 for (int j = 0; j < allFeatures[i].links.size(); j++)
1481 links.addElement(allFeatures[i].links.elementAt(j));
1486 APopupMenu popup = new APopupMenu(ap, null, links);
1488 popup.show(this, evt.getX(), evt.getY());
1494 seqCanvas.cursorX = findRes(evt);
1495 seqCanvas.cursorY = findSeq(evt);
1496 seqCanvas.repaint();
1500 // Only if left mouse button do we want to change group sizes
1502 if (stretchGroup == null)
1504 // define a new group here
1505 SequenceGroup sg = new SequenceGroup();
1506 sg.setStartRes(res);
1508 sg.addSequence(sequence, false);
1509 av.setSelectionGroup(sg);
1512 if (av.getConservationSelected())
1514 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1517 if (av.getAbovePIDThreshold())
1519 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1526 public void doMouseReleasedDefineMode(MouseEvent evt)
1528 if (stretchGroup == null)
1533 stretchGroup.recalcConservation(); // always do this - annotation has own
1535 if (stretchGroup.cs != null)
1537 stretchGroup.cs.alignmentChanged(stretchGroup,
1538 av.getHiddenRepSequences());
1540 if (stretchGroup.cs.conservationApplied())
1542 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1543 stretchGroup.getName());
1547 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1548 stretchGroup.getName());
1551 changeEndRes = false;
1552 changeStartRes = false;
1553 stretchGroup = null;
1554 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1555 ap.paintAlignment(true);
1559 public void doMouseDraggedDefineMode(MouseEvent evt)
1561 int res = findRes(evt);
1562 int y = findSeq(evt);
1564 if (wrappedBlock != startWrapBlock)
1569 if (stretchGroup == null)
1574 mouseDragging = true;
1576 if (y > av.getAlignment().getHeight())
1578 y = av.getAlignment().getHeight() - 1;
1581 if (res >= av.getAlignment().getWidth())
1583 res = av.getAlignment().getWidth() - 1;
1586 if (stretchGroup.getEndRes() == res)
1588 // Edit end res position of selected group
1589 changeEndRes = true;
1591 else if (stretchGroup.getStartRes() == res)
1593 // Edit start res position of selected group
1594 changeStartRes = true;
1604 if (res > (stretchGroup.getStartRes() - 1))
1606 stretchGroup.setEndRes(res);
1609 else if (changeStartRes)
1611 if (res < (stretchGroup.getEndRes() + 1))
1613 stretchGroup.setStartRes(res);
1617 int dragDirection = 0;
1623 else if (y < oldSeq)
1628 while ((y != oldSeq) && (oldSeq > -1)
1629 && (y < av.getAlignment().getHeight()))
1631 // This routine ensures we don't skip any sequences, as the
1632 // selection is quite slow.
1633 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1635 oldSeq += dragDirection;
1642 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1644 if (stretchGroup.getSequences(null).contains(nextSeq))
1646 stretchGroup.deleteSequence(seq, false);
1652 stretchGroup.addSequence(seq, false);
1655 stretchGroup.addSequence(nextSeq, false);
1664 if (res > av.endRes || res < av.startRes || y < av.startSeq
1670 if (scrollThread != null)
1672 scrollThread.setEvent(evt);
1675 seqCanvas.repaint();
1678 public void mouseEntered(MouseEvent e)
1685 if (scrollThread != null)
1687 scrollThread.running = false;
1688 scrollThread = null;
1692 public void mouseExited(MouseEvent e)
1694 if (av.getWrapAlignment())
1699 if (mouseDragging && scrollThread == null)
1701 scrollThread = new ScrollThread();
1705 void scrollCanvas(MouseEvent evt)
1709 if (scrollThread != null)
1711 scrollThread.running = false;
1712 scrollThread = null;
1714 mouseDragging = false;
1718 if (scrollThread == null)
1720 scrollThread = new ScrollThread();
1723 mouseDragging = true;
1724 scrollThread.setEvent(evt);
1729 // this class allows scrolling off the bottom of the visible alignment
1730 class ScrollThread extends Thread
1734 boolean running = false;
1736 public ScrollThread()
1741 public void setEvent(MouseEvent e)
1746 public void stopScrolling()
1760 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1762 running = ap.scrollUp(true);
1765 if (mouseDragging && evt.getY() >= getSize().height
1766 && av.getAlignment().getHeight() > av.getEndSeq())
1768 running = ap.scrollUp(false);
1771 if (mouseDragging && evt.getX() < 0)
1773 running = ap.scrollRight(false);
1776 else if (mouseDragging && evt.getX() >= getSize().width)
1778 running = ap.scrollRight(true);
1785 } catch (Exception ex)
1793 * modify current selection according to a received message.
1795 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1796 SelectionSource source)
1798 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1799 // handles selection messages...
1800 // TODO: extend config options to allow user to control if selections may be
1801 // shared between viewports.
1803 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignmentViewport) source)
1804 .getSequenceSetId().equals(av.getSequenceSetId()))))
1810 * Check for selection in a view of which this one is a dna/protein
1813 if (selectionFromTranslation(seqsel, colsel, source))
1818 // do we want to thread this ? (contention with seqsel and colsel locks, I
1820 // rules are: colsel is copied if there is a real intersection between
1821 // sequence selection
1822 boolean repaint = false, copycolsel = true;
1823 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1825 SequenceGroup sgroup = null;
1826 if (seqsel != null && seqsel.getSize() > 0)
1828 if (av.getAlignment() == null)
1831 .println("Selection message: alignviewport av SeqSetId="
1832 + av.getSequenceSetId() + " ViewId="
1834 + " 's alignment is NULL! returning immediatly.");
1837 sgroup = seqsel.intersect(av.getAlignment(),
1838 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1839 if ((sgroup == null || sgroup.getSize() == 0)
1840 && (colsel == null || colsel.size() == 0))
1842 // don't copy columns if the region didn't intersect.
1846 if (sgroup != null && sgroup.getSize() > 0)
1848 av.setSelectionGroup(sgroup);
1852 av.setSelectionGroup(null);
1854 repaint = av.isSelectionGroupChanged(true);
1857 && (av.getColumnSelection() == null || !av
1858 .isColSelChanged(true)))
1860 // the current selection is unset or from a previous message
1861 // so import the new colsel.
1862 if (colsel == null || colsel.size() == 0)
1864 if (av.getColumnSelection() != null)
1866 av.getColumnSelection().clear();
1871 // TODO: shift colSel according to the intersecting sequences
1872 if (av.getColumnSelection() == null)
1874 av.setColumnSelection(new ColumnSelection(colsel));
1878 av.getColumnSelection().setElementsFrom(colsel);
1881 repaint |= av.isColSelChanged(true);
1884 && av.hasHiddenColumns()
1885 && (av.getColumnSelection() == null || av.getColumnSelection()
1886 .getHiddenColumns() == null))
1888 System.err.println("Bad things");
1892 ap.scalePanelHolder.repaint();
1898 * scroll to the given row/column - or nearest visible location
1903 public void scrollTo(int row, int column)
1906 row = row < 0 ? ap.av.startSeq : row;
1907 column = column < 0 ? ap.av.startRes : column;
1908 ap.scrollTo(column, column, row, true, true);
1912 * scroll to the given row - or nearest visible location
1916 public void scrollToRow(int row)
1919 row = row < 0 ? ap.av.startSeq : row;
1920 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1924 * scroll to the given column - or nearest visible location
1928 public void scrollToColumn(int column)
1931 column = column < 0 ? ap.av.startRes : column;
1932 ap.scrollTo(column, column, ap.av.startSeq, true, true);
1936 * If this panel is a cdna/protein translation view of the selection source,
1937 * tries to map the source selection to a local one, and returns true. Else
1944 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1945 ColumnSelection colsel, SelectionSource source)
1947 if (!(source instanceof AlignViewportI)) {
1950 final AlignViewportI sourceAv = (AlignViewportI) source;
1951 if (sourceAv.getCodingComplement() != av && av.getCodingComplement() != sourceAv)
1957 * Map sequence selection
1959 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1960 av.setSelectionGroup(sg);
1961 av.isSelectionGroupChanged(true);
1964 * Map column selection
1966 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1968 av.setColumnSelection(cs);
1969 av.isColSelChanged(true);
1971 ap.scalePanelHolder.repaint();