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.commands.EditCommand.Action;
25 import jalview.datamodel.ColumnSelection;
26 import jalview.datamodel.SearchResults;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceFeature;
29 import jalview.datamodel.SequenceGroup;
30 import jalview.datamodel.SequenceI;
31 import jalview.io.SequenceAnnotationReport;
32 import jalview.schemes.ResidueProperties;
33 import jalview.structure.SelectionListener;
34 import jalview.structure.SelectionSource;
35 import jalview.structure.SequenceListener;
36 import jalview.structure.StructureSelectionManager;
37 import jalview.util.MessageManager;
39 import java.awt.BorderLayout;
40 import java.awt.Color;
42 import java.awt.FontMetrics;
43 import java.awt.Point;
44 import java.awt.event.MouseEvent;
45 import java.awt.event.MouseListener;
46 import java.awt.event.MouseMotionListener;
47 import java.awt.event.MouseWheelEvent;
48 import java.awt.event.MouseWheelListener;
49 import java.util.List;
50 import java.util.Vector;
52 import javax.swing.JOptionPane;
53 import javax.swing.JPanel;
54 import javax.swing.ToolTipManager;
60 * @version $Revision: 1.130 $
62 public class SeqPanel extends JPanel implements MouseListener,
63 MouseMotionListener, MouseWheelListener, SequenceListener,
68 public SeqCanvas seqCanvas;
71 public AlignmentPanel ap;
73 protected int lastres;
75 protected int startseq;
77 protected AlignViewport av;
79 ScrollThread scrollThread = null;
81 boolean mouseDragging = false;
83 boolean editingSeqs = false;
85 boolean groupEditing = false;
87 // ////////////////////////////////////////
88 // ///Everything below this is for defining the boundary of the rubberband
89 // ////////////////////////////////////////
92 boolean changeEndSeq = false;
94 boolean changeStartSeq = false;
96 boolean changeEndRes = false;
98 boolean changeStartRes = false;
100 SequenceGroup stretchGroup = null;
102 boolean remove = false;
104 Point lastMousePress;
106 boolean mouseWheelPressed = false;
108 StringBuffer keyboardNo1;
110 StringBuffer keyboardNo2;
112 java.net.URL linkImageURL;
114 private final SequenceAnnotationReport seqARep;
116 StringBuffer tooltipText = new StringBuffer();
120 EditCommand editCommand;
122 StructureSelectionManager ssm;
125 * Creates a new SeqPanel object.
132 public SeqPanel(AlignViewport av, AlignmentPanel ap)
134 linkImageURL = getClass().getResource("/images/link.gif");
135 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
136 ToolTipManager.sharedInstance().registerComponent(this);
137 ToolTipManager.sharedInstance().setInitialDelay(0);
138 ToolTipManager.sharedInstance().setDismissDelay(10000);
140 setBackground(Color.white);
142 seqCanvas = new SeqCanvas(ap);
143 setLayout(new BorderLayout());
144 add(seqCanvas, BorderLayout.CENTER);
150 addMouseMotionListener(this);
151 addMouseListener(this);
152 addMouseWheelListener(this);
153 ssm = av.getStructureSelectionManager();
154 ssm.addStructureViewerListener(this);
155 ssm.addSelectionListener(this);
159 int startWrapBlock = -1;
161 int wrappedBlock = -1;
163 int findRes(MouseEvent evt)
168 if (av.wrapAlignment)
171 int hgap = av.charHeight;
172 if (av.scaleAboveWrapped)
174 hgap += av.charHeight;
177 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
178 + seqCanvas.getAnnotationHeight();
182 x -= seqCanvas.LABEL_WEST;
184 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
190 wrappedBlock = y / cHeight;
191 wrappedBlock += av.getStartRes() / cwidth;
193 res = wrappedBlock * cwidth + x / av.getCharWidth();
198 if (x > seqCanvas.getWidth() + seqCanvas.getWidth())
200 // make sure we calculate relative to visible alignment, rather than
202 x = seqCanvas.getX() + seqCanvas.getWidth();
204 res = (x / av.getCharWidth()) + av.getStartRes();
207 if (av.hasHiddenColumns())
209 res = av.getColumnSelection().adjustForHiddenColumns(res);
216 int findSeq(MouseEvent evt)
221 if (av.wrapAlignment)
223 int hgap = av.charHeight;
224 if (av.scaleAboveWrapped)
226 hgap += av.charHeight;
229 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
230 + seqCanvas.getAnnotationHeight();
234 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
239 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
240 .getAlignment().getHeight() - 1);
248 if (editCommand != null && editCommand.getSize() > 0)
250 ap.alignFrame.addHistoryItem(editCommand);
251 av.firePropertyChange("alignment", null, av.getAlignment()
258 groupEditing = false;
266 seqCanvas.cursorY = getKeyboardNo1() - 1;
270 void setCursorColumn()
272 seqCanvas.cursorX = getKeyboardNo1() - 1;
276 void setCursorRowAndColumn()
278 if (keyboardNo2 == null)
280 keyboardNo2 = new StringBuffer();
284 seqCanvas.cursorX = getKeyboardNo1() - 1;
285 seqCanvas.cursorY = getKeyboardNo2() - 1;
290 void setCursorPosition()
292 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
294 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
298 void moveCursor(int dx, int dy)
300 seqCanvas.cursorX += dx;
301 seqCanvas.cursorY += dy;
302 if (av.hasHiddenColumns()
303 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
305 int original = seqCanvas.cursorX - dx;
306 int maxWidth = av.getAlignment().getWidth();
308 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
309 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
311 seqCanvas.cursorX += dx;
314 if (seqCanvas.cursorX >= maxWidth
315 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
317 seqCanvas.cursorX = original;
324 void scrollToVisible()
326 if (seqCanvas.cursorX < 0)
328 seqCanvas.cursorX = 0;
330 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
332 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
335 if (seqCanvas.cursorY < 0)
337 seqCanvas.cursorY = 0;
339 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
341 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
345 if (av.wrapAlignment)
347 ap.scrollToWrappedVisible(seqCanvas.cursorX);
351 while (seqCanvas.cursorY < av.startSeq)
355 while (seqCanvas.cursorY + 1 > av.endSeq)
359 if (!av.wrapAlignment)
361 while (seqCanvas.cursorX < av.getColumnSelection()
362 .adjustForHiddenColumns(av.startRes))
364 if (!ap.scrollRight(false))
369 while (seqCanvas.cursorX > av.getColumnSelection()
370 .adjustForHiddenColumns(av.endRes))
372 if (!ap.scrollRight(true))
379 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
380 seqCanvas.cursorX, seqCanvas.cursorY);
385 void setSelectionAreaAtCursor(boolean topLeft)
387 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
389 if (av.getSelectionGroup() != null)
391 SequenceGroup sg = av.getSelectionGroup();
392 // Find the top and bottom of this group
393 int min = av.getAlignment().getHeight(), max = 0;
394 for (int i = 0; i < sg.getSize(); i++)
396 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
411 sg.setStartRes(seqCanvas.cursorX);
412 if (sg.getEndRes() < seqCanvas.cursorX)
414 sg.setEndRes(seqCanvas.cursorX);
417 min = seqCanvas.cursorY;
421 sg.setEndRes(seqCanvas.cursorX);
422 if (sg.getStartRes() > seqCanvas.cursorX)
424 sg.setStartRes(seqCanvas.cursorX);
427 max = seqCanvas.cursorY + 1;
432 // Only the user can do this
433 av.setSelectionGroup(null);
437 // Now add any sequences between min and max
438 sg.getSequences(null).clear();
439 for (int i = min; i < max; i++)
441 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
446 if (av.getSelectionGroup() == null)
448 SequenceGroup sg = new SequenceGroup();
449 sg.setStartRes(seqCanvas.cursorX);
450 sg.setEndRes(seqCanvas.cursorX);
451 sg.addSequence(sequence, false);
452 av.setSelectionGroup(sg);
455 ap.paintAlignment(false);
459 void insertGapAtCursor(boolean group)
461 groupEditing = group;
462 startseq = seqCanvas.cursorY;
463 lastres = seqCanvas.cursorX;
464 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
468 void deleteGapAtCursor(boolean group)
470 groupEditing = group;
471 startseq = seqCanvas.cursorY;
472 lastres = seqCanvas.cursorX + getKeyboardNo1();
473 editSequence(false, false, seqCanvas.cursorX);
477 void insertNucAtCursor(boolean group, String nuc)
479 groupEditing = group;
480 startseq = seqCanvas.cursorY;
481 lastres = seqCanvas.cursorX;
482 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
486 void numberPressed(char value)
488 if (keyboardNo1 == null)
490 keyboardNo1 = new StringBuffer();
493 if (keyboardNo2 != null)
495 keyboardNo2.append(value);
499 keyboardNo1.append(value);
507 if (keyboardNo1 != null)
509 int value = Integer.parseInt(keyboardNo1.toString());
513 } catch (Exception x)
524 if (keyboardNo2 != null)
526 int value = Integer.parseInt(keyboardNo2.toString());
530 } catch (Exception x)
544 public void mouseReleased(MouseEvent evt)
546 mouseDragging = false;
547 mouseWheelPressed = false;
551 doMouseReleasedDefineMode(evt);
565 public void mousePressed(MouseEvent evt)
567 lastMousePress = evt.getPoint();
569 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
571 mouseWheelPressed = true;
575 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
577 if (evt.isAltDown() || evt.isControlDown())
585 doMousePressedDefineMode(evt);
589 int seq = findSeq(evt);
590 int res = findRes(evt);
592 if (seq < 0 || res < 0)
597 if ((seq < av.getAlignment().getHeight())
598 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
615 public void mouseOverSequence(SequenceI sequence, int index, int pos)
617 String tmp = sequence.hashCode() + " " + index + " " + pos;
619 if (lastMessage == null || !lastMessage.equals(tmp))
621 // System.err.println("mouseOver Sequence: "+tmp);
622 ssm.mouseOverSequence(sequence, index, pos, av);
628 public void highlightSequence(SearchResults results)
630 if (av.followHighlight)
632 if (ap.scrollToPosition(results, false))
634 seqCanvas.revalidate();
637 seqCanvas.highlightSearchResults(results);
641 public void updateColours(SequenceI seq, int index)
643 System.out.println("update the seqPanel colours");
654 public void mouseMoved(MouseEvent evt)
658 // This is because MacOSX creates a mouseMoved
659 // If control is down, other platforms will not.
663 int res = findRes(evt);
664 int seq = findSeq(evt);
666 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
671 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
673 if (res >= sequence.getLength())
678 pos = setStatusMessage(sequence, res, seq);
679 if (ssm != null && pos > -1)
681 mouseOverSequence(sequence, res, pos);
684 tooltipText.setLength(6); // Cuts the buffer back to <html>
686 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
689 for (int g = 0; g < groups.length; g++)
691 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
693 if (!groups[g].getName().startsWith("JTreeGroup")
694 && !groups[g].getName().startsWith("JGroup"))
696 tooltipText.append(groups[g].getName());
699 if (groups[g].getDescription() != null)
701 tooltipText.append(": " + groups[g].getDescription());
707 // use aa to see if the mouse pointer is on a
708 if (av.isShowSequenceFeatures())
711 List<SequenceFeature> features = ap.getFeatureRenderer().findFeaturesAtRes(
712 sequence.getDatasetSequence(),
713 rpos = sequence.findPosition(res));
714 seqARep.appendFeatures(tooltipText, rpos, features,
715 this.ap.getSeqPanel().seqCanvas.fr.getMinMax());
717 if (tooltipText.length() == 6) // <html></html>
719 setToolTipText(null);
724 if (lastTooltip == null
725 || !lastTooltip.equals(tooltipText.toString()))
727 String formatedTooltipText = JvSwingUtils.wrapTooltip(true,
728 tooltipText.toString());
729 // String formatedTooltipText = tooltipText.toString();
730 setToolTipText(formatedTooltipText);
731 lastTooltip = tooltipText.toString();
738 private Point lastp = null;
743 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
745 public Point getToolTipLocation(MouseEvent event)
747 int x = event.getX(), w = getWidth();
748 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
751 if (!event.isShiftDown() || p == null)
753 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
754 event.getX() + wdth, event.getY() - 20) : null;
757 * TODO: try to modify position region is not obcured by tooltip
765 * Set status message in alignment panel
768 * aligned sequence object
772 * index of sequence in alignment
773 * @return position of res in sequence
775 int setStatusMessage(SequenceI sequence, int res, int seq)
778 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
779 + sequence.getName());
782 if (av.getAlignment().isNucleotide())
784 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
788 text.append(" Nucleotide: ");
793 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
796 text.append(" Residue: ");
802 pos = sequence.findPosition(res);
805 text.append(obj + " (" + pos + ")");
808 ap.alignFrame.statusBar.setText(text.toString());
819 public void mouseDragged(MouseEvent evt)
821 if (mouseWheelPressed)
823 int oldWidth = av.charWidth;
825 // Which is bigger, left-right or up-down?
826 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
827 .getX() - lastMousePress.getX()))
829 int fontSize = av.font.getSize();
831 if (evt.getY() < lastMousePress.getY())
835 else if (evt.getY() > lastMousePress.getY())
845 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
846 av.charWidth = oldWidth;
851 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
855 else if (evt.getX() > lastMousePress.getX())
860 ap.paintAlignment(false);
863 FontMetrics fm = getFontMetrics(av.getFont());
864 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
866 lastMousePress = evt.getPoint();
873 doMouseDraggedDefineMode(evt);
877 int res = findRes(evt);
884 if ((lastres == -1) || (lastres == res))
889 if ((res < av.getAlignment().getWidth()) && (res < lastres))
891 // dragLeft, delete gap
892 editSequence(false, false, res);
896 editSequence(true, false, res);
899 mouseDragging = true;
900 if (scrollThread != null)
902 scrollThread.setEvent(evt);
906 // TODO: Make it more clever than many booleans
907 synchronized void editSequence(boolean insertGap, boolean editSeq,
912 boolean fixedColumns = false;
913 SequenceGroup sg = av.getSelectionGroup();
915 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
917 // No group, but the sequence may represent a group
918 if (!groupEditing && av.hasHiddenRows())
920 if (av.isHiddenRepSequence(seq))
922 sg = av.getRepresentedSequences(seq);
927 StringBuffer message = new StringBuffer();
930 message.append("Edit group:");
931 if (editCommand == null)
933 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
938 message.append("Edit sequence: " + seq.getName());
939 String label = seq.getName();
940 if (label.length() > 10)
942 label = label.substring(0, 10);
944 if (editCommand == null)
946 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
952 message.append(" insert ");
956 message.append(" delete ");
959 message.append(Math.abs(startres - lastres) + " gaps.");
960 ap.alignFrame.statusBar.setText(message.toString());
962 // Are we editing within a selection group?
964 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
969 // sg might be null as the user may only see 1 sequence,
970 // but the sequence represents a group
973 if (!av.isHiddenRepSequence(seq))
978 sg = av.getRepresentedSequences(seq);
981 fixedLeft = sg.getStartRes();
982 fixedRight = sg.getEndRes();
984 if ((startres < fixedLeft && lastres >= fixedLeft)
985 || (startres >= fixedLeft && lastres < fixedLeft)
986 || (startres > fixedRight && lastres <= fixedRight)
987 || (startres <= fixedRight && lastres > fixedRight))
993 if (fixedLeft > startres)
995 fixedRight = fixedLeft - 1;
998 else if (fixedRight < startres)
1000 fixedLeft = fixedRight;
1005 if (av.hasHiddenColumns())
1007 fixedColumns = true;
1008 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1009 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1011 if ((insertGap && startres > y1 && lastres < y1)
1012 || (!insertGap && startres < y2 && lastres > y2))
1018 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1019 // Selection spans a hidden region
1020 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1028 fixedRight = y2 - 1;
1035 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1036 int g, groupSize = vseqs.size();
1037 SequenceI[] groupSeqs = new SequenceI[groupSize];
1038 for (g = 0; g < groupSeqs.length; g++)
1040 groupSeqs[g] = vseqs.get(g);
1046 // If the user has selected the whole sequence, and is dragging to
1047 // the right, we can still extend the alignment and selectionGroup
1048 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1049 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1051 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1052 fixedRight = sg.getEndRes();
1055 // Is it valid with fixed columns??
1056 // Find the next gap before the end
1057 // of the visible region boundary
1058 boolean blank = false;
1059 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1063 for (g = 0; g < groupSize; g++)
1065 for (int j = 0; j < startres - lastres; j++)
1067 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1068 .getCharAt(fixedRight - j)))
1083 if (sg.getSize() == av.getAlignment().getHeight())
1085 if ((av.hasHiddenColumns() && startres < av
1086 .getColumnSelection().getHiddenBoundaryRight(startres)))
1092 int alWidth = av.getAlignment().getWidth();
1093 if (av.hasHiddenRows())
1095 int hwidth = av.getAlignment().getHiddenSequences()
1097 if (hwidth > alWidth)
1102 // We can still insert gaps if the selectionGroup
1103 // contains all the sequences
1104 sg.setEndRes(sg.getEndRes() + startres - lastres);
1105 fixedRight = alWidth + startres - lastres;
1116 else if (!insertGap)
1118 // / Are we able to delete?
1119 // ie are all columns blank?
1121 for (g = 0; g < groupSize; g++)
1123 for (int j = startres; j < lastres; j++)
1125 if (groupSeqs[g].getLength() <= j)
1130 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1132 // Not a gap, block edit not valid
1142 // dragging to the right
1143 if (fixedColumns && fixedRight != -1)
1145 for (int j = lastres; j < startres; j++)
1147 insertChar(j, groupSeqs, fixedRight);
1152 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs,
1153 startres, startres - lastres, av.getAlignment(), true);
1158 // dragging to the left
1159 if (fixedColumns && fixedRight != -1)
1161 for (int j = lastres; j > startres; j--)
1163 deleteChar(startres, groupSeqs, fixedRight);
1168 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs,
1169 startres, lastres - startres, av.getAlignment(), true);
1175 // ///Editing a single sequence///////////
1179 // dragging to the right
1180 if (fixedColumns && fixedRight != -1)
1182 for (int j = lastres; j < startres; j++)
1184 insertChar(j, new SequenceI[]
1185 { seq }, fixedRight);
1190 editCommand.appendEdit(Action.INSERT_GAP, new SequenceI[]
1191 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1198 // dragging to the left
1199 if (fixedColumns && fixedRight != -1)
1201 for (int j = lastres; j > startres; j--)
1203 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1208 deleteChar(startres, new SequenceI[]
1209 { seq }, fixedRight);
1214 // could be a keyboard edit trying to delete none gaps
1216 for (int m = startres; m < lastres; m++)
1218 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1227 editCommand.appendEdit(Action.DELETE_GAP,
1229 { seq }, startres, max, av.getAlignment(), true);
1234 {// insertGap==false AND editSeq==TRUE;
1235 if (fixedColumns && fixedRight != -1)
1237 for (int j = lastres; j < startres; j++)
1239 insertChar(j, new SequenceI[]
1240 { seq }, fixedRight);
1245 editCommand.appendEdit(Action.INSERT_NUC, new SequenceI[]
1246 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1253 seqCanvas.repaint();
1256 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1258 int blankColumn = fixedColumn;
1259 for (int s = 0; s < seq.length; s++)
1261 // Find the next gap before the end of the visible region boundary
1262 // If lastCol > j, theres a boundary after the gap insertion
1264 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1266 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1268 // Theres a space, so break and insert the gap
1273 if (blankColumn <= j)
1275 blankColumn = fixedColumn;
1281 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1282 av.getAlignment(), true);
1284 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1,
1285 av.getAlignment(), true);
1289 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1292 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1,
1293 av.getAlignment(), true);
1295 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1296 av.getAlignment(), true);
1306 public void mouseEntered(MouseEvent e)
1313 if (scrollThread != null)
1315 scrollThread.running = false;
1316 scrollThread = null;
1327 public void mouseExited(MouseEvent e)
1329 if (av.getWrapAlignment())
1336 scrollThread = new ScrollThread();
1341 public void mouseClicked(MouseEvent evt)
1343 SequenceGroup sg = null;
1344 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1345 if (evt.getClickCount() > 1)
1347 sg = av.getSelectionGroup();
1348 if (sg != null && sg.getSize() == 1
1349 && sg.getEndRes() - sg.getStartRes() < 2)
1351 av.setSelectionGroup(null);
1354 List<SequenceFeature> features = seqCanvas.getFeatureRenderer().findFeaturesAtRes(
1355 sequence.getDatasetSequence(),
1356 sequence.findPosition(findRes(evt)));
1358 if (features != null && features.size()> 0)
1360 SearchResults highlight = new SearchResults();
1361 highlight.addResult(sequence, features.get(0).getBegin(),
1362 features.get(0).getEnd());
1363 seqCanvas.highlightSearchResults(highlight);
1365 if (features != null && features.size()> 0)
1367 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1368 { sequence }, features.toArray(new SequenceFeature[features.size()]), false, ap);
1370 seqCanvas.highlightSearchResults(null);
1376 public void mouseWheelMoved(MouseWheelEvent e)
1379 if (e.getWheelRotation() > 0)
1381 if (e.isShiftDown())
1383 ap.scrollRight(true);
1393 if (e.isShiftDown())
1395 ap.scrollRight(false);
1402 // TODO Update tooltip for new position.
1411 public void doMousePressedDefineMode(MouseEvent evt)
1413 int res = findRes(evt);
1414 int seq = findSeq(evt);
1417 startWrapBlock = wrappedBlock;
1419 if (av.wrapAlignment && seq > av.getAlignment().getHeight())
1421 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1422 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1423 MessageManager.getString("label.wrapped_view_no_edit"),
1424 JOptionPane.WARNING_MESSAGE);
1428 if (seq < 0 || res < 0)
1433 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1435 if ((sequence == null) || (res > sequence.getLength()))
1440 stretchGroup = av.getSelectionGroup();
1442 if (stretchGroup == null)
1444 stretchGroup = av.getAlignment().findGroup(sequence);
1446 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1447 && (res < stretchGroup.getEndRes()))
1449 av.setSelectionGroup(stretchGroup);
1453 stretchGroup = null;
1456 else if (!stretchGroup.getSequences(null).contains(sequence)
1457 || (stretchGroup.getStartRes() > res)
1458 || (stretchGroup.getEndRes() < res))
1460 stretchGroup = null;
1462 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1464 if (allGroups != null)
1466 for (int i = 0; i < allGroups.length; i++)
1468 if ((allGroups[i].getStartRes() <= res)
1469 && (allGroups[i].getEndRes() >= res))
1471 stretchGroup = allGroups[i];
1477 av.setSelectionGroup(stretchGroup);
1481 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1483 List<SequenceFeature> allFeatures = ap.getFeatureRenderer().findFeaturesAtRes(
1484 sequence.getDatasetSequence(), sequence.findPosition(res));
1485 Vector links = new Vector();
1486 for (SequenceFeature sf:allFeatures)
1488 if (sf.links != null)
1490 for (int j = 0; j < sf.links.size(); j++)
1492 links.addElement(sf.links.elementAt(j));
1497 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1498 pop.show(this, evt.getX(), evt.getY());
1504 seqCanvas.cursorX = findRes(evt);
1505 seqCanvas.cursorY = findSeq(evt);
1506 seqCanvas.repaint();
1510 if (stretchGroup == null)
1512 // Only if left mouse button do we want to change group sizes
1514 // define a new group here
1515 SequenceGroup sg = new SequenceGroup();
1516 sg.setStartRes(res);
1518 sg.addSequence(sequence, false);
1519 av.setSelectionGroup(sg);
1523 if (av.getConservationSelected())
1525 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1529 if (av.getAbovePIDThreshold())
1531 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1534 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1536 // Edit end res position of selected group
1537 changeEndRes = true;
1539 else if ((stretchGroup != null)
1540 && (stretchGroup.getStartRes() == res))
1542 // Edit end res position of selected group
1543 changeStartRes = true;
1545 stretchGroup.getWidth();
1548 seqCanvas.repaint();
1557 public void doMouseReleasedDefineMode(MouseEvent evt)
1559 if (stretchGroup == null)
1564 stretchGroup.recalcConservation(); // always do this - annotation has own
1566 if (stretchGroup.cs != null)
1568 stretchGroup.cs.alignmentChanged(stretchGroup,
1569 av.getHiddenRepSequences());
1571 if (stretchGroup.cs.conservationApplied())
1573 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1574 stretchGroup.getName());
1578 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1579 stretchGroup.getName());
1582 PaintRefresher.Refresh(this, av.getSequenceSetId());
1583 ap.paintAlignment(true);
1585 changeEndRes = false;
1586 changeStartRes = false;
1587 stretchGroup = null;
1597 public void doMouseDraggedDefineMode(MouseEvent evt)
1599 int res = findRes(evt);
1600 int y = findSeq(evt);
1602 if (wrappedBlock != startWrapBlock)
1607 if (stretchGroup == null)
1612 if (res >= av.getAlignment().getWidth())
1614 res = av.getAlignment().getWidth() - 1;
1617 if (stretchGroup.getEndRes() == res)
1619 // Edit end res position of selected group
1620 changeEndRes = true;
1622 else if (stretchGroup.getStartRes() == res)
1624 // Edit start res position of selected group
1625 changeStartRes = true;
1628 if (res < av.getStartRes())
1630 res = av.getStartRes();
1635 if (res > (stretchGroup.getStartRes() - 1))
1637 stretchGroup.setEndRes(res);
1640 else if (changeStartRes)
1642 if (res < (stretchGroup.getEndRes() + 1))
1644 stretchGroup.setStartRes(res);
1648 int dragDirection = 0;
1654 else if (y < oldSeq)
1659 while ((y != oldSeq) && (oldSeq > -1)
1660 && (y < av.getAlignment().getHeight()))
1662 // This routine ensures we don't skip any sequences, as the
1663 // selection is quite slow.
1664 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1666 oldSeq += dragDirection;
1673 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1675 if (stretchGroup.getSequences(null).contains(nextSeq))
1677 stretchGroup.deleteSequence(seq, false);
1683 stretchGroup.addSequence(seq, false);
1686 stretchGroup.addSequence(nextSeq, false);
1695 mouseDragging = true;
1697 if (scrollThread != null)
1699 scrollThread.setEvent(evt);
1702 seqCanvas.repaint();
1705 void scrollCanvas(MouseEvent evt)
1709 if (scrollThread != null)
1711 scrollThread.running = false;
1712 scrollThread = null;
1714 mouseDragging = false;
1718 if (scrollThread == null)
1720 scrollThread = new ScrollThread();
1723 mouseDragging = true;
1724 scrollThread.setEvent(evt);
1729 // this class allows scrolling off the bottom of the visible alignment
1730 class ScrollThread extends Thread
1734 boolean running = false;
1736 public ScrollThread()
1741 public void setEvent(MouseEvent e)
1746 public void stopScrolling()
1760 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1762 running = ap.scrollUp(true);
1765 if (mouseDragging && (evt.getY() >= getHeight())
1766 && (av.getAlignment().getHeight() > av.getEndSeq()))
1768 running = ap.scrollUp(false);
1771 if (mouseDragging && (evt.getX() < 0))
1773 running = ap.scrollRight(false);
1775 else if (mouseDragging && (evt.getX() >= getWidth()))
1777 running = ap.scrollRight(true);
1784 } catch (Exception ex)
1792 * modify current selection according to a received message.
1795 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1796 SelectionSource source)
1798 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1799 // handles selection messages...
1800 // TODO: extend config options to allow user to control if selections may be
1801 // shared between viewports.
1803 || !av.followSelection
1804 || (av.isSelectionGroupChanged(false) || av
1805 .isColSelChanged(false))
1806 || (source instanceof AlignViewport && ((AlignViewport) source)
1807 .getSequenceSetId().equals(av.getSequenceSetId())))
1811 // do we want to thread this ? (contention with seqsel and colsel locks, I
1813 // rules are: colsel is copied if there is a real intersection between
1814 // sequence selection
1815 boolean repaint = false, copycolsel = true;
1816 // if (!av.isSelectionGroupChanged(false))
1818 SequenceGroup sgroup = null;
1819 if (seqsel != null && seqsel.getSize() > 0)
1821 if (av.getAlignment() == null)
1823 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1824 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1825 + " 's alignment is NULL! returning immediatly.");
1828 sgroup = seqsel.intersect(av.getAlignment(),
1829 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1830 if ((sgroup == null || sgroup.getSize() == 0)
1831 || (colsel == null || colsel.size() == 0))
1833 // don't copy columns if the region didn't intersect.
1837 if (sgroup != null && sgroup.getSize() > 0)
1839 av.setSelectionGroup(sgroup);
1843 av.setSelectionGroup(null);
1845 av.isSelectionGroupChanged(true);
1850 // the current selection is unset or from a previous message
1851 // so import the new colsel.
1852 if (colsel == null || colsel.size() == 0)
1854 if (av.getColumnSelection() != null)
1856 av.getColumnSelection().clear();
1862 // TODO: shift colSel according to the intersecting sequences
1863 if (av.getColumnSelection() == null)
1865 av.setColumnSelection(new ColumnSelection(colsel));
1869 av.getColumnSelection().setElementsFrom(colsel);
1872 av.isColSelChanged(true);
1876 && av.hasHiddenColumns()
1877 && (av.getColumnSelection() == null || av.getColumnSelection()
1878 .getHiddenColumns() == null))
1880 System.err.println("Bad things");
1884 // probably finessing with multiple redraws here
1885 PaintRefresher.Refresh(this, av.getSequenceSetId());
1886 // ap.paintAlignment(false);