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();
715 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
717 public Point getToolTipLocation(MouseEvent event)
719 int x=event.getX(),w=getWidth();
720 int wdth = (w-x<200) ? -(w/2) : 5; // switch sides when tooltip is too close to edge
721 Point p = (tooltipText!=null && tooltipText.length()>6) ?
722 new Point(event.getX()+wdth, event.getY()-20) : null;
723 /* TODO: try to modify position region is not obcured by tooltip
729 * appends the features at rpos to the given stringbuffer ready for display in
732 * @param tooltipText2
733 * @param linkImageURL
736 * TODO refactor to Jalview 'utilities' somehow.
738 public static void appendFeatures(StringBuffer tooltipText2,
739 String linkImageURL, int rpos, SequenceFeature[] features)
742 if (features != null)
744 for (int i = 0; i < features.length; i++)
746 if (features[i].getType().equals("disulfide bond"))
748 if (features[i].getBegin() == rpos
749 || features[i].getEnd() == rpos)
751 if (tooltipText2.length() > 6)
753 tooltipText2.append("<br>");
755 tooltipText2.append("disulfide bond " + features[i].getBegin()
756 + ":" + features[i].getEnd());
757 if (features[i].links != null)
759 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
765 if (tooltipText2.length() > 6)
767 tooltipText2.append("<br>");
769 // TODO: remove this hack to display link only features
770 boolean linkOnly = features[i].getValue("linkonly") != null;
773 tooltipText2.append(features[i].getType() + " ");
776 // we are marking a positional feature
777 tooltipText2.append(features[i].begin);
779 if (features[i].begin != features[i].end)
781 tooltipText2.append(" " + features[i].end);
784 if (features[i].getDescription() != null
785 && !features[i].description.equals(features[i]
788 tmpString = features[i].getDescription();
789 int startTag = tmpString.toUpperCase().indexOf("<HTML>");
792 tmpString = tmpString.substring(startTag + 6);
794 int endTag = tmpString.toUpperCase().indexOf("</BODY>");
797 tmpString = tmpString.substring(0, endTag);
799 endTag = tmpString.toUpperCase().indexOf("</HTML>");
802 tmpString = tmpString.substring(0, endTag);
807 tooltipText2.append("; " + tmpString);
811 if (tmpString.indexOf("<") > -1
812 || tmpString.indexOf(">") > -1)
814 // The description does not specify html is to
815 // be used, so we must remove < > symbols
816 tmpString = tmpString.replaceAll("<", "<");
817 tmpString = tmpString.replaceAll(">", ">");
819 tooltipText2.append("; ");
820 tooltipText2.append(tmpString);
825 tooltipText2.append("; " + tmpString);
829 // TODO: check min/max range for this feature type to decide if
830 // score should be shown
831 if (features[i].getScore() != Float.NaN)
833 tooltipText2.append(" Score=" + features[i].getScore());
835 if (features[i].getValue("status") != null)
837 String status = features[i].getValue("status").toString();
838 if (status.length() > 0)
840 tooltipText2.append("; (" + features[i].getValue("status")
845 if (features[i].links != null)
847 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
858 * Set status message in alignment panel
861 * aligned sequence object
865 * index of sequence in alignment
866 * @return position of res in sequence
868 int setStatusMessage(SequenceI sequence, int res, int seq)
871 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
872 + sequence.getName());
875 if (av.alignment.isNucleotide())
877 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
881 text.append(" Nucleotide: ");
886 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
889 text.append(" Residue: ");
895 pos = sequence.findPosition(res);
898 text.append(obj + " (" + pos + ")");
901 ap.alignFrame.statusBar.setText(text.toString());
911 public void mouseDragged(MouseEvent evt)
913 if (mouseWheelPressed)
915 int oldWidth = av.charWidth;
917 // Which is bigger, left-right or up-down?
918 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
920 - lastMousePress.getX()))
922 int fontSize = av.font.getSize();
924 if (evt.getY() < lastMousePress.getY())
928 else if (evt.getY() > lastMousePress.getY())
939 .setFont(new Font(av.font.getName(), av.font.getStyle(),
941 av.charWidth = oldWidth;
946 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
950 else if (evt.getX() > lastMousePress.getX())
955 ap.paintAlignment(false);
958 FontMetrics fm = getFontMetrics(av.getFont());
959 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
961 lastMousePress = evt.getPoint();
968 doMouseDraggedDefineMode(evt);
972 int res = findRes(evt);
979 if ((lastres == -1) || (lastres == res))
984 if ((res < av.getAlignment().getWidth()) && (res < lastres))
986 // dragLeft, delete gap
987 editSequence(false, res);
991 editSequence(true, res);
994 mouseDragging = true;
995 if (scrollThread != null)
997 scrollThread.setEvent(evt);
1001 synchronized void editSequence(boolean insertGap, int startres)
1004 int fixedRight = -1;
1005 boolean fixedColumns = false;
1006 SequenceGroup sg = av.getSelectionGroup();
1008 SequenceI seq = av.alignment.getSequenceAt(startseq);
1010 // No group, but the sequence may represent a group
1011 if (!groupEditing && av.hasHiddenRows)
1013 if (av.hiddenRepSequences != null
1014 && av.hiddenRepSequences.containsKey(seq))
1016 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1017 groupEditing = true;
1021 StringBuffer message = new StringBuffer();
1024 message.append("Edit group:");
1025 if (editCommand == null)
1027 editCommand = new EditCommand("Edit Group");
1032 message.append("Edit sequence: " + seq.getName());
1033 String label = seq.getName();
1034 if (label.length() > 10)
1036 label = label.substring(0, 10);
1038 if (editCommand == null)
1040 editCommand = new EditCommand("Edit " + label);
1046 message.append(" insert ");
1050 message.append(" delete ");
1053 message.append(Math.abs(startres - lastres) + " gaps.");
1054 ap.alignFrame.statusBar.setText(message.toString());
1056 // Are we editing within a selection group?
1058 || (sg != null && sg.getSequences(av.hiddenRepSequences)
1061 fixedColumns = true;
1063 // sg might be null as the user may only see 1 sequence,
1064 // but the sequence represents a group
1067 if (av.hiddenRepSequences == null
1068 || !av.hiddenRepSequences.containsKey(seq))
1073 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1076 fixedLeft = sg.getStartRes();
1077 fixedRight = sg.getEndRes();
1079 if ((startres < fixedLeft && lastres >= fixedLeft)
1080 || (startres >= fixedLeft && lastres < fixedLeft)
1081 || (startres > fixedRight && lastres <= fixedRight)
1082 || (startres <= fixedRight && lastres > fixedRight))
1088 if (fixedLeft > startres)
1090 fixedRight = fixedLeft - 1;
1093 else if (fixedRight < startres)
1095 fixedLeft = fixedRight;
1100 if (av.hasHiddenColumns)
1102 fixedColumns = true;
1103 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1104 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1106 if ((insertGap && startres > y1 && lastres < y1)
1107 || (!insertGap && startres < y2 && lastres > y2))
1113 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1114 // Selection spans a hidden region
1115 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1123 fixedRight = y2 - 1;
1130 Vector vseqs = sg.getSequences(av.hiddenRepSequences);
1131 int g, groupSize = vseqs.size();
1132 SequenceI[] groupSeqs = new SequenceI[groupSize];
1133 for (g = 0; g < groupSeqs.length; g++)
1135 groupSeqs[g] = (SequenceI) vseqs.elementAt(g);
1141 // If the user has selected the whole sequence, and is dragging to
1142 // the right, we can still extend the alignment and selectionGroup
1143 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1144 && sg.getEndRes() == av.alignment.getWidth() - 1)
1146 sg.setEndRes(av.alignment.getWidth() + startres - lastres);
1147 fixedRight = sg.getEndRes();
1150 // Is it valid with fixed columns??
1151 // Find the next gap before the end
1152 // of the visible region boundary
1153 boolean blank = false;
1154 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1158 for (g = 0; g < groupSize; g++)
1160 for (int j = 0; j < startres - lastres; j++)
1162 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1163 .getCharAt(fixedRight - j)))
1178 if (sg.getSize() == av.alignment.getHeight())
1180 if ((av.hasHiddenColumns && startres < av.getColumnSelection()
1181 .getHiddenBoundaryRight(startres)))
1187 int alWidth = av.alignment.getWidth();
1188 if (av.hasHiddenRows)
1190 int hwidth = av.alignment.getHiddenSequences().getWidth();
1191 if (hwidth > alWidth)
1196 // We can still insert gaps if the selectionGroup
1197 // contains all the sequences
1198 sg.setEndRes(sg.getEndRes() + startres - lastres);
1199 fixedRight = alWidth + startres - lastres;
1210 else if (!insertGap)
1212 // / Are we able to delete?
1213 // ie are all columns blank?
1215 for (g = 0; g < groupSize; g++)
1217 for (int j = startres; j < lastres; j++)
1219 if (groupSeqs[g].getLength() <= j)
1224 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1226 // Not a gap, block edit not valid
1236 // dragging to the right
1237 if (fixedColumns && fixedRight != -1)
1239 for (int j = lastres; j < startres; j++)
1241 insertChar(j, groupSeqs, fixedRight);
1246 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1247 startres, startres - lastres, av.alignment, true);
1252 // dragging to the left
1253 if (fixedColumns && fixedRight != -1)
1255 for (int j = lastres; j > startres; j--)
1257 deleteChar(startres, groupSeqs, fixedRight);
1262 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1263 startres, lastres - startres, av.alignment, true);
1269 // ///Editing a single sequence///////////
1273 // dragging to the right
1274 if (fixedColumns && fixedRight != -1)
1276 for (int j = lastres; j < startres; j++)
1278 insertChar(j, new SequenceI[]
1279 { seq }, fixedRight);
1284 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1285 { seq }, lastres, startres - lastres, av.alignment, true);
1290 // dragging to the left
1291 if (fixedColumns && fixedRight != -1)
1293 for (int j = lastres; j > startres; j--)
1295 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1300 deleteChar(startres, new SequenceI[]
1301 { seq }, fixedRight);
1306 // could be a keyboard edit trying to delete none gaps
1308 for (int m = startres; m < lastres; m++)
1310 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1319 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1320 { seq }, startres, max, av.alignment, true);
1327 seqCanvas.repaint();
1330 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1332 int blankColumn = fixedColumn;
1333 for (int s = 0; s < seq.length; s++)
1335 // Find the next gap before the end of the visible region boundary
1336 // If lastCol > j, theres a boundary after the gap insertion
1338 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1340 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1342 // Theres a space, so break and insert the gap
1347 if (blankColumn <= j)
1349 blankColumn = fixedColumn;
1355 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1356 av.alignment, true);
1358 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1, av.alignment,
1363 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1366 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1, av.alignment,
1369 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1370 av.alignment, true);
1379 public void mouseEntered(MouseEvent e)
1386 if (scrollThread != null)
1388 scrollThread.running = false;
1389 scrollThread = null;
1399 public void mouseExited(MouseEvent e)
1401 if (av.getWrapAlignment())
1408 scrollThread = new ScrollThread();
1412 public void mouseClicked(MouseEvent evt)
1414 SequenceI sequence = av.alignment.getSequenceAt(findSeq(evt));
1415 if (evt.getClickCount() > 1)
1417 if (av.getSelectionGroup().getSize() == 1
1418 && av.getSelectionGroup().getEndRes()
1419 - av.getSelectionGroup().getStartRes() < 2)
1421 av.setSelectionGroup(null);
1424 SequenceFeature[] features = findFeaturesAtRes(sequence
1425 .getDatasetSequence(), sequence.findPosition(findRes(evt)));
1427 if (features != null && features.length > 0)
1429 SearchResults highlight = new SearchResults();
1430 highlight.addResult(sequence, features[0].getBegin(), features[0]
1432 seqCanvas.highlightSearchResults(highlight);
1434 if (features != null && features.length > 0)
1436 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1437 { sequence }, features, false, ap);
1439 seqCanvas.highlightSearchResults(null);
1444 public void mouseWheelMoved(MouseWheelEvent e)
1447 if (e.getWheelRotation() > 0)
1463 public void doMousePressedDefineMode(MouseEvent evt)
1465 int res = findRes(evt);
1466 int seq = findSeq(evt);
1469 startWrapBlock = wrappedBlock;
1471 if (av.wrapAlignment && seq > av.alignment.getHeight())
1473 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1474 "Cannot edit annotations in wrapped view.",
1475 "Wrapped view - no edit", JOptionPane.WARNING_MESSAGE);
1479 if (seq < 0 || res < 0)
1484 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1486 if ((sequence == null) || (res > sequence.getLength()))
1491 stretchGroup = av.getSelectionGroup();
1493 if (stretchGroup == null)
1495 stretchGroup = av.alignment.findGroup(sequence);
1497 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1498 && (res < stretchGroup.getEndRes()))
1500 av.setSelectionGroup(stretchGroup);
1504 stretchGroup = null;
1507 else if (!stretchGroup.getSequences(null).contains(sequence)
1508 || (stretchGroup.getStartRes() > res)
1509 || (stretchGroup.getEndRes() < res))
1511 stretchGroup = null;
1513 SequenceGroup[] allGroups = av.alignment.findAllGroups(sequence);
1515 if (allGroups != null)
1517 for (int i = 0; i < allGroups.length; i++)
1519 if ((allGroups[i].getStartRes() <= res)
1520 && (allGroups[i].getEndRes() >= res))
1522 stretchGroup = allGroups[i];
1528 av.setSelectionGroup(stretchGroup);
1532 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1534 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence
1535 .getDatasetSequence(), sequence.findPosition(res));
1536 Vector links = new Vector();
1537 for (int i = 0; i < allFeatures.length; i++)
1539 if (allFeatures[i].links != null)
1541 for (int j = 0; j < allFeatures[i].links.size(); j++)
1543 links.addElement(allFeatures[i].links.elementAt(j));
1548 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1549 pop.show(this, evt.getX(), evt.getY());
1555 seqCanvas.cursorX = findRes(evt);
1556 seqCanvas.cursorY = findSeq(evt);
1557 seqCanvas.repaint();
1561 if (stretchGroup == null)
1563 // Only if left mouse button do we want to change group sizes
1565 // define a new group here
1566 SequenceGroup sg = new SequenceGroup();
1567 sg.setStartRes(res);
1569 sg.addSequence(sequence, false);
1570 av.setSelectionGroup(sg);
1574 if (av.getConservationSelected())
1576 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1580 if (av.getAbovePIDThreshold())
1582 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1585 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1587 // Edit end res position of selected group
1588 changeEndRes = true;
1590 else if ((stretchGroup != null)
1591 && (stretchGroup.getStartRes() == res))
1593 // Edit end res position of selected group
1594 changeStartRes = true;
1596 stretchGroup.getWidth();
1599 seqCanvas.repaint();
1608 public void doMouseReleasedDefineMode(MouseEvent evt)
1610 if (stretchGroup == null)
1615 if (stretchGroup.cs != null)
1617 if (stretchGroup.cs instanceof ClustalxColourScheme)
1619 ((ClustalxColourScheme) stretchGroup.cs).resetClustalX(stretchGroup
1620 .getSequences(av.hiddenRepSequences), stretchGroup
1624 if (stretchGroup.cs instanceof Blosum62ColourScheme
1625 || stretchGroup.cs instanceof PIDColourScheme
1626 || stretchGroup.cs.conservationApplied()
1627 || stretchGroup.cs.getThreshold() > 0)
1629 stretchGroup.recalcConservation();
1632 if (stretchGroup.cs.conservationApplied())
1634 SliderPanel.setConservationSlider(ap, stretchGroup.cs, stretchGroup
1639 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs, stretchGroup
1642 PaintRefresher.Refresh(this, av.getSequenceSetId());
1643 ap.paintAlignment(true);
1646 changeEndRes = false;
1647 changeStartRes = false;
1648 stretchGroup = null;
1658 public void doMouseDraggedDefineMode(MouseEvent evt)
1660 int res = findRes(evt);
1661 int y = findSeq(evt);
1663 if (wrappedBlock != startWrapBlock)
1668 if (stretchGroup == null)
1673 if (res >= av.alignment.getWidth())
1675 res = av.alignment.getWidth() - 1;
1678 if (stretchGroup.getEndRes() == res)
1680 // Edit end res position of selected group
1681 changeEndRes = true;
1683 else if (stretchGroup.getStartRes() == res)
1685 // Edit start res position of selected group
1686 changeStartRes = true;
1689 if (res < av.getStartRes())
1691 res = av.getStartRes();
1696 if (res > (stretchGroup.getStartRes() - 1))
1698 stretchGroup.setEndRes(res);
1701 else if (changeStartRes)
1703 if (res < (stretchGroup.getEndRes() + 1))
1705 stretchGroup.setStartRes(res);
1709 int dragDirection = 0;
1715 else if (y < oldSeq)
1720 while ((y != oldSeq) && (oldSeq > -1) && (y < av.alignment.getHeight()))
1722 // This routine ensures we don't skip any sequences, as the
1723 // selection is quite slow.
1724 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1726 oldSeq += dragDirection;
1733 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1735 if (stretchGroup.getSequences(null).contains(nextSeq))
1737 stretchGroup.deleteSequence(seq, false);
1743 stretchGroup.addSequence(seq, false);
1746 stretchGroup.addSequence(nextSeq, false);
1755 mouseDragging = true;
1757 if (scrollThread != null)
1759 scrollThread.setEvent(evt);
1762 seqCanvas.repaint();
1765 void scrollCanvas(MouseEvent evt)
1769 if (scrollThread != null)
1771 scrollThread.running = false;
1772 scrollThread = null;
1774 mouseDragging = false;
1778 if (scrollThread == null)
1780 scrollThread = new ScrollThread();
1783 mouseDragging = true;
1784 scrollThread.setEvent(evt);
1789 // this class allows scrolling off the bottom of the visible alignment
1790 class ScrollThread extends Thread
1794 boolean running = false;
1796 public ScrollThread()
1801 public void setEvent(MouseEvent e)
1806 public void stopScrolling()
1819 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1821 running = ap.scrollUp(true);
1824 if (mouseDragging && (evt.getY() >= getHeight())
1825 && (av.alignment.getHeight() > av.getEndSeq()))
1827 running = ap.scrollUp(false);
1830 if (mouseDragging && (evt.getX() < 0))
1832 running = ap.scrollRight(false);
1834 else if (mouseDragging && (evt.getX() >= getWidth()))
1836 running = ap.scrollRight(true);
1843 } catch (Exception ex)
1851 * modify current selection according to a received message.
1853 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1854 SelectionSource source)
1856 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1857 // handles selection messages...
1862 // do we want to thread this ? (contention with seqsel and colsel locks, I
1864 boolean repaint = false;
1865 if (av.followSelection)
1867 if (av.selectionGroup == null || !av.isSelectionGroupChanged())
1869 SequenceGroup sgroup = (seqsel != null) ? seqsel.intersect(
1870 av.alignment, (av.hasHiddenRows) ? av.hiddenRepSequences
1872 if (sgroup != null && sgroup.getSize() > 0)
1874 av.setSelectionGroup(sgroup);
1878 av.setSelectionGroup(null);
1880 repaint = av.isSelectionGroupChanged();
1882 if (av.colSel == null || !av.isColSelChanged())
1884 // Check to see if the current selection is from a previous message
1885 if (colsel == null || colsel.size() == 0)
1891 av.colSel = new ColumnSelection(colsel);
1893 repaint |= av.isColSelChanged();
1898 // probably finessing with multiple redraws here
1899 PaintRefresher.Refresh(this, av.getSequenceSetId());
1900 // ap.paintAlignment(false);