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("<html>");
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);
246 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
248 Vector tmp = new Vector();
249 SequenceFeature[] features = sequence.getSequenceFeatures();
250 if (features != null)
252 for (int i = 0; i < features.length; i++)
254 if (av.getFeaturesDisplayed() == null
255 || !av.getFeaturesDisplayed().containsKey(
256 features[i].getType()))
261 if (features[i].featureGroup != null
262 && seqCanvas.fr.featureGroups != null
263 && seqCanvas.fr.featureGroups
264 .containsKey(features[i].featureGroup)
265 && !((Boolean) seqCanvas.fr.featureGroups
266 .get(features[i].featureGroup)).booleanValue())
271 if ((features[i].getBegin() <= res)
272 && (features[i].getEnd() >= res))
274 tmp.addElement(features[i]);
279 features = new SequenceFeature[tmp.size()];
280 tmp.copyInto(features);
287 if (editCommand != null && editCommand.getSize() > 0)
289 ap.alignFrame.addHistoryItem(editCommand);
290 av.firePropertyChange("alignment", null, av.getAlignment()
297 groupEditing = false;
305 seqCanvas.cursorY = getKeyboardNo1() - 1;
309 void setCursorColumn()
311 seqCanvas.cursorX = getKeyboardNo1() - 1;
315 void setCursorRowAndColumn()
317 if (keyboardNo2 == null)
319 keyboardNo2 = new StringBuffer();
323 seqCanvas.cursorX = getKeyboardNo1() - 1;
324 seqCanvas.cursorY = getKeyboardNo2() - 1;
329 void setCursorPosition()
331 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
333 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
337 void moveCursor(int dx, int dy)
339 seqCanvas.cursorX += dx;
340 seqCanvas.cursorY += dy;
341 if (av.hasHiddenColumns()
342 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
344 int original = seqCanvas.cursorX - dx;
345 int maxWidth = av.getAlignment().getWidth();
347 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
348 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
350 seqCanvas.cursorX += dx;
353 if (seqCanvas.cursorX >= maxWidth
354 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
356 seqCanvas.cursorX = original;
363 void scrollToVisible()
365 if (seqCanvas.cursorX < 0)
367 seqCanvas.cursorX = 0;
369 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
371 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
374 if (seqCanvas.cursorY < 0)
376 seqCanvas.cursorY = 0;
378 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
380 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
384 if (av.wrapAlignment)
386 ap.scrollToWrappedVisible(seqCanvas.cursorX);
390 while (seqCanvas.cursorY < av.startSeq)
394 while (seqCanvas.cursorY + 1 > av.endSeq)
398 if (!av.wrapAlignment)
400 while (seqCanvas.cursorX < av.getColumnSelection()
401 .adjustForHiddenColumns(av.startRes))
403 if (!ap.scrollRight(false))
408 while (seqCanvas.cursorX > av.getColumnSelection()
409 .adjustForHiddenColumns(av.endRes))
411 if (!ap.scrollRight(true))
418 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
419 seqCanvas.cursorX, seqCanvas.cursorY);
424 void setSelectionAreaAtCursor(boolean topLeft)
426 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
428 if (av.getSelectionGroup() != null)
430 SequenceGroup sg = av.getSelectionGroup();
431 // Find the top and bottom of this group
432 int min = av.getAlignment().getHeight(), max = 0;
433 for (int i = 0; i < sg.getSize(); i++)
435 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
450 sg.setStartRes(seqCanvas.cursorX);
451 if (sg.getEndRes() < seqCanvas.cursorX)
453 sg.setEndRes(seqCanvas.cursorX);
456 min = seqCanvas.cursorY;
460 sg.setEndRes(seqCanvas.cursorX);
461 if (sg.getStartRes() > seqCanvas.cursorX)
463 sg.setStartRes(seqCanvas.cursorX);
466 max = seqCanvas.cursorY + 1;
471 // Only the user can do this
472 av.setSelectionGroup(null);
476 // Now add any sequences between min and max
477 sg.getSequences(null).clear();
478 for (int i = min; i < max; i++)
480 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
485 if (av.getSelectionGroup() == null)
487 SequenceGroup sg = new SequenceGroup();
488 sg.setStartRes(seqCanvas.cursorX);
489 sg.setEndRes(seqCanvas.cursorX);
490 sg.addSequence(sequence, false);
491 av.setSelectionGroup(sg);
494 ap.paintAlignment(false);
498 void insertGapAtCursor(boolean group)
500 groupEditing = group;
501 startseq = seqCanvas.cursorY;
502 lastres = seqCanvas.cursorX;
503 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
507 void deleteGapAtCursor(boolean group)
509 groupEditing = group;
510 startseq = seqCanvas.cursorY;
511 lastres = seqCanvas.cursorX + getKeyboardNo1();
512 editSequence(false, false, seqCanvas.cursorX);
516 void insertNucAtCursor(boolean group, String nuc)
518 groupEditing = group;
519 startseq = seqCanvas.cursorY;
520 lastres = seqCanvas.cursorX;
521 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
525 void numberPressed(char value)
527 if (keyboardNo1 == null)
529 keyboardNo1 = new StringBuffer();
532 if (keyboardNo2 != null)
534 keyboardNo2.append(value);
538 keyboardNo1.append(value);
546 if (keyboardNo1 != null)
548 int value = Integer.parseInt(keyboardNo1.toString());
552 } catch (Exception x)
563 if (keyboardNo2 != null)
565 int value = Integer.parseInt(keyboardNo2.toString());
569 } catch (Exception x)
583 public void mouseReleased(MouseEvent evt)
585 mouseDragging = false;
586 mouseWheelPressed = false;
590 doMouseReleasedDefineMode(evt);
604 public void mousePressed(MouseEvent evt)
606 lastMousePress = evt.getPoint();
608 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
610 mouseWheelPressed = true;
614 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
616 if (evt.isAltDown() || evt.isControlDown())
624 doMousePressedDefineMode(evt);
628 int seq = findSeq(evt);
629 int res = findRes(evt);
631 if (seq < 0 || res < 0)
636 if ((seq < av.getAlignment().getHeight())
637 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
654 public void mouseOverSequence(SequenceI sequence, int index, int pos)
656 String tmp = sequence.hashCode() + " " + index + " " + pos;
658 if (lastMessage == null || !lastMessage.equals(tmp))
660 // System.err.println("mouseOver Sequence: "+tmp);
661 ssm.mouseOverSequence(sequence, index, pos, av);
667 public void highlightSequence(SearchResults results)
669 if (av.followHighlight)
671 if (ap.scrollToPosition(results, false))
673 seqCanvas.revalidate();
676 seqCanvas.highlightSearchResults(results);
680 public void updateColours(SequenceI seq, int index)
682 System.out.println("update the seqPanel colours");
693 public void mouseMoved(MouseEvent evt)
697 // This is because MacOSX creates a mouseMoved
698 // If control is down, other platforms will not.
702 int res = findRes(evt);
703 int seq = findSeq(evt);
705 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
710 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
712 if (res >= sequence.getLength())
717 pos = setStatusMessage(sequence, res, seq);
718 if (ssm != null && pos > -1)
720 mouseOverSequence(sequence, res, pos);
723 tooltipText.setLength(6); // Cuts the buffer back to <html>
725 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
728 for (int g = 0; g < groups.length; g++)
730 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
732 if (tooltipText.length() > 6)
734 tooltipText.append("<br>");
737 if (!groups[g].getName().startsWith("JTreeGroup")
738 && !groups[g].getName().startsWith("JGroup"))
740 tooltipText.append(groups[g].getName());
743 if (groups[g].getDescription() != null)
745 tooltipText.append(": " + groups[g].getDescription());
751 // use aa to see if the mouse pointer is on a
752 if (av.showSequenceFeatures)
755 SequenceFeature[] features = findFeaturesAtRes(
756 sequence.getDatasetSequence(),
757 rpos = sequence.findPosition(res));
758 seqARep.appendFeatures(tooltipText, rpos, features,
759 this.ap.seqPanel.seqCanvas.fr.minmax);
761 if (tooltipText.length() == 6) // <html></html>
763 setToolTipText(null);
768 tooltipText.append("</html>");
769 if (lastTooltip == null
770 || !lastTooltip.equals(tooltipText.toString()))
772 setToolTipText(JvSwingUtils.wrapTooltip(true,
773 tooltipText.toString()));
774 lastTooltip = tooltipText.toString();
781 private Point lastp = null;
786 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
788 public Point getToolTipLocation(MouseEvent event)
790 int x = event.getX(), w = getWidth();
791 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
794 if (!event.isShiftDown() || p == null)
796 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
797 event.getX() + wdth, event.getY() - 20) : null;
800 * TODO: try to modify position region is not obcured by tooltip
808 * Set status message in alignment panel
811 * aligned sequence object
815 * index of sequence in alignment
816 * @return position of res in sequence
818 int setStatusMessage(SequenceI sequence, int res, int seq)
821 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
822 + sequence.getName());
825 if (av.getAlignment().isNucleotide())
827 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
831 text.append(" Nucleotide: ");
836 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
839 text.append(" Residue: ");
845 pos = sequence.findPosition(res);
848 text.append(obj + " (" + pos + ")");
851 ap.alignFrame.statusBar.setText(text.toString());
862 public void mouseDragged(MouseEvent evt)
864 if (mouseWheelPressed)
866 int oldWidth = av.charWidth;
868 // Which is bigger, left-right or up-down?
869 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
870 .getX() - lastMousePress.getX()))
872 int fontSize = av.font.getSize();
874 if (evt.getY() < lastMousePress.getY())
878 else if (evt.getY() > lastMousePress.getY())
888 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
889 av.charWidth = oldWidth;
894 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
898 else if (evt.getX() > lastMousePress.getX())
903 ap.paintAlignment(false);
906 FontMetrics fm = getFontMetrics(av.getFont());
907 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
909 lastMousePress = evt.getPoint();
916 doMouseDraggedDefineMode(evt);
920 int res = findRes(evt);
927 if ((lastres == -1) || (lastres == res))
932 if ((res < av.getAlignment().getWidth()) && (res < lastres))
934 // dragLeft, delete gap
935 editSequence(false, false, res);
939 editSequence(true, false, res);
942 mouseDragging = true;
943 if (scrollThread != null)
945 scrollThread.setEvent(evt);
949 // TODO: Make it more clever than many booleans
950 synchronized void editSequence(boolean insertGap, boolean editSeq,
955 boolean fixedColumns = false;
956 SequenceGroup sg = av.getSelectionGroup();
958 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
960 // No group, but the sequence may represent a group
961 if (!groupEditing && av.hasHiddenRows())
963 if (av.isHiddenRepSequence(seq))
965 sg = av.getRepresentedSequences(seq);
970 StringBuffer message = new StringBuffer();
973 message.append("Edit group:");
974 if (editCommand == null)
976 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
981 message.append("Edit sequence: " + seq.getName());
982 String label = seq.getName();
983 if (label.length() > 10)
985 label = label.substring(0, 10);
987 if (editCommand == null)
989 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
995 message.append(" insert ");
999 message.append(" delete ");
1002 message.append(Math.abs(startres - lastres) + " gaps.");
1003 ap.alignFrame.statusBar.setText(message.toString());
1005 // Are we editing within a selection group?
1007 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1010 fixedColumns = true;
1012 // sg might be null as the user may only see 1 sequence,
1013 // but the sequence represents a group
1016 if (!av.isHiddenRepSequence(seq))
1021 sg = av.getRepresentedSequences(seq);
1024 fixedLeft = sg.getStartRes();
1025 fixedRight = sg.getEndRes();
1027 if ((startres < fixedLeft && lastres >= fixedLeft)
1028 || (startres >= fixedLeft && lastres < fixedLeft)
1029 || (startres > fixedRight && lastres <= fixedRight)
1030 || (startres <= fixedRight && lastres > fixedRight))
1036 if (fixedLeft > startres)
1038 fixedRight = fixedLeft - 1;
1041 else if (fixedRight < startres)
1043 fixedLeft = fixedRight;
1048 if (av.hasHiddenColumns())
1050 fixedColumns = true;
1051 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1052 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1054 if ((insertGap && startres > y1 && lastres < y1)
1055 || (!insertGap && startres < y2 && lastres > y2))
1061 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1062 // Selection spans a hidden region
1063 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1071 fixedRight = y2 - 1;
1078 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1079 int g, groupSize = vseqs.size();
1080 SequenceI[] groupSeqs = new SequenceI[groupSize];
1081 for (g = 0; g < groupSeqs.length; g++)
1083 groupSeqs[g] = vseqs.get(g);
1089 // If the user has selected the whole sequence, and is dragging to
1090 // the right, we can still extend the alignment and selectionGroup
1091 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1092 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1094 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1095 fixedRight = sg.getEndRes();
1098 // Is it valid with fixed columns??
1099 // Find the next gap before the end
1100 // of the visible region boundary
1101 boolean blank = false;
1102 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1106 for (g = 0; g < groupSize; g++)
1108 for (int j = 0; j < startres - lastres; j++)
1110 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1111 .getCharAt(fixedRight - j)))
1126 if (sg.getSize() == av.getAlignment().getHeight())
1128 if ((av.hasHiddenColumns() && startres < av
1129 .getColumnSelection().getHiddenBoundaryRight(startres)))
1135 int alWidth = av.getAlignment().getWidth();
1136 if (av.hasHiddenRows())
1138 int hwidth = av.getAlignment().getHiddenSequences()
1140 if (hwidth > alWidth)
1145 // We can still insert gaps if the selectionGroup
1146 // contains all the sequences
1147 sg.setEndRes(sg.getEndRes() + startres - lastres);
1148 fixedRight = alWidth + startres - lastres;
1159 else if (!insertGap)
1161 // / Are we able to delete?
1162 // ie are all columns blank?
1164 for (g = 0; g < groupSize; g++)
1166 for (int j = startres; j < lastres; j++)
1168 if (groupSeqs[g].getLength() <= j)
1173 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1175 // Not a gap, block edit not valid
1185 // dragging to the right
1186 if (fixedColumns && fixedRight != -1)
1188 for (int j = lastres; j < startres; j++)
1190 insertChar(j, groupSeqs, fixedRight);
1195 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs,
1196 startres, startres - lastres, av.getAlignment(), true);
1201 // dragging to the left
1202 if (fixedColumns && fixedRight != -1)
1204 for (int j = lastres; j > startres; j--)
1206 deleteChar(startres, groupSeqs, fixedRight);
1211 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs,
1212 startres, lastres - startres, av.getAlignment(), true);
1218 // ///Editing a single sequence///////////
1222 // dragging to the right
1223 if (fixedColumns && fixedRight != -1)
1225 for (int j = lastres; j < startres; j++)
1227 insertChar(j, new SequenceI[]
1228 { seq }, fixedRight);
1233 editCommand.appendEdit(Action.INSERT_GAP, new SequenceI[]
1234 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1241 // dragging to the left
1242 if (fixedColumns && fixedRight != -1)
1244 for (int j = lastres; j > startres; j--)
1246 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1251 deleteChar(startres, new SequenceI[]
1252 { seq }, fixedRight);
1257 // could be a keyboard edit trying to delete none gaps
1259 for (int m = startres; m < lastres; m++)
1261 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1270 editCommand.appendEdit(Action.DELETE_GAP,
1272 { seq }, startres, max, av.getAlignment(), true);
1277 {// insertGap==false AND editSeq==TRUE;
1278 if (fixedColumns && fixedRight != -1)
1280 for (int j = lastres; j < startres; j++)
1282 insertChar(j, new SequenceI[]
1283 { seq }, fixedRight);
1288 editCommand.appendEdit(Action.INSERT_NUC, new SequenceI[]
1289 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1296 seqCanvas.repaint();
1299 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1301 int blankColumn = fixedColumn;
1302 for (int s = 0; s < seq.length; s++)
1304 // Find the next gap before the end of the visible region boundary
1305 // If lastCol > j, theres a boundary after the gap insertion
1307 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1309 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1311 // Theres a space, so break and insert the gap
1316 if (blankColumn <= j)
1318 blankColumn = fixedColumn;
1324 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1325 av.getAlignment(), true);
1327 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1,
1328 av.getAlignment(), true);
1332 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1335 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1,
1336 av.getAlignment(), true);
1338 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1339 av.getAlignment(), true);
1349 public void mouseEntered(MouseEvent e)
1356 if (scrollThread != null)
1358 scrollThread.running = false;
1359 scrollThread = null;
1370 public void mouseExited(MouseEvent e)
1372 if (av.getWrapAlignment())
1379 scrollThread = new ScrollThread();
1384 public void mouseClicked(MouseEvent evt)
1386 SequenceGroup sg = null;
1387 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1388 if (evt.getClickCount() > 1)
1390 sg = av.getSelectionGroup();
1391 if (sg != null && sg.getSize() == 1
1392 && sg.getEndRes() - sg.getStartRes() < 2)
1394 av.setSelectionGroup(null);
1397 SequenceFeature[] features = findFeaturesAtRes(
1398 sequence.getDatasetSequence(),
1399 sequence.findPosition(findRes(evt)));
1401 if (features != null && features.length > 0)
1403 SearchResults highlight = new SearchResults();
1404 highlight.addResult(sequence, features[0].getBegin(),
1405 features[0].getEnd());
1406 seqCanvas.highlightSearchResults(highlight);
1408 if (features != null && features.length > 0)
1410 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1411 { sequence }, features, false, ap);
1413 seqCanvas.highlightSearchResults(null);
1419 public void mouseWheelMoved(MouseWheelEvent e)
1422 if (e.getWheelRotation() > 0)
1424 if (e.isShiftDown())
1426 ap.scrollRight(true);
1436 if (e.isShiftDown())
1438 ap.scrollRight(false);
1445 // TODO Update tooltip for new position.
1454 public void doMousePressedDefineMode(MouseEvent evt)
1456 int res = findRes(evt);
1457 int seq = findSeq(evt);
1460 startWrapBlock = wrappedBlock;
1462 if (av.wrapAlignment && seq > av.getAlignment().getHeight())
1464 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1465 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1466 MessageManager.getString("label.wrapped_view_no_edit"),
1467 JOptionPane.WARNING_MESSAGE);
1471 if (seq < 0 || res < 0)
1476 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1478 if ((sequence == null) || (res > sequence.getLength()))
1483 stretchGroup = av.getSelectionGroup();
1485 if (stretchGroup == null)
1487 stretchGroup = av.getAlignment().findGroup(sequence);
1489 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1490 && (res < stretchGroup.getEndRes()))
1492 av.setSelectionGroup(stretchGroup);
1496 stretchGroup = null;
1499 else if (!stretchGroup.getSequences(null).contains(sequence)
1500 || (stretchGroup.getStartRes() > res)
1501 || (stretchGroup.getEndRes() < res))
1503 stretchGroup = null;
1505 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1507 if (allGroups != null)
1509 for (int i = 0; i < allGroups.length; i++)
1511 if ((allGroups[i].getStartRes() <= res)
1512 && (allGroups[i].getEndRes() >= res))
1514 stretchGroup = allGroups[i];
1520 av.setSelectionGroup(stretchGroup);
1524 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1526 SequenceFeature[] allFeatures = findFeaturesAtRes(
1527 sequence.getDatasetSequence(), sequence.findPosition(res));
1528 Vector links = new Vector();
1529 for (int i = 0; i < allFeatures.length; i++)
1531 if (allFeatures[i].links != null)
1533 for (int j = 0; j < allFeatures[i].links.size(); j++)
1535 links.addElement(allFeatures[i].links.elementAt(j));
1540 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1541 pop.show(this, evt.getX(), evt.getY());
1547 seqCanvas.cursorX = findRes(evt);
1548 seqCanvas.cursorY = findSeq(evt);
1549 seqCanvas.repaint();
1553 if (stretchGroup == null)
1555 // Only if left mouse button do we want to change group sizes
1557 // define a new group here
1558 SequenceGroup sg = new SequenceGroup();
1559 sg.setStartRes(res);
1561 sg.addSequence(sequence, false);
1562 av.setSelectionGroup(sg);
1566 if (av.getConservationSelected())
1568 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1572 if (av.getAbovePIDThreshold())
1574 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1577 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1579 // Edit end res position of selected group
1580 changeEndRes = true;
1582 else if ((stretchGroup != null)
1583 && (stretchGroup.getStartRes() == res))
1585 // Edit end res position of selected group
1586 changeStartRes = true;
1588 stretchGroup.getWidth();
1591 seqCanvas.repaint();
1600 public void doMouseReleasedDefineMode(MouseEvent evt)
1602 if (stretchGroup == null)
1607 stretchGroup.recalcConservation(); // always do this - annotation has own
1609 if (stretchGroup.cs != null)
1611 stretchGroup.cs.alignmentChanged(stretchGroup,
1612 av.getHiddenRepSequences());
1614 if (stretchGroup.cs.conservationApplied())
1616 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1617 stretchGroup.getName());
1621 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1622 stretchGroup.getName());
1625 PaintRefresher.Refresh(this, av.getSequenceSetId());
1626 ap.paintAlignment(true);
1628 changeEndRes = false;
1629 changeStartRes = false;
1630 stretchGroup = null;
1640 public void doMouseDraggedDefineMode(MouseEvent evt)
1642 int res = findRes(evt);
1643 int y = findSeq(evt);
1645 if (wrappedBlock != startWrapBlock)
1650 if (stretchGroup == null)
1655 if (res >= av.getAlignment().getWidth())
1657 res = av.getAlignment().getWidth() - 1;
1660 if (stretchGroup.getEndRes() == res)
1662 // Edit end res position of selected group
1663 changeEndRes = true;
1665 else if (stretchGroup.getStartRes() == res)
1667 // Edit start res position of selected group
1668 changeStartRes = true;
1671 if (res < av.getStartRes())
1673 res = av.getStartRes();
1678 if (res > (stretchGroup.getStartRes() - 1))
1680 stretchGroup.setEndRes(res);
1683 else if (changeStartRes)
1685 if (res < (stretchGroup.getEndRes() + 1))
1687 stretchGroup.setStartRes(res);
1691 int dragDirection = 0;
1697 else if (y < oldSeq)
1702 while ((y != oldSeq) && (oldSeq > -1)
1703 && (y < av.getAlignment().getHeight()))
1705 // This routine ensures we don't skip any sequences, as the
1706 // selection is quite slow.
1707 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1709 oldSeq += dragDirection;
1716 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1718 if (stretchGroup.getSequences(null).contains(nextSeq))
1720 stretchGroup.deleteSequence(seq, false);
1726 stretchGroup.addSequence(seq, false);
1729 stretchGroup.addSequence(nextSeq, false);
1738 mouseDragging = true;
1740 if (scrollThread != null)
1742 scrollThread.setEvent(evt);
1745 seqCanvas.repaint();
1748 void scrollCanvas(MouseEvent evt)
1752 if (scrollThread != null)
1754 scrollThread.running = false;
1755 scrollThread = null;
1757 mouseDragging = false;
1761 if (scrollThread == null)
1763 scrollThread = new ScrollThread();
1766 mouseDragging = true;
1767 scrollThread.setEvent(evt);
1772 // this class allows scrolling off the bottom of the visible alignment
1773 class ScrollThread extends Thread
1777 boolean running = false;
1779 public ScrollThread()
1784 public void setEvent(MouseEvent e)
1789 public void stopScrolling()
1803 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1805 running = ap.scrollUp(true);
1808 if (mouseDragging && (evt.getY() >= getHeight())
1809 && (av.getAlignment().getHeight() > av.getEndSeq()))
1811 running = ap.scrollUp(false);
1814 if (mouseDragging && (evt.getX() < 0))
1816 running = ap.scrollRight(false);
1818 else if (mouseDragging && (evt.getX() >= getWidth()))
1820 running = ap.scrollRight(true);
1827 } catch (Exception ex)
1835 * modify current selection according to a received message.
1838 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1839 SelectionSource source)
1841 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1842 // handles selection messages...
1843 // TODO: extend config options to allow user to control if selections may be
1844 // shared between viewports.
1846 || !av.followSelection
1847 || (av.isSelectionGroupChanged(false) || av
1848 .isColSelChanged(false))
1849 || (source instanceof AlignViewport && ((AlignViewport) source)
1850 .getSequenceSetId().equals(av.getSequenceSetId())))
1854 // do we want to thread this ? (contention with seqsel and colsel locks, I
1856 // rules are: colsel is copied if there is a real intersection between
1857 // sequence selection
1858 boolean repaint = false, copycolsel = true;
1859 // if (!av.isSelectionGroupChanged(false))
1861 SequenceGroup sgroup = null;
1862 if (seqsel != null && seqsel.getSize() > 0)
1864 if (av.getAlignment() == null)
1866 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1867 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1868 + " 's alignment is NULL! returning immediatly.");
1871 sgroup = seqsel.intersect(av.getAlignment(),
1872 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1873 if ((sgroup == null || sgroup.getSize() == 0)
1874 || (colsel == null || colsel.size() == 0))
1876 // don't copy columns if the region didn't intersect.
1880 if (sgroup != null && sgroup.getSize() > 0)
1882 av.setSelectionGroup(sgroup);
1886 av.setSelectionGroup(null);
1888 av.isSelectionGroupChanged(true);
1893 // the current selection is unset or from a previous message
1894 // so import the new colsel.
1895 if (colsel == null || colsel.size() == 0)
1897 if (av.getColumnSelection() != null)
1899 av.getColumnSelection().clear();
1905 // TODO: shift colSel according to the intersecting sequences
1906 if (av.getColumnSelection() == null)
1908 av.setColumnSelection(new ColumnSelection(colsel));
1912 av.getColumnSelection().setElementsFrom(colsel);
1915 av.isColSelChanged(true);
1919 && av.hasHiddenColumns()
1920 && (av.getColumnSelection() == null || av.getColumnSelection()
1921 .getHiddenColumns() == null))
1923 System.err.println("Bad things");
1927 // probably finessing with multiple redraws here
1928 PaintRefresher.Refresh(this, av.getSequenceSetId());
1929 // ap.paintAlignment(false);