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);
614 seqCanvas.revalidate();
616 seqCanvas.highlightSearchResults(results);
619 public void updateColours(SequenceI seq, int index)
621 System.out.println("update the seqPanel colours");
631 public void mouseMoved(MouseEvent evt)
635 // This is because MacOSX creates a mouseMoved
636 // If control is down, other platforms will not.
640 int res = findRes(evt);
641 int seq = findSeq(evt);
643 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
648 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
650 if (res >= sequence.getLength())
655 pos = setStatusMessage(sequence, res, seq);
656 if (ssm != null && pos > -1)
657 mouseOverSequence(sequence, res, pos);
659 tooltipText.setLength(6); // Cuts the buffer back to <html>
661 SequenceGroup[] groups = av.alignment.findAllGroups(sequence);
664 for (int g = 0; g < groups.length; g++)
666 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
668 if (tooltipText.length() > 6)
670 tooltipText.append("<br>");
673 if (!groups[g].getName().startsWith("JTreeGroup")
674 && !groups[g].getName().startsWith("JGroup"))
676 tooltipText.append(groups[g].getName());
679 if (groups[g].getDescription() != null)
681 tooltipText.append(": " + groups[g].getDescription());
687 // use aa to see if the mouse pointer is on a
688 if (av.showSequenceFeatures)
691 SequenceFeature[] features = findFeaturesAtRes(sequence
692 .getDatasetSequence(), rpos = sequence.findPosition(res));
693 appendFeatures(tooltipText, linkImageURL.toString(), rpos, features,
694 this.ap.seqPanel.seqCanvas.fr.minmax);
696 if (tooltipText.length() == 6) // <html></html>
698 setToolTipText(null);
703 tooltipText.append("</html>");
704 if (lastTooltip == null
705 || !lastTooltip.equals(tooltipText.toString()))
707 setToolTipText(tooltipText.toString());
708 lastTooltip = tooltipText.toString();
715 private Point lastp = null;
720 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
722 public Point getToolTipLocation(MouseEvent event)
724 int x = event.getX(), w = getWidth();
725 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
728 if (!event.isShiftDown() || p == null)
730 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
731 event.getX() + wdth, event.getY() - 20) : null;
734 * TODO: try to modify position region is not obcured by tooltip
740 * appends the features at rpos to the given stringbuffer ready for display in
743 * @param tooltipText2
744 * @param linkImageURL
747 * TODO refactor to Jalview 'utilities' somehow.
749 public void appendFeatures(StringBuffer tooltipText2,
750 String linkImageURL, int rpos, SequenceFeature[] features)
752 appendFeatures(tooltipText2, linkImageURL, rpos, features, null);
755 public void appendFeatures(StringBuffer tooltipText2, String string,
756 int rpos, SequenceFeature[] features, Hashtable minmax)
759 if (features != null)
761 for (int i = 0; i < features.length; i++)
763 if (features[i].getType().equals("disulfide bond"))
765 if (features[i].getBegin() == rpos
766 || features[i].getEnd() == rpos)
768 if (tooltipText2.length() > 6)
770 tooltipText2.append("<br>");
772 tooltipText2.append("disulfide bond " + features[i].getBegin()
773 + ":" + features[i].getEnd());
774 if (features[i].links != null)
776 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
782 if (tooltipText2.length() > 6)
784 tooltipText2.append("<br>");
786 // TODO: remove this hack to display link only features
787 boolean linkOnly = features[i].getValue("linkonly") != null;
790 tooltipText2.append(features[i].getType() + " ");
793 // we are marking a positional feature
794 tooltipText2.append(features[i].begin);
796 if (features[i].begin != features[i].end)
798 tooltipText2.append(" " + features[i].end);
801 if (features[i].getDescription() != null
802 && !features[i].description.equals(features[i]
805 tmpString = features[i].getDescription();
806 int startTag = tmpString.toUpperCase().indexOf("<HTML>");
809 tmpString = tmpString.substring(startTag + 6);
811 int endTag = tmpString.toUpperCase().indexOf("</BODY>");
814 tmpString = tmpString.substring(0, endTag);
816 endTag = tmpString.toUpperCase().indexOf("</HTML>");
819 tmpString = tmpString.substring(0, endTag);
824 tooltipText2.append("; " + tmpString);
828 if (tmpString.indexOf("<") > -1
829 || tmpString.indexOf(">") > -1)
831 // The description does not specify html is to
832 // be used, so we must remove < > symbols
833 tmpString = tmpString.replaceAll("<", "<");
834 tmpString = tmpString.replaceAll(">", ">");
836 tooltipText2.append("; ");
837 tooltipText2.append(tmpString);
842 tooltipText2.append("; " + tmpString);
846 // check score should be shown
847 if (features[i].getScore() != Float.NaN)
849 float[][] rng = (minmax == null) ? null : ((float[][]) minmax
850 .get(features[i].getType()));
851 if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
853 tooltipText2.append(" Score=" + features[i].getScore());
856 if (features[i].getValue("status") != null)
858 String status = features[i].getValue("status").toString();
859 if (status.length() > 0)
861 tooltipText2.append("; (" + features[i].getValue("status")
866 if (features[i].links != null)
868 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
879 * Set status message in alignment panel
882 * aligned sequence object
886 * index of sequence in alignment
887 * @return position of res in sequence
889 int setStatusMessage(SequenceI sequence, int res, int seq)
892 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
893 + sequence.getName());
896 if (av.alignment.isNucleotide())
898 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
902 text.append(" Nucleotide: ");
907 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
910 text.append(" Residue: ");
916 pos = sequence.findPosition(res);
919 text.append(obj + " (" + pos + ")");
922 ap.alignFrame.statusBar.setText(text.toString());
932 public void mouseDragged(MouseEvent evt)
934 if (mouseWheelPressed)
936 int oldWidth = av.charWidth;
938 // Which is bigger, left-right or up-down?
939 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
941 - lastMousePress.getX()))
943 int fontSize = av.font.getSize();
945 if (evt.getY() < lastMousePress.getY())
949 else if (evt.getY() > lastMousePress.getY())
960 .setFont(new Font(av.font.getName(), av.font.getStyle(),
962 av.charWidth = oldWidth;
967 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
971 else if (evt.getX() > lastMousePress.getX())
976 ap.paintAlignment(false);
979 FontMetrics fm = getFontMetrics(av.getFont());
980 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
982 lastMousePress = evt.getPoint();
989 doMouseDraggedDefineMode(evt);
993 int res = findRes(evt);
1000 if ((lastres == -1) || (lastres == res))
1005 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1007 // dragLeft, delete gap
1008 editSequence(false, res);
1012 editSequence(true, res);
1015 mouseDragging = true;
1016 if (scrollThread != null)
1018 scrollThread.setEvent(evt);
1022 synchronized void editSequence(boolean insertGap, int startres)
1025 int fixedRight = -1;
1026 boolean fixedColumns = false;
1027 SequenceGroup sg = av.getSelectionGroup();
1029 SequenceI seq = av.alignment.getSequenceAt(startseq);
1031 // No group, but the sequence may represent a group
1032 if (!groupEditing && av.hasHiddenRows)
1034 if (av.hiddenRepSequences != null
1035 && av.hiddenRepSequences.containsKey(seq))
1037 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1038 groupEditing = true;
1042 StringBuffer message = new StringBuffer();
1045 message.append("Edit group:");
1046 if (editCommand == null)
1048 editCommand = new EditCommand("Edit Group");
1053 message.append("Edit sequence: " + seq.getName());
1054 String label = seq.getName();
1055 if (label.length() > 10)
1057 label = label.substring(0, 10);
1059 if (editCommand == null)
1061 editCommand = new EditCommand("Edit " + label);
1067 message.append(" insert ");
1071 message.append(" delete ");
1074 message.append(Math.abs(startres - lastres) + " gaps.");
1075 ap.alignFrame.statusBar.setText(message.toString());
1077 // Are we editing within a selection group?
1079 || (sg != null && sg.getSequences(av.hiddenRepSequences)
1082 fixedColumns = true;
1084 // sg might be null as the user may only see 1 sequence,
1085 // but the sequence represents a group
1088 if (av.hiddenRepSequences == null
1089 || !av.hiddenRepSequences.containsKey(seq))
1094 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1097 fixedLeft = sg.getStartRes();
1098 fixedRight = sg.getEndRes();
1100 if ((startres < fixedLeft && lastres >= fixedLeft)
1101 || (startres >= fixedLeft && lastres < fixedLeft)
1102 || (startres > fixedRight && lastres <= fixedRight)
1103 || (startres <= fixedRight && lastres > fixedRight))
1109 if (fixedLeft > startres)
1111 fixedRight = fixedLeft - 1;
1114 else if (fixedRight < startres)
1116 fixedLeft = fixedRight;
1121 if (av.hasHiddenColumns)
1123 fixedColumns = true;
1124 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1125 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1127 if ((insertGap && startres > y1 && lastres < y1)
1128 || (!insertGap && startres < y2 && lastres > y2))
1134 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1135 // Selection spans a hidden region
1136 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1144 fixedRight = y2 - 1;
1151 Vector vseqs = sg.getSequences(av.hiddenRepSequences);
1152 int g, groupSize = vseqs.size();
1153 SequenceI[] groupSeqs = new SequenceI[groupSize];
1154 for (g = 0; g < groupSeqs.length; g++)
1156 groupSeqs[g] = (SequenceI) vseqs.elementAt(g);
1162 // If the user has selected the whole sequence, and is dragging to
1163 // the right, we can still extend the alignment and selectionGroup
1164 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1165 && sg.getEndRes() == av.alignment.getWidth() - 1)
1167 sg.setEndRes(av.alignment.getWidth() + startres - lastres);
1168 fixedRight = sg.getEndRes();
1171 // Is it valid with fixed columns??
1172 // Find the next gap before the end
1173 // of the visible region boundary
1174 boolean blank = false;
1175 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1179 for (g = 0; g < groupSize; g++)
1181 for (int j = 0; j < startres - lastres; j++)
1183 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1184 .getCharAt(fixedRight - j)))
1199 if (sg.getSize() == av.alignment.getHeight())
1201 if ((av.hasHiddenColumns && startres < av.getColumnSelection()
1202 .getHiddenBoundaryRight(startres)))
1208 int alWidth = av.alignment.getWidth();
1209 if (av.hasHiddenRows)
1211 int hwidth = av.alignment.getHiddenSequences().getWidth();
1212 if (hwidth > alWidth)
1217 // We can still insert gaps if the selectionGroup
1218 // contains all the sequences
1219 sg.setEndRes(sg.getEndRes() + startres - lastres);
1220 fixedRight = alWidth + startres - lastres;
1231 else if (!insertGap)
1233 // / Are we able to delete?
1234 // ie are all columns blank?
1236 for (g = 0; g < groupSize; g++)
1238 for (int j = startres; j < lastres; j++)
1240 if (groupSeqs[g].getLength() <= j)
1245 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1247 // Not a gap, block edit not valid
1257 // dragging to the right
1258 if (fixedColumns && fixedRight != -1)
1260 for (int j = lastres; j < startres; j++)
1262 insertChar(j, groupSeqs, fixedRight);
1267 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1268 startres, startres - lastres, av.alignment, true);
1273 // dragging to the left
1274 if (fixedColumns && fixedRight != -1)
1276 for (int j = lastres; j > startres; j--)
1278 deleteChar(startres, groupSeqs, fixedRight);
1283 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1284 startres, lastres - startres, av.alignment, true);
1290 // ///Editing a single sequence///////////
1294 // dragging to the right
1295 if (fixedColumns && fixedRight != -1)
1297 for (int j = lastres; j < startres; j++)
1299 insertChar(j, new SequenceI[]
1300 { seq }, fixedRight);
1305 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1306 { seq }, lastres, startres - lastres, av.alignment, true);
1311 // dragging to the left
1312 if (fixedColumns && fixedRight != -1)
1314 for (int j = lastres; j > startres; j--)
1316 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1321 deleteChar(startres, new SequenceI[]
1322 { seq }, fixedRight);
1327 // could be a keyboard edit trying to delete none gaps
1329 for (int m = startres; m < lastres; m++)
1331 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1340 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1341 { seq }, startres, max, av.alignment, true);
1348 seqCanvas.repaint();
1351 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1353 int blankColumn = fixedColumn;
1354 for (int s = 0; s < seq.length; s++)
1356 // Find the next gap before the end of the visible region boundary
1357 // If lastCol > j, theres a boundary after the gap insertion
1359 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1361 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1363 // Theres a space, so break and insert the gap
1368 if (blankColumn <= j)
1370 blankColumn = fixedColumn;
1376 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1377 av.alignment, true);
1379 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1, av.alignment,
1384 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1387 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1, av.alignment,
1390 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1391 av.alignment, true);
1400 public void mouseEntered(MouseEvent e)
1407 if (scrollThread != null)
1409 scrollThread.running = false;
1410 scrollThread = null;
1420 public void mouseExited(MouseEvent e)
1422 if (av.getWrapAlignment())
1429 scrollThread = new ScrollThread();
1433 public void mouseClicked(MouseEvent evt)
1435 SequenceGroup sg = null;
1436 SequenceI sequence = av.alignment.getSequenceAt(findSeq(evt));
1437 if (evt.getClickCount() > 1)
1439 sg = av.getSelectionGroup();
1440 if (sg != null && sg.getSize() == 1
1441 && sg.getEndRes() - sg.getStartRes() < 2)
1443 av.setSelectionGroup(null);
1446 SequenceFeature[] features = findFeaturesAtRes(sequence
1447 .getDatasetSequence(), sequence.findPosition(findRes(evt)));
1449 if (features != null && features.length > 0)
1451 SearchResults highlight = new SearchResults();
1452 highlight.addResult(sequence, features[0].getBegin(), features[0]
1454 seqCanvas.highlightSearchResults(highlight);
1456 if (features != null && features.length > 0)
1458 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1459 { sequence }, features, false, ap);
1461 seqCanvas.highlightSearchResults(null);
1466 public void mouseWheelMoved(MouseWheelEvent e)
1469 if (e.getWheelRotation() > 0)
1477 // TODO Update tooltip for new position.
1486 public void doMousePressedDefineMode(MouseEvent evt)
1488 int res = findRes(evt);
1489 int seq = findSeq(evt);
1492 startWrapBlock = wrappedBlock;
1494 if (av.wrapAlignment && seq > av.alignment.getHeight())
1496 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1497 "Cannot edit annotations in wrapped view.",
1498 "Wrapped view - no edit", JOptionPane.WARNING_MESSAGE);
1502 if (seq < 0 || res < 0)
1507 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1509 if ((sequence == null) || (res > sequence.getLength()))
1514 stretchGroup = av.getSelectionGroup();
1516 if (stretchGroup == null)
1518 stretchGroup = av.alignment.findGroup(sequence);
1520 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1521 && (res < stretchGroup.getEndRes()))
1523 av.setSelectionGroup(stretchGroup);
1527 stretchGroup = null;
1530 else if (!stretchGroup.getSequences(null).contains(sequence)
1531 || (stretchGroup.getStartRes() > res)
1532 || (stretchGroup.getEndRes() < res))
1534 stretchGroup = null;
1536 SequenceGroup[] allGroups = av.alignment.findAllGroups(sequence);
1538 if (allGroups != null)
1540 for (int i = 0; i < allGroups.length; i++)
1542 if ((allGroups[i].getStartRes() <= res)
1543 && (allGroups[i].getEndRes() >= res))
1545 stretchGroup = allGroups[i];
1551 av.setSelectionGroup(stretchGroup);
1555 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1557 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence
1558 .getDatasetSequence(), sequence.findPosition(res));
1559 Vector links = new Vector();
1560 for (int i = 0; i < allFeatures.length; i++)
1562 if (allFeatures[i].links != null)
1564 for (int j = 0; j < allFeatures[i].links.size(); j++)
1566 links.addElement(allFeatures[i].links.elementAt(j));
1571 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1572 pop.show(this, evt.getX(), evt.getY());
1578 seqCanvas.cursorX = findRes(evt);
1579 seqCanvas.cursorY = findSeq(evt);
1580 seqCanvas.repaint();
1584 if (stretchGroup == null)
1586 // Only if left mouse button do we want to change group sizes
1588 // define a new group here
1589 SequenceGroup sg = new SequenceGroup();
1590 sg.setStartRes(res);
1592 sg.addSequence(sequence, false);
1593 av.setSelectionGroup(sg);
1597 if (av.getConservationSelected())
1599 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1603 if (av.getAbovePIDThreshold())
1605 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1608 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1610 // Edit end res position of selected group
1611 changeEndRes = true;
1613 else if ((stretchGroup != null)
1614 && (stretchGroup.getStartRes() == res))
1616 // Edit end res position of selected group
1617 changeStartRes = true;
1619 stretchGroup.getWidth();
1622 seqCanvas.repaint();
1631 public void doMouseReleasedDefineMode(MouseEvent evt)
1633 if (stretchGroup == null)
1638 stretchGroup.recalcConservation(); // always do this - annotation has own
1640 if (stretchGroup.cs != null)
1642 if (stretchGroup.cs instanceof ClustalxColourScheme)
1644 ((ClustalxColourScheme) stretchGroup.cs).resetClustalX(stretchGroup
1645 .getSequences(av.hiddenRepSequences), stretchGroup
1649 if (stretchGroup.cs.conservationApplied())
1651 SliderPanel.setConservationSlider(ap, stretchGroup.cs, stretchGroup
1656 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs, stretchGroup
1660 PaintRefresher.Refresh(this, av.getSequenceSetId());
1661 ap.paintAlignment(true);
1663 changeEndRes = false;
1664 changeStartRes = false;
1665 stretchGroup = null;
1675 public void doMouseDraggedDefineMode(MouseEvent evt)
1677 int res = findRes(evt);
1678 int y = findSeq(evt);
1680 if (wrappedBlock != startWrapBlock)
1685 if (stretchGroup == null)
1690 if (res >= av.alignment.getWidth())
1692 res = av.alignment.getWidth() - 1;
1695 if (stretchGroup.getEndRes() == res)
1697 // Edit end res position of selected group
1698 changeEndRes = true;
1700 else if (stretchGroup.getStartRes() == res)
1702 // Edit start res position of selected group
1703 changeStartRes = true;
1706 if (res < av.getStartRes())
1708 res = av.getStartRes();
1713 if (res > (stretchGroup.getStartRes() - 1))
1715 stretchGroup.setEndRes(res);
1718 else if (changeStartRes)
1720 if (res < (stretchGroup.getEndRes() + 1))
1722 stretchGroup.setStartRes(res);
1726 int dragDirection = 0;
1732 else if (y < oldSeq)
1737 while ((y != oldSeq) && (oldSeq > -1) && (y < av.alignment.getHeight()))
1739 // This routine ensures we don't skip any sequences, as the
1740 // selection is quite slow.
1741 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1743 oldSeq += dragDirection;
1750 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1752 if (stretchGroup.getSequences(null).contains(nextSeq))
1754 stretchGroup.deleteSequence(seq, false);
1760 stretchGroup.addSequence(seq, false);
1763 stretchGroup.addSequence(nextSeq, false);
1772 mouseDragging = true;
1774 if (scrollThread != null)
1776 scrollThread.setEvent(evt);
1779 seqCanvas.repaint();
1782 void scrollCanvas(MouseEvent evt)
1786 if (scrollThread != null)
1788 scrollThread.running = false;
1789 scrollThread = null;
1791 mouseDragging = false;
1795 if (scrollThread == null)
1797 scrollThread = new ScrollThread();
1800 mouseDragging = true;
1801 scrollThread.setEvent(evt);
1806 // this class allows scrolling off the bottom of the visible alignment
1807 class ScrollThread extends Thread
1811 boolean running = false;
1813 public ScrollThread()
1818 public void setEvent(MouseEvent e)
1823 public void stopScrolling()
1836 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1838 running = ap.scrollUp(true);
1841 if (mouseDragging && (evt.getY() >= getHeight())
1842 && (av.alignment.getHeight() > av.getEndSeq()))
1844 running = ap.scrollUp(false);
1847 if (mouseDragging && (evt.getX() < 0))
1849 running = ap.scrollRight(false);
1851 else if (mouseDragging && (evt.getX() >= getWidth()))
1853 running = ap.scrollRight(true);
1860 } catch (Exception ex)
1868 * modify current selection according to a received message.
1870 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1871 SelectionSource source)
1873 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1874 // handles selection messages...
1875 // TODO: extend config options to allow user to control if selections may be
1876 // shared between viewports.
1878 || !av.followSelection
1879 || (source instanceof AlignViewport && ((AlignViewport) source)
1880 .getSequenceSetId().equals(av.getSequenceSetId())))
1884 // do we want to thread this ? (contention with seqsel and colsel locks, I
1886 boolean repaint = false;
1887 if (av.selectionGroup == null || !av.isSelectionGroupChanged())
1889 SequenceGroup sgroup = null;
1892 if (av.alignment == null)
1894 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1895 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1896 + " 's alignment is NULL! returning immediatly.");
1899 sgroup = seqsel.intersect(av.alignment,
1900 (av.hasHiddenRows) ? av.hiddenRepSequences : null);
1902 if (sgroup != null && sgroup.getSize() > 0)
1904 av.setSelectionGroup(sgroup);
1908 av.setSelectionGroup(null);
1910 repaint = av.isSelectionGroupChanged();
1912 if (av.colSel == null || !av.isColSelChanged())
1914 // the current selection is unset or from a previous message
1915 // so import the new colsel.
1916 if (colsel == null || colsel.size() == 0)
1918 if (av.colSel != null)
1925 if (av.colSel == null)
1927 av.colSel = new ColumnSelection(colsel);
1931 av.colSel.setElementsFrom(colsel);
1934 repaint |= av.isColSelChanged();
1936 if (av.hasHiddenColumns
1937 && (av.colSel == null || av.colSel.getHiddenColumns() == null))
1939 System.err.println("Bad things");
1943 // probably finessing with multiple redraws here
1944 PaintRefresher.Refresh(this, av.getSequenceSetId());
1945 // ap.paintAlignment(false);