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.commands.EditCommand;
24 import jalview.datamodel.ColumnSelection;
25 import jalview.datamodel.SearchResults;
26 import jalview.datamodel.Sequence;
27 import jalview.datamodel.SequenceFeature;
28 import jalview.datamodel.SequenceGroup;
29 import jalview.datamodel.SequenceI;
30 import jalview.io.SequenceAnnotationReport;
31 import jalview.schemes.ResidueProperties;
32 import jalview.structure.SelectionListener;
33 import jalview.structure.SelectionSource;
34 import jalview.structure.SequenceListener;
35 import jalview.structure.StructureSelectionManager;
36 import jalview.util.MessageManager;
38 import java.awt.BorderLayout;
39 import java.awt.Color;
41 import java.awt.FontMetrics;
42 import java.awt.Point;
43 import java.awt.event.MouseEvent;
44 import java.awt.event.MouseListener;
45 import java.awt.event.MouseMotionListener;
46 import java.awt.event.MouseWheelEvent;
47 import java.awt.event.MouseWheelListener;
48 import java.util.List;
49 import java.util.Vector;
51 import javax.swing.JOptionPane;
52 import javax.swing.JPanel;
53 import javax.swing.ToolTipManager;
59 * @version $Revision: 1.130 $
61 public class SeqPanel extends JPanel implements MouseListener,
62 MouseMotionListener, MouseWheelListener, SequenceListener,
67 public SeqCanvas seqCanvas;
70 public AlignmentPanel ap;
72 protected int lastres;
74 protected int startseq;
76 protected AlignViewport av;
78 ScrollThread scrollThread = null;
80 boolean mouseDragging = false;
82 boolean editingSeqs = false;
84 boolean groupEditing = false;
86 // ////////////////////////////////////////
87 // ///Everything below this is for defining the boundary of the rubberband
88 // ////////////////////////////////////////
91 boolean changeEndSeq = false;
93 boolean changeStartSeq = false;
95 boolean changeEndRes = false;
97 boolean changeStartRes = false;
99 SequenceGroup stretchGroup = null;
101 boolean remove = false;
103 Point lastMousePress;
105 boolean mouseWheelPressed = false;
107 StringBuffer keyboardNo1;
109 StringBuffer keyboardNo2;
111 java.net.URL linkImageURL;
113 private final SequenceAnnotationReport seqARep;
115 StringBuffer tooltipText = new StringBuffer("<html>");
119 EditCommand editCommand;
121 StructureSelectionManager ssm;
124 * Creates a new SeqPanel object.
131 public SeqPanel(AlignViewport av, AlignmentPanel ap)
133 linkImageURL = getClass().getResource("/images/link.gif");
134 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
135 ToolTipManager.sharedInstance().registerComponent(this);
136 ToolTipManager.sharedInstance().setInitialDelay(0);
137 ToolTipManager.sharedInstance().setDismissDelay(10000);
139 setBackground(Color.white);
141 seqCanvas = new SeqCanvas(ap);
142 setLayout(new BorderLayout());
143 add(seqCanvas, BorderLayout.CENTER);
149 addMouseMotionListener(this);
150 addMouseListener(this);
151 addMouseWheelListener(this);
152 ssm = av.getStructureSelectionManager();
153 ssm.addStructureViewerListener(this);
154 ssm.addSelectionListener(this);
158 int startWrapBlock = -1;
160 int wrappedBlock = -1;
162 int findRes(MouseEvent evt)
167 if (av.wrapAlignment)
170 int hgap = av.charHeight;
171 if (av.scaleAboveWrapped)
173 hgap += av.charHeight;
176 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
177 + seqCanvas.getAnnotationHeight();
181 x -= seqCanvas.LABEL_WEST;
183 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
189 wrappedBlock = y / cHeight;
190 wrappedBlock += av.getStartRes() / cwidth;
192 res = wrappedBlock * cwidth + x / av.getCharWidth();
197 if (x > seqCanvas.getWidth() + seqCanvas.getWidth())
199 // make sure we calculate relative to visible alignment, rather than
201 x = seqCanvas.getX() + seqCanvas.getWidth();
203 res = (x / av.getCharWidth()) + av.getStartRes();
206 if (av.hasHiddenColumns())
208 res = av.getColumnSelection().adjustForHiddenColumns(res);
215 int findSeq(MouseEvent evt)
220 if (av.wrapAlignment)
222 int hgap = av.charHeight;
223 if (av.scaleAboveWrapped)
225 hgap += av.charHeight;
228 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
229 + seqCanvas.getAnnotationHeight();
233 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
238 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
239 .getAlignment().getHeight() - 1);
245 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
247 Vector tmp = new Vector();
248 SequenceFeature[] features = sequence.getSequenceFeatures();
249 if (features != null)
251 for (int i = 0; i < features.length; i++)
253 if (av.featuresDisplayed == null
254 || !av.featuresDisplayed.containsKey(features[i].getType()))
259 if (features[i].featureGroup != null
260 && seqCanvas.fr.featureGroups != null
261 && seqCanvas.fr.featureGroups
262 .containsKey(features[i].featureGroup)
263 && !((Boolean) seqCanvas.fr.featureGroups
264 .get(features[i].featureGroup)).booleanValue())
269 if ((features[i].getBegin() <= res)
270 && (features[i].getEnd() >= res))
272 tmp.addElement(features[i]);
277 features = new SequenceFeature[tmp.size()];
278 tmp.copyInto(features);
285 if (editCommand != null && editCommand.getSize() > 0)
287 ap.alignFrame.addHistoryItem(editCommand);
288 av.firePropertyChange("alignment", null, av.getAlignment()
295 groupEditing = false;
303 seqCanvas.cursorY = getKeyboardNo1() - 1;
307 void setCursorColumn()
309 seqCanvas.cursorX = getKeyboardNo1() - 1;
313 void setCursorRowAndColumn()
315 if (keyboardNo2 == null)
317 keyboardNo2 = new StringBuffer();
321 seqCanvas.cursorX = getKeyboardNo1() - 1;
322 seqCanvas.cursorY = getKeyboardNo2() - 1;
327 void setCursorPosition()
329 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
331 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
335 void moveCursor(int dx, int dy)
337 seqCanvas.cursorX += dx;
338 seqCanvas.cursorY += dy;
339 if (av.hasHiddenColumns()
340 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
342 int original = seqCanvas.cursorX - dx;
343 int maxWidth = av.getAlignment().getWidth();
345 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
346 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
348 seqCanvas.cursorX += dx;
351 if (seqCanvas.cursorX >= maxWidth
352 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
354 seqCanvas.cursorX = original;
361 void scrollToVisible()
363 if (seqCanvas.cursorX < 0)
365 seqCanvas.cursorX = 0;
367 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
369 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
372 if (seqCanvas.cursorY < 0)
374 seqCanvas.cursorY = 0;
376 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
378 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
382 if (av.wrapAlignment)
384 ap.scrollToWrappedVisible(seqCanvas.cursorX);
388 while (seqCanvas.cursorY < av.startSeq)
392 while (seqCanvas.cursorY + 1 > av.endSeq)
396 if (!av.wrapAlignment)
398 while (seqCanvas.cursorX < av.getColumnSelection()
399 .adjustForHiddenColumns(av.startRes))
401 if (!ap.scrollRight(false))
406 while (seqCanvas.cursorX > av.getColumnSelection()
407 .adjustForHiddenColumns(av.endRes))
409 if (!ap.scrollRight(true))
416 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
417 seqCanvas.cursorX, seqCanvas.cursorY);
422 void setSelectionAreaAtCursor(boolean topLeft)
424 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
426 if (av.getSelectionGroup() != null)
428 SequenceGroup sg = av.getSelectionGroup();
429 // Find the top and bottom of this group
430 int min = av.getAlignment().getHeight(), max = 0;
431 for (int i = 0; i < sg.getSize(); i++)
433 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
448 sg.setStartRes(seqCanvas.cursorX);
449 if (sg.getEndRes() < seqCanvas.cursorX)
451 sg.setEndRes(seqCanvas.cursorX);
454 min = seqCanvas.cursorY;
458 sg.setEndRes(seqCanvas.cursorX);
459 if (sg.getStartRes() > seqCanvas.cursorX)
461 sg.setStartRes(seqCanvas.cursorX);
464 max = seqCanvas.cursorY + 1;
469 // Only the user can do this
470 av.setSelectionGroup(null);
474 // Now add any sequences between min and max
475 sg.getSequences(null).clear();
476 for (int i = min; i < max; i++)
478 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
483 if (av.getSelectionGroup() == null)
485 SequenceGroup sg = new SequenceGroup();
486 sg.setStartRes(seqCanvas.cursorX);
487 sg.setEndRes(seqCanvas.cursorX);
488 sg.addSequence(sequence, false);
489 av.setSelectionGroup(sg);
492 ap.paintAlignment(false);
496 void insertGapAtCursor(boolean group)
498 groupEditing = group;
499 startseq = seqCanvas.cursorY;
500 lastres = seqCanvas.cursorX;
501 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
505 void deleteGapAtCursor(boolean group)
507 groupEditing = group;
508 startseq = seqCanvas.cursorY;
509 lastres = seqCanvas.cursorX + getKeyboardNo1();
510 editSequence(false, false, seqCanvas.cursorX);
514 void insertNucAtCursor(boolean group, String nuc)
516 groupEditing = group;
517 startseq = seqCanvas.cursorY;
518 lastres = seqCanvas.cursorX;
519 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
523 void numberPressed(char value)
525 if (keyboardNo1 == null)
527 keyboardNo1 = new StringBuffer();
530 if (keyboardNo2 != null)
532 keyboardNo2.append(value);
536 keyboardNo1.append(value);
544 if (keyboardNo1 != null)
546 int value = Integer.parseInt(keyboardNo1.toString());
550 } catch (Exception x)
561 if (keyboardNo2 != null)
563 int value = Integer.parseInt(keyboardNo2.toString());
567 } catch (Exception x)
581 public void mouseReleased(MouseEvent evt)
583 mouseDragging = false;
584 mouseWheelPressed = false;
588 doMouseReleasedDefineMode(evt);
602 public void mousePressed(MouseEvent evt)
604 lastMousePress = evt.getPoint();
606 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
608 mouseWheelPressed = true;
612 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
614 if (evt.isAltDown() || evt.isControlDown())
622 doMousePressedDefineMode(evt);
626 int seq = findSeq(evt);
627 int res = findRes(evt);
629 if (seq < 0 || res < 0)
634 if ((seq < av.getAlignment().getHeight())
635 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
652 public void mouseOverSequence(SequenceI sequence, int index, int pos)
654 String tmp = sequence.hashCode() + " " + index + " " + pos;
656 if (lastMessage == null || !lastMessage.equals(tmp))
658 // System.err.println("mouseOver Sequence: "+tmp);
659 ssm.mouseOverSequence(sequence, index, pos, av);
665 public void highlightSequence(SearchResults results)
667 if (av.followHighlight)
669 if (ap.scrollToPosition(results, false))
671 seqCanvas.revalidate();
674 seqCanvas.highlightSearchResults(results);
678 public void updateColours(SequenceI seq, int index)
680 System.out.println("update the seqPanel colours");
691 public void mouseMoved(MouseEvent evt)
695 // This is because MacOSX creates a mouseMoved
696 // If control is down, other platforms will not.
700 int res = findRes(evt);
701 int seq = findSeq(evt);
703 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
708 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
710 if (res >= sequence.getLength())
715 pos = setStatusMessage(sequence, res, seq);
716 if (ssm != null && pos > -1)
718 mouseOverSequence(sequence, res, pos);
721 tooltipText.setLength(6); // Cuts the buffer back to <html>
723 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
726 for (int g = 0; g < groups.length; g++)
728 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
730 if (tooltipText.length() > 6)
732 tooltipText.append("<br>");
735 if (!groups[g].getName().startsWith("JTreeGroup")
736 && !groups[g].getName().startsWith("JGroup"))
738 tooltipText.append(groups[g].getName());
741 if (groups[g].getDescription() != null)
743 tooltipText.append(": " + groups[g].getDescription());
749 // use aa to see if the mouse pointer is on a
750 if (av.showSequenceFeatures)
753 SequenceFeature[] features = findFeaturesAtRes(
754 sequence.getDatasetSequence(),
755 rpos = sequence.findPosition(res));
756 seqARep.appendFeatures(tooltipText, rpos, features,
757 this.ap.seqPanel.seqCanvas.fr.minmax);
759 if (tooltipText.length() == 6) // <html></html>
761 setToolTipText(null);
766 tooltipText.append("</html>");
767 if (lastTooltip == null
768 || !lastTooltip.equals(tooltipText.toString()))
770 setToolTipText(JvSwingUtils.wrapTooltip(true,
771 tooltipText.toString()));
772 lastTooltip = tooltipText.toString();
779 private Point lastp = null;
784 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
786 public Point getToolTipLocation(MouseEvent event)
788 int x = event.getX(), w = getWidth();
789 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
792 if (!event.isShiftDown() || p == null)
794 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
795 event.getX() + wdth, event.getY() - 20) : null;
798 * TODO: try to modify position region is not obcured by tooltip
806 * Set status message in alignment panel
809 * aligned sequence object
813 * index of sequence in alignment
814 * @return position of res in sequence
816 int setStatusMessage(SequenceI sequence, int res, int seq)
819 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
820 + sequence.getName());
823 if (av.getAlignment().isNucleotide())
825 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
829 text.append(" Nucleotide: ");
834 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
837 text.append(" Residue: ");
843 pos = sequence.findPosition(res);
846 text.append(obj + " (" + pos + ")");
849 ap.alignFrame.statusBar.setText(text.toString());
860 public void mouseDragged(MouseEvent evt)
862 if (mouseWheelPressed)
864 int oldWidth = av.charWidth;
866 // Which is bigger, left-right or up-down?
867 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
868 .getX() - lastMousePress.getX()))
870 int fontSize = av.font.getSize();
872 if (evt.getY() < lastMousePress.getY())
876 else if (evt.getY() > lastMousePress.getY())
886 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
887 av.charWidth = oldWidth;
892 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
896 else if (evt.getX() > lastMousePress.getX())
901 ap.paintAlignment(false);
904 FontMetrics fm = getFontMetrics(av.getFont());
905 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
907 lastMousePress = evt.getPoint();
914 doMouseDraggedDefineMode(evt);
918 int res = findRes(evt);
925 if ((lastres == -1) || (lastres == res))
930 if ((res < av.getAlignment().getWidth()) && (res < lastres))
932 // dragLeft, delete gap
933 editSequence(false, false, res);
937 editSequence(true, false, res);
940 mouseDragging = true;
941 if (scrollThread != null)
943 scrollThread.setEvent(evt);
947 // TODO: Make it more clever than many booleans
948 synchronized void editSequence(boolean insertGap, boolean editSeq,
953 boolean fixedColumns = false;
954 SequenceGroup sg = av.getSelectionGroup();
956 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
958 // No group, but the sequence may represent a group
959 if (!groupEditing && av.hasHiddenRows())
961 if (av.isHiddenRepSequence(seq))
963 sg = av.getRepresentedSequences(seq);
968 StringBuffer message = new StringBuffer();
971 message.append("Edit group:");
972 if (editCommand == null)
974 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
979 message.append("Edit sequence: " + seq.getName());
980 String label = seq.getName();
981 if (label.length() > 10)
983 label = label.substring(0, 10);
985 if (editCommand == null)
987 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
993 message.append(" insert ");
997 message.append(" delete ");
1000 message.append(Math.abs(startres - lastres) + " gaps.");
1001 ap.alignFrame.statusBar.setText(message.toString());
1003 // Are we editing within a selection group?
1005 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1008 fixedColumns = true;
1010 // sg might be null as the user may only see 1 sequence,
1011 // but the sequence represents a group
1014 if (!av.isHiddenRepSequence(seq))
1019 sg = av.getRepresentedSequences(seq);
1022 fixedLeft = sg.getStartRes();
1023 fixedRight = sg.getEndRes();
1025 if ((startres < fixedLeft && lastres >= fixedLeft)
1026 || (startres >= fixedLeft && lastres < fixedLeft)
1027 || (startres > fixedRight && lastres <= fixedRight)
1028 || (startres <= fixedRight && lastres > fixedRight))
1034 if (fixedLeft > startres)
1036 fixedRight = fixedLeft - 1;
1039 else if (fixedRight < startres)
1041 fixedLeft = fixedRight;
1046 if (av.hasHiddenColumns())
1048 fixedColumns = true;
1049 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1050 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1052 if ((insertGap && startres > y1 && lastres < y1)
1053 || (!insertGap && startres < y2 && lastres > y2))
1059 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1060 // Selection spans a hidden region
1061 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1069 fixedRight = y2 - 1;
1076 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1077 int g, groupSize = vseqs.size();
1078 SequenceI[] groupSeqs = new SequenceI[groupSize];
1079 for (g = 0; g < groupSeqs.length; g++)
1081 groupSeqs[g] = vseqs.get(g);
1087 // If the user has selected the whole sequence, and is dragging to
1088 // the right, we can still extend the alignment and selectionGroup
1089 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1090 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1092 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1093 fixedRight = sg.getEndRes();
1096 // Is it valid with fixed columns??
1097 // Find the next gap before the end
1098 // of the visible region boundary
1099 boolean blank = false;
1100 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1104 for (g = 0; g < groupSize; g++)
1106 for (int j = 0; j < startres - lastres; j++)
1108 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1109 .getCharAt(fixedRight - j)))
1124 if (sg.getSize() == av.getAlignment().getHeight())
1126 if ((av.hasHiddenColumns() && startres < av
1127 .getColumnSelection().getHiddenBoundaryRight(startres)))
1133 int alWidth = av.getAlignment().getWidth();
1134 if (av.hasHiddenRows())
1136 int hwidth = av.getAlignment().getHiddenSequences()
1138 if (hwidth > alWidth)
1143 // We can still insert gaps if the selectionGroup
1144 // contains all the sequences
1145 sg.setEndRes(sg.getEndRes() + startres - lastres);
1146 fixedRight = alWidth + startres - lastres;
1157 else if (!insertGap)
1159 // / Are we able to delete?
1160 // ie are all columns blank?
1162 for (g = 0; g < groupSize; g++)
1164 for (int j = startres; j < lastres; j++)
1166 if (groupSeqs[g].getLength() <= j)
1171 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1173 // Not a gap, block edit not valid
1183 // dragging to the right
1184 if (fixedColumns && fixedRight != -1)
1186 for (int j = lastres; j < startres; j++)
1188 insertChar(j, groupSeqs, fixedRight);
1193 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1194 startres, startres - lastres, av.getAlignment(), true);
1199 // dragging to the left
1200 if (fixedColumns && fixedRight != -1)
1202 for (int j = lastres; j > startres; j--)
1204 deleteChar(startres, groupSeqs, fixedRight);
1209 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1210 startres, lastres - startres, av.getAlignment(), true);
1216 // ///Editing a single sequence///////////
1220 // dragging to the right
1221 if (fixedColumns && fixedRight != -1)
1223 for (int j = lastres; j < startres; j++)
1225 insertChar(j, new SequenceI[]
1226 { seq }, fixedRight);
1231 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1232 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1239 // dragging to the left
1240 if (fixedColumns && fixedRight != -1)
1242 for (int j = lastres; j > startres; j--)
1244 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1249 deleteChar(startres, new SequenceI[]
1250 { seq }, fixedRight);
1255 // could be a keyboard edit trying to delete none gaps
1257 for (int m = startres; m < lastres; m++)
1259 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1268 editCommand.appendEdit(EditCommand.DELETE_GAP,
1270 { seq }, startres, max, av.getAlignment(), true);
1275 {// insertGap==false AND editSeq==TRUE;
1276 if (fixedColumns && fixedRight != -1)
1278 for (int j = lastres; j < startres; j++)
1280 insertChar(j, new SequenceI[]
1281 { seq }, fixedRight);
1286 editCommand.appendEdit(EditCommand.INSERT_NUC, new SequenceI[]
1287 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1294 seqCanvas.repaint();
1297 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1299 int blankColumn = fixedColumn;
1300 for (int s = 0; s < seq.length; s++)
1302 // Find the next gap before the end of the visible region boundary
1303 // If lastCol > j, theres a boundary after the gap insertion
1305 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1307 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1309 // Theres a space, so break and insert the gap
1314 if (blankColumn <= j)
1316 blankColumn = fixedColumn;
1322 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1323 av.getAlignment(), true);
1325 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1,
1326 av.getAlignment(), true);
1330 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1333 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1,
1334 av.getAlignment(), true);
1336 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1337 av.getAlignment(), true);
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 SequenceFeature[] features = findFeaturesAtRes(
1396 sequence.getDatasetSequence(),
1397 sequence.findPosition(findRes(evt)));
1399 if (features != null && features.length > 0)
1401 SearchResults highlight = new SearchResults();
1402 highlight.addResult(sequence, features[0].getBegin(),
1403 features[0].getEnd());
1404 seqCanvas.highlightSearchResults(highlight);
1406 if (features != null && features.length > 0)
1408 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1409 { sequence }, features, 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.wrapAlignment && 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 SequenceFeature[] allFeatures = findFeaturesAtRes(
1525 sequence.getDatasetSequence(), sequence.findPosition(res));
1526 Vector links = new Vector();
1527 for (int i = 0; i < allFeatures.length; i++)
1529 if (allFeatures[i].links != null)
1531 for (int j = 0; j < allFeatures[i].links.size(); j++)
1533 links.addElement(allFeatures[i].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.
1844 || !av.followSelection
1845 || (av.isSelectionGroupChanged(false) || av
1846 .isColSelChanged(false))
1847 || (source instanceof AlignViewport && ((AlignViewport) source)
1848 .getSequenceSetId().equals(av.getSequenceSetId())))
1852 // do we want to thread this ? (contention with seqsel and colsel locks, I
1854 // rules are: colsel is copied if there is a real intersection between
1855 // sequence selection
1856 boolean repaint = false, copycolsel = true;
1857 // if (!av.isSelectionGroupChanged(false))
1859 SequenceGroup sgroup = null;
1860 if (seqsel != null && seqsel.getSize() > 0)
1862 if (av.getAlignment() == null)
1864 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1865 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1866 + " 's alignment is NULL! returning immediatly.");
1869 sgroup = seqsel.intersect(av.getAlignment(),
1870 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1871 if ((sgroup == null || sgroup.getSize() == 0)
1872 || (colsel == null || colsel.size() == 0))
1874 // don't copy columns if the region didn't intersect.
1878 if (sgroup != null && sgroup.getSize() > 0)
1880 av.setSelectionGroup(sgroup);
1884 av.setSelectionGroup(null);
1886 av.isSelectionGroupChanged(true);
1891 // the current selection is unset or from a previous message
1892 // so import the new colsel.
1893 if (colsel == null || colsel.size() == 0)
1895 if (av.getColumnSelection() != null)
1897 av.getColumnSelection().clear();
1903 // TODO: shift colSel according to the intersecting sequences
1904 if (av.getColumnSelection() == null)
1906 av.setColumnSelection(new ColumnSelection(colsel));
1910 av.getColumnSelection().setElementsFrom(colsel);
1913 av.isColSelChanged(true);
1917 && av.hasHiddenColumns()
1918 && (av.getColumnSelection() == null || av.getColumnSelection()
1919 .getHiddenColumns() == null))
1921 System.err.println("Bad things");
1925 // probably finessing with multiple redraws here
1926 PaintRefresher.Refresh(this, av.getSequenceSetId());
1927 // ap.paintAlignment(false);