2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
24 import java.util.List;
27 import java.awt.event.*;
31 import jalview.commands.*;
32 import jalview.datamodel.*;
33 import jalview.io.SequenceAnnotationReport;
34 import jalview.schemes.*;
35 import jalview.structure.*;
36 import jalview.util.MessageManager;
42 * @version $Revision: 1.130 $
44 public class SeqPanel extends JPanel implements MouseListener,
45 MouseMotionListener, MouseWheelListener, SequenceListener,
50 public SeqCanvas seqCanvas;
53 public AlignmentPanel ap;
55 protected int lastres;
57 protected int startseq;
59 protected AlignViewport av;
61 ScrollThread scrollThread = null;
63 boolean mouseDragging = false;
65 boolean editingSeqs = false;
67 boolean groupEditing = false;
69 // ////////////////////////////////////////
70 // ///Everything below this is for defining the boundary of the rubberband
71 // ////////////////////////////////////////
74 boolean changeEndSeq = false;
76 boolean changeStartSeq = false;
78 boolean changeEndRes = false;
80 boolean changeStartRes = false;
82 SequenceGroup stretchGroup = null;
84 boolean remove = false;
88 boolean mouseWheelPressed = false;
90 StringBuffer keyboardNo1;
92 StringBuffer keyboardNo2;
94 java.net.URL linkImageURL;
96 private final SequenceAnnotationReport seqARep;
98 StringBuffer tooltipText = new StringBuffer("<html>");
102 EditCommand editCommand;
104 StructureSelectionManager ssm;
107 * Creates a new SeqPanel object.
114 public SeqPanel(AlignViewport av, AlignmentPanel ap)
116 linkImageURL = getClass().getResource("/images/link.gif");
117 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
118 ToolTipManager.sharedInstance().registerComponent(this);
119 ToolTipManager.sharedInstance().setInitialDelay(0);
120 ToolTipManager.sharedInstance().setDismissDelay(10000);
122 setBackground(Color.white);
124 seqCanvas = new SeqCanvas(ap);
125 setLayout(new BorderLayout());
126 add(seqCanvas, BorderLayout.CENTER);
132 addMouseMotionListener(this);
133 addMouseListener(this);
134 addMouseWheelListener(this);
135 ssm = av.getStructureSelectionManager();
136 ssm.addStructureViewerListener(this);
137 ssm.addSelectionListener(this);
141 int startWrapBlock = -1;
143 int wrappedBlock = -1;
145 int findRes(MouseEvent evt)
150 if (av.wrapAlignment)
153 int hgap = av.charHeight;
154 if (av.scaleAboveWrapped)
156 hgap += av.charHeight;
159 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
160 + seqCanvas.getAnnotationHeight();
164 x -= seqCanvas.LABEL_WEST;
166 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
172 wrappedBlock = y / cHeight;
173 wrappedBlock += av.getStartRes() / cwidth;
175 res = wrappedBlock * cwidth + x / av.getCharWidth();
180 if (x > seqCanvas.getWidth() + seqCanvas.getWidth())
182 // make sure we calculate relative to visible alignment, rather than
184 x = seqCanvas.getX() + seqCanvas.getWidth();
186 res = (x / av.getCharWidth()) + av.getStartRes();
189 if (av.hasHiddenColumns())
191 res = av.getColumnSelection().adjustForHiddenColumns(res);
198 int findSeq(MouseEvent evt)
203 if (av.wrapAlignment)
205 int hgap = av.charHeight;
206 if (av.scaleAboveWrapped)
208 hgap += av.charHeight;
211 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
212 + seqCanvas.getAnnotationHeight();
216 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
221 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
222 .getAlignment().getHeight() - 1);
228 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
230 Vector tmp = new Vector();
231 SequenceFeature[] features = sequence.getSequenceFeatures();
232 if (features != null)
234 for (int i = 0; i < features.length; i++)
236 if (av.featuresDisplayed == null
237 || !av.featuresDisplayed.containsKey(features[i].getType()))
242 if (features[i].featureGroup != null
243 && seqCanvas.fr.featureGroups != null
244 && seqCanvas.fr.featureGroups
245 .containsKey(features[i].featureGroup)
246 && !((Boolean) seqCanvas.fr.featureGroups
247 .get(features[i].featureGroup)).booleanValue())
250 if ((features[i].getBegin() <= res)
251 && (features[i].getEnd() >= res))
253 tmp.addElement(features[i]);
258 features = new SequenceFeature[tmp.size()];
259 tmp.copyInto(features);
266 if (editCommand != null && editCommand.getSize() > 0)
268 ap.alignFrame.addHistoryItem(editCommand);
269 av.firePropertyChange("alignment", null, av.getAlignment()
276 groupEditing = false;
284 seqCanvas.cursorY = getKeyboardNo1() - 1;
288 void setCursorColumn()
290 seqCanvas.cursorX = getKeyboardNo1() - 1;
294 void setCursorRowAndColumn()
296 if (keyboardNo2 == null)
298 keyboardNo2 = new StringBuffer();
302 seqCanvas.cursorX = getKeyboardNo1() - 1;
303 seqCanvas.cursorY = getKeyboardNo2() - 1;
308 void setCursorPosition()
310 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
312 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
316 void moveCursor(int dx, int dy)
318 seqCanvas.cursorX += dx;
319 seqCanvas.cursorY += dy;
320 if (av.hasHiddenColumns()
321 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
323 int original = seqCanvas.cursorX - dx;
324 int maxWidth = av.getAlignment().getWidth();
326 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
327 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
329 seqCanvas.cursorX += dx;
332 if (seqCanvas.cursorX >= maxWidth
333 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
335 seqCanvas.cursorX = original;
342 void scrollToVisible()
344 if (seqCanvas.cursorX < 0)
346 seqCanvas.cursorX = 0;
348 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
350 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
353 if (seqCanvas.cursorY < 0)
355 seqCanvas.cursorY = 0;
357 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
359 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
363 if (av.wrapAlignment)
365 ap.scrollToWrappedVisible(seqCanvas.cursorX);
369 while (seqCanvas.cursorY < av.startSeq)
373 while (seqCanvas.cursorY + 1 > av.endSeq)
377 if (!av.wrapAlignment)
379 while (seqCanvas.cursorX < av.getColumnSelection()
380 .adjustForHiddenColumns(av.startRes))
382 if (!ap.scrollRight(false))
387 while (seqCanvas.cursorX > av.getColumnSelection()
388 .adjustForHiddenColumns(av.endRes))
390 if (!ap.scrollRight(true))
397 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
398 seqCanvas.cursorX, seqCanvas.cursorY);
403 void setSelectionAreaAtCursor(boolean topLeft)
405 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
407 if (av.getSelectionGroup() != null)
409 SequenceGroup sg = av.getSelectionGroup();
410 // Find the top and bottom of this group
411 int min = av.getAlignment().getHeight(), max = 0;
412 for (int i = 0; i < sg.getSize(); i++)
414 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
429 sg.setStartRes(seqCanvas.cursorX);
430 if (sg.getEndRes() < seqCanvas.cursorX)
432 sg.setEndRes(seqCanvas.cursorX);
435 min = seqCanvas.cursorY;
439 sg.setEndRes(seqCanvas.cursorX);
440 if (sg.getStartRes() > seqCanvas.cursorX)
442 sg.setStartRes(seqCanvas.cursorX);
445 max = seqCanvas.cursorY + 1;
450 // Only the user can do this
451 av.setSelectionGroup(null);
455 // Now add any sequences between min and max
456 sg.getSequences(null).clear();
457 for (int i = min; i < max; i++)
459 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
464 if (av.getSelectionGroup() == null)
466 SequenceGroup sg = new SequenceGroup();
467 sg.setStartRes(seqCanvas.cursorX);
468 sg.setEndRes(seqCanvas.cursorX);
469 sg.addSequence(sequence, false);
470 av.setSelectionGroup(sg);
473 ap.paintAlignment(false);
477 void insertGapAtCursor(boolean group)
479 groupEditing = group;
480 startseq = seqCanvas.cursorY;
481 lastres = seqCanvas.cursorX;
482 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
486 void deleteGapAtCursor(boolean group)
488 groupEditing = group;
489 startseq = seqCanvas.cursorY;
490 lastres = seqCanvas.cursorX + getKeyboardNo1();
491 editSequence(false, false, seqCanvas.cursorX);
495 void insertNucAtCursor(boolean group, String nuc)
497 groupEditing = group;
498 startseq = seqCanvas.cursorY;
499 lastres = seqCanvas.cursorX;
500 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
504 void numberPressed(char value)
506 if (keyboardNo1 == null)
508 keyboardNo1 = new StringBuffer();
511 if (keyboardNo2 != null)
513 keyboardNo2.append(value);
517 keyboardNo1.append(value);
524 if (keyboardNo1 != null)
526 int value = Integer.parseInt(keyboardNo1.toString());
530 } catch (Exception x)
539 if (keyboardNo2!=null){
540 int value = Integer.parseInt(keyboardNo2.toString());
544 } catch (Exception x)
557 public void mouseReleased(MouseEvent evt)
559 mouseDragging = false;
560 mouseWheelPressed = false;
564 doMouseReleasedDefineMode(evt);
578 public void mousePressed(MouseEvent evt)
580 lastMousePress = evt.getPoint();
582 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
584 mouseWheelPressed = true;
588 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
590 if (evt.isAltDown() || evt.isControlDown())
598 doMousePressedDefineMode(evt);
602 int seq = findSeq(evt);
603 int res = findRes(evt);
605 if (seq < 0 || res < 0)
610 if ((seq < av.getAlignment().getHeight())
611 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
628 public void mouseOverSequence(SequenceI sequence, int index, int pos)
630 String tmp = sequence.hashCode() + " " + index + " " + pos;
632 if (lastMessage == null || !lastMessage.equals(tmp))
634 // System.err.println("mouseOver Sequence: "+tmp);
635 ssm.mouseOverSequence(sequence, index, pos, av);
641 public void highlightSequence(SearchResults results)
643 if (av.followHighlight)
645 if (ap.scrollToPosition(results, false))
647 seqCanvas.revalidate();
650 seqCanvas.highlightSearchResults(results);
654 public void updateColours(SequenceI seq, int index)
656 System.out.println("update the seqPanel colours");
667 public void mouseMoved(MouseEvent evt)
671 // This is because MacOSX creates a mouseMoved
672 // If control is down, other platforms will not.
676 int res = findRes(evt);
677 int seq = findSeq(evt);
679 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
684 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
686 if (res >= sequence.getLength())
691 pos = setStatusMessage(sequence, res, seq);
692 if (ssm != null && pos > -1)
693 mouseOverSequence(sequence, res, pos);
695 tooltipText.setLength(6); // Cuts the buffer back to <html>
697 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
700 for (int g = 0; g < groups.length; g++)
702 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
704 if (tooltipText.length() > 6)
706 tooltipText.append("<br>");
709 if (!groups[g].getName().startsWith("JTreeGroup")
710 && !groups[g].getName().startsWith("JGroup"))
712 tooltipText.append(groups[g].getName());
715 if (groups[g].getDescription() != null)
717 tooltipText.append(": " + groups[g].getDescription());
723 // use aa to see if the mouse pointer is on a
724 if (av.showSequenceFeatures)
727 SequenceFeature[] features = findFeaturesAtRes(
728 sequence.getDatasetSequence(),
729 rpos = sequence.findPosition(res));
730 seqARep.appendFeatures(tooltipText, rpos, features,
731 this.ap.seqPanel.seqCanvas.fr.minmax);
733 if (tooltipText.length() == 6) // <html></html>
735 setToolTipText(null);
740 tooltipText.append("</html>");
741 if (lastTooltip == null
742 || !lastTooltip.equals(tooltipText.toString()))
744 setToolTipText(tooltipText.toString());
745 lastTooltip = tooltipText.toString();
752 private Point lastp = null;
757 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
759 public Point getToolTipLocation(MouseEvent event)
761 int x = event.getX(), w = getWidth();
762 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
765 if (!event.isShiftDown() || p == null)
767 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
768 event.getX() + wdth, event.getY() - 20) : null;
771 * TODO: try to modify position region is not obcured by tooltip
779 * Set status message in alignment panel
782 * aligned sequence object
786 * index of sequence in alignment
787 * @return position of res in sequence
789 int setStatusMessage(SequenceI sequence, int res, int seq)
792 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
793 + sequence.getName());
796 if (av.getAlignment().isNucleotide())
798 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
802 text.append(" Nucleotide: ");
807 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
810 text.append(" Residue: ");
816 pos = sequence.findPosition(res);
819 text.append(obj + " (" + pos + ")");
822 ap.alignFrame.statusBar.setText(text.toString());
833 public void mouseDragged(MouseEvent evt)
835 if (mouseWheelPressed)
837 int oldWidth = av.charWidth;
839 // Which is bigger, left-right or up-down?
840 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
841 .getX() - lastMousePress.getX()))
843 int fontSize = av.font.getSize();
845 if (evt.getY() < lastMousePress.getY())
849 else if (evt.getY() > lastMousePress.getY())
859 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
860 av.charWidth = oldWidth;
865 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
869 else if (evt.getX() > lastMousePress.getX())
874 ap.paintAlignment(false);
877 FontMetrics fm = getFontMetrics(av.getFont());
878 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
880 lastMousePress = evt.getPoint();
887 doMouseDraggedDefineMode(evt);
891 int res = findRes(evt);
898 if ((lastres == -1) || (lastres == res))
903 if ((res < av.getAlignment().getWidth()) && (res < lastres))
905 // dragLeft, delete gap
906 editSequence(false, false, res);
910 editSequence(true, false, res);
913 mouseDragging = true;
914 if (scrollThread != null)
916 scrollThread.setEvent(evt);
920 // TODO: Make it more clever than many booleans
921 synchronized void editSequence(boolean insertGap, boolean editSeq,
926 boolean fixedColumns = false;
927 SequenceGroup sg = av.getSelectionGroup();
929 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
931 // No group, but the sequence may represent a group
932 if (!groupEditing && av.hasHiddenRows())
934 if (av.isHiddenRepSequence(seq))
936 sg = av.getRepresentedSequences(seq);
941 StringBuffer message = new StringBuffer();
944 message.append("Edit group:");
945 if (editCommand == null)
947 editCommand = new EditCommand("Edit Group");
952 message.append("Edit sequence: " + seq.getName());
953 String label = seq.getName();
954 if (label.length() > 10)
956 label = label.substring(0, 10);
958 if (editCommand == null)
960 editCommand = new EditCommand("Edit " + label);
966 message.append(" insert ");
970 message.append(" delete ");
973 message.append(Math.abs(startres - lastres) + " gaps.");
974 ap.alignFrame.statusBar.setText(message.toString());
976 // Are we editing within a selection group?
978 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
983 // sg might be null as the user may only see 1 sequence,
984 // but the sequence represents a group
987 if (!av.isHiddenRepSequence(seq))
992 sg = av.getRepresentedSequences(seq);
995 fixedLeft = sg.getStartRes();
996 fixedRight = sg.getEndRes();
998 if ((startres < fixedLeft && lastres >= fixedLeft)
999 || (startres >= fixedLeft && lastres < fixedLeft)
1000 || (startres > fixedRight && lastres <= fixedRight)
1001 || (startres <= fixedRight && lastres > fixedRight))
1007 if (fixedLeft > startres)
1009 fixedRight = fixedLeft - 1;
1012 else if (fixedRight < startres)
1014 fixedLeft = fixedRight;
1019 if (av.hasHiddenColumns())
1021 fixedColumns = true;
1022 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1023 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1025 if ((insertGap && startres > y1 && lastres < y1)
1026 || (!insertGap && startres < y2 && lastres > y2))
1032 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1033 // Selection spans a hidden region
1034 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1042 fixedRight = y2 - 1;
1049 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1050 int g, groupSize = vseqs.size();
1051 SequenceI[] groupSeqs = new SequenceI[groupSize];
1052 for (g = 0; g < groupSeqs.length; g++)
1054 groupSeqs[g] = vseqs.get(g);
1060 // If the user has selected the whole sequence, and is dragging to
1061 // the right, we can still extend the alignment and selectionGroup
1062 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1063 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1065 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1066 fixedRight = sg.getEndRes();
1069 // Is it valid with fixed columns??
1070 // Find the next gap before the end
1071 // of the visible region boundary
1072 boolean blank = false;
1073 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1077 for (g = 0; g < groupSize; g++)
1079 for (int j = 0; j < startres - lastres; j++)
1081 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1082 .getCharAt(fixedRight - j)))
1097 if (sg.getSize() == av.getAlignment().getHeight())
1099 if ((av.hasHiddenColumns() && startres < av
1100 .getColumnSelection().getHiddenBoundaryRight(startres)))
1106 int alWidth = av.getAlignment().getWidth();
1107 if (av.hasHiddenRows())
1109 int hwidth = av.getAlignment().getHiddenSequences()
1111 if (hwidth > alWidth)
1116 // We can still insert gaps if the selectionGroup
1117 // contains all the sequences
1118 sg.setEndRes(sg.getEndRes() + startres - lastres);
1119 fixedRight = alWidth + startres - lastres;
1130 else if (!insertGap)
1132 // / Are we able to delete?
1133 // ie are all columns blank?
1135 for (g = 0; g < groupSize; g++)
1137 for (int j = startres; j < lastres; j++)
1139 if (groupSeqs[g].getLength() <= j)
1144 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1146 // Not a gap, block edit not valid
1156 // dragging to the right
1157 if (fixedColumns && fixedRight != -1)
1159 for (int j = lastres; j < startres; j++)
1161 insertChar(j, groupSeqs, fixedRight);
1166 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1167 startres, startres - lastres, av.getAlignment(), true);
1172 // dragging to the left
1173 if (fixedColumns && fixedRight != -1)
1175 for (int j = lastres; j > startres; j--)
1177 deleteChar(startres, groupSeqs, fixedRight);
1182 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1183 startres, lastres - startres, av.getAlignment(), true);
1189 // ///Editing a single sequence///////////
1193 // dragging to the right
1194 if (fixedColumns && fixedRight != -1)
1196 for (int j = lastres; j < startres; j++)
1198 insertChar(j, new SequenceI[]
1199 { seq }, fixedRight);
1204 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1205 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1212 // dragging to the left
1213 if (fixedColumns && fixedRight != -1)
1215 for (int j = lastres; j > startres; j--)
1217 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1222 deleteChar(startres, new SequenceI[]
1223 { seq }, fixedRight);
1228 // could be a keyboard edit trying to delete none gaps
1230 for (int m = startres; m < lastres; m++)
1232 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1241 editCommand.appendEdit(EditCommand.DELETE_GAP,
1243 { seq }, startres, max, av.getAlignment(), true);
1248 {// insertGap==false AND editSeq==TRUE;
1249 if (fixedColumns && fixedRight != -1)
1251 for (int j = lastres; j < startres; j++)
1253 insertChar(j, new SequenceI[]
1254 { seq }, fixedRight);
1259 editCommand.appendEdit(EditCommand.INSERT_NUC, new SequenceI[]
1260 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1267 seqCanvas.repaint();
1270 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1272 int blankColumn = fixedColumn;
1273 for (int s = 0; s < seq.length; s++)
1275 // Find the next gap before the end of the visible region boundary
1276 // If lastCol > j, theres a boundary after the gap insertion
1278 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1280 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1282 // Theres a space, so break and insert the gap
1287 if (blankColumn <= j)
1289 blankColumn = fixedColumn;
1295 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1296 av.getAlignment(), true);
1298 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1,
1299 av.getAlignment(), true);
1303 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1306 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1,
1307 av.getAlignment(), true);
1309 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1310 av.getAlignment(), true);
1320 public void mouseEntered(MouseEvent e)
1327 if (scrollThread != null)
1329 scrollThread.running = false;
1330 scrollThread = null;
1341 public void mouseExited(MouseEvent e)
1343 if (av.getWrapAlignment())
1350 scrollThread = new ScrollThread();
1355 public void mouseClicked(MouseEvent evt)
1357 SequenceGroup sg = null;
1358 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1359 if (evt.getClickCount() > 1)
1361 sg = av.getSelectionGroup();
1362 if (sg != null && sg.getSize() == 1
1363 && sg.getEndRes() - sg.getStartRes() < 2)
1365 av.setSelectionGroup(null);
1368 SequenceFeature[] features = findFeaturesAtRes(
1369 sequence.getDatasetSequence(),
1370 sequence.findPosition(findRes(evt)));
1372 if (features != null && features.length > 0)
1374 SearchResults highlight = new SearchResults();
1375 highlight.addResult(sequence, features[0].getBegin(),
1376 features[0].getEnd());
1377 seqCanvas.highlightSearchResults(highlight);
1379 if (features != null && features.length > 0)
1381 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1382 { sequence }, features, false, ap);
1384 seqCanvas.highlightSearchResults(null);
1390 public void mouseWheelMoved(MouseWheelEvent e)
1393 if (e.getWheelRotation() > 0)
1395 if (e.isShiftDown())
1397 ap.scrollRight(true);
1407 if (e.isShiftDown())
1409 ap.scrollRight(false);
1416 // TODO Update tooltip for new position.
1425 public void doMousePressedDefineMode(MouseEvent evt)
1427 int res = findRes(evt);
1428 int seq = findSeq(evt);
1431 startWrapBlock = wrappedBlock;
1433 if (av.wrapAlignment && seq > av.getAlignment().getHeight())
1435 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1436 MessageManager.getString("label.cannot_edit_annotations_in_wrapped_view"),
1437 MessageManager.getString("label.wrapped_view_no_edit"), JOptionPane.WARNING_MESSAGE);
1441 if (seq < 0 || res < 0)
1446 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1448 if ((sequence == null) || (res > sequence.getLength()))
1453 stretchGroup = av.getSelectionGroup();
1455 if (stretchGroup == null)
1457 stretchGroup = av.getAlignment().findGroup(sequence);
1459 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1460 && (res < stretchGroup.getEndRes()))
1462 av.setSelectionGroup(stretchGroup);
1466 stretchGroup = null;
1469 else if (!stretchGroup.getSequences(null).contains(sequence)
1470 || (stretchGroup.getStartRes() > res)
1471 || (stretchGroup.getEndRes() < res))
1473 stretchGroup = null;
1475 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1477 if (allGroups != null)
1479 for (int i = 0; i < allGroups.length; i++)
1481 if ((allGroups[i].getStartRes() <= res)
1482 && (allGroups[i].getEndRes() >= res))
1484 stretchGroup = allGroups[i];
1490 av.setSelectionGroup(stretchGroup);
1494 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1496 SequenceFeature[] allFeatures = findFeaturesAtRes(
1497 sequence.getDatasetSequence(), sequence.findPosition(res));
1498 Vector links = new Vector();
1499 for (int i = 0; i < allFeatures.length; i++)
1501 if (allFeatures[i].links != null)
1503 for (int j = 0; j < allFeatures[i].links.size(); j++)
1505 links.addElement(allFeatures[i].links.elementAt(j));
1510 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1511 pop.show(this, evt.getX(), evt.getY());
1517 seqCanvas.cursorX = findRes(evt);
1518 seqCanvas.cursorY = findSeq(evt);
1519 seqCanvas.repaint();
1523 if (stretchGroup == null)
1525 // Only if left mouse button do we want to change group sizes
1527 // define a new group here
1528 SequenceGroup sg = new SequenceGroup();
1529 sg.setStartRes(res);
1531 sg.addSequence(sequence, false);
1532 av.setSelectionGroup(sg);
1536 if (av.getConservationSelected())
1538 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1542 if (av.getAbovePIDThreshold())
1544 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1547 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1549 // Edit end res position of selected group
1550 changeEndRes = true;
1552 else if ((stretchGroup != null)
1553 && (stretchGroup.getStartRes() == res))
1555 // Edit end res position of selected group
1556 changeStartRes = true;
1558 stretchGroup.getWidth();
1561 seqCanvas.repaint();
1570 public void doMouseReleasedDefineMode(MouseEvent evt)
1572 if (stretchGroup == null)
1577 stretchGroup.recalcConservation(); // always do this - annotation has own
1579 if (stretchGroup.cs != null)
1581 stretchGroup.cs.alignmentChanged(stretchGroup,
1582 av.getHiddenRepSequences());
1584 if (stretchGroup.cs.conservationApplied())
1586 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1587 stretchGroup.getName());
1591 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1592 stretchGroup.getName());
1595 PaintRefresher.Refresh(this, av.getSequenceSetId());
1596 ap.paintAlignment(true);
1598 changeEndRes = false;
1599 changeStartRes = false;
1600 stretchGroup = null;
1610 public void doMouseDraggedDefineMode(MouseEvent evt)
1612 int res = findRes(evt);
1613 int y = findSeq(evt);
1615 if (wrappedBlock != startWrapBlock)
1620 if (stretchGroup == null)
1625 if (res >= av.getAlignment().getWidth())
1627 res = av.getAlignment().getWidth() - 1;
1630 if (stretchGroup.getEndRes() == res)
1632 // Edit end res position of selected group
1633 changeEndRes = true;
1635 else if (stretchGroup.getStartRes() == res)
1637 // Edit start res position of selected group
1638 changeStartRes = true;
1641 if (res < av.getStartRes())
1643 res = av.getStartRes();
1648 if (res > (stretchGroup.getStartRes() - 1))
1650 stretchGroup.setEndRes(res);
1653 else if (changeStartRes)
1655 if (res < (stretchGroup.getEndRes() + 1))
1657 stretchGroup.setStartRes(res);
1661 int dragDirection = 0;
1667 else if (y < oldSeq)
1672 while ((y != oldSeq) && (oldSeq > -1)
1673 && (y < av.getAlignment().getHeight()))
1675 // This routine ensures we don't skip any sequences, as the
1676 // selection is quite slow.
1677 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1679 oldSeq += dragDirection;
1686 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1688 if (stretchGroup.getSequences(null).contains(nextSeq))
1690 stretchGroup.deleteSequence(seq, false);
1696 stretchGroup.addSequence(seq, false);
1699 stretchGroup.addSequence(nextSeq, false);
1708 mouseDragging = true;
1710 if (scrollThread != null)
1712 scrollThread.setEvent(evt);
1715 seqCanvas.repaint();
1718 void scrollCanvas(MouseEvent evt)
1722 if (scrollThread != null)
1724 scrollThread.running = false;
1725 scrollThread = null;
1727 mouseDragging = false;
1731 if (scrollThread == null)
1733 scrollThread = new ScrollThread();
1736 mouseDragging = true;
1737 scrollThread.setEvent(evt);
1742 // this class allows scrolling off the bottom of the visible alignment
1743 class ScrollThread extends Thread
1747 boolean running = false;
1749 public ScrollThread()
1754 public void setEvent(MouseEvent e)
1759 public void stopScrolling()
1773 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1775 running = ap.scrollUp(true);
1778 if (mouseDragging && (evt.getY() >= getHeight())
1779 && (av.getAlignment().getHeight() > av.getEndSeq()))
1781 running = ap.scrollUp(false);
1784 if (mouseDragging && (evt.getX() < 0))
1786 running = ap.scrollRight(false);
1788 else if (mouseDragging && (evt.getX() >= getWidth()))
1790 running = ap.scrollRight(true);
1797 } catch (Exception ex)
1805 * modify current selection according to a received message.
1808 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1809 SelectionSource source)
1811 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1812 // handles selection messages...
1813 // TODO: extend config options to allow user to control if selections may be
1814 // shared between viewports.
1816 || !av.followSelection
1817 || (av.isSelectionGroupChanged(false) || av
1818 .isColSelChanged(false))
1819 || (source instanceof AlignViewport && ((AlignViewport) source)
1820 .getSequenceSetId().equals(av.getSequenceSetId())))
1824 // do we want to thread this ? (contention with seqsel and colsel locks, I
1826 // rules are: colsel is copied if there is a real intersection between
1827 // sequence selection
1828 boolean repaint = false, copycolsel = true;
1829 // if (!av.isSelectionGroupChanged(false))
1831 SequenceGroup sgroup = null;
1832 if (seqsel != null && seqsel.getSize() > 0)
1834 if (av.getAlignment() == null)
1836 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1837 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1838 + " 's alignment is NULL! returning immediatly.");
1841 sgroup = seqsel.intersect(av.getAlignment(),
1842 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1843 if ((sgroup == null || sgroup.getSize() == 0)
1844 || (colsel == null || colsel.size() == 0))
1846 // don't copy columns if the region didn't intersect.
1850 if (sgroup != null && sgroup.getSize() > 0)
1852 av.setSelectionGroup(sgroup);
1856 av.setSelectionGroup(null);
1858 av.isSelectionGroupChanged(true);
1863 // the current selection is unset or from a previous message
1864 // so import the new colsel.
1865 if (colsel == null || colsel.size() == 0)
1867 if (av.getColumnSelection() != null)
1869 av.getColumnSelection().clear();
1875 // TODO: shift colSel according to the intersecting sequences
1876 if (av.getColumnSelection() == null)
1878 av.setColumnSelection(new ColumnSelection(colsel));
1882 av.getColumnSelection().setElementsFrom(colsel);
1885 av.isColSelChanged(true);
1889 && av.hasHiddenColumns()
1890 && (av.getColumnSelection() == null || av.getColumnSelection()
1891 .getHiddenColumns() == null))
1893 System.err.println("Bad things");
1897 // probably finessing with multiple redraws here
1898 PaintRefresher.Refresh(this, av.getSequenceSetId());
1899 // ap.paintAlignment(false);