2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 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, SelectionListener
43 public SeqCanvas seqCanvas;
46 public AlignmentPanel ap;
48 protected int lastres;
50 protected int startseq;
52 protected AlignViewport av;
54 ScrollThread scrollThread = null;
56 boolean mouseDragging = false;
58 boolean editingSeqs = false;
60 boolean groupEditing = false;
62 // ////////////////////////////////////////
63 // ///Everything below this is for defining the boundary of the rubberband
64 // ////////////////////////////////////////
67 boolean changeEndSeq = false;
69 boolean changeStartSeq = false;
71 boolean changeEndRes = false;
73 boolean changeStartRes = false;
75 SequenceGroup stretchGroup = null;
77 boolean remove = false;
81 boolean mouseWheelPressed = false;
83 StringBuffer keyboardNo1;
85 StringBuffer keyboardNo2;
87 java.net.URL linkImageURL;
89 StringBuffer tooltipText = new StringBuffer("<html>");
93 EditCommand editCommand;
95 StructureSelectionManager ssm;
98 * Creates a new SeqPanel object.
105 public SeqPanel(AlignViewport av, AlignmentPanel ap)
107 linkImageURL = getClass().getResource("/images/link.gif");
108 ToolTipManager.sharedInstance().registerComponent(this);
109 ToolTipManager.sharedInstance().setInitialDelay(0);
110 ToolTipManager.sharedInstance().setDismissDelay(10000);
112 setBackground(Color.white);
114 seqCanvas = new SeqCanvas(ap);
115 setLayout(new BorderLayout());
116 add(seqCanvas, BorderLayout.CENTER);
122 addMouseMotionListener(this);
123 addMouseListener(this);
124 addMouseWheelListener(this);
125 ssm = StructureSelectionManager.getStructureSelectionManager();
126 ssm.addStructureViewerListener(this);
127 ssm.addSelectionListener(this);
131 int startWrapBlock = -1;
133 int wrappedBlock = -1;
135 int findRes(MouseEvent evt)
140 if (av.wrapAlignment)
143 int hgap = av.charHeight;
144 if (av.scaleAboveWrapped)
146 hgap += av.charHeight;
149 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
150 + seqCanvas.getAnnotationHeight();
154 x -= seqCanvas.LABEL_WEST;
156 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
162 wrappedBlock = y / cHeight;
163 wrappedBlock += av.getStartRes() / cwidth;
165 res = wrappedBlock * cwidth + x / av.getCharWidth();
170 res = (x / av.getCharWidth()) + av.getStartRes();
173 if (av.hasHiddenColumns)
175 res = av.getColumnSelection().adjustForHiddenColumns(res);
182 int findSeq(MouseEvent evt)
187 if (av.wrapAlignment)
189 int hgap = av.charHeight;
190 if (av.scaleAboveWrapped)
192 hgap += av.charHeight;
195 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
196 + seqCanvas.getAnnotationHeight();
200 seq = Math.min((y % cHeight) / av.getCharHeight(), av.alignment
205 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(),
206 av.alignment.getHeight() - 1);
212 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
214 Vector tmp = new Vector();
215 SequenceFeature[] features = sequence.getSequenceFeatures();
216 if (features != null)
218 for (int i = 0; i < features.length; i++)
220 if (av.featuresDisplayed == null
221 || !av.featuresDisplayed.containsKey(features[i].getType()))
226 if (features[i].featureGroup != null
227 && seqCanvas.fr.featureGroups != null
228 && seqCanvas.fr.featureGroups
229 .containsKey(features[i].featureGroup)
230 && !((Boolean) seqCanvas.fr.featureGroups
231 .get(features[i].featureGroup)).booleanValue())
234 if ((features[i].getBegin() <= res)
235 && (features[i].getEnd() >= res))
237 tmp.addElement(features[i]);
242 features = new SequenceFeature[tmp.size()];
243 tmp.copyInto(features);
250 if (editCommand != null && editCommand.getSize() > 0)
252 ap.alignFrame.addHistoryItem(editCommand);
253 av.firePropertyChange("alignment", null, av.getAlignment()
260 groupEditing = false;
268 seqCanvas.cursorY = getKeyboardNo1() - 1;
272 void setCursorColumn()
274 seqCanvas.cursorX = getKeyboardNo1() - 1;
278 void setCursorRowAndColumn()
280 if (keyboardNo2 == null)
282 keyboardNo2 = new StringBuffer();
286 seqCanvas.cursorX = getKeyboardNo1() - 1;
287 seqCanvas.cursorY = getKeyboardNo2() - 1;
292 void setCursorPosition()
294 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
297 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1() - 1);
301 void moveCursor(int dx, int dy)
303 seqCanvas.cursorX += dx;
304 seqCanvas.cursorY += dy;
305 if (av.hasHiddenColumns && !av.colSel.isVisible(seqCanvas.cursorX))
307 int original = seqCanvas.cursorX - dx;
308 int maxWidth = av.alignment.getWidth();
310 while (!av.colSel.isVisible(seqCanvas.cursorX)
311 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
313 seqCanvas.cursorX += dx;
316 if (seqCanvas.cursorX >= maxWidth
317 || !av.colSel.isVisible(seqCanvas.cursorX))
319 seqCanvas.cursorX = original;
326 void scrollToVisible()
328 if (seqCanvas.cursorX < 0)
330 seqCanvas.cursorX = 0;
332 else if (seqCanvas.cursorX > av.alignment.getWidth() - 1)
334 seqCanvas.cursorX = av.alignment.getWidth() - 1;
337 if (seqCanvas.cursorY < 0)
339 seqCanvas.cursorY = 0;
341 else if (seqCanvas.cursorY > av.alignment.getHeight() - 1)
343 seqCanvas.cursorY = av.alignment.getHeight() - 1;
347 if (av.wrapAlignment)
349 ap.scrollToWrappedVisible(seqCanvas.cursorX);
353 while (seqCanvas.cursorY < av.startSeq)
357 while (seqCanvas.cursorY + 1 > av.endSeq)
361 if (!av.wrapAlignment)
363 while (seqCanvas.cursorX < av.colSel
364 .adjustForHiddenColumns(av.startRes))
366 if (!ap.scrollRight(false))
371 while (seqCanvas.cursorX > av.colSel
372 .adjustForHiddenColumns(av.endRes))
374 if (!ap.scrollRight(true))
381 setStatusMessage(av.alignment.getSequenceAt(seqCanvas.cursorY),
382 seqCanvas.cursorX, seqCanvas.cursorY);
387 void setSelectionAreaAtCursor(boolean topLeft)
389 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
392 if (av.getSelectionGroup() != null)
394 SequenceGroup sg = av.selectionGroup;
395 // Find the top and bottom of this group
396 int min = av.alignment.getHeight(), max = 0;
397 for (int i = 0; i < sg.getSize(); i++)
399 int index = av.alignment.findIndex(sg.getSequenceAt(i));
414 sg.setStartRes(seqCanvas.cursorX);
415 if (sg.getEndRes() < seqCanvas.cursorX)
417 sg.setEndRes(seqCanvas.cursorX);
420 min = seqCanvas.cursorY;
424 sg.setEndRes(seqCanvas.cursorX);
425 if (sg.getStartRes() > seqCanvas.cursorX)
427 sg.setStartRes(seqCanvas.cursorX);
430 max = seqCanvas.cursorY + 1;
435 // Only the user can do this
436 av.setSelectionGroup(null);
440 // Now add any sequences between min and max
441 sg.getSequences(null).clear();
442 for (int i = min; i < max; i++)
444 sg.addSequence(av.alignment.getSequenceAt(i), false);
449 if (av.getSelectionGroup() == null)
451 SequenceGroup sg = new SequenceGroup();
452 sg.setStartRes(seqCanvas.cursorX);
453 sg.setEndRes(seqCanvas.cursorX);
454 sg.addSequence(sequence, false);
455 av.setSelectionGroup(sg);
458 ap.paintAlignment(false);
462 void insertGapAtCursor(boolean group)
464 groupEditing = group;
465 startseq = seqCanvas.cursorY;
466 lastres = seqCanvas.cursorX;
467 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
471 void deleteGapAtCursor(boolean group)
473 groupEditing = group;
474 startseq = seqCanvas.cursorY;
475 lastres = seqCanvas.cursorX + getKeyboardNo1();
476 editSequence(false, seqCanvas.cursorX);
480 void numberPressed(char value)
482 if (keyboardNo1 == null)
484 keyboardNo1 = new StringBuffer();
487 if (keyboardNo2 != null)
489 keyboardNo2.append(value);
493 keyboardNo1.append(value);
499 if (keyboardNo1 == null)
503 int value = Integer.parseInt(keyboardNo1.toString());
511 if (keyboardNo2 == null)
515 int value = Integer.parseInt(keyboardNo2.toString());
527 public void mouseReleased(MouseEvent evt)
529 mouseDragging = false;
530 mouseWheelPressed = false;
534 doMouseReleasedDefineMode(evt);
547 public void mousePressed(MouseEvent evt)
549 lastMousePress = evt.getPoint();
551 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
553 mouseWheelPressed = true;
557 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
559 if (evt.isAltDown() || evt.isControlDown())
567 doMousePressedDefineMode(evt);
571 int seq = findSeq(evt);
572 int res = findRes(evt);
574 if (seq < 0 || res < 0)
579 if ((seq < av.getAlignment().getHeight())
580 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
596 public void mouseOverSequence(SequenceI sequence, int index, int pos)
598 String tmp = sequence.hashCode() + " " + index + " " + pos;
600 if (lastMessage == null || !lastMessage.equals(tmp))
602 // System.err.println("mouseOver Sequence: "+tmp);
603 ssm.mouseOverSequence(sequence, index, pos);
608 public void highlightSequence(SearchResults results)
610 if (av.followHighlight)
612 ap.scrollToPosition(results, false);
614 seqCanvas.highlightSearchResults(results);
617 public void updateColours(SequenceI seq, int index)
619 System.out.println("update the seqPanel colours");
629 public void mouseMoved(MouseEvent evt)
633 // This is because MacOSX creates a mouseMoved
634 // If control is down, other platforms will not.
638 int res = findRes(evt);
639 int seq = findSeq(evt);
641 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
646 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
648 if (res >= sequence.getLength())
653 pos = setStatusMessage(sequence, res, seq);
654 if (ssm != null && pos > -1)
655 mouseOverSequence(sequence, res, pos);
657 tooltipText.setLength(6); // Cuts the buffer back to <html>
659 SequenceGroup[] groups = av.alignment.findAllGroups(sequence);
662 for (int g = 0; g < groups.length; g++)
664 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
666 if (tooltipText.length() > 6)
668 tooltipText.append("<br>");
671 if (!groups[g].getName().startsWith("JTreeGroup")
672 && !groups[g].getName().startsWith("JGroup"))
674 tooltipText.append(groups[g].getName());
677 if (groups[g].getDescription() != null)
679 tooltipText.append(": " + groups[g].getDescription());
685 // use aa to see if the mouse pointer is on a
686 if (av.showSequenceFeatures)
689 SequenceFeature[] features = findFeaturesAtRes(sequence
690 .getDatasetSequence(), rpos = sequence.findPosition(res));
691 appendFeatures(tooltipText, linkImageURL.toString(), rpos, features);
693 if (tooltipText.length() == 6) // <html></html>
695 setToolTipText(null);
699 tooltipText.append("</html>");
700 if (lastTooltip == null
701 || !lastTooltip.equals(tooltipText.toString()))
702 setToolTipText(tooltipText.toString());
704 lastTooltip = tooltipText.toString();
709 * appends the features at rpos to the given stringbuffer ready for display in a tooltip
710 * @param tooltipText2
711 * @param linkImageURL
714 * TODO refactor to Jalview 'utilities' somehow.
716 public static void appendFeatures(StringBuffer tooltipText2, String linkImageURL,
717 int rpos, SequenceFeature[] features)
720 if (features != null)
722 for (int i = 0; i < features.length; i++)
724 if (features[i].getType().equals("disulfide bond"))
726 if (features[i].getBegin() == rpos
727 || features[i].getEnd() == rpos)
729 if (tooltipText2.length() > 6)
731 tooltipText2.append("<br>");
733 tooltipText2.append("disulfide bond " + features[i].getBegin()
734 + ":" + features[i].getEnd());
735 if (features[i].links != null)
737 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
743 if (tooltipText2.length() > 6)
745 tooltipText2.append("<br>");
748 tooltipText2.append(features[i].getType() + " ");
751 // we are marking a positional feature
752 tooltipText2.append(features[i].begin);
754 if (features[i].begin != features[i].end)
756 tooltipText2.append(" " + features[i].end);
759 if (features[i].getDescription() != null
760 && !features[i].description.equals(features[i]
763 tmpString = features[i].getDescription();
764 int startTag = tmpString.toUpperCase().indexOf("<HTML>");
767 tmpString = tmpString.substring(startTag + 6);
769 int endTag = tmpString.toUpperCase().indexOf("</BODY>");
772 tmpString = tmpString.substring(0, endTag);
774 endTag = tmpString.toUpperCase().indexOf("</HTML>");
777 tmpString = tmpString.substring(0, endTag);
782 tooltipText2.append("; " + tmpString);
786 if (tmpString.indexOf("<") > -1
787 || tmpString.indexOf(">") > -1)
789 // The description does not specify html is to
790 // be used, so we must remove < > symbols
791 tmpString = tmpString.replaceAll("<", "<");
792 tmpString = tmpString.replaceAll(">", ">");
794 tooltipText2.append("; ");
795 tooltipText2.append(tmpString);
800 tooltipText2.append("; " + tmpString);
804 if (features[i].getScore()!=Float.NaN)
806 tooltipText2.append(" Score="+features[i].getScore());
808 if (features[i].getValue("status") != null)
810 String status = features[i].getValue("status").toString();
811 if (status.length() > 0)
813 tooltipText2.append("; (" + features[i].getValue("status")
818 if (features[i].links != null)
820 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
831 * Set status message in alignment panel
834 * aligned sequence object
838 * index of sequence in alignment
839 * @return position of res in sequence
841 int setStatusMessage(SequenceI sequence, int res, int seq)
844 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
845 + sequence.getName());
848 if (av.alignment.isNucleotide())
850 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
854 text.append(" Nucleotide: ");
859 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
862 text.append(" Residue: ");
868 pos = sequence.findPosition(res);
871 text.append(obj + " (" + pos + ")");
874 ap.alignFrame.statusBar.setText(text.toString());
884 public void mouseDragged(MouseEvent evt)
886 if (mouseWheelPressed)
888 int oldWidth = av.charWidth;
890 // Which is bigger, left-right or up-down?
891 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
893 - lastMousePress.getX()))
895 int fontSize = av.font.getSize();
897 if (evt.getY() < lastMousePress.getY())
901 else if (evt.getY() > lastMousePress.getY())
912 .setFont(new Font(av.font.getName(), av.font.getStyle(),
914 av.charWidth = oldWidth;
919 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
923 else if (evt.getX() > lastMousePress.getX())
928 ap.paintAlignment(false);
931 FontMetrics fm = getFontMetrics(av.getFont());
932 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
934 lastMousePress = evt.getPoint();
941 doMouseDraggedDefineMode(evt);
945 int res = findRes(evt);
952 if ((lastres == -1) || (lastres == res))
957 if ((res < av.getAlignment().getWidth()) && (res < lastres))
959 // dragLeft, delete gap
960 editSequence(false, res);
964 editSequence(true, res);
967 mouseDragging = true;
968 if (scrollThread != null)
970 scrollThread.setEvent(evt);
974 synchronized void editSequence(boolean insertGap, int startres)
978 boolean fixedColumns = false;
979 SequenceGroup sg = av.getSelectionGroup();
981 SequenceI seq = av.alignment.getSequenceAt(startseq);
983 // No group, but the sequence may represent a group
984 if (!groupEditing && av.hasHiddenRows)
986 if (av.hiddenRepSequences != null
987 && av.hiddenRepSequences.containsKey(seq))
989 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
994 StringBuffer message = new StringBuffer();
997 message.append("Edit group:");
998 if (editCommand == null)
1000 editCommand = new EditCommand("Edit Group");
1005 message.append("Edit sequence: " + seq.getName());
1006 String label = seq.getName();
1007 if (label.length() > 10)
1009 label = label.substring(0, 10);
1011 if (editCommand == null)
1013 editCommand = new EditCommand("Edit " + label);
1019 message.append(" insert ");
1023 message.append(" delete ");
1026 message.append(Math.abs(startres - lastres) + " gaps.");
1027 ap.alignFrame.statusBar.setText(message.toString());
1029 // Are we editing within a selection group?
1031 || (sg != null && sg.getSequences(av.hiddenRepSequences)
1034 fixedColumns = true;
1036 // sg might be null as the user may only see 1 sequence,
1037 // but the sequence represents a group
1040 if (av.hiddenRepSequences == null
1041 || !av.hiddenRepSequences.containsKey(seq))
1046 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1049 fixedLeft = sg.getStartRes();
1050 fixedRight = sg.getEndRes();
1052 if ((startres < fixedLeft && lastres >= fixedLeft)
1053 || (startres >= fixedLeft && lastres < fixedLeft)
1054 || (startres > fixedRight && lastres <= fixedRight)
1055 || (startres <= fixedRight && lastres > fixedRight))
1061 if (fixedLeft > startres)
1063 fixedRight = fixedLeft - 1;
1066 else if (fixedRight < startres)
1068 fixedLeft = fixedRight;
1073 if (av.hasHiddenColumns)
1075 fixedColumns = true;
1076 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1077 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1079 if ((insertGap && startres > y1 && lastres < y1)
1080 || (!insertGap && startres < y2 && lastres > y2))
1086 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1087 // Selection spans a hidden region
1088 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1096 fixedRight = y2 - 1;
1103 Vector vseqs = sg.getSequences(av.hiddenRepSequences);
1104 int g, groupSize = vseqs.size();
1105 SequenceI[] groupSeqs = new SequenceI[groupSize];
1106 for (g = 0; g < groupSeqs.length; g++)
1108 groupSeqs[g] = (SequenceI) vseqs.elementAt(g);
1114 // If the user has selected the whole sequence, and is dragging to
1115 // the right, we can still extend the alignment and selectionGroup
1116 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1117 && sg.getEndRes() == av.alignment.getWidth() - 1)
1119 sg.setEndRes(av.alignment.getWidth() + startres - lastres);
1120 fixedRight = sg.getEndRes();
1123 // Is it valid with fixed columns??
1124 // Find the next gap before the end
1125 // of the visible region boundary
1126 boolean blank = false;
1127 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1131 for (g = 0; g < groupSize; g++)
1133 for (int j = 0; j < startres - lastres; j++)
1135 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1136 .getCharAt(fixedRight - j)))
1151 if (sg.getSize() == av.alignment.getHeight())
1153 if ((av.hasHiddenColumns && startres < av.getColumnSelection()
1154 .getHiddenBoundaryRight(startres)))
1160 int alWidth = av.alignment.getWidth();
1161 if (av.hasHiddenRows)
1163 int hwidth = av.alignment.getHiddenSequences().getWidth();
1164 if (hwidth > alWidth)
1169 // We can still insert gaps if the selectionGroup
1170 // contains all the sequences
1171 sg.setEndRes(sg.getEndRes() + startres - lastres);
1172 fixedRight = alWidth + startres - lastres;
1183 else if (!insertGap)
1185 // / Are we able to delete?
1186 // ie are all columns blank?
1188 for (g = 0; g < groupSize; g++)
1190 for (int j = startres; j < lastres; j++)
1192 if (groupSeqs[g].getLength() <= j)
1197 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1199 // Not a gap, block edit not valid
1209 // dragging to the right
1210 if (fixedColumns && fixedRight != -1)
1212 for (int j = lastres; j < startres; j++)
1214 insertChar(j, groupSeqs, fixedRight);
1219 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1220 startres, startres - lastres, av.alignment, true);
1225 // dragging to the left
1226 if (fixedColumns && fixedRight != -1)
1228 for (int j = lastres; j > startres; j--)
1230 deleteChar(startres, groupSeqs, fixedRight);
1235 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1236 startres, lastres - startres, av.alignment, true);
1242 // ///Editing a single sequence///////////
1246 // dragging to the right
1247 if (fixedColumns && fixedRight != -1)
1249 for (int j = lastres; j < startres; j++)
1251 insertChar(j, new SequenceI[]
1252 { seq }, fixedRight);
1257 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1258 { seq }, lastres, startres - lastres, av.alignment, true);
1263 // dragging to the left
1264 if (fixedColumns && fixedRight != -1)
1266 for (int j = lastres; j > startres; j--)
1268 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1273 deleteChar(startres, new SequenceI[]
1274 { seq }, fixedRight);
1279 // could be a keyboard edit trying to delete none gaps
1281 for (int m = startres; m < lastres; m++)
1283 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1292 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1293 { seq }, startres, max, av.alignment, true);
1300 seqCanvas.repaint();
1303 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1305 int blankColumn = fixedColumn;
1306 for (int s = 0; s < seq.length; s++)
1308 // Find the next gap before the end of the visible region boundary
1309 // If lastCol > j, theres a boundary after the gap insertion
1311 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1313 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1315 // Theres a space, so break and insert the gap
1320 if (blankColumn <= j)
1322 blankColumn = fixedColumn;
1328 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1329 av.alignment, true);
1331 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1, av.alignment,
1336 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1339 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1, av.alignment,
1342 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1343 av.alignment, true);
1352 public void mouseEntered(MouseEvent e)
1359 if (scrollThread != null)
1361 scrollThread.running = false;
1362 scrollThread = null;
1372 public void mouseExited(MouseEvent e)
1374 if (av.getWrapAlignment())
1381 scrollThread = new ScrollThread();
1385 public void mouseClicked(MouseEvent evt)
1387 SequenceI sequence = av.alignment.getSequenceAt(findSeq(evt));
1388 if (evt.getClickCount() > 1)
1390 if (av.getSelectionGroup().getSize() == 1
1391 && av.getSelectionGroup().getEndRes()
1392 - av.getSelectionGroup().getStartRes() < 2)
1394 av.setSelectionGroup(null);
1397 SequenceFeature[] features = findFeaturesAtRes(sequence
1398 .getDatasetSequence(), sequence.findPosition(findRes(evt)));
1400 if (features != null && features.length > 0)
1402 SearchResults highlight = new SearchResults();
1403 highlight.addResult(sequence, features[0].getBegin(), features[0]
1405 seqCanvas.highlightSearchResults(highlight);
1407 if (features != null && features.length > 0)
1409 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1410 { sequence }, features, false, ap);
1412 seqCanvas.highlightSearchResults(null);
1417 public void mouseWheelMoved(MouseWheelEvent e)
1420 if (e.getWheelRotation() > 0)
1436 public void doMousePressedDefineMode(MouseEvent evt)
1438 int res = findRes(evt);
1439 int seq = findSeq(evt);
1442 startWrapBlock = wrappedBlock;
1444 if (av.wrapAlignment && seq > av.alignment.getHeight())
1446 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1447 "Cannot edit annotations in wrapped view.",
1448 "Wrapped view - no edit", JOptionPane.WARNING_MESSAGE);
1452 if (seq < 0 || res < 0)
1457 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1459 if ((sequence == null) || (res > sequence.getLength()))
1464 stretchGroup = av.getSelectionGroup();
1466 if (stretchGroup == null)
1468 stretchGroup = av.alignment.findGroup(sequence);
1470 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1471 && (res < stretchGroup.getEndRes()))
1473 av.setSelectionGroup(stretchGroup);
1477 stretchGroup = null;
1480 else if (!stretchGroup.getSequences(null).contains(sequence)
1481 || (stretchGroup.getStartRes() > res)
1482 || (stretchGroup.getEndRes() < res))
1484 stretchGroup = null;
1486 SequenceGroup[] allGroups = av.alignment.findAllGroups(sequence);
1488 if (allGroups != null)
1490 for (int i = 0; i < allGroups.length; i++)
1492 if ((allGroups[i].getStartRes() <= res)
1493 && (allGroups[i].getEndRes() >= res))
1495 stretchGroup = allGroups[i];
1501 av.setSelectionGroup(stretchGroup);
1505 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1507 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence
1508 .getDatasetSequence(), sequence.findPosition(res));
1509 Vector links = new Vector();
1510 for (int i = 0; i < allFeatures.length; i++)
1512 if (allFeatures[i].links != null)
1514 for (int j = 0; j < allFeatures[i].links.size(); j++)
1516 links.addElement(allFeatures[i].links.elementAt(j));
1521 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1522 pop.show(this, evt.getX(), evt.getY());
1528 seqCanvas.cursorX = findRes(evt);
1529 seqCanvas.cursorY = findSeq(evt);
1530 seqCanvas.repaint();
1534 if (stretchGroup == null)
1536 // Only if left mouse button do we want to change group sizes
1538 // define a new group here
1539 SequenceGroup sg = new SequenceGroup();
1540 sg.setStartRes(res);
1542 sg.addSequence(sequence, false);
1543 av.setSelectionGroup(sg);
1547 if (av.getConservationSelected())
1549 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1553 if (av.getAbovePIDThreshold())
1555 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1558 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1560 // Edit end res position of selected group
1561 changeEndRes = true;
1563 else if ((stretchGroup != null)
1564 && (stretchGroup.getStartRes() == res))
1566 // Edit end res position of selected group
1567 changeStartRes = true;
1569 stretchGroup.getWidth();
1572 seqCanvas.repaint();
1581 public void doMouseReleasedDefineMode(MouseEvent evt)
1583 if (stretchGroup == null)
1588 if (stretchGroup.cs != null)
1590 if (stretchGroup.cs instanceof ClustalxColourScheme)
1592 ((ClustalxColourScheme) stretchGroup.cs).resetClustalX(stretchGroup
1593 .getSequences(av.hiddenRepSequences), stretchGroup
1597 if (stretchGroup.cs instanceof Blosum62ColourScheme
1598 || stretchGroup.cs instanceof PIDColourScheme
1599 || stretchGroup.cs.conservationApplied()
1600 || stretchGroup.cs.getThreshold() > 0)
1602 stretchGroup.recalcConservation();
1605 if (stretchGroup.cs.conservationApplied())
1607 SliderPanel.setConservationSlider(ap, stretchGroup.cs, stretchGroup
1612 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs, stretchGroup
1615 PaintRefresher.Refresh(this, av.getSequenceSetId());
1616 ap.paintAlignment(true);
1619 changeEndRes = false;
1620 changeStartRes = false;
1621 stretchGroup = null;
1631 public void doMouseDraggedDefineMode(MouseEvent evt)
1633 int res = findRes(evt);
1634 int y = findSeq(evt);
1636 if (wrappedBlock != startWrapBlock)
1641 if (stretchGroup == null)
1646 if (res >= av.alignment.getWidth())
1648 res = av.alignment.getWidth() - 1;
1651 if (stretchGroup.getEndRes() == res)
1653 // Edit end res position of selected group
1654 changeEndRes = true;
1656 else if (stretchGroup.getStartRes() == res)
1658 // Edit start res position of selected group
1659 changeStartRes = true;
1662 if (res < av.getStartRes())
1664 res = av.getStartRes();
1669 if (res > (stretchGroup.getStartRes() - 1))
1671 stretchGroup.setEndRes(res);
1674 else if (changeStartRes)
1676 if (res < (stretchGroup.getEndRes() + 1))
1678 stretchGroup.setStartRes(res);
1682 int dragDirection = 0;
1688 else if (y < oldSeq)
1693 while ((y != oldSeq) && (oldSeq > -1) && (y < av.alignment.getHeight()))
1695 // This routine ensures we don't skip any sequences, as the
1696 // selection is quite slow.
1697 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1699 oldSeq += dragDirection;
1706 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1708 if (stretchGroup.getSequences(null).contains(nextSeq))
1710 stretchGroup.deleteSequence(seq, false);
1716 stretchGroup.addSequence(seq, false);
1719 stretchGroup.addSequence(nextSeq, false);
1728 mouseDragging = true;
1730 if (scrollThread != null)
1732 scrollThread.setEvent(evt);
1735 seqCanvas.repaint();
1738 void scrollCanvas(MouseEvent evt)
1742 if (scrollThread != null)
1744 scrollThread.running = false;
1745 scrollThread = null;
1747 mouseDragging = false;
1751 if (scrollThread == null)
1753 scrollThread = new ScrollThread();
1756 mouseDragging = true;
1757 scrollThread.setEvent(evt);
1762 // this class allows scrolling off the bottom of the visible alignment
1763 class ScrollThread extends Thread
1767 boolean running = false;
1769 public ScrollThread()
1774 public void setEvent(MouseEvent e)
1779 public void stopScrolling()
1792 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1794 running = ap.scrollUp(true);
1797 if (mouseDragging && (evt.getY() >= getHeight())
1798 && (av.alignment.getHeight() > av.getEndSeq()))
1800 running = ap.scrollUp(false);
1803 if (mouseDragging && (evt.getX() < 0))
1805 running = ap.scrollRight(false);
1807 else if (mouseDragging && (evt.getX() >= getWidth()))
1809 running = ap.scrollRight(true);
1816 } catch (Exception ex)
1823 * modify current selection according to a received message.
1825 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1826 SelectionSource source)
1828 // TODO: fix this hack - source of messages is align viewport, but SeqPanel handles selection messages...
1833 // do we want to thread this ? (contention with seqsel and colsel locks, I suspect)
1834 boolean repaint=false;
1835 if (av.followSelection)
1837 if (av.selectionGroup==null || !av.isSelectionGroupChanged())
1839 SequenceGroup sgroup = (seqsel!=null) ? seqsel.intersect(av.alignment, (av.hasHiddenRows) ? av.hiddenRepSequences : null)
1841 if (sgroup!=null && sgroup.getSize()>0)
1843 av.setSelectionGroup(sgroup);
1845 av.setSelectionGroup(null);
1847 repaint = av.isSelectionGroupChanged();
1849 if (av.colSel==null || !av.isColSelChanged())
1851 // Check to see if the current selection is from a previous message
1852 if (colsel==null || colsel.size()==0)
1856 av.colSel = new ColumnSelection(colsel);
1858 repaint |= av.isColSelChanged();
1863 // probably finessing with multiple redraws here
1864 PaintRefresher.Refresh(this, av.getSequenceSetId());
1865 // ap.paintAlignment(false);