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);
522 if (keyboardNo1 != null)
524 int value = Integer.parseInt(keyboardNo1.toString());
528 } catch (Exception x)
537 if (keyboardNo2!=null){
538 int value = Integer.parseInt(keyboardNo2.toString());
542 } catch (Exception x)
555 public void mouseReleased(MouseEvent evt)
557 mouseDragging = false;
558 mouseWheelPressed = false;
562 doMouseReleasedDefineMode(evt);
576 public void mousePressed(MouseEvent evt)
578 lastMousePress = evt.getPoint();
580 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
582 mouseWheelPressed = true;
586 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
588 if (evt.isAltDown() || evt.isControlDown())
596 doMousePressedDefineMode(evt);
600 int seq = findSeq(evt);
601 int res = findRes(evt);
603 if (seq < 0 || res < 0)
608 if ((seq < av.getAlignment().getHeight())
609 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
626 public void mouseOverSequence(SequenceI sequence, int index, int pos)
628 String tmp = sequence.hashCode() + " " + index + " " + pos;
630 if (lastMessage == null || !lastMessage.equals(tmp))
632 // System.err.println("mouseOver Sequence: "+tmp);
633 ssm.mouseOverSequence(sequence, index, pos, av);
639 public void highlightSequence(SearchResults results)
641 if (av.followHighlight)
643 if (ap.scrollToPosition(results, false))
645 seqCanvas.revalidate();
648 seqCanvas.highlightSearchResults(results);
652 public void updateColours(SequenceI seq, int index)
654 System.out.println("update the seqPanel colours");
665 public void mouseMoved(MouseEvent evt)
669 // This is because MacOSX creates a mouseMoved
670 // If control is down, other platforms will not.
674 int res = findRes(evt);
675 int seq = findSeq(evt);
677 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
682 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
684 if (res >= sequence.getLength())
689 pos = setStatusMessage(sequence, res, seq);
690 if (ssm != null && pos > -1)
691 mouseOverSequence(sequence, res, pos);
693 tooltipText.setLength(6); // Cuts the buffer back to <html>
695 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
698 for (int g = 0; g < groups.length; g++)
700 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
702 if (tooltipText.length() > 6)
704 tooltipText.append("<br>");
707 if (!groups[g].getName().startsWith("JTreeGroup")
708 && !groups[g].getName().startsWith("JGroup"))
710 tooltipText.append(groups[g].getName());
713 if (groups[g].getDescription() != null)
715 tooltipText.append(": " + groups[g].getDescription());
721 // use aa to see if the mouse pointer is on a
722 if (av.showSequenceFeatures)
725 SequenceFeature[] features = findFeaturesAtRes(
726 sequence.getDatasetSequence(),
727 rpos = sequence.findPosition(res));
728 seqARep.appendFeatures(tooltipText, rpos, features,
729 this.ap.seqPanel.seqCanvas.fr.minmax);
731 if (tooltipText.length() == 6) // <html></html>
733 setToolTipText(null);
738 tooltipText.append("</html>");
739 if (lastTooltip == null
740 || !lastTooltip.equals(tooltipText.toString()))
742 setToolTipText(tooltipText.toString());
743 lastTooltip = tooltipText.toString();
750 private Point lastp = null;
755 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
757 public Point getToolTipLocation(MouseEvent event)
759 int x = event.getX(), w = getWidth();
760 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
763 if (!event.isShiftDown() || p == null)
765 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
766 event.getX() + wdth, event.getY() - 20) : null;
769 * TODO: try to modify position region is not obcured by tooltip
777 * Set status message in alignment panel
780 * aligned sequence object
784 * index of sequence in alignment
785 * @return position of res in sequence
787 int setStatusMessage(SequenceI sequence, int res, int seq)
790 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
791 + sequence.getName());
794 if (av.getAlignment().isNucleotide())
796 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
800 text.append(" Nucleotide: ");
805 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
808 text.append(" Residue: ");
814 pos = sequence.findPosition(res);
817 text.append(obj + " (" + pos + ")");
820 ap.alignFrame.statusBar.setText(text.toString());
831 public void mouseDragged(MouseEvent evt)
833 if (mouseWheelPressed)
835 int oldWidth = av.charWidth;
837 // Which is bigger, left-right or up-down?
838 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
839 .getX() - lastMousePress.getX()))
841 int fontSize = av.font.getSize();
843 if (evt.getY() < lastMousePress.getY())
847 else if (evt.getY() > lastMousePress.getY())
857 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
858 av.charWidth = oldWidth;
863 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
867 else if (evt.getX() > lastMousePress.getX())
872 ap.paintAlignment(false);
875 FontMetrics fm = getFontMetrics(av.getFont());
876 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
878 lastMousePress = evt.getPoint();
885 doMouseDraggedDefineMode(evt);
889 int res = findRes(evt);
896 if ((lastres == -1) || (lastres == res))
901 if ((res < av.getAlignment().getWidth()) && (res < lastres))
903 // dragLeft, delete gap
904 editSequence(false, false, res);
908 editSequence(true, false, res);
911 mouseDragging = true;
912 if (scrollThread != null)
914 scrollThread.setEvent(evt);
918 // TODO: Make it more clever than many booleans
919 synchronized void editSequence(boolean insertGap, boolean editSeq,
924 boolean fixedColumns = false;
925 SequenceGroup sg = av.getSelectionGroup();
927 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
929 // No group, but the sequence may represent a group
930 if (!groupEditing && av.hasHiddenRows())
932 if (av.isHiddenRepSequence(seq))
934 sg = av.getRepresentedSequences(seq);
939 StringBuffer message = new StringBuffer();
942 message.append("Edit group:");
943 if (editCommand == null)
945 editCommand = new EditCommand("Edit Group");
950 message.append("Edit sequence: " + seq.getName());
951 String label = seq.getName();
952 if (label.length() > 10)
954 label = label.substring(0, 10);
956 if (editCommand == null)
958 editCommand = new EditCommand("Edit " + label);
964 message.append(" insert ");
968 message.append(" delete ");
971 message.append(Math.abs(startres - lastres) + " gaps.");
972 ap.alignFrame.statusBar.setText(message.toString());
974 // Are we editing within a selection group?
976 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
981 // sg might be null as the user may only see 1 sequence,
982 // but the sequence represents a group
985 if (!av.isHiddenRepSequence(seq))
990 sg = av.getRepresentedSequences(seq);
993 fixedLeft = sg.getStartRes();
994 fixedRight = sg.getEndRes();
996 if ((startres < fixedLeft && lastres >= fixedLeft)
997 || (startres >= fixedLeft && lastres < fixedLeft)
998 || (startres > fixedRight && lastres <= fixedRight)
999 || (startres <= fixedRight && lastres > fixedRight))
1005 if (fixedLeft > startres)
1007 fixedRight = fixedLeft - 1;
1010 else if (fixedRight < startres)
1012 fixedLeft = fixedRight;
1017 if (av.hasHiddenColumns())
1019 fixedColumns = true;
1020 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1021 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1023 if ((insertGap && startres > y1 && lastres < y1)
1024 || (!insertGap && startres < y2 && lastres > y2))
1030 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1031 // Selection spans a hidden region
1032 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1040 fixedRight = y2 - 1;
1047 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1048 int g, groupSize = vseqs.size();
1049 SequenceI[] groupSeqs = new SequenceI[groupSize];
1050 for (g = 0; g < groupSeqs.length; g++)
1052 groupSeqs[g] = vseqs.get(g);
1058 // If the user has selected the whole sequence, and is dragging to
1059 // the right, we can still extend the alignment and selectionGroup
1060 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1061 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1063 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1064 fixedRight = sg.getEndRes();
1067 // Is it valid with fixed columns??
1068 // Find the next gap before the end
1069 // of the visible region boundary
1070 boolean blank = false;
1071 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1075 for (g = 0; g < groupSize; g++)
1077 for (int j = 0; j < startres - lastres; j++)
1079 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1080 .getCharAt(fixedRight - j)))
1095 if (sg.getSize() == av.getAlignment().getHeight())
1097 if ((av.hasHiddenColumns() && startres < av
1098 .getColumnSelection().getHiddenBoundaryRight(startres)))
1104 int alWidth = av.getAlignment().getWidth();
1105 if (av.hasHiddenRows())
1107 int hwidth = av.getAlignment().getHiddenSequences()
1109 if (hwidth > alWidth)
1114 // We can still insert gaps if the selectionGroup
1115 // contains all the sequences
1116 sg.setEndRes(sg.getEndRes() + startres - lastres);
1117 fixedRight = alWidth + startres - lastres;
1128 else if (!insertGap)
1130 // / Are we able to delete?
1131 // ie are all columns blank?
1133 for (g = 0; g < groupSize; g++)
1135 for (int j = startres; j < lastres; j++)
1137 if (groupSeqs[g].getLength() <= j)
1142 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1144 // Not a gap, block edit not valid
1154 // dragging to the right
1155 if (fixedColumns && fixedRight != -1)
1157 for (int j = lastres; j < startres; j++)
1159 insertChar(j, groupSeqs, fixedRight);
1164 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1165 startres, startres - lastres, av.getAlignment(), true);
1170 // dragging to the left
1171 if (fixedColumns && fixedRight != -1)
1173 for (int j = lastres; j > startres; j--)
1175 deleteChar(startres, groupSeqs, fixedRight);
1180 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1181 startres, lastres - startres, av.getAlignment(), true);
1187 // ///Editing a single sequence///////////
1191 // dragging to the right
1192 if (fixedColumns && fixedRight != -1)
1194 for (int j = lastres; j < startres; j++)
1196 insertChar(j, new SequenceI[]
1197 { seq }, fixedRight);
1202 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1203 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1210 // dragging to the left
1211 if (fixedColumns && fixedRight != -1)
1213 for (int j = lastres; j > startres; j--)
1215 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1220 deleteChar(startres, new SequenceI[]
1221 { seq }, fixedRight);
1226 // could be a keyboard edit trying to delete none gaps
1228 for (int m = startres; m < lastres; m++)
1230 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1239 editCommand.appendEdit(EditCommand.DELETE_GAP,
1241 { seq }, startres, max, av.getAlignment(), true);
1246 {// insertGap==false AND editSeq==TRUE;
1247 if (fixedColumns && fixedRight != -1)
1249 for (int j = lastres; j < startres; j++)
1251 insertChar(j, new SequenceI[]
1252 { seq }, fixedRight);
1257 editCommand.appendEdit(EditCommand.INSERT_NUC, new SequenceI[]
1258 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1265 seqCanvas.repaint();
1268 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1270 int blankColumn = fixedColumn;
1271 for (int s = 0; s < seq.length; s++)
1273 // Find the next gap before the end of the visible region boundary
1274 // If lastCol > j, theres a boundary after the gap insertion
1276 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1278 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1280 // Theres a space, so break and insert the gap
1285 if (blankColumn <= j)
1287 blankColumn = fixedColumn;
1293 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1294 av.getAlignment(), true);
1296 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1,
1297 av.getAlignment(), true);
1301 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1304 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1,
1305 av.getAlignment(), true);
1307 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1308 av.getAlignment(), true);
1318 public void mouseEntered(MouseEvent e)
1325 if (scrollThread != null)
1327 scrollThread.running = false;
1328 scrollThread = null;
1339 public void mouseExited(MouseEvent e)
1341 if (av.getWrapAlignment())
1348 scrollThread = new ScrollThread();
1353 public void mouseClicked(MouseEvent evt)
1355 SequenceGroup sg = null;
1356 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1357 if (evt.getClickCount() > 1)
1359 sg = av.getSelectionGroup();
1360 if (sg != null && sg.getSize() == 1
1361 && sg.getEndRes() - sg.getStartRes() < 2)
1363 av.setSelectionGroup(null);
1366 SequenceFeature[] features = findFeaturesAtRes(
1367 sequence.getDatasetSequence(),
1368 sequence.findPosition(findRes(evt)));
1370 if (features != null && features.length > 0)
1372 SearchResults highlight = new SearchResults();
1373 highlight.addResult(sequence, features[0].getBegin(),
1374 features[0].getEnd());
1375 seqCanvas.highlightSearchResults(highlight);
1377 if (features != null && features.length > 0)
1379 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1380 { sequence }, features, false, ap);
1382 seqCanvas.highlightSearchResults(null);
1388 public void mouseWheelMoved(MouseWheelEvent e)
1391 if (e.getWheelRotation() > 0)
1393 if (e.isShiftDown())
1395 ap.scrollRight(true);
1405 if (e.isShiftDown())
1407 ap.scrollRight(false);
1414 // TODO Update tooltip for new position.
1423 public void doMousePressedDefineMode(MouseEvent evt)
1425 int res = findRes(evt);
1426 int seq = findSeq(evt);
1429 startWrapBlock = wrappedBlock;
1431 if (av.wrapAlignment && seq > av.getAlignment().getHeight())
1433 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1434 "Cannot edit annotations in wrapped view.",
1435 "Wrapped view - no edit", JOptionPane.WARNING_MESSAGE);
1439 if (seq < 0 || res < 0)
1444 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1446 if ((sequence == null) || (res > sequence.getLength()))
1451 stretchGroup = av.getSelectionGroup();
1453 if (stretchGroup == null)
1455 stretchGroup = av.getAlignment().findGroup(sequence);
1457 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1458 && (res < stretchGroup.getEndRes()))
1460 av.setSelectionGroup(stretchGroup);
1464 stretchGroup = null;
1467 else if (!stretchGroup.getSequences(null).contains(sequence)
1468 || (stretchGroup.getStartRes() > res)
1469 || (stretchGroup.getEndRes() < res))
1471 stretchGroup = null;
1473 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1475 if (allGroups != null)
1477 for (int i = 0; i < allGroups.length; i++)
1479 if ((allGroups[i].getStartRes() <= res)
1480 && (allGroups[i].getEndRes() >= res))
1482 stretchGroup = allGroups[i];
1488 av.setSelectionGroup(stretchGroup);
1492 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1494 SequenceFeature[] allFeatures = findFeaturesAtRes(
1495 sequence.getDatasetSequence(), sequence.findPosition(res));
1496 Vector links = new Vector();
1497 for (int i = 0; i < allFeatures.length; i++)
1499 if (allFeatures[i].links != null)
1501 for (int j = 0; j < allFeatures[i].links.size(); j++)
1503 links.addElement(allFeatures[i].links.elementAt(j));
1508 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1509 pop.show(this, evt.getX(), evt.getY());
1515 seqCanvas.cursorX = findRes(evt);
1516 seqCanvas.cursorY = findSeq(evt);
1517 seqCanvas.repaint();
1521 if (stretchGroup == null)
1523 // Only if left mouse button do we want to change group sizes
1525 // define a new group here
1526 SequenceGroup sg = new SequenceGroup();
1527 sg.setStartRes(res);
1529 sg.addSequence(sequence, false);
1530 av.setSelectionGroup(sg);
1534 if (av.getConservationSelected())
1536 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1540 if (av.getAbovePIDThreshold())
1542 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1545 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1547 // Edit end res position of selected group
1548 changeEndRes = true;
1550 else if ((stretchGroup != null)
1551 && (stretchGroup.getStartRes() == res))
1553 // Edit end res position of selected group
1554 changeStartRes = true;
1556 stretchGroup.getWidth();
1559 seqCanvas.repaint();
1568 public void doMouseReleasedDefineMode(MouseEvent evt)
1570 if (stretchGroup == null)
1575 stretchGroup.recalcConservation(); // always do this - annotation has own
1577 if (stretchGroup.cs != null)
1579 stretchGroup.cs.alignmentChanged(stretchGroup,
1580 av.getHiddenRepSequences());
1582 if (stretchGroup.cs.conservationApplied())
1584 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1585 stretchGroup.getName());
1589 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1590 stretchGroup.getName());
1593 PaintRefresher.Refresh(this, av.getSequenceSetId());
1594 ap.paintAlignment(true);
1596 changeEndRes = false;
1597 changeStartRes = false;
1598 stretchGroup = null;
1608 public void doMouseDraggedDefineMode(MouseEvent evt)
1610 int res = findRes(evt);
1611 int y = findSeq(evt);
1613 if (wrappedBlock != startWrapBlock)
1618 if (stretchGroup == null)
1623 if (res >= av.getAlignment().getWidth())
1625 res = av.getAlignment().getWidth() - 1;
1628 if (stretchGroup.getEndRes() == res)
1630 // Edit end res position of selected group
1631 changeEndRes = true;
1633 else if (stretchGroup.getStartRes() == res)
1635 // Edit start res position of selected group
1636 changeStartRes = true;
1639 if (res < av.getStartRes())
1641 res = av.getStartRes();
1646 if (res > (stretchGroup.getStartRes() - 1))
1648 stretchGroup.setEndRes(res);
1651 else if (changeStartRes)
1653 if (res < (stretchGroup.getEndRes() + 1))
1655 stretchGroup.setStartRes(res);
1659 int dragDirection = 0;
1665 else if (y < oldSeq)
1670 while ((y != oldSeq) && (oldSeq > -1)
1671 && (y < av.getAlignment().getHeight()))
1673 // This routine ensures we don't skip any sequences, as the
1674 // selection is quite slow.
1675 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1677 oldSeq += dragDirection;
1684 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1686 if (stretchGroup.getSequences(null).contains(nextSeq))
1688 stretchGroup.deleteSequence(seq, false);
1694 stretchGroup.addSequence(seq, false);
1697 stretchGroup.addSequence(nextSeq, false);
1706 mouseDragging = true;
1708 if (scrollThread != null)
1710 scrollThread.setEvent(evt);
1713 seqCanvas.repaint();
1716 void scrollCanvas(MouseEvent evt)
1720 if (scrollThread != null)
1722 scrollThread.running = false;
1723 scrollThread = null;
1725 mouseDragging = false;
1729 if (scrollThread == null)
1731 scrollThread = new ScrollThread();
1734 mouseDragging = true;
1735 scrollThread.setEvent(evt);
1740 // this class allows scrolling off the bottom of the visible alignment
1741 class ScrollThread extends Thread
1745 boolean running = false;
1747 public ScrollThread()
1752 public void setEvent(MouseEvent e)
1757 public void stopScrolling()
1771 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1773 running = ap.scrollUp(true);
1776 if (mouseDragging && (evt.getY() >= getHeight())
1777 && (av.getAlignment().getHeight() > av.getEndSeq()))
1779 running = ap.scrollUp(false);
1782 if (mouseDragging && (evt.getX() < 0))
1784 running = ap.scrollRight(false);
1786 else if (mouseDragging && (evt.getX() >= getWidth()))
1788 running = ap.scrollRight(true);
1795 } catch (Exception ex)
1803 * modify current selection according to a received message.
1806 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1807 SelectionSource source)
1809 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1810 // handles selection messages...
1811 // TODO: extend config options to allow user to control if selections may be
1812 // shared between viewports.
1814 || !av.followSelection
1815 || (av.isSelectionGroupChanged(false) || av
1816 .isColSelChanged(false))
1817 || (source instanceof AlignViewport && ((AlignViewport) source)
1818 .getSequenceSetId().equals(av.getSequenceSetId())))
1822 // do we want to thread this ? (contention with seqsel and colsel locks, I
1824 // rules are: colsel is copied if there is a real intersection between
1825 // sequence selection
1826 boolean repaint = false, copycolsel = true;
1827 // if (!av.isSelectionGroupChanged(false))
1829 SequenceGroup sgroup = null;
1830 if (seqsel != null && seqsel.getSize() > 0)
1832 if (av.getAlignment() == null)
1834 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1835 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1836 + " 's alignment is NULL! returning immediatly.");
1839 sgroup = seqsel.intersect(av.getAlignment(),
1840 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1841 if ((sgroup == null || sgroup.getSize() == 0)
1842 || (colsel == null || colsel.size() == 0))
1844 // don't copy columns if the region didn't intersect.
1848 if (sgroup != null && sgroup.getSize() > 0)
1850 av.setSelectionGroup(sgroup);
1854 av.setSelectionGroup(null);
1856 av.isSelectionGroupChanged(true);
1861 // the current selection is unset or from a previous message
1862 // so import the new colsel.
1863 if (colsel == null || colsel.size() == 0)
1865 if (av.getColumnSelection() != null)
1867 av.getColumnSelection().clear();
1873 // TODO: shift colSel according to the intersecting sequences
1874 if (av.getColumnSelection() == null)
1876 av.setColumnSelection(new ColumnSelection(colsel));
1880 av.getColumnSelection().setElementsFrom(colsel);
1883 av.isColSelChanged(true);
1887 && av.hasHiddenColumns()
1888 && (av.getColumnSelection() == null || av.getColumnSelection()
1889 .getHiddenColumns() == null))
1891 System.err.println("Bad things");
1895 // probably finessing with multiple redraws here
1896 PaintRefresher.Refresh(this, av.getSequenceSetId());
1897 // ap.paintAlignment(false);