2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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 int startTag = tmpString.toUpperCase().indexOf("<HTML>");
825 tmpString = tmpString.substring(startTag + 6);
827 int endTag = tmpString.toUpperCase().indexOf("</BODY>");
830 tmpString = tmpString.substring(0, endTag);
832 endTag = tmpString.toUpperCase().indexOf("</HTML>");
835 tmpString = tmpString.substring(0, endTag);
840 tooltipText2.append("; " + tmpString);
844 if (tmpString.indexOf("<") > -1
845 || tmpString.indexOf(">") > -1)
847 // The description does not specify html is to
848 // be used, so we must remove < > symbols
849 tmpString = tmpString.replaceAll("<", "<");
850 tmpString = tmpString.replaceAll(">", ">");
852 tooltipText2.append("; ");
853 tooltipText2.append(tmpString);
858 tooltipText2.append("; " + tmpString);
862 // check score should be shown
863 if (features[i].getScore() != Float.NaN)
865 float[][] rng = (minmax == null) ? null : ((float[][]) minmax
866 .get(features[i].getType()));
867 if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
869 tooltipText2.append(" Score=" + features[i].getScore());
872 if (features[i].getValue("status") != null)
874 String status = features[i].getValue("status").toString();
875 if (status.length() > 0)
877 tooltipText2.append("; (" + features[i].getValue("status")
882 if (features[i].links != null)
884 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
895 * Set status message in alignment panel
898 * aligned sequence object
902 * index of sequence in alignment
903 * @return position of res in sequence
905 int setStatusMessage(SequenceI sequence, int res, int seq)
908 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
909 + sequence.getName());
912 if (av.alignment.isNucleotide())
914 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
918 text.append(" Nucleotide: ");
923 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
926 text.append(" Residue: ");
932 pos = sequence.findPosition(res);
935 text.append(obj + " (" + pos + ")");
938 ap.alignFrame.statusBar.setText(text.toString());
948 public void mouseDragged(MouseEvent evt)
950 if (mouseWheelPressed)
952 int oldWidth = av.charWidth;
954 // Which is bigger, left-right or up-down?
955 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
956 .getX() - lastMousePress.getX()))
958 int fontSize = av.font.getSize();
960 if (evt.getY() < lastMousePress.getY())
964 else if (evt.getY() > lastMousePress.getY())
974 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
975 av.charWidth = oldWidth;
980 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
984 else if (evt.getX() > lastMousePress.getX())
989 ap.paintAlignment(false);
992 FontMetrics fm = getFontMetrics(av.getFont());
993 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
995 lastMousePress = evt.getPoint();
1002 doMouseDraggedDefineMode(evt);
1006 int res = findRes(evt);
1013 if ((lastres == -1) || (lastres == res))
1018 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1020 // dragLeft, delete gap
1021 editSequence(false, false,res);
1025 editSequence(true, false,res);
1028 mouseDragging = true;
1029 if (scrollThread != null)
1031 scrollThread.setEvent(evt);
1035 //TODO: Make it more clever than many booleans
1036 synchronized void editSequence(boolean insertGap, boolean editSeq, int startres)
1039 int fixedRight = -1;
1040 boolean fixedColumns = false;
1041 SequenceGroup sg = av.getSelectionGroup();
1043 SequenceI seq = av.alignment.getSequenceAt(startseq);
1045 // No group, but the sequence may represent a group
1046 if (!groupEditing && av.hasHiddenRows)
1048 if (av.hiddenRepSequences != null
1049 && av.hiddenRepSequences.containsKey(seq))
1051 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1052 groupEditing = true;
1056 StringBuffer message = new StringBuffer();
1059 message.append("Edit group:");
1060 if (editCommand == null)
1062 editCommand = new EditCommand("Edit Group");
1067 message.append("Edit sequence: " + seq.getName());
1068 String label = seq.getName();
1069 if (label.length() > 10)
1071 label = label.substring(0, 10);
1073 if (editCommand == null)
1075 editCommand = new EditCommand("Edit " + label);
1081 message.append(" insert ");
1085 message.append(" delete ");
1088 message.append(Math.abs(startres - lastres) + " gaps.");
1089 ap.alignFrame.statusBar.setText(message.toString());
1091 // Are we editing within a selection group?
1093 || (sg != null && sg.getSequences(av.hiddenRepSequences)
1096 fixedColumns = true;
1098 // sg might be null as the user may only see 1 sequence,
1099 // but the sequence represents a group
1102 if (av.hiddenRepSequences == null
1103 || !av.hiddenRepSequences.containsKey(seq))
1108 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1111 fixedLeft = sg.getStartRes();
1112 fixedRight = sg.getEndRes();
1114 if ((startres < fixedLeft && lastres >= fixedLeft)
1115 || (startres >= fixedLeft && lastres < fixedLeft)
1116 || (startres > fixedRight && lastres <= fixedRight)
1117 || (startres <= fixedRight && lastres > fixedRight))
1123 if (fixedLeft > startres)
1125 fixedRight = fixedLeft - 1;
1128 else if (fixedRight < startres)
1130 fixedLeft = fixedRight;
1135 if (av.hasHiddenColumns)
1137 fixedColumns = true;
1138 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1139 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1141 if ((insertGap && startres > y1 && lastres < y1)
1142 || (!insertGap && startres < y2 && lastres > y2))
1148 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1149 // Selection spans a hidden region
1150 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1158 fixedRight = y2 - 1;
1165 Vector vseqs = sg.getSequences(av.hiddenRepSequences);
1166 int g, groupSize = vseqs.size();
1167 SequenceI[] groupSeqs = new SequenceI[groupSize];
1168 for (g = 0; g < groupSeqs.length; g++)
1170 groupSeqs[g] = (SequenceI) vseqs.elementAt(g);
1176 // If the user has selected the whole sequence, and is dragging to
1177 // the right, we can still extend the alignment and selectionGroup
1178 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1179 && sg.getEndRes() == av.alignment.getWidth() - 1)
1181 sg.setEndRes(av.alignment.getWidth() + startres - lastres);
1182 fixedRight = sg.getEndRes();
1185 // Is it valid with fixed columns??
1186 // Find the next gap before the end
1187 // of the visible region boundary
1188 boolean blank = false;
1189 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1193 for (g = 0; g < groupSize; g++)
1195 for (int j = 0; j < startres - lastres; j++)
1197 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1198 .getCharAt(fixedRight - j)))
1213 if (sg.getSize() == av.alignment.getHeight())
1215 if ((av.hasHiddenColumns && startres < av.getColumnSelection()
1216 .getHiddenBoundaryRight(startres)))
1222 int alWidth = av.alignment.getWidth();
1223 if (av.hasHiddenRows)
1225 int hwidth = av.alignment.getHiddenSequences().getWidth();
1226 if (hwidth > alWidth)
1231 // We can still insert gaps if the selectionGroup
1232 // contains all the sequences
1233 sg.setEndRes(sg.getEndRes() + startres - lastres);
1234 fixedRight = alWidth + startres - lastres;
1245 else if (!insertGap)
1247 // / Are we able to delete?
1248 // ie are all columns blank?
1250 for (g = 0; g < groupSize; g++)
1252 for (int j = startres; j < lastres; j++)
1254 if (groupSeqs[g].getLength() <= j)
1259 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1261 // Not a gap, block edit not valid
1271 // dragging to the right
1272 if (fixedColumns && fixedRight != -1)
1274 for (int j = lastres; j < startres; j++)
1276 insertChar(j, groupSeqs, fixedRight);
1281 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1282 startres, startres - lastres, av.alignment, true);
1287 // dragging to the left
1288 if (fixedColumns && fixedRight != -1)
1290 for (int j = lastres; j > startres; j--)
1292 deleteChar(startres, groupSeqs, fixedRight);
1297 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1298 startres, lastres - startres, av.alignment, true);
1304 // ///Editing a single sequence///////////
1308 // dragging to the right
1309 if (fixedColumns && fixedRight != -1)
1311 for (int j = lastres; j < startres; j++)
1313 insertChar(j, new SequenceI[]
1314 { seq }, fixedRight);
1319 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1320 { seq }, lastres, startres - lastres, av.alignment, true);
1326 // dragging to the left
1327 if (fixedColumns && fixedRight != -1)
1329 for (int j = lastres; j > startres; j--)
1331 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1336 deleteChar(startres, new SequenceI[]
1337 { seq }, fixedRight);
1342 // could be a keyboard edit trying to delete none gaps
1344 for (int m = startres; m < lastres; m++)
1346 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1355 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1356 { seq }, startres, max, av.alignment, true);
1359 }else{//insertGap==false AND editSeq==TRUE;
1360 if (fixedColumns && fixedRight != -1)
1362 for (int j = lastres; j < startres; j++)
1364 insertChar(j, new SequenceI[]
1365 { seq }, fixedRight);
1370 editCommand.appendEdit(EditCommand.INSERT_NUC, new SequenceI[]
1371 { seq }, lastres, startres - lastres, av.alignment, true);
1378 seqCanvas.repaint();
1381 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1383 int blankColumn = fixedColumn;
1384 for (int s = 0; s < seq.length; s++)
1386 // Find the next gap before the end of the visible region boundary
1387 // If lastCol > j, theres a boundary after the gap insertion
1389 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1391 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1393 // Theres a space, so break and insert the gap
1398 if (blankColumn <= j)
1400 blankColumn = fixedColumn;
1406 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1407 av.alignment, true);
1409 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1, av.alignment,
1414 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1417 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1, av.alignment,
1420 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1421 av.alignment, true);
1430 public void mouseEntered(MouseEvent e)
1437 if (scrollThread != null)
1439 scrollThread.running = false;
1440 scrollThread = null;
1450 public void mouseExited(MouseEvent e)
1452 if (av.getWrapAlignment())
1459 scrollThread = new ScrollThread();
1463 public void mouseClicked(MouseEvent evt)
1465 SequenceGroup sg = null;
1466 SequenceI sequence = av.alignment.getSequenceAt(findSeq(evt));
1467 if (evt.getClickCount() > 1)
1469 sg = av.getSelectionGroup();
1470 if (sg != null && sg.getSize() == 1
1471 && sg.getEndRes() - sg.getStartRes() < 2)
1473 av.setSelectionGroup(null);
1476 SequenceFeature[] features = findFeaturesAtRes(
1477 sequence.getDatasetSequence(),
1478 sequence.findPosition(findRes(evt)));
1480 if (features != null && features.length > 0)
1482 SearchResults highlight = new SearchResults();
1483 highlight.addResult(sequence, features[0].getBegin(),
1484 features[0].getEnd());
1485 seqCanvas.highlightSearchResults(highlight);
1487 if (features != null && features.length > 0)
1489 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1490 { sequence }, features, false, ap);
1492 seqCanvas.highlightSearchResults(null);
1497 public void mouseWheelMoved(MouseWheelEvent e)
1500 if (e.getWheelRotation() > 0)
1508 // TODO Update tooltip for new position.
1517 public void doMousePressedDefineMode(MouseEvent evt)
1519 int res = findRes(evt);
1520 int seq = findSeq(evt);
1523 startWrapBlock = wrappedBlock;
1525 if (av.wrapAlignment && seq > av.alignment.getHeight())
1527 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1528 "Cannot edit annotations in wrapped view.",
1529 "Wrapped view - no edit", JOptionPane.WARNING_MESSAGE);
1533 if (seq < 0 || res < 0)
1538 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1540 if ((sequence == null) || (res > sequence.getLength()))
1545 stretchGroup = av.getSelectionGroup();
1547 if (stretchGroup == null)
1549 stretchGroup = av.alignment.findGroup(sequence);
1551 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1552 && (res < stretchGroup.getEndRes()))
1554 av.setSelectionGroup(stretchGroup);
1558 stretchGroup = null;
1561 else if (!stretchGroup.getSequences(null).contains(sequence)
1562 || (stretchGroup.getStartRes() > res)
1563 || (stretchGroup.getEndRes() < res))
1565 stretchGroup = null;
1567 SequenceGroup[] allGroups = av.alignment.findAllGroups(sequence);
1569 if (allGroups != null)
1571 for (int i = 0; i < allGroups.length; i++)
1573 if ((allGroups[i].getStartRes() <= res)
1574 && (allGroups[i].getEndRes() >= res))
1576 stretchGroup = allGroups[i];
1582 av.setSelectionGroup(stretchGroup);
1586 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1588 SequenceFeature[] allFeatures = findFeaturesAtRes(
1589 sequence.getDatasetSequence(), sequence.findPosition(res));
1590 Vector links = new Vector();
1591 for (int i = 0; i < allFeatures.length; i++)
1593 if (allFeatures[i].links != null)
1595 for (int j = 0; j < allFeatures[i].links.size(); j++)
1597 links.addElement(allFeatures[i].links.elementAt(j));
1602 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1603 pop.show(this, evt.getX(), evt.getY());
1609 seqCanvas.cursorX = findRes(evt);
1610 seqCanvas.cursorY = findSeq(evt);
1611 seqCanvas.repaint();
1615 if (stretchGroup == null)
1617 // Only if left mouse button do we want to change group sizes
1619 // define a new group here
1620 SequenceGroup sg = new SequenceGroup();
1621 sg.setStartRes(res);
1623 sg.addSequence(sequence, false);
1624 av.setSelectionGroup(sg);
1628 if (av.getConservationSelected())
1630 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1634 if (av.getAbovePIDThreshold())
1636 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1639 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1641 // Edit end res position of selected group
1642 changeEndRes = true;
1644 else if ((stretchGroup != null)
1645 && (stretchGroup.getStartRes() == res))
1647 // Edit end res position of selected group
1648 changeStartRes = true;
1650 stretchGroup.getWidth();
1653 seqCanvas.repaint();
1662 public void doMouseReleasedDefineMode(MouseEvent evt)
1664 if (stretchGroup == null)
1669 stretchGroup.recalcConservation(); // always do this - annotation has own
1671 if (stretchGroup.cs != null)
1673 if (stretchGroup.cs instanceof ClustalxColourScheme)
1675 ((ClustalxColourScheme) stretchGroup.cs).resetClustalX(
1676 stretchGroup.getSequences(av.hiddenRepSequences),
1677 stretchGroup.getWidth());
1680 if (stretchGroup.cs.conservationApplied())
1682 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1683 stretchGroup.getName());
1687 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1688 stretchGroup.getName());
1691 PaintRefresher.Refresh(this, av.getSequenceSetId());
1692 ap.paintAlignment(true);
1694 changeEndRes = false;
1695 changeStartRes = false;
1696 stretchGroup = null;
1706 public void doMouseDraggedDefineMode(MouseEvent evt)
1708 int res = findRes(evt);
1709 int y = findSeq(evt);
1711 if (wrappedBlock != startWrapBlock)
1716 if (stretchGroup == null)
1721 if (res >= av.alignment.getWidth())
1723 res = av.alignment.getWidth() - 1;
1726 if (stretchGroup.getEndRes() == res)
1728 // Edit end res position of selected group
1729 changeEndRes = true;
1731 else if (stretchGroup.getStartRes() == res)
1733 // Edit start res position of selected group
1734 changeStartRes = true;
1737 if (res < av.getStartRes())
1739 res = av.getStartRes();
1744 if (res > (stretchGroup.getStartRes() - 1))
1746 stretchGroup.setEndRes(res);
1749 else if (changeStartRes)
1751 if (res < (stretchGroup.getEndRes() + 1))
1753 stretchGroup.setStartRes(res);
1757 int dragDirection = 0;
1763 else if (y < oldSeq)
1768 while ((y != oldSeq) && (oldSeq > -1) && (y < av.alignment.getHeight()))
1770 // This routine ensures we don't skip any sequences, as the
1771 // selection is quite slow.
1772 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1774 oldSeq += dragDirection;
1781 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1783 if (stretchGroup.getSequences(null).contains(nextSeq))
1785 stretchGroup.deleteSequence(seq, false);
1791 stretchGroup.addSequence(seq, false);
1794 stretchGroup.addSequence(nextSeq, false);
1803 mouseDragging = true;
1805 if (scrollThread != null)
1807 scrollThread.setEvent(evt);
1810 seqCanvas.repaint();
1813 void scrollCanvas(MouseEvent evt)
1817 if (scrollThread != null)
1819 scrollThread.running = false;
1820 scrollThread = null;
1822 mouseDragging = false;
1826 if (scrollThread == null)
1828 scrollThread = new ScrollThread();
1831 mouseDragging = true;
1832 scrollThread.setEvent(evt);
1837 // this class allows scrolling off the bottom of the visible alignment
1838 class ScrollThread extends Thread
1842 boolean running = false;
1844 public ScrollThread()
1849 public void setEvent(MouseEvent e)
1854 public void stopScrolling()
1867 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1869 running = ap.scrollUp(true);
1872 if (mouseDragging && (evt.getY() >= getHeight())
1873 && (av.alignment.getHeight() > av.getEndSeq()))
1875 running = ap.scrollUp(false);
1878 if (mouseDragging && (evt.getX() < 0))
1880 running = ap.scrollRight(false);
1882 else if (mouseDragging && (evt.getX() >= getWidth()))
1884 running = ap.scrollRight(true);
1891 } catch (Exception ex)
1899 * modify current selection according to a received message.
1901 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1902 SelectionSource source)
1904 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1905 // handles selection messages...
1906 // TODO: extend config options to allow user to control if selections may be
1907 // shared between viewports.
1909 || !av.followSelection
1910 || (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1911 || (source instanceof AlignViewport && ((AlignViewport) source)
1912 .getSequenceSetId().equals(av.getSequenceSetId())))
1916 // do we want to thread this ? (contention with seqsel and colsel locks, I
1918 // rules are: colsel is copied if there is a real intersection between
1919 // sequence selection
1920 boolean repaint = false, copycolsel = true;
1921 // if (!av.isSelectionGroupChanged(false))
1923 SequenceGroup sgroup = null;
1924 if (seqsel != null && seqsel.getSize()>0)
1926 if (av.alignment == null)
1928 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1929 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1930 + " 's alignment is NULL! returning immediatly.");
1933 sgroup = seqsel.intersect(av.alignment,
1934 (av.hasHiddenRows) ? av.hiddenRepSequences : null);
1935 if ((sgroup == null || sgroup.getSize() == 0)
1936 || (colsel == null || colsel.size() == 0))
1938 // don't copy columns if the region didn't intersect.
1942 if (sgroup != null && sgroup.getSize() > 0)
1944 av.setSelectionGroup(sgroup);
1948 av.setSelectionGroup(null);
1950 av.isSelectionGroupChanged(true);
1955 // the current selection is unset or from a previous message
1956 // so import the new colsel.
1957 if (colsel == null || colsel.size() == 0)
1959 if (av.colSel != null)
1967 // TODO: shift colSel according to the intersecting sequences
1968 if (av.colSel == null)
1970 av.colSel = new ColumnSelection(colsel);
1974 av.colSel.setElementsFrom(colsel);
1977 av.isColSelChanged(true);
1980 if (copycolsel && av.hasHiddenColumns
1981 && (av.colSel == null || av.colSel.getHiddenColumns() == null))
1983 System.err.println("Bad things");
1987 // probably finessing with multiple redraws here
1988 PaintRefresher.Refresh(this, av.getSequenceSetId());
1989 // ap.paintAlignment(false);