2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
21 import java.util.List;
24 import java.awt.event.*;
28 import jalview.commands.*;
29 import jalview.datamodel.*;
30 import jalview.io.SequenceAnnotationReport;
31 import jalview.schemes.*;
32 import jalview.structure.*;
38 * @version $Revision: 1.130 $
40 public class SeqPanel extends JPanel implements MouseListener,
41 MouseMotionListener, MouseWheelListener, SequenceListener,
46 public SeqCanvas seqCanvas;
49 public AlignmentPanel ap;
51 protected int lastres;
53 protected int startseq;
55 protected AlignViewport av;
57 ScrollThread scrollThread = null;
59 boolean mouseDragging = false;
61 boolean editingSeqs = false;
63 boolean groupEditing = false;
65 // ////////////////////////////////////////
66 // ///Everything below this is for defining the boundary of the rubberband
67 // ////////////////////////////////////////
70 boolean changeEndSeq = false;
72 boolean changeStartSeq = false;
74 boolean changeEndRes = false;
76 boolean changeStartRes = false;
78 SequenceGroup stretchGroup = null;
80 boolean remove = false;
84 boolean mouseWheelPressed = false;
86 StringBuffer keyboardNo1;
88 StringBuffer keyboardNo2;
90 java.net.URL linkImageURL;
91 private final SequenceAnnotationReport seqARep;
92 StringBuffer tooltipText = new StringBuffer("<html>");
96 EditCommand editCommand;
98 StructureSelectionManager ssm;
101 * Creates a new SeqPanel object.
108 public SeqPanel(AlignViewport av, AlignmentPanel ap)
110 linkImageURL = getClass().getResource("/images/link.gif");
111 seqARep=new SequenceAnnotationReport(linkImageURL.toString());
112 ToolTipManager.sharedInstance().registerComponent(this);
113 ToolTipManager.sharedInstance().setInitialDelay(0);
114 ToolTipManager.sharedInstance().setDismissDelay(10000);
116 setBackground(Color.white);
118 seqCanvas = new SeqCanvas(ap);
119 setLayout(new BorderLayout());
120 add(seqCanvas, BorderLayout.CENTER);
126 addMouseMotionListener(this);
127 addMouseListener(this);
128 addMouseWheelListener(this);
129 ssm = StructureSelectionManager.getStructureSelectionManager(Desktop.instance);
130 ssm.addStructureViewerListener(this);
131 ssm.addSelectionListener(this);
135 int startWrapBlock = -1;
137 int wrappedBlock = -1;
139 int findRes(MouseEvent evt)
144 if (av.wrapAlignment)
147 int hgap = av.charHeight;
148 if (av.scaleAboveWrapped)
150 hgap += av.charHeight;
153 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
154 + seqCanvas.getAnnotationHeight();
158 x -= seqCanvas.LABEL_WEST;
160 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
166 wrappedBlock = y / cHeight;
167 wrappedBlock += av.getStartRes() / cwidth;
169 res = wrappedBlock * cwidth + x / av.getCharWidth();
174 if (x>seqCanvas.getWidth()+seqCanvas.getWidth())
176 // make sure we calculate relative to visible alignment, rather than right-hand gutter
177 x = seqCanvas.getX()+seqCanvas.getWidth();
179 res = (x / av.getCharWidth()) + av.getStartRes();
182 if (av.hasHiddenColumns())
184 res = av.getColumnSelection().adjustForHiddenColumns(res);
191 int findSeq(MouseEvent evt)
196 if (av.wrapAlignment)
198 int hgap = av.charHeight;
199 if (av.scaleAboveWrapped)
201 hgap += av.charHeight;
204 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
205 + seqCanvas.getAnnotationHeight();
209 seq = Math.min((y % cHeight) / av.getCharHeight(),
210 av.getAlignment().getHeight() - 1);
214 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(),
215 av.getAlignment().getHeight() - 1);
221 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
223 Vector tmp = new Vector();
224 SequenceFeature[] features = sequence.getSequenceFeatures();
225 if (features != null)
227 for (int i = 0; i < features.length; i++)
229 if (av.featuresDisplayed == null
230 || !av.featuresDisplayed.containsKey(features[i].getType()))
235 if (features[i].featureGroup != null
236 && seqCanvas.fr.featureGroups != null
237 && seqCanvas.fr.featureGroups
238 .containsKey(features[i].featureGroup)
239 && !((Boolean) seqCanvas.fr.featureGroups
240 .get(features[i].featureGroup)).booleanValue())
243 if ((features[i].getBegin() <= res)
244 && (features[i].getEnd() >= res))
246 tmp.addElement(features[i]);
251 features = new SequenceFeature[tmp.size()];
252 tmp.copyInto(features);
259 if (editCommand != null && editCommand.getSize() > 0)
261 ap.alignFrame.addHistoryItem(editCommand);
262 av.firePropertyChange("alignment", null, av.getAlignment()
269 groupEditing = false;
277 seqCanvas.cursorY = getKeyboardNo1() - 1;
281 void setCursorColumn()
283 seqCanvas.cursorX = getKeyboardNo1() - 1;
287 void setCursorRowAndColumn()
289 if (keyboardNo2 == null)
291 keyboardNo2 = new StringBuffer();
295 seqCanvas.cursorX = getKeyboardNo1() - 1;
296 seqCanvas.cursorY = getKeyboardNo2() - 1;
301 void setCursorPosition()
303 SequenceI sequence = av.getAlignment().getSequenceAt(
306 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1() - 1);
310 void moveCursor(int dx, int dy)
312 seqCanvas.cursorX += dx;
313 seqCanvas.cursorY += dy;
314 if (av.hasHiddenColumns() && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
316 int original = seqCanvas.cursorX - dx;
317 int maxWidth = av.getAlignment().getWidth();
319 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
320 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
322 seqCanvas.cursorX += dx;
325 if (seqCanvas.cursorX >= maxWidth
326 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
328 seqCanvas.cursorX = original;
335 void scrollToVisible()
337 if (seqCanvas.cursorX < 0)
339 seqCanvas.cursorX = 0;
341 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
343 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
346 if (seqCanvas.cursorY < 0)
348 seqCanvas.cursorY = 0;
350 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
352 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
356 if (av.wrapAlignment)
358 ap.scrollToWrappedVisible(seqCanvas.cursorX);
362 while (seqCanvas.cursorY < av.startSeq)
366 while (seqCanvas.cursorY + 1 > av.endSeq)
370 if (!av.wrapAlignment)
372 while (seqCanvas.cursorX < av.getColumnSelection()
373 .adjustForHiddenColumns(av.startRes))
375 if (!ap.scrollRight(false))
380 while (seqCanvas.cursorX > av.getColumnSelection()
381 .adjustForHiddenColumns(av.endRes))
383 if (!ap.scrollRight(true))
390 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
391 seqCanvas.cursorX, seqCanvas.cursorY);
396 void setSelectionAreaAtCursor(boolean topLeft)
398 SequenceI sequence = av.getAlignment().getSequenceAt(
401 if (av.getSelectionGroup() != null)
403 SequenceGroup sg = av.getSelectionGroup();
404 // Find the top and bottom of this group
405 int min = av.getAlignment().getHeight(), max = 0;
406 for (int i = 0; i < sg.getSize(); i++)
408 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
423 sg.setStartRes(seqCanvas.cursorX);
424 if (sg.getEndRes() < seqCanvas.cursorX)
426 sg.setEndRes(seqCanvas.cursorX);
429 min = seqCanvas.cursorY;
433 sg.setEndRes(seqCanvas.cursorX);
434 if (sg.getStartRes() > seqCanvas.cursorX)
436 sg.setStartRes(seqCanvas.cursorX);
439 max = seqCanvas.cursorY + 1;
444 // Only the user can do this
445 av.setSelectionGroup(null);
449 // Now add any sequences between min and max
450 sg.getSequences(null).clear();
451 for (int i = min; i < max; i++)
453 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
458 if (av.getSelectionGroup() == null)
460 SequenceGroup sg = new SequenceGroup();
461 sg.setStartRes(seqCanvas.cursorX);
462 sg.setEndRes(seqCanvas.cursorX);
463 sg.addSequence(sequence, false);
464 av.setSelectionGroup(sg);
467 ap.paintAlignment(false);
471 void insertGapAtCursor(boolean group)
473 groupEditing = group;
474 startseq = seqCanvas.cursorY;
475 lastres = seqCanvas.cursorX;
476 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
480 void deleteGapAtCursor(boolean group)
482 groupEditing = group;
483 startseq = seqCanvas.cursorY;
484 lastres = seqCanvas.cursorX + getKeyboardNo1();
485 editSequence(false, false, seqCanvas.cursorX);
489 void insertNucAtCursor(boolean group,String nuc){
490 groupEditing = group;
491 startseq = seqCanvas.cursorY;
492 lastres = seqCanvas.cursorX;
493 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
497 void numberPressed(char value)
499 if (keyboardNo1 == null)
501 keyboardNo1 = new StringBuffer();
504 if (keyboardNo2 != null)
506 keyboardNo2.append(value);
510 keyboardNo1.append(value);
516 if (keyboardNo1 == null)
520 int value = Integer.parseInt(keyboardNo1.toString());
528 if (keyboardNo2 == null)
532 int value = Integer.parseInt(keyboardNo2.toString());
545 public void mouseReleased(MouseEvent evt)
547 mouseDragging = false;
548 mouseWheelPressed = false;
552 doMouseReleasedDefineMode(evt);
566 public void mousePressed(MouseEvent evt)
568 lastMousePress = evt.getPoint();
570 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
572 mouseWheelPressed = true;
576 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
578 if (evt.isAltDown() || evt.isControlDown())
586 doMousePressedDefineMode(evt);
590 int seq = findSeq(evt);
591 int res = findRes(evt);
593 if (seq < 0 || res < 0)
598 if ((seq < av.getAlignment().getHeight())
599 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
616 public void mouseOverSequence(SequenceI sequence, int index, int pos)
618 String tmp = sequence.hashCode() + " " + index + " " + pos;
620 if (lastMessage == null || !lastMessage.equals(tmp))
622 // System.err.println("mouseOver Sequence: "+tmp);
623 ssm.mouseOverSequence(sequence, index, pos, av);
629 public void highlightSequence(SearchResults results)
631 if (av.followHighlight)
633 if (ap.scrollToPosition(results, false))
635 seqCanvas.revalidate();
638 seqCanvas.highlightSearchResults(results);
642 public void updateColours(SequenceI seq, int index)
644 System.out.println("update the seqPanel colours");
655 public void mouseMoved(MouseEvent evt)
659 // This is because MacOSX creates a mouseMoved
660 // If control is down, other platforms will not.
664 int res = findRes(evt);
665 int seq = findSeq(evt);
667 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
672 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
674 if (res >= sequence.getLength())
679 pos = setStatusMessage(sequence, res, seq);
680 if (ssm != null && pos > -1)
681 mouseOverSequence(sequence, res, pos);
683 tooltipText.setLength(6); // Cuts the buffer back to <html>
685 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
688 for (int g = 0; g < groups.length; g++)
690 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
692 if (tooltipText.length() > 6)
694 tooltipText.append("<br>");
697 if (!groups[g].getName().startsWith("JTreeGroup")
698 && !groups[g].getName().startsWith("JGroup"))
700 tooltipText.append(groups[g].getName());
703 if (groups[g].getDescription() != null)
705 tooltipText.append(": " + groups[g].getDescription());
711 // use aa to see if the mouse pointer is on a
712 if (av.showSequenceFeatures)
715 SequenceFeature[] features = findFeaturesAtRes(
716 sequence.getDatasetSequence(),
717 rpos = sequence.findPosition(res));
718 seqARep.appendFeatures(tooltipText, rpos, features,
719 this.ap.seqPanel.seqCanvas.fr.minmax);
721 if (tooltipText.length() == 6) // <html></html>
723 setToolTipText(null);
728 tooltipText.append("</html>");
729 if (lastTooltip == null
730 || !lastTooltip.equals(tooltipText.toString()))
732 setToolTipText(tooltipText.toString());
733 lastTooltip = tooltipText.toString();
740 private Point lastp = null;
745 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
747 public Point getToolTipLocation(MouseEvent event)
749 int x = event.getX(), w = getWidth();
750 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
753 if (!event.isShiftDown() || p == null)
755 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
756 event.getX() + wdth, event.getY() - 20) : null;
759 * TODO: try to modify position region is not obcured by tooltip
768 * Set status message in alignment panel
771 * aligned sequence object
775 * index of sequence in alignment
776 * @return position of res in sequence
778 int setStatusMessage(SequenceI sequence, int res, int seq)
781 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
782 + sequence.getName());
785 if (av.getAlignment().isNucleotide())
787 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
791 text.append(" Nucleotide: ");
796 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
799 text.append(" Residue: ");
805 pos = sequence.findPosition(res);
808 text.append(obj + " (" + pos + ")");
811 ap.alignFrame.statusBar.setText(text.toString());
822 public void mouseDragged(MouseEvent evt)
824 if (mouseWheelPressed)
826 int oldWidth = av.charWidth;
828 // Which is bigger, left-right or up-down?
829 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
830 .getX() - lastMousePress.getX()))
832 int fontSize = av.font.getSize();
834 if (evt.getY() < lastMousePress.getY())
838 else if (evt.getY() > lastMousePress.getY())
848 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
849 av.charWidth = oldWidth;
854 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
858 else if (evt.getX() > lastMousePress.getX())
863 ap.paintAlignment(false);
866 FontMetrics fm = getFontMetrics(av.getFont());
867 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
869 lastMousePress = evt.getPoint();
876 doMouseDraggedDefineMode(evt);
880 int res = findRes(evt);
887 if ((lastres == -1) || (lastres == res))
892 if ((res < av.getAlignment().getWidth()) && (res < lastres))
894 // dragLeft, delete gap
895 editSequence(false, false,res);
899 editSequence(true, false,res);
902 mouseDragging = true;
903 if (scrollThread != null)
905 scrollThread.setEvent(evt);
909 //TODO: Make it more clever than many booleans
910 synchronized void editSequence(boolean insertGap, boolean editSeq, int startres)
914 boolean fixedColumns = false;
915 SequenceGroup sg = av.getSelectionGroup();
917 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
919 // No group, but the sequence may represent a group
920 if (!groupEditing && av.hasHiddenRows())
922 if (av.isHiddenRepSequence(seq))
924 sg = av.getRepresentedSequences(seq);
929 StringBuffer message = new StringBuffer();
932 message.append("Edit group:");
933 if (editCommand == null)
935 editCommand = new EditCommand("Edit Group");
940 message.append("Edit sequence: " + seq.getName());
941 String label = seq.getName();
942 if (label.length() > 10)
944 label = label.substring(0, 10);
946 if (editCommand == null)
948 editCommand = new EditCommand("Edit " + label);
954 message.append(" insert ");
958 message.append(" delete ");
961 message.append(Math.abs(startres - lastres) + " gaps.");
962 ap.alignFrame.statusBar.setText(message.toString());
964 // Are we editing within a selection group?
966 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
971 // sg might be null as the user may only see 1 sequence,
972 // but the sequence represents a group
975 if (!av.isHiddenRepSequence(seq))
980 sg = av.getRepresentedSequences(seq);
983 fixedLeft = sg.getStartRes();
984 fixedRight = sg.getEndRes();
986 if ((startres < fixedLeft && lastres >= fixedLeft)
987 || (startres >= fixedLeft && lastres < fixedLeft)
988 || (startres > fixedRight && lastres <= fixedRight)
989 || (startres <= fixedRight && lastres > fixedRight))
995 if (fixedLeft > startres)
997 fixedRight = fixedLeft - 1;
1000 else if (fixedRight < startres)
1002 fixedLeft = fixedRight;
1007 if (av.hasHiddenColumns())
1009 fixedColumns = true;
1010 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1011 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1013 if ((insertGap && startres > y1 && lastres < y1)
1014 || (!insertGap && startres < y2 && lastres > y2))
1020 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1021 // Selection spans a hidden region
1022 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1030 fixedRight = y2 - 1;
1037 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1038 int g, groupSize = vseqs.size();
1039 SequenceI[] groupSeqs = new SequenceI[groupSize];
1040 for (g = 0; g < groupSeqs.length; g++)
1042 groupSeqs[g] = vseqs.get(g);
1048 // If the user has selected the whole sequence, and is dragging to
1049 // the right, we can still extend the alignment and selectionGroup
1050 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1051 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1053 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1054 fixedRight = sg.getEndRes();
1057 // Is it valid with fixed columns??
1058 // Find the next gap before the end
1059 // of the visible region boundary
1060 boolean blank = false;
1061 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1065 for (g = 0; g < groupSize; g++)
1067 for (int j = 0; j < startres - lastres; j++)
1069 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1070 .getCharAt(fixedRight - j)))
1085 if (sg.getSize() == av.getAlignment().getHeight())
1087 if ((av.hasHiddenColumns() && startres < av.getColumnSelection()
1088 .getHiddenBoundaryRight(startres)))
1094 int alWidth = av.getAlignment().getWidth();
1095 if (av.hasHiddenRows())
1097 int hwidth = av.getAlignment().getHiddenSequences().getWidth();
1098 if (hwidth > alWidth)
1103 // We can still insert gaps if the selectionGroup
1104 // contains all the sequences
1105 sg.setEndRes(sg.getEndRes() + startres - lastres);
1106 fixedRight = alWidth + startres - lastres;
1117 else if (!insertGap)
1119 // / Are we able to delete?
1120 // ie are all columns blank?
1122 for (g = 0; g < groupSize; g++)
1124 for (int j = startres; j < lastres; j++)
1126 if (groupSeqs[g].getLength() <= j)
1131 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1133 // Not a gap, block edit not valid
1143 // dragging to the right
1144 if (fixedColumns && fixedRight != -1)
1146 for (int j = lastres; j < startres; j++)
1148 insertChar(j, groupSeqs, fixedRight);
1153 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1154 startres, startres - lastres, av.getAlignment(), true);
1159 // dragging to the left
1160 if (fixedColumns && fixedRight != -1)
1162 for (int j = lastres; j > startres; j--)
1164 deleteChar(startres, groupSeqs, fixedRight);
1169 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1170 startres, lastres - startres, av.getAlignment(), true);
1176 // ///Editing a single sequence///////////
1180 // dragging to the right
1181 if (fixedColumns && fixedRight != -1)
1183 for (int j = lastres; j < startres; j++)
1185 insertChar(j, new SequenceI[]
1186 { seq }, fixedRight);
1191 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1192 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1198 // dragging to the left
1199 if (fixedColumns && fixedRight != -1)
1201 for (int j = lastres; j > startres; j--)
1203 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1208 deleteChar(startres, new SequenceI[]
1209 { seq }, fixedRight);
1214 // could be a keyboard edit trying to delete none gaps
1216 for (int m = startres; m < lastres; m++)
1218 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1227 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1228 { seq }, startres, max, av.getAlignment(), true);
1231 }else{//insertGap==false AND editSeq==TRUE;
1232 if (fixedColumns && fixedRight != -1)
1234 for (int j = lastres; j < startres; j++)
1236 insertChar(j, new SequenceI[]
1237 { seq }, fixedRight);
1242 editCommand.appendEdit(EditCommand.INSERT_NUC, new SequenceI[]
1243 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1250 seqCanvas.repaint();
1253 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1255 int blankColumn = fixedColumn;
1256 for (int s = 0; s < seq.length; s++)
1258 // Find the next gap before the end of the visible region boundary
1259 // If lastCol > j, theres a boundary after the gap insertion
1261 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1263 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1265 // Theres a space, so break and insert the gap
1270 if (blankColumn <= j)
1272 blankColumn = fixedColumn;
1278 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1279 av.getAlignment(), true);
1281 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1, av.getAlignment(),
1286 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1289 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1, av.getAlignment(),
1292 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1293 av.getAlignment(), true);
1303 public void mouseEntered(MouseEvent e)
1310 if (scrollThread != null)
1312 scrollThread.running = false;
1313 scrollThread = null;
1324 public void mouseExited(MouseEvent e)
1326 if (av.getWrapAlignment())
1333 scrollThread = new ScrollThread();
1338 public void mouseClicked(MouseEvent evt)
1340 SequenceGroup sg = null;
1341 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1342 if (evt.getClickCount() > 1)
1344 sg = av.getSelectionGroup();
1345 if (sg != null && sg.getSize() == 1
1346 && sg.getEndRes() - sg.getStartRes() < 2)
1348 av.setSelectionGroup(null);
1351 SequenceFeature[] features = findFeaturesAtRes(
1352 sequence.getDatasetSequence(),
1353 sequence.findPosition(findRes(evt)));
1355 if (features != null && features.length > 0)
1357 SearchResults highlight = new SearchResults();
1358 highlight.addResult(sequence, features[0].getBegin(),
1359 features[0].getEnd());
1360 seqCanvas.highlightSearchResults(highlight);
1362 if (features != null && features.length > 0)
1364 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1365 { sequence }, features, false, ap);
1367 seqCanvas.highlightSearchResults(null);
1373 public void mouseWheelMoved(MouseWheelEvent e)
1376 if (e.getWheelRotation() > 0)
1378 if (e.isShiftDown())
1380 ap.scrollRight(true);
1388 if (e.isShiftDown())
1390 ap.scrollRight(false);
1395 // TODO Update tooltip for new position.
1404 public void doMousePressedDefineMode(MouseEvent evt)
1406 int res = findRes(evt);
1407 int seq = findSeq(evt);
1410 startWrapBlock = wrappedBlock;
1412 if (av.wrapAlignment && seq > av.getAlignment().getHeight())
1414 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1415 "Cannot edit annotations in wrapped view.",
1416 "Wrapped view - no edit", JOptionPane.WARNING_MESSAGE);
1420 if (seq < 0 || res < 0)
1425 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1427 if ((sequence == null) || (res > sequence.getLength()))
1432 stretchGroup = av.getSelectionGroup();
1434 if (stretchGroup == null)
1436 stretchGroup = av.getAlignment().findGroup(sequence);
1438 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1439 && (res < stretchGroup.getEndRes()))
1441 av.setSelectionGroup(stretchGroup);
1445 stretchGroup = null;
1448 else if (!stretchGroup.getSequences(null).contains(sequence)
1449 || (stretchGroup.getStartRes() > res)
1450 || (stretchGroup.getEndRes() < res))
1452 stretchGroup = null;
1454 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1456 if (allGroups != null)
1458 for (int i = 0; i < allGroups.length; i++)
1460 if ((allGroups[i].getStartRes() <= res)
1461 && (allGroups[i].getEndRes() >= res))
1463 stretchGroup = allGroups[i];
1469 av.setSelectionGroup(stretchGroup);
1473 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1475 SequenceFeature[] allFeatures = findFeaturesAtRes(
1476 sequence.getDatasetSequence(), sequence.findPosition(res));
1477 Vector links = new Vector();
1478 for (int i = 0; i < allFeatures.length; i++)
1480 if (allFeatures[i].links != null)
1482 for (int j = 0; j < allFeatures[i].links.size(); j++)
1484 links.addElement(allFeatures[i].links.elementAt(j));
1489 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1490 pop.show(this, evt.getX(), evt.getY());
1496 seqCanvas.cursorX = findRes(evt);
1497 seqCanvas.cursorY = findSeq(evt);
1498 seqCanvas.repaint();
1502 if (stretchGroup == null)
1504 // Only if left mouse button do we want to change group sizes
1506 // define a new group here
1507 SequenceGroup sg = new SequenceGroup();
1508 sg.setStartRes(res);
1510 sg.addSequence(sequence, false);
1511 av.setSelectionGroup(sg);
1515 if (av.getConservationSelected())
1517 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1521 if (av.getAbovePIDThreshold())
1523 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1526 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1528 // Edit end res position of selected group
1529 changeEndRes = true;
1531 else if ((stretchGroup != null)
1532 && (stretchGroup.getStartRes() == res))
1534 // Edit end res position of selected group
1535 changeStartRes = true;
1537 stretchGroup.getWidth();
1540 seqCanvas.repaint();
1549 public void doMouseReleasedDefineMode(MouseEvent evt)
1551 if (stretchGroup == null)
1556 stretchGroup.recalcConservation(); // always do this - annotation has own
1558 if (stretchGroup.cs != null)
1560 stretchGroup.cs.alignmentChanged(
1561 stretchGroup,av.getHiddenRepSequences());
1563 if (stretchGroup.cs.conservationApplied())
1565 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1566 stretchGroup.getName());
1570 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1571 stretchGroup.getName());
1574 PaintRefresher.Refresh(this, av.getSequenceSetId());
1575 ap.paintAlignment(true);
1577 changeEndRes = false;
1578 changeStartRes = false;
1579 stretchGroup = null;
1589 public void doMouseDraggedDefineMode(MouseEvent evt)
1591 int res = findRes(evt);
1592 int y = findSeq(evt);
1594 if (wrappedBlock != startWrapBlock)
1599 if (stretchGroup == null)
1604 if (res >= av.getAlignment().getWidth())
1606 res = av.getAlignment().getWidth() - 1;
1609 if (stretchGroup.getEndRes() == res)
1611 // Edit end res position of selected group
1612 changeEndRes = true;
1614 else if (stretchGroup.getStartRes() == res)
1616 // Edit start res position of selected group
1617 changeStartRes = true;
1620 if (res < av.getStartRes())
1622 res = av.getStartRes();
1627 if (res > (stretchGroup.getStartRes() - 1))
1629 stretchGroup.setEndRes(res);
1632 else if (changeStartRes)
1634 if (res < (stretchGroup.getEndRes() + 1))
1636 stretchGroup.setStartRes(res);
1640 int dragDirection = 0;
1646 else if (y < oldSeq)
1651 while ((y != oldSeq) && (oldSeq > -1) && (y < av.getAlignment().getHeight()))
1653 // This routine ensures we don't skip any sequences, as the
1654 // selection is quite slow.
1655 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1657 oldSeq += dragDirection;
1664 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1666 if (stretchGroup.getSequences(null).contains(nextSeq))
1668 stretchGroup.deleteSequence(seq, false);
1674 stretchGroup.addSequence(seq, false);
1677 stretchGroup.addSequence(nextSeq, false);
1686 mouseDragging = true;
1688 if (scrollThread != null)
1690 scrollThread.setEvent(evt);
1693 seqCanvas.repaint();
1696 void scrollCanvas(MouseEvent evt)
1700 if (scrollThread != null)
1702 scrollThread.running = false;
1703 scrollThread = null;
1705 mouseDragging = false;
1709 if (scrollThread == null)
1711 scrollThread = new ScrollThread();
1714 mouseDragging = true;
1715 scrollThread.setEvent(evt);
1720 // this class allows scrolling off the bottom of the visible alignment
1721 class ScrollThread extends Thread
1725 boolean running = false;
1727 public ScrollThread()
1732 public void setEvent(MouseEvent e)
1737 public void stopScrolling()
1751 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1753 running = ap.scrollUp(true);
1756 if (mouseDragging && (evt.getY() >= getHeight())
1757 && (av.getAlignment().getHeight() > av.getEndSeq()))
1759 running = ap.scrollUp(false);
1762 if (mouseDragging && (evt.getX() < 0))
1764 running = ap.scrollRight(false);
1766 else if (mouseDragging && (evt.getX() >= getWidth()))
1768 running = ap.scrollRight(true);
1775 } catch (Exception ex)
1783 * modify current selection according to a received message.
1786 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1787 SelectionSource source)
1789 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1790 // handles selection messages...
1791 // TODO: extend config options to allow user to control if selections may be
1792 // shared between viewports.
1794 || !av.followSelection
1795 || (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1796 || (source instanceof AlignViewport && ((AlignViewport) source)
1797 .getSequenceSetId().equals(av.getSequenceSetId())))
1801 // do we want to thread this ? (contention with seqsel and colsel locks, I
1803 // rules are: colsel is copied if there is a real intersection between
1804 // sequence selection
1805 boolean repaint = false, copycolsel = true;
1806 // if (!av.isSelectionGroupChanged(false))
1808 SequenceGroup sgroup = null;
1809 if (seqsel != null && seqsel.getSize()>0)
1811 if (av.getAlignment() == null)
1813 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1814 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1815 + " 's alignment is NULL! returning immediatly.");
1818 sgroup = seqsel.intersect(av.getAlignment(),
1819 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1820 if ((sgroup == null || sgroup.getSize() == 0)
1821 || (colsel == null || colsel.size() == 0))
1823 // don't copy columns if the region didn't intersect.
1827 if (sgroup != null && sgroup.getSize() > 0)
1829 av.setSelectionGroup(sgroup);
1833 av.setSelectionGroup(null);
1835 av.isSelectionGroupChanged(true);
1840 // the current selection is unset or from a previous message
1841 // so import the new colsel.
1842 if (colsel == null || colsel.size() == 0)
1844 if (av.getColumnSelection() != null)
1846 av.getColumnSelection().clear();
1852 // TODO: shift colSel according to the intersecting sequences
1853 if (av.getColumnSelection() == null)
1855 av.setColumnSelection(new ColumnSelection(colsel));
1859 av.getColumnSelection().setElementsFrom(colsel);
1862 av.isColSelChanged(true);
1865 if (copycolsel && av.hasHiddenColumns()
1866 && (av.getColumnSelection() == null || av.getColumnSelection().getHiddenColumns() == null))
1868 System.err.println("Bad things");
1872 // probably finessing with multiple redraws here
1873 PaintRefresher.Refresh(this, av.getSequenceSetId());
1874 // ap.paintAlignment(false);