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.featuresDisplayed == null
255 || !av.featuresDisplayed.containsKey(features[i].getType()))
260 if (features[i].featureGroup != null
261 && seqCanvas.fr.featureGroups != null
262 && seqCanvas.fr.featureGroups
263 .containsKey(features[i].featureGroup)
264 && !((Boolean) seqCanvas.fr.featureGroups
265 .get(features[i].featureGroup)).booleanValue())
270 if ((features[i].getBegin() <= res)
271 && (features[i].getEnd() >= res))
273 tmp.addElement(features[i]);
278 features = new SequenceFeature[tmp.size()];
279 tmp.copyInto(features);
286 if (editCommand != null && editCommand.getSize() > 0)
288 ap.alignFrame.addHistoryItem(editCommand);
289 av.firePropertyChange("alignment", null, av.getAlignment()
296 groupEditing = false;
304 seqCanvas.cursorY = getKeyboardNo1() - 1;
308 void setCursorColumn()
310 seqCanvas.cursorX = getKeyboardNo1() - 1;
314 void setCursorRowAndColumn()
316 if (keyboardNo2 == null)
318 keyboardNo2 = new StringBuffer();
322 seqCanvas.cursorX = getKeyboardNo1() - 1;
323 seqCanvas.cursorY = getKeyboardNo2() - 1;
328 void setCursorPosition()
330 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
332 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
336 void moveCursor(int dx, int dy)
338 seqCanvas.cursorX += dx;
339 seqCanvas.cursorY += dy;
340 if (av.hasHiddenColumns()
341 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
343 int original = seqCanvas.cursorX - dx;
344 int maxWidth = av.getAlignment().getWidth();
346 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
347 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
349 seqCanvas.cursorX += dx;
352 if (seqCanvas.cursorX >= maxWidth
353 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
355 seqCanvas.cursorX = original;
362 void scrollToVisible()
364 if (seqCanvas.cursorX < 0)
366 seqCanvas.cursorX = 0;
368 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
370 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
373 if (seqCanvas.cursorY < 0)
375 seqCanvas.cursorY = 0;
377 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
379 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
383 if (av.wrapAlignment)
385 ap.scrollToWrappedVisible(seqCanvas.cursorX);
389 while (seqCanvas.cursorY < av.startSeq)
393 while (seqCanvas.cursorY + 1 > av.endSeq)
397 if (!av.wrapAlignment)
399 while (seqCanvas.cursorX < av.getColumnSelection()
400 .adjustForHiddenColumns(av.startRes))
402 if (!ap.scrollRight(false))
407 while (seqCanvas.cursorX > av.getColumnSelection()
408 .adjustForHiddenColumns(av.endRes))
410 if (!ap.scrollRight(true))
417 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
418 seqCanvas.cursorX, seqCanvas.cursorY);
423 void setSelectionAreaAtCursor(boolean topLeft)
425 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
427 if (av.getSelectionGroup() != null)
429 SequenceGroup sg = av.getSelectionGroup();
430 // Find the top and bottom of this group
431 int min = av.getAlignment().getHeight(), max = 0;
432 for (int i = 0; i < sg.getSize(); i++)
434 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
449 sg.setStartRes(seqCanvas.cursorX);
450 if (sg.getEndRes() < seqCanvas.cursorX)
452 sg.setEndRes(seqCanvas.cursorX);
455 min = seqCanvas.cursorY;
459 sg.setEndRes(seqCanvas.cursorX);
460 if (sg.getStartRes() > seqCanvas.cursorX)
462 sg.setStartRes(seqCanvas.cursorX);
465 max = seqCanvas.cursorY + 1;
470 // Only the user can do this
471 av.setSelectionGroup(null);
475 // Now add any sequences between min and max
476 sg.getSequences(null).clear();
477 for (int i = min; i < max; i++)
479 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
484 if (av.getSelectionGroup() == null)
486 SequenceGroup sg = new SequenceGroup();
487 sg.setStartRes(seqCanvas.cursorX);
488 sg.setEndRes(seqCanvas.cursorX);
489 sg.addSequence(sequence, false);
490 av.setSelectionGroup(sg);
493 ap.paintAlignment(false);
497 void insertGapAtCursor(boolean group)
499 groupEditing = group;
500 startseq = seqCanvas.cursorY;
501 lastres = seqCanvas.cursorX;
502 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
506 void deleteGapAtCursor(boolean group)
508 groupEditing = group;
509 startseq = seqCanvas.cursorY;
510 lastres = seqCanvas.cursorX + getKeyboardNo1();
511 editSequence(false, false, seqCanvas.cursorX);
515 void insertNucAtCursor(boolean group, String nuc)
517 groupEditing = group;
518 startseq = seqCanvas.cursorY;
519 lastres = seqCanvas.cursorX;
520 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
524 void numberPressed(char value)
526 if (keyboardNo1 == null)
528 keyboardNo1 = new StringBuffer();
531 if (keyboardNo2 != null)
533 keyboardNo2.append(value);
537 keyboardNo1.append(value);
545 if (keyboardNo1 != null)
547 int value = Integer.parseInt(keyboardNo1.toString());
551 } catch (Exception x)
562 if (keyboardNo2 != null)
564 int value = Integer.parseInt(keyboardNo2.toString());
568 } catch (Exception x)
582 public void mouseReleased(MouseEvent evt)
584 mouseDragging = false;
585 mouseWheelPressed = false;
589 doMouseReleasedDefineMode(evt);
603 public void mousePressed(MouseEvent evt)
605 lastMousePress = evt.getPoint();
607 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
609 mouseWheelPressed = true;
613 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
615 if (evt.isAltDown() || evt.isControlDown())
623 doMousePressedDefineMode(evt);
627 int seq = findSeq(evt);
628 int res = findRes(evt);
630 if (seq < 0 || res < 0)
635 if ((seq < av.getAlignment().getHeight())
636 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
653 public void mouseOverSequence(SequenceI sequence, int index, int pos)
655 String tmp = sequence.hashCode() + " " + index + " " + pos;
657 if (lastMessage == null || !lastMessage.equals(tmp))
659 // System.err.println("mouseOver Sequence: "+tmp);
660 ssm.mouseOverSequence(sequence, index, pos, av);
666 public void highlightSequence(SearchResults results)
668 if (av.followHighlight)
670 if (ap.scrollToPosition(results, false))
672 seqCanvas.revalidate();
675 seqCanvas.highlightSearchResults(results);
679 public void updateColours(SequenceI seq, int index)
681 System.out.println("update the seqPanel colours");
692 public void mouseMoved(MouseEvent evt)
696 // This is because MacOSX creates a mouseMoved
697 // If control is down, other platforms will not.
701 int res = findRes(evt);
702 int seq = findSeq(evt);
704 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
709 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
711 if (res >= sequence.getLength())
716 pos = setStatusMessage(sequence, res, seq);
717 if (ssm != null && pos > -1)
719 mouseOverSequence(sequence, res, pos);
722 tooltipText.setLength(6); // Cuts the buffer back to <html>
724 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
727 for (int g = 0; g < groups.length; g++)
729 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
731 if (tooltipText.length() > 6)
733 tooltipText.append("<br>");
736 if (!groups[g].getName().startsWith("JTreeGroup")
737 && !groups[g].getName().startsWith("JGroup"))
739 tooltipText.append(groups[g].getName());
742 if (groups[g].getDescription() != null)
744 tooltipText.append(": " + groups[g].getDescription());
750 // use aa to see if the mouse pointer is on a
751 if (av.showSequenceFeatures)
754 SequenceFeature[] features = findFeaturesAtRes(
755 sequence.getDatasetSequence(),
756 rpos = sequence.findPosition(res));
757 seqARep.appendFeatures(tooltipText, rpos, features,
758 this.ap.seqPanel.seqCanvas.fr.minmax);
760 if (tooltipText.length() == 6) // <html></html>
762 setToolTipText(null);
767 tooltipText.append("</html>");
768 if (lastTooltip == null
769 || !lastTooltip.equals(tooltipText.toString()))
771 setToolTipText(JvSwingUtils.wrapTooltip(true,
772 tooltipText.toString()));
773 lastTooltip = tooltipText.toString();
780 private Point lastp = null;
785 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
787 public Point getToolTipLocation(MouseEvent event)
789 int x = event.getX(), w = getWidth();
790 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
793 if (!event.isShiftDown() || p == null)
795 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
796 event.getX() + wdth, event.getY() - 20) : null;
799 * TODO: try to modify position region is not obcured by tooltip
807 * Set status message in alignment panel
810 * aligned sequence object
814 * index of sequence in alignment
815 * @return position of res in sequence
817 int setStatusMessage(SequenceI sequence, int res, int seq)
820 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
821 + sequence.getName());
824 if (av.getAlignment().isNucleotide())
826 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
830 text.append(" Nucleotide: ");
835 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
838 text.append(" Residue: ");
844 pos = sequence.findPosition(res);
847 text.append(obj + " (" + pos + ")");
850 ap.alignFrame.statusBar.setText(text.toString());
861 public void mouseDragged(MouseEvent evt)
863 if (mouseWheelPressed)
865 int oldWidth = av.charWidth;
867 // Which is bigger, left-right or up-down?
868 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
869 .getX() - lastMousePress.getX()))
871 int fontSize = av.font.getSize();
873 if (evt.getY() < lastMousePress.getY())
877 else if (evt.getY() > lastMousePress.getY())
887 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
888 av.charWidth = oldWidth;
893 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
897 else if (evt.getX() > lastMousePress.getX())
902 ap.paintAlignment(false);
905 FontMetrics fm = getFontMetrics(av.getFont());
906 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
908 lastMousePress = evt.getPoint();
915 doMouseDraggedDefineMode(evt);
919 int res = findRes(evt);
926 if ((lastres == -1) || (lastres == res))
931 if ((res < av.getAlignment().getWidth()) && (res < lastres))
933 // dragLeft, delete gap
934 editSequence(false, false, res);
938 editSequence(true, false, res);
941 mouseDragging = true;
942 if (scrollThread != null)
944 scrollThread.setEvent(evt);
948 // TODO: Make it more clever than many booleans
949 synchronized void editSequence(boolean insertGap, boolean editSeq,
954 boolean fixedColumns = false;
955 SequenceGroup sg = av.getSelectionGroup();
957 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
959 // No group, but the sequence may represent a group
960 if (!groupEditing && av.hasHiddenRows())
962 if (av.isHiddenRepSequence(seq))
964 sg = av.getRepresentedSequences(seq);
969 StringBuffer message = new StringBuffer();
972 message.append("Edit group:");
973 if (editCommand == null)
975 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
980 message.append("Edit sequence: " + seq.getName());
981 String label = seq.getName();
982 if (label.length() > 10)
984 label = label.substring(0, 10);
986 if (editCommand == null)
988 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
994 message.append(" insert ");
998 message.append(" delete ");
1001 message.append(Math.abs(startres - lastres) + " gaps.");
1002 ap.alignFrame.statusBar.setText(message.toString());
1004 // Are we editing within a selection group?
1006 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1009 fixedColumns = true;
1011 // sg might be null as the user may only see 1 sequence,
1012 // but the sequence represents a group
1015 if (!av.isHiddenRepSequence(seq))
1020 sg = av.getRepresentedSequences(seq);
1023 fixedLeft = sg.getStartRes();
1024 fixedRight = sg.getEndRes();
1026 if ((startres < fixedLeft && lastres >= fixedLeft)
1027 || (startres >= fixedLeft && lastres < fixedLeft)
1028 || (startres > fixedRight && lastres <= fixedRight)
1029 || (startres <= fixedRight && lastres > fixedRight))
1035 if (fixedLeft > startres)
1037 fixedRight = fixedLeft - 1;
1040 else if (fixedRight < startres)
1042 fixedLeft = fixedRight;
1047 if (av.hasHiddenColumns())
1049 fixedColumns = true;
1050 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1051 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1053 if ((insertGap && startres > y1 && lastres < y1)
1054 || (!insertGap && startres < y2 && lastres > y2))
1060 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1061 // Selection spans a hidden region
1062 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1070 fixedRight = y2 - 1;
1077 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1078 int g, groupSize = vseqs.size();
1079 SequenceI[] groupSeqs = new SequenceI[groupSize];
1080 for (g = 0; g < groupSeqs.length; g++)
1082 groupSeqs[g] = vseqs.get(g);
1088 // If the user has selected the whole sequence, and is dragging to
1089 // the right, we can still extend the alignment and selectionGroup
1090 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1091 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1093 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1094 fixedRight = sg.getEndRes();
1097 // Is it valid with fixed columns??
1098 // Find the next gap before the end
1099 // of the visible region boundary
1100 boolean blank = false;
1101 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1105 for (g = 0; g < groupSize; g++)
1107 for (int j = 0; j < startres - lastres; j++)
1109 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1110 .getCharAt(fixedRight - j)))
1125 if (sg.getSize() == av.getAlignment().getHeight())
1127 if ((av.hasHiddenColumns() && startres < av
1128 .getColumnSelection().getHiddenBoundaryRight(startres)))
1134 int alWidth = av.getAlignment().getWidth();
1135 if (av.hasHiddenRows())
1137 int hwidth = av.getAlignment().getHiddenSequences()
1139 if (hwidth > alWidth)
1144 // We can still insert gaps if the selectionGroup
1145 // contains all the sequences
1146 sg.setEndRes(sg.getEndRes() + startres - lastres);
1147 fixedRight = alWidth + startres - lastres;
1158 else if (!insertGap)
1160 // / Are we able to delete?
1161 // ie are all columns blank?
1163 for (g = 0; g < groupSize; g++)
1165 for (int j = startres; j < lastres; j++)
1167 if (groupSeqs[g].getLength() <= j)
1172 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1174 // Not a gap, block edit not valid
1184 // dragging to the right
1185 if (fixedColumns && fixedRight != -1)
1187 for (int j = lastres; j < startres; j++)
1189 insertChar(j, groupSeqs, fixedRight);
1194 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs,
1195 startres, startres - lastres, av.getAlignment(), true);
1200 // dragging to the left
1201 if (fixedColumns && fixedRight != -1)
1203 for (int j = lastres; j > startres; j--)
1205 deleteChar(startres, groupSeqs, fixedRight);
1210 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs,
1211 startres, lastres - startres, av.getAlignment(), true);
1217 // ///Editing a single sequence///////////
1221 // dragging to the right
1222 if (fixedColumns && fixedRight != -1)
1224 for (int j = lastres; j < startres; j++)
1226 insertChar(j, new SequenceI[]
1227 { seq }, fixedRight);
1232 editCommand.appendEdit(Action.INSERT_GAP, new SequenceI[]
1233 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1240 // dragging to the left
1241 if (fixedColumns && fixedRight != -1)
1243 for (int j = lastres; j > startres; j--)
1245 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1250 deleteChar(startres, new SequenceI[]
1251 { seq }, fixedRight);
1256 // could be a keyboard edit trying to delete none gaps
1258 for (int m = startres; m < lastres; m++)
1260 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1269 editCommand.appendEdit(Action.DELETE_GAP,
1271 { seq }, startres, max, av.getAlignment(), true);
1276 {// insertGap==false AND editSeq==TRUE;
1277 if (fixedColumns && fixedRight != -1)
1279 for (int j = lastres; j < startres; j++)
1281 insertChar(j, new SequenceI[]
1282 { seq }, fixedRight);
1287 editCommand.appendEdit(Action.INSERT_NUC, new SequenceI[]
1288 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1295 seqCanvas.repaint();
1298 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1300 int blankColumn = fixedColumn;
1301 for (int s = 0; s < seq.length; s++)
1303 // Find the next gap before the end of the visible region boundary
1304 // If lastCol > j, theres a boundary after the gap insertion
1306 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1308 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1310 // Theres a space, so break and insert the gap
1315 if (blankColumn <= j)
1317 blankColumn = fixedColumn;
1323 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1324 av.getAlignment(), true);
1326 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1,
1327 av.getAlignment(), true);
1331 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1334 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1,
1335 av.getAlignment(), true);
1337 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1338 av.getAlignment(), true);
1348 public void mouseEntered(MouseEvent e)
1355 if (scrollThread != null)
1357 scrollThread.running = false;
1358 scrollThread = null;
1369 public void mouseExited(MouseEvent e)
1371 if (av.getWrapAlignment())
1378 scrollThread = new ScrollThread();
1383 public void mouseClicked(MouseEvent evt)
1385 SequenceGroup sg = null;
1386 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1387 if (evt.getClickCount() > 1)
1389 sg = av.getSelectionGroup();
1390 if (sg != null && sg.getSize() == 1
1391 && sg.getEndRes() - sg.getStartRes() < 2)
1393 av.setSelectionGroup(null);
1396 SequenceFeature[] features = findFeaturesAtRes(
1397 sequence.getDatasetSequence(),
1398 sequence.findPosition(findRes(evt)));
1400 if (features != null && features.length > 0)
1402 SearchResults highlight = new SearchResults();
1403 highlight.addResult(sequence, features[0].getBegin(),
1404 features[0].getEnd());
1405 seqCanvas.highlightSearchResults(highlight);
1407 if (features != null && features.length > 0)
1409 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1410 { sequence }, features, false, ap);
1412 seqCanvas.highlightSearchResults(null);
1418 public void mouseWheelMoved(MouseWheelEvent e)
1421 if (e.getWheelRotation() > 0)
1423 if (e.isShiftDown())
1425 ap.scrollRight(true);
1435 if (e.isShiftDown())
1437 ap.scrollRight(false);
1444 // TODO Update tooltip for new position.
1453 public void doMousePressedDefineMode(MouseEvent evt)
1455 int res = findRes(evt);
1456 int seq = findSeq(evt);
1459 startWrapBlock = wrappedBlock;
1461 if (av.wrapAlignment && seq > av.getAlignment().getHeight())
1463 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1464 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1465 MessageManager.getString("label.wrapped_view_no_edit"),
1466 JOptionPane.WARNING_MESSAGE);
1470 if (seq < 0 || res < 0)
1475 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1477 if ((sequence == null) || (res > sequence.getLength()))
1482 stretchGroup = av.getSelectionGroup();
1484 if (stretchGroup == null)
1486 stretchGroup = av.getAlignment().findGroup(sequence);
1488 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1489 && (res < stretchGroup.getEndRes()))
1491 av.setSelectionGroup(stretchGroup);
1495 stretchGroup = null;
1498 else if (!stretchGroup.getSequences(null).contains(sequence)
1499 || (stretchGroup.getStartRes() > res)
1500 || (stretchGroup.getEndRes() < res))
1502 stretchGroup = null;
1504 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1506 if (allGroups != null)
1508 for (int i = 0; i < allGroups.length; i++)
1510 if ((allGroups[i].getStartRes() <= res)
1511 && (allGroups[i].getEndRes() >= res))
1513 stretchGroup = allGroups[i];
1519 av.setSelectionGroup(stretchGroup);
1523 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1525 SequenceFeature[] allFeatures = findFeaturesAtRes(
1526 sequence.getDatasetSequence(), sequence.findPosition(res));
1527 Vector links = new Vector();
1528 for (int i = 0; i < allFeatures.length; i++)
1530 if (allFeatures[i].links != null)
1532 for (int j = 0; j < allFeatures[i].links.size(); j++)
1534 links.addElement(allFeatures[i].links.elementAt(j));
1539 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1540 pop.show(this, evt.getX(), evt.getY());
1546 seqCanvas.cursorX = findRes(evt);
1547 seqCanvas.cursorY = findSeq(evt);
1548 seqCanvas.repaint();
1552 if (stretchGroup == null)
1554 // Only if left mouse button do we want to change group sizes
1556 // define a new group here
1557 SequenceGroup sg = new SequenceGroup();
1558 sg.setStartRes(res);
1560 sg.addSequence(sequence, false);
1561 av.setSelectionGroup(sg);
1565 if (av.getConservationSelected())
1567 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1571 if (av.getAbovePIDThreshold())
1573 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1576 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1578 // Edit end res position of selected group
1579 changeEndRes = true;
1581 else if ((stretchGroup != null)
1582 && (stretchGroup.getStartRes() == res))
1584 // Edit end res position of selected group
1585 changeStartRes = true;
1587 stretchGroup.getWidth();
1590 seqCanvas.repaint();
1599 public void doMouseReleasedDefineMode(MouseEvent evt)
1601 if (stretchGroup == null)
1606 stretchGroup.recalcConservation(); // always do this - annotation has own
1608 if (stretchGroup.cs != null)
1610 stretchGroup.cs.alignmentChanged(stretchGroup,
1611 av.getHiddenRepSequences());
1613 if (stretchGroup.cs.conservationApplied())
1615 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1616 stretchGroup.getName());
1620 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1621 stretchGroup.getName());
1624 PaintRefresher.Refresh(this, av.getSequenceSetId());
1625 ap.paintAlignment(true);
1627 changeEndRes = false;
1628 changeStartRes = false;
1629 stretchGroup = null;
1639 public void doMouseDraggedDefineMode(MouseEvent evt)
1641 int res = findRes(evt);
1642 int y = findSeq(evt);
1644 if (wrappedBlock != startWrapBlock)
1649 if (stretchGroup == null)
1654 if (res >= av.getAlignment().getWidth())
1656 res = av.getAlignment().getWidth() - 1;
1659 if (stretchGroup.getEndRes() == res)
1661 // Edit end res position of selected group
1662 changeEndRes = true;
1664 else if (stretchGroup.getStartRes() == res)
1666 // Edit start res position of selected group
1667 changeStartRes = true;
1670 if (res < av.getStartRes())
1672 res = av.getStartRes();
1677 if (res > (stretchGroup.getStartRes() - 1))
1679 stretchGroup.setEndRes(res);
1682 else if (changeStartRes)
1684 if (res < (stretchGroup.getEndRes() + 1))
1686 stretchGroup.setStartRes(res);
1690 int dragDirection = 0;
1696 else if (y < oldSeq)
1701 while ((y != oldSeq) && (oldSeq > -1)
1702 && (y < av.getAlignment().getHeight()))
1704 // This routine ensures we don't skip any sequences, as the
1705 // selection is quite slow.
1706 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1708 oldSeq += dragDirection;
1715 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1717 if (stretchGroup.getSequences(null).contains(nextSeq))
1719 stretchGroup.deleteSequence(seq, false);
1725 stretchGroup.addSequence(seq, false);
1728 stretchGroup.addSequence(nextSeq, false);
1737 mouseDragging = true;
1739 if (scrollThread != null)
1741 scrollThread.setEvent(evt);
1744 seqCanvas.repaint();
1747 void scrollCanvas(MouseEvent evt)
1751 if (scrollThread != null)
1753 scrollThread.running = false;
1754 scrollThread = null;
1756 mouseDragging = false;
1760 if (scrollThread == null)
1762 scrollThread = new ScrollThread();
1765 mouseDragging = true;
1766 scrollThread.setEvent(evt);
1771 // this class allows scrolling off the bottom of the visible alignment
1772 class ScrollThread extends Thread
1776 boolean running = false;
1778 public ScrollThread()
1783 public void setEvent(MouseEvent e)
1788 public void stopScrolling()
1802 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1804 running = ap.scrollUp(true);
1807 if (mouseDragging && (evt.getY() >= getHeight())
1808 && (av.getAlignment().getHeight() > av.getEndSeq()))
1810 running = ap.scrollUp(false);
1813 if (mouseDragging && (evt.getX() < 0))
1815 running = ap.scrollRight(false);
1817 else if (mouseDragging && (evt.getX() >= getWidth()))
1819 running = ap.scrollRight(true);
1826 } catch (Exception ex)
1834 * modify current selection according to a received message.
1837 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1838 SelectionSource source)
1840 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1841 // handles selection messages...
1842 // TODO: extend config options to allow user to control if selections may be
1843 // shared between viewports.
1845 || !av.followSelection
1846 || (av.isSelectionGroupChanged(false) || av
1847 .isColSelChanged(false))
1848 || (source instanceof AlignViewport && ((AlignViewport) source)
1849 .getSequenceSetId().equals(av.getSequenceSetId())))
1853 // do we want to thread this ? (contention with seqsel and colsel locks, I
1855 // rules are: colsel is copied if there is a real intersection between
1856 // sequence selection
1857 boolean repaint = false, copycolsel = true;
1858 // if (!av.isSelectionGroupChanged(false))
1860 SequenceGroup sgroup = null;
1861 if (seqsel != null && seqsel.getSize() > 0)
1863 if (av.getAlignment() == null)
1865 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1866 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1867 + " 's alignment is NULL! returning immediatly.");
1870 sgroup = seqsel.intersect(av.getAlignment(),
1871 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1872 if ((sgroup == null || sgroup.getSize() == 0)
1873 || (colsel == null || colsel.size() == 0))
1875 // don't copy columns if the region didn't intersect.
1879 if (sgroup != null && sgroup.getSize() > 0)
1881 av.setSelectionGroup(sgroup);
1885 av.setSelectionGroup(null);
1887 av.isSelectionGroupChanged(true);
1892 // the current selection is unset or from a previous message
1893 // so import the new colsel.
1894 if (colsel == null || colsel.size() == 0)
1896 if (av.getColumnSelection() != null)
1898 av.getColumnSelection().clear();
1904 // TODO: shift colSel according to the intersecting sequences
1905 if (av.getColumnSelection() == null)
1907 av.setColumnSelection(new ColumnSelection(colsel));
1911 av.getColumnSelection().setElementsFrom(colsel);
1914 av.isColSelChanged(true);
1918 && av.hasHiddenColumns()
1919 && (av.getColumnSelection() == null || av.getColumnSelection()
1920 .getHiddenColumns() == null))
1922 System.err.println("Bad things");
1926 // probably finessing with multiple redraws here
1927 PaintRefresher.Refresh(this, av.getSequenceSetId());
1928 // ap.paintAlignment(false);