2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 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, seqCanvas.cursorX + getKeyboardNo1());
477 void deleteGapAtCursor(boolean group)
479 groupEditing = group;
480 startseq = seqCanvas.cursorY;
481 lastres = seqCanvas.cursorX + getKeyboardNo1();
482 editSequence(false, seqCanvas.cursorX);
486 void numberPressed(char value)
488 if (keyboardNo1 == null)
490 keyboardNo1 = new StringBuffer();
493 if (keyboardNo2 != null)
495 keyboardNo2.append(value);
499 keyboardNo1.append(value);
505 if (keyboardNo1 == null)
509 int value = Integer.parseInt(keyboardNo1.toString());
517 if (keyboardNo2 == null)
521 int value = Integer.parseInt(keyboardNo2.toString());
533 public void mouseReleased(MouseEvent evt)
535 mouseDragging = false;
536 mouseWheelPressed = false;
540 doMouseReleasedDefineMode(evt);
553 public void mousePressed(MouseEvent evt)
555 lastMousePress = evt.getPoint();
557 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
559 mouseWheelPressed = true;
563 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
565 if (evt.isAltDown() || evt.isControlDown())
573 doMousePressedDefineMode(evt);
577 int seq = findSeq(evt);
578 int res = findRes(evt);
580 if (seq < 0 || res < 0)
585 if ((seq < av.getAlignment().getHeight())
586 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
602 public void mouseOverSequence(SequenceI sequence, int index, int pos)
604 String tmp = sequence.hashCode() + " " + index + " " + pos;
606 if (lastMessage == null || !lastMessage.equals(tmp))
608 // System.err.println("mouseOver Sequence: "+tmp);
609 ssm.mouseOverSequence(sequence, index, pos, av);
614 public void highlightSequence(SearchResults results)
616 if (av.followHighlight)
618 if (ap.scrollToPosition(results, false))
620 seqCanvas.revalidate();
623 seqCanvas.highlightSearchResults(results);
626 public void updateColours(SequenceI seq, int index)
628 System.out.println("update the seqPanel colours");
638 public void mouseMoved(MouseEvent evt)
642 // This is because MacOSX creates a mouseMoved
643 // If control is down, other platforms will not.
647 int res = findRes(evt);
648 int seq = findSeq(evt);
650 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
655 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
657 if (res >= sequence.getLength())
662 pos = setStatusMessage(sequence, res, seq);
663 if (ssm != null && pos > -1)
664 mouseOverSequence(sequence, res, pos);
666 tooltipText.setLength(6); // Cuts the buffer back to <html>
668 SequenceGroup[] groups = av.alignment.findAllGroups(sequence);
671 for (int g = 0; g < groups.length; g++)
673 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
675 if (tooltipText.length() > 6)
677 tooltipText.append("<br>");
680 if (!groups[g].getName().startsWith("JTreeGroup")
681 && !groups[g].getName().startsWith("JGroup"))
683 tooltipText.append(groups[g].getName());
686 if (groups[g].getDescription() != null)
688 tooltipText.append(": " + groups[g].getDescription());
694 // use aa to see if the mouse pointer is on a
695 if (av.showSequenceFeatures)
698 SequenceFeature[] features = findFeaturesAtRes(
699 sequence.getDatasetSequence(),
700 rpos = sequence.findPosition(res));
701 appendFeatures(tooltipText, linkImageURL.toString(), rpos, features,
702 this.ap.seqPanel.seqCanvas.fr.minmax);
704 if (tooltipText.length() == 6) // <html></html>
706 setToolTipText(null);
711 tooltipText.append("</html>");
712 if (lastTooltip == null
713 || !lastTooltip.equals(tooltipText.toString()))
715 setToolTipText(tooltipText.toString());
716 lastTooltip = tooltipText.toString();
723 private Point lastp = null;
728 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
730 public Point getToolTipLocation(MouseEvent event)
732 int x = event.getX(), w = getWidth();
733 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
736 if (!event.isShiftDown() || p == null)
738 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
739 event.getX() + wdth, event.getY() - 20) : null;
742 * TODO: try to modify position region is not obcured by tooltip
748 * appends the features at rpos to the given stringbuffer ready for display in
751 * @param tooltipText2
752 * @param linkImageURL
755 * TODO refactor to Jalview 'utilities' somehow.
757 public void appendFeatures(StringBuffer tooltipText2,
758 String linkImageURL, int rpos, SequenceFeature[] features)
760 appendFeatures(tooltipText2, linkImageURL, rpos, features, null);
763 public void appendFeatures(StringBuffer tooltipText2, String string,
764 int rpos, SequenceFeature[] features, Hashtable minmax)
767 if (features != null)
769 for (int i = 0; i < features.length; i++)
771 if (features[i].getType().equals("disulfide bond"))
773 if (features[i].getBegin() == rpos
774 || features[i].getEnd() == rpos)
776 if (tooltipText2.length() > 6)
778 tooltipText2.append("<br>");
780 tooltipText2.append("disulfide bond " + features[i].getBegin()
781 + ":" + features[i].getEnd());
782 if (features[i].links != null)
784 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
790 if (tooltipText2.length() > 6)
792 tooltipText2.append("<br>");
794 // TODO: remove this hack to display link only features
795 boolean linkOnly = features[i].getValue("linkonly") != null;
798 tooltipText2.append(features[i].getType() + " ");
801 // we are marking a positional feature
802 tooltipText2.append(features[i].begin);
804 if (features[i].begin != features[i].end)
806 tooltipText2.append(" " + features[i].end);
809 if (features[i].getDescription() != null
810 && !features[i].description.equals(features[i]
813 tmpString = features[i].getDescription();
814 String tmp2up=tmpString.toUpperCase();
815 int startTag = tmp2up.indexOf("<HTML>");
818 tmpString = tmpString.substring(startTag + 6);
819 tmp2up = tmp2up.substring(startTag+6);
821 int endTag = tmp2up.indexOf("</BODY>");
824 tmpString = tmpString.substring(0, endTag);
825 tmp2up = tmp2up.substring(0, endTag);
827 endTag = tmp2up.indexOf("</HTML>");
830 tmpString = tmpString.substring(0, endTag);
835 tooltipText2.append("; " + tmpString);
839 if (tmpString.indexOf("<") > -1
840 || tmpString.indexOf(">") > -1)
842 // The description does not specify html is to
843 // be used, so we must remove < > symbols
844 tmpString = tmpString.replaceAll("<", "<");
845 tmpString = tmpString.replaceAll(">", ">");
847 tooltipText2.append("; ");
848 tooltipText2.append(tmpString);
853 tooltipText2.append("; " + tmpString);
857 // check score should be shown
858 if (features[i].getScore() != Float.NaN)
860 float[][] rng = (minmax == null) ? null : ((float[][]) minmax
861 .get(features[i].getType()));
862 if (rng != null && rng[0] != null && rng[0][0] != rng[0][1])
864 tooltipText2.append(" Score=" + features[i].getScore());
867 if (features[i].getValue("status") != null)
869 String status = features[i].getValue("status").toString();
870 if (status.length() > 0)
872 tooltipText2.append("; (" + features[i].getValue("status")
877 if (features[i].links != null)
879 tooltipText2.append(" <img src=\"" + linkImageURL + "\">");
890 * Set status message in alignment panel
893 * aligned sequence object
897 * index of sequence in alignment
898 * @return position of res in sequence
900 int setStatusMessage(SequenceI sequence, int res, int seq)
903 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
904 + sequence.getName());
907 if (av.alignment.isNucleotide())
909 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
913 text.append(" Nucleotide: ");
918 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
921 text.append(" Residue: ");
927 pos = sequence.findPosition(res);
930 text.append(obj + " (" + pos + ")");
933 ap.alignFrame.statusBar.setText(text.toString());
943 public void mouseDragged(MouseEvent evt)
945 if (mouseWheelPressed)
947 int oldWidth = av.charWidth;
949 // Which is bigger, left-right or up-down?
950 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
951 .getX() - lastMousePress.getX()))
953 int fontSize = av.font.getSize();
955 if (evt.getY() < lastMousePress.getY())
959 else if (evt.getY() > lastMousePress.getY())
969 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
970 av.charWidth = oldWidth;
975 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
979 else if (evt.getX() > lastMousePress.getX())
984 ap.paintAlignment(false);
987 FontMetrics fm = getFontMetrics(av.getFont());
988 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
990 lastMousePress = evt.getPoint();
997 doMouseDraggedDefineMode(evt);
1001 int res = findRes(evt);
1008 if ((lastres == -1) || (lastres == res))
1013 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1015 // dragLeft, delete gap
1016 editSequence(false, res);
1020 editSequence(true, res);
1023 mouseDragging = true;
1024 if (scrollThread != null)
1026 scrollThread.setEvent(evt);
1030 synchronized void editSequence(boolean insertGap, int startres)
1033 int fixedRight = -1;
1034 boolean fixedColumns = false;
1035 SequenceGroup sg = av.getSelectionGroup();
1037 SequenceI seq = av.alignment.getSequenceAt(startseq);
1039 // No group, but the sequence may represent a group
1040 if (!groupEditing && av.hasHiddenRows)
1042 if (av.hiddenRepSequences != null
1043 && av.hiddenRepSequences.containsKey(seq))
1045 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1046 groupEditing = true;
1050 StringBuffer message = new StringBuffer();
1053 message.append("Edit group:");
1054 if (editCommand == null)
1056 editCommand = new EditCommand("Edit Group");
1061 message.append("Edit sequence: " + seq.getName());
1062 String label = seq.getName();
1063 if (label.length() > 10)
1065 label = label.substring(0, 10);
1067 if (editCommand == null)
1069 editCommand = new EditCommand("Edit " + label);
1075 message.append(" insert ");
1079 message.append(" delete ");
1082 message.append(Math.abs(startres - lastres) + " gaps.");
1083 ap.alignFrame.statusBar.setText(message.toString());
1085 // Are we editing within a selection group?
1087 || (sg != null && sg.getSequences(av.hiddenRepSequences)
1090 fixedColumns = true;
1092 // sg might be null as the user may only see 1 sequence,
1093 // but the sequence represents a group
1096 if (av.hiddenRepSequences == null
1097 || !av.hiddenRepSequences.containsKey(seq))
1102 sg = (SequenceGroup) av.hiddenRepSequences.get(seq);
1105 fixedLeft = sg.getStartRes();
1106 fixedRight = sg.getEndRes();
1108 if ((startres < fixedLeft && lastres >= fixedLeft)
1109 || (startres >= fixedLeft && lastres < fixedLeft)
1110 || (startres > fixedRight && lastres <= fixedRight)
1111 || (startres <= fixedRight && lastres > fixedRight))
1117 if (fixedLeft > startres)
1119 fixedRight = fixedLeft - 1;
1122 else if (fixedRight < startres)
1124 fixedLeft = fixedRight;
1129 if (av.hasHiddenColumns)
1131 fixedColumns = true;
1132 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1133 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1135 if ((insertGap && startres > y1 && lastres < y1)
1136 || (!insertGap && startres < y2 && lastres > y2))
1142 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1143 // Selection spans a hidden region
1144 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1152 fixedRight = y2 - 1;
1159 Vector vseqs = sg.getSequences(av.hiddenRepSequences);
1160 int g, groupSize = vseqs.size();
1161 SequenceI[] groupSeqs = new SequenceI[groupSize];
1162 for (g = 0; g < groupSeqs.length; g++)
1164 groupSeqs[g] = (SequenceI) vseqs.elementAt(g);
1170 // If the user has selected the whole sequence, and is dragging to
1171 // the right, we can still extend the alignment and selectionGroup
1172 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1173 && sg.getEndRes() == av.alignment.getWidth() - 1)
1175 sg.setEndRes(av.alignment.getWidth() + startres - lastres);
1176 fixedRight = sg.getEndRes();
1179 // Is it valid with fixed columns??
1180 // Find the next gap before the end
1181 // of the visible region boundary
1182 boolean blank = false;
1183 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1187 for (g = 0; g < groupSize; g++)
1189 for (int j = 0; j < startres - lastres; j++)
1191 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1192 .getCharAt(fixedRight - j)))
1207 if (sg.getSize() == av.alignment.getHeight())
1209 if ((av.hasHiddenColumns && startres < av.getColumnSelection()
1210 .getHiddenBoundaryRight(startres)))
1216 int alWidth = av.alignment.getWidth();
1217 if (av.hasHiddenRows)
1219 int hwidth = av.alignment.getHiddenSequences().getWidth();
1220 if (hwidth > alWidth)
1225 // We can still insert gaps if the selectionGroup
1226 // contains all the sequences
1227 sg.setEndRes(sg.getEndRes() + startres - lastres);
1228 fixedRight = alWidth + startres - lastres;
1239 else if (!insertGap)
1241 // / Are we able to delete?
1242 // ie are all columns blank?
1244 for (g = 0; g < groupSize; g++)
1246 for (int j = startres; j < lastres; j++)
1248 if (groupSeqs[g].getLength() <= j)
1253 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1255 // Not a gap, block edit not valid
1265 // dragging to the right
1266 if (fixedColumns && fixedRight != -1)
1268 for (int j = lastres; j < startres; j++)
1270 insertChar(j, groupSeqs, fixedRight);
1275 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1276 startres, startres - lastres, av.alignment, true);
1281 // dragging to the left
1282 if (fixedColumns && fixedRight != -1)
1284 for (int j = lastres; j > startres; j--)
1286 deleteChar(startres, groupSeqs, fixedRight);
1291 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1292 startres, lastres - startres, av.alignment, true);
1298 // ///Editing a single sequence///////////
1302 // dragging to the right
1303 if (fixedColumns && fixedRight != -1)
1305 for (int j = lastres; j < startres; j++)
1307 insertChar(j, new SequenceI[]
1308 { seq }, fixedRight);
1313 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1314 { seq }, lastres, startres - lastres, av.alignment, true);
1319 // dragging to the left
1320 if (fixedColumns && fixedRight != -1)
1322 for (int j = lastres; j > startres; j--)
1324 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1329 deleteChar(startres, new SequenceI[]
1330 { seq }, fixedRight);
1335 // could be a keyboard edit trying to delete none gaps
1337 for (int m = startres; m < lastres; m++)
1339 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1348 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1349 { seq }, startres, max, av.alignment, true);
1356 seqCanvas.repaint();
1359 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1361 int blankColumn = fixedColumn;
1362 for (int s = 0; s < seq.length; s++)
1364 // Find the next gap before the end of the visible region boundary
1365 // If lastCol > j, theres a boundary after the gap insertion
1367 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1369 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1371 // Theres a space, so break and insert the gap
1376 if (blankColumn <= j)
1378 blankColumn = fixedColumn;
1384 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1385 av.alignment, true);
1387 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1, av.alignment,
1392 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1395 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1, av.alignment,
1398 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1399 av.alignment, true);
1408 public void mouseEntered(MouseEvent e)
1415 if (scrollThread != null)
1417 scrollThread.running = false;
1418 scrollThread = null;
1428 public void mouseExited(MouseEvent e)
1430 if (av.getWrapAlignment())
1437 scrollThread = new ScrollThread();
1441 public void mouseClicked(MouseEvent evt)
1443 SequenceGroup sg = null;
1444 SequenceI sequence = av.alignment.getSequenceAt(findSeq(evt));
1445 if (evt.getClickCount() > 1)
1447 sg = av.getSelectionGroup();
1448 if (sg != null && sg.getSize() == 1
1449 && sg.getEndRes() - sg.getStartRes() < 2)
1451 av.setSelectionGroup(null);
1454 SequenceFeature[] features = findFeaturesAtRes(
1455 sequence.getDatasetSequence(),
1456 sequence.findPosition(findRes(evt)));
1458 if (features != null && features.length > 0)
1460 SearchResults highlight = new SearchResults();
1461 highlight.addResult(sequence, features[0].getBegin(),
1462 features[0].getEnd());
1463 seqCanvas.highlightSearchResults(highlight);
1465 if (features != null && features.length > 0)
1467 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1468 { sequence }, features, false, ap);
1470 seqCanvas.highlightSearchResults(null);
1475 public void mouseWheelMoved(MouseWheelEvent e)
1478 if (e.getWheelRotation() > 0)
1486 // TODO Update tooltip for new position.
1495 public void doMousePressedDefineMode(MouseEvent evt)
1497 int res = findRes(evt);
1498 int seq = findSeq(evt);
1501 startWrapBlock = wrappedBlock;
1503 if (av.wrapAlignment && seq > av.alignment.getHeight())
1505 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1506 "Cannot edit annotations in wrapped view.",
1507 "Wrapped view - no edit", JOptionPane.WARNING_MESSAGE);
1511 if (seq < 0 || res < 0)
1516 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1518 if ((sequence == null) || (res > sequence.getLength()))
1523 stretchGroup = av.getSelectionGroup();
1525 if (stretchGroup == null)
1527 stretchGroup = av.alignment.findGroup(sequence);
1529 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1530 && (res < stretchGroup.getEndRes()))
1532 av.setSelectionGroup(stretchGroup);
1536 stretchGroup = null;
1539 else if (!stretchGroup.getSequences(null).contains(sequence)
1540 || (stretchGroup.getStartRes() > res)
1541 || (stretchGroup.getEndRes() < res))
1543 stretchGroup = null;
1545 SequenceGroup[] allGroups = av.alignment.findAllGroups(sequence);
1547 if (allGroups != null)
1549 for (int i = 0; i < allGroups.length; i++)
1551 if ((allGroups[i].getStartRes() <= res)
1552 && (allGroups[i].getEndRes() >= res))
1554 stretchGroup = allGroups[i];
1560 av.setSelectionGroup(stretchGroup);
1564 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1566 SequenceFeature[] allFeatures = findFeaturesAtRes(
1567 sequence.getDatasetSequence(), sequence.findPosition(res));
1568 Vector links = new Vector();
1569 for (int i = 0; i < allFeatures.length; i++)
1571 if (allFeatures[i].links != null)
1573 for (int j = 0; j < allFeatures[i].links.size(); j++)
1575 links.addElement(allFeatures[i].links.elementAt(j));
1580 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1581 pop.show(this, evt.getX(), evt.getY());
1587 seqCanvas.cursorX = findRes(evt);
1588 seqCanvas.cursorY = findSeq(evt);
1589 seqCanvas.repaint();
1593 if (stretchGroup == null)
1595 // Only if left mouse button do we want to change group sizes
1597 // define a new group here
1598 SequenceGroup sg = new SequenceGroup();
1599 sg.setStartRes(res);
1601 sg.addSequence(sequence, false);
1602 av.setSelectionGroup(sg);
1606 if (av.getConservationSelected())
1608 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1612 if (av.getAbovePIDThreshold())
1614 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1617 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1619 // Edit end res position of selected group
1620 changeEndRes = true;
1622 else if ((stretchGroup != null)
1623 && (stretchGroup.getStartRes() == res))
1625 // Edit end res position of selected group
1626 changeStartRes = true;
1628 stretchGroup.getWidth();
1631 seqCanvas.repaint();
1640 public void doMouseReleasedDefineMode(MouseEvent evt)
1642 if (stretchGroup == null)
1647 stretchGroup.recalcConservation(); // always do this - annotation has own
1649 if (stretchGroup.cs != null)
1651 if (stretchGroup.cs instanceof ClustalxColourScheme)
1653 ((ClustalxColourScheme) stretchGroup.cs).resetClustalX(
1654 stretchGroup.getSequences(av.hiddenRepSequences),
1655 stretchGroup.getWidth());
1658 if (stretchGroup.cs.conservationApplied())
1660 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1661 stretchGroup.getName());
1665 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1666 stretchGroup.getName());
1669 PaintRefresher.Refresh(this, av.getSequenceSetId());
1670 ap.paintAlignment(true);
1672 changeEndRes = false;
1673 changeStartRes = false;
1674 stretchGroup = null;
1684 public void doMouseDraggedDefineMode(MouseEvent evt)
1686 int res = findRes(evt);
1687 int y = findSeq(evt);
1689 if (wrappedBlock != startWrapBlock)
1694 if (stretchGroup == null)
1699 if (res >= av.alignment.getWidth())
1701 res = av.alignment.getWidth() - 1;
1704 if (stretchGroup.getEndRes() == res)
1706 // Edit end res position of selected group
1707 changeEndRes = true;
1709 else if (stretchGroup.getStartRes() == res)
1711 // Edit start res position of selected group
1712 changeStartRes = true;
1715 if (res < av.getStartRes())
1717 res = av.getStartRes();
1722 if (res > (stretchGroup.getStartRes() - 1))
1724 stretchGroup.setEndRes(res);
1727 else if (changeStartRes)
1729 if (res < (stretchGroup.getEndRes() + 1))
1731 stretchGroup.setStartRes(res);
1735 int dragDirection = 0;
1741 else if (y < oldSeq)
1746 while ((y != oldSeq) && (oldSeq > -1) && (y < av.alignment.getHeight()))
1748 // This routine ensures we don't skip any sequences, as the
1749 // selection is quite slow.
1750 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1752 oldSeq += dragDirection;
1759 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1761 if (stretchGroup.getSequences(null).contains(nextSeq))
1763 stretchGroup.deleteSequence(seq, false);
1769 stretchGroup.addSequence(seq, false);
1772 stretchGroup.addSequence(nextSeq, false);
1781 mouseDragging = true;
1783 if (scrollThread != null)
1785 scrollThread.setEvent(evt);
1788 seqCanvas.repaint();
1791 void scrollCanvas(MouseEvent evt)
1795 if (scrollThread != null)
1797 scrollThread.running = false;
1798 scrollThread = null;
1800 mouseDragging = false;
1804 if (scrollThread == null)
1806 scrollThread = new ScrollThread();
1809 mouseDragging = true;
1810 scrollThread.setEvent(evt);
1815 // this class allows scrolling off the bottom of the visible alignment
1816 class ScrollThread extends Thread
1820 boolean running = false;
1822 public ScrollThread()
1827 public void setEvent(MouseEvent e)
1832 public void stopScrolling()
1845 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1847 running = ap.scrollUp(true);
1850 if (mouseDragging && (evt.getY() >= getHeight())
1851 && (av.alignment.getHeight() > av.getEndSeq()))
1853 running = ap.scrollUp(false);
1856 if (mouseDragging && (evt.getX() < 0))
1858 running = ap.scrollRight(false);
1860 else if (mouseDragging && (evt.getX() >= getWidth()))
1862 running = ap.scrollRight(true);
1869 } catch (Exception ex)
1877 * modify current selection according to a received message.
1879 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1880 SelectionSource source)
1882 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1883 // handles selection messages...
1884 // TODO: extend config options to allow user to control if selections may be
1885 // shared between viewports.
1887 || !av.followSelection
1888 || (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1889 || (source instanceof AlignViewport && ((AlignViewport) source)
1890 .getSequenceSetId().equals(av.getSequenceSetId())))
1894 // do we want to thread this ? (contention with seqsel and colsel locks, I
1896 // rules are: colsel is copied if there is a real intersection between
1897 // sequence selection
1898 boolean repaint = false, copycolsel = true;
1899 // if (!av.isSelectionGroupChanged(false))
1901 SequenceGroup sgroup = null;
1902 if (seqsel != null && seqsel.getSize()>0)
1904 if (av.alignment == null)
1906 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1907 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1908 + " 's alignment is NULL! returning immediatly.");
1911 sgroup = seqsel.intersect(av.alignment,
1912 (av.hasHiddenRows) ? av.hiddenRepSequences : null);
1913 if ((sgroup == null || sgroup.getSize() == 0)
1914 || (colsel == null || colsel.size() == 0))
1916 // don't copy columns if the region didn't intersect.
1920 if (sgroup != null && sgroup.getSize() > 0)
1922 av.setSelectionGroup(sgroup);
1926 av.setSelectionGroup(null);
1928 av.isSelectionGroupChanged(true);
1933 // the current selection is unset or from a previous message
1934 // so import the new colsel.
1935 if (colsel == null || colsel.size() == 0)
1937 if (av.colSel != null)
1945 // TODO: shift colSel according to the intersecting sequences
1946 if (av.colSel == null)
1948 av.colSel = new ColumnSelection(colsel);
1952 av.colSel.setElementsFrom(colsel);
1955 av.isColSelChanged(true);
1958 if (copycolsel && av.hasHiddenColumns
1959 && (av.colSel == null || av.colSel.getHiddenColumns() == null))
1961 System.err.println("Bad things");
1965 // probably finessing with multiple redraws here
1966 PaintRefresher.Refresh(this, av.getSequenceSetId());
1967 // ap.paintAlignment(false);