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.*;
34 import jalview.util.MessageManager;
40 * @version $Revision: 1.130 $
42 public class SeqPanel extends JPanel implements MouseListener,
43 MouseMotionListener, MouseWheelListener, SequenceListener,
48 public SeqCanvas seqCanvas;
51 public AlignmentPanel ap;
53 protected int lastres;
55 protected int startseq;
57 protected AlignViewport av;
59 ScrollThread scrollThread = null;
61 boolean mouseDragging = false;
63 boolean editingSeqs = false;
65 boolean groupEditing = false;
67 // ////////////////////////////////////////
68 // ///Everything below this is for defining the boundary of the rubberband
69 // ////////////////////////////////////////
72 boolean changeEndSeq = false;
74 boolean changeStartSeq = false;
76 boolean changeEndRes = false;
78 boolean changeStartRes = false;
80 SequenceGroup stretchGroup = null;
82 boolean remove = false;
86 boolean mouseWheelPressed = false;
88 StringBuffer keyboardNo1;
90 StringBuffer keyboardNo2;
92 java.net.URL linkImageURL;
94 private final SequenceAnnotationReport seqARep;
96 StringBuffer tooltipText = new StringBuffer("<html>");
100 EditCommand editCommand;
102 StructureSelectionManager ssm;
105 * Creates a new SeqPanel object.
112 public SeqPanel(AlignViewport av, AlignmentPanel ap)
114 linkImageURL = getClass().getResource("/images/link.gif");
115 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
116 ToolTipManager.sharedInstance().registerComponent(this);
117 ToolTipManager.sharedInstance().setInitialDelay(0);
118 ToolTipManager.sharedInstance().setDismissDelay(10000);
120 setBackground(Color.white);
122 seqCanvas = new SeqCanvas(ap);
123 setLayout(new BorderLayout());
124 add(seqCanvas, BorderLayout.CENTER);
130 addMouseMotionListener(this);
131 addMouseListener(this);
132 addMouseWheelListener(this);
133 ssm = StructureSelectionManager
134 .getStructureSelectionManager(Desktop.instance);
135 ssm.addStructureViewerListener(this);
136 ssm.addSelectionListener(this);
140 int startWrapBlock = -1;
142 int wrappedBlock = -1;
144 int findRes(MouseEvent evt)
149 if (av.wrapAlignment)
152 int hgap = av.charHeight;
153 if (av.scaleAboveWrapped)
155 hgap += av.charHeight;
158 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
159 + seqCanvas.getAnnotationHeight();
163 x -= seqCanvas.LABEL_WEST;
165 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
171 wrappedBlock = y / cHeight;
172 wrappedBlock += av.getStartRes() / cwidth;
174 res = wrappedBlock * cwidth + x / av.getCharWidth();
179 if (x > seqCanvas.getWidth() + seqCanvas.getWidth())
181 // make sure we calculate relative to visible alignment, rather than
183 x = seqCanvas.getX() + seqCanvas.getWidth();
185 res = (x / av.getCharWidth()) + av.getStartRes();
188 if (av.hasHiddenColumns())
190 res = av.getColumnSelection().adjustForHiddenColumns(res);
197 int findSeq(MouseEvent evt)
202 if (av.wrapAlignment)
204 int hgap = av.charHeight;
205 if (av.scaleAboveWrapped)
207 hgap += av.charHeight;
210 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
211 + seqCanvas.getAnnotationHeight();
215 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
220 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
221 .getAlignment().getHeight() - 1);
227 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
229 Vector tmp = new Vector();
230 SequenceFeature[] features = sequence.getSequenceFeatures();
231 if (features != null)
233 for (int i = 0; i < features.length; i++)
235 if (av.featuresDisplayed == null
236 || !av.featuresDisplayed.containsKey(features[i].getType()))
241 if (features[i].featureGroup != null
242 && seqCanvas.fr.featureGroups != null
243 && seqCanvas.fr.featureGroups
244 .containsKey(features[i].featureGroup)
245 && !((Boolean) seqCanvas.fr.featureGroups
246 .get(features[i].featureGroup)).booleanValue())
249 if ((features[i].getBegin() <= res)
250 && (features[i].getEnd() >= res))
252 tmp.addElement(features[i]);
257 features = new SequenceFeature[tmp.size()];
258 tmp.copyInto(features);
265 if (editCommand != null && editCommand.getSize() > 0)
267 ap.alignFrame.addHistoryItem(editCommand);
268 av.firePropertyChange("alignment", null, av.getAlignment()
275 groupEditing = false;
283 seqCanvas.cursorY = getKeyboardNo1() - 1;
287 void setCursorColumn()
289 seqCanvas.cursorX = getKeyboardNo1() - 1;
293 void setCursorRowAndColumn()
295 if (keyboardNo2 == null)
297 keyboardNo2 = new StringBuffer();
301 seqCanvas.cursorX = getKeyboardNo1() - 1;
302 seqCanvas.cursorY = getKeyboardNo2() - 1;
307 void setCursorPosition()
309 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
311 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
315 void moveCursor(int dx, int dy)
317 seqCanvas.cursorX += dx;
318 seqCanvas.cursorY += dy;
319 if (av.hasHiddenColumns()
320 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
322 int original = seqCanvas.cursorX - dx;
323 int maxWidth = av.getAlignment().getWidth();
325 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
326 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
328 seqCanvas.cursorX += dx;
331 if (seqCanvas.cursorX >= maxWidth
332 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
334 seqCanvas.cursorX = original;
341 void scrollToVisible()
343 if (seqCanvas.cursorX < 0)
345 seqCanvas.cursorX = 0;
347 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
349 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
352 if (seqCanvas.cursorY < 0)
354 seqCanvas.cursorY = 0;
356 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
358 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
362 if (av.wrapAlignment)
364 ap.scrollToWrappedVisible(seqCanvas.cursorX);
368 while (seqCanvas.cursorY < av.startSeq)
372 while (seqCanvas.cursorY + 1 > av.endSeq)
376 if (!av.wrapAlignment)
378 while (seqCanvas.cursorX < av.getColumnSelection()
379 .adjustForHiddenColumns(av.startRes))
381 if (!ap.scrollRight(false))
386 while (seqCanvas.cursorX > av.getColumnSelection()
387 .adjustForHiddenColumns(av.endRes))
389 if (!ap.scrollRight(true))
396 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
397 seqCanvas.cursorX, seqCanvas.cursorY);
402 void setSelectionAreaAtCursor(boolean topLeft)
404 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
406 if (av.getSelectionGroup() != null)
408 SequenceGroup sg = av.getSelectionGroup();
409 // Find the top and bottom of this group
410 int min = av.getAlignment().getHeight(), max = 0;
411 for (int i = 0; i < sg.getSize(); i++)
413 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
428 sg.setStartRes(seqCanvas.cursorX);
429 if (sg.getEndRes() < seqCanvas.cursorX)
431 sg.setEndRes(seqCanvas.cursorX);
434 min = seqCanvas.cursorY;
438 sg.setEndRes(seqCanvas.cursorX);
439 if (sg.getStartRes() > seqCanvas.cursorX)
441 sg.setStartRes(seqCanvas.cursorX);
444 max = seqCanvas.cursorY + 1;
449 // Only the user can do this
450 av.setSelectionGroup(null);
454 // Now add any sequences between min and max
455 sg.getSequences(null).clear();
456 for (int i = min; i < max; i++)
458 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
463 if (av.getSelectionGroup() == null)
465 SequenceGroup sg = new SequenceGroup();
466 sg.setStartRes(seqCanvas.cursorX);
467 sg.setEndRes(seqCanvas.cursorX);
468 sg.addSequence(sequence, false);
469 av.setSelectionGroup(sg);
472 ap.paintAlignment(false);
476 void insertGapAtCursor(boolean group)
478 groupEditing = group;
479 startseq = seqCanvas.cursorY;
480 lastres = seqCanvas.cursorX;
481 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
485 void deleteGapAtCursor(boolean group)
487 groupEditing = group;
488 startseq = seqCanvas.cursorY;
489 lastres = seqCanvas.cursorX + getKeyboardNo1();
490 editSequence(false, false, seqCanvas.cursorX);
494 void insertNucAtCursor(boolean group, String nuc)
496 groupEditing = group;
497 startseq = seqCanvas.cursorY;
498 lastres = seqCanvas.cursorX;
499 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
503 void numberPressed(char value)
505 if (keyboardNo1 == null)
507 keyboardNo1 = new StringBuffer();
510 if (keyboardNo2 != null)
512 keyboardNo2.append(value);
516 keyboardNo1.append(value);
523 if (keyboardNo1 != null)
525 int value = Integer.parseInt(keyboardNo1.toString());
529 } catch (Exception x)
538 if (keyboardNo2!=null){
539 int value = Integer.parseInt(keyboardNo2.toString());
543 } catch (Exception x)
556 public void mouseReleased(MouseEvent evt)
558 mouseDragging = false;
559 mouseWheelPressed = false;
563 doMouseReleasedDefineMode(evt);
577 public void mousePressed(MouseEvent evt)
579 lastMousePress = evt.getPoint();
581 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
583 mouseWheelPressed = true;
587 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
589 if (evt.isAltDown() || evt.isControlDown())
597 doMousePressedDefineMode(evt);
601 int seq = findSeq(evt);
602 int res = findRes(evt);
604 if (seq < 0 || res < 0)
609 if ((seq < av.getAlignment().getHeight())
610 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
627 public void mouseOverSequence(SequenceI sequence, int index, int pos)
629 String tmp = sequence.hashCode() + " " + index + " " + pos;
631 if (lastMessage == null || !lastMessage.equals(tmp))
633 // System.err.println("mouseOver Sequence: "+tmp);
634 ssm.mouseOverSequence(sequence, index, pos, av);
640 public void highlightSequence(SearchResults results)
642 if (av.followHighlight)
644 if (ap.scrollToPosition(results, false))
646 seqCanvas.revalidate();
649 seqCanvas.highlightSearchResults(results);
653 public void updateColours(SequenceI seq, int index)
655 System.out.println("update the seqPanel colours");
666 public void mouseMoved(MouseEvent evt)
670 // This is because MacOSX creates a mouseMoved
671 // If control is down, other platforms will not.
675 int res = findRes(evt);
676 int seq = findSeq(evt);
678 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
683 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
685 if (res >= sequence.getLength())
690 pos = setStatusMessage(sequence, res, seq);
691 if (ssm != null && pos > -1)
692 mouseOverSequence(sequence, res, pos);
694 tooltipText.setLength(6); // Cuts the buffer back to <html>
696 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
699 for (int g = 0; g < groups.length; g++)
701 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
703 if (tooltipText.length() > 6)
705 tooltipText.append("<br>");
708 if (!groups[g].getName().startsWith("JTreeGroup")
709 && !groups[g].getName().startsWith("JGroup"))
711 tooltipText.append(groups[g].getName());
714 if (groups[g].getDescription() != null)
716 tooltipText.append(": " + groups[g].getDescription());
722 // use aa to see if the mouse pointer is on a
723 if (av.showSequenceFeatures)
726 SequenceFeature[] features = findFeaturesAtRes(
727 sequence.getDatasetSequence(),
728 rpos = sequence.findPosition(res));
729 seqARep.appendFeatures(tooltipText, rpos, features,
730 this.ap.seqPanel.seqCanvas.fr.minmax);
732 if (tooltipText.length() == 6) // <html></html>
734 setToolTipText(null);
739 tooltipText.append("</html>");
740 if (lastTooltip == null
741 || !lastTooltip.equals(tooltipText.toString()))
743 setToolTipText(tooltipText.toString());
744 lastTooltip = tooltipText.toString();
751 private Point lastp = null;
756 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
758 public Point getToolTipLocation(MouseEvent event)
760 int x = event.getX(), w = getWidth();
761 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
764 if (!event.isShiftDown() || p == null)
766 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
767 event.getX() + wdth, event.getY() - 20) : null;
770 * TODO: try to modify position region is not obcured by tooltip
778 * Set status message in alignment panel
781 * aligned sequence object
785 * index of sequence in alignment
786 * @return position of res in sequence
788 int setStatusMessage(SequenceI sequence, int res, int seq)
791 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
792 + sequence.getName());
795 if (av.getAlignment().isNucleotide())
797 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
801 text.append(" Nucleotide: ");
806 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
809 text.append(" Residue: ");
815 pos = sequence.findPosition(res);
818 text.append(obj + " (" + pos + ")");
821 ap.alignFrame.statusBar.setText(text.toString());
832 public void mouseDragged(MouseEvent evt)
834 if (mouseWheelPressed)
836 int oldWidth = av.charWidth;
838 // Which is bigger, left-right or up-down?
839 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
840 .getX() - lastMousePress.getX()))
842 int fontSize = av.font.getSize();
844 if (evt.getY() < lastMousePress.getY())
848 else if (evt.getY() > lastMousePress.getY())
858 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
859 av.charWidth = oldWidth;
864 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
868 else if (evt.getX() > lastMousePress.getX())
873 ap.paintAlignment(false);
876 FontMetrics fm = getFontMetrics(av.getFont());
877 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
879 lastMousePress = evt.getPoint();
886 doMouseDraggedDefineMode(evt);
890 int res = findRes(evt);
897 if ((lastres == -1) || (lastres == res))
902 if ((res < av.getAlignment().getWidth()) && (res < lastres))
904 // dragLeft, delete gap
905 editSequence(false, false, res);
909 editSequence(true, false, res);
912 mouseDragging = true;
913 if (scrollThread != null)
915 scrollThread.setEvent(evt);
919 // TODO: Make it more clever than many booleans
920 synchronized void editSequence(boolean insertGap, boolean editSeq,
925 boolean fixedColumns = false;
926 SequenceGroup sg = av.getSelectionGroup();
928 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
930 // No group, but the sequence may represent a group
931 if (!groupEditing && av.hasHiddenRows())
933 if (av.isHiddenRepSequence(seq))
935 sg = av.getRepresentedSequences(seq);
940 StringBuffer message = new StringBuffer();
943 message.append("Edit group:");
944 if (editCommand == null)
946 editCommand = new EditCommand("Edit Group");
951 message.append("Edit sequence: " + seq.getName());
952 String label = seq.getName();
953 if (label.length() > 10)
955 label = label.substring(0, 10);
957 if (editCommand == null)
959 editCommand = new EditCommand("Edit " + label);
965 message.append(" insert ");
969 message.append(" delete ");
972 message.append(Math.abs(startres - lastres) + " gaps.");
973 ap.alignFrame.statusBar.setText(message.toString());
975 // Are we editing within a selection group?
977 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
982 // sg might be null as the user may only see 1 sequence,
983 // but the sequence represents a group
986 if (!av.isHiddenRepSequence(seq))
991 sg = av.getRepresentedSequences(seq);
994 fixedLeft = sg.getStartRes();
995 fixedRight = sg.getEndRes();
997 if ((startres < fixedLeft && lastres >= fixedLeft)
998 || (startres >= fixedLeft && lastres < fixedLeft)
999 || (startres > fixedRight && lastres <= fixedRight)
1000 || (startres <= fixedRight && lastres > fixedRight))
1006 if (fixedLeft > startres)
1008 fixedRight = fixedLeft - 1;
1011 else if (fixedRight < startres)
1013 fixedLeft = fixedRight;
1018 if (av.hasHiddenColumns())
1020 fixedColumns = true;
1021 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1022 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1024 if ((insertGap && startres > y1 && lastres < y1)
1025 || (!insertGap && startres < y2 && lastres > y2))
1031 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1032 // Selection spans a hidden region
1033 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1041 fixedRight = y2 - 1;
1048 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1049 int g, groupSize = vseqs.size();
1050 SequenceI[] groupSeqs = new SequenceI[groupSize];
1051 for (g = 0; g < groupSeqs.length; g++)
1053 groupSeqs[g] = vseqs.get(g);
1059 // If the user has selected the whole sequence, and is dragging to
1060 // the right, we can still extend the alignment and selectionGroup
1061 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1062 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1064 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1065 fixedRight = sg.getEndRes();
1068 // Is it valid with fixed columns??
1069 // Find the next gap before the end
1070 // of the visible region boundary
1071 boolean blank = false;
1072 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1076 for (g = 0; g < groupSize; g++)
1078 for (int j = 0; j < startres - lastres; j++)
1080 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1081 .getCharAt(fixedRight - j)))
1096 if (sg.getSize() == av.getAlignment().getHeight())
1098 if ((av.hasHiddenColumns() && startres < av
1099 .getColumnSelection().getHiddenBoundaryRight(startres)))
1105 int alWidth = av.getAlignment().getWidth();
1106 if (av.hasHiddenRows())
1108 int hwidth = av.getAlignment().getHiddenSequences()
1110 if (hwidth > alWidth)
1115 // We can still insert gaps if the selectionGroup
1116 // contains all the sequences
1117 sg.setEndRes(sg.getEndRes() + startres - lastres);
1118 fixedRight = alWidth + startres - lastres;
1129 else if (!insertGap)
1131 // / Are we able to delete?
1132 // ie are all columns blank?
1134 for (g = 0; g < groupSize; g++)
1136 for (int j = startres; j < lastres; j++)
1138 if (groupSeqs[g].getLength() <= j)
1143 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1145 // Not a gap, block edit not valid
1155 // dragging to the right
1156 if (fixedColumns && fixedRight != -1)
1158 for (int j = lastres; j < startres; j++)
1160 insertChar(j, groupSeqs, fixedRight);
1165 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1166 startres, startres - lastres, av.getAlignment(), true);
1171 // dragging to the left
1172 if (fixedColumns && fixedRight != -1)
1174 for (int j = lastres; j > startres; j--)
1176 deleteChar(startres, groupSeqs, fixedRight);
1181 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1182 startres, lastres - startres, av.getAlignment(), true);
1188 // ///Editing a single sequence///////////
1192 // dragging to the right
1193 if (fixedColumns && fixedRight != -1)
1195 for (int j = lastres; j < startres; j++)
1197 insertChar(j, new SequenceI[]
1198 { seq }, fixedRight);
1203 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1204 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1211 // dragging to the left
1212 if (fixedColumns && fixedRight != -1)
1214 for (int j = lastres; j > startres; j--)
1216 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1221 deleteChar(startres, new SequenceI[]
1222 { seq }, fixedRight);
1227 // could be a keyboard edit trying to delete none gaps
1229 for (int m = startres; m < lastres; m++)
1231 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1240 editCommand.appendEdit(EditCommand.DELETE_GAP,
1242 { seq }, startres, max, av.getAlignment(), true);
1247 {// insertGap==false AND editSeq==TRUE;
1248 if (fixedColumns && fixedRight != -1)
1250 for (int j = lastres; j < startres; j++)
1252 insertChar(j, new SequenceI[]
1253 { seq }, fixedRight);
1258 editCommand.appendEdit(EditCommand.INSERT_NUC, new SequenceI[]
1259 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1266 seqCanvas.repaint();
1269 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1271 int blankColumn = fixedColumn;
1272 for (int s = 0; s < seq.length; s++)
1274 // Find the next gap before the end of the visible region boundary
1275 // If lastCol > j, theres a boundary after the gap insertion
1277 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1279 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1281 // Theres a space, so break and insert the gap
1286 if (blankColumn <= j)
1288 blankColumn = fixedColumn;
1294 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1295 av.getAlignment(), true);
1297 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1,
1298 av.getAlignment(), true);
1302 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1305 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1,
1306 av.getAlignment(), true);
1308 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1309 av.getAlignment(), true);
1319 public void mouseEntered(MouseEvent e)
1326 if (scrollThread != null)
1328 scrollThread.running = false;
1329 scrollThread = null;
1340 public void mouseExited(MouseEvent e)
1342 if (av.getWrapAlignment())
1349 scrollThread = new ScrollThread();
1354 public void mouseClicked(MouseEvent evt)
1356 SequenceGroup sg = null;
1357 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1358 if (evt.getClickCount() > 1)
1360 sg = av.getSelectionGroup();
1361 if (sg != null && sg.getSize() == 1
1362 && sg.getEndRes() - sg.getStartRes() < 2)
1364 av.setSelectionGroup(null);
1367 SequenceFeature[] features = findFeaturesAtRes(
1368 sequence.getDatasetSequence(),
1369 sequence.findPosition(findRes(evt)));
1371 if (features != null && features.length > 0)
1373 SearchResults highlight = new SearchResults();
1374 highlight.addResult(sequence, features[0].getBegin(),
1375 features[0].getEnd());
1376 seqCanvas.highlightSearchResults(highlight);
1378 if (features != null && features.length > 0)
1380 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1381 { sequence }, features, false, ap);
1383 seqCanvas.highlightSearchResults(null);
1389 public void mouseWheelMoved(MouseWheelEvent e)
1392 if (e.getWheelRotation() > 0)
1394 if (e.isShiftDown())
1396 ap.scrollRight(true);
1406 if (e.isShiftDown())
1408 ap.scrollRight(false);
1415 // TODO Update tooltip for new position.
1424 public void doMousePressedDefineMode(MouseEvent evt)
1426 int res = findRes(evt);
1427 int seq = findSeq(evt);
1430 startWrapBlock = wrappedBlock;
1432 if (av.wrapAlignment && seq > av.getAlignment().getHeight())
1434 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1435 MessageManager.getString("label.cannot_edit_annotations_in_wrapped_view"),
1436 MessageManager.getString("label.wrapped_view_no_edit"), JOptionPane.WARNING_MESSAGE);
1440 if (seq < 0 || res < 0)
1445 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1447 if ((sequence == null) || (res > sequence.getLength()))
1452 stretchGroup = av.getSelectionGroup();
1454 if (stretchGroup == null)
1456 stretchGroup = av.getAlignment().findGroup(sequence);
1458 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1459 && (res < stretchGroup.getEndRes()))
1461 av.setSelectionGroup(stretchGroup);
1465 stretchGroup = null;
1468 else if (!stretchGroup.getSequences(null).contains(sequence)
1469 || (stretchGroup.getStartRes() > res)
1470 || (stretchGroup.getEndRes() < res))
1472 stretchGroup = null;
1474 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1476 if (allGroups != null)
1478 for (int i = 0; i < allGroups.length; i++)
1480 if ((allGroups[i].getStartRes() <= res)
1481 && (allGroups[i].getEndRes() >= res))
1483 stretchGroup = allGroups[i];
1489 av.setSelectionGroup(stretchGroup);
1493 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1495 SequenceFeature[] allFeatures = findFeaturesAtRes(
1496 sequence.getDatasetSequence(), sequence.findPosition(res));
1497 Vector links = new Vector();
1498 for (int i = 0; i < allFeatures.length; i++)
1500 if (allFeatures[i].links != null)
1502 for (int j = 0; j < allFeatures[i].links.size(); j++)
1504 links.addElement(allFeatures[i].links.elementAt(j));
1509 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1510 pop.show(this, evt.getX(), evt.getY());
1516 seqCanvas.cursorX = findRes(evt);
1517 seqCanvas.cursorY = findSeq(evt);
1518 seqCanvas.repaint();
1522 if (stretchGroup == null)
1524 // Only if left mouse button do we want to change group sizes
1526 // define a new group here
1527 SequenceGroup sg = new SequenceGroup();
1528 sg.setStartRes(res);
1530 sg.addSequence(sequence, false);
1531 av.setSelectionGroup(sg);
1535 if (av.getConservationSelected())
1537 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1541 if (av.getAbovePIDThreshold())
1543 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1546 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1548 // Edit end res position of selected group
1549 changeEndRes = true;
1551 else if ((stretchGroup != null)
1552 && (stretchGroup.getStartRes() == res))
1554 // Edit end res position of selected group
1555 changeStartRes = true;
1557 stretchGroup.getWidth();
1560 seqCanvas.repaint();
1569 public void doMouseReleasedDefineMode(MouseEvent evt)
1571 if (stretchGroup == null)
1576 stretchGroup.recalcConservation(); // always do this - annotation has own
1578 if (stretchGroup.cs != null)
1580 stretchGroup.cs.alignmentChanged(stretchGroup,
1581 av.getHiddenRepSequences());
1583 if (stretchGroup.cs.conservationApplied())
1585 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1586 stretchGroup.getName());
1590 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1591 stretchGroup.getName());
1594 PaintRefresher.Refresh(this, av.getSequenceSetId());
1595 ap.paintAlignment(true);
1597 changeEndRes = false;
1598 changeStartRes = false;
1599 stretchGroup = null;
1609 public void doMouseDraggedDefineMode(MouseEvent evt)
1611 int res = findRes(evt);
1612 int y = findSeq(evt);
1614 if (wrappedBlock != startWrapBlock)
1619 if (stretchGroup == null)
1624 if (res >= av.getAlignment().getWidth())
1626 res = av.getAlignment().getWidth() - 1;
1629 if (stretchGroup.getEndRes() == res)
1631 // Edit end res position of selected group
1632 changeEndRes = true;
1634 else if (stretchGroup.getStartRes() == res)
1636 // Edit start res position of selected group
1637 changeStartRes = true;
1640 if (res < av.getStartRes())
1642 res = av.getStartRes();
1647 if (res > (stretchGroup.getStartRes() - 1))
1649 stretchGroup.setEndRes(res);
1652 else if (changeStartRes)
1654 if (res < (stretchGroup.getEndRes() + 1))
1656 stretchGroup.setStartRes(res);
1660 int dragDirection = 0;
1666 else if (y < oldSeq)
1671 while ((y != oldSeq) && (oldSeq > -1)
1672 && (y < av.getAlignment().getHeight()))
1674 // This routine ensures we don't skip any sequences, as the
1675 // selection is quite slow.
1676 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1678 oldSeq += dragDirection;
1685 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1687 if (stretchGroup.getSequences(null).contains(nextSeq))
1689 stretchGroup.deleteSequence(seq, false);
1695 stretchGroup.addSequence(seq, false);
1698 stretchGroup.addSequence(nextSeq, false);
1707 mouseDragging = true;
1709 if (scrollThread != null)
1711 scrollThread.setEvent(evt);
1714 seqCanvas.repaint();
1717 void scrollCanvas(MouseEvent evt)
1721 if (scrollThread != null)
1723 scrollThread.running = false;
1724 scrollThread = null;
1726 mouseDragging = false;
1730 if (scrollThread == null)
1732 scrollThread = new ScrollThread();
1735 mouseDragging = true;
1736 scrollThread.setEvent(evt);
1741 // this class allows scrolling off the bottom of the visible alignment
1742 class ScrollThread extends Thread
1746 boolean running = false;
1748 public ScrollThread()
1753 public void setEvent(MouseEvent e)
1758 public void stopScrolling()
1772 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1774 running = ap.scrollUp(true);
1777 if (mouseDragging && (evt.getY() >= getHeight())
1778 && (av.getAlignment().getHeight() > av.getEndSeq()))
1780 running = ap.scrollUp(false);
1783 if (mouseDragging && (evt.getX() < 0))
1785 running = ap.scrollRight(false);
1787 else if (mouseDragging && (evt.getX() >= getWidth()))
1789 running = ap.scrollRight(true);
1796 } catch (Exception ex)
1804 * modify current selection according to a received message.
1807 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1808 SelectionSource source)
1810 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1811 // handles selection messages...
1812 // TODO: extend config options to allow user to control if selections may be
1813 // shared between viewports.
1815 || !av.followSelection
1816 || (av.isSelectionGroupChanged(false) || av
1817 .isColSelChanged(false))
1818 || (source instanceof AlignViewport && ((AlignViewport) source)
1819 .getSequenceSetId().equals(av.getSequenceSetId())))
1823 // do we want to thread this ? (contention with seqsel and colsel locks, I
1825 // rules are: colsel is copied if there is a real intersection between
1826 // sequence selection
1827 boolean repaint = false, copycolsel = true;
1828 // if (!av.isSelectionGroupChanged(false))
1830 SequenceGroup sgroup = null;
1831 if (seqsel != null && seqsel.getSize() > 0)
1833 if (av.getAlignment() == null)
1835 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1836 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1837 + " 's alignment is NULL! returning immediatly.");
1840 sgroup = seqsel.intersect(av.getAlignment(),
1841 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1842 if ((sgroup == null || sgroup.getSize() == 0)
1843 || (colsel == null || colsel.size() == 0))
1845 // don't copy columns if the region didn't intersect.
1849 if (sgroup != null && sgroup.getSize() > 0)
1851 av.setSelectionGroup(sgroup);
1855 av.setSelectionGroup(null);
1857 av.isSelectionGroupChanged(true);
1862 // the current selection is unset or from a previous message
1863 // so import the new colsel.
1864 if (colsel == null || colsel.size() == 0)
1866 if (av.getColumnSelection() != null)
1868 av.getColumnSelection().clear();
1874 // TODO: shift colSel according to the intersecting sequences
1875 if (av.getColumnSelection() == null)
1877 av.setColumnSelection(new ColumnSelection(colsel));
1881 av.getColumnSelection().setElementsFrom(colsel);
1884 av.isColSelChanged(true);
1888 && av.hasHiddenColumns()
1889 && (av.getColumnSelection() == null || av.getColumnSelection()
1890 .getHiddenColumns() == null))
1892 System.err.println("Bad things");
1896 // probably finessing with multiple redraws here
1897 PaintRefresher.Refresh(this, av.getSequenceSetId());
1898 // ap.paintAlignment(false);