2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
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(),
202 av.alignment.getHeight() - 1);
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(
692 sequence.getDatasetSequence(),
693 rpos = sequence.findPosition(res));
694 appendFeatures(tooltipText, linkImageURL.toString(), rpos, features,
695 this.ap.seqPanel.seqCanvas.fr.minmax);
697 if (tooltipText.length() == 6) // <html></html>
699 setToolTipText(null);
704 tooltipText.append("</html>");
705 if (lastTooltip == null
706 || !lastTooltip.equals(tooltipText.toString()))
708 setToolTipText(tooltipText.toString());
709 lastTooltip = tooltipText.toString();
716 private Point lastp = null;
721 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
723 public Point getToolTipLocation(MouseEvent event)
725 int x = event.getX(), w = getWidth();
726 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
729 if (!event.isShiftDown() || p == null)
731 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
732 event.getX() + wdth, event.getY() - 20) : null;
735 * TODO: try to modify position region is not obcured by tooltip
741 * appends the features at rpos to the given stringbuffer ready for display in
744 * @param tooltipText2
745 * @param linkImageURL
748 * TODO refactor to Jalview 'utilities' somehow.
750 public void appendFeatures(StringBuffer tooltipText2,
751 String linkImageURL, int rpos, SequenceFeature[] features)
753 appendFeatures(tooltipText2, linkImageURL, rpos, features, null);
756 public void appendFeatures(StringBuffer tooltipText2, String string,
757 int rpos, SequenceFeature[] features, Hashtable minmax)
760 if (features != null)
762 for (int i = 0; i < features.length; i++)
764 if (features[i].getType().equals("disulfide bond"))
766 if (features[i].getBegin() == rpos
767 || features[i].getEnd() == rpos)
769 if (tooltipText2.length() > 6)
771 tooltipText2.append("<br>");
773 tooltipText2.append("disulfide bond " + features[i].getBegin()
774 + ":" + features[i].getEnd());
775 if (features[i].links != null)
777 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
783 if (tooltipText2.length() > 6)
785 tooltipText2.append("<br>");
787 // TODO: remove this hack to display link only features
788 boolean linkOnly = features[i].getValue("linkonly") != null;
791 tooltipText2.append(features[i].getType() + " ");
794 // we are marking a positional feature
795 tooltipText2.append(features[i].begin);
797 if (features[i].begin != features[i].end)
799 tooltipText2.append(" " + features[i].end);
802 if (features[i].getDescription() != null
803 && !features[i].description.equals(features[i]
806 tmpString = features[i].getDescription();
807 int startTag = tmpString.toUpperCase().indexOf("<HTML>");
810 tmpString = tmpString.substring(startTag + 6);
812 int endTag = tmpString.toUpperCase().indexOf("</BODY>");
815 tmpString = tmpString.substring(0, endTag);
817 endTag = tmpString.toUpperCase().indexOf("</HTML>");
820 tmpString = tmpString.substring(0, endTag);
825 tooltipText2.append("; " + tmpString);
829 if (tmpString.indexOf("<") > -1
830 || tmpString.indexOf(">") > -1)
832 // The description does not specify html is to
833 // be used, so we must remove < > symbols
834 tmpString = tmpString.replaceAll("<", "<");
835 tmpString = tmpString.replaceAll(">", ">");
837 tooltipText2.append("; ");
838 tooltipText2.append(tmpString);
843 tooltipText2.append("; " + tmpString);
847 // check score should be shown
848 if (features[i].getScore() != Float.NaN)
850 float[][] rng = (minmax == null) ? null : ((float[][]) minmax
851 .get(features[i].getType()));
852 if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
854 tooltipText2.append(" Score=" + features[i].getScore());
857 if (features[i].getValue("status") != null)
859 String status = features[i].getValue("status").toString();
860 if (status.length() > 0)
862 tooltipText2.append("; (" + features[i].getValue("status")
867 if (features[i].links != null)
869 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
880 * Set status message in alignment panel
883 * aligned sequence object
887 * index of sequence in alignment
888 * @return position of res in sequence
890 int setStatusMessage(SequenceI sequence, int res, int seq)
893 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
894 + sequence.getName());
897 if (av.alignment.isNucleotide())
899 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
903 text.append(" Nucleotide: ");
908 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
911 text.append(" Residue: ");
917 pos = sequence.findPosition(res);
920 text.append(obj + " (" + pos + ")");
923 ap.alignFrame.statusBar.setText(text.toString());
933 public void mouseDragged(MouseEvent evt)
935 if (mouseWheelPressed)
937 int oldWidth = av.charWidth;
939 // Which is bigger, left-right or up-down?
940 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
941 .getX() - lastMousePress.getX()))
943 int fontSize = av.font.getSize();
945 if (evt.getY() < lastMousePress.getY())
949 else if (evt.getY() > lastMousePress.getY())
959 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
960 av.charWidth = oldWidth;
965 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
969 else if (evt.getX() > lastMousePress.getX())
974 ap.paintAlignment(false);
977 FontMetrics fm = getFontMetrics(av.getFont());
978 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
980 lastMousePress = evt.getPoint();
987 doMouseDraggedDefineMode(evt);
991 int res = findRes(evt);
998 if ((lastres == -1) || (lastres == res))
1003 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1005 // dragLeft, delete gap
1006 editSequence(false, res);
1010 editSequence(true, res);
1013 mouseDragging = true;
1014 if (scrollThread != null)
1016 scrollThread.setEvent(evt);
1020 synchronized void editSequence(boolean insertGap, int startres)
1023 int fixedRight = -1;
1024 boolean fixedColumns = false;
1025 SequenceGroup sg = av.getSelectionGroup();
1027 SequenceI seq = av.alignment.getSequenceAt(startseq);
1029 // No group, but the sequence may represent a group
1030 if (!groupEditing && av.hasHiddenRows)
1032 if (av.hiddenRepSequences != null
1033 && av.hiddenRepSequences.containsKey(seq))
1035 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1036 groupEditing = true;
1040 StringBuffer message = new StringBuffer();
1043 message.append("Edit group:");
1044 if (editCommand == null)
1046 editCommand = new EditCommand("Edit Group");
1051 message.append("Edit sequence: " + seq.getName());
1052 String label = seq.getName();
1053 if (label.length() > 10)
1055 label = label.substring(0, 10);
1057 if (editCommand == null)
1059 editCommand = new EditCommand("Edit " + label);
1065 message.append(" insert ");
1069 message.append(" delete ");
1072 message.append(Math.abs(startres - lastres) + " gaps.");
1073 ap.alignFrame.statusBar.setText(message.toString());
1075 // Are we editing within a selection group?
1077 || (sg != null && sg.getSequences(av.hiddenRepSequences)
1080 fixedColumns = true;
1082 // sg might be null as the user may only see 1 sequence,
1083 // but the sequence represents a group
1086 if (av.hiddenRepSequences == null
1087 || !av.hiddenRepSequences.containsKey(seq))
1092 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1095 fixedLeft = sg.getStartRes();
1096 fixedRight = sg.getEndRes();
1098 if ((startres < fixedLeft && lastres >= fixedLeft)
1099 || (startres >= fixedLeft && lastres < fixedLeft)
1100 || (startres > fixedRight && lastres <= fixedRight)
1101 || (startres <= fixedRight && lastres > fixedRight))
1107 if (fixedLeft > startres)
1109 fixedRight = fixedLeft - 1;
1112 else if (fixedRight < startres)
1114 fixedLeft = fixedRight;
1119 if (av.hasHiddenColumns)
1121 fixedColumns = true;
1122 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1123 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1125 if ((insertGap && startres > y1 && lastres < y1)
1126 || (!insertGap && startres < y2 && lastres > y2))
1132 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1133 // Selection spans a hidden region
1134 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1142 fixedRight = y2 - 1;
1149 Vector vseqs = sg.getSequences(av.hiddenRepSequences);
1150 int g, groupSize = vseqs.size();
1151 SequenceI[] groupSeqs = new SequenceI[groupSize];
1152 for (g = 0; g < groupSeqs.length; g++)
1154 groupSeqs[g] = (SequenceI) vseqs.elementAt(g);
1160 // If the user has selected the whole sequence, and is dragging to
1161 // the right, we can still extend the alignment and selectionGroup
1162 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1163 && sg.getEndRes() == av.alignment.getWidth() - 1)
1165 sg.setEndRes(av.alignment.getWidth() + startres - lastres);
1166 fixedRight = sg.getEndRes();
1169 // Is it valid with fixed columns??
1170 // Find the next gap before the end
1171 // of the visible region boundary
1172 boolean blank = false;
1173 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1177 for (g = 0; g < groupSize; g++)
1179 for (int j = 0; j < startres - lastres; j++)
1181 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1182 .getCharAt(fixedRight - j)))
1197 if (sg.getSize() == av.alignment.getHeight())
1199 if ((av.hasHiddenColumns && startres < av.getColumnSelection()
1200 .getHiddenBoundaryRight(startres)))
1206 int alWidth = av.alignment.getWidth();
1207 if (av.hasHiddenRows)
1209 int hwidth = av.alignment.getHiddenSequences().getWidth();
1210 if (hwidth > alWidth)
1215 // We can still insert gaps if the selectionGroup
1216 // contains all the sequences
1217 sg.setEndRes(sg.getEndRes() + startres - lastres);
1218 fixedRight = alWidth + startres - lastres;
1229 else if (!insertGap)
1231 // / Are we able to delete?
1232 // ie are all columns blank?
1234 for (g = 0; g < groupSize; g++)
1236 for (int j = startres; j < lastres; j++)
1238 if (groupSeqs[g].getLength() <= j)
1243 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1245 // Not a gap, block edit not valid
1255 // dragging to the right
1256 if (fixedColumns && fixedRight != -1)
1258 for (int j = lastres; j < startres; j++)
1260 insertChar(j, groupSeqs, fixedRight);
1265 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1266 startres, startres - lastres, av.alignment, true);
1271 // dragging to the left
1272 if (fixedColumns && fixedRight != -1)
1274 for (int j = lastres; j > startres; j--)
1276 deleteChar(startres, groupSeqs, fixedRight);
1281 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1282 startres, lastres - startres, av.alignment, true);
1288 // ///Editing a single sequence///////////
1292 // dragging to the right
1293 if (fixedColumns && fixedRight != -1)
1295 for (int j = lastres; j < startres; j++)
1297 insertChar(j, new SequenceI[]
1298 { seq }, fixedRight);
1303 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1304 { seq }, lastres, startres - lastres, av.alignment, true);
1309 // dragging to the left
1310 if (fixedColumns && fixedRight != -1)
1312 for (int j = lastres; j > startres; j--)
1314 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1319 deleteChar(startres, new SequenceI[]
1320 { seq }, fixedRight);
1325 // could be a keyboard edit trying to delete none gaps
1327 for (int m = startres; m < lastres; m++)
1329 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1338 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1339 { seq }, startres, max, av.alignment, true);
1346 seqCanvas.repaint();
1349 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1351 int blankColumn = fixedColumn;
1352 for (int s = 0; s < seq.length; s++)
1354 // Find the next gap before the end of the visible region boundary
1355 // If lastCol > j, theres a boundary after the gap insertion
1357 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1359 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1361 // Theres a space, so break and insert the gap
1366 if (blankColumn <= j)
1368 blankColumn = fixedColumn;
1374 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1375 av.alignment, true);
1377 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1, av.alignment,
1382 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1385 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1, av.alignment,
1388 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1389 av.alignment, true);
1398 public void mouseEntered(MouseEvent e)
1405 if (scrollThread != null)
1407 scrollThread.running = false;
1408 scrollThread = null;
1418 public void mouseExited(MouseEvent e)
1420 if (av.getWrapAlignment())
1427 scrollThread = new ScrollThread();
1431 public void mouseClicked(MouseEvent evt)
1433 SequenceGroup sg = null;
1434 SequenceI sequence = av.alignment.getSequenceAt(findSeq(evt));
1435 if (evt.getClickCount() > 1)
1437 sg = av.getSelectionGroup();
1438 if (sg != null && sg.getSize() == 1
1439 && sg.getEndRes() - sg.getStartRes() < 2)
1441 av.setSelectionGroup(null);
1444 SequenceFeature[] features = findFeaturesAtRes(
1445 sequence.getDatasetSequence(),
1446 sequence.findPosition(findRes(evt)));
1448 if (features != null && features.length > 0)
1450 SearchResults highlight = new SearchResults();
1451 highlight.addResult(sequence, features[0].getBegin(),
1452 features[0].getEnd());
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(
1557 sequence.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(
1644 stretchGroup.getSequences(av.hiddenRepSequences),
1645 stretchGroup.getWidth());
1648 if (stretchGroup.cs.conservationApplied())
1650 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1651 stretchGroup.getName());
1655 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1656 stretchGroup.getName());
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 // rules are: colsel is copied if there is a real intersection between
1886 // sequence selection
1887 boolean repaint = false, copycolsel = true;
1888 if (av.selectionGroup == null || !av.isSelectionGroupChanged())
1890 SequenceGroup sgroup = null;
1893 if (av.alignment == null)
1895 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1896 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1897 + " 's alignment is NULL! returning immediatly.");
1900 sgroup = seqsel.intersect(av.alignment,
1901 (av.hasHiddenRows) ? av.hiddenRepSequences : null);
1902 if ((sgroup == null || sgroup.getSize() == 0)
1903 && (colsel == null || colsel.size() == 0))
1905 // don't copy columns if the region didn't intersect.
1909 if (sgroup != null && sgroup.getSize() > 0)
1911 av.setSelectionGroup(sgroup);
1915 av.setSelectionGroup(null);
1917 repaint = av.isSelectionGroupChanged();
1919 if (copycolsel && (av.colSel == null || !av.isColSelChanged()))
1921 // the current selection is unset or from a previous message
1922 // so import the new colsel.
1923 if (colsel == null || colsel.size() == 0)
1925 if (av.colSel != null)
1932 // TODO: shift colSel according to the intersecting sequences
1933 if (av.colSel == null)
1935 av.colSel = new ColumnSelection(colsel);
1939 av.colSel.setElementsFrom(colsel);
1942 repaint |= av.isColSelChanged();
1944 if (copycolsel && av.hasHiddenColumns
1945 && (av.colSel == null || av.colSel.getHiddenColumns() == null))
1947 System.err.println("Bad things");
1951 // probably finessing with multiple redraws here
1952 PaintRefresher.Refresh(this, av.getSequenceSetId());
1953 // ap.paintAlignment(false);