2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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/>.
22 import java.util.List;
25 import java.awt.event.*;
29 import jalview.commands.*;
30 import jalview.datamodel.*;
31 import jalview.io.SequenceAnnotationReport;
32 import jalview.schemes.*;
33 import jalview.structure.*;
39 * @version $Revision: 1.130 $
41 public class SeqPanel extends JPanel implements MouseListener,
42 MouseMotionListener, MouseWheelListener, SequenceListener,
47 public SeqCanvas seqCanvas;
50 public AlignmentPanel ap;
52 protected int lastres;
54 protected int startseq;
56 protected AlignViewport av;
58 ScrollThread scrollThread = null;
60 boolean mouseDragging = false;
62 boolean editingSeqs = false;
64 boolean groupEditing = false;
66 // ////////////////////////////////////////
67 // ///Everything below this is for defining the boundary of the rubberband
68 // ////////////////////////////////////////
71 boolean changeEndSeq = false;
73 boolean changeStartSeq = false;
75 boolean changeEndRes = false;
77 boolean changeStartRes = false;
79 SequenceGroup stretchGroup = null;
81 boolean remove = false;
85 boolean mouseWheelPressed = false;
87 StringBuffer keyboardNo1;
89 StringBuffer keyboardNo2;
91 java.net.URL linkImageURL;
93 private final SequenceAnnotationReport seqARep;
95 StringBuffer tooltipText = new StringBuffer("<html>");
99 EditCommand editCommand;
101 StructureSelectionManager ssm;
104 * Creates a new SeqPanel object.
111 public SeqPanel(AlignViewport av, AlignmentPanel ap)
113 linkImageURL = getClass().getResource("/images/link.gif");
114 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
115 ToolTipManager.sharedInstance().registerComponent(this);
116 ToolTipManager.sharedInstance().setInitialDelay(0);
117 ToolTipManager.sharedInstance().setDismissDelay(10000);
119 setBackground(Color.white);
121 seqCanvas = new SeqCanvas(ap);
122 setLayout(new BorderLayout());
123 add(seqCanvas, BorderLayout.CENTER);
129 addMouseMotionListener(this);
130 addMouseListener(this);
131 addMouseWheelListener(this);
132 ssm = av.getStructureSelectionManager();
133 ssm.addStructureViewerListener(this);
134 ssm.addSelectionListener(this);
138 int startWrapBlock = -1;
140 int wrappedBlock = -1;
142 int findRes(MouseEvent evt)
147 if (av.wrapAlignment)
150 int hgap = av.charHeight;
151 if (av.scaleAboveWrapped)
153 hgap += av.charHeight;
156 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
157 + seqCanvas.getAnnotationHeight();
161 x -= seqCanvas.LABEL_WEST;
163 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
169 wrappedBlock = y / cHeight;
170 wrappedBlock += av.getStartRes() / cwidth;
172 res = wrappedBlock * cwidth + x / av.getCharWidth();
177 if (x > seqCanvas.getWidth() + seqCanvas.getWidth())
179 // make sure we calculate relative to visible alignment, rather than
181 x = seqCanvas.getX() + seqCanvas.getWidth();
183 res = (x / av.getCharWidth()) + av.getStartRes();
186 if (av.hasHiddenColumns())
188 res = av.getColumnSelection().adjustForHiddenColumns(res);
195 int findSeq(MouseEvent evt)
200 if (av.wrapAlignment)
202 int hgap = av.charHeight;
203 if (av.scaleAboveWrapped)
205 hgap += av.charHeight;
208 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
209 + seqCanvas.getAnnotationHeight();
213 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
218 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
219 .getAlignment().getHeight() - 1);
225 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
227 Vector tmp = new Vector();
228 SequenceFeature[] features = sequence.getSequenceFeatures();
229 if (features != null)
231 for (int i = 0; i < features.length; i++)
233 if (av.featuresDisplayed == null
234 || !av.featuresDisplayed.containsKey(features[i].getType()))
239 if (features[i].featureGroup != null
240 && seqCanvas.fr.featureGroups != null
241 && seqCanvas.fr.featureGroups
242 .containsKey(features[i].featureGroup)
243 && !((Boolean) seqCanvas.fr.featureGroups
244 .get(features[i].featureGroup)).booleanValue())
247 if ((features[i].getBegin() <= res)
248 && (features[i].getEnd() >= res))
250 tmp.addElement(features[i]);
255 features = new SequenceFeature[tmp.size()];
256 tmp.copyInto(features);
263 if (editCommand != null && editCommand.getSize() > 0)
265 ap.alignFrame.addHistoryItem(editCommand);
266 av.firePropertyChange("alignment", null, av.getAlignment()
273 groupEditing = false;
281 seqCanvas.cursorY = getKeyboardNo1() - 1;
285 void setCursorColumn()
287 seqCanvas.cursorX = getKeyboardNo1() - 1;
291 void setCursorRowAndColumn()
293 if (keyboardNo2 == null)
295 keyboardNo2 = new StringBuffer();
299 seqCanvas.cursorX = getKeyboardNo1() - 1;
300 seqCanvas.cursorY = getKeyboardNo2() - 1;
305 void setCursorPosition()
307 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
309 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
313 void moveCursor(int dx, int dy)
315 seqCanvas.cursorX += dx;
316 seqCanvas.cursorY += dy;
317 if (av.hasHiddenColumns()
318 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
320 int original = seqCanvas.cursorX - dx;
321 int maxWidth = av.getAlignment().getWidth();
323 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
324 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
326 seqCanvas.cursorX += dx;
329 if (seqCanvas.cursorX >= maxWidth
330 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
332 seqCanvas.cursorX = original;
339 void scrollToVisible()
341 if (seqCanvas.cursorX < 0)
343 seqCanvas.cursorX = 0;
345 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
347 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
350 if (seqCanvas.cursorY < 0)
352 seqCanvas.cursorY = 0;
354 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
356 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
360 if (av.wrapAlignment)
362 ap.scrollToWrappedVisible(seqCanvas.cursorX);
366 while (seqCanvas.cursorY < av.startSeq)
370 while (seqCanvas.cursorY + 1 > av.endSeq)
374 if (!av.wrapAlignment)
376 while (seqCanvas.cursorX < av.getColumnSelection()
377 .adjustForHiddenColumns(av.startRes))
379 if (!ap.scrollRight(false))
384 while (seqCanvas.cursorX > av.getColumnSelection()
385 .adjustForHiddenColumns(av.endRes))
387 if (!ap.scrollRight(true))
394 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
395 seqCanvas.cursorX, seqCanvas.cursorY);
400 void setSelectionAreaAtCursor(boolean topLeft)
402 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
404 if (av.getSelectionGroup() != null)
406 SequenceGroup sg = av.getSelectionGroup();
407 // Find the top and bottom of this group
408 int min = av.getAlignment().getHeight(), max = 0;
409 for (int i = 0; i < sg.getSize(); i++)
411 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
426 sg.setStartRes(seqCanvas.cursorX);
427 if (sg.getEndRes() < seqCanvas.cursorX)
429 sg.setEndRes(seqCanvas.cursorX);
432 min = seqCanvas.cursorY;
436 sg.setEndRes(seqCanvas.cursorX);
437 if (sg.getStartRes() > seqCanvas.cursorX)
439 sg.setStartRes(seqCanvas.cursorX);
442 max = seqCanvas.cursorY + 1;
447 // Only the user can do this
448 av.setSelectionGroup(null);
452 // Now add any sequences between min and max
453 sg.getSequences(null).clear();
454 for (int i = min; i < max; i++)
456 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
461 if (av.getSelectionGroup() == null)
463 SequenceGroup sg = new SequenceGroup();
464 sg.setStartRes(seqCanvas.cursorX);
465 sg.setEndRes(seqCanvas.cursorX);
466 sg.addSequence(sequence, false);
467 av.setSelectionGroup(sg);
470 ap.paintAlignment(false);
474 void insertGapAtCursor(boolean group)
476 groupEditing = group;
477 startseq = seqCanvas.cursorY;
478 lastres = seqCanvas.cursorX;
479 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
483 void deleteGapAtCursor(boolean group)
485 groupEditing = group;
486 startseq = seqCanvas.cursorY;
487 lastres = seqCanvas.cursorX + getKeyboardNo1();
488 editSequence(false, false, seqCanvas.cursorX);
492 void insertNucAtCursor(boolean group, String nuc)
494 groupEditing = group;
495 startseq = seqCanvas.cursorY;
496 lastres = seqCanvas.cursorX;
497 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
501 void numberPressed(char value)
503 if (keyboardNo1 == null)
505 keyboardNo1 = new StringBuffer();
508 if (keyboardNo2 != null)
510 keyboardNo2.append(value);
514 keyboardNo1.append(value);
521 if (keyboardNo1 != null)
523 int value = Integer.parseInt(keyboardNo1.toString());
527 } catch (Exception x)
536 if (keyboardNo2!=null){
537 int value = Integer.parseInt(keyboardNo2.toString());
541 } catch (Exception x)
554 public void mouseReleased(MouseEvent evt)
556 mouseDragging = false;
557 mouseWheelPressed = false;
561 doMouseReleasedDefineMode(evt);
575 public void mousePressed(MouseEvent evt)
577 lastMousePress = evt.getPoint();
579 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
581 mouseWheelPressed = true;
585 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
587 if (evt.isAltDown() || evt.isControlDown())
595 doMousePressedDefineMode(evt);
599 int seq = findSeq(evt);
600 int res = findRes(evt);
602 if (seq < 0 || res < 0)
607 if ((seq < av.getAlignment().getHeight())
608 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
625 public void mouseOverSequence(SequenceI sequence, int index, int pos)
627 String tmp = sequence.hashCode() + " " + index + " " + pos;
629 if (lastMessage == null || !lastMessage.equals(tmp))
631 // System.err.println("mouseOver Sequence: "+tmp);
632 ssm.mouseOverSequence(sequence, index, pos, av);
638 public void highlightSequence(SearchResults results)
640 if (av.followHighlight)
642 if (ap.scrollToPosition(results, false))
644 seqCanvas.revalidate();
647 seqCanvas.highlightSearchResults(results);
651 public void updateColours(SequenceI seq, int index)
653 System.out.println("update the seqPanel colours");
664 public void mouseMoved(MouseEvent evt)
668 // This is because MacOSX creates a mouseMoved
669 // If control is down, other platforms will not.
673 int res = findRes(evt);
674 int seq = findSeq(evt);
676 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
681 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
683 if (res >= sequence.getLength())
688 pos = setStatusMessage(sequence, res, seq);
689 if (ssm != null && pos > -1)
690 mouseOverSequence(sequence, res, pos);
692 tooltipText.setLength(6); // Cuts the buffer back to <html>
694 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
697 for (int g = 0; g < groups.length; g++)
699 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
701 if (tooltipText.length() > 6)
703 tooltipText.append("<br>");
706 if (!groups[g].getName().startsWith("JTreeGroup")
707 && !groups[g].getName().startsWith("JGroup"))
709 tooltipText.append(groups[g].getName());
712 if (groups[g].getDescription() != null)
714 tooltipText.append(": " + groups[g].getDescription());
720 // use aa to see if the mouse pointer is on a
721 if (av.showSequenceFeatures)
724 SequenceFeature[] features = findFeaturesAtRes(
725 sequence.getDatasetSequence(),
726 rpos = sequence.findPosition(res));
727 seqARep.appendFeatures(tooltipText, rpos, features,
728 this.ap.seqPanel.seqCanvas.fr.minmax);
730 if (tooltipText.length() == 6) // <html></html>
732 setToolTipText(null);
737 tooltipText.append("</html>");
738 if (lastTooltip == null
739 || !lastTooltip.equals(tooltipText.toString()))
741 setToolTipText(tooltipText.toString());
742 lastTooltip = tooltipText.toString();
749 private Point lastp = null;
754 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
756 public Point getToolTipLocation(MouseEvent event)
758 int x = event.getX(), w = getWidth();
759 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
762 if (!event.isShiftDown() || p == null)
764 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
765 event.getX() + wdth, event.getY() - 20) : null;
768 * TODO: try to modify position region is not obcured by tooltip
776 * Set status message in alignment panel
779 * aligned sequence object
783 * index of sequence in alignment
784 * @return position of res in sequence
786 int setStatusMessage(SequenceI sequence, int res, int seq)
789 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
790 + sequence.getName());
793 if (av.getAlignment().isNucleotide())
795 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
799 text.append(" Nucleotide: ");
804 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
807 text.append(" Residue: ");
813 pos = sequence.findPosition(res);
816 text.append(obj + " (" + pos + ")");
819 ap.alignFrame.statusBar.setText(text.toString());
830 public void mouseDragged(MouseEvent evt)
832 if (mouseWheelPressed)
834 int oldWidth = av.charWidth;
836 // Which is bigger, left-right or up-down?
837 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
838 .getX() - lastMousePress.getX()))
840 int fontSize = av.font.getSize();
842 if (evt.getY() < lastMousePress.getY())
846 else if (evt.getY() > lastMousePress.getY())
856 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
857 av.charWidth = oldWidth;
862 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
866 else if (evt.getX() > lastMousePress.getX())
871 ap.paintAlignment(false);
874 FontMetrics fm = getFontMetrics(av.getFont());
875 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
877 lastMousePress = evt.getPoint();
884 doMouseDraggedDefineMode(evt);
888 int res = findRes(evt);
895 if ((lastres == -1) || (lastres == res))
900 if ((res < av.getAlignment().getWidth()) && (res < lastres))
902 // dragLeft, delete gap
903 editSequence(false, false, res);
907 editSequence(true, false, res);
910 mouseDragging = true;
911 if (scrollThread != null)
913 scrollThread.setEvent(evt);
917 // TODO: Make it more clever than many booleans
918 synchronized void editSequence(boolean insertGap, boolean editSeq,
923 boolean fixedColumns = false;
924 SequenceGroup sg = av.getSelectionGroup();
926 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
928 // No group, but the sequence may represent a group
929 if (!groupEditing && av.hasHiddenRows())
931 if (av.isHiddenRepSequence(seq))
933 sg = av.getRepresentedSequences(seq);
938 StringBuffer message = new StringBuffer();
941 message.append("Edit group:");
942 if (editCommand == null)
944 editCommand = new EditCommand("Edit Group");
949 message.append("Edit sequence: " + seq.getName());
950 String label = seq.getName();
951 if (label.length() > 10)
953 label = label.substring(0, 10);
955 if (editCommand == null)
957 editCommand = new EditCommand("Edit " + label);
963 message.append(" insert ");
967 message.append(" delete ");
970 message.append(Math.abs(startres - lastres) + " gaps.");
971 ap.alignFrame.statusBar.setText(message.toString());
973 // Are we editing within a selection group?
975 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
980 // sg might be null as the user may only see 1 sequence,
981 // but the sequence represents a group
984 if (!av.isHiddenRepSequence(seq))
989 sg = av.getRepresentedSequences(seq);
992 fixedLeft = sg.getStartRes();
993 fixedRight = sg.getEndRes();
995 if ((startres < fixedLeft && lastres >= fixedLeft)
996 || (startres >= fixedLeft && lastres < fixedLeft)
997 || (startres > fixedRight && lastres <= fixedRight)
998 || (startres <= fixedRight && lastres > fixedRight))
1004 if (fixedLeft > startres)
1006 fixedRight = fixedLeft - 1;
1009 else if (fixedRight < startres)
1011 fixedLeft = fixedRight;
1016 if (av.hasHiddenColumns())
1018 fixedColumns = true;
1019 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1020 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1022 if ((insertGap && startres > y1 && lastres < y1)
1023 || (!insertGap && startres < y2 && lastres > y2))
1029 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1030 // Selection spans a hidden region
1031 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1039 fixedRight = y2 - 1;
1046 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1047 int g, groupSize = vseqs.size();
1048 SequenceI[] groupSeqs = new SequenceI[groupSize];
1049 for (g = 0; g < groupSeqs.length; g++)
1051 groupSeqs[g] = vseqs.get(g);
1057 // If the user has selected the whole sequence, and is dragging to
1058 // the right, we can still extend the alignment and selectionGroup
1059 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1060 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1062 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1063 fixedRight = sg.getEndRes();
1066 // Is it valid with fixed columns??
1067 // Find the next gap before the end
1068 // of the visible region boundary
1069 boolean blank = false;
1070 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1074 for (g = 0; g < groupSize; g++)
1076 for (int j = 0; j < startres - lastres; j++)
1078 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1079 .getCharAt(fixedRight - j)))
1094 if (sg.getSize() == av.getAlignment().getHeight())
1096 if ((av.hasHiddenColumns() && startres < av
1097 .getColumnSelection().getHiddenBoundaryRight(startres)))
1103 int alWidth = av.getAlignment().getWidth();
1104 if (av.hasHiddenRows())
1106 int hwidth = av.getAlignment().getHiddenSequences()
1108 if (hwidth > alWidth)
1113 // We can still insert gaps if the selectionGroup
1114 // contains all the sequences
1115 sg.setEndRes(sg.getEndRes() + startres - lastres);
1116 fixedRight = alWidth + startres - lastres;
1127 else if (!insertGap)
1129 // / Are we able to delete?
1130 // ie are all columns blank?
1132 for (g = 0; g < groupSize; g++)
1134 for (int j = startres; j < lastres; j++)
1136 if (groupSeqs[g].getLength() <= j)
1141 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1143 // Not a gap, block edit not valid
1153 // dragging to the right
1154 if (fixedColumns && fixedRight != -1)
1156 for (int j = lastres; j < startres; j++)
1158 insertChar(j, groupSeqs, fixedRight);
1163 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1164 startres, startres - lastres, av.getAlignment(), true);
1169 // dragging to the left
1170 if (fixedColumns && fixedRight != -1)
1172 for (int j = lastres; j > startres; j--)
1174 deleteChar(startres, groupSeqs, fixedRight);
1179 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1180 startres, lastres - startres, av.getAlignment(), true);
1186 // ///Editing a single sequence///////////
1190 // dragging to the right
1191 if (fixedColumns && fixedRight != -1)
1193 for (int j = lastres; j < startres; j++)
1195 insertChar(j, new SequenceI[]
1196 { seq }, fixedRight);
1201 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1202 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1209 // dragging to the left
1210 if (fixedColumns && fixedRight != -1)
1212 for (int j = lastres; j > startres; j--)
1214 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1219 deleteChar(startres, new SequenceI[]
1220 { seq }, fixedRight);
1225 // could be a keyboard edit trying to delete none gaps
1227 for (int m = startres; m < lastres; m++)
1229 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1238 editCommand.appendEdit(EditCommand.DELETE_GAP,
1240 { seq }, startres, max, av.getAlignment(), true);
1245 {// insertGap==false AND editSeq==TRUE;
1246 if (fixedColumns && fixedRight != -1)
1248 for (int j = lastres; j < startres; j++)
1250 insertChar(j, new SequenceI[]
1251 { seq }, fixedRight);
1256 editCommand.appendEdit(EditCommand.INSERT_NUC, new SequenceI[]
1257 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1264 seqCanvas.repaint();
1267 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1269 int blankColumn = fixedColumn;
1270 for (int s = 0; s < seq.length; s++)
1272 // Find the next gap before the end of the visible region boundary
1273 // If lastCol > j, theres a boundary after the gap insertion
1275 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1277 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1279 // Theres a space, so break and insert the gap
1284 if (blankColumn <= j)
1286 blankColumn = fixedColumn;
1292 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1293 av.getAlignment(), true);
1295 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1,
1296 av.getAlignment(), true);
1300 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1303 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1,
1304 av.getAlignment(), true);
1306 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1307 av.getAlignment(), true);
1317 public void mouseEntered(MouseEvent e)
1324 if (scrollThread != null)
1326 scrollThread.running = false;
1327 scrollThread = null;
1338 public void mouseExited(MouseEvent e)
1340 if (av.getWrapAlignment())
1347 scrollThread = new ScrollThread();
1352 public void mouseClicked(MouseEvent evt)
1354 SequenceGroup sg = null;
1355 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1356 if (evt.getClickCount() > 1)
1358 sg = av.getSelectionGroup();
1359 if (sg != null && sg.getSize() == 1
1360 && sg.getEndRes() - sg.getStartRes() < 2)
1362 av.setSelectionGroup(null);
1365 SequenceFeature[] features = findFeaturesAtRes(
1366 sequence.getDatasetSequence(),
1367 sequence.findPosition(findRes(evt)));
1369 if (features != null && features.length > 0)
1371 SearchResults highlight = new SearchResults();
1372 highlight.addResult(sequence, features[0].getBegin(),
1373 features[0].getEnd());
1374 seqCanvas.highlightSearchResults(highlight);
1376 if (features != null && features.length > 0)
1378 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1379 { sequence }, features, false, ap);
1381 seqCanvas.highlightSearchResults(null);
1387 public void mouseWheelMoved(MouseWheelEvent e)
1390 if (e.getWheelRotation() > 0)
1392 if (e.isShiftDown())
1394 ap.scrollRight(true);
1404 if (e.isShiftDown())
1406 ap.scrollRight(false);
1413 // TODO Update tooltip for new position.
1422 public void doMousePressedDefineMode(MouseEvent evt)
1424 int res = findRes(evt);
1425 int seq = findSeq(evt);
1428 startWrapBlock = wrappedBlock;
1430 if (av.wrapAlignment && seq > av.getAlignment().getHeight())
1432 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1433 "Cannot edit annotations in wrapped view.",
1434 "Wrapped view - no edit", JOptionPane.WARNING_MESSAGE);
1438 if (seq < 0 || res < 0)
1443 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1445 if ((sequence == null) || (res > sequence.getLength()))
1450 stretchGroup = av.getSelectionGroup();
1452 if (stretchGroup == null)
1454 stretchGroup = av.getAlignment().findGroup(sequence);
1456 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1457 && (res < stretchGroup.getEndRes()))
1459 av.setSelectionGroup(stretchGroup);
1463 stretchGroup = null;
1466 else if (!stretchGroup.getSequences(null).contains(sequence)
1467 || (stretchGroup.getStartRes() > res)
1468 || (stretchGroup.getEndRes() < res))
1470 stretchGroup = null;
1472 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1474 if (allGroups != null)
1476 for (int i = 0; i < allGroups.length; i++)
1478 if ((allGroups[i].getStartRes() <= res)
1479 && (allGroups[i].getEndRes() >= res))
1481 stretchGroup = allGroups[i];
1487 av.setSelectionGroup(stretchGroup);
1491 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1493 SequenceFeature[] allFeatures = findFeaturesAtRes(
1494 sequence.getDatasetSequence(), sequence.findPosition(res));
1495 Vector links = new Vector();
1496 for (int i = 0; i < allFeatures.length; i++)
1498 if (allFeatures[i].links != null)
1500 for (int j = 0; j < allFeatures[i].links.size(); j++)
1502 links.addElement(allFeatures[i].links.elementAt(j));
1507 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1508 pop.show(this, evt.getX(), evt.getY());
1514 seqCanvas.cursorX = findRes(evt);
1515 seqCanvas.cursorY = findSeq(evt);
1516 seqCanvas.repaint();
1520 if (stretchGroup == null)
1522 // Only if left mouse button do we want to change group sizes
1524 // define a new group here
1525 SequenceGroup sg = new SequenceGroup();
1526 sg.setStartRes(res);
1528 sg.addSequence(sequence, false);
1529 av.setSelectionGroup(sg);
1533 if (av.getConservationSelected())
1535 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1539 if (av.getAbovePIDThreshold())
1541 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1544 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1546 // Edit end res position of selected group
1547 changeEndRes = true;
1549 else if ((stretchGroup != null)
1550 && (stretchGroup.getStartRes() == res))
1552 // Edit end res position of selected group
1553 changeStartRes = true;
1555 stretchGroup.getWidth();
1558 seqCanvas.repaint();
1567 public void doMouseReleasedDefineMode(MouseEvent evt)
1569 if (stretchGroup == null)
1574 stretchGroup.recalcConservation(); // always do this - annotation has own
1576 if (stretchGroup.cs != null)
1578 stretchGroup.cs.alignmentChanged(stretchGroup,
1579 av.getHiddenRepSequences());
1581 if (stretchGroup.cs.conservationApplied())
1583 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1584 stretchGroup.getName());
1588 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1589 stretchGroup.getName());
1592 PaintRefresher.Refresh(this, av.getSequenceSetId());
1593 ap.paintAlignment(true);
1595 changeEndRes = false;
1596 changeStartRes = false;
1597 stretchGroup = null;
1607 public void doMouseDraggedDefineMode(MouseEvent evt)
1609 int res = findRes(evt);
1610 int y = findSeq(evt);
1612 if (wrappedBlock != startWrapBlock)
1617 if (stretchGroup == null)
1622 if (res >= av.getAlignment().getWidth())
1624 res = av.getAlignment().getWidth() - 1;
1627 if (stretchGroup.getEndRes() == res)
1629 // Edit end res position of selected group
1630 changeEndRes = true;
1632 else if (stretchGroup.getStartRes() == res)
1634 // Edit start res position of selected group
1635 changeStartRes = true;
1638 if (res < av.getStartRes())
1640 res = av.getStartRes();
1645 if (res > (stretchGroup.getStartRes() - 1))
1647 stretchGroup.setEndRes(res);
1650 else if (changeStartRes)
1652 if (res < (stretchGroup.getEndRes() + 1))
1654 stretchGroup.setStartRes(res);
1658 int dragDirection = 0;
1664 else if (y < oldSeq)
1669 while ((y != oldSeq) && (oldSeq > -1)
1670 && (y < av.getAlignment().getHeight()))
1672 // This routine ensures we don't skip any sequences, as the
1673 // selection is quite slow.
1674 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1676 oldSeq += dragDirection;
1683 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1685 if (stretchGroup.getSequences(null).contains(nextSeq))
1687 stretchGroup.deleteSequence(seq, false);
1693 stretchGroup.addSequence(seq, false);
1696 stretchGroup.addSequence(nextSeq, false);
1705 mouseDragging = true;
1707 if (scrollThread != null)
1709 scrollThread.setEvent(evt);
1712 seqCanvas.repaint();
1715 void scrollCanvas(MouseEvent evt)
1719 if (scrollThread != null)
1721 scrollThread.running = false;
1722 scrollThread = null;
1724 mouseDragging = false;
1728 if (scrollThread == null)
1730 scrollThread = new ScrollThread();
1733 mouseDragging = true;
1734 scrollThread.setEvent(evt);
1739 // this class allows scrolling off the bottom of the visible alignment
1740 class ScrollThread extends Thread
1744 boolean running = false;
1746 public ScrollThread()
1751 public void setEvent(MouseEvent e)
1756 public void stopScrolling()
1770 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1772 running = ap.scrollUp(true);
1775 if (mouseDragging && (evt.getY() >= getHeight())
1776 && (av.getAlignment().getHeight() > av.getEndSeq()))
1778 running = ap.scrollUp(false);
1781 if (mouseDragging && (evt.getX() < 0))
1783 running = ap.scrollRight(false);
1785 else if (mouseDragging && (evt.getX() >= getWidth()))
1787 running = ap.scrollRight(true);
1794 } catch (Exception ex)
1802 * modify current selection according to a received message.
1805 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1806 SelectionSource source)
1808 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1809 // handles selection messages...
1810 // TODO: extend config options to allow user to control if selections may be
1811 // shared between viewports.
1813 || !av.followSelection
1814 || (av.isSelectionGroupChanged(false) || av
1815 .isColSelChanged(false))
1816 || (source instanceof AlignViewport && ((AlignViewport) source)
1817 .getSequenceSetId().equals(av.getSequenceSetId())))
1821 // do we want to thread this ? (contention with seqsel and colsel locks, I
1823 // rules are: colsel is copied if there is a real intersection between
1824 // sequence selection
1825 boolean repaint = false, copycolsel = true;
1826 // if (!av.isSelectionGroupChanged(false))
1828 SequenceGroup sgroup = null;
1829 if (seqsel != null && seqsel.getSize() > 0)
1831 if (av.getAlignment() == null)
1833 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1834 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1835 + " 's alignment is NULL! returning immediatly.");
1838 sgroup = seqsel.intersect(av.getAlignment(),
1839 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1840 if ((sgroup == null || sgroup.getSize() == 0)
1841 || (colsel == null || colsel.size() == 0))
1843 // don't copy columns if the region didn't intersect.
1847 if (sgroup != null && sgroup.getSize() > 0)
1849 av.setSelectionGroup(sgroup);
1853 av.setSelectionGroup(null);
1855 av.isSelectionGroupChanged(true);
1860 // the current selection is unset or from a previous message
1861 // so import the new colsel.
1862 if (colsel == null || colsel.size() == 0)
1864 if (av.getColumnSelection() != null)
1866 av.getColumnSelection().clear();
1872 // TODO: shift colSel according to the intersecting sequences
1873 if (av.getColumnSelection() == null)
1875 av.setColumnSelection(new ColumnSelection(colsel));
1879 av.getColumnSelection().setElementsFrom(colsel);
1882 av.isColSelChanged(true);
1886 && av.hasHiddenColumns()
1887 && (av.getColumnSelection() == null || av.getColumnSelection()
1888 .getHiddenColumns() == null))
1890 System.err.println("Bad things");
1894 // probably finessing with multiple redraws here
1895 PaintRefresher.Refresh(this, av.getSequenceSetId());
1896 // ap.paintAlignment(false);