2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
22 import java.util.List;
25 import java.awt.event.*;
29 import jalview.commands.*;
30 import jalview.datamodel.*;
31 import jalview.io.SequenceAnnotationReport;
32 import jalview.schemes.*;
33 import jalview.structure.*;
39 * @version $Revision: 1.130 $
41 public class SeqPanel extends JPanel implements MouseListener,
42 MouseMotionListener, MouseWheelListener, SequenceListener,
47 public SeqCanvas seqCanvas;
50 public AlignmentPanel ap;
52 protected int lastres;
54 protected int startseq;
56 protected AlignViewport av;
58 ScrollThread scrollThread = null;
60 boolean mouseDragging = false;
62 boolean editingSeqs = false;
64 boolean groupEditing = false;
66 // ////////////////////////////////////////
67 // ///Everything below this is for defining the boundary of the rubberband
68 // ////////////////////////////////////////
71 boolean changeEndSeq = false;
73 boolean changeStartSeq = false;
75 boolean changeEndRes = false;
77 boolean changeStartRes = false;
79 SequenceGroup stretchGroup = null;
81 boolean remove = false;
85 boolean mouseWheelPressed = false;
87 StringBuffer keyboardNo1;
89 StringBuffer keyboardNo2;
91 java.net.URL linkImageURL;
93 private final SequenceAnnotationReport seqARep;
95 StringBuffer tooltipText = new StringBuffer("<html>");
99 EditCommand editCommand;
101 StructureSelectionManager ssm;
104 * Creates a new SeqPanel object.
111 public SeqPanel(AlignViewport av, AlignmentPanel ap)
113 linkImageURL = getClass().getResource("/images/link.gif");
114 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
115 ToolTipManager.sharedInstance().registerComponent(this);
116 ToolTipManager.sharedInstance().setInitialDelay(0);
117 ToolTipManager.sharedInstance().setDismissDelay(10000);
119 setBackground(Color.white);
121 seqCanvas = new SeqCanvas(ap);
122 setLayout(new BorderLayout());
123 add(seqCanvas, BorderLayout.CENTER);
129 addMouseMotionListener(this);
130 addMouseListener(this);
131 addMouseWheelListener(this);
132 ssm = StructureSelectionManager
133 .getStructureSelectionManager(Desktop.instance);
134 ssm.addStructureViewerListener(this);
135 ssm.addSelectionListener(this);
139 int startWrapBlock = -1;
141 int wrappedBlock = -1;
143 int findRes(MouseEvent evt)
148 if (av.wrapAlignment)
151 int hgap = av.charHeight;
152 if (av.scaleAboveWrapped)
154 hgap += av.charHeight;
157 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
158 + seqCanvas.getAnnotationHeight();
162 x -= seqCanvas.LABEL_WEST;
164 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
170 wrappedBlock = y / cHeight;
171 wrappedBlock += av.getStartRes() / cwidth;
173 res = wrappedBlock * cwidth + x / av.getCharWidth();
178 if (x > seqCanvas.getWidth() + seqCanvas.getWidth())
180 // make sure we calculate relative to visible alignment, rather than
182 x = seqCanvas.getX() + seqCanvas.getWidth();
184 res = (x / av.getCharWidth()) + av.getStartRes();
187 if (av.hasHiddenColumns())
189 res = av.getColumnSelection().adjustForHiddenColumns(res);
196 int findSeq(MouseEvent evt)
201 if (av.wrapAlignment)
203 int hgap = av.charHeight;
204 if (av.scaleAboveWrapped)
206 hgap += av.charHeight;
209 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
210 + seqCanvas.getAnnotationHeight();
214 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
219 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
220 .getAlignment().getHeight() - 1);
226 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
228 Vector tmp = new Vector();
229 SequenceFeature[] features = sequence.getSequenceFeatures();
230 if (features != null)
232 for (int i = 0; i < features.length; i++)
234 if (av.featuresDisplayed == null
235 || !av.featuresDisplayed.containsKey(features[i].getType()))
240 if (features[i].featureGroup != null
241 && seqCanvas.fr.featureGroups != null
242 && seqCanvas.fr.featureGroups
243 .containsKey(features[i].featureGroup)
244 && !((Boolean) seqCanvas.fr.featureGroups
245 .get(features[i].featureGroup)).booleanValue())
248 if ((features[i].getBegin() <= res)
249 && (features[i].getEnd() >= res))
251 tmp.addElement(features[i]);
256 features = new SequenceFeature[tmp.size()];
257 tmp.copyInto(features);
264 if (editCommand != null && editCommand.getSize() > 0)
266 ap.alignFrame.addHistoryItem(editCommand);
267 av.firePropertyChange("alignment", null, av.getAlignment()
274 groupEditing = false;
282 seqCanvas.cursorY = getKeyboardNo1() - 1;
286 void setCursorColumn()
288 seqCanvas.cursorX = getKeyboardNo1() - 1;
292 void setCursorRowAndColumn()
294 if (keyboardNo2 == null)
296 keyboardNo2 = new StringBuffer();
300 seqCanvas.cursorX = getKeyboardNo1() - 1;
301 seqCanvas.cursorY = getKeyboardNo2() - 1;
306 void setCursorPosition()
308 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
310 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1() - 1);
314 void moveCursor(int dx, int dy)
316 seqCanvas.cursorX += dx;
317 seqCanvas.cursorY += dy;
318 if (av.hasHiddenColumns()
319 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
321 int original = seqCanvas.cursorX - dx;
322 int maxWidth = av.getAlignment().getWidth();
324 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
325 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
327 seqCanvas.cursorX += dx;
330 if (seqCanvas.cursorX >= maxWidth
331 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
333 seqCanvas.cursorX = original;
340 void scrollToVisible()
342 if (seqCanvas.cursorX < 0)
344 seqCanvas.cursorX = 0;
346 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
348 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
351 if (seqCanvas.cursorY < 0)
353 seqCanvas.cursorY = 0;
355 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
357 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
361 if (av.wrapAlignment)
363 ap.scrollToWrappedVisible(seqCanvas.cursorX);
367 while (seqCanvas.cursorY < av.startSeq)
371 while (seqCanvas.cursorY + 1 > av.endSeq)
375 if (!av.wrapAlignment)
377 while (seqCanvas.cursorX < av.getColumnSelection()
378 .adjustForHiddenColumns(av.startRes))
380 if (!ap.scrollRight(false))
385 while (seqCanvas.cursorX > av.getColumnSelection()
386 .adjustForHiddenColumns(av.endRes))
388 if (!ap.scrollRight(true))
395 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
396 seqCanvas.cursorX, seqCanvas.cursorY);
401 void setSelectionAreaAtCursor(boolean topLeft)
403 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
405 if (av.getSelectionGroup() != null)
407 SequenceGroup sg = av.getSelectionGroup();
408 // Find the top and bottom of this group
409 int min = av.getAlignment().getHeight(), max = 0;
410 for (int i = 0; i < sg.getSize(); i++)
412 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
427 sg.setStartRes(seqCanvas.cursorX);
428 if (sg.getEndRes() < seqCanvas.cursorX)
430 sg.setEndRes(seqCanvas.cursorX);
433 min = seqCanvas.cursorY;
437 sg.setEndRes(seqCanvas.cursorX);
438 if (sg.getStartRes() > seqCanvas.cursorX)
440 sg.setStartRes(seqCanvas.cursorX);
443 max = seqCanvas.cursorY + 1;
448 // Only the user can do this
449 av.setSelectionGroup(null);
453 // Now add any sequences between min and max
454 sg.getSequences(null).clear();
455 for (int i = min; i < max; i++)
457 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
462 if (av.getSelectionGroup() == null)
464 SequenceGroup sg = new SequenceGroup();
465 sg.setStartRes(seqCanvas.cursorX);
466 sg.setEndRes(seqCanvas.cursorX);
467 sg.addSequence(sequence, false);
468 av.setSelectionGroup(sg);
471 ap.paintAlignment(false);
475 void insertGapAtCursor(boolean group)
477 groupEditing = group;
478 startseq = seqCanvas.cursorY;
479 lastres = seqCanvas.cursorX;
480 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
484 void deleteGapAtCursor(boolean group)
486 groupEditing = group;
487 startseq = seqCanvas.cursorY;
488 lastres = seqCanvas.cursorX + getKeyboardNo1();
489 editSequence(false, false, seqCanvas.cursorX);
493 void insertNucAtCursor(boolean group, String nuc)
495 groupEditing = group;
496 startseq = seqCanvas.cursorY;
497 lastres = seqCanvas.cursorX;
498 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
502 void numberPressed(char value)
504 if (keyboardNo1 == null)
506 keyboardNo1 = new StringBuffer();
509 if (keyboardNo2 != null)
511 keyboardNo2.append(value);
515 keyboardNo1.append(value);
521 if (keyboardNo1 == null)
525 int value = Integer.parseInt(keyboardNo1.toString());
533 if (keyboardNo2 == null)
537 int value = Integer.parseInt(keyboardNo2.toString());
550 public void mouseReleased(MouseEvent evt)
552 mouseDragging = false;
553 mouseWheelPressed = false;
557 doMouseReleasedDefineMode(evt);
571 public void mousePressed(MouseEvent evt)
573 lastMousePress = evt.getPoint();
575 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
577 mouseWheelPressed = true;
581 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
583 if (evt.isAltDown() || evt.isControlDown())
591 doMousePressedDefineMode(evt);
595 int seq = findSeq(evt);
596 int res = findRes(evt);
598 if (seq < 0 || res < 0)
603 if ((seq < av.getAlignment().getHeight())
604 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
621 public void mouseOverSequence(SequenceI sequence, int index, int pos)
623 String tmp = sequence.hashCode() + " " + index + " " + pos;
625 if (lastMessage == null || !lastMessage.equals(tmp))
627 // System.err.println("mouseOver Sequence: "+tmp);
628 ssm.mouseOverSequence(sequence, index, pos, av);
634 public void highlightSequence(SearchResults results)
636 if (av.followHighlight)
638 if (ap.scrollToPosition(results, false))
640 seqCanvas.revalidate();
643 seqCanvas.highlightSearchResults(results);
647 public void updateColours(SequenceI seq, int index)
649 System.out.println("update the seqPanel colours");
660 public void mouseMoved(MouseEvent evt)
664 // This is because MacOSX creates a mouseMoved
665 // If control is down, other platforms will not.
669 int res = findRes(evt);
670 int seq = findSeq(evt);
672 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
677 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
679 if (res >= sequence.getLength())
684 pos = setStatusMessage(sequence, res, seq);
685 if (ssm != null && pos > -1)
686 mouseOverSequence(sequence, res, pos);
688 tooltipText.setLength(6); // Cuts the buffer back to <html>
690 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
693 for (int g = 0; g < groups.length; g++)
695 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
697 if (tooltipText.length() > 6)
699 tooltipText.append("<br>");
702 if (!groups[g].getName().startsWith("JTreeGroup")
703 && !groups[g].getName().startsWith("JGroup"))
705 tooltipText.append(groups[g].getName());
708 if (groups[g].getDescription() != null)
710 tooltipText.append(": " + groups[g].getDescription());
716 // use aa to see if the mouse pointer is on a
717 if (av.showSequenceFeatures)
720 SequenceFeature[] features = findFeaturesAtRes(
721 sequence.getDatasetSequence(),
722 rpos = sequence.findPosition(res));
723 seqARep.appendFeatures(tooltipText, rpos, features,
724 this.ap.seqPanel.seqCanvas.fr.minmax);
726 if (tooltipText.length() == 6) // <html></html>
728 setToolTipText(null);
733 tooltipText.append("</html>");
734 if (lastTooltip == null
735 || !lastTooltip.equals(tooltipText.toString()))
737 setToolTipText(tooltipText.toString());
738 lastTooltip = tooltipText.toString();
745 private Point lastp = null;
750 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
752 public Point getToolTipLocation(MouseEvent event)
754 int x = event.getX(), w = getWidth();
755 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
758 if (!event.isShiftDown() || p == null)
760 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
761 event.getX() + wdth, event.getY() - 20) : null;
764 * TODO: try to modify position region is not obcured by tooltip
772 * Set status message in alignment panel
775 * aligned sequence object
779 * index of sequence in alignment
780 * @return position of res in sequence
782 int setStatusMessage(SequenceI sequence, int res, int seq)
785 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
786 + sequence.getName());
789 if (av.getAlignment().isNucleotide())
791 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
795 text.append(" Nucleotide: ");
800 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
803 text.append(" Residue: ");
809 pos = sequence.findPosition(res);
812 text.append(obj + " (" + pos + ")");
815 ap.alignFrame.statusBar.setText(text.toString());
826 public void mouseDragged(MouseEvent evt)
828 if (mouseWheelPressed)
830 int oldWidth = av.charWidth;
832 // Which is bigger, left-right or up-down?
833 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
834 .getX() - lastMousePress.getX()))
836 int fontSize = av.font.getSize();
838 if (evt.getY() < lastMousePress.getY())
842 else if (evt.getY() > lastMousePress.getY())
852 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
853 av.charWidth = oldWidth;
858 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
862 else if (evt.getX() > lastMousePress.getX())
867 ap.paintAlignment(false);
870 FontMetrics fm = getFontMetrics(av.getFont());
871 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
873 lastMousePress = evt.getPoint();
880 doMouseDraggedDefineMode(evt);
884 int res = findRes(evt);
891 if ((lastres == -1) || (lastres == res))
896 if ((res < av.getAlignment().getWidth()) && (res < lastres))
898 // dragLeft, delete gap
899 editSequence(false, false, res);
903 editSequence(true, false, res);
906 mouseDragging = true;
907 if (scrollThread != null)
909 scrollThread.setEvent(evt);
913 // TODO: Make it more clever than many booleans
914 synchronized void editSequence(boolean insertGap, boolean editSeq,
919 boolean fixedColumns = false;
920 SequenceGroup sg = av.getSelectionGroup();
922 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
924 // No group, but the sequence may represent a group
925 if (!groupEditing && av.hasHiddenRows())
927 if (av.isHiddenRepSequence(seq))
929 sg = av.getRepresentedSequences(seq);
934 StringBuffer message = new StringBuffer();
937 message.append("Edit group:");
938 if (editCommand == null)
940 editCommand = new EditCommand("Edit Group");
945 message.append("Edit sequence: " + seq.getName());
946 String label = seq.getName();
947 if (label.length() > 10)
949 label = label.substring(0, 10);
951 if (editCommand == null)
953 editCommand = new EditCommand("Edit " + label);
959 message.append(" insert ");
963 message.append(" delete ");
966 message.append(Math.abs(startres - lastres) + " gaps.");
967 ap.alignFrame.statusBar.setText(message.toString());
969 // Are we editing within a selection group?
971 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
976 // sg might be null as the user may only see 1 sequence,
977 // but the sequence represents a group
980 if (!av.isHiddenRepSequence(seq))
985 sg = av.getRepresentedSequences(seq);
988 fixedLeft = sg.getStartRes();
989 fixedRight = sg.getEndRes();
991 if ((startres < fixedLeft && lastres >= fixedLeft)
992 || (startres >= fixedLeft && lastres < fixedLeft)
993 || (startres > fixedRight && lastres <= fixedRight)
994 || (startres <= fixedRight && lastres > fixedRight))
1000 if (fixedLeft > startres)
1002 fixedRight = fixedLeft - 1;
1005 else if (fixedRight < startres)
1007 fixedLeft = fixedRight;
1012 if (av.hasHiddenColumns())
1014 fixedColumns = true;
1015 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1016 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1018 if ((insertGap && startres > y1 && lastres < y1)
1019 || (!insertGap && startres < y2 && lastres > y2))
1025 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1026 // Selection spans a hidden region
1027 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1035 fixedRight = y2 - 1;
1042 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1043 int g, groupSize = vseqs.size();
1044 SequenceI[] groupSeqs = new SequenceI[groupSize];
1045 for (g = 0; g < groupSeqs.length; g++)
1047 groupSeqs[g] = vseqs.get(g);
1053 // If the user has selected the whole sequence, and is dragging to
1054 // the right, we can still extend the alignment and selectionGroup
1055 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1056 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1058 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1059 fixedRight = sg.getEndRes();
1062 // Is it valid with fixed columns??
1063 // Find the next gap before the end
1064 // of the visible region boundary
1065 boolean blank = false;
1066 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1070 for (g = 0; g < groupSize; g++)
1072 for (int j = 0; j < startres - lastres; j++)
1074 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1075 .getCharAt(fixedRight - j)))
1090 if (sg.getSize() == av.getAlignment().getHeight())
1092 if ((av.hasHiddenColumns() && startres < av
1093 .getColumnSelection().getHiddenBoundaryRight(startres)))
1099 int alWidth = av.getAlignment().getWidth();
1100 if (av.hasHiddenRows())
1102 int hwidth = av.getAlignment().getHiddenSequences()
1104 if (hwidth > alWidth)
1109 // We can still insert gaps if the selectionGroup
1110 // contains all the sequences
1111 sg.setEndRes(sg.getEndRes() + startres - lastres);
1112 fixedRight = alWidth + startres - lastres;
1123 else if (!insertGap)
1125 // / Are we able to delete?
1126 // ie are all columns blank?
1128 for (g = 0; g < groupSize; g++)
1130 for (int j = startres; j < lastres; j++)
1132 if (groupSeqs[g].getLength() <= j)
1137 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1139 // Not a gap, block edit not valid
1149 // dragging to the right
1150 if (fixedColumns && fixedRight != -1)
1152 for (int j = lastres; j < startres; j++)
1154 insertChar(j, groupSeqs, fixedRight);
1159 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1160 startres, startres - lastres, av.getAlignment(), true);
1165 // dragging to the left
1166 if (fixedColumns && fixedRight != -1)
1168 for (int j = lastres; j > startres; j--)
1170 deleteChar(startres, groupSeqs, fixedRight);
1175 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1176 startres, lastres - startres, av.getAlignment(), true);
1182 // ///Editing a single sequence///////////
1186 // dragging to the right
1187 if (fixedColumns && fixedRight != -1)
1189 for (int j = lastres; j < startres; j++)
1191 insertChar(j, new SequenceI[]
1192 { seq }, fixedRight);
1197 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1198 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1205 // dragging to the left
1206 if (fixedColumns && fixedRight != -1)
1208 for (int j = lastres; j > startres; j--)
1210 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1215 deleteChar(startres, new SequenceI[]
1216 { seq }, fixedRight);
1221 // could be a keyboard edit trying to delete none gaps
1223 for (int m = startres; m < lastres; m++)
1225 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1234 editCommand.appendEdit(EditCommand.DELETE_GAP,
1236 { seq }, startres, max, av.getAlignment(), true);
1241 {// insertGap==false AND editSeq==TRUE;
1242 if (fixedColumns && fixedRight != -1)
1244 for (int j = lastres; j < startres; j++)
1246 insertChar(j, new SequenceI[]
1247 { seq }, fixedRight);
1252 editCommand.appendEdit(EditCommand.INSERT_NUC, new SequenceI[]
1253 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1260 seqCanvas.repaint();
1263 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1265 int blankColumn = fixedColumn;
1266 for (int s = 0; s < seq.length; s++)
1268 // Find the next gap before the end of the visible region boundary
1269 // If lastCol > j, theres a boundary after the gap insertion
1271 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1273 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1275 // Theres a space, so break and insert the gap
1280 if (blankColumn <= j)
1282 blankColumn = fixedColumn;
1288 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1289 av.getAlignment(), true);
1291 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1,
1292 av.getAlignment(), true);
1296 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1299 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1,
1300 av.getAlignment(), true);
1302 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1303 av.getAlignment(), true);
1313 public void mouseEntered(MouseEvent e)
1320 if (scrollThread != null)
1322 scrollThread.running = false;
1323 scrollThread = null;
1334 public void mouseExited(MouseEvent e)
1336 if (av.getWrapAlignment())
1343 scrollThread = new ScrollThread();
1348 public void mouseClicked(MouseEvent evt)
1350 SequenceGroup sg = null;
1351 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1352 if (evt.getClickCount() > 1)
1354 sg = av.getSelectionGroup();
1355 if (sg != null && sg.getSize() == 1
1356 && sg.getEndRes() - sg.getStartRes() < 2)
1358 av.setSelectionGroup(null);
1361 SequenceFeature[] features = findFeaturesAtRes(
1362 sequence.getDatasetSequence(),
1363 sequence.findPosition(findRes(evt)));
1365 if (features != null && features.length > 0)
1367 SearchResults highlight = new SearchResults();
1368 highlight.addResult(sequence, features[0].getBegin(),
1369 features[0].getEnd());
1370 seqCanvas.highlightSearchResults(highlight);
1372 if (features != null && features.length > 0)
1374 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1375 { sequence }, features, false, ap);
1377 seqCanvas.highlightSearchResults(null);
1383 public void mouseWheelMoved(MouseWheelEvent e)
1386 if (e.getWheelRotation() > 0)
1388 if (e.isShiftDown())
1390 ap.scrollRight(true);
1400 if (e.isShiftDown())
1402 ap.scrollRight(false);
1409 // TODO Update tooltip for new position.
1418 public void doMousePressedDefineMode(MouseEvent evt)
1420 int res = findRes(evt);
1421 int seq = findSeq(evt);
1424 startWrapBlock = wrappedBlock;
1426 if (av.wrapAlignment && seq > av.getAlignment().getHeight())
1428 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1429 "Cannot edit annotations in wrapped view.",
1430 "Wrapped view - no edit", JOptionPane.WARNING_MESSAGE);
1434 if (seq < 0 || res < 0)
1439 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1441 if ((sequence == null) || (res > sequence.getLength()))
1446 stretchGroup = av.getSelectionGroup();
1448 if (stretchGroup == null)
1450 stretchGroup = av.getAlignment().findGroup(sequence);
1452 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1453 && (res < stretchGroup.getEndRes()))
1455 av.setSelectionGroup(stretchGroup);
1459 stretchGroup = null;
1462 else if (!stretchGroup.getSequences(null).contains(sequence)
1463 || (stretchGroup.getStartRes() > res)
1464 || (stretchGroup.getEndRes() < res))
1466 stretchGroup = null;
1468 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1470 if (allGroups != null)
1472 for (int i = 0; i < allGroups.length; i++)
1474 if ((allGroups[i].getStartRes() <= res)
1475 && (allGroups[i].getEndRes() >= res))
1477 stretchGroup = allGroups[i];
1483 av.setSelectionGroup(stretchGroup);
1487 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1489 SequenceFeature[] allFeatures = findFeaturesAtRes(
1490 sequence.getDatasetSequence(), sequence.findPosition(res));
1491 Vector links = new Vector();
1492 for (int i = 0; i < allFeatures.length; i++)
1494 if (allFeatures[i].links != null)
1496 for (int j = 0; j < allFeatures[i].links.size(); j++)
1498 links.addElement(allFeatures[i].links.elementAt(j));
1503 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1504 pop.show(this, evt.getX(), evt.getY());
1510 seqCanvas.cursorX = findRes(evt);
1511 seqCanvas.cursorY = findSeq(evt);
1512 seqCanvas.repaint();
1516 if (stretchGroup == null)
1518 // Only if left mouse button do we want to change group sizes
1520 // define a new group here
1521 SequenceGroup sg = new SequenceGroup();
1522 sg.setStartRes(res);
1524 sg.addSequence(sequence, false);
1525 av.setSelectionGroup(sg);
1529 if (av.getConservationSelected())
1531 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1535 if (av.getAbovePIDThreshold())
1537 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1540 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1542 // Edit end res position of selected group
1543 changeEndRes = true;
1545 else if ((stretchGroup != null)
1546 && (stretchGroup.getStartRes() == res))
1548 // Edit end res position of selected group
1549 changeStartRes = true;
1551 stretchGroup.getWidth();
1554 seqCanvas.repaint();
1563 public void doMouseReleasedDefineMode(MouseEvent evt)
1565 if (stretchGroup == null)
1570 stretchGroup.recalcConservation(); // always do this - annotation has own
1572 if (stretchGroup.cs != null)
1574 stretchGroup.cs.alignmentChanged(stretchGroup,
1575 av.getHiddenRepSequences());
1577 if (stretchGroup.cs.conservationApplied())
1579 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1580 stretchGroup.getName());
1584 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1585 stretchGroup.getName());
1588 PaintRefresher.Refresh(this, av.getSequenceSetId());
1589 ap.paintAlignment(true);
1591 changeEndRes = false;
1592 changeStartRes = false;
1593 stretchGroup = null;
1603 public void doMouseDraggedDefineMode(MouseEvent evt)
1605 int res = findRes(evt);
1606 int y = findSeq(evt);
1608 if (wrappedBlock != startWrapBlock)
1613 if (stretchGroup == null)
1618 if (res >= av.getAlignment().getWidth())
1620 res = av.getAlignment().getWidth() - 1;
1623 if (stretchGroup.getEndRes() == res)
1625 // Edit end res position of selected group
1626 changeEndRes = true;
1628 else if (stretchGroup.getStartRes() == res)
1630 // Edit start res position of selected group
1631 changeStartRes = true;
1634 if (res < av.getStartRes())
1636 res = av.getStartRes();
1641 if (res > (stretchGroup.getStartRes() - 1))
1643 stretchGroup.setEndRes(res);
1646 else if (changeStartRes)
1648 if (res < (stretchGroup.getEndRes() + 1))
1650 stretchGroup.setStartRes(res);
1654 int dragDirection = 0;
1660 else if (y < oldSeq)
1665 while ((y != oldSeq) && (oldSeq > -1)
1666 && (y < av.getAlignment().getHeight()))
1668 // This routine ensures we don't skip any sequences, as the
1669 // selection is quite slow.
1670 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1672 oldSeq += dragDirection;
1679 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1681 if (stretchGroup.getSequences(null).contains(nextSeq))
1683 stretchGroup.deleteSequence(seq, false);
1689 stretchGroup.addSequence(seq, false);
1692 stretchGroup.addSequence(nextSeq, false);
1701 mouseDragging = true;
1703 if (scrollThread != null)
1705 scrollThread.setEvent(evt);
1708 seqCanvas.repaint();
1711 void scrollCanvas(MouseEvent evt)
1715 if (scrollThread != null)
1717 scrollThread.running = false;
1718 scrollThread = null;
1720 mouseDragging = false;
1724 if (scrollThread == null)
1726 scrollThread = new ScrollThread();
1729 mouseDragging = true;
1730 scrollThread.setEvent(evt);
1735 // this class allows scrolling off the bottom of the visible alignment
1736 class ScrollThread extends Thread
1740 boolean running = false;
1742 public ScrollThread()
1747 public void setEvent(MouseEvent e)
1752 public void stopScrolling()
1766 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1768 running = ap.scrollUp(true);
1771 if (mouseDragging && (evt.getY() >= getHeight())
1772 && (av.getAlignment().getHeight() > av.getEndSeq()))
1774 running = ap.scrollUp(false);
1777 if (mouseDragging && (evt.getX() < 0))
1779 running = ap.scrollRight(false);
1781 else if (mouseDragging && (evt.getX() >= getWidth()))
1783 running = ap.scrollRight(true);
1790 } catch (Exception ex)
1798 * modify current selection according to a received message.
1801 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1802 SelectionSource source)
1804 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1805 // handles selection messages...
1806 // TODO: extend config options to allow user to control if selections may be
1807 // shared between viewports.
1809 || !av.followSelection
1810 || (av.isSelectionGroupChanged(false) || av
1811 .isColSelChanged(false))
1812 || (source instanceof AlignViewport && ((AlignViewport) source)
1813 .getSequenceSetId().equals(av.getSequenceSetId())))
1817 // do we want to thread this ? (contention with seqsel and colsel locks, I
1819 // rules are: colsel is copied if there is a real intersection between
1820 // sequence selection
1821 boolean repaint = false, copycolsel = true;
1822 // if (!av.isSelectionGroupChanged(false))
1824 SequenceGroup sgroup = null;
1825 if (seqsel != null && seqsel.getSize() > 0)
1827 if (av.getAlignment() == null)
1829 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1830 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
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)
1837 || (colsel == null || colsel.size() == 0))
1839 // don't copy columns if the region didn't intersect.
1843 if (sgroup != null && sgroup.getSize() > 0)
1845 av.setSelectionGroup(sgroup);
1849 av.setSelectionGroup(null);
1851 av.isSelectionGroupChanged(true);
1856 // the current selection is unset or from a previous message
1857 // so import the new colsel.
1858 if (colsel == null || colsel.size() == 0)
1860 if (av.getColumnSelection() != null)
1862 av.getColumnSelection().clear();
1868 // TODO: shift colSel according to the intersecting sequences
1869 if (av.getColumnSelection() == null)
1871 av.setColumnSelection(new ColumnSelection(colsel));
1875 av.getColumnSelection().setElementsFrom(colsel);
1878 av.isColSelChanged(true);
1882 && av.hasHiddenColumns()
1883 && (av.getColumnSelection() == null || av.getColumnSelection()
1884 .getHiddenColumns() == null))
1886 System.err.println("Bad things");
1890 // probably finessing with multiple redraws here
1891 PaintRefresher.Refresh(this, av.getSequenceSetId());
1892 // ap.paintAlignment(false);