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.getWrapAlignment())
171 int hgap = av.getCharHeight();
172 if (av.getScaleAboveWrapped())
174 hgap += av.getCharHeight();
177 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
179 + seqCanvas.getAnnotationHeight();
183 x -= seqCanvas.LABEL_WEST;
185 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
191 wrappedBlock = y / cHeight;
192 wrappedBlock += av.getStartRes() / cwidth;
194 res = wrappedBlock * cwidth + x / av.getCharWidth();
199 if (x > seqCanvas.getWidth() + seqCanvas.getWidth())
201 // make sure we calculate relative to visible alignment, rather than
203 x = seqCanvas.getX() + seqCanvas.getWidth();
205 res = (x / av.getCharWidth()) + av.getStartRes();
208 if (av.hasHiddenColumns())
210 res = av.getColumnSelection().adjustForHiddenColumns(res);
217 int findSeq(MouseEvent evt)
222 if (av.getWrapAlignment())
224 int hgap = av.getCharHeight();
225 if (av.getScaleAboveWrapped())
227 hgap += av.getCharHeight();
230 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
232 + seqCanvas.getAnnotationHeight();
236 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
241 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
242 .getAlignment().getHeight() - 1);
250 if (editCommand != null && editCommand.getSize() > 0)
252 ap.alignFrame.addHistoryItem(editCommand);
253 av.firePropertyChange("alignment", null, av.getAlignment()
260 groupEditing = false;
268 seqCanvas.cursorY = getKeyboardNo1() - 1;
272 void setCursorColumn()
274 seqCanvas.cursorX = getKeyboardNo1() - 1;
278 void setCursorRowAndColumn()
280 if (keyboardNo2 == null)
282 keyboardNo2 = new StringBuffer();
286 seqCanvas.cursorX = getKeyboardNo1() - 1;
287 seqCanvas.cursorY = getKeyboardNo2() - 1;
292 void setCursorPosition()
294 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
296 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
300 void moveCursor(int dx, int dy)
302 seqCanvas.cursorX += dx;
303 seqCanvas.cursorY += dy;
304 if (av.hasHiddenColumns()
305 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
307 int original = seqCanvas.cursorX - dx;
308 int maxWidth = av.getAlignment().getWidth();
310 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
311 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
313 seqCanvas.cursorX += dx;
316 if (seqCanvas.cursorX >= maxWidth
317 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
319 seqCanvas.cursorX = original;
326 void scrollToVisible()
328 if (seqCanvas.cursorX < 0)
330 seqCanvas.cursorX = 0;
332 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
334 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
337 if (seqCanvas.cursorY < 0)
339 seqCanvas.cursorY = 0;
341 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
343 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
347 if (av.getWrapAlignment())
349 ap.scrollToWrappedVisible(seqCanvas.cursorX);
353 while (seqCanvas.cursorY < av.startSeq)
357 while (seqCanvas.cursorY + 1 > av.endSeq)
361 if (!av.getWrapAlignment())
363 while (seqCanvas.cursorX < av.getColumnSelection()
364 .adjustForHiddenColumns(av.startRes))
366 if (!ap.scrollRight(false))
371 while (seqCanvas.cursorX > av.getColumnSelection()
372 .adjustForHiddenColumns(av.endRes))
374 if (!ap.scrollRight(true))
381 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
382 seqCanvas.cursorX, seqCanvas.cursorY);
387 void setSelectionAreaAtCursor(boolean topLeft)
389 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
391 if (av.getSelectionGroup() != null)
393 SequenceGroup sg = av.getSelectionGroup();
394 // Find the top and bottom of this group
395 int min = av.getAlignment().getHeight(), max = 0;
396 for (int i = 0; i < sg.getSize(); i++)
398 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
413 sg.setStartRes(seqCanvas.cursorX);
414 if (sg.getEndRes() < seqCanvas.cursorX)
416 sg.setEndRes(seqCanvas.cursorX);
419 min = seqCanvas.cursorY;
423 sg.setEndRes(seqCanvas.cursorX);
424 if (sg.getStartRes() > seqCanvas.cursorX)
426 sg.setStartRes(seqCanvas.cursorX);
429 max = seqCanvas.cursorY + 1;
434 // Only the user can do this
435 av.setSelectionGroup(null);
439 // Now add any sequences between min and max
440 sg.getSequences(null).clear();
441 for (int i = min; i < max; i++)
443 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
448 if (av.getSelectionGroup() == null)
450 SequenceGroup sg = new SequenceGroup();
451 sg.setStartRes(seqCanvas.cursorX);
452 sg.setEndRes(seqCanvas.cursorX);
453 sg.addSequence(sequence, false);
454 av.setSelectionGroup(sg);
457 ap.paintAlignment(false);
461 void insertGapAtCursor(boolean group)
463 groupEditing = group;
464 startseq = seqCanvas.cursorY;
465 lastres = seqCanvas.cursorX;
466 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
470 void deleteGapAtCursor(boolean group)
472 groupEditing = group;
473 startseq = seqCanvas.cursorY;
474 lastres = seqCanvas.cursorX + getKeyboardNo1();
475 editSequence(false, false, seqCanvas.cursorX);
479 void insertNucAtCursor(boolean group, String nuc)
481 groupEditing = group;
482 startseq = seqCanvas.cursorY;
483 lastres = seqCanvas.cursorX;
484 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
488 void numberPressed(char value)
490 if (keyboardNo1 == null)
492 keyboardNo1 = new StringBuffer();
495 if (keyboardNo2 != null)
497 keyboardNo2.append(value);
501 keyboardNo1.append(value);
509 if (keyboardNo1 != null)
511 int value = Integer.parseInt(keyboardNo1.toString());
515 } catch (Exception x)
526 if (keyboardNo2 != null)
528 int value = Integer.parseInt(keyboardNo2.toString());
532 } catch (Exception x)
546 public void mouseReleased(MouseEvent evt)
548 mouseDragging = false;
549 mouseWheelPressed = false;
553 doMouseReleasedDefineMode(evt);
567 public void mousePressed(MouseEvent evt)
569 lastMousePress = evt.getPoint();
571 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
573 mouseWheelPressed = true;
577 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
579 if (evt.isAltDown() || evt.isControlDown())
587 doMousePressedDefineMode(evt);
591 int seq = findSeq(evt);
592 int res = findRes(evt);
594 if (seq < 0 || res < 0)
599 if ((seq < av.getAlignment().getHeight())
600 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
617 public void mouseOverSequence(SequenceI sequence, int index, int pos)
619 String tmp = sequence.hashCode() + " " + index + " " + pos;
621 if (lastMessage == null || !lastMessage.equals(tmp))
623 // System.err.println("mouseOver Sequence: "+tmp);
624 ssm.mouseOverSequence(sequence, index, pos, av);
630 public void highlightSequence(SearchResults results)
632 if (av.followHighlight)
634 if (ap.scrollToPosition(results, false))
636 seqCanvas.revalidate();
639 seqCanvas.highlightSearchResults(results);
643 public void updateColours(SequenceI seq, int index)
645 System.out.println("update the seqPanel colours");
656 public void mouseMoved(MouseEvent evt)
660 // This is because MacOSX creates a mouseMoved
661 // If control is down, other platforms will not.
665 int res = findRes(evt);
666 int seq = findSeq(evt);
668 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
673 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
675 if (res >= sequence.getLength())
680 pos = setStatusMessage(sequence, res, seq);
681 if (ssm != null && pos > -1)
683 mouseOverSequence(sequence, res, pos);
686 tooltipText.setLength(6); // Cuts the buffer back to <html>
688 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
691 for (int g = 0; g < groups.length; g++)
693 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
695 if (!groups[g].getName().startsWith("JTreeGroup")
696 && !groups[g].getName().startsWith("JGroup"))
698 tooltipText.append(groups[g].getName());
701 if (groups[g].getDescription() != null)
703 tooltipText.append(": " + groups[g].getDescription());
709 // use aa to see if the mouse pointer is on a
710 if (av.isShowSequenceFeatures())
713 List<SequenceFeature> features = ap.getFeatureRenderer().findFeaturesAtRes(
714 sequence.getDatasetSequence(),
715 rpos = sequence.findPosition(res));
716 seqARep.appendFeatures(tooltipText, rpos, features,
717 this.ap.getSeqPanel().seqCanvas.fr.getMinMax());
719 if (tooltipText.length() == 6) // <html></html>
721 setToolTipText(null);
726 if (lastTooltip == null
727 || !lastTooltip.equals(tooltipText.toString()))
729 String formatedTooltipText = JvSwingUtils.wrapTooltip(true,
730 tooltipText.toString());
731 // String formatedTooltipText = tooltipText.toString();
732 setToolTipText(formatedTooltipText);
733 lastTooltip = tooltipText.toString();
740 private Point lastp = null;
745 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
747 public Point getToolTipLocation(MouseEvent event)
749 int x = event.getX(), w = getWidth();
750 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
753 if (!event.isShiftDown() || p == null)
755 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
756 event.getX() + wdth, event.getY() - 20) : null;
759 * TODO: try to modify position region is not obcured by tooltip
767 * Set status message in alignment panel
770 * aligned sequence object
774 * index of sequence in alignment
775 * @return position of res in sequence
777 int setStatusMessage(SequenceI sequence, int res, int seq)
780 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
781 + sequence.getName());
784 if (av.getAlignment().isNucleotide())
786 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
790 text.append(" Nucleotide: ");
795 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
798 text.append(" Residue: ");
804 pos = sequence.findPosition(res);
807 text.append(obj + " (" + pos + ")");
810 ap.alignFrame.statusBar.setText(text.toString());
821 public void mouseDragged(MouseEvent evt)
823 if (mouseWheelPressed)
825 int oldWidth = av.getCharWidth();
827 // Which is bigger, left-right or up-down?
828 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
829 .getX() - lastMousePress.getX()))
831 int fontSize = av.font.getSize();
833 if (evt.getY() < lastMousePress.getY())
837 else if (evt.getY() > lastMousePress.getY())
847 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
848 av.setCharWidth(oldWidth);
853 if (evt.getX() < lastMousePress.getX() && av.getCharWidth() > 1)
855 av.setCharWidth(av.getCharWidth() - 1);
857 else if (evt.getX() > lastMousePress.getX())
859 av.setCharWidth(av.getCharWidth() + 1);
862 ap.paintAlignment(false);
865 FontMetrics fm = getFontMetrics(av.getFont());
866 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
868 lastMousePress = evt.getPoint();
875 doMouseDraggedDefineMode(evt);
879 int res = findRes(evt);
886 if ((lastres == -1) || (lastres == res))
891 if ((res < av.getAlignment().getWidth()) && (res < lastres))
893 // dragLeft, delete gap
894 editSequence(false, false, res);
898 editSequence(true, false, res);
901 mouseDragging = true;
902 if (scrollThread != null)
904 scrollThread.setEvent(evt);
908 // TODO: Make it more clever than many booleans
909 synchronized void editSequence(boolean insertGap, boolean editSeq,
914 boolean fixedColumns = false;
915 SequenceGroup sg = av.getSelectionGroup();
917 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
919 // No group, but the sequence may represent a group
920 if (!groupEditing && av.hasHiddenRows())
922 if (av.isHiddenRepSequence(seq))
924 sg = av.getRepresentedSequences(seq);
929 StringBuffer message = new StringBuffer();
932 message.append("Edit group:");
933 if (editCommand == null)
935 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
940 message.append("Edit sequence: " + seq.getName());
941 String label = seq.getName();
942 if (label.length() > 10)
944 label = label.substring(0, 10);
946 if (editCommand == null)
948 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
954 message.append(" insert ");
958 message.append(" delete ");
961 message.append(Math.abs(startres - lastres) + " gaps.");
962 ap.alignFrame.statusBar.setText(message.toString());
964 // Are we editing within a selection group?
966 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
971 // sg might be null as the user may only see 1 sequence,
972 // but the sequence represents a group
975 if (!av.isHiddenRepSequence(seq))
980 sg = av.getRepresentedSequences(seq);
983 fixedLeft = sg.getStartRes();
984 fixedRight = sg.getEndRes();
986 if ((startres < fixedLeft && lastres >= fixedLeft)
987 || (startres >= fixedLeft && lastres < fixedLeft)
988 || (startres > fixedRight && lastres <= fixedRight)
989 || (startres <= fixedRight && lastres > fixedRight))
995 if (fixedLeft > startres)
997 fixedRight = fixedLeft - 1;
1000 else if (fixedRight < startres)
1002 fixedLeft = fixedRight;
1007 if (av.hasHiddenColumns())
1009 fixedColumns = true;
1010 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1011 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1013 if ((insertGap && startres > y1 && lastres < y1)
1014 || (!insertGap && startres < y2 && lastres > y2))
1020 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1021 // Selection spans a hidden region
1022 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1030 fixedRight = y2 - 1;
1037 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1038 int g, groupSize = vseqs.size();
1039 SequenceI[] groupSeqs = new SequenceI[groupSize];
1040 for (g = 0; g < groupSeqs.length; g++)
1042 groupSeqs[g] = vseqs.get(g);
1048 // If the user has selected the whole sequence, and is dragging to
1049 // the right, we can still extend the alignment and selectionGroup
1050 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1051 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1053 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1054 fixedRight = sg.getEndRes();
1057 // Is it valid with fixed columns??
1058 // Find the next gap before the end
1059 // of the visible region boundary
1060 boolean blank = false;
1061 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1065 for (g = 0; g < groupSize; g++)
1067 for (int j = 0; j < startres - lastres; j++)
1069 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1070 .getCharAt(fixedRight - j)))
1085 if (sg.getSize() == av.getAlignment().getHeight())
1087 if ((av.hasHiddenColumns() && startres < av
1088 .getColumnSelection().getHiddenBoundaryRight(startres)))
1094 int alWidth = av.getAlignment().getWidth();
1095 if (av.hasHiddenRows())
1097 int hwidth = av.getAlignment().getHiddenSequences()
1099 if (hwidth > alWidth)
1104 // We can still insert gaps if the selectionGroup
1105 // contains all the sequences
1106 sg.setEndRes(sg.getEndRes() + startres - lastres);
1107 fixedRight = alWidth + startres - lastres;
1118 else if (!insertGap)
1120 // / Are we able to delete?
1121 // ie are all columns blank?
1123 for (g = 0; g < groupSize; g++)
1125 for (int j = startres; j < lastres; j++)
1127 if (groupSeqs[g].getLength() <= j)
1132 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1134 // Not a gap, block edit not valid
1144 // dragging to the right
1145 if (fixedColumns && fixedRight != -1)
1147 for (int j = lastres; j < startres; j++)
1149 insertChar(j, groupSeqs, fixedRight);
1154 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs,
1155 startres, startres - lastres, av.getAlignment(), true);
1160 // dragging to the left
1161 if (fixedColumns && fixedRight != -1)
1163 for (int j = lastres; j > startres; j--)
1165 deleteChar(startres, groupSeqs, fixedRight);
1170 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs,
1171 startres, lastres - startres, av.getAlignment(), true);
1177 // ///Editing a single sequence///////////
1181 // dragging to the right
1182 if (fixedColumns && fixedRight != -1)
1184 for (int j = lastres; j < startres; j++)
1186 insertChar(j, new SequenceI[]
1187 { seq }, fixedRight);
1192 editCommand.appendEdit(Action.INSERT_GAP, new SequenceI[]
1193 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1200 // dragging to the left
1201 if (fixedColumns && fixedRight != -1)
1203 for (int j = lastres; j > startres; j--)
1205 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1210 deleteChar(startres, new SequenceI[]
1211 { seq }, fixedRight);
1216 // could be a keyboard edit trying to delete none gaps
1218 for (int m = startres; m < lastres; m++)
1220 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1229 editCommand.appendEdit(Action.DELETE_GAP,
1231 { seq }, startres, max, av.getAlignment(), true);
1236 {// insertGap==false AND editSeq==TRUE;
1237 if (fixedColumns && fixedRight != -1)
1239 for (int j = lastres; j < startres; j++)
1241 insertChar(j, new SequenceI[]
1242 { seq }, fixedRight);
1247 editCommand.appendEdit(Action.INSERT_NUC, new SequenceI[]
1248 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1255 seqCanvas.repaint();
1258 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1260 int blankColumn = fixedColumn;
1261 for (int s = 0; s < seq.length; s++)
1263 // Find the next gap before the end of the visible region boundary
1264 // If lastCol > j, theres a boundary after the gap insertion
1266 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1268 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1270 // Theres a space, so break and insert the gap
1275 if (blankColumn <= j)
1277 blankColumn = fixedColumn;
1283 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1284 av.getAlignment(), true);
1286 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1,
1287 av.getAlignment(), true);
1291 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1294 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1,
1295 av.getAlignment(), true);
1297 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1298 av.getAlignment(), true);
1308 public void mouseEntered(MouseEvent e)
1315 if (scrollThread != null)
1317 scrollThread.running = false;
1318 scrollThread = null;
1329 public void mouseExited(MouseEvent e)
1331 if (av.getWrapAlignment())
1338 scrollThread = new ScrollThread();
1343 public void mouseClicked(MouseEvent evt)
1345 SequenceGroup sg = null;
1346 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1347 if (evt.getClickCount() > 1)
1349 sg = av.getSelectionGroup();
1350 if (sg != null && sg.getSize() == 1
1351 && sg.getEndRes() - sg.getStartRes() < 2)
1353 av.setSelectionGroup(null);
1356 List<SequenceFeature> features = seqCanvas.getFeatureRenderer().findFeaturesAtRes(
1357 sequence.getDatasetSequence(),
1358 sequence.findPosition(findRes(evt)));
1360 if (features != null && features.size()> 0)
1362 SearchResults highlight = new SearchResults();
1363 highlight.addResult(sequence, features.get(0).getBegin(),
1364 features.get(0).getEnd());
1365 seqCanvas.highlightSearchResults(highlight);
1367 if (features != null && features.size()> 0)
1369 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1370 { sequence }, features.toArray(new SequenceFeature[features.size()]), false, ap);
1372 seqCanvas.highlightSearchResults(null);
1378 public void mouseWheelMoved(MouseWheelEvent e)
1381 if (e.getWheelRotation() > 0)
1383 if (e.isShiftDown())
1385 ap.scrollRight(true);
1395 if (e.isShiftDown())
1397 ap.scrollRight(false);
1404 // TODO Update tooltip for new position.
1413 public void doMousePressedDefineMode(MouseEvent evt)
1415 int res = findRes(evt);
1416 int seq = findSeq(evt);
1419 startWrapBlock = wrappedBlock;
1421 if (av.getWrapAlignment() && seq > av.getAlignment().getHeight())
1423 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1424 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1425 MessageManager.getString("label.wrapped_view_no_edit"),
1426 JOptionPane.WARNING_MESSAGE);
1430 if (seq < 0 || res < 0)
1435 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1437 if ((sequence == null) || (res > sequence.getLength()))
1442 stretchGroup = av.getSelectionGroup();
1444 if (stretchGroup == null)
1446 stretchGroup = av.getAlignment().findGroup(sequence);
1448 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1449 && (res < stretchGroup.getEndRes()))
1451 av.setSelectionGroup(stretchGroup);
1455 stretchGroup = null;
1458 else if (!stretchGroup.getSequences(null).contains(sequence)
1459 || (stretchGroup.getStartRes() > res)
1460 || (stretchGroup.getEndRes() < res))
1462 stretchGroup = null;
1464 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1466 if (allGroups != null)
1468 for (int i = 0; i < allGroups.length; i++)
1470 if ((allGroups[i].getStartRes() <= res)
1471 && (allGroups[i].getEndRes() >= res))
1473 stretchGroup = allGroups[i];
1479 av.setSelectionGroup(stretchGroup);
1483 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1485 List<SequenceFeature> allFeatures = ap.getFeatureRenderer().findFeaturesAtRes(
1486 sequence.getDatasetSequence(), sequence.findPosition(res));
1487 Vector links = new Vector();
1488 for (SequenceFeature sf:allFeatures)
1490 if (sf.links != null)
1492 for (int j = 0; j < sf.links.size(); j++)
1494 links.addElement(sf.links.elementAt(j));
1499 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1500 pop.show(this, evt.getX(), evt.getY());
1506 seqCanvas.cursorX = findRes(evt);
1507 seqCanvas.cursorY = findSeq(evt);
1508 seqCanvas.repaint();
1512 if (stretchGroup == null)
1514 // Only if left mouse button do we want to change group sizes
1516 // define a new group here
1517 SequenceGroup sg = new SequenceGroup();
1518 sg.setStartRes(res);
1520 sg.addSequence(sequence, false);
1521 av.setSelectionGroup(sg);
1525 if (av.getConservationSelected())
1527 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1531 if (av.getAbovePIDThreshold())
1533 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1536 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1538 // Edit end res position of selected group
1539 changeEndRes = true;
1541 else if ((stretchGroup != null)
1542 && (stretchGroup.getStartRes() == res))
1544 // Edit end res position of selected group
1545 changeStartRes = true;
1547 stretchGroup.getWidth();
1550 seqCanvas.repaint();
1559 public void doMouseReleasedDefineMode(MouseEvent evt)
1561 if (stretchGroup == null)
1566 stretchGroup.recalcConservation(); // always do this - annotation has own
1568 if (stretchGroup.cs != null)
1570 stretchGroup.cs.alignmentChanged(stretchGroup,
1571 av.getHiddenRepSequences());
1573 if (stretchGroup.cs.conservationApplied())
1575 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1576 stretchGroup.getName());
1580 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1581 stretchGroup.getName());
1584 PaintRefresher.Refresh(this, av.getSequenceSetId());
1585 ap.paintAlignment(true);
1587 changeEndRes = false;
1588 changeStartRes = false;
1589 stretchGroup = null;
1599 public void doMouseDraggedDefineMode(MouseEvent evt)
1601 int res = findRes(evt);
1602 int y = findSeq(evt);
1604 if (wrappedBlock != startWrapBlock)
1609 if (stretchGroup == null)
1614 if (res >= av.getAlignment().getWidth())
1616 res = av.getAlignment().getWidth() - 1;
1619 if (stretchGroup.getEndRes() == res)
1621 // Edit end res position of selected group
1622 changeEndRes = true;
1624 else if (stretchGroup.getStartRes() == res)
1626 // Edit start res position of selected group
1627 changeStartRes = true;
1630 if (res < av.getStartRes())
1632 res = av.getStartRes();
1637 if (res > (stretchGroup.getStartRes() - 1))
1639 stretchGroup.setEndRes(res);
1642 else if (changeStartRes)
1644 if (res < (stretchGroup.getEndRes() + 1))
1646 stretchGroup.setStartRes(res);
1650 int dragDirection = 0;
1656 else if (y < oldSeq)
1661 while ((y != oldSeq) && (oldSeq > -1)
1662 && (y < av.getAlignment().getHeight()))
1664 // This routine ensures we don't skip any sequences, as the
1665 // selection is quite slow.
1666 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1668 oldSeq += dragDirection;
1675 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1677 if (stretchGroup.getSequences(null).contains(nextSeq))
1679 stretchGroup.deleteSequence(seq, false);
1685 stretchGroup.addSequence(seq, false);
1688 stretchGroup.addSequence(nextSeq, false);
1697 mouseDragging = true;
1699 if (scrollThread != null)
1701 scrollThread.setEvent(evt);
1704 seqCanvas.repaint();
1707 void scrollCanvas(MouseEvent evt)
1711 if (scrollThread != null)
1713 scrollThread.running = false;
1714 scrollThread = null;
1716 mouseDragging = false;
1720 if (scrollThread == null)
1722 scrollThread = new ScrollThread();
1725 mouseDragging = true;
1726 scrollThread.setEvent(evt);
1731 // this class allows scrolling off the bottom of the visible alignment
1732 class ScrollThread extends Thread
1736 boolean running = false;
1738 public ScrollThread()
1743 public void setEvent(MouseEvent e)
1748 public void stopScrolling()
1762 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1764 running = ap.scrollUp(true);
1767 if (mouseDragging && (evt.getY() >= getHeight())
1768 && (av.getAlignment().getHeight() > av.getEndSeq()))
1770 running = ap.scrollUp(false);
1773 if (mouseDragging && (evt.getX() < 0))
1775 running = ap.scrollRight(false);
1777 else if (mouseDragging && (evt.getX() >= getWidth()))
1779 running = ap.scrollRight(true);
1786 } catch (Exception ex)
1794 * modify current selection according to a received message.
1797 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1798 SelectionSource source)
1800 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1801 // handles selection messages...
1802 // TODO: extend config options to allow user to control if selections may be
1803 // shared between viewports.
1805 || !av.followSelection
1806 || (av.isSelectionGroupChanged(false) || av
1807 .isColSelChanged(false))
1808 || (source instanceof AlignViewport && ((AlignViewport) source)
1809 .getSequenceSetId().equals(av.getSequenceSetId())))
1813 // do we want to thread this ? (contention with seqsel and colsel locks, I
1815 // rules are: colsel is copied if there is a real intersection between
1816 // sequence selection
1817 boolean repaint = false, copycolsel = true;
1818 // if (!av.isSelectionGroupChanged(false))
1820 SequenceGroup sgroup = null;
1821 if (seqsel != null && seqsel.getSize() > 0)
1823 if (av.getAlignment() == null)
1825 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1826 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1827 + " 's alignment is NULL! returning immediatly.");
1830 sgroup = seqsel.intersect(av.getAlignment(),
1831 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1832 if ((sgroup == null || sgroup.getSize() == 0)
1833 || (colsel == null || colsel.size() == 0))
1835 // don't copy columns if the region didn't intersect.
1839 if (sgroup != null && sgroup.getSize() > 0)
1841 av.setSelectionGroup(sgroup);
1845 av.setSelectionGroup(null);
1847 av.isSelectionGroupChanged(true);
1852 // the current selection is unset or from a previous message
1853 // so import the new colsel.
1854 if (colsel == null || colsel.size() == 0)
1856 if (av.getColumnSelection() != null)
1858 av.getColumnSelection().clear();
1864 // TODO: shift colSel according to the intersecting sequences
1865 if (av.getColumnSelection() == null)
1867 av.setColumnSelection(new ColumnSelection(colsel));
1871 av.getColumnSelection().setElementsFrom(colsel);
1874 av.isColSelChanged(true);
1878 && av.hasHiddenColumns()
1879 && (av.getColumnSelection() == null || av.getColumnSelection()
1880 .getHiddenColumns() == null))
1882 System.err.println("Bad things");
1886 // probably finessing with multiple redraws here
1887 PaintRefresher.Refresh(this, av.getSequenceSetId());
1888 // ap.paintAlignment(false);