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.*;
28 import jalview.commands.*;
29 import jalview.datamodel.*;
30 import jalview.schemes.*;
31 import jalview.structure.*;
39 public class SeqPanel extends JPanel implements MouseListener,
40 MouseMotionListener, MouseWheelListener, SequenceListener,
45 public SeqCanvas seqCanvas;
48 public AlignmentPanel ap;
50 protected int lastres;
52 protected int startseq;
54 protected AlignViewport av;
56 ScrollThread scrollThread = null;
58 boolean mouseDragging = false;
60 boolean editingSeqs = false;
62 boolean groupEditing = false;
64 // ////////////////////////////////////////
65 // ///Everything below this is for defining the boundary of the rubberband
66 // ////////////////////////////////////////
69 boolean changeEndSeq = false;
71 boolean changeStartSeq = false;
73 boolean changeEndRes = false;
75 boolean changeStartRes = false;
77 SequenceGroup stretchGroup = null;
79 boolean remove = false;
83 boolean mouseWheelPressed = false;
85 StringBuffer keyboardNo1;
87 StringBuffer keyboardNo2;
89 java.net.URL linkImageURL;
91 StringBuffer tooltipText = new StringBuffer("<html>");
95 EditCommand editCommand;
97 StructureSelectionManager ssm;
100 * Creates a new SeqPanel object.
107 public SeqPanel(AlignViewport av, AlignmentPanel ap)
109 linkImageURL = getClass().getResource("/images/link.gif");
110 ToolTipManager.sharedInstance().registerComponent(this);
111 ToolTipManager.sharedInstance().setInitialDelay(0);
112 ToolTipManager.sharedInstance().setDismissDelay(10000);
114 setBackground(Color.white);
116 seqCanvas = new SeqCanvas(ap);
117 setLayout(new BorderLayout());
118 add(seqCanvas, BorderLayout.CENTER);
124 addMouseMotionListener(this);
125 addMouseListener(this);
126 addMouseWheelListener(this);
127 ssm = StructureSelectionManager.getStructureSelectionManager();
128 ssm.addStructureViewerListener(this);
129 ssm.addSelectionListener(this);
133 int startWrapBlock = -1;
135 int wrappedBlock = -1;
137 int findRes(MouseEvent evt)
142 if (av.wrapAlignment)
145 int hgap = av.charHeight;
146 if (av.scaleAboveWrapped)
148 hgap += av.charHeight;
151 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
152 + seqCanvas.getAnnotationHeight();
156 x -= seqCanvas.LABEL_WEST;
158 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
164 wrappedBlock = y / cHeight;
165 wrappedBlock += av.getStartRes() / cwidth;
167 res = wrappedBlock * cwidth + x / av.getCharWidth();
172 res = (x / av.getCharWidth()) + av.getStartRes();
175 if (av.hasHiddenColumns)
177 res = av.getColumnSelection().adjustForHiddenColumns(res);
184 int findSeq(MouseEvent evt)
189 if (av.wrapAlignment)
191 int hgap = av.charHeight;
192 if (av.scaleAboveWrapped)
194 hgap += av.charHeight;
197 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
198 + seqCanvas.getAnnotationHeight();
202 seq = Math.min((y % cHeight) / av.getCharHeight(), av.alignment
207 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(),
208 av.alignment.getHeight() - 1);
214 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
216 Vector tmp = new Vector();
217 SequenceFeature[] features = sequence.getSequenceFeatures();
218 if (features != null)
220 for (int i = 0; i < features.length; i++)
222 if (av.featuresDisplayed == null
223 || !av.featuresDisplayed.containsKey(features[i].getType()))
228 if (features[i].featureGroup != null
229 && seqCanvas.fr.featureGroups != null
230 && seqCanvas.fr.featureGroups
231 .containsKey(features[i].featureGroup)
232 && !((Boolean) seqCanvas.fr.featureGroups
233 .get(features[i].featureGroup)).booleanValue())
236 if ((features[i].getBegin() <= res)
237 && (features[i].getEnd() >= res))
239 tmp.addElement(features[i]);
244 features = new SequenceFeature[tmp.size()];
245 tmp.copyInto(features);
252 if (editCommand != null && editCommand.getSize() > 0)
254 ap.alignFrame.addHistoryItem(editCommand);
255 av.firePropertyChange("alignment", null, av.getAlignment()
262 groupEditing = false;
270 seqCanvas.cursorY = getKeyboardNo1() - 1;
274 void setCursorColumn()
276 seqCanvas.cursorX = getKeyboardNo1() - 1;
280 void setCursorRowAndColumn()
282 if (keyboardNo2 == null)
284 keyboardNo2 = new StringBuffer();
288 seqCanvas.cursorX = getKeyboardNo1() - 1;
289 seqCanvas.cursorY = getKeyboardNo2() - 1;
294 void setCursorPosition()
296 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
299 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1() - 1);
303 void moveCursor(int dx, int dy)
305 seqCanvas.cursorX += dx;
306 seqCanvas.cursorY += dy;
307 if (av.hasHiddenColumns && !av.colSel.isVisible(seqCanvas.cursorX))
309 int original = seqCanvas.cursorX - dx;
310 int maxWidth = av.alignment.getWidth();
312 while (!av.colSel.isVisible(seqCanvas.cursorX)
313 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
315 seqCanvas.cursorX += dx;
318 if (seqCanvas.cursorX >= maxWidth
319 || !av.colSel.isVisible(seqCanvas.cursorX))
321 seqCanvas.cursorX = original;
328 void scrollToVisible()
330 if (seqCanvas.cursorX < 0)
332 seqCanvas.cursorX = 0;
334 else if (seqCanvas.cursorX > av.alignment.getWidth() - 1)
336 seqCanvas.cursorX = av.alignment.getWidth() - 1;
339 if (seqCanvas.cursorY < 0)
341 seqCanvas.cursorY = 0;
343 else if (seqCanvas.cursorY > av.alignment.getHeight() - 1)
345 seqCanvas.cursorY = av.alignment.getHeight() - 1;
349 if (av.wrapAlignment)
351 ap.scrollToWrappedVisible(seqCanvas.cursorX);
355 while (seqCanvas.cursorY < av.startSeq)
359 while (seqCanvas.cursorY + 1 > av.endSeq)
363 if (!av.wrapAlignment)
365 while (seqCanvas.cursorX < av.colSel
366 .adjustForHiddenColumns(av.startRes))
368 if (!ap.scrollRight(false))
373 while (seqCanvas.cursorX > av.colSel
374 .adjustForHiddenColumns(av.endRes))
376 if (!ap.scrollRight(true))
383 setStatusMessage(av.alignment.getSequenceAt(seqCanvas.cursorY),
384 seqCanvas.cursorX, seqCanvas.cursorY);
389 void setSelectionAreaAtCursor(boolean topLeft)
391 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
394 if (av.getSelectionGroup() != null)
396 SequenceGroup sg = av.selectionGroup;
397 // Find the top and bottom of this group
398 int min = av.alignment.getHeight(), max = 0;
399 for (int i = 0; i < sg.getSize(); i++)
401 int index = av.alignment.findIndex(sg.getSequenceAt(i));
416 sg.setStartRes(seqCanvas.cursorX);
417 if (sg.getEndRes() < seqCanvas.cursorX)
419 sg.setEndRes(seqCanvas.cursorX);
422 min = seqCanvas.cursorY;
426 sg.setEndRes(seqCanvas.cursorX);
427 if (sg.getStartRes() > seqCanvas.cursorX)
429 sg.setStartRes(seqCanvas.cursorX);
432 max = seqCanvas.cursorY + 1;
437 // Only the user can do this
438 av.setSelectionGroup(null);
442 // Now add any sequences between min and max
443 sg.getSequences(null).clear();
444 for (int i = min; i < max; i++)
446 sg.addSequence(av.alignment.getSequenceAt(i), false);
451 if (av.getSelectionGroup() == null)
453 SequenceGroup sg = new SequenceGroup();
454 sg.setStartRes(seqCanvas.cursorX);
455 sg.setEndRes(seqCanvas.cursorX);
456 sg.addSequence(sequence, false);
457 av.setSelectionGroup(sg);
460 ap.paintAlignment(false);
464 void insertGapAtCursor(boolean group)
466 groupEditing = group;
467 startseq = seqCanvas.cursorY;
468 lastres = seqCanvas.cursorX;
469 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
473 void deleteGapAtCursor(boolean group)
475 groupEditing = group;
476 startseq = seqCanvas.cursorY;
477 lastres = seqCanvas.cursorX + getKeyboardNo1();
478 editSequence(false, seqCanvas.cursorX);
482 void numberPressed(char value)
484 if (keyboardNo1 == null)
486 keyboardNo1 = new StringBuffer();
489 if (keyboardNo2 != null)
491 keyboardNo2.append(value);
495 keyboardNo1.append(value);
501 if (keyboardNo1 == null)
505 int value = Integer.parseInt(keyboardNo1.toString());
513 if (keyboardNo2 == null)
517 int value = Integer.parseInt(keyboardNo2.toString());
529 public void mouseReleased(MouseEvent evt)
531 mouseDragging = false;
532 mouseWheelPressed = false;
536 doMouseReleasedDefineMode(evt);
549 public void mousePressed(MouseEvent evt)
551 lastMousePress = evt.getPoint();
553 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
555 mouseWheelPressed = true;
559 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
561 if (evt.isAltDown() || evt.isControlDown())
569 doMousePressedDefineMode(evt);
573 int seq = findSeq(evt);
574 int res = findRes(evt);
576 if (seq < 0 || res < 0)
581 if ((seq < av.getAlignment().getHeight())
582 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
598 public void mouseOverSequence(SequenceI sequence, int index, int pos)
600 String tmp = sequence.hashCode() + " " + index + " " + pos;
602 if (lastMessage == null || !lastMessage.equals(tmp))
604 // System.err.println("mouseOver Sequence: "+tmp);
605 ssm.mouseOverSequence(sequence, index, pos);
610 public void highlightSequence(SearchResults results)
612 if (av.followHighlight)
614 ap.scrollToPosition(results, false);
616 seqCanvas.highlightSearchResults(results);
619 public void updateColours(SequenceI seq, int index)
621 System.out.println("update the seqPanel colours");
631 public void mouseMoved(MouseEvent evt)
635 // This is because MacOSX creates a mouseMoved
636 // If control is down, other platforms will not.
640 int res = findRes(evt);
641 int seq = findSeq(evt);
643 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
648 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
650 if (res >= sequence.getLength())
655 pos = setStatusMessage(sequence, res, seq);
656 if (ssm != null && pos > -1)
657 mouseOverSequence(sequence, res, pos);
659 tooltipText.setLength(6); // Cuts the buffer back to <html>
661 SequenceGroup[] groups = av.alignment.findAllGroups(sequence);
664 for (int g = 0; g < groups.length; g++)
666 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
668 if (tooltipText.length() > 6)
670 tooltipText.append("<br>");
673 if (!groups[g].getName().startsWith("JTreeGroup")
674 && !groups[g].getName().startsWith("JGroup"))
676 tooltipText.append(groups[g].getName());
679 if (groups[g].getDescription() != null)
681 tooltipText.append(": " + groups[g].getDescription());
687 // use aa to see if the mouse pointer is on a
688 if (av.showSequenceFeatures)
691 SequenceFeature[] features = findFeaturesAtRes(sequence
692 .getDatasetSequence(), rpos = sequence.findPosition(res));
693 appendFeatures(tooltipText, linkImageURL.toString(), rpos, features);
695 if (tooltipText.length() == 6) // <html></html>
697 setToolTipText(null);
702 tooltipText.append("</html>");
703 if (lastTooltip == null
704 || !lastTooltip.equals(tooltipText.toString()))
706 setToolTipText(tooltipText.toString());
707 lastTooltip = tooltipText.toString();
717 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
719 public Point getToolTipLocation(MouseEvent event)
721 int x = event.getX(), w = getWidth();
722 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
724 Point p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
725 event.getX() + wdth, event.getY() - 20)
728 * TODO: try to modify position region is not obcured by tooltip
734 * appends the features at rpos to the given stringbuffer ready for display in
737 * @param tooltipText2
738 * @param linkImageURL
741 * TODO refactor to Jalview 'utilities' somehow.
743 public static void appendFeatures(StringBuffer tooltipText2,
744 String linkImageURL, int rpos, SequenceFeature[] features)
747 if (features != null)
749 for (int i = 0; i < features.length; i++)
751 if (features[i].getType().equals("disulfide bond"))
753 if (features[i].getBegin() == rpos
754 || features[i].getEnd() == rpos)
756 if (tooltipText2.length() > 6)
758 tooltipText2.append("<br>");
760 tooltipText2.append("disulfide bond " + features[i].getBegin()
761 + ":" + features[i].getEnd());
762 if (features[i].links != null)
764 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
770 if (tooltipText2.length() > 6)
772 tooltipText2.append("<br>");
774 // TODO: remove this hack to display link only features
775 boolean linkOnly = features[i].getValue("linkonly") != null;
778 tooltipText2.append(features[i].getType() + " ");
781 // we are marking a positional feature
782 tooltipText2.append(features[i].begin);
784 if (features[i].begin != features[i].end)
786 tooltipText2.append(" " + features[i].end);
789 if (features[i].getDescription() != null
790 && !features[i].description.equals(features[i]
793 tmpString = features[i].getDescription();
794 int startTag = tmpString.toUpperCase().indexOf("<HTML>");
797 tmpString = tmpString.substring(startTag + 6);
799 int endTag = tmpString.toUpperCase().indexOf("</BODY>");
802 tmpString = tmpString.substring(0, endTag);
804 endTag = tmpString.toUpperCase().indexOf("</HTML>");
807 tmpString = tmpString.substring(0, endTag);
812 tooltipText2.append("; " + tmpString);
816 if (tmpString.indexOf("<") > -1
817 || tmpString.indexOf(">") > -1)
819 // The description does not specify html is to
820 // be used, so we must remove < > symbols
821 tmpString = tmpString.replaceAll("<", "<");
822 tmpString = tmpString.replaceAll(">", ">");
824 tooltipText2.append("; ");
825 tooltipText2.append(tmpString);
830 tooltipText2.append("; " + tmpString);
834 // TODO: check min/max range for this feature type to decide if
835 // score should be shown
836 if (features[i].getScore() != Float.NaN)
838 tooltipText2.append(" Score=" + features[i].getScore());
840 if (features[i].getValue("status") != null)
842 String status = features[i].getValue("status").toString();
843 if (status.length() > 0)
845 tooltipText2.append("; (" + features[i].getValue("status")
850 if (features[i].links != null)
852 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
863 * Set status message in alignment panel
866 * aligned sequence object
870 * index of sequence in alignment
871 * @return position of res in sequence
873 int setStatusMessage(SequenceI sequence, int res, int seq)
876 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
877 + sequence.getName());
880 if (av.alignment.isNucleotide())
882 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
886 text.append(" Nucleotide: ");
891 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
894 text.append(" Residue: ");
900 pos = sequence.findPosition(res);
903 text.append(obj + " (" + pos + ")");
906 ap.alignFrame.statusBar.setText(text.toString());
916 public void mouseDragged(MouseEvent evt)
918 if (mouseWheelPressed)
920 int oldWidth = av.charWidth;
922 // Which is bigger, left-right or up-down?
923 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
925 - lastMousePress.getX()))
927 int fontSize = av.font.getSize();
929 if (evt.getY() < lastMousePress.getY())
933 else if (evt.getY() > lastMousePress.getY())
944 .setFont(new Font(av.font.getName(), av.font.getStyle(),
946 av.charWidth = oldWidth;
951 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
955 else if (evt.getX() > lastMousePress.getX())
960 ap.paintAlignment(false);
963 FontMetrics fm = getFontMetrics(av.getFont());
964 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
966 lastMousePress = evt.getPoint();
973 doMouseDraggedDefineMode(evt);
977 int res = findRes(evt);
984 if ((lastres == -1) || (lastres == res))
989 if ((res < av.getAlignment().getWidth()) && (res < lastres))
991 // dragLeft, delete gap
992 editSequence(false, res);
996 editSequence(true, res);
999 mouseDragging = true;
1000 if (scrollThread != null)
1002 scrollThread.setEvent(evt);
1006 synchronized void editSequence(boolean insertGap, int startres)
1009 int fixedRight = -1;
1010 boolean fixedColumns = false;
1011 SequenceGroup sg = av.getSelectionGroup();
1013 SequenceI seq = av.alignment.getSequenceAt(startseq);
1015 // No group, but the sequence may represent a group
1016 if (!groupEditing && av.hasHiddenRows)
1018 if (av.hiddenRepSequences != null
1019 && av.hiddenRepSequences.containsKey(seq))
1021 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1022 groupEditing = true;
1026 StringBuffer message = new StringBuffer();
1029 message.append("Edit group:");
1030 if (editCommand == null)
1032 editCommand = new EditCommand("Edit Group");
1037 message.append("Edit sequence: " + seq.getName());
1038 String label = seq.getName();
1039 if (label.length() > 10)
1041 label = label.substring(0, 10);
1043 if (editCommand == null)
1045 editCommand = new EditCommand("Edit " + label);
1051 message.append(" insert ");
1055 message.append(" delete ");
1058 message.append(Math.abs(startres - lastres) + " gaps.");
1059 ap.alignFrame.statusBar.setText(message.toString());
1061 // Are we editing within a selection group?
1063 || (sg != null && sg.getSequences(av.hiddenRepSequences)
1066 fixedColumns = true;
1068 // sg might be null as the user may only see 1 sequence,
1069 // but the sequence represents a group
1072 if (av.hiddenRepSequences == null
1073 || !av.hiddenRepSequences.containsKey(seq))
1078 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1081 fixedLeft = sg.getStartRes();
1082 fixedRight = sg.getEndRes();
1084 if ((startres < fixedLeft && lastres >= fixedLeft)
1085 || (startres >= fixedLeft && lastres < fixedLeft)
1086 || (startres > fixedRight && lastres <= fixedRight)
1087 || (startres <= fixedRight && lastres > fixedRight))
1093 if (fixedLeft > startres)
1095 fixedRight = fixedLeft - 1;
1098 else if (fixedRight < startres)
1100 fixedLeft = fixedRight;
1105 if (av.hasHiddenColumns)
1107 fixedColumns = true;
1108 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1109 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1111 if ((insertGap && startres > y1 && lastres < y1)
1112 || (!insertGap && startres < y2 && lastres > y2))
1118 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1119 // Selection spans a hidden region
1120 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1128 fixedRight = y2 - 1;
1135 Vector vseqs = sg.getSequences(av.hiddenRepSequences);
1136 int g, groupSize = vseqs.size();
1137 SequenceI[] groupSeqs = new SequenceI[groupSize];
1138 for (g = 0; g < groupSeqs.length; g++)
1140 groupSeqs[g] = (SequenceI) vseqs.elementAt(g);
1146 // If the user has selected the whole sequence, and is dragging to
1147 // the right, we can still extend the alignment and selectionGroup
1148 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1149 && sg.getEndRes() == av.alignment.getWidth() - 1)
1151 sg.setEndRes(av.alignment.getWidth() + startres - lastres);
1152 fixedRight = sg.getEndRes();
1155 // Is it valid with fixed columns??
1156 // Find the next gap before the end
1157 // of the visible region boundary
1158 boolean blank = false;
1159 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1163 for (g = 0; g < groupSize; g++)
1165 for (int j = 0; j < startres - lastres; j++)
1167 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1168 .getCharAt(fixedRight - j)))
1183 if (sg.getSize() == av.alignment.getHeight())
1185 if ((av.hasHiddenColumns && startres < av.getColumnSelection()
1186 .getHiddenBoundaryRight(startres)))
1192 int alWidth = av.alignment.getWidth();
1193 if (av.hasHiddenRows)
1195 int hwidth = av.alignment.getHiddenSequences().getWidth();
1196 if (hwidth > alWidth)
1201 // We can still insert gaps if the selectionGroup
1202 // contains all the sequences
1203 sg.setEndRes(sg.getEndRes() + startres - lastres);
1204 fixedRight = alWidth + startres - lastres;
1215 else if (!insertGap)
1217 // / Are we able to delete?
1218 // ie are all columns blank?
1220 for (g = 0; g < groupSize; g++)
1222 for (int j = startres; j < lastres; j++)
1224 if (groupSeqs[g].getLength() <= j)
1229 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1231 // Not a gap, block edit not valid
1241 // dragging to the right
1242 if (fixedColumns && fixedRight != -1)
1244 for (int j = lastres; j < startres; j++)
1246 insertChar(j, groupSeqs, fixedRight);
1251 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1252 startres, startres - lastres, av.alignment, true);
1257 // dragging to the left
1258 if (fixedColumns && fixedRight != -1)
1260 for (int j = lastres; j > startres; j--)
1262 deleteChar(startres, groupSeqs, fixedRight);
1267 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1268 startres, lastres - startres, av.alignment, true);
1274 // ///Editing a single sequence///////////
1278 // dragging to the right
1279 if (fixedColumns && fixedRight != -1)
1281 for (int j = lastres; j < startres; j++)
1283 insertChar(j, new SequenceI[]
1284 { seq }, fixedRight);
1289 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1290 { seq }, lastres, startres - lastres, av.alignment, true);
1295 // dragging to the left
1296 if (fixedColumns && fixedRight != -1)
1298 for (int j = lastres; j > startres; j--)
1300 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1305 deleteChar(startres, new SequenceI[]
1306 { seq }, fixedRight);
1311 // could be a keyboard edit trying to delete none gaps
1313 for (int m = startres; m < lastres; m++)
1315 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1324 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1325 { seq }, startres, max, av.alignment, true);
1332 seqCanvas.repaint();
1335 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1337 int blankColumn = fixedColumn;
1338 for (int s = 0; s < seq.length; s++)
1340 // Find the next gap before the end of the visible region boundary
1341 // If lastCol > j, theres a boundary after the gap insertion
1343 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1345 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1347 // Theres a space, so break and insert the gap
1352 if (blankColumn <= j)
1354 blankColumn = fixedColumn;
1360 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1361 av.alignment, true);
1363 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1, av.alignment,
1368 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1371 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1, av.alignment,
1374 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1375 av.alignment, true);
1384 public void mouseEntered(MouseEvent e)
1391 if (scrollThread != null)
1393 scrollThread.running = false;
1394 scrollThread = null;
1404 public void mouseExited(MouseEvent e)
1406 if (av.getWrapAlignment())
1413 scrollThread = new ScrollThread();
1417 public void mouseClicked(MouseEvent evt)
1419 SequenceI sequence = av.alignment.getSequenceAt(findSeq(evt));
1420 if (evt.getClickCount() > 1)
1422 if (av.getSelectionGroup().getSize() == 1
1423 && av.getSelectionGroup().getEndRes()
1424 - av.getSelectionGroup().getStartRes() < 2)
1426 av.setSelectionGroup(null);
1429 SequenceFeature[] features = findFeaturesAtRes(sequence
1430 .getDatasetSequence(), sequence.findPosition(findRes(evt)));
1432 if (features != null && features.length > 0)
1434 SearchResults highlight = new SearchResults();
1435 highlight.addResult(sequence, features[0].getBegin(), features[0]
1437 seqCanvas.highlightSearchResults(highlight);
1439 if (features != null && features.length > 0)
1441 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1442 { sequence }, features, false, ap);
1444 seqCanvas.highlightSearchResults(null);
1449 public void mouseWheelMoved(MouseWheelEvent e)
1452 if (e.getWheelRotation() > 0)
1468 public void doMousePressedDefineMode(MouseEvent evt)
1470 int res = findRes(evt);
1471 int seq = findSeq(evt);
1474 startWrapBlock = wrappedBlock;
1476 if (av.wrapAlignment && seq > av.alignment.getHeight())
1478 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1479 "Cannot edit annotations in wrapped view.",
1480 "Wrapped view - no edit", JOptionPane.WARNING_MESSAGE);
1484 if (seq < 0 || res < 0)
1489 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1491 if ((sequence == null) || (res > sequence.getLength()))
1496 stretchGroup = av.getSelectionGroup();
1498 if (stretchGroup == null)
1500 stretchGroup = av.alignment.findGroup(sequence);
1502 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1503 && (res < stretchGroup.getEndRes()))
1505 av.setSelectionGroup(stretchGroup);
1509 stretchGroup = null;
1512 else if (!stretchGroup.getSequences(null).contains(sequence)
1513 || (stretchGroup.getStartRes() > res)
1514 || (stretchGroup.getEndRes() < res))
1516 stretchGroup = null;
1518 SequenceGroup[] allGroups = av.alignment.findAllGroups(sequence);
1520 if (allGroups != null)
1522 for (int i = 0; i < allGroups.length; i++)
1524 if ((allGroups[i].getStartRes() <= res)
1525 && (allGroups[i].getEndRes() >= res))
1527 stretchGroup = allGroups[i];
1533 av.setSelectionGroup(stretchGroup);
1537 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1539 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence
1540 .getDatasetSequence(), sequence.findPosition(res));
1541 Vector links = new Vector();
1542 for (int i = 0; i < allFeatures.length; i++)
1544 if (allFeatures[i].links != null)
1546 for (int j = 0; j < allFeatures[i].links.size(); j++)
1548 links.addElement(allFeatures[i].links.elementAt(j));
1553 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1554 pop.show(this, evt.getX(), evt.getY());
1560 seqCanvas.cursorX = findRes(evt);
1561 seqCanvas.cursorY = findSeq(evt);
1562 seqCanvas.repaint();
1566 if (stretchGroup == null)
1568 // Only if left mouse button do we want to change group sizes
1570 // define a new group here
1571 SequenceGroup sg = new SequenceGroup();
1572 sg.setStartRes(res);
1574 sg.addSequence(sequence, false);
1575 av.setSelectionGroup(sg);
1579 if (av.getConservationSelected())
1581 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1585 if (av.getAbovePIDThreshold())
1587 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1590 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1592 // Edit end res position of selected group
1593 changeEndRes = true;
1595 else if ((stretchGroup != null)
1596 && (stretchGroup.getStartRes() == res))
1598 // Edit end res position of selected group
1599 changeStartRes = true;
1601 stretchGroup.getWidth();
1604 seqCanvas.repaint();
1613 public void doMouseReleasedDefineMode(MouseEvent evt)
1615 if (stretchGroup == null)
1620 if (stretchGroup.cs != null)
1622 if (stretchGroup.cs instanceof ClustalxColourScheme)
1624 ((ClustalxColourScheme) stretchGroup.cs).resetClustalX(stretchGroup
1625 .getSequences(av.hiddenRepSequences), stretchGroup
1629 if (stretchGroup.cs instanceof Blosum62ColourScheme
1630 || stretchGroup.cs instanceof PIDColourScheme
1631 || stretchGroup.cs.conservationApplied()
1632 || stretchGroup.cs.getThreshold() > 0)
1634 stretchGroup.recalcConservation();
1637 if (stretchGroup.cs.conservationApplied())
1639 SliderPanel.setConservationSlider(ap, stretchGroup.cs, stretchGroup
1644 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs, stretchGroup
1647 PaintRefresher.Refresh(this, av.getSequenceSetId());
1648 ap.paintAlignment(true);
1651 changeEndRes = false;
1652 changeStartRes = false;
1653 stretchGroup = null;
1663 public void doMouseDraggedDefineMode(MouseEvent evt)
1665 int res = findRes(evt);
1666 int y = findSeq(evt);
1668 if (wrappedBlock != startWrapBlock)
1673 if (stretchGroup == null)
1678 if (res >= av.alignment.getWidth())
1680 res = av.alignment.getWidth() - 1;
1683 if (stretchGroup.getEndRes() == res)
1685 // Edit end res position of selected group
1686 changeEndRes = true;
1688 else if (stretchGroup.getStartRes() == res)
1690 // Edit start res position of selected group
1691 changeStartRes = true;
1694 if (res < av.getStartRes())
1696 res = av.getStartRes();
1701 if (res > (stretchGroup.getStartRes() - 1))
1703 stretchGroup.setEndRes(res);
1706 else if (changeStartRes)
1708 if (res < (stretchGroup.getEndRes() + 1))
1710 stretchGroup.setStartRes(res);
1714 int dragDirection = 0;
1720 else if (y < oldSeq)
1725 while ((y != oldSeq) && (oldSeq > -1) && (y < av.alignment.getHeight()))
1727 // This routine ensures we don't skip any sequences, as the
1728 // selection is quite slow.
1729 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1731 oldSeq += dragDirection;
1738 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1740 if (stretchGroup.getSequences(null).contains(nextSeq))
1742 stretchGroup.deleteSequence(seq, false);
1748 stretchGroup.addSequence(seq, false);
1751 stretchGroup.addSequence(nextSeq, false);
1760 mouseDragging = true;
1762 if (scrollThread != null)
1764 scrollThread.setEvent(evt);
1767 seqCanvas.repaint();
1770 void scrollCanvas(MouseEvent evt)
1774 if (scrollThread != null)
1776 scrollThread.running = false;
1777 scrollThread = null;
1779 mouseDragging = false;
1783 if (scrollThread == null)
1785 scrollThread = new ScrollThread();
1788 mouseDragging = true;
1789 scrollThread.setEvent(evt);
1794 // this class allows scrolling off the bottom of the visible alignment
1795 class ScrollThread extends Thread
1799 boolean running = false;
1801 public ScrollThread()
1806 public void setEvent(MouseEvent e)
1811 public void stopScrolling()
1824 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1826 running = ap.scrollUp(true);
1829 if (mouseDragging && (evt.getY() >= getHeight())
1830 && (av.alignment.getHeight() > av.getEndSeq()))
1832 running = ap.scrollUp(false);
1835 if (mouseDragging && (evt.getX() < 0))
1837 running = ap.scrollRight(false);
1839 else if (mouseDragging && (evt.getX() >= getWidth()))
1841 running = ap.scrollRight(true);
1848 } catch (Exception ex)
1856 * modify current selection according to a received message.
1858 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1859 SelectionSource source)
1861 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1862 // handles selection messages...
1867 // do we want to thread this ? (contention with seqsel and colsel locks, I
1869 boolean repaint = false;
1870 if (av.followSelection)
1872 if (av.selectionGroup == null || !av.isSelectionGroupChanged())
1874 SequenceGroup sgroup = null;
1877 seqsel = seqsel.intersect(av.alignment,
1878 (av.hasHiddenRows) ? av.hiddenRepSequences : null);
1880 if (sgroup != null && sgroup.getSize() > 0)
1882 av.setSelectionGroup(sgroup);
1886 av.setSelectionGroup(null);
1888 repaint = av.isSelectionGroupChanged();
1890 if (av.colSel == null || !av.isColSelChanged())
1892 // Check to see if the current selection is from a previous message
1893 if (colsel == null || colsel.size() == 0)
1899 av.colSel = new ColumnSelection(colsel);
1901 repaint |= av.isColSelChanged();
1906 // probably finessing with multiple redraws here
1907 PaintRefresher.Refresh(this, av.getSequenceSetId());
1908 // ap.paintAlignment(false);