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.
21 package jalview.appletgui;
26 import java.awt.event.*;
28 import jalview.commands.*;
29 import jalview.datamodel.*;
30 import jalview.schemes.*;
31 import jalview.structure.SelectionSource;
32 import jalview.structure.SequenceListener;
33 import jalview.structure.StructureSelectionManager;
35 public class SeqPanel extends Panel implements MouseMotionListener,
36 MouseListener, SequenceListener
39 public SeqCanvas seqCanvas;
41 public AlignmentPanel ap;
43 protected int lastres;
45 protected int startseq;
47 protected AlignViewport av;
49 // if character is inserted or deleted, we will need to recalculate the
51 boolean seqEditOccurred = false;
53 ScrollThread scrollThread = null;
55 boolean mouseDragging = false;
57 boolean editingSeqs = false;
59 boolean groupEditing = false;
63 boolean changeEndSeq = false;
65 boolean changeStartSeq = false;
67 boolean changeEndRes = false;
69 boolean changeStartRes = false;
71 SequenceGroup stretchGroup = null;
73 StringBuffer keyboardNo1;
75 StringBuffer keyboardNo2;
77 boolean mouseWheelPressed = false;
81 EditCommand editCommand;
83 StructureSelectionManager ssm;
85 public SeqPanel(AlignViewport avp, AlignmentPanel p)
89 seqCanvas = new SeqCanvas(avp);
90 setLayout(new BorderLayout());
95 seqCanvas.addMouseMotionListener(this);
96 seqCanvas.addMouseListener(this);
97 ssm = StructureSelectionManager.getStructureSelectionManager(av.applet);
98 ssm.addStructureViewerListener(this);
105 if (editCommand != null && editCommand.getSize() > 0)
107 ap.alignFrame.addHistoryItem(editCommand);
108 av.firePropertyChange("alignment", null, av.getAlignment()
115 groupEditing = false;
123 seqCanvas.cursorY = getKeyboardNo1() - 1;
127 void setCursorColumn()
129 seqCanvas.cursorX = getKeyboardNo1() - 1;
133 void setCursorRowAndColumn()
135 if (keyboardNo2 == null)
137 keyboardNo2 = new StringBuffer();
141 seqCanvas.cursorX = getKeyboardNo1() - 1;
142 seqCanvas.cursorY = getKeyboardNo2() - 1;
147 void setCursorPosition()
149 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
152 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
156 void moveCursor(int dx, int dy)
158 seqCanvas.cursorX += dx;
159 seqCanvas.cursorY += dy;
160 if (av.hasHiddenColumns()
161 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
163 int original = seqCanvas.cursorX - dx;
164 int maxWidth = av.getAlignment().getWidth();
166 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
167 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
169 seqCanvas.cursorX += dx;
172 if (seqCanvas.cursorX >= maxWidth
173 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
175 seqCanvas.cursorX = original;
181 void scrollToVisible()
183 if (seqCanvas.cursorX < 0)
185 seqCanvas.cursorX = 0;
187 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
189 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
192 if (seqCanvas.cursorY < 0)
194 seqCanvas.cursorY = 0;
196 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
198 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
202 if (av.wrapAlignment)
204 ap.scrollToWrappedVisible(seqCanvas.cursorX);
208 while (seqCanvas.cursorY < av.startSeq)
212 while (seqCanvas.cursorY + 1 > av.endSeq)
216 while (seqCanvas.cursorX < av.getColumnSelection()
217 .adjustForHiddenColumns(av.startRes))
220 if (!ap.scrollRight(false))
225 while (seqCanvas.cursorX > av.getColumnSelection()
226 .adjustForHiddenColumns(av.endRes))
228 if (!ap.scrollRight(true))
234 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
235 seqCanvas.cursorX, seqCanvas.cursorY);
240 void setSelectionAreaAtCursor(boolean topLeft)
242 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
245 if (av.getSelectionGroup() != null)
247 SequenceGroup sg = av.getSelectionGroup();
248 // Find the top and bottom of this group
249 int min = av.getAlignment().getHeight(), max = 0;
250 for (int i = 0; i < sg.getSize(); i++)
252 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
267 sg.setStartRes(seqCanvas.cursorX);
268 if (sg.getEndRes() < seqCanvas.cursorX)
270 sg.setEndRes(seqCanvas.cursorX);
273 min = seqCanvas.cursorY;
277 sg.setEndRes(seqCanvas.cursorX);
278 if (sg.getStartRes() > seqCanvas.cursorX)
280 sg.setStartRes(seqCanvas.cursorX);
283 max = seqCanvas.cursorY + 1;
288 // Only the user can do this
289 av.setSelectionGroup(null);
293 // Now add any sequences between min and max
295 for (int i = min; i < max; i++)
297 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
302 if (av.getSelectionGroup() == null)
304 SequenceGroup sg = new SequenceGroup();
305 sg.setStartRes(seqCanvas.cursorX);
306 sg.setEndRes(seqCanvas.cursorX);
307 sg.addSequence(sequence, false);
308 av.setSelectionGroup(sg);
310 ap.paintAlignment(false);
314 void insertGapAtCursor(boolean group)
316 groupEditing = group;
317 startseq = seqCanvas.cursorY;
318 lastres = seqCanvas.cursorX;
319 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
323 void deleteGapAtCursor(boolean group)
325 groupEditing = group;
326 startseq = seqCanvas.cursorY;
327 lastres = seqCanvas.cursorX + getKeyboardNo1();
328 editSequence(false, seqCanvas.cursorX);
332 void numberPressed(char value)
334 if (keyboardNo1 == null)
336 keyboardNo1 = new StringBuffer();
339 if (keyboardNo2 != null)
341 keyboardNo2.append(value);
345 keyboardNo1.append(value);
353 if (keyboardNo1 != null)
355 int value = Integer.parseInt(keyboardNo1.toString());
359 } catch (Exception x)
370 if (keyboardNo2 != null)
372 int value = Integer.parseInt(keyboardNo2.toString());
376 } catch (Exception x)
383 void setStatusMessage(SequenceI sequence, int res, int seq)
385 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
386 + sequence.getName());
389 if (av.getAlignment().isNucleotide())
391 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
395 text.append(" Nucleotide: ");
400 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
403 text.append(" Residue: ");
412 text.append(obj + " (" + sequence.findPosition(res) + ")");
416 ap.alignFrame.statusBar.setText(text.toString());
420 public void mousePressed(MouseEvent evt)
422 lastMousePress = evt.getPoint();
424 // For now, ignore the mouseWheel font resizing on Macs
425 // As the Button2_mask always seems to be true
426 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
429 mouseWheelPressed = true;
433 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
435 if (evt.isControlDown() || evt.isAltDown())
443 doMousePressedDefineMode(evt);
447 int seq = findSeq(evt);
448 int res = findRes(evt);
450 if (seq < 0 || res < 0)
455 if ((seq < av.getAlignment().getHeight())
456 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
470 public void mouseClicked(MouseEvent evt)
472 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
473 if (evt.getClickCount() > 1)
475 if (av.getSelectionGroup() != null
476 && av.getSelectionGroup().getSize() == 1
477 && av.getSelectionGroup().getEndRes()
478 - av.getSelectionGroup().getStartRes() < 2)
480 av.setSelectionGroup(null);
483 SequenceFeature[] features = findFeaturesAtRes(sequence,
484 sequence.findPosition(findRes(evt)));
486 if (features != null && features.length > 0)
488 SearchResults highlight = new SearchResults();
489 highlight.addResult(sequence, features[0].getBegin(),
490 features[0].getEnd());
491 seqCanvas.highlightSearchResults(highlight);
493 if (features != null && features.length > 0)
495 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
496 { sequence }, features, false, ap);
498 seqCanvas.highlightSearchResults(null);
503 public void mouseReleased(MouseEvent evt)
505 mouseDragging = false;
506 mouseWheelPressed = false;
507 ap.paintAlignment(true);
511 doMouseReleasedDefineMode(evt);
519 int startWrapBlock = -1;
521 int wrappedBlock = -1;
523 int findRes(MouseEvent evt)
528 if (av.wrapAlignment)
531 int hgap = av.charHeight;
532 if (av.scaleAboveWrapped)
534 hgap += av.charHeight;
537 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
538 + seqCanvas.getAnnotationHeight();
542 x -= seqCanvas.LABEL_WEST;
544 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
550 wrappedBlock = y / cHeight;
551 wrappedBlock += av.getStartRes() / cwidth;
553 res = wrappedBlock * cwidth + x / av.getCharWidth();
558 res = (x / av.getCharWidth()) + av.getStartRes();
561 if (av.hasHiddenColumns())
563 res = av.getColumnSelection().adjustForHiddenColumns(res);
570 int findSeq(MouseEvent evt)
572 final int sqnum = findAlRow(evt);
573 return (sqnum < 0) ? 0 : sqnum;
579 * @return row in alignment that was selected (or -1 for column selection)
581 private int findAlRow(MouseEvent evt)
586 if (av.wrapAlignment)
588 int hgap = av.charHeight;
589 if (av.scaleAboveWrapped)
591 hgap += av.charHeight;
594 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
595 + seqCanvas.getAnnotationHeight();
599 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
608 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
609 .getAlignment().getHeight() - 1);
619 public void doMousePressed(MouseEvent evt)
622 int seq = findSeq(evt);
623 int res = findRes(evt);
625 if (seq < av.getAlignment().getHeight()
626 && res < av.getAlignment().getSequenceAt(seq).getLength())
628 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
629 // Find the residue's position in the sequence (res is the position
646 public void mouseOverSequence(SequenceI sequence, int index, int pos)
648 String tmp = sequence.hashCode() + index + "";
649 if (lastMessage == null || !lastMessage.equals(tmp))
650 ssm.mouseOverSequence(sequence, index, pos, av);
655 public void highlightSequence(SearchResults results)
657 if (av.followHighlight)
659 if (ap.scrollToPosition(results, true))
661 ap.alignFrame.repaint();
664 seqCanvas.highlightSearchResults(results);
668 public void updateColours(SequenceI seq, int index)
670 System.out.println("update the seqPanel colours");
674 public void mouseMoved(MouseEvent evt)
676 int res = findRes(evt);
677 int seq = findSeq(evt);
679 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
688 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
689 if (res > sequence.getLength())
698 int respos = sequence.findPosition(res);
700 mouseOverSequence(sequence, res, respos);
702 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
703 + sequence.getName());
706 if (av.getAlignment().isNucleotide())
708 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
712 text.append(" Nucleotide: ");
717 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
720 text.append(" Residue: ");
728 text.append(obj + " (" + respos + ")");
732 ap.alignFrame.statusBar.setText(text.toString());
734 StringBuffer tooltipText = new StringBuffer();
735 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
738 for (int g = 0; g < groups.length; g++)
740 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
742 if (!groups[g].getName().startsWith("JTreeGroup")
743 && !groups[g].getName().startsWith("JGroup"))
745 tooltipText.append(groups[g].getName() + " ");
747 if (groups[g].getDescription() != null)
749 tooltipText.append(groups[g].getDescription());
751 tooltipText.append("\n");
756 // use aa to see if the mouse pointer is on a
757 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
758 sequence.findPosition(res));
761 while (index < allFeatures.length)
763 SequenceFeature sf = allFeatures[index];
765 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
767 if (sf.getDescription() != null)
769 tooltipText.append(" " + sf.getDescription());
772 if (sf.getValue("status") != null)
774 String status = sf.getValue("status").toString();
775 if (status.length() > 0)
777 tooltipText.append(" (" + sf.getValue("status") + ")");
780 tooltipText.append("\n");
787 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
791 tooltip.setTip(tooltipText.toString());
795 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
797 Vector tmp = new Vector();
798 SequenceFeature[] features = sequence.getSequenceFeatures();
799 if (features != null)
801 for (int i = 0; i < features.length; i++)
803 if (av.getFeaturesDisplayed() == null
804 || !av.getFeaturesDisplayed().isVisible(features[i].getType()))
809 if (features[i].featureGroup != null
810 && !seqCanvas.fr.checkGroupVisibility(features[i].featureGroup,false))
815 if ((features[i].getBegin() <= res)
816 && (features[i].getEnd() >= res))
818 tmp.addElement(features[i]);
823 features = new SequenceFeature[tmp.size()];
824 tmp.copyInto(features);
831 public void mouseDragged(MouseEvent evt)
833 if (mouseWheelPressed)
835 int oldWidth = av.charWidth;
837 // Which is bigger, left-right or up-down?
838 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
841 int fontSize = av.font.getSize();
843 if (evt.getY() < lastMousePress.y && av.charHeight > 1)
847 else if (evt.getY() > lastMousePress.y)
857 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
858 av.charWidth = oldWidth;
862 if (evt.getX() < lastMousePress.x && av.charWidth > 1)
866 else if (evt.getX() > lastMousePress.x)
871 if (av.charWidth < 1)
879 FontMetrics fm = getFontMetrics(av.getFont());
880 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
882 lastMousePress = evt.getPoint();
884 ap.paintAlignment(false);
885 ap.annotationPanel.image = null;
891 doMouseDraggedDefineMode(evt);
895 int res = findRes(evt);
902 if ((lastres == -1) || (lastres == res))
907 if ((res < av.getAlignment().getWidth()) && (res < lastres))
909 // dragLeft, delete gap
910 editSequence(false, res);
914 editSequence(true, res);
917 mouseDragging = true;
918 if (scrollThread != null)
920 scrollThread.setEvent(evt);
925 synchronized void editSequence(boolean insertGap, int startres)
929 boolean fixedColumns = false;
930 SequenceGroup sg = av.getSelectionGroup();
932 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
934 if (!groupEditing && av.hasHiddenRows())
936 if (av.isHiddenRepSequence(seq))
938 sg = (SequenceGroup) av.getRepresentedSequences(seq);
943 StringBuffer message = new StringBuffer();
946 message.append("Edit group:");
947 if (editCommand == null)
949 editCommand = new EditCommand("Edit Group");
954 message.append("Edit sequence: " + seq.getName());
955 String label = seq.getName();
956 if (label.length() > 10)
958 label = label.substring(0, 10);
960 if (editCommand == null)
962 editCommand = new EditCommand("Edit " + label);
968 message.append(" insert ");
972 message.append(" delete ");
975 message.append(Math.abs(startres - lastres) + " gaps.");
976 ap.alignFrame.statusBar.setText(message.toString());
978 // Are we editing within a selection group?
980 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
985 // sg might be null as the user may only see 1 sequence,
986 // but the sequence represents a group
989 if (!av.isHiddenRepSequence(seq))
995 sg = av.getRepresentedSequences(seq);
998 fixedLeft = sg.getStartRes();
999 fixedRight = sg.getEndRes();
1001 if ((startres < fixedLeft && lastres >= fixedLeft)
1002 || (startres >= fixedLeft && lastres < fixedLeft)
1003 || (startres > fixedRight && lastres <= fixedRight)
1004 || (startres <= fixedRight && lastres > fixedRight))
1010 if (fixedLeft > startres)
1012 fixedRight = fixedLeft - 1;
1015 else if (fixedRight < startres)
1017 fixedLeft = fixedRight;
1022 if (av.hasHiddenColumns())
1024 fixedColumns = true;
1025 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1026 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1028 if ((insertGap && startres > y1 && lastres < y1)
1029 || (!insertGap && startres < y2 && lastres > y2))
1035 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1036 // Selection spans a hidden region
1037 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1045 fixedRight = y2 - 1;
1052 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1053 .toArray(new SequenceI[0]);
1058 // If the user has selected the whole sequence, and is dragging to
1059 // the right, we can still extend the alignment and selectionGroup
1060 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1061 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1063 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1064 fixedRight = sg.getEndRes();
1067 // Is it valid with fixed columns??
1068 // Find the next gap before the end
1069 // of the visible region boundary
1070 boolean blank = false;
1071 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1075 for (SequenceI gs : groupSeqs)
1077 for (int j = 0; j < startres - lastres; j++)
1079 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1095 if (sg.getSize() == av.getAlignment().getHeight())
1097 if ((av.hasHiddenColumns() && startres < av
1098 .getColumnSelection().getHiddenBoundaryRight(startres)))
1104 int alWidth = av.getAlignment().getWidth();
1105 if (av.hasHiddenRows())
1107 int hwidth = av.getAlignment().getHiddenSequences()
1109 if (hwidth > alWidth)
1114 // We can still insert gaps if the selectionGroup
1115 // contains all the sequences
1116 sg.setEndRes(sg.getEndRes() + startres - lastres);
1117 fixedRight = alWidth + startres - lastres;
1128 else if (!insertGap)
1130 // / Are we able to delete?
1131 // ie are all columns blank?
1133 for (SequenceI gs : groupSeqs)
1135 for (int j = startres; j < lastres; j++)
1137 if (gs.getLength() <= j)
1142 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1144 // Not a gap, block edit not valid
1154 // dragging to the right
1155 if (fixedColumns && fixedRight != -1)
1157 for (int j = lastres; j < startres; j++)
1159 insertChar(j, groupSeqs, fixedRight);
1164 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1165 startres, startres - lastres, av.getAlignment(), true);
1170 // dragging to the left
1171 if (fixedColumns && fixedRight != -1)
1173 for (int j = lastres; j > startres; j--)
1175 deleteChar(startres, groupSeqs, fixedRight);
1180 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1181 startres, lastres - startres, av.getAlignment(), true);
1187 // ///Editing a single sequence///////////
1191 // dragging to the right
1192 if (fixedColumns && fixedRight != -1)
1194 for (int j = lastres; j < startres; j++)
1196 insertChar(j, new SequenceI[]
1197 { seq }, fixedRight);
1202 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1203 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1208 // dragging to the left
1209 if (fixedColumns && fixedRight != -1)
1211 for (int j = lastres; j > startres; j--)
1213 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1218 deleteChar(startres, new SequenceI[]
1219 { seq }, fixedRight);
1224 // could be a keyboard edit trying to delete none gaps
1226 for (int m = startres; m < lastres; m++)
1228 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1237 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1238 { seq }, startres, max, av.getAlignment(), true);
1245 seqCanvas.repaint();
1248 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1250 int blankColumn = fixedColumn;
1251 for (int s = 0; s < seq.length; s++)
1253 // Find the next gap before the end of the visible region boundary
1254 // If lastCol > j, theres a boundary after the gap insertion
1256 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1258 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1260 // Theres a space, so break and insert the gap
1265 if (blankColumn <= j)
1267 blankColumn = fixedColumn;
1273 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1274 av.getAlignment(), true);
1276 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1,
1277 av.getAlignment(), true);
1281 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1284 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1,
1285 av.getAlignment(), true);
1287 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1288 av.getAlignment(), true);
1291 // ////////////////////////////////////////
1292 // ///Everything below this is for defining the boundary of the rubberband
1293 // ////////////////////////////////////////
1294 public void doMousePressedDefineMode(MouseEvent evt)
1296 if (scrollThread != null)
1298 scrollThread.running = false;
1299 scrollThread = null;
1302 int res = findRes(evt);
1303 int seq = findSeq(evt);
1305 startWrapBlock = wrappedBlock;
1312 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1314 if (sequence == null || res > sequence.getLength())
1319 stretchGroup = av.getSelectionGroup();
1321 if (stretchGroup == null)
1323 stretchGroup = av.getAlignment().findGroup(sequence);
1324 if (stretchGroup != null && res > stretchGroup.getStartRes()
1325 && res < stretchGroup.getEndRes())
1327 av.setSelectionGroup(stretchGroup);
1331 stretchGroup = null;
1335 else if (!stretchGroup.getSequences(null).contains(sequence)
1336 || stretchGroup.getStartRes() > res
1337 || stretchGroup.getEndRes() < res)
1339 stretchGroup = null;
1341 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1343 if (allGroups != null)
1345 for (int i = 0; i < allGroups.length; i++)
1347 if (allGroups[i].getStartRes() <= res
1348 && allGroups[i].getEndRes() >= res)
1350 stretchGroup = allGroups[i];
1355 av.setSelectionGroup(stretchGroup);
1358 // DETECT RIGHT MOUSE BUTTON IN AWT
1359 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1361 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1362 sequence.findPosition(res));
1364 Vector links = null;
1365 if (allFeatures != null)
1367 for (int i = 0; i < allFeatures.length; i++)
1369 if (allFeatures[i].links != null)
1373 links = new Vector();
1375 for (int j = 0; j < allFeatures[i].links.size(); j++)
1377 links.addElement(allFeatures[i].links.elementAt(j));
1382 APopupMenu popup = new APopupMenu(ap, null, links);
1384 popup.show(this, evt.getX(), evt.getY());
1390 seqCanvas.cursorX = findRes(evt);
1391 seqCanvas.cursorY = findSeq(evt);
1392 seqCanvas.repaint();
1396 // Only if left mouse button do we want to change group sizes
1398 if (stretchGroup == null)
1400 // define a new group here
1401 SequenceGroup sg = new SequenceGroup();
1402 sg.setStartRes(res);
1404 sg.addSequence(sequence, false);
1405 av.setSelectionGroup(sg);
1408 if (av.getConservationSelected())
1410 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1413 if (av.getAbovePIDThreshold())
1415 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1422 public void doMouseReleasedDefineMode(MouseEvent evt)
1424 if (stretchGroup == null)
1429 stretchGroup.recalcConservation(); // always do this - annotation has own
1431 if (stretchGroup.cs != null)
1433 stretchGroup.cs.alignmentChanged(stretchGroup,
1434 av.getHiddenRepSequences());
1436 if (stretchGroup.cs.conservationApplied())
1438 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1439 stretchGroup.getName());
1443 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1444 stretchGroup.getName());
1447 changeEndRes = false;
1448 changeStartRes = false;
1449 stretchGroup = null;
1450 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1451 ap.paintAlignment(true);
1455 public void doMouseDraggedDefineMode(MouseEvent evt)
1457 int res = findRes(evt);
1458 int y = findSeq(evt);
1460 if (wrappedBlock != startWrapBlock)
1465 if (stretchGroup == null)
1470 mouseDragging = true;
1472 if (y > av.getAlignment().getHeight())
1474 y = av.getAlignment().getHeight() - 1;
1477 if (res >= av.getAlignment().getWidth())
1479 res = av.getAlignment().getWidth() - 1;
1482 if (stretchGroup.getEndRes() == res)
1484 // Edit end res position of selected group
1485 changeEndRes = true;
1487 else if (stretchGroup.getStartRes() == res)
1489 // Edit start res position of selected group
1490 changeStartRes = true;
1500 if (res > (stretchGroup.getStartRes() - 1))
1502 stretchGroup.setEndRes(res);
1505 else if (changeStartRes)
1507 if (res < (stretchGroup.getEndRes() + 1))
1509 stretchGroup.setStartRes(res);
1513 int dragDirection = 0;
1519 else if (y < oldSeq)
1524 while ((y != oldSeq) && (oldSeq > -1)
1525 && (y < av.getAlignment().getHeight()))
1527 // This routine ensures we don't skip any sequences, as the
1528 // selection is quite slow.
1529 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1531 oldSeq += dragDirection;
1538 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1540 if (stretchGroup.getSequences(null).contains(nextSeq))
1542 stretchGroup.deleteSequence(seq, false);
1548 stretchGroup.addSequence(seq, false);
1551 stretchGroup.addSequence(nextSeq, false);
1560 if (res > av.endRes || res < av.startRes || y < av.startSeq
1566 if (scrollThread != null)
1568 scrollThread.setEvent(evt);
1571 seqCanvas.repaint();
1574 public void mouseEntered(MouseEvent e)
1581 if (scrollThread != null)
1583 scrollThread.running = false;
1584 scrollThread = null;
1588 public void mouseExited(MouseEvent e)
1590 if (av.getWrapAlignment())
1595 if (mouseDragging && scrollThread == null)
1597 scrollThread = new ScrollThread();
1601 void scrollCanvas(MouseEvent evt)
1605 if (scrollThread != null)
1607 scrollThread.running = false;
1608 scrollThread = null;
1610 mouseDragging = false;
1614 if (scrollThread == null)
1616 scrollThread = new ScrollThread();
1619 mouseDragging = true;
1620 scrollThread.setEvent(evt);
1625 // this class allows scrolling off the bottom of the visible alignment
1626 class ScrollThread extends Thread
1630 boolean running = false;
1632 public ScrollThread()
1637 public void setEvent(MouseEvent e)
1642 public void stopScrolling()
1656 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1658 running = ap.scrollUp(true);
1661 if (mouseDragging && evt.getY() >= getSize().height
1662 && av.getAlignment().getHeight() > av.getEndSeq())
1664 running = ap.scrollUp(false);
1667 if (mouseDragging && evt.getX() < 0)
1669 running = ap.scrollRight(false);
1672 else if (mouseDragging && evt.getX() >= getSize().width)
1674 running = ap.scrollRight(true);
1681 } catch (Exception ex)
1689 * modify current selection according to a received message.
1691 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1692 SelectionSource source)
1694 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1695 // handles selection messages...
1696 // TODO: extend config options to allow user to control if selections may be
1697 // shared between viewports.
1699 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignViewport) source)
1700 .getSequenceSetId().equals(av.getSequenceSetId()))))
1704 // do we want to thread this ? (contention with seqsel and colsel locks, I
1706 // rules are: colsel is copied if there is a real intersection between
1707 // sequence selection
1708 boolean repaint = false, copycolsel = true;
1709 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1711 SequenceGroup sgroup = null;
1712 if (seqsel != null && seqsel.getSize() > 0)
1714 if (av.getAlignment() == null)
1717 .println("Selection message: alignviewport av SeqSetId="
1718 + av.getSequenceSetId() + " ViewId="
1720 + " 's alignment is NULL! returning immediatly.");
1723 sgroup = seqsel.intersect(av.getAlignment(),
1724 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1725 if ((sgroup == null || sgroup.getSize() == 0)
1726 && (colsel == null || colsel.size() == 0))
1728 // don't copy columns if the region didn't intersect.
1732 if (sgroup != null && sgroup.getSize() > 0)
1734 av.setSelectionGroup(sgroup);
1738 av.setSelectionGroup(null);
1740 repaint = av.isSelectionGroupChanged(true);
1743 && (av.getColumnSelection() == null || !av
1744 .isColSelChanged(true)))
1746 // the current selection is unset or from a previous message
1747 // so import the new colsel.
1748 if (colsel == null || colsel.size() == 0)
1750 if (av.getColumnSelection() != null)
1752 av.getColumnSelection().clear();
1757 // TODO: shift colSel according to the intersecting sequences
1758 if (av.getColumnSelection() == null)
1760 av.setColumnSelection(new ColumnSelection(colsel));
1764 av.getColumnSelection().setElementsFrom(colsel);
1767 repaint |= av.isColSelChanged(true);
1770 && av.hasHiddenColumns()
1771 && (av.getColumnSelection() == null || av.getColumnSelection()
1772 .getHiddenColumns() == null))
1774 System.err.println("Bad things");
1778 ap.scalePanelHolder.repaint();
1784 * scroll to the given row/column - or nearest visible location
1789 public void scrollTo(int row, int column)
1792 row = row < 0 ? ap.av.startSeq : row;
1793 column = column < 0 ? ap.av.startRes : column;
1794 ap.scrollTo(column, column, row, true, true);
1798 * scroll to the given row - or nearest visible location
1802 public void scrollToRow(int row)
1805 row = row < 0 ? ap.av.startSeq : row;
1806 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1810 * scroll to the given column - or nearest visible location
1814 public void scrollToColumn(int column)
1817 column = column < 0 ? ap.av.startRes : column;
1818 ap.scrollTo(column, column, ap.av.startSeq, true, true);