2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
23 import java.awt.event.*;
27 import jalview.commands.*;
28 import jalview.datamodel.*;
29 import jalview.schemes.*;
30 import jalview.structure.*;
36 * @version $Revision: 1.130 $
38 public class SeqPanel extends JPanel implements MouseListener,
39 MouseMotionListener, MouseWheelListener, SequenceListener,
44 public SeqCanvas seqCanvas;
47 public AlignmentPanel ap;
49 protected int lastres;
51 protected int startseq;
53 protected AlignViewport av;
55 ScrollThread scrollThread = null;
57 boolean mouseDragging = false;
59 boolean editingSeqs = false;
61 boolean groupEditing = false;
63 // ////////////////////////////////////////
64 // ///Everything below this is for defining the boundary of the rubberband
65 // ////////////////////////////////////////
68 boolean changeEndSeq = false;
70 boolean changeStartSeq = false;
72 boolean changeEndRes = false;
74 boolean changeStartRes = false;
76 SequenceGroup stretchGroup = null;
78 boolean remove = false;
82 boolean mouseWheelPressed = false;
84 StringBuffer keyboardNo1;
86 StringBuffer keyboardNo2;
88 java.net.URL linkImageURL;
90 StringBuffer tooltipText = new StringBuffer("<html>");
94 EditCommand editCommand;
96 StructureSelectionManager ssm;
99 * Creates a new SeqPanel object.
106 public SeqPanel(AlignViewport av, AlignmentPanel ap)
108 linkImageURL = getClass().getResource("/images/link.gif");
109 ToolTipManager.sharedInstance().registerComponent(this);
110 ToolTipManager.sharedInstance().setInitialDelay(0);
111 ToolTipManager.sharedInstance().setDismissDelay(10000);
113 setBackground(Color.white);
115 seqCanvas = new SeqCanvas(ap);
116 setLayout(new BorderLayout());
117 add(seqCanvas, BorderLayout.CENTER);
123 addMouseMotionListener(this);
124 addMouseListener(this);
125 addMouseWheelListener(this);
126 ssm = StructureSelectionManager.getStructureSelectionManager(Desktop.instance);
127 ssm.addStructureViewerListener(this);
128 ssm.addSelectionListener(this);
132 int startWrapBlock = -1;
134 int wrappedBlock = -1;
136 int findRes(MouseEvent evt)
141 if (av.wrapAlignment)
144 int hgap = av.charHeight;
145 if (av.scaleAboveWrapped)
147 hgap += av.charHeight;
150 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
151 + seqCanvas.getAnnotationHeight();
155 x -= seqCanvas.LABEL_WEST;
157 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
163 wrappedBlock = y / cHeight;
164 wrappedBlock += av.getStartRes() / cwidth;
166 res = wrappedBlock * cwidth + x / av.getCharWidth();
171 if (x>seqCanvas.getWidth()+seqCanvas.getWidth())
173 // make sure we calculate relative to visible alignment, rather than right-hand gutter
174 x = seqCanvas.getX()+seqCanvas.getWidth();
176 res = (x / av.getCharWidth()) + av.getStartRes();
179 if (av.hasHiddenColumns)
181 res = av.getColumnSelection().adjustForHiddenColumns(res);
188 int findSeq(MouseEvent evt)
193 if (av.wrapAlignment)
195 int hgap = av.charHeight;
196 if (av.scaleAboveWrapped)
198 hgap += av.charHeight;
201 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
202 + seqCanvas.getAnnotationHeight();
206 seq = Math.min((y % cHeight) / av.getCharHeight(),
207 av.alignment.getHeight() - 1);
211 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(),
212 av.alignment.getHeight() - 1);
218 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
220 Vector tmp = new Vector();
221 SequenceFeature[] features = sequence.getSequenceFeatures();
222 if (features != null)
224 for (int i = 0; i < features.length; i++)
226 if (av.featuresDisplayed == null
227 || !av.featuresDisplayed.containsKey(features[i].getType()))
232 if (features[i].featureGroup != null
233 && seqCanvas.fr.featureGroups != null
234 && seqCanvas.fr.featureGroups
235 .containsKey(features[i].featureGroup)
236 && !((Boolean) seqCanvas.fr.featureGroups
237 .get(features[i].featureGroup)).booleanValue())
240 if ((features[i].getBegin() <= res)
241 && (features[i].getEnd() >= res))
243 tmp.addElement(features[i]);
248 features = new SequenceFeature[tmp.size()];
249 tmp.copyInto(features);
256 if (editCommand != null && editCommand.getSize() > 0)
258 ap.alignFrame.addHistoryItem(editCommand);
259 av.firePropertyChange("alignment", null, av.getAlignment()
266 groupEditing = false;
274 seqCanvas.cursorY = getKeyboardNo1() - 1;
278 void setCursorColumn()
280 seqCanvas.cursorX = getKeyboardNo1() - 1;
284 void setCursorRowAndColumn()
286 if (keyboardNo2 == null)
288 keyboardNo2 = new StringBuffer();
292 seqCanvas.cursorX = getKeyboardNo1() - 1;
293 seqCanvas.cursorY = getKeyboardNo2() - 1;
298 void setCursorPosition()
300 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
303 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1() - 1);
307 void moveCursor(int dx, int dy)
309 seqCanvas.cursorX += dx;
310 seqCanvas.cursorY += dy;
311 if (av.hasHiddenColumns && !av.colSel.isVisible(seqCanvas.cursorX))
313 int original = seqCanvas.cursorX - dx;
314 int maxWidth = av.alignment.getWidth();
316 while (!av.colSel.isVisible(seqCanvas.cursorX)
317 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
319 seqCanvas.cursorX += dx;
322 if (seqCanvas.cursorX >= maxWidth
323 || !av.colSel.isVisible(seqCanvas.cursorX))
325 seqCanvas.cursorX = original;
332 void scrollToVisible()
334 if (seqCanvas.cursorX < 0)
336 seqCanvas.cursorX = 0;
338 else if (seqCanvas.cursorX > av.alignment.getWidth() - 1)
340 seqCanvas.cursorX = av.alignment.getWidth() - 1;
343 if (seqCanvas.cursorY < 0)
345 seqCanvas.cursorY = 0;
347 else if (seqCanvas.cursorY > av.alignment.getHeight() - 1)
349 seqCanvas.cursorY = av.alignment.getHeight() - 1;
353 if (av.wrapAlignment)
355 ap.scrollToWrappedVisible(seqCanvas.cursorX);
359 while (seqCanvas.cursorY < av.startSeq)
363 while (seqCanvas.cursorY + 1 > av.endSeq)
367 if (!av.wrapAlignment)
369 while (seqCanvas.cursorX < av.colSel
370 .adjustForHiddenColumns(av.startRes))
372 if (!ap.scrollRight(false))
377 while (seqCanvas.cursorX > av.colSel
378 .adjustForHiddenColumns(av.endRes))
380 if (!ap.scrollRight(true))
387 setStatusMessage(av.alignment.getSequenceAt(seqCanvas.cursorY),
388 seqCanvas.cursorX, seqCanvas.cursorY);
393 void setSelectionAreaAtCursor(boolean topLeft)
395 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
398 if (av.getSelectionGroup() != null)
400 SequenceGroup sg = av.selectionGroup;
401 // Find the top and bottom of this group
402 int min = av.alignment.getHeight(), max = 0;
403 for (int i = 0; i < sg.getSize(); i++)
405 int index = av.alignment.findIndex(sg.getSequenceAt(i));
420 sg.setStartRes(seqCanvas.cursorX);
421 if (sg.getEndRes() < seqCanvas.cursorX)
423 sg.setEndRes(seqCanvas.cursorX);
426 min = seqCanvas.cursorY;
430 sg.setEndRes(seqCanvas.cursorX);
431 if (sg.getStartRes() > seqCanvas.cursorX)
433 sg.setStartRes(seqCanvas.cursorX);
436 max = seqCanvas.cursorY + 1;
441 // Only the user can do this
442 av.setSelectionGroup(null);
446 // Now add any sequences between min and max
447 sg.getSequences(null).clear();
448 for (int i = min; i < max; i++)
450 sg.addSequence(av.alignment.getSequenceAt(i), false);
455 if (av.getSelectionGroup() == null)
457 SequenceGroup sg = new SequenceGroup();
458 sg.setStartRes(seqCanvas.cursorX);
459 sg.setEndRes(seqCanvas.cursorX);
460 sg.addSequence(sequence, false);
461 av.setSelectionGroup(sg);
464 ap.paintAlignment(false);
468 void insertGapAtCursor(boolean group)
470 groupEditing = group;
471 startseq = seqCanvas.cursorY;
472 lastres = seqCanvas.cursorX;
473 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
477 void deleteGapAtCursor(boolean group)
479 groupEditing = group;
480 startseq = seqCanvas.cursorY;
481 lastres = seqCanvas.cursorX + getKeyboardNo1();
482 editSequence(false, false, seqCanvas.cursorX);
486 void insertNucAtCursor(boolean group,String nuc){
487 groupEditing = group;
488 startseq = seqCanvas.cursorY;
489 lastres = seqCanvas.cursorX;
490 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
494 void numberPressed(char value)
496 if (keyboardNo1 == null)
498 keyboardNo1 = new StringBuffer();
501 if (keyboardNo2 != null)
503 keyboardNo2.append(value);
507 keyboardNo1.append(value);
513 if (keyboardNo1 == null)
517 int value = Integer.parseInt(keyboardNo1.toString());
525 if (keyboardNo2 == null)
529 int value = Integer.parseInt(keyboardNo2.toString());
541 public void mouseReleased(MouseEvent evt)
543 mouseDragging = false;
544 mouseWheelPressed = false;
548 doMouseReleasedDefineMode(evt);
561 public void mousePressed(MouseEvent evt)
563 lastMousePress = evt.getPoint();
565 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
567 mouseWheelPressed = true;
571 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
573 if (evt.isAltDown() || evt.isControlDown())
581 doMousePressedDefineMode(evt);
585 int seq = findSeq(evt);
586 int res = findRes(evt);
588 if (seq < 0 || res < 0)
593 if ((seq < av.getAlignment().getHeight())
594 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
610 public void mouseOverSequence(SequenceI sequence, int index, int pos)
612 String tmp = sequence.hashCode() + " " + index + " " + pos;
614 if (lastMessage == null || !lastMessage.equals(tmp))
616 // System.err.println("mouseOver Sequence: "+tmp);
617 ssm.mouseOverSequence(sequence, index, pos, av);
622 public void highlightSequence(SearchResults results)
624 if (av.followHighlight)
626 if (ap.scrollToPosition(results, false))
628 seqCanvas.revalidate();
631 seqCanvas.highlightSearchResults(results);
634 public void updateColours(SequenceI seq, int index)
636 System.out.println("update the seqPanel colours");
646 public void mouseMoved(MouseEvent evt)
650 // This is because MacOSX creates a mouseMoved
651 // If control is down, other platforms will not.
655 int res = findRes(evt);
656 int seq = findSeq(evt);
658 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
663 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
665 if (res >= sequence.getLength())
670 pos = setStatusMessage(sequence, res, seq);
671 if (ssm != null && pos > -1)
672 mouseOverSequence(sequence, res, pos);
674 tooltipText.setLength(6); // Cuts the buffer back to <html>
676 SequenceGroup[] groups = av.alignment.findAllGroups(sequence);
679 for (int g = 0; g < groups.length; g++)
681 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
683 if (tooltipText.length() > 6)
685 tooltipText.append("<br>");
688 if (!groups[g].getName().startsWith("JTreeGroup")
689 && !groups[g].getName().startsWith("JGroup"))
691 tooltipText.append(groups[g].getName());
694 if (groups[g].getDescription() != null)
696 tooltipText.append(": " + groups[g].getDescription());
702 // use aa to see if the mouse pointer is on a
703 if (av.showSequenceFeatures)
706 SequenceFeature[] features = findFeaturesAtRes(
707 sequence.getDatasetSequence(),
708 rpos = sequence.findPosition(res));
709 appendFeatures(tooltipText, linkImageURL.toString(), rpos, features,
710 this.ap.seqPanel.seqCanvas.fr.minmax);
712 if (tooltipText.length() == 6) // <html></html>
714 setToolTipText(null);
719 tooltipText.append("</html>");
720 if (lastTooltip == null
721 || !lastTooltip.equals(tooltipText.toString()))
723 setToolTipText(tooltipText.toString());
724 lastTooltip = tooltipText.toString();
731 private Point lastp = null;
736 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
738 public Point getToolTipLocation(MouseEvent event)
740 int x = event.getX(), w = getWidth();
741 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
744 if (!event.isShiftDown() || p == null)
746 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
747 event.getX() + wdth, event.getY() - 20) : null;
750 * TODO: try to modify position region is not obcured by tooltip
756 * appends the features at rpos to the given stringbuffer ready for display in
759 * @param tooltipText2
760 * @param linkImageURL
763 * TODO refactor to Jalview 'utilities' somehow.
765 public void appendFeatures(StringBuffer tooltipText2,
766 String linkImageURL, int rpos, SequenceFeature[] features)
768 appendFeatures(tooltipText2, linkImageURL, rpos, features, null);
771 public void appendFeatures(StringBuffer tooltipText2, String string,
772 int rpos, SequenceFeature[] features, Hashtable minmax)
775 if (features != null)
777 for (int i = 0; i < features.length; i++)
779 if (features[i].getType().equals("disulfide bond"))
781 if (features[i].getBegin() == rpos
782 || features[i].getEnd() == rpos)
784 if (tooltipText2.length() > 6)
786 tooltipText2.append("<br>");
788 tooltipText2.append("disulfide bond " + features[i].getBegin()
789 + ":" + features[i].getEnd());
790 if (features[i].links != null)
792 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
798 if (tooltipText2.length() > 6)
800 tooltipText2.append("<br>");
802 // TODO: remove this hack to display link only features
803 boolean linkOnly = features[i].getValue("linkonly") != null;
806 tooltipText2.append(features[i].getType() + " ");
809 // we are marking a positional feature
810 tooltipText2.append(features[i].begin);
812 if (features[i].begin != features[i].end)
814 tooltipText2.append(" " + features[i].end);
817 if (features[i].getDescription() != null
818 && !features[i].description.equals(features[i]
821 tmpString = features[i].getDescription();
822 String tmp2up=tmpString.toUpperCase();
823 int startTag = tmp2up.indexOf("<HTML>");
826 tmpString = tmpString.substring(startTag + 6);
827 tmp2up = tmp2up.substring(startTag+6);
829 int endTag = tmp2up.indexOf("</BODY>");
832 tmpString = tmpString.substring(0, endTag);
833 tmp2up = tmp2up.substring(0, endTag);
835 endTag = tmp2up.indexOf("</HTML>");
838 tmpString = tmpString.substring(0, endTag);
843 tooltipText2.append("; " + tmpString);
847 if (tmpString.indexOf("<") > -1
848 || tmpString.indexOf(">") > -1)
850 // The description does not specify html is to
851 // be used, so we must remove < > symbols
852 tmpString = tmpString.replaceAll("<", "<");
853 tmpString = tmpString.replaceAll(">", ">");
855 tooltipText2.append("; ");
856 tooltipText2.append(tmpString);
861 tooltipText2.append("; " + tmpString);
865 // check score should be shown
866 if (features[i].getScore() != Float.NaN)
868 float[][] rng = (minmax == null) ? null : ((float[][]) minmax
869 .get(features[i].getType()));
870 if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
872 tooltipText2.append(" Score=" + features[i].getScore());
875 if (features[i].getValue("status") != null)
877 String status = features[i].getValue("status").toString();
878 if (status.length() > 0)
880 tooltipText2.append("; (" + features[i].getValue("status")
885 if (features[i].links != null)
887 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
898 * Set status message in alignment panel
901 * aligned sequence object
905 * index of sequence in alignment
906 * @return position of res in sequence
908 int setStatusMessage(SequenceI sequence, int res, int seq)
911 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
912 + sequence.getName());
915 if (av.alignment.isNucleotide())
917 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
921 text.append(" Nucleotide: ");
926 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
929 text.append(" Residue: ");
935 pos = sequence.findPosition(res);
938 text.append(obj + " (" + pos + ")");
941 ap.alignFrame.statusBar.setText(text.toString());
951 public void mouseDragged(MouseEvent evt)
953 if (mouseWheelPressed)
955 int oldWidth = av.charWidth;
957 // Which is bigger, left-right or up-down?
958 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
959 .getX() - lastMousePress.getX()))
961 int fontSize = av.font.getSize();
963 if (evt.getY() < lastMousePress.getY())
967 else if (evt.getY() > lastMousePress.getY())
977 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
978 av.charWidth = oldWidth;
983 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
987 else if (evt.getX() > lastMousePress.getX())
992 ap.paintAlignment(false);
995 FontMetrics fm = getFontMetrics(av.getFont());
996 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
998 lastMousePress = evt.getPoint();
1005 doMouseDraggedDefineMode(evt);
1009 int res = findRes(evt);
1016 if ((lastres == -1) || (lastres == res))
1021 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1023 // dragLeft, delete gap
1024 editSequence(false, false,res);
1028 editSequence(true, false,res);
1031 mouseDragging = true;
1032 if (scrollThread != null)
1034 scrollThread.setEvent(evt);
1038 //TODO: Make it more clever than many booleans
1039 synchronized void editSequence(boolean insertGap, boolean editSeq, int startres)
1042 int fixedRight = -1;
1043 boolean fixedColumns = false;
1044 SequenceGroup sg = av.getSelectionGroup();
1046 SequenceI seq = av.alignment.getSequenceAt(startseq);
1048 // No group, but the sequence may represent a group
1049 if (!groupEditing && av.hasHiddenRows)
1051 if (av.hiddenRepSequences != null
1052 && av.hiddenRepSequences.containsKey(seq))
1054 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1055 groupEditing = true;
1059 StringBuffer message = new StringBuffer();
1062 message.append("Edit group:");
1063 if (editCommand == null)
1065 editCommand = new EditCommand("Edit Group");
1070 message.append("Edit sequence: " + seq.getName());
1071 String label = seq.getName();
1072 if (label.length() > 10)
1074 label = label.substring(0, 10);
1076 if (editCommand == null)
1078 editCommand = new EditCommand("Edit " + label);
1084 message.append(" insert ");
1088 message.append(" delete ");
1091 message.append(Math.abs(startres - lastres) + " gaps.");
1092 ap.alignFrame.statusBar.setText(message.toString());
1094 // Are we editing within a selection group?
1096 || (sg != null && sg.getSequences(av.hiddenRepSequences)
1099 fixedColumns = true;
1101 // sg might be null as the user may only see 1 sequence,
1102 // but the sequence represents a group
1105 if (av.hiddenRepSequences == null
1106 || !av.hiddenRepSequences.containsKey(seq))
1111 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1114 fixedLeft = sg.getStartRes();
1115 fixedRight = sg.getEndRes();
1117 if ((startres < fixedLeft && lastres >= fixedLeft)
1118 || (startres >= fixedLeft && lastres < fixedLeft)
1119 || (startres > fixedRight && lastres <= fixedRight)
1120 || (startres <= fixedRight && lastres > fixedRight))
1126 if (fixedLeft > startres)
1128 fixedRight = fixedLeft - 1;
1131 else if (fixedRight < startres)
1133 fixedLeft = fixedRight;
1138 if (av.hasHiddenColumns)
1140 fixedColumns = true;
1141 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1142 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1144 if ((insertGap && startres > y1 && lastres < y1)
1145 || (!insertGap && startres < y2 && lastres > y2))
1151 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1152 // Selection spans a hidden region
1153 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1161 fixedRight = y2 - 1;
1168 Vector vseqs = sg.getSequences(av.hiddenRepSequences);
1169 int g, groupSize = vseqs.size();
1170 SequenceI[] groupSeqs = new SequenceI[groupSize];
1171 for (g = 0; g < groupSeqs.length; g++)
1173 groupSeqs[g] = (SequenceI) vseqs.elementAt(g);
1179 // If the user has selected the whole sequence, and is dragging to
1180 // the right, we can still extend the alignment and selectionGroup
1181 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1182 && sg.getEndRes() == av.alignment.getWidth() - 1)
1184 sg.setEndRes(av.alignment.getWidth() + startres - lastres);
1185 fixedRight = sg.getEndRes();
1188 // Is it valid with fixed columns??
1189 // Find the next gap before the end
1190 // of the visible region boundary
1191 boolean blank = false;
1192 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1196 for (g = 0; g < groupSize; g++)
1198 for (int j = 0; j < startres - lastres; j++)
1200 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1201 .getCharAt(fixedRight - j)))
1216 if (sg.getSize() == av.alignment.getHeight())
1218 if ((av.hasHiddenColumns && startres < av.getColumnSelection()
1219 .getHiddenBoundaryRight(startres)))
1225 int alWidth = av.alignment.getWidth();
1226 if (av.hasHiddenRows)
1228 int hwidth = av.alignment.getHiddenSequences().getWidth();
1229 if (hwidth > alWidth)
1234 // We can still insert gaps if the selectionGroup
1235 // contains all the sequences
1236 sg.setEndRes(sg.getEndRes() + startres - lastres);
1237 fixedRight = alWidth + startres - lastres;
1248 else if (!insertGap)
1250 // / Are we able to delete?
1251 // ie are all columns blank?
1253 for (g = 0; g < groupSize; g++)
1255 for (int j = startres; j < lastres; j++)
1257 if (groupSeqs[g].getLength() <= j)
1262 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1264 // Not a gap, block edit not valid
1274 // dragging to the right
1275 if (fixedColumns && fixedRight != -1)
1277 for (int j = lastres; j < startres; j++)
1279 insertChar(j, groupSeqs, fixedRight);
1284 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1285 startres, startres - lastres, av.alignment, true);
1290 // dragging to the left
1291 if (fixedColumns && fixedRight != -1)
1293 for (int j = lastres; j > startres; j--)
1295 deleteChar(startres, groupSeqs, fixedRight);
1300 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1301 startres, lastres - startres, av.alignment, true);
1307 // ///Editing a single sequence///////////
1311 // dragging to the right
1312 if (fixedColumns && fixedRight != -1)
1314 for (int j = lastres; j < startres; j++)
1316 insertChar(j, new SequenceI[]
1317 { seq }, fixedRight);
1322 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1323 { seq }, lastres, startres - lastres, av.alignment, true);
1329 // dragging to the left
1330 if (fixedColumns && fixedRight != -1)
1332 for (int j = lastres; j > startres; j--)
1334 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1339 deleteChar(startres, new SequenceI[]
1340 { seq }, fixedRight);
1345 // could be a keyboard edit trying to delete none gaps
1347 for (int m = startres; m < lastres; m++)
1349 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1358 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1359 { seq }, startres, max, av.alignment, true);
1362 }else{//insertGap==false AND editSeq==TRUE;
1363 if (fixedColumns && fixedRight != -1)
1365 for (int j = lastres; j < startres; j++)
1367 insertChar(j, new SequenceI[]
1368 { seq }, fixedRight);
1373 editCommand.appendEdit(EditCommand.INSERT_NUC, new SequenceI[]
1374 { seq }, lastres, startres - lastres, av.alignment, true);
1381 seqCanvas.repaint();
1384 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1386 int blankColumn = fixedColumn;
1387 for (int s = 0; s < seq.length; s++)
1389 // Find the next gap before the end of the visible region boundary
1390 // If lastCol > j, theres a boundary after the gap insertion
1392 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1394 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1396 // Theres a space, so break and insert the gap
1401 if (blankColumn <= j)
1403 blankColumn = fixedColumn;
1409 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1410 av.alignment, true);
1412 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1, av.alignment,
1417 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1420 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1, av.alignment,
1423 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1424 av.alignment, true);
1433 public void mouseEntered(MouseEvent e)
1440 if (scrollThread != null)
1442 scrollThread.running = false;
1443 scrollThread = null;
1453 public void mouseExited(MouseEvent e)
1455 if (av.getWrapAlignment())
1462 scrollThread = new ScrollThread();
1466 public void mouseClicked(MouseEvent evt)
1468 SequenceGroup sg = null;
1469 SequenceI sequence = av.alignment.getSequenceAt(findSeq(evt));
1470 if (evt.getClickCount() > 1)
1472 sg = av.getSelectionGroup();
1473 if (sg != null && sg.getSize() == 1
1474 && sg.getEndRes() - sg.getStartRes() < 2)
1476 av.setSelectionGroup(null);
1479 SequenceFeature[] features = findFeaturesAtRes(
1480 sequence.getDatasetSequence(),
1481 sequence.findPosition(findRes(evt)));
1483 if (features != null && features.length > 0)
1485 SearchResults highlight = new SearchResults();
1486 highlight.addResult(sequence, features[0].getBegin(),
1487 features[0].getEnd());
1488 seqCanvas.highlightSearchResults(highlight);
1490 if (features != null && features.length > 0)
1492 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1493 { sequence }, features, false, ap);
1495 seqCanvas.highlightSearchResults(null);
1500 public void mouseWheelMoved(MouseWheelEvent e)
1503 if (e.getWheelRotation() > 0)
1511 // TODO Update tooltip for new position.
1520 public void doMousePressedDefineMode(MouseEvent evt)
1522 int res = findRes(evt);
1523 int seq = findSeq(evt);
1526 startWrapBlock = wrappedBlock;
1528 if (av.wrapAlignment && seq > av.alignment.getHeight())
1530 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1531 "Cannot edit annotations in wrapped view.",
1532 "Wrapped view - no edit", JOptionPane.WARNING_MESSAGE);
1536 if (seq < 0 || res < 0)
1541 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1543 if ((sequence == null) || (res > sequence.getLength()))
1548 stretchGroup = av.getSelectionGroup();
1550 if (stretchGroup == null)
1552 stretchGroup = av.alignment.findGroup(sequence);
1554 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1555 && (res < stretchGroup.getEndRes()))
1557 av.setSelectionGroup(stretchGroup);
1561 stretchGroup = null;
1564 else if (!stretchGroup.getSequences(null).contains(sequence)
1565 || (stretchGroup.getStartRes() > res)
1566 || (stretchGroup.getEndRes() < res))
1568 stretchGroup = null;
1570 SequenceGroup[] allGroups = av.alignment.findAllGroups(sequence);
1572 if (allGroups != null)
1574 for (int i = 0; i < allGroups.length; i++)
1576 if ((allGroups[i].getStartRes() <= res)
1577 && (allGroups[i].getEndRes() >= res))
1579 stretchGroup = allGroups[i];
1585 av.setSelectionGroup(stretchGroup);
1589 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1591 SequenceFeature[] allFeatures = findFeaturesAtRes(
1592 sequence.getDatasetSequence(), sequence.findPosition(res));
1593 Vector links = new Vector();
1594 for (int i = 0; i < allFeatures.length; i++)
1596 if (allFeatures[i].links != null)
1598 for (int j = 0; j < allFeatures[i].links.size(); j++)
1600 links.addElement(allFeatures[i].links.elementAt(j));
1605 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1606 pop.show(this, evt.getX(), evt.getY());
1612 seqCanvas.cursorX = findRes(evt);
1613 seqCanvas.cursorY = findSeq(evt);
1614 seqCanvas.repaint();
1618 if (stretchGroup == null)
1620 // Only if left mouse button do we want to change group sizes
1622 // define a new group here
1623 SequenceGroup sg = new SequenceGroup();
1624 sg.setStartRes(res);
1626 sg.addSequence(sequence, false);
1627 av.setSelectionGroup(sg);
1631 if (av.getConservationSelected())
1633 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1637 if (av.getAbovePIDThreshold())
1639 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1642 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1644 // Edit end res position of selected group
1645 changeEndRes = true;
1647 else if ((stretchGroup != null)
1648 && (stretchGroup.getStartRes() == res))
1650 // Edit end res position of selected group
1651 changeStartRes = true;
1653 stretchGroup.getWidth();
1656 seqCanvas.repaint();
1665 public void doMouseReleasedDefineMode(MouseEvent evt)
1667 if (stretchGroup == null)
1672 stretchGroup.recalcConservation(); // always do this - annotation has own
1674 if (stretchGroup.cs != null)
1676 if (stretchGroup.cs instanceof ClustalxColourScheme)
1678 ((ClustalxColourScheme) stretchGroup.cs).resetClustalX(
1679 stretchGroup.getSequences(av.hiddenRepSequences),
1680 stretchGroup.getWidth());
1683 if (stretchGroup.cs.conservationApplied())
1685 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1686 stretchGroup.getName());
1690 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1691 stretchGroup.getName());
1694 PaintRefresher.Refresh(this, av.getSequenceSetId());
1695 ap.paintAlignment(true);
1697 changeEndRes = false;
1698 changeStartRes = false;
1699 stretchGroup = null;
1709 public void doMouseDraggedDefineMode(MouseEvent evt)
1711 int res = findRes(evt);
1712 int y = findSeq(evt);
1714 if (wrappedBlock != startWrapBlock)
1719 if (stretchGroup == null)
1724 if (res >= av.alignment.getWidth())
1726 res = av.alignment.getWidth() - 1;
1729 if (stretchGroup.getEndRes() == res)
1731 // Edit end res position of selected group
1732 changeEndRes = true;
1734 else if (stretchGroup.getStartRes() == res)
1736 // Edit start res position of selected group
1737 changeStartRes = true;
1740 if (res < av.getStartRes())
1742 res = av.getStartRes();
1747 if (res > (stretchGroup.getStartRes() - 1))
1749 stretchGroup.setEndRes(res);
1752 else if (changeStartRes)
1754 if (res < (stretchGroup.getEndRes() + 1))
1756 stretchGroup.setStartRes(res);
1760 int dragDirection = 0;
1766 else if (y < oldSeq)
1771 while ((y != oldSeq) && (oldSeq > -1) && (y < av.alignment.getHeight()))
1773 // This routine ensures we don't skip any sequences, as the
1774 // selection is quite slow.
1775 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1777 oldSeq += dragDirection;
1784 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1786 if (stretchGroup.getSequences(null).contains(nextSeq))
1788 stretchGroup.deleteSequence(seq, false);
1794 stretchGroup.addSequence(seq, false);
1797 stretchGroup.addSequence(nextSeq, false);
1806 mouseDragging = true;
1808 if (scrollThread != null)
1810 scrollThread.setEvent(evt);
1813 seqCanvas.repaint();
1816 void scrollCanvas(MouseEvent evt)
1820 if (scrollThread != null)
1822 scrollThread.running = false;
1823 scrollThread = null;
1825 mouseDragging = false;
1829 if (scrollThread == null)
1831 scrollThread = new ScrollThread();
1834 mouseDragging = true;
1835 scrollThread.setEvent(evt);
1840 // this class allows scrolling off the bottom of the visible alignment
1841 class ScrollThread extends Thread
1845 boolean running = false;
1847 public ScrollThread()
1852 public void setEvent(MouseEvent e)
1857 public void stopScrolling()
1870 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1872 running = ap.scrollUp(true);
1875 if (mouseDragging && (evt.getY() >= getHeight())
1876 && (av.alignment.getHeight() > av.getEndSeq()))
1878 running = ap.scrollUp(false);
1881 if (mouseDragging && (evt.getX() < 0))
1883 running = ap.scrollRight(false);
1885 else if (mouseDragging && (evt.getX() >= getWidth()))
1887 running = ap.scrollRight(true);
1894 } catch (Exception ex)
1902 * modify current selection according to a received message.
1904 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1905 SelectionSource source)
1907 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1908 // handles selection messages...
1909 // TODO: extend config options to allow user to control if selections may be
1910 // shared between viewports.
1912 || !av.followSelection
1913 || (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1914 || (source instanceof AlignViewport && ((AlignViewport) source)
1915 .getSequenceSetId().equals(av.getSequenceSetId())))
1919 // do we want to thread this ? (contention with seqsel and colsel locks, I
1921 // rules are: colsel is copied if there is a real intersection between
1922 // sequence selection
1923 boolean repaint = false, copycolsel = true;
1924 // if (!av.isSelectionGroupChanged(false))
1926 SequenceGroup sgroup = null;
1927 if (seqsel != null && seqsel.getSize()>0)
1929 if (av.alignment == null)
1931 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1932 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1933 + " 's alignment is NULL! returning immediatly.");
1936 sgroup = seqsel.intersect(av.alignment,
1937 (av.hasHiddenRows) ? av.hiddenRepSequences : null);
1938 if ((sgroup == null || sgroup.getSize() == 0)
1939 || (colsel == null || colsel.size() == 0))
1941 // don't copy columns if the region didn't intersect.
1945 if (sgroup != null && sgroup.getSize() > 0)
1947 av.setSelectionGroup(sgroup);
1951 av.setSelectionGroup(null);
1953 av.isSelectionGroupChanged(true);
1958 // the current selection is unset or from a previous message
1959 // so import the new colsel.
1960 if (colsel == null || colsel.size() == 0)
1962 if (av.colSel != null)
1970 // TODO: shift colSel according to the intersecting sequences
1971 if (av.colSel == null)
1973 av.colSel = new ColumnSelection(colsel);
1977 av.colSel.setElementsFrom(colsel);
1980 av.isColSelChanged(true);
1983 if (copycolsel && av.hasHiddenColumns
1984 && (av.colSel == null || av.colSel.getHiddenColumns() == null))
1986 System.err.println("Bad things");
1990 // probably finessing with multiple redraws here
1991 PaintRefresher.Refresh(this, av.getSequenceSetId());
1992 // ap.paintAlignment(false);