2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.appletgui;
23 import jalview.api.AlignViewportI;
24 import jalview.commands.EditCommand;
25 import jalview.commands.EditCommand.Action;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.ColumnSelection;
28 import jalview.datamodel.SearchResultMatchI;
29 import jalview.datamodel.SearchResults;
30 import jalview.datamodel.SearchResultsI;
31 import jalview.datamodel.Sequence;
32 import jalview.datamodel.SequenceFeature;
33 import jalview.datamodel.SequenceGroup;
34 import jalview.datamodel.SequenceI;
35 import jalview.schemes.ResidueProperties;
36 import jalview.structure.SelectionListener;
37 import jalview.structure.SelectionSource;
38 import jalview.structure.SequenceListener;
39 import jalview.structure.StructureSelectionManager;
40 import jalview.structure.VamsasSource;
41 import jalview.util.MappingUtils;
42 import jalview.util.MessageManager;
43 import jalview.viewmodel.AlignmentViewport;
44 import jalview.viewmodel.ViewportRanges;
46 import java.awt.BorderLayout;
48 import java.awt.FontMetrics;
49 import java.awt.Panel;
50 import java.awt.Point;
51 import java.awt.event.InputEvent;
52 import java.awt.event.MouseEvent;
53 import java.awt.event.MouseListener;
54 import java.awt.event.MouseMotionListener;
55 import java.util.Vector;
57 public class SeqPanel extends Panel 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 ViewportRanges ranges = av.getRanges();
231 while (seqCanvas.cursorY < ranges.getStartSeq())
235 while (seqCanvas.cursorY + 1 > ranges.getEndSeq())
239 while (seqCanvas.cursorX < av.getColumnSelection()
240 .adjustForHiddenColumns(ranges.getStartRes()))
243 if (!ap.scrollRight(false))
248 while (seqCanvas.cursorX > av.getColumnSelection()
249 .adjustForHiddenColumns(ranges.getEndRes()))
251 if (!ap.scrollRight(true))
257 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
258 seqCanvas.cursorX, seqCanvas.cursorY);
263 void setSelectionAreaAtCursor(boolean topLeft)
265 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
267 if (av.getSelectionGroup() != null)
269 SequenceGroup sg = av.getSelectionGroup();
270 // Find the top and bottom of this group
271 int min = av.getAlignment().getHeight(), max = 0;
272 for (int i = 0; i < sg.getSize(); i++)
274 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
289 sg.setStartRes(seqCanvas.cursorX);
290 if (sg.getEndRes() < seqCanvas.cursorX)
292 sg.setEndRes(seqCanvas.cursorX);
295 min = seqCanvas.cursorY;
299 sg.setEndRes(seqCanvas.cursorX);
300 if (sg.getStartRes() > seqCanvas.cursorX)
302 sg.setStartRes(seqCanvas.cursorX);
305 max = seqCanvas.cursorY + 1;
310 // Only the user can do this
311 av.setSelectionGroup(null);
315 // Now add any sequences between min and max
317 for (int i = min; i < max; i++)
319 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
324 if (av.getSelectionGroup() == null)
326 SequenceGroup sg = new SequenceGroup();
327 sg.setStartRes(seqCanvas.cursorX);
328 sg.setEndRes(seqCanvas.cursorX);
329 sg.addSequence(sequence, false);
330 av.setSelectionGroup(sg);
332 ap.paintAlignment(false);
336 void insertGapAtCursor(boolean group)
338 groupEditing = group;
339 startseq = seqCanvas.cursorY;
340 lastres = seqCanvas.cursorX;
341 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
345 void deleteGapAtCursor(boolean group)
347 groupEditing = group;
348 startseq = seqCanvas.cursorY;
349 lastres = seqCanvas.cursorX + getKeyboardNo1();
350 editSequence(false, seqCanvas.cursorX);
354 void numberPressed(char value)
356 if (keyboardNo1 == null)
358 keyboardNo1 = new StringBuffer();
361 if (keyboardNo2 != null)
363 keyboardNo2.append(value);
367 keyboardNo1.append(value);
375 if (keyboardNo1 != null)
377 int value = Integer.parseInt(keyboardNo1.toString());
381 } catch (Exception x)
392 if (keyboardNo2 != null)
394 int value = Integer.parseInt(keyboardNo2.toString());
398 } catch (Exception x)
406 * Set status message in alignment panel
409 * aligned sequence object
413 * index of sequence in alignment
414 * @return position of res in sequence
416 void setStatusMessage(SequenceI sequence, int res, int seq)
418 // TODO remove duplication of identical gui method
419 StringBuilder text = new StringBuilder(32);
420 String seqno = seq == -1 ? "" : " " + (seq + 1);
421 text.append("Sequence" + seqno + " ID: " + sequence.getName());
423 String residue = null;
425 * Try to translate the display character to residue name (null for gap).
427 final String displayChar = String.valueOf(sequence.getCharAt(res));
428 if (av.getAlignment().isNucleotide())
430 residue = ResidueProperties.nucleotideName.get(displayChar);
433 text.append(" Nucleotide: ").append(residue);
438 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
439 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
443 text.append(" Residue: ").append(residue);
450 pos = sequence.findPosition(res);
451 text.append(" (").append(Integer.toString(pos)).append(")");
454 ap.alignFrame.statusBar.setText(text.toString());
458 * Set the status bar message to highlight the first matched position in
462 * @return true if results were matched, false if not
464 private boolean setStatusMessage(SearchResultsI results)
466 AlignmentI al = this.av.getAlignment();
467 int sequenceIndex = al.findIndex(results);
468 if (sequenceIndex == -1)
472 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
473 for (SearchResultMatchI m : results.getResults())
475 SequenceI seq = m.getSequence();
476 if (seq.getDatasetSequence() != null)
478 seq = seq.getDatasetSequence();
484 * Convert position in sequence (base 1) to sequence character array
487 int start = m.getStart() - m.getSequence().getStart();
488 setStatusMessage(seq, start, sequenceIndex);
496 public void mousePressed(MouseEvent evt)
498 lastMousePress = evt.getPoint();
500 // For now, ignore the mouseWheel font resizing on Macs
501 // As the Button2_mask always seems to be true
502 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
505 mouseWheelPressed = true;
509 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
511 if (evt.isControlDown() || evt.isAltDown())
519 doMousePressedDefineMode(evt);
523 int seq = findSeq(evt);
524 int res = findRes(evt);
526 if (seq < 0 || res < 0)
531 if ((seq < av.getAlignment().getHeight())
532 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
547 public void mouseClicked(MouseEvent evt)
549 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
550 if (evt.getClickCount() > 1)
552 if (av.getSelectionGroup() != null
553 && av.getSelectionGroup().getSize() == 1
554 && av.getSelectionGroup().getEndRes()
555 - av.getSelectionGroup().getStartRes() < 2)
557 av.setSelectionGroup(null);
560 SequenceFeature[] features = findFeaturesAtRes(sequence,
561 sequence.findPosition(findRes(evt)));
563 if (features != null && features.length > 0)
565 SearchResultsI highlight = new SearchResults();
566 highlight.addResult(sequence, features[0].getBegin(),
567 features[0].getEnd());
568 seqCanvas.highlightSearchResults(highlight);
570 if (features != null && features.length > 0)
572 seqCanvas.getFeatureRenderer().amendFeatures(
573 new SequenceI[] { sequence }, features, false, ap, null);
575 seqCanvas.highlightSearchResults(null);
581 public void mouseReleased(MouseEvent evt)
583 mouseDragging = false;
584 mouseWheelPressed = false;
585 ap.paintAlignment(true);
589 doMouseReleasedDefineMode(evt);
597 int startWrapBlock = -1;
599 int wrappedBlock = -1;
601 int findRes(MouseEvent evt)
606 if (av.getWrapAlignment())
609 int hgap = av.getCharHeight();
610 if (av.getScaleAboveWrapped())
612 hgap += av.getCharHeight();
615 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
616 + hgap + seqCanvas.getAnnotationHeight();
620 x -= seqCanvas.LABEL_WEST;
622 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
628 wrappedBlock = y / cHeight;
629 wrappedBlock += av.getRanges().getStartRes() / cwidth;
631 res = wrappedBlock * cwidth + x / av.getCharWidth();
636 res = (x / av.getCharWidth()) + av.getRanges().getStartRes();
639 if (av.hasHiddenColumns())
641 res = av.getColumnSelection().adjustForHiddenColumns(res);
648 int findSeq(MouseEvent evt)
650 final int sqnum = findAlRow(evt);
651 return (sqnum < 0) ? 0 : sqnum;
657 * @return row in alignment that was selected (or -1 for column selection)
659 private int findAlRow(MouseEvent evt)
664 if (av.getWrapAlignment())
666 int hgap = av.getCharHeight();
667 if (av.getScaleAboveWrapped())
669 hgap += av.getCharHeight();
672 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
673 + hgap + seqCanvas.getAnnotationHeight();
677 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
686 seq = Math.min((y / av.getCharHeight())
687 + av.getRanges().getStartSeq(),
689 .getAlignment().getHeight() - 1);
699 public void doMousePressed(MouseEvent evt)
702 int seq = findSeq(evt);
703 int res = findRes(evt);
705 if (seq < av.getAlignment().getHeight()
706 && res < av.getAlignment().getSequenceAt(seq).getLength())
708 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
709 // Find the residue's position in the sequence (res is the position
727 public void mouseOverSequence(SequenceI sequence, int index, int pos)
729 String tmp = sequence.hashCode() + index + "";
730 if (lastMessage == null || !lastMessage.equals(tmp))
732 ssm.mouseOverSequence(sequence, index, pos, av);
739 public void highlightSequence(SearchResultsI results)
741 if (av.isFollowHighlight())
743 if (ap.scrollToPosition(results, true))
745 ap.alignFrame.repaint();
748 setStatusMessage(results);
749 seqCanvas.highlightSearchResults(results);
754 public VamsasSource getVamsasSource()
756 return this.ap == null ? null : this.ap.av;
760 public void updateColours(SequenceI seq, int index)
762 System.out.println("update the seqPanel colours");
767 public void mouseMoved(MouseEvent evt)
769 int res = findRes(evt);
770 int seq = findSeq(evt);
772 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
781 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
782 if (res > sequence.getLength())
791 int respos = sequence.findPosition(res);
794 mouseOverSequence(sequence, res, respos);
797 StringBuilder text = new StringBuilder();
798 text.append("Sequence ").append(Integer.toString(seq + 1))
799 .append(" ID: ").append(sequence.getName());
802 final String ch = String.valueOf(sequence.getCharAt(res));
803 if (av.getAlignment().isNucleotide())
805 obj = ResidueProperties.nucleotideName.get(ch);
808 text.append(" Nucleotide: ").append(obj);
813 obj = "X".equalsIgnoreCase(ch) ? "X" : ResidueProperties.aa2Triplet
817 text.append(" Residue: ").append(obj);
823 text.append(" (").append(Integer.toString(respos)).append(")");
826 ap.alignFrame.statusBar.setText(text.toString());
828 StringBuilder tooltipText = new StringBuilder();
829 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
832 for (int g = 0; g < groups.length; g++)
834 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
836 if (!groups[g].getName().startsWith("JTreeGroup")
837 && !groups[g].getName().startsWith("JGroup"))
839 tooltipText.append(groups[g].getName()).append(" ");
841 if (groups[g].getDescription() != null)
843 tooltipText.append(groups[g].getDescription());
845 tooltipText.append("\n");
850 // use aa to see if the mouse pointer is on a
851 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
852 sequence.findPosition(res));
855 while (index < allFeatures.length)
857 SequenceFeature sf = allFeatures[index];
859 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
861 if (sf.getDescription() != null)
863 tooltipText.append(" " + sf.getDescription());
866 if (sf.getValue("status") != null)
868 String status = sf.getValue("status").toString();
869 if (status.length() > 0)
871 tooltipText.append(" (" + sf.getValue("status") + ")");
874 tooltipText.append("\n");
881 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
885 tooltip.setTip(tooltipText.toString());
889 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
891 Vector tmp = new Vector();
892 SequenceFeature[] features = sequence.getSequenceFeatures();
893 if (features != null)
895 for (int i = 0; i < features.length; i++)
897 if (av.getFeaturesDisplayed() == null
898 || !av.getFeaturesDisplayed().isVisible(
899 features[i].getType()))
904 if (features[i].featureGroup != null
905 && !seqCanvas.fr.checkGroupVisibility(
906 features[i].featureGroup, false))
911 if ((features[i].getBegin() <= res)
912 && (features[i].getEnd() >= res))
914 tmp.addElement(features[i]);
919 features = new SequenceFeature[tmp.size()];
920 tmp.copyInto(features);
928 * set when the current UI interaction has resulted in a change that requires
929 * overview shading to be recalculated. this could be changed to something
930 * more expressive that indicates what actually has changed, so selective
931 * redraws can be applied
933 private boolean needOverviewUpdate; // TODO: refactor to avcontroller
936 public void mouseDragged(MouseEvent evt)
938 if (mouseWheelPressed)
940 int oldWidth = av.getCharWidth();
942 // Which is bigger, left-right or up-down?
943 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
946 int fontSize = av.font.getSize();
948 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
952 else if (evt.getY() > lastMousePress.y)
962 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
963 av.setCharWidth(oldWidth);
967 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
969 av.setCharWidth(av.getCharWidth() - 1);
971 else if (evt.getX() > lastMousePress.x)
973 av.setCharWidth(av.getCharWidth() + 1);
976 if (av.getCharWidth() < 1)
984 FontMetrics fm = getFontMetrics(av.getFont());
985 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
987 lastMousePress = evt.getPoint();
989 ap.paintAlignment(false);
990 ap.annotationPanel.image = null;
996 doMouseDraggedDefineMode(evt);
1000 int res = findRes(evt);
1007 if ((lastres == -1) || (lastres == res))
1012 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1014 // dragLeft, delete gap
1015 editSequence(false, res);
1019 editSequence(true, res);
1022 mouseDragging = true;
1023 if (scrollThread != null)
1025 scrollThread.setEvent(evt);
1030 synchronized void editSequence(boolean insertGap, int startres)
1033 int fixedRight = -1;
1034 boolean fixedColumns = false;
1035 SequenceGroup sg = av.getSelectionGroup();
1037 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1039 if (!groupEditing && av.hasHiddenRows())
1041 if (av.isHiddenRepSequence(seq))
1043 sg = av.getRepresentedSequences(seq);
1044 groupEditing = true;
1048 StringBuffer message = new StringBuffer();
1051 message.append(MessageManager.getString("action.edit_group")).append(
1053 if (editCommand == null)
1055 editCommand = new EditCommand(
1056 MessageManager.getString("action.edit_group"));
1061 message.append(MessageManager.getString("label.edit_sequence"))
1062 .append(" " + seq.getName());
1063 String label = seq.getName();
1064 if (label.length() > 10)
1066 label = label.substring(0, 10);
1068 if (editCommand == null)
1070 editCommand = new EditCommand(MessageManager.formatMessage(
1071 "label.edit_params", new String[] { label }));
1077 message.append(" insert ");
1081 message.append(" delete ");
1084 message.append(Math.abs(startres - lastres) + " gaps.");
1085 ap.alignFrame.statusBar.setText(message.toString());
1087 // Are we editing within a selection group?
1089 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1092 fixedColumns = true;
1094 // sg might be null as the user may only see 1 sequence,
1095 // but the sequence represents a group
1098 if (!av.isHiddenRepSequence(seq))
1104 sg = av.getRepresentedSequences(seq);
1107 fixedLeft = sg.getStartRes();
1108 fixedRight = sg.getEndRes();
1110 if ((startres < fixedLeft && lastres >= fixedLeft)
1111 || (startres >= fixedLeft && lastres < fixedLeft)
1112 || (startres > fixedRight && lastres <= fixedRight)
1113 || (startres <= fixedRight && lastres > fixedRight))
1119 if (fixedLeft > startres)
1121 fixedRight = fixedLeft - 1;
1124 else if (fixedRight < startres)
1126 fixedLeft = fixedRight;
1131 if (av.hasHiddenColumns())
1133 fixedColumns = true;
1134 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1135 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1137 if ((insertGap && startres > y1 && lastres < y1)
1138 || (!insertGap && startres < y2 && lastres > y2))
1144 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1145 // Selection spans a hidden region
1146 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1154 fixedRight = y2 - 1;
1161 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1162 .toArray(new SequenceI[0]);
1167 // If the user has selected the whole sequence, and is dragging to
1168 // the right, we can still extend the alignment and selectionGroup
1169 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1170 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1172 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1173 fixedRight = sg.getEndRes();
1176 // Is it valid with fixed columns??
1177 // Find the next gap before the end
1178 // of the visible region boundary
1179 boolean blank = false;
1180 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1184 for (SequenceI gs : groupSeqs)
1186 for (int j = 0; j < startres - lastres; j++)
1188 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1204 if (sg.getSize() == av.getAlignment().getHeight())
1206 if ((av.hasHiddenColumns() && startres < av
1207 .getColumnSelection().getHiddenBoundaryRight(startres)))
1213 int alWidth = av.getAlignment().getWidth();
1214 if (av.hasHiddenRows())
1216 int hwidth = av.getAlignment().getHiddenSequences()
1218 if (hwidth > alWidth)
1223 // We can still insert gaps if the selectionGroup
1224 // contains all the sequences
1225 sg.setEndRes(sg.getEndRes() + startres - lastres);
1226 fixedRight = alWidth + startres - lastres;
1237 else if (!insertGap)
1239 // / Are we able to delete?
1240 // ie are all columns blank?
1242 for (SequenceI gs : groupSeqs)
1244 for (int j = startres; j < lastres; j++)
1246 if (gs.getLength() <= j)
1251 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1253 // Not a gap, block edit not valid
1263 // dragging to the right
1264 if (fixedColumns && fixedRight != -1)
1266 for (int j = lastres; j < startres; j++)
1268 insertChar(j, groupSeqs, fixedRight);
1273 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1274 startres - lastres, av.getAlignment(), true);
1279 // dragging to the left
1280 if (fixedColumns && fixedRight != -1)
1282 for (int j = lastres; j > startres; j--)
1284 deleteChar(startres, groupSeqs, fixedRight);
1289 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1290 lastres - startres, av.getAlignment(), true);
1296 // ///Editing a single sequence///////////
1300 // dragging to the right
1301 if (fixedColumns && fixedRight != -1)
1303 for (int j = lastres; j < startres; j++)
1305 insertChar(j, new SequenceI[] { seq }, fixedRight);
1310 editCommand.appendEdit(Action.INSERT_GAP,
1311 new SequenceI[] { seq }, lastres, startres - lastres,
1312 av.getAlignment(), true);
1317 // dragging to the left
1318 if (fixedColumns && fixedRight != -1)
1320 for (int j = lastres; j > startres; j--)
1322 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1327 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1332 // could be a keyboard edit trying to delete none gaps
1334 for (int m = startres; m < lastres; m++)
1336 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1345 editCommand.appendEdit(Action.DELETE_GAP,
1346 new SequenceI[] { seq }, startres, max,
1347 av.getAlignment(), true);
1354 seqCanvas.repaint();
1357 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1359 int blankColumn = fixedColumn;
1360 for (int s = 0; s < seq.length; s++)
1362 // Find the next gap before the end of the visible region boundary
1363 // If lastCol > j, theres a boundary after the gap insertion
1365 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1367 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1369 // Theres a space, so break and insert the gap
1374 if (blankColumn <= j)
1376 blankColumn = fixedColumn;
1382 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1383 av.getAlignment(), true);
1385 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1390 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1393 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1396 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1397 av.getAlignment(), true);
1400 // ////////////////////////////////////////
1401 // ///Everything below this is for defining the boundary of the rubberband
1402 // ////////////////////////////////////////
1403 public void doMousePressedDefineMode(MouseEvent evt)
1405 if (scrollThread != null)
1407 scrollThread.running = false;
1408 scrollThread = null;
1411 int res = findRes(evt);
1412 int seq = findSeq(evt);
1414 startWrapBlock = wrappedBlock;
1421 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1423 if (sequence == null || res > sequence.getLength())
1428 stretchGroup = av.getSelectionGroup();
1430 if (stretchGroup == null || !stretchGroup.contains(sequence, res))
1432 stretchGroup = av.getAlignment().findGroup(sequence, res);
1433 if (stretchGroup != null)
1435 // only update the current selection if the popup menu has a group to
1437 av.setSelectionGroup(stretchGroup);
1441 // DETECT RIGHT MOUSE BUTTON IN AWT
1442 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1444 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1445 sequence.findPosition(res));
1447 Vector<String> links = null;
1448 if (allFeatures != null)
1450 for (int i = 0; i < allFeatures.length; i++)
1452 if (allFeatures[i].links != null)
1456 links = new Vector<String>();
1458 for (int j = 0; j < allFeatures[i].links.size(); j++)
1460 links.addElement(allFeatures[i].links.elementAt(j));
1465 APopupMenu popup = new APopupMenu(ap, null, links);
1467 popup.show(this, evt.getX(), evt.getY());
1473 seqCanvas.cursorX = findRes(evt);
1474 seqCanvas.cursorY = findSeq(evt);
1475 seqCanvas.repaint();
1479 // Only if left mouse button do we want to change group sizes
1481 if (stretchGroup == null)
1483 // define a new group here
1484 SequenceGroup sg = new SequenceGroup();
1485 sg.setStartRes(res);
1487 sg.addSequence(sequence, false);
1488 av.setSelectionGroup(sg);
1491 if (av.getConservationSelected())
1493 SliderPanel.setConservationSlider(ap, av.getResidueShading(),
1496 if (av.getAbovePIDThreshold())
1498 SliderPanel.setPIDSliderSource(ap, av.getResidueShading(),
1505 public void doMouseReleasedDefineMode(MouseEvent evt)
1507 if (stretchGroup == null)
1511 // always do this - annotation has own state
1512 // but defer colourscheme update until hidden sequences are passed in
1513 boolean vischange = stretchGroup.recalcConservation(true);
1514 // here we rely on stretchGroup == av.getSelection()
1515 needOverviewUpdate |= vischange && av.isSelectionDefinedGroup();
1516 if (stretchGroup.cs != null)
1518 stretchGroup.cs.alignmentChanged(stretchGroup,
1519 av.getHiddenRepSequences());
1521 if (stretchGroup.cs.conservationApplied())
1523 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1524 stretchGroup.getName());
1526 if (stretchGroup.cs.getThreshold() > 0)
1528 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1529 stretchGroup.getName());
1532 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1533 ap.paintAlignment(needOverviewUpdate);
1534 needOverviewUpdate = false;
1535 changeEndRes = false;
1536 changeStartRes = false;
1537 stretchGroup = null;
1541 public void doMouseDraggedDefineMode(MouseEvent evt)
1543 int res = findRes(evt);
1544 int y = findSeq(evt);
1546 if (wrappedBlock != startWrapBlock)
1551 if (stretchGroup == null)
1556 mouseDragging = true;
1558 if (y > av.getAlignment().getHeight())
1560 y = av.getAlignment().getHeight() - 1;
1563 if (res >= av.getAlignment().getWidth())
1565 res = av.getAlignment().getWidth() - 1;
1568 if (stretchGroup.getEndRes() == res)
1570 // Edit end res position of selected group
1571 changeEndRes = true;
1573 else if (stretchGroup.getStartRes() == res)
1575 // Edit start res position of selected group
1576 changeStartRes = true;
1586 if (res > (stretchGroup.getStartRes() - 1))
1588 stretchGroup.setEndRes(res);
1589 needOverviewUpdate |= av.isSelectionDefinedGroup();
1592 else if (changeStartRes)
1594 if (res < (stretchGroup.getEndRes() + 1))
1596 stretchGroup.setStartRes(res);
1597 needOverviewUpdate |= av.isSelectionDefinedGroup();
1601 int dragDirection = 0;
1607 else if (y < oldSeq)
1612 while ((y != oldSeq) && (oldSeq > -1)
1613 && (y < av.getAlignment().getHeight()))
1615 // This routine ensures we don't skip any sequences, as the
1616 // selection is quite slow.
1617 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1619 oldSeq += dragDirection;
1626 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1628 if (stretchGroup.getSequences(null).contains(nextSeq))
1630 stretchGroup.deleteSequence(seq, false);
1631 needOverviewUpdate |= av.isSelectionDefinedGroup();
1637 stretchGroup.addSequence(seq, false);
1640 stretchGroup.addSequence(nextSeq, false);
1641 needOverviewUpdate |= av.isSelectionDefinedGroup();
1650 if (res > av.getRanges().getEndRes()
1651 || res < av.getRanges().getStartRes()
1652 || y < av.getRanges().getStartSeq()
1653 || y > av.getRanges().getEndSeq())
1658 if (scrollThread != null)
1660 scrollThread.setEvent(evt);
1663 seqCanvas.repaint();
1667 public void mouseEntered(MouseEvent e)
1674 if (scrollThread != null)
1676 scrollThread.running = false;
1677 scrollThread = null;
1682 public void mouseExited(MouseEvent e)
1684 if (av.getWrapAlignment())
1689 if (mouseDragging && scrollThread == null)
1691 scrollThread = new ScrollThread();
1695 void scrollCanvas(MouseEvent evt)
1699 if (scrollThread != null)
1701 scrollThread.running = false;
1702 scrollThread = null;
1704 mouseDragging = false;
1708 if (scrollThread == null)
1710 scrollThread = new ScrollThread();
1713 mouseDragging = true;
1714 scrollThread.setEvent(evt);
1719 // this class allows scrolling off the bottom of the visible alignment
1720 class ScrollThread extends Thread
1724 boolean running = false;
1726 public ScrollThread()
1731 public void setEvent(MouseEvent e)
1736 public void stopScrolling()
1751 if (mouseDragging && evt.getY() < 0
1752 && av.getRanges().getStartSeq() > 0)
1754 running = ap.scrollUp(true);
1757 if (mouseDragging && evt.getY() >= getSize().height
1758 && av.getAlignment().getHeight() > av.getRanges()
1761 running = ap.scrollUp(false);
1764 if (mouseDragging && evt.getX() < 0)
1766 running = ap.scrollRight(false);
1769 else if (mouseDragging && evt.getX() >= getSize().width)
1771 running = ap.scrollRight(true);
1778 } catch (Exception ex)
1786 * modify current selection according to a received message.
1789 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1790 SelectionSource source)
1792 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1793 // handles selection messages...
1794 // TODO: extend config options to allow user to control if selections may be
1795 // shared between viewports.
1797 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignmentViewport) source)
1798 .getSequenceSetId().equals(av.getSequenceSetId()))))
1804 * Check for selection in a view of which this one is a dna/protein
1807 if (selectionFromTranslation(seqsel, colsel, source))
1812 // do we want to thread this ? (contention with seqsel and colsel locks, I
1815 * only copy colsel if there is a real intersection between
1816 * sequence selection and this panel's alignment
1818 boolean repaint = false;
1819 boolean copycolsel = false;
1820 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1822 SequenceGroup sgroup = null;
1823 if (seqsel != null && seqsel.getSize() > 0)
1825 if (av.getAlignment() == null)
1828 .println("Selection message: alignviewport av SeqSetId="
1829 + av.getSequenceSetId() + " ViewId="
1831 + " 's alignment is NULL! returning immediatly.");
1834 sgroup = seqsel.intersect(av.getAlignment(),
1835 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1836 if ((sgroup != null && sgroup.getSize() > 0))
1841 if (sgroup != null && sgroup.getSize() > 0)
1843 av.setSelectionGroup(sgroup);
1847 av.setSelectionGroup(null);
1849 repaint = av.isSelectionGroupChanged(true);
1852 && (av.getColumnSelection() == null || !av
1853 .isColSelChanged(true)))
1855 // the current selection is unset or from a previous message
1856 // so import the new colsel.
1857 if (colsel == null || colsel.isEmpty())
1859 if (av.getColumnSelection() != null)
1861 av.getColumnSelection().clear();
1866 // TODO: shift colSel according to the intersecting sequences
1867 if (av.getColumnSelection() == null)
1869 av.setColumnSelection(new ColumnSelection(colsel));
1873 av.getColumnSelection().setElementsFrom(colsel);
1876 repaint |= av.isColSelChanged(true);
1879 && av.hasHiddenColumns()
1880 && (av.getColumnSelection() == null || av.getColumnSelection()
1881 .getHiddenColumns() == null))
1883 System.err.println("Bad things");
1887 ap.scalePanelHolder.repaint();
1893 * scroll to the given row/column - or nearest visible location
1898 public void scrollTo(int row, int column)
1901 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1902 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1903 ap.scrollTo(column, column, row, true, true);
1907 * scroll to the given row - or nearest visible location
1911 public void scrollToRow(int row)
1914 row = row < 0 ? ap.av.getRanges().getStartSeq() : row;
1915 ap.scrollTo(ap.av.getRanges().getStartRes(), ap.av.getRanges()
1916 .getStartRes(), row, true, true);
1920 * scroll to the given column - or nearest visible location
1924 public void scrollToColumn(int column)
1927 column = column < 0 ? ap.av.getRanges().getStartRes() : column;
1928 ap.scrollTo(column, column, ap.av.getRanges().getStartSeq(), true, true);
1932 * If this panel is a cdna/protein translation view of the selection source,
1933 * tries to map the source selection to a local one, and returns true. Else
1940 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1941 ColumnSelection colsel, SelectionSource source)
1943 if (!(source instanceof AlignViewportI))
1947 final AlignViewportI sourceAv = (AlignViewportI) source;
1948 if (sourceAv.getCodingComplement() != av
1949 && 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);
1968 ap.scalePanelHolder.repaint();