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.AlignedCodonFrame;
28 import jalview.datamodel.ColumnSelection;
29 import jalview.datamodel.SearchResults;
30 import jalview.datamodel.Sequence;
31 import jalview.datamodel.SequenceFeature;
32 import jalview.datamodel.SequenceGroup;
33 import jalview.datamodel.SequenceI;
34 import jalview.io.SequenceAnnotationReport;
35 import jalview.schemes.ResidueProperties;
36 import jalview.structure.SelectionListener;
37 import jalview.structure.SelectionSource;
38 import jalview.structure.SequenceListener;
39 import jalview.structure.StructureSelectionManager;
40 import jalview.structure.VamsasSource;
41 import jalview.util.Comparison;
42 import jalview.util.MessageManager;
44 import java.awt.BorderLayout;
45 import java.awt.Color;
47 import java.awt.FontMetrics;
48 import java.awt.Point;
49 import java.awt.event.MouseEvent;
50 import java.awt.event.MouseListener;
51 import java.awt.event.MouseMotionListener;
52 import java.awt.event.MouseWheelEvent;
53 import java.awt.event.MouseWheelListener;
54 import java.util.List;
55 import java.util.Vector;
57 import javax.swing.JOptionPane;
58 import javax.swing.JPanel;
59 import javax.swing.ToolTipManager;
65 * @version $Revision: 1.130 $
67 public class SeqPanel extends JPanel implements MouseListener,
68 MouseMotionListener, MouseWheelListener, SequenceListener,
73 public SeqCanvas seqCanvas;
76 public AlignmentPanel ap;
78 protected int lastres;
80 protected int startseq;
82 protected AlignViewport av;
84 ScrollThread scrollThread = null;
86 boolean mouseDragging = false;
88 boolean editingSeqs = false;
90 boolean groupEditing = false;
92 // ////////////////////////////////////////
93 // ///Everything below this is for defining the boundary of the rubberband
94 // ////////////////////////////////////////
97 boolean changeEndSeq = false;
99 boolean changeStartSeq = false;
101 boolean changeEndRes = false;
103 boolean changeStartRes = false;
105 SequenceGroup stretchGroup = null;
107 boolean remove = false;
109 Point lastMousePress;
111 boolean mouseWheelPressed = false;
113 StringBuffer keyboardNo1;
115 StringBuffer keyboardNo2;
117 java.net.URL linkImageURL;
119 private final SequenceAnnotationReport seqARep;
121 StringBuffer tooltipText = new StringBuffer();
125 EditCommand editCommand;
127 StructureSelectionManager ssm;
130 * Creates a new SeqPanel object.
137 public SeqPanel(AlignViewport av, AlignmentPanel ap)
139 linkImageURL = getClass().getResource("/images/link.gif");
140 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
141 ToolTipManager.sharedInstance().registerComponent(this);
142 ToolTipManager.sharedInstance().setInitialDelay(0);
143 ToolTipManager.sharedInstance().setDismissDelay(10000);
145 setBackground(Color.white);
147 seqCanvas = new SeqCanvas(ap);
148 setLayout(new BorderLayout());
149 add(seqCanvas, BorderLayout.CENTER);
155 addMouseMotionListener(this);
156 addMouseListener(this);
157 addMouseWheelListener(this);
158 ssm = av.getStructureSelectionManager();
159 ssm.addStructureViewerListener(this);
160 ssm.addSelectionListener(this);
164 int startWrapBlock = -1;
166 int wrappedBlock = -1;
168 int findRes(MouseEvent evt)
173 if (av.wrapAlignment)
176 int hgap = av.charHeight;
177 if (av.scaleAboveWrapped)
179 hgap += av.charHeight;
182 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
183 + seqCanvas.getAnnotationHeight();
187 x -= seqCanvas.LABEL_WEST;
189 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
195 wrappedBlock = y / cHeight;
196 wrappedBlock += av.getStartRes() / cwidth;
198 res = wrappedBlock * cwidth + x / av.getCharWidth();
203 if (x > seqCanvas.getWidth() + seqCanvas.getWidth())
205 // make sure we calculate relative to visible alignment, rather than
207 x = seqCanvas.getX() + seqCanvas.getWidth();
209 res = (x / av.getCharWidth()) + av.getStartRes();
212 if (av.hasHiddenColumns())
214 res = av.getColumnSelection().adjustForHiddenColumns(res);
221 int findSeq(MouseEvent evt)
226 if (av.wrapAlignment)
228 int hgap = av.charHeight;
229 if (av.scaleAboveWrapped)
231 hgap += av.charHeight;
234 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
235 + seqCanvas.getAnnotationHeight();
239 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
244 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
245 .getAlignment().getHeight() - 1);
251 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
253 Vector tmp = new Vector();
254 SequenceFeature[] features = sequence.getSequenceFeatures();
255 if (features != null)
257 for (int i = 0; i < features.length; i++)
259 if (av.featuresDisplayed == null
260 || !av.featuresDisplayed.containsKey(features[i].getType()))
265 if (features[i].featureGroup != null
266 && seqCanvas.fr.featureGroups != null
267 && seqCanvas.fr.featureGroups
268 .containsKey(features[i].featureGroup)
269 && !((Boolean) seqCanvas.fr.featureGroups
270 .get(features[i].featureGroup)).booleanValue())
275 if ((features[i].getBegin() <= res)
276 && (features[i].getEnd() >= res))
278 tmp.addElement(features[i]);
283 features = new SequenceFeature[tmp.size()];
284 tmp.copyInto(features);
290 * When all of a sequence of edits are complete, put the resulting edit list
291 * on the history stack (undo list), and reset flags for editing in progress.
297 if (editCommand != null && editCommand.getSize() > 0)
299 ap.alignFrame.addHistoryItem(editCommand);
300 av.firePropertyChange("alignment", null, av.getAlignment()
306 * Tidy up come what may...
311 groupEditing = false;
320 seqCanvas.cursorY = getKeyboardNo1() - 1;
324 void setCursorColumn()
326 seqCanvas.cursorX = getKeyboardNo1() - 1;
330 void setCursorRowAndColumn()
332 if (keyboardNo2 == null)
334 keyboardNo2 = new StringBuffer();
338 seqCanvas.cursorX = getKeyboardNo1() - 1;
339 seqCanvas.cursorY = getKeyboardNo2() - 1;
344 void setCursorPosition()
346 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
348 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
352 void moveCursor(int dx, int dy)
354 seqCanvas.cursorX += dx;
355 seqCanvas.cursorY += dy;
356 if (av.hasHiddenColumns()
357 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
359 int original = seqCanvas.cursorX - dx;
360 int maxWidth = av.getAlignment().getWidth();
362 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
363 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
365 seqCanvas.cursorX += dx;
368 if (seqCanvas.cursorX >= maxWidth
369 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
371 seqCanvas.cursorX = original;
378 void scrollToVisible()
380 if (seqCanvas.cursorX < 0)
382 seqCanvas.cursorX = 0;
384 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
386 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
389 if (seqCanvas.cursorY < 0)
391 seqCanvas.cursorY = 0;
393 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
395 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
399 if (av.wrapAlignment)
401 ap.scrollToWrappedVisible(seqCanvas.cursorX);
405 while (seqCanvas.cursorY < av.startSeq)
409 while (seqCanvas.cursorY + 1 > av.endSeq)
413 if (!av.wrapAlignment)
415 while (seqCanvas.cursorX < av.getColumnSelection()
416 .adjustForHiddenColumns(av.startRes))
418 if (!ap.scrollRight(false))
423 while (seqCanvas.cursorX > av.getColumnSelection()
424 .adjustForHiddenColumns(av.endRes))
426 if (!ap.scrollRight(true))
433 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
434 seqCanvas.cursorX, seqCanvas.cursorY);
439 void setSelectionAreaAtCursor(boolean topLeft)
441 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
443 if (av.getSelectionGroup() != null)
445 SequenceGroup sg = av.getSelectionGroup();
446 // Find the top and bottom of this group
447 int min = av.getAlignment().getHeight(), max = 0;
448 for (int i = 0; i < sg.getSize(); i++)
450 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
465 sg.setStartRes(seqCanvas.cursorX);
466 if (sg.getEndRes() < seqCanvas.cursorX)
468 sg.setEndRes(seqCanvas.cursorX);
471 min = seqCanvas.cursorY;
475 sg.setEndRes(seqCanvas.cursorX);
476 if (sg.getStartRes() > seqCanvas.cursorX)
478 sg.setStartRes(seqCanvas.cursorX);
481 max = seqCanvas.cursorY + 1;
486 // Only the user can do this
487 av.setSelectionGroup(null);
491 // Now add any sequences between min and max
492 sg.getSequences(null).clear();
493 for (int i = min; i < max; i++)
495 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
500 if (av.getSelectionGroup() == null)
502 SequenceGroup sg = new SequenceGroup();
503 sg.setStartRes(seqCanvas.cursorX);
504 sg.setEndRes(seqCanvas.cursorX);
505 sg.addSequence(sequence, false);
506 av.setSelectionGroup(sg);
509 ap.paintAlignment(false);
513 void insertGapAtCursor(boolean group)
515 groupEditing = group;
516 startseq = seqCanvas.cursorY;
517 lastres = seqCanvas.cursorX;
518 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
522 void deleteGapAtCursor(boolean group)
524 groupEditing = group;
525 startseq = seqCanvas.cursorY;
526 lastres = seqCanvas.cursorX + getKeyboardNo1();
527 editSequence(false, false, seqCanvas.cursorX);
531 void insertNucAtCursor(boolean group, String nuc)
533 groupEditing = group;
534 startseq = seqCanvas.cursorY;
535 lastres = seqCanvas.cursorX;
536 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
540 void numberPressed(char value)
542 if (keyboardNo1 == null)
544 keyboardNo1 = new StringBuffer();
547 if (keyboardNo2 != null)
549 keyboardNo2.append(value);
553 keyboardNo1.append(value);
561 if (keyboardNo1 != null)
563 int value = Integer.parseInt(keyboardNo1.toString());
567 } catch (Exception x)
578 if (keyboardNo2 != null)
580 int value = Integer.parseInt(keyboardNo2.toString());
584 } catch (Exception x)
598 public void mouseReleased(MouseEvent evt)
600 mouseDragging = false;
601 mouseWheelPressed = false;
605 doMouseReleasedDefineMode(evt);
619 public void mousePressed(MouseEvent evt)
621 lastMousePress = evt.getPoint();
623 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
625 mouseWheelPressed = true;
629 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
631 if (evt.isAltDown() || evt.isControlDown())
639 doMousePressedDefineMode(evt);
643 int seq = findSeq(evt);
644 int res = findRes(evt);
646 if (seq < 0 || res < 0)
651 if ((seq < av.getAlignment().getHeight())
652 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
668 public void mouseOverSequence(SequenceI sequence, int index, int pos)
670 String tmp = sequence.hashCode() + " " + index + " " + pos;
672 if (lastMessage == null || !lastMessage.equals(tmp))
674 // System.err.println("mouseOver Sequence: "+tmp);
675 ssm.mouseOverSequence(sequence, index, pos, av);
681 public void highlightSequence(SearchResults results)
683 if (av.followHighlight)
685 if (ap.scrollToPosition(results, false))
687 seqCanvas.revalidate();
690 seqCanvas.highlightSearchResults(results);
694 public VamsasSource getVamsasSource()
696 return this.ap == null ? null : this.ap.av;
698 public void updateColours(SequenceI seq, int index)
700 System.out.println("update the seqPanel colours");
711 public void mouseMoved(MouseEvent evt)
715 // This is because MacOSX creates a mouseMoved
716 // If control is down, other platforms will not.
720 int res = findRes(evt);
721 int seq = findSeq(evt);
723 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
728 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
730 if (res >= sequence.getLength())
735 pos = setStatusMessage(sequence, res, seq);
736 if (ssm != null && pos > -1)
738 mouseOverSequence(sequence, res, pos);
741 tooltipText.setLength(6); // Cuts the buffer back to <html>
743 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
746 for (int g = 0; g < groups.length; g++)
748 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
750 if (!groups[g].getName().startsWith("JTreeGroup")
751 && !groups[g].getName().startsWith("JGroup"))
753 tooltipText.append(groups[g].getName());
756 if (groups[g].getDescription() != null)
758 tooltipText.append(": " + groups[g].getDescription());
764 // use aa to see if the mouse pointer is on a
765 if (av.showSequenceFeatures)
768 SequenceFeature[] features = findFeaturesAtRes(
769 sequence.getDatasetSequence(),
770 rpos = sequence.findPosition(res));
771 seqARep.appendFeatures(tooltipText, rpos, features,
772 this.ap.seqPanel.seqCanvas.fr.minmax);
774 if (tooltipText.length() == 6) // <html></html>
776 setToolTipText(null);
781 if (lastTooltip == null
782 || !lastTooltip.equals(tooltipText.toString()))
784 String formatedTooltipText = JvSwingUtils.wrapTooltip(true,
785 tooltipText.toString());
786 // String formatedTooltipText = tooltipText.toString();
787 setToolTipText(formatedTooltipText);
788 lastTooltip = tooltipText.toString();
795 private Point lastp = null;
800 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
802 public Point getToolTipLocation(MouseEvent event)
804 int x = event.getX(), w = getWidth();
805 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
808 if (!event.isShiftDown() || p == null)
810 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
811 event.getX() + wdth, event.getY() - 20) : null;
814 * TODO: try to modify position region is not obcured by tooltip
822 * Set status message in alignment panel
825 * aligned sequence object
829 * index of sequence in alignment
830 * @return position of res in sequence
832 int setStatusMessage(SequenceI sequence, int res, int seq)
835 StringBuilder text = new StringBuilder(32);
836 text.append("Sequence " + (seq + 1) + " ID: " + sequence.getName());
838 String residue = null;
840 * Try to translate the display character to residue name (null for gap).
842 final String displayChar = String.valueOf(sequence.getCharAt(res));
843 if (av.getAlignment().isNucleotide())
845 residue = ResidueProperties.nucleotideName.get(displayChar);
848 text.append(" Nucleotide: ").append(residue);
853 residue = "X".equalsIgnoreCase(displayChar) ? "STOP"
854 : ResidueProperties.aa2Triplet.get(displayChar);
857 text.append(" Residue: ").append(residue);
863 pos = sequence.findPosition(res);
864 text.append(" (").append(Integer.toString(pos)).append(")");
866 ap.alignFrame.statusBar.setText(text.toString());
877 public void mouseDragged(MouseEvent evt)
879 if (mouseWheelPressed)
881 int oldWidth = av.charWidth;
883 // Which is bigger, left-right or up-down?
884 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
885 .getX() - lastMousePress.getX()))
887 int fontSize = av.font.getSize();
889 if (evt.getY() < lastMousePress.getY())
893 else if (evt.getY() > lastMousePress.getY())
903 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
904 av.charWidth = oldWidth;
909 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
913 else if (evt.getX() > lastMousePress.getX())
918 ap.paintAlignment(false);
921 FontMetrics fm = getFontMetrics(av.getFont());
922 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
924 lastMousePress = evt.getPoint();
931 doMouseDraggedDefineMode(evt);
935 int res = findRes(evt);
942 if ((lastres == -1) || (lastres == res))
947 if ((res < av.getAlignment().getWidth()) && (res < lastres))
949 // dragLeft, delete gap
950 editSequence(false, false, res);
954 editSequence(true, false, res);
957 mouseDragging = true;
958 if (scrollThread != null)
960 scrollThread.setEvent(evt);
964 // TODO: Make it more clever than many booleans
965 synchronized void editSequence(boolean insertGap, boolean editSeq,
970 boolean fixedColumns = false;
971 SequenceGroup sg = av.getSelectionGroup();
973 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
975 // No group, but the sequence may represent a group
976 if (!groupEditing && av.hasHiddenRows())
978 if (av.isHiddenRepSequence(seq))
980 sg = av.getRepresentedSequences(seq);
985 StringBuilder message = new StringBuilder(64);
988 message.append("Edit group:");
989 if (editCommand == null)
991 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
996 message.append("Edit sequence: " + seq.getName());
997 String label = seq.getName();
998 if (label.length() > 10)
1000 label = label.substring(0, 10);
1002 if (editCommand == null)
1004 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
1010 message.append(" insert ");
1014 message.append(" delete ");
1017 message.append(Math.abs(startres - lastres) + " gaps.");
1018 ap.alignFrame.statusBar.setText(message.toString());
1020 // Are we editing within a selection group?
1022 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1025 fixedColumns = true;
1027 // sg might be null as the user may only see 1 sequence,
1028 // but the sequence represents a group
1031 if (!av.isHiddenRepSequence(seq))
1036 sg = av.getRepresentedSequences(seq);
1039 fixedLeft = sg.getStartRes();
1040 fixedRight = sg.getEndRes();
1042 if ((startres < fixedLeft && lastres >= fixedLeft)
1043 || (startres >= fixedLeft && lastres < fixedLeft)
1044 || (startres > fixedRight && lastres <= fixedRight)
1045 || (startres <= fixedRight && lastres > fixedRight))
1051 if (fixedLeft > startres)
1053 fixedRight = fixedLeft - 1;
1056 else if (fixedRight < startres)
1058 fixedLeft = fixedRight;
1063 if (av.hasHiddenColumns())
1065 fixedColumns = true;
1066 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1067 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1069 if ((insertGap && startres > y1 && lastres < y1)
1070 || (!insertGap && startres < y2 && lastres > y2))
1076 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1077 // Selection spans a hidden region
1078 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1086 fixedRight = y2 - 1;
1093 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1094 int g, groupSize = vseqs.size();
1095 SequenceI[] groupSeqs = new SequenceI[groupSize];
1096 for (g = 0; g < groupSeqs.length; g++)
1098 groupSeqs[g] = vseqs.get(g);
1104 // If the user has selected the whole sequence, and is dragging to
1105 // the right, we can still extend the alignment and selectionGroup
1106 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1107 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1109 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1110 fixedRight = sg.getEndRes();
1113 // Is it valid with fixed columns??
1114 // Find the next gap before the end
1115 // of the visible region boundary
1116 boolean blank = false;
1117 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1121 for (g = 0; g < groupSize; g++)
1123 for (int j = 0; j < startres - lastres; j++)
1125 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1126 .getCharAt(fixedRight - j)))
1141 if (sg.getSize() == av.getAlignment().getHeight())
1143 if ((av.hasHiddenColumns() && startres < av
1144 .getColumnSelection().getHiddenBoundaryRight(startres)))
1150 int alWidth = av.getAlignment().getWidth();
1151 if (av.hasHiddenRows())
1153 int hwidth = av.getAlignment().getHiddenSequences()
1155 if (hwidth > alWidth)
1160 // We can still insert gaps if the selectionGroup
1161 // contains all the sequences
1162 sg.setEndRes(sg.getEndRes() + startres - lastres);
1163 fixedRight = alWidth + startres - lastres;
1174 else if (!insertGap)
1176 // / Are we able to delete?
1177 // ie are all columns blank?
1179 for (g = 0; g < groupSize; g++)
1181 for (int j = startres; j < lastres; j++)
1183 if (groupSeqs[g].getLength() <= j)
1188 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1190 // Not a gap, block edit not valid
1200 // dragging to the right
1201 if (fixedColumns && fixedRight != -1)
1203 for (int j = lastres; j < startres; j++)
1205 insertChar(j, groupSeqs, fixedRight);
1210 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1216 // dragging to the left
1217 if (fixedColumns && fixedRight != -1)
1219 for (int j = lastres; j > startres; j--)
1221 deleteChar(startres, groupSeqs, fixedRight);
1226 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1233 // ///Editing a single sequence///////////
1237 // dragging to the right
1238 if (fixedColumns && fixedRight != -1)
1240 for (int j = lastres; j < startres; j++)
1242 insertChar(j, new SequenceI[]
1243 { seq }, fixedRight);
1248 appendEdit(Action.INSERT_GAP, new SequenceI[]
1249 { seq }, lastres, startres - lastres);
1256 // dragging to the left
1257 if (fixedColumns && fixedRight != -1)
1259 for (int j = lastres; j > startres; j--)
1261 if (!Comparison.isGap(seq.getCharAt(startres)))
1266 deleteChar(startres, new SequenceI[]
1267 { seq }, fixedRight);
1272 // could be a keyboard edit trying to delete none gaps
1274 for (int m = startres; m < lastres; m++)
1276 if (!Comparison.isGap(seq.getCharAt(m)))
1285 appendEdit(Action.DELETE_GAP, new SequenceI[]
1286 { seq }, startres, max);
1291 {// insertGap==false AND editSeq==TRUE;
1292 if (fixedColumns && fixedRight != -1)
1294 for (int j = lastres; j < startres; j++)
1296 insertChar(j, new SequenceI[]
1297 { seq }, fixedRight);
1302 appendEdit(Action.INSERT_NUC, new SequenceI[]
1303 { seq }, lastres, startres - lastres);
1310 seqCanvas.repaint();
1313 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1315 int blankColumn = fixedColumn;
1316 for (int s = 0; s < seq.length; s++)
1318 // Find the next gap before the end of the visible region boundary
1319 // If lastCol > j, theres a boundary after the gap insertion
1321 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1323 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1325 // Theres a space, so break and insert the gap
1330 if (blankColumn <= j)
1332 blankColumn = fixedColumn;
1338 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1340 appendEdit(Action.INSERT_GAP, seq, j, 1);
1345 * Helper method to add and perform one edit action.
1352 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1356 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1357 av.getAlignment().getGapCharacter());
1359 editCommand.appendEdit(edit, av.getAlignment(),
1363 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1366 appendEdit(Action.DELETE_GAP, seq, j, 1);
1368 appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1);
1378 public void mouseEntered(MouseEvent e)
1385 if (scrollThread != null)
1387 scrollThread.running = false;
1388 scrollThread = null;
1399 public void mouseExited(MouseEvent e)
1401 if (av.getWrapAlignment())
1408 scrollThread = new ScrollThread();
1413 public void mouseClicked(MouseEvent evt)
1415 SequenceGroup sg = null;
1416 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1417 if (evt.getClickCount() > 1)
1419 sg = av.getSelectionGroup();
1420 if (sg != null && sg.getSize() == 1
1421 && sg.getEndRes() - sg.getStartRes() < 2)
1423 av.setSelectionGroup(null);
1426 SequenceFeature[] features = findFeaturesAtRes(
1427 sequence.getDatasetSequence(),
1428 sequence.findPosition(findRes(evt)));
1430 if (features != null && features.length > 0)
1432 SearchResults highlight = new SearchResults();
1433 highlight.addResult(sequence, features[0].getBegin(),
1434 features[0].getEnd());
1435 seqCanvas.highlightSearchResults(highlight);
1437 if (features != null && features.length > 0)
1439 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1440 { sequence }, features, false, ap);
1442 seqCanvas.highlightSearchResults(null);
1448 public void mouseWheelMoved(MouseWheelEvent e)
1451 if (e.getWheelRotation() > 0)
1453 if (e.isShiftDown())
1455 ap.scrollRight(true);
1465 if (e.isShiftDown())
1467 ap.scrollRight(false);
1474 // TODO Update tooltip for new position.
1483 public void doMousePressedDefineMode(MouseEvent evt)
1485 int res = findRes(evt);
1486 int seq = findSeq(evt);
1489 startWrapBlock = wrappedBlock;
1491 if (av.wrapAlignment && seq > av.getAlignment().getHeight())
1493 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1494 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1495 MessageManager.getString("label.wrapped_view_no_edit"),
1496 JOptionPane.WARNING_MESSAGE);
1500 if (seq < 0 || res < 0)
1505 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1507 if ((sequence == null) || (res > sequence.getLength()))
1512 stretchGroup = av.getSelectionGroup();
1514 if (stretchGroup == null)
1516 stretchGroup = av.getAlignment().findGroup(sequence);
1518 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1519 && (res < stretchGroup.getEndRes()))
1521 av.setSelectionGroup(stretchGroup);
1525 stretchGroup = null;
1528 else if (!stretchGroup.getSequences(null).contains(sequence)
1529 || (stretchGroup.getStartRes() > res)
1530 || (stretchGroup.getEndRes() < res))
1532 stretchGroup = null;
1534 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1536 if (allGroups != null)
1538 for (int i = 0; i < allGroups.length; i++)
1540 if ((allGroups[i].getStartRes() <= res)
1541 && (allGroups[i].getEndRes() >= res))
1543 stretchGroup = allGroups[i];
1549 av.setSelectionGroup(stretchGroup);
1553 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1555 SequenceFeature[] allFeatures = findFeaturesAtRes(
1556 sequence.getDatasetSequence(), sequence.findPosition(res));
1557 Vector links = new Vector();
1558 for (int i = 0; i < allFeatures.length; i++)
1560 if (allFeatures[i].links != null)
1562 for (int j = 0; j < allFeatures[i].links.size(); j++)
1564 links.addElement(allFeatures[i].links.elementAt(j));
1569 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1570 pop.show(this, evt.getX(), evt.getY());
1576 seqCanvas.cursorX = findRes(evt);
1577 seqCanvas.cursorY = findSeq(evt);
1578 seqCanvas.repaint();
1582 if (stretchGroup == null)
1584 // Only if left mouse button do we want to change group sizes
1586 // define a new group here
1587 SequenceGroup sg = new SequenceGroup();
1588 sg.setStartRes(res);
1590 sg.addSequence(sequence, false);
1591 av.setSelectionGroup(sg);
1595 if (av.getConservationSelected())
1597 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1601 if (av.getAbovePIDThreshold())
1603 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1606 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1608 // Edit end res position of selected group
1609 changeEndRes = true;
1611 else if ((stretchGroup != null)
1612 && (stretchGroup.getStartRes() == res))
1614 // Edit end res position of selected group
1615 changeStartRes = true;
1617 stretchGroup.getWidth();
1620 seqCanvas.repaint();
1629 public void doMouseReleasedDefineMode(MouseEvent evt)
1631 if (stretchGroup == null)
1636 stretchGroup.recalcConservation(); // always do this - annotation has own
1638 if (stretchGroup.cs != null)
1640 stretchGroup.cs.alignmentChanged(stretchGroup,
1641 av.getHiddenRepSequences());
1643 if (stretchGroup.cs.conservationApplied())
1645 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1646 stretchGroup.getName());
1650 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1651 stretchGroup.getName());
1654 PaintRefresher.Refresh(this, av.getSequenceSetId());
1655 ap.paintAlignment(true);
1657 changeEndRes = false;
1658 changeStartRes = false;
1659 stretchGroup = null;
1669 public void doMouseDraggedDefineMode(MouseEvent evt)
1671 int res = findRes(evt);
1672 int y = findSeq(evt);
1674 if (wrappedBlock != startWrapBlock)
1679 if (stretchGroup == null)
1684 if (res >= av.getAlignment().getWidth())
1686 res = av.getAlignment().getWidth() - 1;
1689 if (stretchGroup.getEndRes() == res)
1691 // Edit end res position of selected group
1692 changeEndRes = true;
1694 else if (stretchGroup.getStartRes() == res)
1696 // Edit start res position of selected group
1697 changeStartRes = true;
1700 if (res < av.getStartRes())
1702 res = av.getStartRes();
1707 if (res > (stretchGroup.getStartRes() - 1))
1709 stretchGroup.setEndRes(res);
1712 else if (changeStartRes)
1714 if (res < (stretchGroup.getEndRes() + 1))
1716 stretchGroup.setStartRes(res);
1720 int dragDirection = 0;
1726 else if (y < oldSeq)
1731 while ((y != oldSeq) && (oldSeq > -1)
1732 && (y < av.getAlignment().getHeight()))
1734 // This routine ensures we don't skip any sequences, as the
1735 // selection is quite slow.
1736 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1738 oldSeq += dragDirection;
1745 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1747 if (stretchGroup.getSequences(null).contains(nextSeq))
1749 stretchGroup.deleteSequence(seq, false);
1755 stretchGroup.addSequence(seq, false);
1758 stretchGroup.addSequence(nextSeq, false);
1767 mouseDragging = true;
1769 if (scrollThread != null)
1771 scrollThread.setEvent(evt);
1774 seqCanvas.repaint();
1777 void scrollCanvas(MouseEvent evt)
1781 if (scrollThread != null)
1783 scrollThread.running = false;
1784 scrollThread = null;
1786 mouseDragging = false;
1790 if (scrollThread == null)
1792 scrollThread = new ScrollThread();
1795 mouseDragging = true;
1796 scrollThread.setEvent(evt);
1801 // this class allows scrolling off the bottom of the visible alignment
1802 class ScrollThread extends Thread
1806 boolean running = false;
1808 public ScrollThread()
1813 public void setEvent(MouseEvent e)
1818 public void stopScrolling()
1832 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1834 running = ap.scrollUp(true);
1837 if (mouseDragging && (evt.getY() >= getHeight())
1838 && (av.getAlignment().getHeight() > av.getEndSeq()))
1840 running = ap.scrollUp(false);
1843 if (mouseDragging && (evt.getX() < 0))
1845 running = ap.scrollRight(false);
1847 else if (mouseDragging && (evt.getX() >= getWidth()))
1849 running = ap.scrollRight(true);
1856 } catch (Exception ex)
1864 * modify current selection according to a received message.
1867 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1868 SelectionSource source)
1870 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1871 // handles selection messages...
1872 // TODO: extend config options to allow user to control if selections may be
1873 // shared between viewports.
1874 boolean iSentTheSelection = (av == source
1875 || (source instanceof AlignViewport && ((AlignViewport) source)
1876 .getSequenceSetId().equals(av.getSequenceSetId())));
1877 if (iSentTheSelection || !av.followSelection)
1883 * Ignore the selection if there is one of our own pending.
1885 if (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1891 * Check for selection in a view of which this one is a 'slave'.
1893 if (selectionAsSlave(seqsel, colsel, source))
1898 // do we want to thread this ? (contention with seqsel and colsel locks, I
1900 // rules are: colsel is copied if there is a real intersection between
1901 // sequence selection
1902 boolean repaint = false;
1903 boolean copycolsel = true;
1905 SequenceGroup sgroup = null;
1906 if (seqsel != null && seqsel.getSize() > 0)
1908 if (av.getAlignment() == null)
1910 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1911 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1912 + " 's alignment is NULL! returning immediately.");
1915 sgroup = seqsel.intersect(av.getAlignment(),
1916 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1917 if ((sgroup == null || sgroup.getSize() == 0)
1918 || (colsel == null || colsel.size() == 0))
1920 // don't copy columns if the region didn't intersect.
1924 if (sgroup != null && sgroup.getSize() > 0)
1926 av.setSelectionGroup(sgroup);
1930 av.setSelectionGroup(null);
1932 av.isSelectionGroupChanged(true);
1937 // the current selection is unset or from a previous message
1938 // so import the new colsel.
1939 if (colsel == null || colsel.size() == 0)
1941 if (av.getColumnSelection() != null)
1943 av.getColumnSelection().clear();
1949 // TODO: shift colSel according to the intersecting sequences
1950 if (av.getColumnSelection() == null)
1952 av.setColumnSelection(new ColumnSelection(colsel));
1956 av.getColumnSelection().setElementsFrom(colsel);
1959 av.isColSelChanged(true);
1964 && av.hasHiddenColumns()
1965 && (av.getColumnSelection() == null || av.getColumnSelection()
1966 .getHiddenColumns() == null))
1968 System.err.println("Bad things");
1970 if (repaint) // always true!
1972 // probably finessing with multiple redraws here
1973 PaintRefresher.Refresh(this, av.getSequenceSetId());
1974 // ap.paintAlignment(false);
1979 * If this panel is a 'slave' of the selection source, tries to map the source
1980 * selection to a local one, and returns true. Else returns false.
1986 protected boolean selectionAsSlave(SequenceGroup seqsel,
1987 ColumnSelection colsel, SelectionSource source)
1989 if (!(source instanceof AlignViewportI)) {
1992 final AlignViewportI sourceAv = (AlignViewportI) source;
1993 if (sourceAv.getSlave() != av)
1999 * Map sequence selection. Note the SequenceGroup holds aligned sequences,
2000 * the mappings hold dataset sequences.
2002 AlignedCodonFrame[] codonFrames = sourceAv.getAlignment()
2004 SequenceGroup sg = new SequenceGroup();
2005 for (SequenceI selected : seqsel.getSequences())
2007 for (AlignedCodonFrame acf : codonFrames)
2009 SequenceI dnaSeq = acf.getDnaForAaSeq(selected);
2012 for (SequenceI seq : av.getAlignment().getSequences())
2014 if (seq.getDatasetSequence() == dnaSeq)
2016 sg.addSequence(seq, false);
2023 av.setSelectionGroup(sg);
2024 av.isSelectionGroupChanged(true);
2025 // ((AlignmentViewport) slave).firePropertyChange("alignment", null, slave
2027 // .getSequences());
2030 * Map column selection
2034 AlignmentPanel ap = av.getAlignPanel();
2035 PaintRefresher.Refresh(this, av.getSequenceSetId());