2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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/>.
23 import java.awt.event.*;
27 import jalview.commands.*;
28 import jalview.datamodel.*;
29 import jalview.schemes.*;
30 import jalview.structure.*;
38 public class SeqPanel extends JPanel implements MouseListener,
39 MouseMotionListener, MouseWheelListener, SequenceListener,
44 public SeqCanvas seqCanvas;
47 public AlignmentPanel ap;
49 protected int lastres;
51 protected int startseq;
53 protected AlignViewport av;
55 ScrollThread scrollThread = null;
57 boolean mouseDragging = false;
59 boolean editingSeqs = false;
61 boolean groupEditing = false;
63 // ////////////////////////////////////////
64 // ///Everything below this is for defining the boundary of the rubberband
65 // ////////////////////////////////////////
68 boolean changeEndSeq = false;
70 boolean changeStartSeq = false;
72 boolean changeEndRes = false;
74 boolean changeStartRes = false;
76 SequenceGroup stretchGroup = null;
78 boolean remove = false;
82 boolean mouseWheelPressed = false;
84 StringBuffer keyboardNo1;
86 StringBuffer keyboardNo2;
88 java.net.URL linkImageURL;
90 StringBuffer tooltipText = new StringBuffer("<html>");
94 EditCommand editCommand;
96 StructureSelectionManager ssm;
99 * Creates a new SeqPanel object.
106 public SeqPanel(AlignViewport av, AlignmentPanel ap)
108 linkImageURL = getClass().getResource("/images/link.gif");
109 ToolTipManager.sharedInstance().registerComponent(this);
110 ToolTipManager.sharedInstance().setInitialDelay(0);
111 ToolTipManager.sharedInstance().setDismissDelay(10000);
113 setBackground(Color.white);
115 seqCanvas = new SeqCanvas(ap);
116 setLayout(new BorderLayout());
117 add(seqCanvas, BorderLayout.CENTER);
123 addMouseMotionListener(this);
124 addMouseListener(this);
125 addMouseWheelListener(this);
126 ssm = StructureSelectionManager.getStructureSelectionManager();
127 ssm.addStructureViewerListener(this);
128 ssm.addSelectionListener(this);
132 int startWrapBlock = -1;
134 int wrappedBlock = -1;
136 int findRes(MouseEvent evt)
141 if (av.wrapAlignment)
144 int hgap = av.charHeight;
145 if (av.scaleAboveWrapped)
147 hgap += av.charHeight;
150 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
151 + seqCanvas.getAnnotationHeight();
155 x -= seqCanvas.LABEL_WEST;
157 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
163 wrappedBlock = y / cHeight;
164 wrappedBlock += av.getStartRes() / cwidth;
166 res = wrappedBlock * cwidth + x / av.getCharWidth();
171 res = (x / av.getCharWidth()) + av.getStartRes();
174 if (av.hasHiddenColumns)
176 res = av.getColumnSelection().adjustForHiddenColumns(res);
183 int findSeq(MouseEvent evt)
188 if (av.wrapAlignment)
190 int hgap = av.charHeight;
191 if (av.scaleAboveWrapped)
193 hgap += av.charHeight;
196 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
197 + seqCanvas.getAnnotationHeight();
201 seq = Math.min((y % cHeight) / av.getCharHeight(), av.alignment
206 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(),
207 av.alignment.getHeight() - 1);
213 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
215 Vector tmp = new Vector();
216 SequenceFeature[] features = sequence.getSequenceFeatures();
217 if (features != null)
219 for (int i = 0; i < features.length; i++)
221 if (av.featuresDisplayed == null
222 || !av.featuresDisplayed.containsKey(features[i].getType()))
227 if (features[i].featureGroup != null
228 && seqCanvas.fr.featureGroups != null
229 && seqCanvas.fr.featureGroups
230 .containsKey(features[i].featureGroup)
231 && !((Boolean) seqCanvas.fr.featureGroups
232 .get(features[i].featureGroup)).booleanValue())
235 if ((features[i].getBegin() <= res)
236 && (features[i].getEnd() >= res))
238 tmp.addElement(features[i]);
243 features = new SequenceFeature[tmp.size()];
244 tmp.copyInto(features);
251 if (editCommand != null && editCommand.getSize() > 0)
253 ap.alignFrame.addHistoryItem(editCommand);
254 av.firePropertyChange("alignment", null, av.getAlignment()
261 groupEditing = false;
269 seqCanvas.cursorY = getKeyboardNo1() - 1;
273 void setCursorColumn()
275 seqCanvas.cursorX = getKeyboardNo1() - 1;
279 void setCursorRowAndColumn()
281 if (keyboardNo2 == null)
283 keyboardNo2 = new StringBuffer();
287 seqCanvas.cursorX = getKeyboardNo1() - 1;
288 seqCanvas.cursorY = getKeyboardNo2() - 1;
293 void setCursorPosition()
295 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
298 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1() - 1);
302 void moveCursor(int dx, int dy)
304 seqCanvas.cursorX += dx;
305 seqCanvas.cursorY += dy;
306 if (av.hasHiddenColumns && !av.colSel.isVisible(seqCanvas.cursorX))
308 int original = seqCanvas.cursorX - dx;
309 int maxWidth = av.alignment.getWidth();
311 while (!av.colSel.isVisible(seqCanvas.cursorX)
312 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
314 seqCanvas.cursorX += dx;
317 if (seqCanvas.cursorX >= maxWidth
318 || !av.colSel.isVisible(seqCanvas.cursorX))
320 seqCanvas.cursorX = original;
327 void scrollToVisible()
329 if (seqCanvas.cursorX < 0)
331 seqCanvas.cursorX = 0;
333 else if (seqCanvas.cursorX > av.alignment.getWidth() - 1)
335 seqCanvas.cursorX = av.alignment.getWidth() - 1;
338 if (seqCanvas.cursorY < 0)
340 seqCanvas.cursorY = 0;
342 else if (seqCanvas.cursorY > av.alignment.getHeight() - 1)
344 seqCanvas.cursorY = av.alignment.getHeight() - 1;
348 if (av.wrapAlignment)
350 ap.scrollToWrappedVisible(seqCanvas.cursorX);
354 while (seqCanvas.cursorY < av.startSeq)
358 while (seqCanvas.cursorY + 1 > av.endSeq)
362 if (!av.wrapAlignment)
364 while (seqCanvas.cursorX < av.colSel
365 .adjustForHiddenColumns(av.startRes))
367 if (!ap.scrollRight(false))
372 while (seqCanvas.cursorX > av.colSel
373 .adjustForHiddenColumns(av.endRes))
375 if (!ap.scrollRight(true))
382 setStatusMessage(av.alignment.getSequenceAt(seqCanvas.cursorY),
383 seqCanvas.cursorX, seqCanvas.cursorY);
388 void setSelectionAreaAtCursor(boolean topLeft)
390 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
393 if (av.getSelectionGroup() != null)
395 SequenceGroup sg = av.selectionGroup;
396 // Find the top and bottom of this group
397 int min = av.alignment.getHeight(), max = 0;
398 for (int i = 0; i < sg.getSize(); i++)
400 int index = av.alignment.findIndex(sg.getSequenceAt(i));
415 sg.setStartRes(seqCanvas.cursorX);
416 if (sg.getEndRes() < seqCanvas.cursorX)
418 sg.setEndRes(seqCanvas.cursorX);
421 min = seqCanvas.cursorY;
425 sg.setEndRes(seqCanvas.cursorX);
426 if (sg.getStartRes() > seqCanvas.cursorX)
428 sg.setStartRes(seqCanvas.cursorX);
431 max = seqCanvas.cursorY + 1;
436 // Only the user can do this
437 av.setSelectionGroup(null);
441 // Now add any sequences between min and max
442 sg.getSequences(null).clear();
443 for (int i = min; i < max; i++)
445 sg.addSequence(av.alignment.getSequenceAt(i), false);
450 if (av.getSelectionGroup() == null)
452 SequenceGroup sg = new SequenceGroup();
453 sg.setStartRes(seqCanvas.cursorX);
454 sg.setEndRes(seqCanvas.cursorX);
455 sg.addSequence(sequence, false);
456 av.setSelectionGroup(sg);
459 ap.paintAlignment(false);
463 void insertGapAtCursor(boolean group)
465 groupEditing = group;
466 startseq = seqCanvas.cursorY;
467 lastres = seqCanvas.cursorX;
468 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
472 void deleteGapAtCursor(boolean group)
474 groupEditing = group;
475 startseq = seqCanvas.cursorY;
476 lastres = seqCanvas.cursorX + getKeyboardNo1();
477 editSequence(false, seqCanvas.cursorX);
481 void numberPressed(char value)
483 if (keyboardNo1 == null)
485 keyboardNo1 = new StringBuffer();
488 if (keyboardNo2 != null)
490 keyboardNo2.append(value);
494 keyboardNo1.append(value);
500 if (keyboardNo1 == null)
504 int value = Integer.parseInt(keyboardNo1.toString());
512 if (keyboardNo2 == null)
516 int value = Integer.parseInt(keyboardNo2.toString());
528 public void mouseReleased(MouseEvent evt)
530 mouseDragging = false;
531 mouseWheelPressed = false;
535 doMouseReleasedDefineMode(evt);
548 public void mousePressed(MouseEvent evt)
550 lastMousePress = evt.getPoint();
552 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
554 mouseWheelPressed = true;
558 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
560 if (evt.isAltDown() || evt.isControlDown())
568 doMousePressedDefineMode(evt);
572 int seq = findSeq(evt);
573 int res = findRes(evt);
575 if (seq < 0 || res < 0)
580 if ((seq < av.getAlignment().getHeight())
581 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
597 public void mouseOverSequence(SequenceI sequence, int index, int pos)
599 String tmp = sequence.hashCode() + " " + index + " " + pos;
601 if (lastMessage == null || !lastMessage.equals(tmp))
603 // System.err.println("mouseOver Sequence: "+tmp);
604 ssm.mouseOverSequence(sequence, index, pos);
609 public void highlightSequence(SearchResults results)
611 if (av.followHighlight)
613 ap.scrollToPosition(results, false);
615 seqCanvas.highlightSearchResults(results);
618 public void updateColours(SequenceI seq, int index)
620 System.out.println("update the seqPanel colours");
630 public void mouseMoved(MouseEvent evt)
634 // This is because MacOSX creates a mouseMoved
635 // If control is down, other platforms will not.
639 int res = findRes(evt);
640 int seq = findSeq(evt);
642 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
647 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
649 if (res >= sequence.getLength())
654 pos = setStatusMessage(sequence, res, seq);
655 if (ssm != null && pos > -1)
656 mouseOverSequence(sequence, res, pos);
658 tooltipText.setLength(6); // Cuts the buffer back to <html>
660 SequenceGroup[] groups = av.alignment.findAllGroups(sequence);
663 for (int g = 0; g < groups.length; g++)
665 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
667 if (tooltipText.length() > 6)
669 tooltipText.append("<br>");
672 if (!groups[g].getName().startsWith("JTreeGroup")
673 && !groups[g].getName().startsWith("JGroup"))
675 tooltipText.append(groups[g].getName());
678 if (groups[g].getDescription() != null)
680 tooltipText.append(": " + groups[g].getDescription());
686 // use aa to see if the mouse pointer is on a
687 if (av.showSequenceFeatures)
690 SequenceFeature[] features = findFeaturesAtRes(sequence
691 .getDatasetSequence(), rpos = sequence.findPosition(res));
692 appendFeatures(tooltipText, linkImageURL.toString(), rpos, features,
693 this.ap.seqPanel.seqCanvas.fr.minmax);
695 if (tooltipText.length() == 6) // <html></html>
697 setToolTipText(null);
702 tooltipText.append("</html>");
703 if (lastTooltip == null
704 || !lastTooltip.equals(tooltipText.toString()))
706 setToolTipText(tooltipText.toString());
707 lastTooltip = tooltipText.toString();
714 private Point lastp = null;
719 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
721 public Point getToolTipLocation(MouseEvent event)
723 int x = event.getX(), w = getWidth();
724 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
727 if (!event.isShiftDown() || p == null)
729 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
730 event.getX() + wdth, event.getY() - 20) : null;
733 * TODO: try to modify position region is not obcured by tooltip
739 * appends the features at rpos to the given stringbuffer ready for display in
742 * @param tooltipText2
743 * @param linkImageURL
746 * TODO refactor to Jalview 'utilities' somehow.
748 public void appendFeatures(StringBuffer tooltipText2,
749 String linkImageURL, int rpos, SequenceFeature[] features)
751 appendFeatures(tooltipText2, linkImageURL, rpos, features, null);
754 public void appendFeatures(StringBuffer tooltipText2, String string,
755 int rpos, SequenceFeature[] features, Hashtable minmax)
758 if (features != null)
760 for (int i = 0; i < features.length; i++)
762 if (features[i].getType().equals("disulfide bond"))
764 if (features[i].getBegin() == rpos
765 || features[i].getEnd() == rpos)
767 if (tooltipText2.length() > 6)
769 tooltipText2.append("<br>");
771 tooltipText2.append("disulfide bond " + features[i].getBegin()
772 + ":" + features[i].getEnd());
773 if (features[i].links != null)
775 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
781 if (tooltipText2.length() > 6)
783 tooltipText2.append("<br>");
785 // TODO: remove this hack to display link only features
786 boolean linkOnly = features[i].getValue("linkonly") != null;
789 tooltipText2.append(features[i].getType() + " ");
792 // we are marking a positional feature
793 tooltipText2.append(features[i].begin);
795 if (features[i].begin != features[i].end)
797 tooltipText2.append(" " + features[i].end);
800 if (features[i].getDescription() != null
801 && !features[i].description.equals(features[i]
804 tmpString = features[i].getDescription();
805 int startTag = tmpString.toUpperCase().indexOf("<HTML>");
808 tmpString = tmpString.substring(startTag + 6);
810 int endTag = tmpString.toUpperCase().indexOf("</BODY>");
813 tmpString = tmpString.substring(0, endTag);
815 endTag = tmpString.toUpperCase().indexOf("</HTML>");
818 tmpString = tmpString.substring(0, endTag);
823 tooltipText2.append("; " + tmpString);
827 if (tmpString.indexOf("<") > -1
828 || tmpString.indexOf(">") > -1)
830 // The description does not specify html is to
831 // be used, so we must remove < > symbols
832 tmpString = tmpString.replaceAll("<", "<");
833 tmpString = tmpString.replaceAll(">", ">");
835 tooltipText2.append("; ");
836 tooltipText2.append(tmpString);
841 tooltipText2.append("; " + tmpString);
845 // check score should be shown
846 if (features[i].getScore() != Float.NaN)
848 float[][] rng = (minmax == null) ? null : ((float[][]) minmax
849 .get(features[i].getType()));
850 if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
852 tooltipText2.append(" Score=" + features[i].getScore());
855 if (features[i].getValue("status") != null)
857 String status = features[i].getValue("status").toString();
858 if (status.length() > 0)
860 tooltipText2.append("; (" + features[i].getValue("status")
865 if (features[i].links != null)
867 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
878 * Set status message in alignment panel
881 * aligned sequence object
885 * index of sequence in alignment
886 * @return position of res in sequence
888 int setStatusMessage(SequenceI sequence, int res, int seq)
891 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
892 + sequence.getName());
895 if (av.alignment.isNucleotide())
897 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
901 text.append(" Nucleotide: ");
906 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
909 text.append(" Residue: ");
915 pos = sequence.findPosition(res);
918 text.append(obj + " (" + pos + ")");
921 ap.alignFrame.statusBar.setText(text.toString());
931 public void mouseDragged(MouseEvent evt)
933 if (mouseWheelPressed)
935 int oldWidth = av.charWidth;
937 // Which is bigger, left-right or up-down?
938 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
940 - lastMousePress.getX()))
942 int fontSize = av.font.getSize();
944 if (evt.getY() < lastMousePress.getY())
948 else if (evt.getY() > lastMousePress.getY())
959 .setFont(new Font(av.font.getName(), av.font.getStyle(),
961 av.charWidth = oldWidth;
966 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
970 else if (evt.getX() > lastMousePress.getX())
975 ap.paintAlignment(false);
978 FontMetrics fm = getFontMetrics(av.getFont());
979 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
981 lastMousePress = evt.getPoint();
988 doMouseDraggedDefineMode(evt);
992 int res = findRes(evt);
999 if ((lastres == -1) || (lastres == res))
1004 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1006 // dragLeft, delete gap
1007 editSequence(false, res);
1011 editSequence(true, res);
1014 mouseDragging = true;
1015 if (scrollThread != null)
1017 scrollThread.setEvent(evt);
1021 synchronized void editSequence(boolean insertGap, int startres)
1024 int fixedRight = -1;
1025 boolean fixedColumns = false;
1026 SequenceGroup sg = av.getSelectionGroup();
1028 SequenceI seq = av.alignment.getSequenceAt(startseq);
1030 // No group, but the sequence may represent a group
1031 if (!groupEditing && av.hasHiddenRows)
1033 if (av.hiddenRepSequences != null
1034 && av.hiddenRepSequences.containsKey(seq))
1036 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1037 groupEditing = true;
1041 StringBuffer message = new StringBuffer();
1044 message.append("Edit group:");
1045 if (editCommand == null)
1047 editCommand = new EditCommand("Edit Group");
1052 message.append("Edit sequence: " + seq.getName());
1053 String label = seq.getName();
1054 if (label.length() > 10)
1056 label = label.substring(0, 10);
1058 if (editCommand == null)
1060 editCommand = new EditCommand("Edit " + label);
1066 message.append(" insert ");
1070 message.append(" delete ");
1073 message.append(Math.abs(startres - lastres) + " gaps.");
1074 ap.alignFrame.statusBar.setText(message.toString());
1076 // Are we editing within a selection group?
1078 || (sg != null && sg.getSequences(av.hiddenRepSequences)
1081 fixedColumns = true;
1083 // sg might be null as the user may only see 1 sequence,
1084 // but the sequence represents a group
1087 if (av.hiddenRepSequences == null
1088 || !av.hiddenRepSequences.containsKey(seq))
1093 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1096 fixedLeft = sg.getStartRes();
1097 fixedRight = sg.getEndRes();
1099 if ((startres < fixedLeft && lastres >= fixedLeft)
1100 || (startres >= fixedLeft && lastres < fixedLeft)
1101 || (startres > fixedRight && lastres <= fixedRight)
1102 || (startres <= fixedRight && lastres > fixedRight))
1108 if (fixedLeft > startres)
1110 fixedRight = fixedLeft - 1;
1113 else if (fixedRight < startres)
1115 fixedLeft = fixedRight;
1120 if (av.hasHiddenColumns)
1122 fixedColumns = true;
1123 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1124 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1126 if ((insertGap && startres > y1 && lastres < y1)
1127 || (!insertGap && startres < y2 && lastres > y2))
1133 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1134 // Selection spans a hidden region
1135 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1143 fixedRight = y2 - 1;
1150 Vector vseqs = sg.getSequences(av.hiddenRepSequences);
1151 int g, groupSize = vseqs.size();
1152 SequenceI[] groupSeqs = new SequenceI[groupSize];
1153 for (g = 0; g < groupSeqs.length; g++)
1155 groupSeqs[g] = (SequenceI) vseqs.elementAt(g);
1161 // If the user has selected the whole sequence, and is dragging to
1162 // the right, we can still extend the alignment and selectionGroup
1163 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1164 && sg.getEndRes() == av.alignment.getWidth() - 1)
1166 sg.setEndRes(av.alignment.getWidth() + startres - lastres);
1167 fixedRight = sg.getEndRes();
1170 // Is it valid with fixed columns??
1171 // Find the next gap before the end
1172 // of the visible region boundary
1173 boolean blank = false;
1174 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1178 for (g = 0; g < groupSize; g++)
1180 for (int j = 0; j < startres - lastres; j++)
1182 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1183 .getCharAt(fixedRight - j)))
1198 if (sg.getSize() == av.alignment.getHeight())
1200 if ((av.hasHiddenColumns && startres < av.getColumnSelection()
1201 .getHiddenBoundaryRight(startres)))
1207 int alWidth = av.alignment.getWidth();
1208 if (av.hasHiddenRows)
1210 int hwidth = av.alignment.getHiddenSequences().getWidth();
1211 if (hwidth > alWidth)
1216 // We can still insert gaps if the selectionGroup
1217 // contains all the sequences
1218 sg.setEndRes(sg.getEndRes() + startres - lastres);
1219 fixedRight = alWidth + startres - lastres;
1230 else if (!insertGap)
1232 // / Are we able to delete?
1233 // ie are all columns blank?
1235 for (g = 0; g < groupSize; g++)
1237 for (int j = startres; j < lastres; j++)
1239 if (groupSeqs[g].getLength() <= j)
1244 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1246 // Not a gap, block edit not valid
1256 // dragging to the right
1257 if (fixedColumns && fixedRight != -1)
1259 for (int j = lastres; j < startres; j++)
1261 insertChar(j, groupSeqs, fixedRight);
1266 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1267 startres, startres - lastres, av.alignment, true);
1272 // dragging to the left
1273 if (fixedColumns && fixedRight != -1)
1275 for (int j = lastres; j > startres; j--)
1277 deleteChar(startres, groupSeqs, fixedRight);
1282 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1283 startres, lastres - startres, av.alignment, true);
1289 // ///Editing a single sequence///////////
1293 // dragging to the right
1294 if (fixedColumns && fixedRight != -1)
1296 for (int j = lastres; j < startres; j++)
1298 insertChar(j, new SequenceI[]
1299 { seq }, fixedRight);
1304 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1305 { seq }, lastres, startres - lastres, av.alignment, true);
1310 // dragging to the left
1311 if (fixedColumns && fixedRight != -1)
1313 for (int j = lastres; j > startres; j--)
1315 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1320 deleteChar(startres, new SequenceI[]
1321 { seq }, fixedRight);
1326 // could be a keyboard edit trying to delete none gaps
1328 for (int m = startres; m < lastres; m++)
1330 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1339 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1340 { seq }, startres, max, av.alignment, true);
1347 seqCanvas.repaint();
1350 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1352 int blankColumn = fixedColumn;
1353 for (int s = 0; s < seq.length; s++)
1355 // Find the next gap before the end of the visible region boundary
1356 // If lastCol > j, theres a boundary after the gap insertion
1358 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1360 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1362 // Theres a space, so break and insert the gap
1367 if (blankColumn <= j)
1369 blankColumn = fixedColumn;
1375 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1376 av.alignment, true);
1378 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1, av.alignment,
1383 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1386 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1, av.alignment,
1389 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1390 av.alignment, true);
1399 public void mouseEntered(MouseEvent e)
1406 if (scrollThread != null)
1408 scrollThread.running = false;
1409 scrollThread = null;
1419 public void mouseExited(MouseEvent e)
1421 if (av.getWrapAlignment())
1428 scrollThread = new ScrollThread();
1432 public void mouseClicked(MouseEvent evt)
1434 SequenceGroup sg = null;
1435 SequenceI sequence = av.alignment.getSequenceAt(findSeq(evt));
1436 if (evt.getClickCount() > 1)
1438 sg = av.getSelectionGroup();
1439 if (sg != null && sg.getSize() == 1
1440 && sg.getEndRes() - sg.getStartRes() < 2)
1442 av.setSelectionGroup(null);
1445 SequenceFeature[] features = findFeaturesAtRes(sequence
1446 .getDatasetSequence(), sequence.findPosition(findRes(evt)));
1448 if (features != null && features.length > 0)
1450 SearchResults highlight = new SearchResults();
1451 highlight.addResult(sequence, features[0].getBegin(), features[0]
1453 seqCanvas.highlightSearchResults(highlight);
1455 if (features != null && features.length > 0)
1457 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1458 { sequence }, features, false, ap);
1460 seqCanvas.highlightSearchResults(null);
1465 public void mouseWheelMoved(MouseWheelEvent e)
1468 if (e.getWheelRotation() > 0)
1476 // TODO Update tooltip for new position.
1485 public void doMousePressedDefineMode(MouseEvent evt)
1487 int res = findRes(evt);
1488 int seq = findSeq(evt);
1491 startWrapBlock = wrappedBlock;
1493 if (av.wrapAlignment && seq > av.alignment.getHeight())
1495 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1496 "Cannot edit annotations in wrapped view.",
1497 "Wrapped view - no edit", JOptionPane.WARNING_MESSAGE);
1501 if (seq < 0 || res < 0)
1506 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1508 if ((sequence == null) || (res > sequence.getLength()))
1513 stretchGroup = av.getSelectionGroup();
1515 if (stretchGroup == null)
1517 stretchGroup = av.alignment.findGroup(sequence);
1519 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1520 && (res < stretchGroup.getEndRes()))
1522 av.setSelectionGroup(stretchGroup);
1526 stretchGroup = null;
1529 else if (!stretchGroup.getSequences(null).contains(sequence)
1530 || (stretchGroup.getStartRes() > res)
1531 || (stretchGroup.getEndRes() < res))
1533 stretchGroup = null;
1535 SequenceGroup[] allGroups = av.alignment.findAllGroups(sequence);
1537 if (allGroups != null)
1539 for (int i = 0; i < allGroups.length; i++)
1541 if ((allGroups[i].getStartRes() <= res)
1542 && (allGroups[i].getEndRes() >= res))
1544 stretchGroup = allGroups[i];
1550 av.setSelectionGroup(stretchGroup);
1554 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1556 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence
1557 .getDatasetSequence(), sequence.findPosition(res));
1558 Vector links = new Vector();
1559 for (int i = 0; i < allFeatures.length; i++)
1561 if (allFeatures[i].links != null)
1563 for (int j = 0; j < allFeatures[i].links.size(); j++)
1565 links.addElement(allFeatures[i].links.elementAt(j));
1570 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1571 pop.show(this, evt.getX(), evt.getY());
1577 seqCanvas.cursorX = findRes(evt);
1578 seqCanvas.cursorY = findSeq(evt);
1579 seqCanvas.repaint();
1583 if (stretchGroup == null)
1585 // Only if left mouse button do we want to change group sizes
1587 // define a new group here
1588 SequenceGroup sg = new SequenceGroup();
1589 sg.setStartRes(res);
1591 sg.addSequence(sequence, false);
1592 av.setSelectionGroup(sg);
1596 if (av.getConservationSelected())
1598 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1602 if (av.getAbovePIDThreshold())
1604 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1607 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1609 // Edit end res position of selected group
1610 changeEndRes = true;
1612 else if ((stretchGroup != null)
1613 && (stretchGroup.getStartRes() == res))
1615 // Edit end res position of selected group
1616 changeStartRes = true;
1618 stretchGroup.getWidth();
1621 seqCanvas.repaint();
1630 public void doMouseReleasedDefineMode(MouseEvent evt)
1632 if (stretchGroup == null)
1637 stretchGroup.recalcConservation(); // always do this - annotation has own
1639 if (stretchGroup.cs != null)
1641 if (stretchGroup.cs instanceof ClustalxColourScheme)
1643 ((ClustalxColourScheme) stretchGroup.cs).resetClustalX(stretchGroup
1644 .getSequences(av.hiddenRepSequences), stretchGroup
1648 if (stretchGroup.cs.conservationApplied())
1650 SliderPanel.setConservationSlider(ap, stretchGroup.cs, stretchGroup
1655 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs, stretchGroup
1659 PaintRefresher.Refresh(this, av.getSequenceSetId());
1660 ap.paintAlignment(true);
1662 changeEndRes = false;
1663 changeStartRes = false;
1664 stretchGroup = null;
1674 public void doMouseDraggedDefineMode(MouseEvent evt)
1676 int res = findRes(evt);
1677 int y = findSeq(evt);
1679 if (wrappedBlock != startWrapBlock)
1684 if (stretchGroup == null)
1689 if (res >= av.alignment.getWidth())
1691 res = av.alignment.getWidth() - 1;
1694 if (stretchGroup.getEndRes() == res)
1696 // Edit end res position of selected group
1697 changeEndRes = true;
1699 else if (stretchGroup.getStartRes() == res)
1701 // Edit start res position of selected group
1702 changeStartRes = true;
1705 if (res < av.getStartRes())
1707 res = av.getStartRes();
1712 if (res > (stretchGroup.getStartRes() - 1))
1714 stretchGroup.setEndRes(res);
1717 else if (changeStartRes)
1719 if (res < (stretchGroup.getEndRes() + 1))
1721 stretchGroup.setStartRes(res);
1725 int dragDirection = 0;
1731 else if (y < oldSeq)
1736 while ((y != oldSeq) && (oldSeq > -1) && (y < av.alignment.getHeight()))
1738 // This routine ensures we don't skip any sequences, as the
1739 // selection is quite slow.
1740 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1742 oldSeq += dragDirection;
1749 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1751 if (stretchGroup.getSequences(null).contains(nextSeq))
1753 stretchGroup.deleteSequence(seq, false);
1759 stretchGroup.addSequence(seq, false);
1762 stretchGroup.addSequence(nextSeq, false);
1771 mouseDragging = true;
1773 if (scrollThread != null)
1775 scrollThread.setEvent(evt);
1778 seqCanvas.repaint();
1781 void scrollCanvas(MouseEvent evt)
1785 if (scrollThread != null)
1787 scrollThread.running = false;
1788 scrollThread = null;
1790 mouseDragging = false;
1794 if (scrollThread == null)
1796 scrollThread = new ScrollThread();
1799 mouseDragging = true;
1800 scrollThread.setEvent(evt);
1805 // this class allows scrolling off the bottom of the visible alignment
1806 class ScrollThread extends Thread
1810 boolean running = false;
1812 public ScrollThread()
1817 public void setEvent(MouseEvent e)
1822 public void stopScrolling()
1835 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1837 running = ap.scrollUp(true);
1840 if (mouseDragging && (evt.getY() >= getHeight())
1841 && (av.alignment.getHeight() > av.getEndSeq()))
1843 running = ap.scrollUp(false);
1846 if (mouseDragging && (evt.getX() < 0))
1848 running = ap.scrollRight(false);
1850 else if (mouseDragging && (evt.getX() >= getWidth()))
1852 running = ap.scrollRight(true);
1859 } catch (Exception ex)
1867 * modify current selection according to a received message.
1869 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1870 SelectionSource source)
1872 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1873 // handles selection messages...
1874 // TODO: extend config options to allow user to control if selections may be
1875 // shared between viewports.
1877 || !av.followSelection
1878 || (source instanceof AlignViewport && ((AlignViewport) source)
1879 .getSequenceSetId().equals(av.getSequenceSetId())))
1883 // do we want to thread this ? (contention with seqsel and colsel locks, I
1885 boolean repaint = false;
1886 if (av.selectionGroup == null || !av.isSelectionGroupChanged())
1888 SequenceGroup sgroup = null;
1891 if (av.alignment == null)
1893 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1894 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1895 + " 's alignment is NULL! returning immediatly.");
1898 sgroup = seqsel.intersect(av.alignment,
1899 (av.hasHiddenRows) ? av.hiddenRepSequences : null);
1901 if (sgroup != null && sgroup.getSize() > 0)
1903 av.setSelectionGroup(sgroup);
1907 av.setSelectionGroup(null);
1909 repaint = av.isSelectionGroupChanged();
1911 if (av.colSel == null || !av.isColSelChanged())
1913 // the current selection is unset or from a previous message
1914 // so import the new colsel.
1915 if (colsel == null || colsel.size() == 0)
1917 if (av.colSel != null)
1924 if (av.colSel == null)
1926 av.colSel = new ColumnSelection(colsel);
1930 av.colSel.setElementsFrom(colsel);
1933 repaint |= av.isColSelChanged();
1935 if (av.hasHiddenColumns
1936 && (av.colSel == null || av.colSel.getHiddenColumns() == null))
1938 System.err.println("Bad things");
1942 // probably finessing with multiple redraws here
1943 PaintRefresher.Refresh(this, av.getSequenceSetId());
1944 // ap.paintAlignment(false);