2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.api.AlignViewportI;
24 import jalview.commands.EditCommand;
25 import jalview.commands.EditCommand.Action;
26 import jalview.commands.EditCommand.Edit;
27 import jalview.datamodel.ColumnSelection;
28 import jalview.datamodel.SearchResults;
29 import jalview.datamodel.Sequence;
30 import jalview.datamodel.SequenceFeature;
31 import jalview.datamodel.SequenceGroup;
32 import jalview.datamodel.SequenceI;
33 import jalview.io.SequenceAnnotationReport;
34 import jalview.schemes.ResidueProperties;
35 import jalview.structure.SelectionListener;
36 import jalview.structure.SelectionSource;
37 import jalview.structure.SequenceListener;
38 import jalview.structure.StructureSelectionManager;
39 import jalview.structure.VamsasSource;
40 import jalview.util.Comparison;
41 import jalview.util.MappingUtils;
42 import jalview.util.MessageManager;
43 import jalview.viewmodel.AlignmentViewport;
45 import java.awt.BorderLayout;
46 import java.awt.Color;
48 import java.awt.FontMetrics;
49 import java.awt.Point;
50 import java.awt.event.MouseEvent;
51 import java.awt.event.MouseListener;
52 import java.awt.event.MouseMotionListener;
53 import java.awt.event.MouseWheelEvent;
54 import java.awt.event.MouseWheelListener;
55 import java.util.List;
56 import java.util.Vector;
58 import javax.swing.JOptionPane;
59 import javax.swing.JPanel;
60 import javax.swing.ToolTipManager;
66 * @version $Revision: 1.130 $
68 public class SeqPanel extends JPanel implements MouseListener,
69 MouseMotionListener, MouseWheelListener, SequenceListener,
74 public SeqCanvas seqCanvas;
77 public AlignmentPanel ap;
79 protected int lastres;
81 protected int startseq;
83 protected AlignViewport av;
85 ScrollThread scrollThread = null;
87 boolean mouseDragging = false;
89 boolean editingSeqs = false;
91 boolean groupEditing = false;
93 // ////////////////////////////////////////
94 // ///Everything below this is for defining the boundary of the rubberband
95 // ////////////////////////////////////////
98 boolean changeEndSeq = false;
100 boolean changeStartSeq = false;
102 boolean changeEndRes = false;
104 boolean changeStartRes = false;
106 SequenceGroup stretchGroup = null;
108 boolean remove = false;
110 Point lastMousePress;
112 boolean mouseWheelPressed = false;
114 StringBuffer keyboardNo1;
116 StringBuffer keyboardNo2;
118 java.net.URL linkImageURL;
120 private final SequenceAnnotationReport seqARep;
122 StringBuffer tooltipText = new StringBuffer();
126 EditCommand editCommand;
128 StructureSelectionManager ssm;
131 * Creates a new SeqPanel object.
138 public SeqPanel(AlignViewport av, AlignmentPanel ap)
140 linkImageURL = getClass().getResource("/images/link.gif");
141 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
142 ToolTipManager.sharedInstance().registerComponent(this);
143 ToolTipManager.sharedInstance().setInitialDelay(0);
144 ToolTipManager.sharedInstance().setDismissDelay(10000);
146 setBackground(Color.white);
148 seqCanvas = new SeqCanvas(ap);
149 setLayout(new BorderLayout());
150 add(seqCanvas, BorderLayout.CENTER);
156 addMouseMotionListener(this);
157 addMouseListener(this);
158 addMouseWheelListener(this);
159 ssm = av.getStructureSelectionManager();
160 ssm.addStructureViewerListener(this);
161 ssm.addSelectionListener(this);
165 int startWrapBlock = -1;
167 int wrappedBlock = -1;
169 int findRes(MouseEvent evt)
174 if (av.getWrapAlignment())
177 int hgap = av.getCharHeight();
178 if (av.getScaleAboveWrapped())
180 hgap += av.getCharHeight();
183 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
185 + seqCanvas.getAnnotationHeight();
189 x -= seqCanvas.LABEL_WEST;
191 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
197 wrappedBlock = y / cHeight;
198 wrappedBlock += av.getStartRes() / cwidth;
200 res = wrappedBlock * cwidth + x / av.getCharWidth();
205 if (x > seqCanvas.getWidth() + seqCanvas.getWidth())
207 // make sure we calculate relative to visible alignment, rather than
209 x = seqCanvas.getX() + seqCanvas.getWidth();
211 res = (x / av.getCharWidth()) + av.getStartRes();
214 if (av.hasHiddenColumns())
216 res = av.getColumnSelection().adjustForHiddenColumns(res);
223 int findSeq(MouseEvent evt)
228 if (av.getWrapAlignment())
230 int hgap = av.getCharHeight();
231 if (av.getScaleAboveWrapped())
233 hgap += av.getCharHeight();
236 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
238 + seqCanvas.getAnnotationHeight();
242 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
247 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
248 .getAlignment().getHeight() - 1);
255 * When all of a sequence of edits are complete, put the resulting edit list
256 * on the history stack (undo list), and reset flags for editing in progress.
262 if (editCommand != null && editCommand.getSize() > 0)
264 ap.alignFrame.addHistoryItem(editCommand);
265 av.firePropertyChange("alignment", null, av.getAlignment()
271 * Tidy up come what may...
276 groupEditing = false;
285 seqCanvas.cursorY = getKeyboardNo1() - 1;
289 void setCursorColumn()
291 seqCanvas.cursorX = getKeyboardNo1() - 1;
295 void setCursorRowAndColumn()
297 if (keyboardNo2 == null)
299 keyboardNo2 = new StringBuffer();
303 seqCanvas.cursorX = getKeyboardNo1() - 1;
304 seqCanvas.cursorY = getKeyboardNo2() - 1;
309 void setCursorPosition()
311 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
313 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
317 void moveCursor(int dx, int dy)
319 seqCanvas.cursorX += dx;
320 seqCanvas.cursorY += dy;
321 if (av.hasHiddenColumns()
322 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
324 int original = seqCanvas.cursorX - dx;
325 int maxWidth = av.getAlignment().getWidth();
327 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
328 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
330 seqCanvas.cursorX += dx;
333 if (seqCanvas.cursorX >= maxWidth
334 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
336 seqCanvas.cursorX = original;
343 void scrollToVisible()
345 if (seqCanvas.cursorX < 0)
347 seqCanvas.cursorX = 0;
349 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
351 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
354 if (seqCanvas.cursorY < 0)
356 seqCanvas.cursorY = 0;
358 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
360 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
364 if (av.getWrapAlignment())
366 ap.scrollToWrappedVisible(seqCanvas.cursorX);
370 while (seqCanvas.cursorY < av.startSeq)
374 while (seqCanvas.cursorY + 1 > av.endSeq)
378 if (!av.getWrapAlignment())
380 while (seqCanvas.cursorX < av.getColumnSelection()
381 .adjustForHiddenColumns(av.startRes))
383 if (!ap.scrollRight(false))
388 while (seqCanvas.cursorX > av.getColumnSelection()
389 .adjustForHiddenColumns(av.endRes))
391 if (!ap.scrollRight(true))
398 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
399 seqCanvas.cursorX, seqCanvas.cursorY);
404 void setSelectionAreaAtCursor(boolean topLeft)
406 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
408 if (av.getSelectionGroup() != null)
410 SequenceGroup sg = av.getSelectionGroup();
411 // Find the top and bottom of this group
412 int min = av.getAlignment().getHeight(), max = 0;
413 for (int i = 0; i < sg.getSize(); i++)
415 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
430 sg.setStartRes(seqCanvas.cursorX);
431 if (sg.getEndRes() < seqCanvas.cursorX)
433 sg.setEndRes(seqCanvas.cursorX);
436 min = seqCanvas.cursorY;
440 sg.setEndRes(seqCanvas.cursorX);
441 if (sg.getStartRes() > seqCanvas.cursorX)
443 sg.setStartRes(seqCanvas.cursorX);
446 max = seqCanvas.cursorY + 1;
451 // Only the user can do this
452 av.setSelectionGroup(null);
456 // Now add any sequences between min and max
457 sg.getSequences(null).clear();
458 for (int i = min; i < max; i++)
460 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
465 if (av.getSelectionGroup() == null)
467 SequenceGroup sg = new SequenceGroup();
468 sg.setStartRes(seqCanvas.cursorX);
469 sg.setEndRes(seqCanvas.cursorX);
470 sg.addSequence(sequence, false);
471 av.setSelectionGroup(sg);
474 ap.paintAlignment(false);
478 void insertGapAtCursor(boolean group)
480 groupEditing = group;
481 startseq = seqCanvas.cursorY;
482 lastres = seqCanvas.cursorX;
483 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
487 void deleteGapAtCursor(boolean group)
489 groupEditing = group;
490 startseq = seqCanvas.cursorY;
491 lastres = seqCanvas.cursorX + getKeyboardNo1();
492 editSequence(false, false, seqCanvas.cursorX);
496 void insertNucAtCursor(boolean group, String nuc)
498 groupEditing = group;
499 startseq = seqCanvas.cursorY;
500 lastres = seqCanvas.cursorX;
501 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
505 void numberPressed(char value)
507 if (keyboardNo1 == null)
509 keyboardNo1 = new StringBuffer();
512 if (keyboardNo2 != null)
514 keyboardNo2.append(value);
518 keyboardNo1.append(value);
526 if (keyboardNo1 != null)
528 int value = Integer.parseInt(keyboardNo1.toString());
532 } catch (Exception x)
543 if (keyboardNo2 != null)
545 int value = Integer.parseInt(keyboardNo2.toString());
549 } catch (Exception x)
563 public void mouseReleased(MouseEvent evt)
565 mouseDragging = false;
566 mouseWheelPressed = false;
570 doMouseReleasedDefineMode(evt);
584 public void mousePressed(MouseEvent evt)
586 lastMousePress = evt.getPoint();
588 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
590 mouseWheelPressed = true;
594 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
596 if (evt.isAltDown() || evt.isControlDown())
604 doMousePressedDefineMode(evt);
608 int seq = findSeq(evt);
609 int res = findRes(evt);
611 if (seq < 0 || res < 0)
616 if ((seq < av.getAlignment().getHeight())
617 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
634 public void mouseOverSequence(SequenceI sequence, int index, int pos)
636 String tmp = sequence.hashCode() + " " + index + " " + pos;
638 if (lastMessage == null || !lastMessage.equals(tmp))
640 // System.err.println("mouseOver Sequence: "+tmp);
641 ssm.mouseOverSequence(sequence, index, pos, av);
647 public void highlightSequence(SearchResults results)
649 if (av.followHighlight)
651 if (ap.scrollToPosition(results, false))
653 seqCanvas.revalidate();
656 seqCanvas.highlightSearchResults(results);
660 public VamsasSource getVamsasSource()
662 return this.ap == null ? null : this.ap.av;
665 public void updateColours(SequenceI seq, int index)
667 System.out.println("update the seqPanel colours");
678 public void mouseMoved(MouseEvent evt)
682 // This is because MacOSX creates a mouseMoved
683 // If control is down, other platforms will not.
687 int res = findRes(evt);
688 int seq = findSeq(evt);
690 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
695 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
697 if (res >= sequence.getLength())
702 pos = setStatusMessage(sequence, res, seq);
703 if (ssm != null && pos > -1)
705 mouseOverSequence(sequence, res, pos);
708 tooltipText.setLength(6); // Cuts the buffer back to <html>
710 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
713 for (int g = 0; g < groups.length; g++)
715 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
717 if (!groups[g].getName().startsWith("JTreeGroup")
718 && !groups[g].getName().startsWith("JGroup"))
720 tooltipText.append(groups[g].getName());
723 if (groups[g].getDescription() != null)
725 tooltipText.append(": " + groups[g].getDescription());
731 // use aa to see if the mouse pointer is on a
732 if (av.isShowSequenceFeatures())
735 List<SequenceFeature> features = ap.getFeatureRenderer().findFeaturesAtRes(
736 sequence.getDatasetSequence(),
737 rpos = sequence.findPosition(res));
738 seqARep.appendFeatures(tooltipText, rpos, features,
739 this.ap.getSeqPanel().seqCanvas.fr.getMinMax());
741 if (tooltipText.length() == 6) // <html></html>
743 setToolTipText(null);
748 if (lastTooltip == null
749 || !lastTooltip.equals(tooltipText.toString()))
751 String formatedTooltipText = JvSwingUtils.wrapTooltip(true,
752 tooltipText.toString());
753 // String formatedTooltipText = tooltipText.toString();
754 setToolTipText(formatedTooltipText);
755 lastTooltip = tooltipText.toString();
762 private Point lastp = null;
767 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
769 public Point getToolTipLocation(MouseEvent event)
771 int x = event.getX(), w = getWidth();
772 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
775 if (!event.isShiftDown() || p == null)
777 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
778 event.getX() + wdth, event.getY() - 20) : null;
781 * TODO: try to modify position region is not obcured by tooltip
789 * Set status message in alignment panel
792 * aligned sequence object
796 * index of sequence in alignment
797 * @return position of res in sequence
799 int setStatusMessage(SequenceI sequence, int res, int seq)
802 StringBuilder text = new StringBuilder(32);
803 text.append("Sequence " + (seq + 1) + " ID: " + sequence.getName());
805 String residue = null;
807 * Try to translate the display character to residue name (null for gap).
809 final String displayChar = String.valueOf(sequence.getCharAt(res));
810 if (av.getAlignment().isNucleotide())
812 residue = ResidueProperties.nucleotideName.get(displayChar);
815 text.append(" Nucleotide: ").append(residue);
820 residue = "X".equalsIgnoreCase(displayChar) ? "STOP"
821 : ResidueProperties.aa2Triplet.get(displayChar);
824 text.append(" Residue: ").append(residue);
830 pos = sequence.findPosition(res);
831 text.append(" (").append(Integer.toString(pos)).append(")");
833 ap.alignFrame.statusBar.setText(text.toString());
844 public void mouseDragged(MouseEvent evt)
846 if (mouseWheelPressed)
848 int oldWidth = av.getCharWidth();
850 // Which is bigger, left-right or up-down?
851 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
852 .getX() - lastMousePress.getX()))
854 int fontSize = av.font.getSize();
856 if (evt.getY() < lastMousePress.getY())
860 else if (evt.getY() > lastMousePress.getY())
871 new Font(av.font.getName(), av.font.getStyle(), fontSize),
873 av.setCharWidth(oldWidth);
878 if (evt.getX() < lastMousePress.getX() && av.getCharWidth() > 1)
880 av.setCharWidth(av.getCharWidth() - 1);
882 else if (evt.getX() > lastMousePress.getX())
884 av.setCharWidth(av.getCharWidth() + 1);
887 ap.paintAlignment(false);
890 FontMetrics fm = getFontMetrics(av.getFont());
891 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
893 lastMousePress = evt.getPoint();
900 doMouseDraggedDefineMode(evt);
904 int res = findRes(evt);
911 if ((lastres == -1) || (lastres == res))
916 if ((res < av.getAlignment().getWidth()) && (res < lastres))
918 // dragLeft, delete gap
919 editSequence(false, false, res);
923 editSequence(true, false, res);
926 mouseDragging = true;
927 if (scrollThread != null)
929 scrollThread.setEvent(evt);
933 // TODO: Make it more clever than many booleans
934 synchronized void editSequence(boolean insertGap, boolean editSeq,
939 boolean fixedColumns = false;
940 SequenceGroup sg = av.getSelectionGroup();
942 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
944 // No group, but the sequence may represent a group
945 if (!groupEditing && av.hasHiddenRows())
947 if (av.isHiddenRepSequence(seq))
949 sg = av.getRepresentedSequences(seq);
954 StringBuilder message = new StringBuilder(64);
957 message.append("Edit group:");
958 if (editCommand == null)
960 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
965 message.append("Edit sequence: " + seq.getName());
966 String label = seq.getName();
967 if (label.length() > 10)
969 label = label.substring(0, 10);
971 if (editCommand == null)
973 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
979 message.append(" insert ");
983 message.append(" delete ");
986 message.append(Math.abs(startres - lastres) + " gaps.");
987 ap.alignFrame.statusBar.setText(message.toString());
989 // Are we editing within a selection group?
991 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
996 // sg might be null as the user may only see 1 sequence,
997 // but the sequence represents a group
1000 if (!av.isHiddenRepSequence(seq))
1005 sg = av.getRepresentedSequences(seq);
1008 fixedLeft = sg.getStartRes();
1009 fixedRight = sg.getEndRes();
1011 if ((startres < fixedLeft && lastres >= fixedLeft)
1012 || (startres >= fixedLeft && lastres < fixedLeft)
1013 || (startres > fixedRight && lastres <= fixedRight)
1014 || (startres <= fixedRight && lastres > fixedRight))
1020 if (fixedLeft > startres)
1022 fixedRight = fixedLeft - 1;
1025 else if (fixedRight < startres)
1027 fixedLeft = fixedRight;
1032 if (av.hasHiddenColumns())
1034 fixedColumns = true;
1035 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1036 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1038 if ((insertGap && startres > y1 && lastres < y1)
1039 || (!insertGap && startres < y2 && lastres > y2))
1045 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1046 // Selection spans a hidden region
1047 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1055 fixedRight = y2 - 1;
1062 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1063 int g, groupSize = vseqs.size();
1064 SequenceI[] groupSeqs = new SequenceI[groupSize];
1065 for (g = 0; g < groupSeqs.length; g++)
1067 groupSeqs[g] = vseqs.get(g);
1073 // If the user has selected the whole sequence, and is dragging to
1074 // the right, we can still extend the alignment and selectionGroup
1075 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1076 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1078 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1079 fixedRight = sg.getEndRes();
1082 // Is it valid with fixed columns??
1083 // Find the next gap before the end
1084 // of the visible region boundary
1085 boolean blank = false;
1086 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1090 for (g = 0; g < groupSize; g++)
1092 for (int j = 0; j < startres - lastres; j++)
1094 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1095 .getCharAt(fixedRight - j)))
1110 if (sg.getSize() == av.getAlignment().getHeight())
1112 if ((av.hasHiddenColumns() && startres < av
1113 .getColumnSelection().getHiddenBoundaryRight(startres)))
1119 int alWidth = av.getAlignment().getWidth();
1120 if (av.hasHiddenRows())
1122 int hwidth = av.getAlignment().getHiddenSequences()
1124 if (hwidth > alWidth)
1129 // We can still insert gaps if the selectionGroup
1130 // contains all the sequences
1131 sg.setEndRes(sg.getEndRes() + startres - lastres);
1132 fixedRight = alWidth + startres - lastres;
1143 else if (!insertGap)
1145 // / Are we able to delete?
1146 // ie are all columns blank?
1148 for (g = 0; g < groupSize; g++)
1150 for (int j = startres; j < lastres; j++)
1152 if (groupSeqs[g].getLength() <= j)
1157 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1159 // Not a gap, block edit not valid
1169 // dragging to the right
1170 if (fixedColumns && fixedRight != -1)
1172 for (int j = lastres; j < startres; j++)
1174 insertChar(j, groupSeqs, fixedRight);
1179 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1185 // dragging to the left
1186 if (fixedColumns && fixedRight != -1)
1188 for (int j = lastres; j > startres; j--)
1190 deleteChar(startres, groupSeqs, fixedRight);
1195 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1202 // ///Editing a single sequence///////////
1206 // dragging to the right
1207 if (fixedColumns && fixedRight != -1)
1209 for (int j = lastres; j < startres; j++)
1211 insertChar(j, new SequenceI[]
1212 { seq }, fixedRight);
1217 appendEdit(Action.INSERT_GAP, new SequenceI[]
1218 { seq }, lastres, startres - lastres);
1225 // dragging to the left
1226 if (fixedColumns && fixedRight != -1)
1228 for (int j = lastres; j > startres; j--)
1230 if (!Comparison.isGap(seq.getCharAt(startres)))
1235 deleteChar(startres, new SequenceI[]
1236 { seq }, fixedRight);
1241 // could be a keyboard edit trying to delete none gaps
1243 for (int m = startres; m < lastres; m++)
1245 if (!Comparison.isGap(seq.getCharAt(m)))
1254 appendEdit(Action.DELETE_GAP, new SequenceI[]
1255 { seq }, startres, max);
1260 {// insertGap==false AND editSeq==TRUE;
1261 if (fixedColumns && fixedRight != -1)
1263 for (int j = lastres; j < startres; j++)
1265 insertChar(j, new SequenceI[]
1266 { seq }, fixedRight);
1271 appendEdit(Action.INSERT_NUC, new SequenceI[]
1272 { seq }, lastres, startres - lastres);
1279 seqCanvas.repaint();
1282 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1284 int blankColumn = fixedColumn;
1285 for (int s = 0; s < seq.length; s++)
1287 // Find the next gap before the end of the visible region boundary
1288 // If lastCol > j, theres a boundary after the gap insertion
1290 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1292 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1294 // Theres a space, so break and insert the gap
1299 if (blankColumn <= j)
1301 blankColumn = fixedColumn;
1307 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1309 appendEdit(Action.INSERT_GAP, seq, j, 1);
1314 * Helper method to add and perform one edit action.
1321 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1325 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1326 av.getAlignment().getGapCharacter());
1328 editCommand.appendEdit(edit, av.getAlignment(),
1332 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1335 appendEdit(Action.DELETE_GAP, seq, j, 1);
1337 appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1);
1347 public void mouseEntered(MouseEvent e)
1354 if (scrollThread != null)
1356 scrollThread.running = false;
1357 scrollThread = null;
1368 public void mouseExited(MouseEvent e)
1370 if (av.getWrapAlignment())
1377 scrollThread = new ScrollThread();
1382 public void mouseClicked(MouseEvent evt)
1384 SequenceGroup sg = null;
1385 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1386 if (evt.getClickCount() > 1)
1388 sg = av.getSelectionGroup();
1389 if (sg != null && sg.getSize() == 1
1390 && sg.getEndRes() - sg.getStartRes() < 2)
1392 av.setSelectionGroup(null);
1395 List<SequenceFeature> features = seqCanvas.getFeatureRenderer().findFeaturesAtRes(
1396 sequence.getDatasetSequence(),
1397 sequence.findPosition(findRes(evt)));
1399 if (features != null && features.size()> 0)
1401 SearchResults highlight = new SearchResults();
1402 highlight.addResult(sequence, features.get(0).getBegin(),
1403 features.get(0).getEnd());
1404 seqCanvas.highlightSearchResults(highlight);
1406 if (features != null && features.size()> 0)
1408 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1409 { sequence }, features.toArray(new SequenceFeature[features.size()]), false, ap);
1411 seqCanvas.highlightSearchResults(null);
1417 public void mouseWheelMoved(MouseWheelEvent e)
1420 if (e.getWheelRotation() > 0)
1422 if (e.isShiftDown())
1424 ap.scrollRight(true);
1434 if (e.isShiftDown())
1436 ap.scrollRight(false);
1443 // TODO Update tooltip for new position.
1452 public void doMousePressedDefineMode(MouseEvent evt)
1454 int res = findRes(evt);
1455 int seq = findSeq(evt);
1458 startWrapBlock = wrappedBlock;
1460 if (av.getWrapAlignment() && seq > av.getAlignment().getHeight())
1462 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1463 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1464 MessageManager.getString("label.wrapped_view_no_edit"),
1465 JOptionPane.WARNING_MESSAGE);
1469 if (seq < 0 || res < 0)
1474 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1476 if ((sequence == null) || (res > sequence.getLength()))
1481 stretchGroup = av.getSelectionGroup();
1483 if (stretchGroup == null)
1485 stretchGroup = av.getAlignment().findGroup(sequence);
1487 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1488 && (res < stretchGroup.getEndRes()))
1490 av.setSelectionGroup(stretchGroup);
1494 stretchGroup = null;
1497 else if (!stretchGroup.getSequences(null).contains(sequence)
1498 || (stretchGroup.getStartRes() > res)
1499 || (stretchGroup.getEndRes() < res))
1501 stretchGroup = null;
1503 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1505 if (allGroups != null)
1507 for (int i = 0; i < allGroups.length; i++)
1509 if ((allGroups[i].getStartRes() <= res)
1510 && (allGroups[i].getEndRes() >= res))
1512 stretchGroup = allGroups[i];
1518 av.setSelectionGroup(stretchGroup);
1522 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1524 List<SequenceFeature> allFeatures = ap.getFeatureRenderer().findFeaturesAtRes(
1525 sequence.getDatasetSequence(), sequence.findPosition(res));
1526 Vector links = new Vector();
1527 for (SequenceFeature sf:allFeatures)
1529 if (sf.links != null)
1531 for (int j = 0; j < sf.links.size(); j++)
1533 links.addElement(sf.links.elementAt(j));
1538 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1539 pop.show(this, evt.getX(), evt.getY());
1545 seqCanvas.cursorX = findRes(evt);
1546 seqCanvas.cursorY = findSeq(evt);
1547 seqCanvas.repaint();
1551 if (stretchGroup == null)
1553 // Only if left mouse button do we want to change group sizes
1555 // define a new group here
1556 SequenceGroup sg = new SequenceGroup();
1557 sg.setStartRes(res);
1559 sg.addSequence(sequence, false);
1560 av.setSelectionGroup(sg);
1564 if (av.getConservationSelected())
1566 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1570 if (av.getAbovePIDThreshold())
1572 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1575 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1577 // Edit end res position of selected group
1578 changeEndRes = true;
1580 else if ((stretchGroup != null)
1581 && (stretchGroup.getStartRes() == res))
1583 // Edit end res position of selected group
1584 changeStartRes = true;
1586 stretchGroup.getWidth();
1589 seqCanvas.repaint();
1598 public void doMouseReleasedDefineMode(MouseEvent evt)
1600 if (stretchGroup == null)
1605 stretchGroup.recalcConservation(); // always do this - annotation has own
1607 if (stretchGroup.cs != null)
1609 stretchGroup.cs.alignmentChanged(stretchGroup,
1610 av.getHiddenRepSequences());
1612 if (stretchGroup.cs.conservationApplied())
1614 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1615 stretchGroup.getName());
1619 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1620 stretchGroup.getName());
1623 PaintRefresher.Refresh(this, av.getSequenceSetId());
1624 ap.paintAlignment(true);
1626 changeEndRes = false;
1627 changeStartRes = false;
1628 stretchGroup = null;
1638 public void doMouseDraggedDefineMode(MouseEvent evt)
1640 int res = findRes(evt);
1641 int y = findSeq(evt);
1643 if (wrappedBlock != startWrapBlock)
1648 if (stretchGroup == null)
1653 if (res >= av.getAlignment().getWidth())
1655 res = av.getAlignment().getWidth() - 1;
1658 if (stretchGroup.getEndRes() == res)
1660 // Edit end res position of selected group
1661 changeEndRes = true;
1663 else if (stretchGroup.getStartRes() == res)
1665 // Edit start res position of selected group
1666 changeStartRes = true;
1669 if (res < av.getStartRes())
1671 res = av.getStartRes();
1676 if (res > (stretchGroup.getStartRes() - 1))
1678 stretchGroup.setEndRes(res);
1681 else if (changeStartRes)
1683 if (res < (stretchGroup.getEndRes() + 1))
1685 stretchGroup.setStartRes(res);
1689 int dragDirection = 0;
1695 else if (y < oldSeq)
1700 while ((y != oldSeq) && (oldSeq > -1)
1701 && (y < av.getAlignment().getHeight()))
1703 // This routine ensures we don't skip any sequences, as the
1704 // selection is quite slow.
1705 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1707 oldSeq += dragDirection;
1714 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1716 if (stretchGroup.getSequences(null).contains(nextSeq))
1718 stretchGroup.deleteSequence(seq, false);
1724 stretchGroup.addSequence(seq, false);
1727 stretchGroup.addSequence(nextSeq, false);
1736 mouseDragging = true;
1738 if (scrollThread != null)
1740 scrollThread.setEvent(evt);
1743 seqCanvas.repaint();
1746 void scrollCanvas(MouseEvent evt)
1750 if (scrollThread != null)
1752 scrollThread.running = false;
1753 scrollThread = null;
1755 mouseDragging = false;
1759 if (scrollThread == null)
1761 scrollThread = new ScrollThread();
1764 mouseDragging = true;
1765 scrollThread.setEvent(evt);
1770 // this class allows scrolling off the bottom of the visible alignment
1771 class ScrollThread extends Thread
1775 boolean running = false;
1777 public ScrollThread()
1782 public void setEvent(MouseEvent e)
1787 public void stopScrolling()
1801 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1803 running = ap.scrollUp(true);
1806 if (mouseDragging && (evt.getY() >= getHeight())
1807 && (av.getAlignment().getHeight() > av.getEndSeq()))
1809 running = ap.scrollUp(false);
1812 if (mouseDragging && (evt.getX() < 0))
1814 running = ap.scrollRight(false);
1816 else if (mouseDragging && (evt.getX() >= getWidth()))
1818 running = ap.scrollRight(true);
1825 } catch (Exception ex)
1833 * modify current selection according to a received message.
1836 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1837 SelectionSource source)
1839 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1840 // handles selection messages...
1841 // TODO: extend config options to allow user to control if selections may be
1842 // shared between viewports.
1843 boolean iSentTheSelection = (av == source
1844 || (source instanceof AlignViewport && ((AlignmentViewport) source)
1845 .getSequenceSetId().equals(av.getSequenceSetId())));
1846 if (iSentTheSelection || !av.followSelection)
1852 * Ignore the selection if there is one of our own pending.
1854 if (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1860 * Check for selection in a view of which this one is a dna/protein
1863 if (selectionFromTranslation(seqsel, colsel, source))
1868 // do we want to thread this ? (contention with seqsel and colsel locks, I
1870 // rules are: colsel is copied if there is a real intersection between
1871 // sequence selection
1872 boolean repaint = false;
1873 boolean copycolsel = true;
1875 SequenceGroup sgroup = null;
1876 if (seqsel != null && seqsel.getSize() > 0)
1878 if (av.getAlignment() == null)
1880 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1881 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1882 + " 's alignment is NULL! returning immediately.");
1885 sgroup = seqsel.intersect(av.getAlignment(),
1886 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1887 if ((sgroup == null || sgroup.getSize() == 0)
1888 || (colsel == null || colsel.size() == 0))
1890 // don't copy columns if the region didn't intersect.
1894 if (sgroup != null && sgroup.getSize() > 0)
1896 av.setSelectionGroup(sgroup);
1900 av.setSelectionGroup(null);
1902 av.isSelectionGroupChanged(true);
1907 // the current selection is unset or from a previous message
1908 // so import the new colsel.
1909 if (colsel == null || colsel.size() == 0)
1911 if (av.getColumnSelection() != null)
1913 av.getColumnSelection().clear();
1919 // TODO: shift colSel according to the intersecting sequences
1920 if (av.getColumnSelection() == null)
1922 av.setColumnSelection(new ColumnSelection(colsel));
1926 av.getColumnSelection().setElementsFrom(colsel);
1929 av.isColSelChanged(true);
1934 && av.hasHiddenColumns()
1935 && (av.getColumnSelection() == null || av.getColumnSelection()
1936 .getHiddenColumns() == null))
1938 System.err.println("Bad things");
1940 if (repaint) // always true!
1942 // probably finessing with multiple redraws here
1943 PaintRefresher.Refresh(this, av.getSequenceSetId());
1944 // ap.paintAlignment(false);
1949 * If this panel is a cdna/protein translation view of the selection source,
1950 * tries to map the source selection to a local one, and returns true. Else
1957 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1958 ColumnSelection colsel, SelectionSource source)
1960 if (!(source instanceof AlignViewportI)) {
1963 final AlignViewportI sourceAv = (AlignViewportI) source;
1964 if (sourceAv.getCodingComplement() != av && av.getCodingComplement() != sourceAv)
1970 * Map sequence selection
1972 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1973 av.setSelectionGroup(sg);
1974 av.isSelectionGroupChanged(true);
1977 * Map column selection
1979 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1981 av.setColumnSelection(cs);
1982 av.isColSelChanged(true);
1984 PaintRefresher.Refresh(this, av.getSequenceSetId());