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].getValue("status") != null)
806 String status = features[i].getValue("status").toString();
807 if (status.length() > 0)
809 tooltipText2.append("; (" + features[i].getValue("status")
814 if (features[i].links != null)
816 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
827 * Set status message in alignment panel
830 * aligned sequence object
834 * index of sequence in alignment
835 * @return position of res in sequence
837 int setStatusMessage(SequenceI sequence, int res, int seq)
840 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
841 + sequence.getName());
844 if (av.alignment.isNucleotide())
846 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
850 text.append(" Nucleotide: ");
855 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
858 text.append(" Residue: ");
864 pos = sequence.findPosition(res);
867 text.append(obj + " (" + pos + ")");
870 ap.alignFrame.statusBar.setText(text.toString());
880 public void mouseDragged(MouseEvent evt)
882 if (mouseWheelPressed)
884 int oldWidth = av.charWidth;
886 // Which is bigger, left-right or up-down?
887 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
889 - lastMousePress.getX()))
891 int fontSize = av.font.getSize();
893 if (evt.getY() < lastMousePress.getY())
897 else if (evt.getY() > lastMousePress.getY())
908 .setFont(new Font(av.font.getName(), av.font.getStyle(),
910 av.charWidth = oldWidth;
915 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
919 else if (evt.getX() > lastMousePress.getX())
924 ap.paintAlignment(false);
927 FontMetrics fm = getFontMetrics(av.getFont());
928 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
930 lastMousePress = evt.getPoint();
937 doMouseDraggedDefineMode(evt);
941 int res = findRes(evt);
948 if ((lastres == -1) || (lastres == res))
953 if ((res < av.getAlignment().getWidth()) && (res < lastres))
955 // dragLeft, delete gap
956 editSequence(false, res);
960 editSequence(true, res);
963 mouseDragging = true;
964 if (scrollThread != null)
966 scrollThread.setEvent(evt);
970 synchronized void editSequence(boolean insertGap, int startres)
974 boolean fixedColumns = false;
975 SequenceGroup sg = av.getSelectionGroup();
977 SequenceI seq = av.alignment.getSequenceAt(startseq);
979 // No group, but the sequence may represent a group
980 if (!groupEditing && av.hasHiddenRows)
982 if (av.hiddenRepSequences != null
983 && av.hiddenRepSequences.containsKey(seq))
985 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
990 StringBuffer message = new StringBuffer();
993 message.append("Edit group:");
994 if (editCommand == null)
996 editCommand = new EditCommand("Edit Group");
1001 message.append("Edit sequence: " + seq.getName());
1002 String label = seq.getName();
1003 if (label.length() > 10)
1005 label = label.substring(0, 10);
1007 if (editCommand == null)
1009 editCommand = new EditCommand("Edit " + label);
1015 message.append(" insert ");
1019 message.append(" delete ");
1022 message.append(Math.abs(startres - lastres) + " gaps.");
1023 ap.alignFrame.statusBar.setText(message.toString());
1025 // Are we editing within a selection group?
1027 || (sg != null && sg.getSequences(av.hiddenRepSequences)
1030 fixedColumns = true;
1032 // sg might be null as the user may only see 1 sequence,
1033 // but the sequence represents a group
1036 if (av.hiddenRepSequences == null
1037 || !av.hiddenRepSequences.containsKey(seq))
1042 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1045 fixedLeft = sg.getStartRes();
1046 fixedRight = sg.getEndRes();
1048 if ((startres < fixedLeft && lastres >= fixedLeft)
1049 || (startres >= fixedLeft && lastres < fixedLeft)
1050 || (startres > fixedRight && lastres <= fixedRight)
1051 || (startres <= fixedRight && lastres > fixedRight))
1057 if (fixedLeft > startres)
1059 fixedRight = fixedLeft - 1;
1062 else if (fixedRight < startres)
1064 fixedLeft = fixedRight;
1069 if (av.hasHiddenColumns)
1071 fixedColumns = true;
1072 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1073 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1075 if ((insertGap && startres > y1 && lastres < y1)
1076 || (!insertGap && startres < y2 && lastres > y2))
1082 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1083 // Selection spans a hidden region
1084 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1092 fixedRight = y2 - 1;
1099 Vector vseqs = sg.getSequences(av.hiddenRepSequences);
1100 int g, groupSize = vseqs.size();
1101 SequenceI[] groupSeqs = new SequenceI[groupSize];
1102 for (g = 0; g < groupSeqs.length; g++)
1104 groupSeqs[g] = (SequenceI) vseqs.elementAt(g);
1110 // If the user has selected the whole sequence, and is dragging to
1111 // the right, we can still extend the alignment and selectionGroup
1112 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1113 && sg.getEndRes() == av.alignment.getWidth() - 1)
1115 sg.setEndRes(av.alignment.getWidth() + startres - lastres);
1116 fixedRight = sg.getEndRes();
1119 // Is it valid with fixed columns??
1120 // Find the next gap before the end
1121 // of the visible region boundary
1122 boolean blank = false;
1123 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1127 for (g = 0; g < groupSize; g++)
1129 for (int j = 0; j < startres - lastres; j++)
1131 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1132 .getCharAt(fixedRight - j)))
1147 if (sg.getSize() == av.alignment.getHeight())
1149 if ((av.hasHiddenColumns && startres < av.getColumnSelection()
1150 .getHiddenBoundaryRight(startres)))
1156 int alWidth = av.alignment.getWidth();
1157 if (av.hasHiddenRows)
1159 int hwidth = av.alignment.getHiddenSequences().getWidth();
1160 if (hwidth > alWidth)
1165 // We can still insert gaps if the selectionGroup
1166 // contains all the sequences
1167 sg.setEndRes(sg.getEndRes() + startres - lastres);
1168 fixedRight = alWidth + startres - lastres;
1179 else if (!insertGap)
1181 // / Are we able to delete?
1182 // ie are all columns blank?
1184 for (g = 0; g < groupSize; g++)
1186 for (int j = startres; j < lastres; j++)
1188 if (groupSeqs[g].getLength() <= j)
1193 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1195 // Not a gap, block edit not valid
1205 // dragging to the right
1206 if (fixedColumns && fixedRight != -1)
1208 for (int j = lastres; j < startres; j++)
1210 insertChar(j, groupSeqs, fixedRight);
1215 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1216 startres, startres - lastres, av.alignment, true);
1221 // dragging to the left
1222 if (fixedColumns && fixedRight != -1)
1224 for (int j = lastres; j > startres; j--)
1226 deleteChar(startres, groupSeqs, fixedRight);
1231 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1232 startres, lastres - startres, av.alignment, true);
1238 // ///Editing a single sequence///////////
1242 // dragging to the right
1243 if (fixedColumns && fixedRight != -1)
1245 for (int j = lastres; j < startres; j++)
1247 insertChar(j, new SequenceI[]
1248 { seq }, fixedRight);
1253 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1254 { seq }, lastres, startres - lastres, av.alignment, true);
1259 // dragging to the left
1260 if (fixedColumns && fixedRight != -1)
1262 for (int j = lastres; j > startres; j--)
1264 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1269 deleteChar(startres, new SequenceI[]
1270 { seq }, fixedRight);
1275 // could be a keyboard edit trying to delete none gaps
1277 for (int m = startres; m < lastres; m++)
1279 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1288 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1289 { seq }, startres, max, av.alignment, true);
1296 seqCanvas.repaint();
1299 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1301 int blankColumn = fixedColumn;
1302 for (int s = 0; s < seq.length; s++)
1304 // Find the next gap before the end of the visible region boundary
1305 // If lastCol > j, theres a boundary after the gap insertion
1307 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1309 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1311 // Theres a space, so break and insert the gap
1316 if (blankColumn <= j)
1318 blankColumn = fixedColumn;
1324 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1325 av.alignment, true);
1327 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1, av.alignment,
1332 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1335 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1, av.alignment,
1338 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1339 av.alignment, true);
1348 public void mouseEntered(MouseEvent e)
1355 if (scrollThread != null)
1357 scrollThread.running = false;
1358 scrollThread = null;
1368 public void mouseExited(MouseEvent e)
1370 if (av.getWrapAlignment())
1377 scrollThread = new ScrollThread();
1381 public void mouseClicked(MouseEvent evt)
1383 SequenceI sequence = av.alignment.getSequenceAt(findSeq(evt));
1384 if (evt.getClickCount() > 1)
1386 if (av.getSelectionGroup().getSize() == 1
1387 && av.getSelectionGroup().getEndRes()
1388 - av.getSelectionGroup().getStartRes() < 2)
1390 av.setSelectionGroup(null);
1393 SequenceFeature[] features = findFeaturesAtRes(sequence
1394 .getDatasetSequence(), sequence.findPosition(findRes(evt)));
1396 if (features != null && features.length > 0)
1398 SearchResults highlight = new SearchResults();
1399 highlight.addResult(sequence, features[0].getBegin(), features[0]
1401 seqCanvas.highlightSearchResults(highlight);
1403 if (features != null && features.length > 0)
1405 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1406 { sequence }, features, false, ap);
1408 seqCanvas.highlightSearchResults(null);
1413 public void mouseWheelMoved(MouseWheelEvent e)
1416 if (e.getWheelRotation() > 0)
1432 public void doMousePressedDefineMode(MouseEvent evt)
1434 int res = findRes(evt);
1435 int seq = findSeq(evt);
1438 startWrapBlock = wrappedBlock;
1440 if (av.wrapAlignment && seq > av.alignment.getHeight())
1442 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1443 "Cannot edit annotations in wrapped view.",
1444 "Wrapped view - no edit", JOptionPane.WARNING_MESSAGE);
1448 if (seq < 0 || res < 0)
1453 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1455 if ((sequence == null) || (res > sequence.getLength()))
1460 stretchGroup = av.getSelectionGroup();
1462 if (stretchGroup == null)
1464 stretchGroup = av.alignment.findGroup(sequence);
1466 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1467 && (res < stretchGroup.getEndRes()))
1469 av.setSelectionGroup(stretchGroup);
1473 stretchGroup = null;
1476 else if (!stretchGroup.getSequences(null).contains(sequence)
1477 || (stretchGroup.getStartRes() > res)
1478 || (stretchGroup.getEndRes() < res))
1480 stretchGroup = null;
1482 SequenceGroup[] allGroups = av.alignment.findAllGroups(sequence);
1484 if (allGroups != null)
1486 for (int i = 0; i < allGroups.length; i++)
1488 if ((allGroups[i].getStartRes() <= res)
1489 && (allGroups[i].getEndRes() >= res))
1491 stretchGroup = allGroups[i];
1497 av.setSelectionGroup(stretchGroup);
1501 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1503 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence
1504 .getDatasetSequence(), sequence.findPosition(res));
1505 Vector links = new Vector();
1506 for (int i = 0; i < allFeatures.length; i++)
1508 if (allFeatures[i].links != null)
1510 for (int j = 0; j < allFeatures[i].links.size(); j++)
1512 links.addElement(allFeatures[i].links.elementAt(j));
1517 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1518 pop.show(this, evt.getX(), evt.getY());
1524 seqCanvas.cursorX = findRes(evt);
1525 seqCanvas.cursorY = findSeq(evt);
1526 seqCanvas.repaint();
1530 if (stretchGroup == null)
1532 // Only if left mouse button do we want to change group sizes
1534 // define a new group here
1535 SequenceGroup sg = new SequenceGroup();
1536 sg.setStartRes(res);
1538 sg.addSequence(sequence, false);
1539 av.setSelectionGroup(sg);
1543 if (av.getConservationSelected())
1545 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1549 if (av.getAbovePIDThreshold())
1551 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1554 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1556 // Edit end res position of selected group
1557 changeEndRes = true;
1559 else if ((stretchGroup != null)
1560 && (stretchGroup.getStartRes() == res))
1562 // Edit end res position of selected group
1563 changeStartRes = true;
1565 stretchGroup.getWidth();
1568 seqCanvas.repaint();
1577 public void doMouseReleasedDefineMode(MouseEvent evt)
1579 if (stretchGroup == null)
1584 if (stretchGroup.cs != null)
1586 if (stretchGroup.cs instanceof ClustalxColourScheme)
1588 ((ClustalxColourScheme) stretchGroup.cs).resetClustalX(stretchGroup
1589 .getSequences(av.hiddenRepSequences), stretchGroup
1593 if (stretchGroup.cs instanceof Blosum62ColourScheme
1594 || stretchGroup.cs instanceof PIDColourScheme
1595 || stretchGroup.cs.conservationApplied()
1596 || stretchGroup.cs.getThreshold() > 0)
1598 stretchGroup.recalcConservation();
1601 if (stretchGroup.cs.conservationApplied())
1603 SliderPanel.setConservationSlider(ap, stretchGroup.cs, stretchGroup
1608 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs, stretchGroup
1611 PaintRefresher.Refresh(this, av.getSequenceSetId());
1612 ap.paintAlignment(true);
1615 changeEndRes = false;
1616 changeStartRes = false;
1617 stretchGroup = null;
1627 public void doMouseDraggedDefineMode(MouseEvent evt)
1629 int res = findRes(evt);
1630 int y = findSeq(evt);
1632 if (wrappedBlock != startWrapBlock)
1637 if (stretchGroup == null)
1642 if (res >= av.alignment.getWidth())
1644 res = av.alignment.getWidth() - 1;
1647 if (stretchGroup.getEndRes() == res)
1649 // Edit end res position of selected group
1650 changeEndRes = true;
1652 else if (stretchGroup.getStartRes() == res)
1654 // Edit start res position of selected group
1655 changeStartRes = true;
1658 if (res < av.getStartRes())
1660 res = av.getStartRes();
1665 if (res > (stretchGroup.getStartRes() - 1))
1667 stretchGroup.setEndRes(res);
1670 else if (changeStartRes)
1672 if (res < (stretchGroup.getEndRes() + 1))
1674 stretchGroup.setStartRes(res);
1678 int dragDirection = 0;
1684 else if (y < oldSeq)
1689 while ((y != oldSeq) && (oldSeq > -1) && (y < av.alignment.getHeight()))
1691 // This routine ensures we don't skip any sequences, as the
1692 // selection is quite slow.
1693 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1695 oldSeq += dragDirection;
1702 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1704 if (stretchGroup.getSequences(null).contains(nextSeq))
1706 stretchGroup.deleteSequence(seq, false);
1712 stretchGroup.addSequence(seq, false);
1715 stretchGroup.addSequence(nextSeq, false);
1724 mouseDragging = true;
1726 if (scrollThread != null)
1728 scrollThread.setEvent(evt);
1731 seqCanvas.repaint();
1734 void scrollCanvas(MouseEvent evt)
1738 if (scrollThread != null)
1740 scrollThread.running = false;
1741 scrollThread = null;
1743 mouseDragging = false;
1747 if (scrollThread == null)
1749 scrollThread = new ScrollThread();
1752 mouseDragging = true;
1753 scrollThread.setEvent(evt);
1758 // this class allows scrolling off the bottom of the visible alignment
1759 class ScrollThread extends Thread
1763 boolean running = false;
1765 public ScrollThread()
1770 public void setEvent(MouseEvent e)
1775 public void stopScrolling()
1788 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1790 running = ap.scrollUp(true);
1793 if (mouseDragging && (evt.getY() >= getHeight())
1794 && (av.alignment.getHeight() > av.getEndSeq()))
1796 running = ap.scrollUp(false);
1799 if (mouseDragging && (evt.getX() < 0))
1801 running = ap.scrollRight(false);
1803 else if (mouseDragging && (evt.getX() >= getWidth()))
1805 running = ap.scrollRight(true);
1812 } catch (Exception ex)
1819 * modify current selection according to a received message.
1821 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1822 SelectionSource source)
1824 // TODO: fix this hack - source of messages is align viewport, but SeqPanel handles selection messages...
1829 // do we want to thread this ? (contention with seqsel and colsel locks, I suspect)
1830 boolean repaint=false;
1831 if (av.followSelection)
1833 if (av.selectionGroup==null || !av.isSelectionGroupChanged())
1835 SequenceGroup sgroup = (seqsel!=null) ? seqsel.intersect(av.alignment, (av.hasHiddenRows) ? av.hiddenRepSequences : null)
1837 if (sgroup!=null && sgroup.getSize()>0)
1839 av.setSelectionGroup(sgroup);
1841 av.setSelectionGroup(null);
1843 repaint = av.isSelectionGroupChanged();
1845 if (av.colSel==null || !av.isColSelChanged())
1847 // Check to see if the current selection is from a previous message
1848 if (colsel==null || colsel.size()==0)
1852 av.colSel = new ColumnSelection(colsel);
1854 repaint |= av.isColSelChanged();
1859 // probably finessing with multiple redraws here
1860 PaintRefresher.Refresh(this, av.getSequenceSetId());
1861 // ap.paintAlignment(false);