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;
92 private final SequenceAnnotationReport seqARep;
94 StringBuffer tooltipText = new StringBuffer("<html>");
98 EditCommand editCommand;
100 StructureSelectionManager ssm;
103 * Creates a new SeqPanel object.
110 public SeqPanel(AlignViewport av, AlignmentPanel ap)
112 linkImageURL = getClass().getResource("/images/link.gif");
113 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
114 ToolTipManager.sharedInstance().registerComponent(this);
115 ToolTipManager.sharedInstance().setInitialDelay(0);
116 ToolTipManager.sharedInstance().setDismissDelay(10000);
118 setBackground(Color.white);
120 seqCanvas = new SeqCanvas(ap);
121 setLayout(new BorderLayout());
122 add(seqCanvas, BorderLayout.CENTER);
128 addMouseMotionListener(this);
129 addMouseListener(this);
130 addMouseWheelListener(this);
131 ssm = StructureSelectionManager
132 .getStructureSelectionManager(Desktop.instance);
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);
520 if (keyboardNo1 == null)
524 int value = Integer.parseInt(keyboardNo1.toString());
532 if (keyboardNo2 == null)
536 int value = Integer.parseInt(keyboardNo2.toString());
549 public void mouseReleased(MouseEvent evt)
551 mouseDragging = false;
552 mouseWheelPressed = false;
556 doMouseReleasedDefineMode(evt);
570 public void mousePressed(MouseEvent evt)
572 lastMousePress = evt.getPoint();
574 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
576 mouseWheelPressed = true;
580 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
582 if (evt.isAltDown() || evt.isControlDown())
590 doMousePressedDefineMode(evt);
594 int seq = findSeq(evt);
595 int res = findRes(evt);
597 if (seq < 0 || res < 0)
602 if ((seq < av.getAlignment().getHeight())
603 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
620 public void mouseOverSequence(SequenceI sequence, int index, int pos)
622 String tmp = sequence.hashCode() + " " + index + " " + pos;
624 if (lastMessage == null || !lastMessage.equals(tmp))
626 // System.err.println("mouseOver Sequence: "+tmp);
627 ssm.mouseOverSequence(sequence, index, pos, av);
633 public void highlightSequence(SearchResults results)
635 if (av.followHighlight)
637 if (ap.scrollToPosition(results, false))
639 seqCanvas.revalidate();
642 seqCanvas.highlightSearchResults(results);
646 public void updateColours(SequenceI seq, int index)
648 System.out.println("update the seqPanel colours");
659 public void mouseMoved(MouseEvent evt)
663 // This is because MacOSX creates a mouseMoved
664 // If control is down, other platforms will not.
668 int res = findRes(evt);
669 int seq = findSeq(evt);
671 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
676 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
678 if (res >= sequence.getLength())
683 pos = setStatusMessage(sequence, res, seq);
684 if (ssm != null && pos > -1)
685 mouseOverSequence(sequence, res, pos);
687 tooltipText.setLength(6); // Cuts the buffer back to <html>
689 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
692 for (int g = 0; g < groups.length; g++)
694 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
696 if (tooltipText.length() > 6)
698 tooltipText.append("<br>");
701 if (!groups[g].getName().startsWith("JTreeGroup")
702 && !groups[g].getName().startsWith("JGroup"))
704 tooltipText.append(groups[g].getName());
707 if (groups[g].getDescription() != null)
709 tooltipText.append(": " + groups[g].getDescription());
715 // use aa to see if the mouse pointer is on a
716 if (av.showSequenceFeatures)
719 SequenceFeature[] features = findFeaturesAtRes(
720 sequence.getDatasetSequence(),
721 rpos = sequence.findPosition(res));
722 seqARep.appendFeatures(tooltipText, rpos, features,
723 this.ap.seqPanel.seqCanvas.fr.minmax);
725 if (tooltipText.length() == 6) // <html></html>
727 setToolTipText(null);
732 tooltipText.append("</html>");
733 if (lastTooltip == null
734 || !lastTooltip.equals(tooltipText.toString()))
736 setToolTipText(tooltipText.toString());
737 lastTooltip = tooltipText.toString();
744 private Point lastp = null;
749 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
751 public Point getToolTipLocation(MouseEvent event)
753 int x = event.getX(), w = getWidth();
754 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
757 if (!event.isShiftDown() || p == null)
759 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
760 event.getX() + wdth, event.getY() - 20) : null;
763 * TODO: try to modify position region is not obcured by tooltip
771 * Set status message in alignment panel
774 * aligned sequence object
778 * index of sequence in alignment
779 * @return position of res in sequence
781 int setStatusMessage(SequenceI sequence, int res, int seq)
784 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
785 + sequence.getName());
788 if (av.getAlignment().isNucleotide())
790 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
794 text.append(" Nucleotide: ");
799 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
802 text.append(" Residue: ");
808 pos = sequence.findPosition(res);
811 text.append(obj + " (" + pos + ")");
814 ap.alignFrame.statusBar.setText(text.toString());
825 public void mouseDragged(MouseEvent evt)
827 if (mouseWheelPressed)
829 int oldWidth = av.charWidth;
831 // Which is bigger, left-right or up-down?
832 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
833 .getX() - lastMousePress.getX()))
835 int fontSize = av.font.getSize();
837 if (evt.getY() < lastMousePress.getY())
841 else if (evt.getY() > lastMousePress.getY())
851 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
852 av.charWidth = oldWidth;
857 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
861 else if (evt.getX() > lastMousePress.getX())
866 ap.paintAlignment(false);
869 FontMetrics fm = getFontMetrics(av.getFont());
870 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
872 lastMousePress = evt.getPoint();
879 doMouseDraggedDefineMode(evt);
883 int res = findRes(evt);
890 if ((lastres == -1) || (lastres == res))
895 if ((res < av.getAlignment().getWidth()) && (res < lastres))
897 // dragLeft, delete gap
898 editSequence(false, false, res);
902 editSequence(true, false, res);
905 mouseDragging = true;
906 if (scrollThread != null)
908 scrollThread.setEvent(evt);
912 // TODO: Make it more clever than many booleans
913 synchronized void editSequence(boolean insertGap, boolean editSeq,
918 boolean fixedColumns = false;
919 SequenceGroup sg = av.getSelectionGroup();
921 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
923 // No group, but the sequence may represent a group
924 if (!groupEditing && av.hasHiddenRows())
926 if (av.isHiddenRepSequence(seq))
928 sg = av.getRepresentedSequences(seq);
933 StringBuffer message = new StringBuffer();
936 message.append("Edit group:");
937 if (editCommand == null)
939 editCommand = new EditCommand("Edit Group");
944 message.append("Edit sequence: " + seq.getName());
945 String label = seq.getName();
946 if (label.length() > 10)
948 label = label.substring(0, 10);
950 if (editCommand == null)
952 editCommand = new EditCommand("Edit " + label);
958 message.append(" insert ");
962 message.append(" delete ");
965 message.append(Math.abs(startres - lastres) + " gaps.");
966 ap.alignFrame.statusBar.setText(message.toString());
968 // Are we editing within a selection group?
970 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
975 // sg might be null as the user may only see 1 sequence,
976 // but the sequence represents a group
979 if (!av.isHiddenRepSequence(seq))
984 sg = av.getRepresentedSequences(seq);
987 fixedLeft = sg.getStartRes();
988 fixedRight = sg.getEndRes();
990 if ((startres < fixedLeft && lastres >= fixedLeft)
991 || (startres >= fixedLeft && lastres < fixedLeft)
992 || (startres > fixedRight && lastres <= fixedRight)
993 || (startres <= fixedRight && lastres > fixedRight))
999 if (fixedLeft > startres)
1001 fixedRight = fixedLeft - 1;
1004 else if (fixedRight < startres)
1006 fixedLeft = fixedRight;
1011 if (av.hasHiddenColumns())
1013 fixedColumns = true;
1014 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1015 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1017 if ((insertGap && startres > y1 && lastres < y1)
1018 || (!insertGap && startres < y2 && lastres > y2))
1024 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1025 // Selection spans a hidden region
1026 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1034 fixedRight = y2 - 1;
1041 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1042 int g, groupSize = vseqs.size();
1043 SequenceI[] groupSeqs = new SequenceI[groupSize];
1044 for (g = 0; g < groupSeqs.length; g++)
1046 groupSeqs[g] = vseqs.get(g);
1052 // If the user has selected the whole sequence, and is dragging to
1053 // the right, we can still extend the alignment and selectionGroup
1054 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1055 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1057 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1058 fixedRight = sg.getEndRes();
1061 // Is it valid with fixed columns??
1062 // Find the next gap before the end
1063 // of the visible region boundary
1064 boolean blank = false;
1065 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1069 for (g = 0; g < groupSize; g++)
1071 for (int j = 0; j < startres - lastres; j++)
1073 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1074 .getCharAt(fixedRight - j)))
1089 if (sg.getSize() == av.getAlignment().getHeight())
1091 if ((av.hasHiddenColumns() && startres < av
1092 .getColumnSelection().getHiddenBoundaryRight(startres)))
1098 int alWidth = av.getAlignment().getWidth();
1099 if (av.hasHiddenRows())
1101 int hwidth = av.getAlignment().getHiddenSequences()
1103 if (hwidth > alWidth)
1108 // We can still insert gaps if the selectionGroup
1109 // contains all the sequences
1110 sg.setEndRes(sg.getEndRes() + startres - lastres);
1111 fixedRight = alWidth + startres - lastres;
1122 else if (!insertGap)
1124 // / Are we able to delete?
1125 // ie are all columns blank?
1127 for (g = 0; g < groupSize; g++)
1129 for (int j = startres; j < lastres; j++)
1131 if (groupSeqs[g].getLength() <= j)
1136 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1138 // Not a gap, block edit not valid
1148 // dragging to the right
1149 if (fixedColumns && fixedRight != -1)
1151 for (int j = lastres; j < startres; j++)
1153 insertChar(j, groupSeqs, fixedRight);
1158 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1159 startres, startres - lastres, av.getAlignment(), true);
1164 // dragging to the left
1165 if (fixedColumns && fixedRight != -1)
1167 for (int j = lastres; j > startres; j--)
1169 deleteChar(startres, groupSeqs, fixedRight);
1174 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1175 startres, lastres - startres, av.getAlignment(), true);
1181 // ///Editing a single sequence///////////
1185 // dragging to the right
1186 if (fixedColumns && fixedRight != -1)
1188 for (int j = lastres; j < startres; j++)
1190 insertChar(j, new SequenceI[]
1191 { seq }, fixedRight);
1196 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1197 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1204 // dragging to the left
1205 if (fixedColumns && fixedRight != -1)
1207 for (int j = lastres; j > startres; j--)
1209 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1214 deleteChar(startres, new SequenceI[]
1215 { seq }, fixedRight);
1220 // could be a keyboard edit trying to delete none gaps
1222 for (int m = startres; m < lastres; m++)
1224 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1233 editCommand.appendEdit(EditCommand.DELETE_GAP,
1235 { seq }, startres, max, av.getAlignment(), true);
1240 {// insertGap==false AND editSeq==TRUE;
1241 if (fixedColumns && fixedRight != -1)
1243 for (int j = lastres; j < startres; j++)
1245 insertChar(j, new SequenceI[]
1246 { seq }, fixedRight);
1251 editCommand.appendEdit(EditCommand.INSERT_NUC, new SequenceI[]
1252 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1259 seqCanvas.repaint();
1262 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1264 int blankColumn = fixedColumn;
1265 for (int s = 0; s < seq.length; s++)
1267 // Find the next gap before the end of the visible region boundary
1268 // If lastCol > j, theres a boundary after the gap insertion
1270 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1272 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1274 // Theres a space, so break and insert the gap
1279 if (blankColumn <= j)
1281 blankColumn = fixedColumn;
1287 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1288 av.getAlignment(), true);
1290 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1,
1291 av.getAlignment(), true);
1295 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1298 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1,
1299 av.getAlignment(), true);
1301 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1302 av.getAlignment(), true);
1312 public void mouseEntered(MouseEvent e)
1319 if (scrollThread != null)
1321 scrollThread.running = false;
1322 scrollThread = null;
1333 public void mouseExited(MouseEvent e)
1335 if (av.getWrapAlignment())
1342 scrollThread = new ScrollThread();
1347 public void mouseClicked(MouseEvent evt)
1349 SequenceGroup sg = null;
1350 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1351 if (evt.getClickCount() > 1)
1353 sg = av.getSelectionGroup();
1354 if (sg != null && sg.getSize() == 1
1355 && sg.getEndRes() - sg.getStartRes() < 2)
1357 av.setSelectionGroup(null);
1360 SequenceFeature[] features = findFeaturesAtRes(
1361 sequence.getDatasetSequence(),
1362 sequence.findPosition(findRes(evt)));
1364 if (features != null && features.length > 0)
1366 SearchResults highlight = new SearchResults();
1367 highlight.addResult(sequence, features[0].getBegin(),
1368 features[0].getEnd());
1369 seqCanvas.highlightSearchResults(highlight);
1371 if (features != null && features.length > 0)
1373 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1374 { sequence }, features, false, ap);
1376 seqCanvas.highlightSearchResults(null);
1382 public void mouseWheelMoved(MouseWheelEvent e)
1385 if (e.getWheelRotation() > 0)
1387 if (e.isShiftDown())
1389 ap.scrollRight(true);
1399 if (e.isShiftDown())
1401 ap.scrollRight(false);
1408 // TODO Update tooltip for new position.
1417 public void doMousePressedDefineMode(MouseEvent evt)
1419 int res = findRes(evt);
1420 int seq = findSeq(evt);
1423 startWrapBlock = wrappedBlock;
1425 if (av.wrapAlignment && seq > av.getAlignment().getHeight())
1427 JOptionPane.showInternalMessageDialog(Desktop.desktop,
1428 "Cannot edit annotations in wrapped view.",
1429 "Wrapped view - no edit", JOptionPane.WARNING_MESSAGE);
1433 if (seq < 0 || res < 0)
1438 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1440 if ((sequence == null) || (res > sequence.getLength()))
1445 stretchGroup = av.getSelectionGroup();
1447 if (stretchGroup == null)
1449 stretchGroup = av.getAlignment().findGroup(sequence);
1451 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1452 && (res < stretchGroup.getEndRes()))
1454 av.setSelectionGroup(stretchGroup);
1458 stretchGroup = null;
1461 else if (!stretchGroup.getSequences(null).contains(sequence)
1462 || (stretchGroup.getStartRes() > res)
1463 || (stretchGroup.getEndRes() < res))
1465 stretchGroup = null;
1467 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1469 if (allGroups != null)
1471 for (int i = 0; i < allGroups.length; i++)
1473 if ((allGroups[i].getStartRes() <= res)
1474 && (allGroups[i].getEndRes() >= res))
1476 stretchGroup = allGroups[i];
1482 av.setSelectionGroup(stretchGroup);
1486 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1488 SequenceFeature[] allFeatures = findFeaturesAtRes(
1489 sequence.getDatasetSequence(), sequence.findPosition(res));
1490 Vector links = new Vector();
1491 for (int i = 0; i < allFeatures.length; i++)
1493 if (allFeatures[i].links != null)
1495 for (int j = 0; j < allFeatures[i].links.size(); j++)
1497 links.addElement(allFeatures[i].links.elementAt(j));
1502 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1503 pop.show(this, evt.getX(), evt.getY());
1509 seqCanvas.cursorX = findRes(evt);
1510 seqCanvas.cursorY = findSeq(evt);
1511 seqCanvas.repaint();
1515 if (stretchGroup == null)
1517 // Only if left mouse button do we want to change group sizes
1519 // define a new group here
1520 SequenceGroup sg = new SequenceGroup();
1521 sg.setStartRes(res);
1523 sg.addSequence(sequence, false);
1524 av.setSelectionGroup(sg);
1528 if (av.getConservationSelected())
1530 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1534 if (av.getAbovePIDThreshold())
1536 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1539 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1541 // Edit end res position of selected group
1542 changeEndRes = true;
1544 else if ((stretchGroup != null)
1545 && (stretchGroup.getStartRes() == res))
1547 // Edit end res position of selected group
1548 changeStartRes = true;
1550 stretchGroup.getWidth();
1553 seqCanvas.repaint();
1562 public void doMouseReleasedDefineMode(MouseEvent evt)
1564 if (stretchGroup == null)
1569 stretchGroup.recalcConservation(); // always do this - annotation has own
1571 if (stretchGroup.cs != null)
1573 stretchGroup.cs.alignmentChanged(stretchGroup,
1574 av.getHiddenRepSequences());
1576 if (stretchGroup.cs.conservationApplied())
1578 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1579 stretchGroup.getName());
1583 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1584 stretchGroup.getName());
1587 PaintRefresher.Refresh(this, av.getSequenceSetId());
1588 ap.paintAlignment(true);
1590 changeEndRes = false;
1591 changeStartRes = false;
1592 stretchGroup = null;
1602 public void doMouseDraggedDefineMode(MouseEvent evt)
1604 int res = findRes(evt);
1605 int y = findSeq(evt);
1607 if (wrappedBlock != startWrapBlock)
1612 if (stretchGroup == null)
1617 if (res >= av.getAlignment().getWidth())
1619 res = av.getAlignment().getWidth() - 1;
1622 if (stretchGroup.getEndRes() == res)
1624 // Edit end res position of selected group
1625 changeEndRes = true;
1627 else if (stretchGroup.getStartRes() == res)
1629 // Edit start res position of selected group
1630 changeStartRes = true;
1633 if (res < av.getStartRes())
1635 res = av.getStartRes();
1640 if (res > (stretchGroup.getStartRes() - 1))
1642 stretchGroup.setEndRes(res);
1645 else if (changeStartRes)
1647 if (res < (stretchGroup.getEndRes() + 1))
1649 stretchGroup.setStartRes(res);
1653 int dragDirection = 0;
1659 else if (y < oldSeq)
1664 while ((y != oldSeq) && (oldSeq > -1)
1665 && (y < av.getAlignment().getHeight()))
1667 // This routine ensures we don't skip any sequences, as the
1668 // selection is quite slow.
1669 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1671 oldSeq += dragDirection;
1678 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1680 if (stretchGroup.getSequences(null).contains(nextSeq))
1682 stretchGroup.deleteSequence(seq, false);
1688 stretchGroup.addSequence(seq, false);
1691 stretchGroup.addSequence(nextSeq, false);
1700 mouseDragging = true;
1702 if (scrollThread != null)
1704 scrollThread.setEvent(evt);
1707 seqCanvas.repaint();
1710 void scrollCanvas(MouseEvent evt)
1714 if (scrollThread != null)
1716 scrollThread.running = false;
1717 scrollThread = null;
1719 mouseDragging = false;
1723 if (scrollThread == null)
1725 scrollThread = new ScrollThread();
1728 mouseDragging = true;
1729 scrollThread.setEvent(evt);
1734 // this class allows scrolling off the bottom of the visible alignment
1735 class ScrollThread extends Thread
1739 boolean running = false;
1741 public ScrollThread()
1746 public void setEvent(MouseEvent e)
1751 public void stopScrolling()
1765 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1767 running = ap.scrollUp(true);
1770 if (mouseDragging && (evt.getY() >= getHeight())
1771 && (av.getAlignment().getHeight() > av.getEndSeq()))
1773 running = ap.scrollUp(false);
1776 if (mouseDragging && (evt.getX() < 0))
1778 running = ap.scrollRight(false);
1780 else if (mouseDragging && (evt.getX() >= getWidth()))
1782 running = ap.scrollRight(true);
1789 } catch (Exception ex)
1797 * modify current selection according to a received message.
1800 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1801 SelectionSource source)
1803 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1804 // handles selection messages...
1805 // TODO: extend config options to allow user to control if selections may be
1806 // shared between viewports.
1808 || !av.followSelection
1809 || (av.isSelectionGroupChanged(false) || av
1810 .isColSelChanged(false))
1811 || (source instanceof AlignViewport && ((AlignViewport) source)
1812 .getSequenceSetId().equals(av.getSequenceSetId())))
1816 // do we want to thread this ? (contention with seqsel and colsel locks, I
1818 // rules are: colsel is copied if there is a real intersection between
1819 // sequence selection
1820 boolean repaint = false, copycolsel = true;
1821 // if (!av.isSelectionGroupChanged(false))
1823 SequenceGroup sgroup = null;
1824 if (seqsel != null && seqsel.getSize() > 0)
1826 if (av.getAlignment() == null)
1828 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1829 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1830 + " 's alignment is NULL! returning immediatly.");
1833 sgroup = seqsel.intersect(av.getAlignment(),
1834 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1835 if ((sgroup == null || sgroup.getSize() == 0)
1836 || (colsel == null || colsel.size() == 0))
1838 // don't copy columns if the region didn't intersect.
1842 if (sgroup != null && sgroup.getSize() > 0)
1844 av.setSelectionGroup(sgroup);
1848 av.setSelectionGroup(null);
1850 av.isSelectionGroupChanged(true);
1855 // the current selection is unset or from a previous message
1856 // so import the new colsel.
1857 if (colsel == null || colsel.size() == 0)
1859 if (av.getColumnSelection() != null)
1861 av.getColumnSelection().clear();
1867 // TODO: shift colSel according to the intersecting sequences
1868 if (av.getColumnSelection() == null)
1870 av.setColumnSelection(new ColumnSelection(colsel));
1874 av.getColumnSelection().setElementsFrom(colsel);
1877 av.isColSelChanged(true);
1881 && av.hasHiddenColumns()
1882 && (av.getColumnSelection() == null || av.getColumnSelection()
1883 .getHiddenColumns() == null))
1885 System.err.println("Bad things");
1889 // probably finessing with multiple redraws here
1890 PaintRefresher.Refresh(this, av.getSequenceSetId());
1891 // ap.paintAlignment(false);