2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 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,this.ap.seqPanel.seqCanvas.fr.minmax);
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();
713 private Point lastp=null;
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
725 if (!event.isShiftDown() || p==null) {
726 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
727 event.getX() + wdth, event.getY() - 20)
731 * TODO: try to modify position region is not obcured by tooltip
737 * appends the features at rpos to the given stringbuffer ready for display in
740 * @param tooltipText2
741 * @param linkImageURL
744 * TODO refactor to Jalview 'utilities' somehow.
746 public void appendFeatures(StringBuffer tooltipText2,
747 String linkImageURL, int rpos, SequenceFeature[] features)
749 appendFeatures(tooltipText2,linkImageURL,rpos,features,null);
751 public void appendFeatures(StringBuffer tooltipText2, String string,
752 int rpos, SequenceFeature[] features, Hashtable minmax)
755 if (features != null)
757 for (int i = 0; i < features.length; i++)
759 if (features[i].getType().equals("disulfide bond"))
761 if (features[i].getBegin() == rpos
762 || features[i].getEnd() == rpos)
764 if (tooltipText2.length() > 6)
766 tooltipText2.append("<br>");
768 tooltipText2.append("disulfide bond " + features[i].getBegin()
769 + ":" + features[i].getEnd());
770 if (features[i].links != null)
772 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
778 if (tooltipText2.length() > 6)
780 tooltipText2.append("<br>");
782 // TODO: remove this hack to display link only features
783 boolean linkOnly = features[i].getValue("linkonly") != null;
786 tooltipText2.append(features[i].getType() + " ");
789 // we are marking a positional feature
790 tooltipText2.append(features[i].begin);
792 if (features[i].begin != features[i].end)
794 tooltipText2.append(" " + features[i].end);
797 if (features[i].getDescription() != null
798 && !features[i].description.equals(features[i]
801 tmpString = features[i].getDescription();
802 int startTag = tmpString.toUpperCase().indexOf("<HTML>");
805 tmpString = tmpString.substring(startTag + 6);
807 int endTag = tmpString.toUpperCase().indexOf("</BODY>");
810 tmpString = tmpString.substring(0, endTag);
812 endTag = tmpString.toUpperCase().indexOf("</HTML>");
815 tmpString = tmpString.substring(0, endTag);
820 tooltipText2.append("; " + tmpString);
824 if (tmpString.indexOf("<") > -1
825 || tmpString.indexOf(">") > -1)
827 // The description does not specify html is to
828 // be used, so we must remove < > symbols
829 tmpString = tmpString.replaceAll("<", "<");
830 tmpString = tmpString.replaceAll(">", ">");
832 tooltipText2.append("; ");
833 tooltipText2.append(tmpString);
838 tooltipText2.append("; " + tmpString);
842 // check score should be shown
843 if (features[i].getScore() != Float.NaN) {
844 float[][] rng=(minmax==null) ? null : ((float[][])minmax.get(features[i].getType()));
845 if (rng!=null && rng[0]!=null && rng[0][0]!=rng[0][1]) {
846 tooltipText2.append(" Score=" + features[i].getScore());
849 if (features[i].getValue("status") != null)
851 String status = features[i].getValue("status").toString();
852 if (status.length() > 0)
854 tooltipText2.append("; (" + features[i].getValue("status")
859 if (features[i].links != null)
861 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
872 * Set status message in alignment panel
875 * aligned sequence object
879 * index of sequence in alignment
880 * @return position of res in sequence
882 int setStatusMessage(SequenceI sequence, int res, int seq)
885 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
886 + sequence.getName());
889 if (av.alignment.isNucleotide())
891 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
895 text.append(" Nucleotide: ");
900 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
903 text.append(" Residue: ");
909 pos = sequence.findPosition(res);
912 text.append(obj + " (" + pos + ")");
915 ap.alignFrame.statusBar.setText(text.toString());
925 public void mouseDragged(MouseEvent evt)
927 if (mouseWheelPressed)
929 int oldWidth = av.charWidth;
931 // Which is bigger, left-right or up-down?
932 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
934 - lastMousePress.getX()))
936 int fontSize = av.font.getSize();
938 if (evt.getY() < lastMousePress.getY())
942 else if (evt.getY() > lastMousePress.getY())
953 .setFont(new Font(av.font.getName(), av.font.getStyle(),
955 av.charWidth = oldWidth;
960 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
964 else if (evt.getX() > lastMousePress.getX())
969 ap.paintAlignment(false);
972 FontMetrics fm = getFontMetrics(av.getFont());
973 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
975 lastMousePress = evt.getPoint();
982 doMouseDraggedDefineMode(evt);
986 int res = findRes(evt);
993 if ((lastres == -1) || (lastres == res))
998 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1000 // dragLeft, delete gap
1001 editSequence(false, res);
1005 editSequence(true, res);
1008 mouseDragging = true;
1009 if (scrollThread != null)
1011 scrollThread.setEvent(evt);
1015 synchronized void editSequence(boolean insertGap, int startres)
1018 int fixedRight = -1;
1019 boolean fixedColumns = false;
1020 SequenceGroup sg = av.getSelectionGroup();
1022 SequenceI seq = av.alignment.getSequenceAt(startseq);
1024 // No group, but the sequence may represent a group
1025 if (!groupEditing && av.hasHiddenRows)
1027 if (av.hiddenRepSequences != null
1028 && av.hiddenRepSequences.containsKey(seq))
1030 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1031 groupEditing = true;
1035 StringBuffer message = new StringBuffer();
1038 message.append("Edit group:");
1039 if (editCommand == null)
1041 editCommand = new EditCommand("Edit Group");
1046 message.append("Edit sequence: " + seq.getName());
1047 String label = seq.getName();
1048 if (label.length() > 10)
1050 label = label.substring(0, 10);
1052 if (editCommand == null)
1054 editCommand = new EditCommand("Edit " + label);
1060 message.append(" insert ");
1064 message.append(" delete ");
1067 message.append(Math.abs(startres - lastres) + " gaps.");
1068 ap.alignFrame.statusBar.setText(message.toString());
1070 // Are we editing within a selection group?
1072 || (sg != null && sg.getSequences(av.hiddenRepSequences)
1075 fixedColumns = true;
1077 // sg might be null as the user may only see 1 sequence,
1078 // but the sequence represents a group
1081 if (av.hiddenRepSequences == null
1082 || !av.hiddenRepSequences.containsKey(seq))
1087 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1090 fixedLeft = sg.getStartRes();
1091 fixedRight = sg.getEndRes();
1093 if ((startres < fixedLeft && lastres >= fixedLeft)
1094 || (startres >= fixedLeft && lastres < fixedLeft)
1095 || (startres > fixedRight && lastres <= fixedRight)
1096 || (startres <= fixedRight && lastres > fixedRight))
1102 if (fixedLeft > startres)
1104 fixedRight = fixedLeft - 1;
1107 else if (fixedRight < startres)
1109 fixedLeft = fixedRight;
1114 if (av.hasHiddenColumns)
1116 fixedColumns = true;
1117 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1118 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1120 if ((insertGap && startres > y1 && lastres < y1)
1121 || (!insertGap && startres < y2 && lastres > y2))
1127 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1128 // Selection spans a hidden region
1129 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1137 fixedRight = y2 - 1;
1144 Vector vseqs = sg.getSequences(av.hiddenRepSequences);
1145 int g, groupSize = vseqs.size();
1146 SequenceI[] groupSeqs = new SequenceI[groupSize];
1147 for (g = 0; g < groupSeqs.length; g++)
1149 groupSeqs[g] = (SequenceI) vseqs.elementAt(g);
1155 // If the user has selected the whole sequence, and is dragging to
1156 // the right, we can still extend the alignment and selectionGroup
1157 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1158 && sg.getEndRes() == av.alignment.getWidth() - 1)
1160 sg.setEndRes(av.alignment.getWidth() + startres - lastres);
1161 fixedRight = sg.getEndRes();
1164 // Is it valid with fixed columns??
1165 // Find the next gap before the end
1166 // of the visible region boundary
1167 boolean blank = false;
1168 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1172 for (g = 0; g < groupSize; g++)
1174 for (int j = 0; j < startres - lastres; j++)
1176 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1177 .getCharAt(fixedRight - j)))
1192 if (sg.getSize() == av.alignment.getHeight())
1194 if ((av.hasHiddenColumns && startres < av.getColumnSelection()
1195 .getHiddenBoundaryRight(startres)))
1201 int alWidth = av.alignment.getWidth();
1202 if (av.hasHiddenRows)
1204 int hwidth = av.alignment.getHiddenSequences().getWidth();
1205 if (hwidth > alWidth)
1210 // We can still insert gaps if the selectionGroup
1211 // contains all the sequences
1212 sg.setEndRes(sg.getEndRes() + startres - lastres);
1213 fixedRight = alWidth + startres - lastres;
1224 else if (!insertGap)
1226 // / Are we able to delete?
1227 // ie are all columns blank?
1229 for (g = 0; g < groupSize; g++)
1231 for (int j = startres; j < lastres; j++)
1233 if (groupSeqs[g].getLength() <= j)
1238 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1240 // Not a gap, block edit not valid
1250 // dragging to the right
1251 if (fixedColumns && fixedRight != -1)
1253 for (int j = lastres; j < startres; j++)
1255 insertChar(j, groupSeqs, fixedRight);
1260 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1261 startres, startres - lastres, av.alignment, true);
1266 // dragging to the left
1267 if (fixedColumns && fixedRight != -1)
1269 for (int j = lastres; j > startres; j--)
1271 deleteChar(startres, groupSeqs, fixedRight);
1276 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1277 startres, lastres - startres, av.alignment, true);
1283 // ///Editing a single sequence///////////
1287 // dragging to the right
1288 if (fixedColumns && fixedRight != -1)
1290 for (int j = lastres; j < startres; j++)
1292 insertChar(j, new SequenceI[]
1293 { seq }, fixedRight);
1298 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1299 { seq }, lastres, startres - lastres, av.alignment, true);
1304 // dragging to the left
1305 if (fixedColumns && fixedRight != -1)
1307 for (int j = lastres; j > startres; j--)
1309 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1314 deleteChar(startres, new SequenceI[]
1315 { seq }, fixedRight);
1320 // could be a keyboard edit trying to delete none gaps
1322 for (int m = startres; m < lastres; m++)
1324 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1333 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1334 { seq }, startres, max, av.alignment, true);
1341 seqCanvas.repaint();
1344 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1346 int blankColumn = fixedColumn;
1347 for (int s = 0; s < seq.length; s++)
1349 // Find the next gap before the end of the visible region boundary
1350 // If lastCol > j, theres a boundary after the gap insertion
1352 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1354 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1356 // Theres a space, so break and insert the gap
1361 if (blankColumn <= j)
1363 blankColumn = fixedColumn;
1369 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1370 av.alignment, true);
1372 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1, av.alignment,
1377 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1380 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1, av.alignment,
1383 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1384 av.alignment, true);
1393 public void mouseEntered(MouseEvent e)
1400 if (scrollThread != null)
1402 scrollThread.running = false;
1403 scrollThread = null;
1413 public void mouseExited(MouseEvent e)
1415 if (av.getWrapAlignment())
1422 scrollThread = new ScrollThread();
1426 public void mouseClicked(MouseEvent evt)
1428 SequenceI sequence = av.alignment.getSequenceAt(findSeq(evt));
1429 if (evt.getClickCount() > 1)
1431 if (av.getSelectionGroup().getSize() == 1
1432 && av.getSelectionGroup().getEndRes()
1433 - av.getSelectionGroup().getStartRes() < 2)
1435 av.setSelectionGroup(null);
1438 SequenceFeature[] features = findFeaturesAtRes(sequence
1439 .getDatasetSequence(), sequence.findPosition(findRes(evt)));
1441 if (features != null && features.length > 0)
1443 SearchResults highlight = new SearchResults();
1444 highlight.addResult(sequence, features[0].getBegin(), features[0]
1446 seqCanvas.highlightSearchResults(highlight);
1448 if (features != null && features.length > 0)
1450 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1451 { sequence }, features, false, ap);
1453 seqCanvas.highlightSearchResults(null);
1458 public void mouseWheelMoved(MouseWheelEvent e)
1461 if (e.getWheelRotation() > 0)
1469 // TODO Update tooltip for new position.
1478 public void doMousePressedDefineMode(MouseEvent evt)
1480 int res = findRes(evt);
1481 int seq = findSeq(evt);
1484 startWrapBlock = wrappedBlock;
1486 if (av.wrapAlignment && seq > av.alignment.getHeight())
1488 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1489 "Cannot edit annotations in wrapped view.",
1490 "Wrapped view - no edit", JOptionPane.WARNING_MESSAGE);
1494 if (seq < 0 || res < 0)
1499 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1501 if ((sequence == null) || (res > sequence.getLength()))
1506 stretchGroup = av.getSelectionGroup();
1508 if (stretchGroup == null)
1510 stretchGroup = av.alignment.findGroup(sequence);
1512 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1513 && (res < stretchGroup.getEndRes()))
1515 av.setSelectionGroup(stretchGroup);
1519 stretchGroup = null;
1522 else if (!stretchGroup.getSequences(null).contains(sequence)
1523 || (stretchGroup.getStartRes() > res)
1524 || (stretchGroup.getEndRes() < res))
1526 stretchGroup = null;
1528 SequenceGroup[] allGroups = av.alignment.findAllGroups(sequence);
1530 if (allGroups != null)
1532 for (int i = 0; i < allGroups.length; i++)
1534 if ((allGroups[i].getStartRes() <= res)
1535 && (allGroups[i].getEndRes() >= res))
1537 stretchGroup = allGroups[i];
1543 av.setSelectionGroup(stretchGroup);
1547 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1549 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence
1550 .getDatasetSequence(), sequence.findPosition(res));
1551 Vector links = new Vector();
1552 for (int i = 0; i < allFeatures.length; i++)
1554 if (allFeatures[i].links != null)
1556 for (int j = 0; j < allFeatures[i].links.size(); j++)
1558 links.addElement(allFeatures[i].links.elementAt(j));
1563 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1564 pop.show(this, evt.getX(), evt.getY());
1570 seqCanvas.cursorX = findRes(evt);
1571 seqCanvas.cursorY = findSeq(evt);
1572 seqCanvas.repaint();
1576 if (stretchGroup == null)
1578 // Only if left mouse button do we want to change group sizes
1580 // define a new group here
1581 SequenceGroup sg = new SequenceGroup();
1582 sg.setStartRes(res);
1584 sg.addSequence(sequence, false);
1585 av.setSelectionGroup(sg);
1589 if (av.getConservationSelected())
1591 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1595 if (av.getAbovePIDThreshold())
1597 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1600 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1602 // Edit end res position of selected group
1603 changeEndRes = true;
1605 else if ((stretchGroup != null)
1606 && (stretchGroup.getStartRes() == res))
1608 // Edit end res position of selected group
1609 changeStartRes = true;
1611 stretchGroup.getWidth();
1614 seqCanvas.repaint();
1623 public void doMouseReleasedDefineMode(MouseEvent evt)
1625 if (stretchGroup == null)
1630 if (stretchGroup.cs != null)
1632 if (stretchGroup.cs instanceof ClustalxColourScheme)
1634 ((ClustalxColourScheme) stretchGroup.cs).resetClustalX(stretchGroup
1635 .getSequences(av.hiddenRepSequences), stretchGroup
1639 if (stretchGroup.cs instanceof Blosum62ColourScheme
1640 || stretchGroup.cs instanceof PIDColourScheme
1641 || stretchGroup.cs.conservationApplied()
1642 || stretchGroup.cs.getThreshold() > 0)
1644 stretchGroup.recalcConservation();
1647 if (stretchGroup.cs.conservationApplied())
1649 SliderPanel.setConservationSlider(ap, stretchGroup.cs, stretchGroup
1654 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs, stretchGroup
1657 PaintRefresher.Refresh(this, av.getSequenceSetId());
1658 ap.paintAlignment(true);
1661 changeEndRes = false;
1662 changeStartRes = false;
1663 stretchGroup = null;
1673 public void doMouseDraggedDefineMode(MouseEvent evt)
1675 int res = findRes(evt);
1676 int y = findSeq(evt);
1678 if (wrappedBlock != startWrapBlock)
1683 if (stretchGroup == null)
1688 if (res >= av.alignment.getWidth())
1690 res = av.alignment.getWidth() - 1;
1693 if (stretchGroup.getEndRes() == res)
1695 // Edit end res position of selected group
1696 changeEndRes = true;
1698 else if (stretchGroup.getStartRes() == res)
1700 // Edit start res position of selected group
1701 changeStartRes = true;
1704 if (res < av.getStartRes())
1706 res = av.getStartRes();
1711 if (res > (stretchGroup.getStartRes() - 1))
1713 stretchGroup.setEndRes(res);
1716 else if (changeStartRes)
1718 if (res < (stretchGroup.getEndRes() + 1))
1720 stretchGroup.setStartRes(res);
1724 int dragDirection = 0;
1730 else if (y < oldSeq)
1735 while ((y != oldSeq) && (oldSeq > -1) && (y < av.alignment.getHeight()))
1737 // This routine ensures we don't skip any sequences, as the
1738 // selection is quite slow.
1739 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1741 oldSeq += dragDirection;
1748 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1750 if (stretchGroup.getSequences(null).contains(nextSeq))
1752 stretchGroup.deleteSequence(seq, false);
1758 stretchGroup.addSequence(seq, false);
1761 stretchGroup.addSequence(nextSeq, false);
1770 mouseDragging = true;
1772 if (scrollThread != null)
1774 scrollThread.setEvent(evt);
1777 seqCanvas.repaint();
1780 void scrollCanvas(MouseEvent evt)
1784 if (scrollThread != null)
1786 scrollThread.running = false;
1787 scrollThread = null;
1789 mouseDragging = false;
1793 if (scrollThread == null)
1795 scrollThread = new ScrollThread();
1798 mouseDragging = true;
1799 scrollThread.setEvent(evt);
1804 // this class allows scrolling off the bottom of the visible alignment
1805 class ScrollThread extends Thread
1809 boolean running = false;
1811 public ScrollThread()
1816 public void setEvent(MouseEvent e)
1821 public void stopScrolling()
1834 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1836 running = ap.scrollUp(true);
1839 if (mouseDragging && (evt.getY() >= getHeight())
1840 && (av.alignment.getHeight() > av.getEndSeq()))
1842 running = ap.scrollUp(false);
1845 if (mouseDragging && (evt.getX() < 0))
1847 running = ap.scrollRight(false);
1849 else if (mouseDragging && (evt.getX() >= getWidth()))
1851 running = ap.scrollRight(true);
1858 } catch (Exception ex)
1866 * modify current selection according to a received message.
1868 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1869 SelectionSource source)
1871 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1872 // handles selection messages...
1877 // do we want to thread this ? (contention with seqsel and colsel locks, I
1879 boolean repaint = false;
1880 if (av.followSelection)
1882 if (av.selectionGroup == null || !av.isSelectionGroupChanged())
1884 SequenceGroup sgroup = null;
1887 if (av.alignment==null)
1889 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="+av.getSequenceSetId()+" ViewId="+av.getViewId()+" 's alignment is NULL! returning immediatly.");
1892 sgroup = seqsel.intersect(av.alignment,
1893 (av.hasHiddenRows) ? av.hiddenRepSequences : null);
1895 if (sgroup != null && sgroup.getSize() > 0)
1897 av.setSelectionGroup(sgroup);
1901 av.setSelectionGroup(null);
1903 repaint = av.isSelectionGroupChanged();
1905 if (av.colSel == null || !av.isColSelChanged())
1907 // Check to see if the current selection is from a previous message
1908 if (colsel == null || colsel.size() == 0)
1914 av.colSel = new ColumnSelection(colsel);
1916 repaint |= av.isColSelChanged();
1921 // probably finessing with multiple redraws here
1922 PaintRefresher.Refresh(this, av.getSequenceSetId());
1923 // ap.paintAlignment(false);