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;
34 import jalview.util.MessageManager;
36 public class SeqPanel extends Panel implements MouseMotionListener,
37 MouseListener, SequenceListener
40 public SeqCanvas seqCanvas;
42 public AlignmentPanel ap;
44 protected int lastres;
46 protected int startseq;
48 protected AlignViewport av;
50 // if character is inserted or deleted, we will need to recalculate the
52 boolean seqEditOccurred = false;
54 ScrollThread scrollThread = null;
56 boolean mouseDragging = false;
58 boolean editingSeqs = false;
60 boolean groupEditing = false;
64 boolean changeEndSeq = false;
66 boolean changeStartSeq = false;
68 boolean changeEndRes = false;
70 boolean changeStartRes = false;
72 SequenceGroup stretchGroup = null;
74 StringBuffer keyboardNo1;
76 StringBuffer keyboardNo2;
78 boolean mouseWheelPressed = false;
82 EditCommand editCommand;
84 StructureSelectionManager ssm;
86 public SeqPanel(AlignViewport avp, AlignmentPanel p)
90 seqCanvas = new SeqCanvas(avp);
91 setLayout(new BorderLayout());
96 seqCanvas.addMouseMotionListener(this);
97 seqCanvas.addMouseListener(this);
98 ssm = StructureSelectionManager.getStructureSelectionManager(av.applet);
99 ssm.addStructureViewerListener(this);
106 if (editCommand != null && editCommand.getSize() > 0)
108 ap.alignFrame.addHistoryItem(editCommand);
109 av.firePropertyChange("alignment", null, av.getAlignment()
116 groupEditing = false;
124 seqCanvas.cursorY = getKeyboardNo1() - 1;
128 void setCursorColumn()
130 seqCanvas.cursorX = getKeyboardNo1() - 1;
134 void setCursorRowAndColumn()
136 if (keyboardNo2 == null)
138 keyboardNo2 = new StringBuffer();
142 seqCanvas.cursorX = getKeyboardNo1() - 1;
143 seqCanvas.cursorY = getKeyboardNo2() - 1;
148 void setCursorPosition()
150 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
153 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
157 void moveCursor(int dx, int dy)
159 seqCanvas.cursorX += dx;
160 seqCanvas.cursorY += dy;
161 if (av.hasHiddenColumns()
162 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
164 int original = seqCanvas.cursorX - dx;
165 int maxWidth = av.getAlignment().getWidth();
167 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
168 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
170 seqCanvas.cursorX += dx;
173 if (seqCanvas.cursorX >= maxWidth
174 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
176 seqCanvas.cursorX = original;
182 void scrollToVisible()
184 if (seqCanvas.cursorX < 0)
186 seqCanvas.cursorX = 0;
188 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
190 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
193 if (seqCanvas.cursorY < 0)
195 seqCanvas.cursorY = 0;
197 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
199 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
203 if (av.wrapAlignment)
205 ap.scrollToWrappedVisible(seqCanvas.cursorX);
209 while (seqCanvas.cursorY < av.startSeq)
213 while (seqCanvas.cursorY + 1 > av.endSeq)
217 while (seqCanvas.cursorX < av.getColumnSelection()
218 .adjustForHiddenColumns(av.startRes))
221 if (!ap.scrollRight(false))
226 while (seqCanvas.cursorX > av.getColumnSelection()
227 .adjustForHiddenColumns(av.endRes))
229 if (!ap.scrollRight(true))
235 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
236 seqCanvas.cursorX, seqCanvas.cursorY);
241 void setSelectionAreaAtCursor(boolean topLeft)
243 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(
246 if (av.getSelectionGroup() != null)
248 SequenceGroup sg = av.getSelectionGroup();
249 // Find the top and bottom of this group
250 int min = av.getAlignment().getHeight(), max = 0;
251 for (int i = 0; i < sg.getSize(); i++)
253 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
268 sg.setStartRes(seqCanvas.cursorX);
269 if (sg.getEndRes() < seqCanvas.cursorX)
271 sg.setEndRes(seqCanvas.cursorX);
274 min = seqCanvas.cursorY;
278 sg.setEndRes(seqCanvas.cursorX);
279 if (sg.getStartRes() > seqCanvas.cursorX)
281 sg.setStartRes(seqCanvas.cursorX);
284 max = seqCanvas.cursorY + 1;
289 // Only the user can do this
290 av.setSelectionGroup(null);
294 // Now add any sequences between min and max
296 for (int i = min; i < max; i++)
298 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
303 if (av.getSelectionGroup() == null)
305 SequenceGroup sg = new SequenceGroup();
306 sg.setStartRes(seqCanvas.cursorX);
307 sg.setEndRes(seqCanvas.cursorX);
308 sg.addSequence(sequence, false);
309 av.setSelectionGroup(sg);
311 ap.paintAlignment(false);
315 void insertGapAtCursor(boolean group)
317 groupEditing = group;
318 startseq = seqCanvas.cursorY;
319 lastres = seqCanvas.cursorX;
320 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
324 void deleteGapAtCursor(boolean group)
326 groupEditing = group;
327 startseq = seqCanvas.cursorY;
328 lastres = seqCanvas.cursorX + getKeyboardNo1();
329 editSequence(false, seqCanvas.cursorX);
333 void numberPressed(char value)
335 if (keyboardNo1 == null)
337 keyboardNo1 = new StringBuffer();
340 if (keyboardNo2 != null)
342 keyboardNo2.append(value);
346 keyboardNo1.append(value);
354 if (keyboardNo1 != null)
356 int value = Integer.parseInt(keyboardNo1.toString());
360 } catch (Exception x)
371 if (keyboardNo2 != null)
373 int value = Integer.parseInt(keyboardNo2.toString());
377 } catch (Exception x)
384 void setStatusMessage(SequenceI sequence, int res, int seq)
386 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
387 + sequence.getName());
390 if (av.getAlignment().isNucleotide())
392 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
396 text.append(" Nucleotide: ");
401 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
404 text.append(" Residue: ");
413 text.append(obj + " (" + sequence.findPosition(res) + ")");
417 ap.alignFrame.statusBar.setText(text.toString());
421 public void mousePressed(MouseEvent evt)
423 lastMousePress = evt.getPoint();
425 // For now, ignore the mouseWheel font resizing on Macs
426 // As the Button2_mask always seems to be true
427 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
430 mouseWheelPressed = true;
434 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
436 if (evt.isControlDown() || evt.isAltDown())
444 doMousePressedDefineMode(evt);
448 int seq = findSeq(evt);
449 int res = findRes(evt);
451 if (seq < 0 || res < 0)
456 if ((seq < av.getAlignment().getHeight())
457 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
471 public void mouseClicked(MouseEvent evt)
473 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
474 if (evt.getClickCount() > 1)
476 if (av.getSelectionGroup() != null
477 && av.getSelectionGroup().getSize() == 1
478 && av.getSelectionGroup().getEndRes()
479 - av.getSelectionGroup().getStartRes() < 2)
481 av.setSelectionGroup(null);
484 SequenceFeature[] features = findFeaturesAtRes(sequence,
485 sequence.findPosition(findRes(evt)));
487 if (features != null && features.length > 0)
489 SearchResults highlight = new SearchResults();
490 highlight.addResult(sequence, features[0].getBegin(),
491 features[0].getEnd());
492 seqCanvas.highlightSearchResults(highlight);
494 if (features != null && features.length > 0)
496 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
497 { sequence }, features, false, ap);
499 seqCanvas.highlightSearchResults(null);
504 public void mouseReleased(MouseEvent evt)
506 mouseDragging = false;
507 mouseWheelPressed = false;
508 ap.paintAlignment(true);
512 doMouseReleasedDefineMode(evt);
520 int startWrapBlock = -1;
522 int wrappedBlock = -1;
524 int findRes(MouseEvent evt)
529 if (av.wrapAlignment)
532 int hgap = av.charHeight;
533 if (av.scaleAboveWrapped)
535 hgap += av.charHeight;
538 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
539 + seqCanvas.getAnnotationHeight();
543 x -= seqCanvas.LABEL_WEST;
545 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
551 wrappedBlock = y / cHeight;
552 wrappedBlock += av.getStartRes() / cwidth;
554 res = wrappedBlock * cwidth + x / av.getCharWidth();
559 res = (x / av.getCharWidth()) + av.getStartRes();
562 if (av.hasHiddenColumns())
564 res = av.getColumnSelection().adjustForHiddenColumns(res);
571 int findSeq(MouseEvent evt)
573 final int sqnum = findAlRow(evt);
574 return (sqnum < 0) ? 0 : sqnum;
580 * @return row in alignment that was selected (or -1 for column selection)
582 private int findAlRow(MouseEvent evt)
587 if (av.wrapAlignment)
589 int hgap = av.charHeight;
590 if (av.scaleAboveWrapped)
592 hgap += av.charHeight;
595 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
596 + seqCanvas.getAnnotationHeight();
600 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
609 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
610 .getAlignment().getHeight() - 1);
620 public void doMousePressed(MouseEvent evt)
623 int seq = findSeq(evt);
624 int res = findRes(evt);
626 if (seq < av.getAlignment().getHeight()
627 && res < av.getAlignment().getSequenceAt(seq).getLength())
629 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
630 // Find the residue's position in the sequence (res is the position
647 public void mouseOverSequence(SequenceI sequence, int index, int pos)
649 String tmp = sequence.hashCode() + index + "";
650 if (lastMessage == null || !lastMessage.equals(tmp))
651 ssm.mouseOverSequence(sequence, index, pos, av);
656 public void highlightSequence(SearchResults results)
658 if (av.followHighlight)
660 if (ap.scrollToPosition(results, true))
662 ap.alignFrame.repaint();
665 seqCanvas.highlightSearchResults(results);
669 public void updateColours(SequenceI seq, int index)
671 System.out.println("update the seqPanel colours");
675 public void mouseMoved(MouseEvent evt)
677 int res = findRes(evt);
678 int seq = findSeq(evt);
680 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
689 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
690 if (res > sequence.getLength())
699 int respos = sequence.findPosition(res);
701 mouseOverSequence(sequence, res, respos);
703 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
704 + sequence.getName());
707 if (av.getAlignment().isNucleotide())
709 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
713 text.append(" Nucleotide: ");
718 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
721 text.append(" Residue: ");
729 text.append(obj + " (" + respos + ")");
733 ap.alignFrame.statusBar.setText(text.toString());
735 StringBuffer tooltipText = new StringBuffer();
736 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
739 for (int g = 0; g < groups.length; g++)
741 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
743 if (!groups[g].getName().startsWith("JTreeGroup")
744 && !groups[g].getName().startsWith("JGroup"))
746 tooltipText.append(groups[g].getName() + " ");
748 if (groups[g].getDescription() != null)
750 tooltipText.append(groups[g].getDescription());
752 tooltipText.append("\n");
757 // use aa to see if the mouse pointer is on a
758 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
759 sequence.findPosition(res));
762 while (index < allFeatures.length)
764 SequenceFeature sf = allFeatures[index];
766 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
768 if (sf.getDescription() != null)
770 tooltipText.append(" " + sf.getDescription());
773 if (sf.getValue("status") != null)
775 String status = sf.getValue("status").toString();
776 if (status.length() > 0)
778 tooltipText.append(" (" + sf.getValue("status") + ")");
781 tooltipText.append("\n");
788 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
792 tooltip.setTip(tooltipText.toString());
796 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
798 Vector tmp = new Vector();
799 SequenceFeature[] features = sequence.getSequenceFeatures();
800 if (features != null)
802 for (int i = 0; i < features.length; i++)
804 if (av.getFeaturesDisplayed() == null
805 || !av.getFeaturesDisplayed().isVisible(features[i].getType()))
810 if (features[i].featureGroup != null
811 && !seqCanvas.fr.checkGroupVisibility(features[i].featureGroup,false))
816 if ((features[i].getBegin() <= res)
817 && (features[i].getEnd() >= res))
819 tmp.addElement(features[i]);
824 features = new SequenceFeature[tmp.size()];
825 tmp.copyInto(features);
832 public void mouseDragged(MouseEvent evt)
834 if (mouseWheelPressed)
836 int oldWidth = av.charWidth;
838 // Which is bigger, left-right or up-down?
839 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
842 int fontSize = av.font.getSize();
844 if (evt.getY() < lastMousePress.y && av.charHeight > 1)
848 else if (evt.getY() > lastMousePress.y)
858 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
859 av.charWidth = oldWidth;
863 if (evt.getX() < lastMousePress.x && av.charWidth > 1)
867 else if (evt.getX() > lastMousePress.x)
872 if (av.charWidth < 1)
880 FontMetrics fm = getFontMetrics(av.getFont());
881 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
883 lastMousePress = evt.getPoint();
885 ap.paintAlignment(false);
886 ap.annotationPanel.image = null;
892 doMouseDraggedDefineMode(evt);
896 int res = findRes(evt);
903 if ((lastres == -1) || (lastres == res))
908 if ((res < av.getAlignment().getWidth()) && (res < lastres))
910 // dragLeft, delete gap
911 editSequence(false, res);
915 editSequence(true, res);
918 mouseDragging = true;
919 if (scrollThread != null)
921 scrollThread.setEvent(evt);
926 synchronized void editSequence(boolean insertGap, int startres)
930 boolean fixedColumns = false;
931 SequenceGroup sg = av.getSelectionGroup();
933 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
935 if (!groupEditing && av.hasHiddenRows())
937 if (av.isHiddenRepSequence(seq))
939 sg = (SequenceGroup) av.getRepresentedSequences(seq);
944 StringBuffer message = new StringBuffer();
947 message.append(MessageManager.getString("action.edit_group")).append(":");
948 if (editCommand == null)
950 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
955 message.append(MessageManager.getString("label.edit_sequence")).append(" " + seq.getName());
956 String label = seq.getName();
957 if (label.length() > 10)
959 label = label.substring(0, 10);
961 if (editCommand == null)
963 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
969 message.append(" insert ");
973 message.append(" delete ");
976 message.append(Math.abs(startres - lastres) + " gaps.");
977 ap.alignFrame.statusBar.setText(message.toString());
979 // Are we editing within a selection group?
981 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
986 // sg might be null as the user may only see 1 sequence,
987 // but the sequence represents a group
990 if (!av.isHiddenRepSequence(seq))
996 sg = av.getRepresentedSequences(seq);
999 fixedLeft = sg.getStartRes();
1000 fixedRight = sg.getEndRes();
1002 if ((startres < fixedLeft && lastres >= fixedLeft)
1003 || (startres >= fixedLeft && lastres < fixedLeft)
1004 || (startres > fixedRight && lastres <= fixedRight)
1005 || (startres <= fixedRight && lastres > fixedRight))
1011 if (fixedLeft > startres)
1013 fixedRight = fixedLeft - 1;
1016 else if (fixedRight < startres)
1018 fixedLeft = fixedRight;
1023 if (av.hasHiddenColumns())
1025 fixedColumns = true;
1026 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1027 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1029 if ((insertGap && startres > y1 && lastres < y1)
1030 || (!insertGap && startres < y2 && lastres > y2))
1036 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1037 // Selection spans a hidden region
1038 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1046 fixedRight = y2 - 1;
1053 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1054 .toArray(new SequenceI[0]);
1059 // If the user has selected the whole sequence, and is dragging to
1060 // the right, we can still extend the alignment and selectionGroup
1061 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1062 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1064 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1065 fixedRight = sg.getEndRes();
1068 // Is it valid with fixed columns??
1069 // Find the next gap before the end
1070 // of the visible region boundary
1071 boolean blank = false;
1072 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1076 for (SequenceI gs : groupSeqs)
1078 for (int j = 0; j < startres - lastres; j++)
1080 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1096 if (sg.getSize() == av.getAlignment().getHeight())
1098 if ((av.hasHiddenColumns() && startres < av
1099 .getColumnSelection().getHiddenBoundaryRight(startres)))
1105 int alWidth = av.getAlignment().getWidth();
1106 if (av.hasHiddenRows())
1108 int hwidth = av.getAlignment().getHiddenSequences()
1110 if (hwidth > alWidth)
1115 // We can still insert gaps if the selectionGroup
1116 // contains all the sequences
1117 sg.setEndRes(sg.getEndRes() + startres - lastres);
1118 fixedRight = alWidth + startres - lastres;
1129 else if (!insertGap)
1131 // / Are we able to delete?
1132 // ie are all columns blank?
1134 for (SequenceI gs : groupSeqs)
1136 for (int j = startres; j < lastres; j++)
1138 if (gs.getLength() <= j)
1143 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1145 // Not a gap, block edit not valid
1155 // dragging to the right
1156 if (fixedColumns && fixedRight != -1)
1158 for (int j = lastres; j < startres; j++)
1160 insertChar(j, groupSeqs, fixedRight);
1165 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1166 startres, startres - lastres, av.getAlignment(), true);
1171 // dragging to the left
1172 if (fixedColumns && fixedRight != -1)
1174 for (int j = lastres; j > startres; j--)
1176 deleteChar(startres, groupSeqs, fixedRight);
1181 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1182 startres, lastres - startres, av.getAlignment(), true);
1188 // ///Editing a single sequence///////////
1192 // dragging to the right
1193 if (fixedColumns && fixedRight != -1)
1195 for (int j = lastres; j < startres; j++)
1197 insertChar(j, new SequenceI[]
1198 { seq }, fixedRight);
1203 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1204 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1209 // dragging to the left
1210 if (fixedColumns && fixedRight != -1)
1212 for (int j = lastres; j > startres; j--)
1214 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1219 deleteChar(startres, new SequenceI[]
1220 { seq }, fixedRight);
1225 // could be a keyboard edit trying to delete none gaps
1227 for (int m = startres; m < lastres; m++)
1229 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1238 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1239 { seq }, startres, max, av.getAlignment(), true);
1246 seqCanvas.repaint();
1249 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1251 int blankColumn = fixedColumn;
1252 for (int s = 0; s < seq.length; s++)
1254 // Find the next gap before the end of the visible region boundary
1255 // If lastCol > j, theres a boundary after the gap insertion
1257 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1259 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1261 // Theres a space, so break and insert the gap
1266 if (blankColumn <= j)
1268 blankColumn = fixedColumn;
1274 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1275 av.getAlignment(), true);
1277 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1,
1278 av.getAlignment(), true);
1282 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1285 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1,
1286 av.getAlignment(), true);
1288 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1289 av.getAlignment(), true);
1292 // ////////////////////////////////////////
1293 // ///Everything below this is for defining the boundary of the rubberband
1294 // ////////////////////////////////////////
1295 public void doMousePressedDefineMode(MouseEvent evt)
1297 if (scrollThread != null)
1299 scrollThread.running = false;
1300 scrollThread = null;
1303 int res = findRes(evt);
1304 int seq = findSeq(evt);
1306 startWrapBlock = wrappedBlock;
1313 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1315 if (sequence == null || res > sequence.getLength())
1320 stretchGroup = av.getSelectionGroup();
1322 if (stretchGroup == null)
1324 stretchGroup = av.getAlignment().findGroup(sequence);
1325 if (stretchGroup != null && res > stretchGroup.getStartRes()
1326 && res < stretchGroup.getEndRes())
1328 av.setSelectionGroup(stretchGroup);
1332 stretchGroup = null;
1336 else if (!stretchGroup.getSequences(null).contains(sequence)
1337 || stretchGroup.getStartRes() > res
1338 || stretchGroup.getEndRes() < res)
1340 stretchGroup = null;
1342 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1344 if (allGroups != null)
1346 for (int i = 0; i < allGroups.length; i++)
1348 if (allGroups[i].getStartRes() <= res
1349 && allGroups[i].getEndRes() >= res)
1351 stretchGroup = allGroups[i];
1356 av.setSelectionGroup(stretchGroup);
1359 // DETECT RIGHT MOUSE BUTTON IN AWT
1360 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1362 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1363 sequence.findPosition(res));
1365 Vector links = null;
1366 if (allFeatures != null)
1368 for (int i = 0; i < allFeatures.length; i++)
1370 if (allFeatures[i].links != null)
1374 links = new Vector();
1376 for (int j = 0; j < allFeatures[i].links.size(); j++)
1378 links.addElement(allFeatures[i].links.elementAt(j));
1383 APopupMenu popup = new APopupMenu(ap, null, links);
1385 popup.show(this, evt.getX(), evt.getY());
1391 seqCanvas.cursorX = findRes(evt);
1392 seqCanvas.cursorY = findSeq(evt);
1393 seqCanvas.repaint();
1397 // Only if left mouse button do we want to change group sizes
1399 if (stretchGroup == null)
1401 // define a new group here
1402 SequenceGroup sg = new SequenceGroup();
1403 sg.setStartRes(res);
1405 sg.addSequence(sequence, false);
1406 av.setSelectionGroup(sg);
1409 if (av.getConservationSelected())
1411 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1414 if (av.getAbovePIDThreshold())
1416 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1423 public void doMouseReleasedDefineMode(MouseEvent evt)
1425 if (stretchGroup == null)
1430 stretchGroup.recalcConservation(); // always do this - annotation has own
1432 if (stretchGroup.cs != null)
1434 stretchGroup.cs.alignmentChanged(stretchGroup,
1435 av.getHiddenRepSequences());
1437 if (stretchGroup.cs.conservationApplied())
1439 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1440 stretchGroup.getName());
1444 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1445 stretchGroup.getName());
1448 changeEndRes = false;
1449 changeStartRes = false;
1450 stretchGroup = null;
1451 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1452 ap.paintAlignment(true);
1456 public void doMouseDraggedDefineMode(MouseEvent evt)
1458 int res = findRes(evt);
1459 int y = findSeq(evt);
1461 if (wrappedBlock != startWrapBlock)
1466 if (stretchGroup == null)
1471 mouseDragging = true;
1473 if (y > av.getAlignment().getHeight())
1475 y = av.getAlignment().getHeight() - 1;
1478 if (res >= av.getAlignment().getWidth())
1480 res = av.getAlignment().getWidth() - 1;
1483 if (stretchGroup.getEndRes() == res)
1485 // Edit end res position of selected group
1486 changeEndRes = true;
1488 else if (stretchGroup.getStartRes() == res)
1490 // Edit start res position of selected group
1491 changeStartRes = true;
1501 if (res > (stretchGroup.getStartRes() - 1))
1503 stretchGroup.setEndRes(res);
1506 else if (changeStartRes)
1508 if (res < (stretchGroup.getEndRes() + 1))
1510 stretchGroup.setStartRes(res);
1514 int dragDirection = 0;
1520 else if (y < oldSeq)
1525 while ((y != oldSeq) && (oldSeq > -1)
1526 && (y < av.getAlignment().getHeight()))
1528 // This routine ensures we don't skip any sequences, as the
1529 // selection is quite slow.
1530 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1532 oldSeq += dragDirection;
1539 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1541 if (stretchGroup.getSequences(null).contains(nextSeq))
1543 stretchGroup.deleteSequence(seq, false);
1549 stretchGroup.addSequence(seq, false);
1552 stretchGroup.addSequence(nextSeq, false);
1561 if (res > av.endRes || res < av.startRes || y < av.startSeq
1567 if (scrollThread != null)
1569 scrollThread.setEvent(evt);
1572 seqCanvas.repaint();
1575 public void mouseEntered(MouseEvent e)
1582 if (scrollThread != null)
1584 scrollThread.running = false;
1585 scrollThread = null;
1589 public void mouseExited(MouseEvent e)
1591 if (av.getWrapAlignment())
1596 if (mouseDragging && scrollThread == null)
1598 scrollThread = new ScrollThread();
1602 void scrollCanvas(MouseEvent evt)
1606 if (scrollThread != null)
1608 scrollThread.running = false;
1609 scrollThread = null;
1611 mouseDragging = false;
1615 if (scrollThread == null)
1617 scrollThread = new ScrollThread();
1620 mouseDragging = true;
1621 scrollThread.setEvent(evt);
1626 // this class allows scrolling off the bottom of the visible alignment
1627 class ScrollThread extends Thread
1631 boolean running = false;
1633 public ScrollThread()
1638 public void setEvent(MouseEvent e)
1643 public void stopScrolling()
1657 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1659 running = ap.scrollUp(true);
1662 if (mouseDragging && evt.getY() >= getSize().height
1663 && av.getAlignment().getHeight() > av.getEndSeq())
1665 running = ap.scrollUp(false);
1668 if (mouseDragging && evt.getX() < 0)
1670 running = ap.scrollRight(false);
1673 else if (mouseDragging && evt.getX() >= getSize().width)
1675 running = ap.scrollRight(true);
1682 } catch (Exception ex)
1690 * modify current selection according to a received message.
1692 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1693 SelectionSource source)
1695 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1696 // handles selection messages...
1697 // TODO: extend config options to allow user to control if selections may be
1698 // shared between viewports.
1700 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignViewport) source)
1701 .getSequenceSetId().equals(av.getSequenceSetId()))))
1705 // do we want to thread this ? (contention with seqsel and colsel locks, I
1707 // rules are: colsel is copied if there is a real intersection between
1708 // sequence selection
1709 boolean repaint = false, copycolsel = true;
1710 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1712 SequenceGroup sgroup = null;
1713 if (seqsel != null && seqsel.getSize() > 0)
1715 if (av.getAlignment() == null)
1718 .println("Selection message: alignviewport av SeqSetId="
1719 + av.getSequenceSetId() + " ViewId="
1721 + " 's alignment is NULL! returning immediatly.");
1724 sgroup = seqsel.intersect(av.getAlignment(),
1725 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1726 if ((sgroup == null || sgroup.getSize() == 0)
1727 && (colsel == null || colsel.size() == 0))
1729 // don't copy columns if the region didn't intersect.
1733 if (sgroup != null && sgroup.getSize() > 0)
1735 av.setSelectionGroup(sgroup);
1739 av.setSelectionGroup(null);
1741 repaint = av.isSelectionGroupChanged(true);
1744 && (av.getColumnSelection() == null || !av
1745 .isColSelChanged(true)))
1747 // the current selection is unset or from a previous message
1748 // so import the new colsel.
1749 if (colsel == null || colsel.size() == 0)
1751 if (av.getColumnSelection() != null)
1753 av.getColumnSelection().clear();
1758 // TODO: shift colSel according to the intersecting sequences
1759 if (av.getColumnSelection() == null)
1761 av.setColumnSelection(new ColumnSelection(colsel));
1765 av.getColumnSelection().setElementsFrom(colsel);
1768 repaint |= av.isColSelChanged(true);
1771 && av.hasHiddenColumns()
1772 && (av.getColumnSelection() == null || av.getColumnSelection()
1773 .getHiddenColumns() == null))
1775 System.err.println("Bad things");
1779 ap.scalePanelHolder.repaint();
1785 * scroll to the given row/column - or nearest visible location
1790 public void scrollTo(int row, int column)
1793 row = row < 0 ? ap.av.startSeq : row;
1794 column = column < 0 ? ap.av.startRes : column;
1795 ap.scrollTo(column, column, row, true, true);
1799 * scroll to the given row - or nearest visible location
1803 public void scrollToRow(int row)
1806 row = row < 0 ? ap.av.startSeq : row;
1807 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1811 * scroll to the given column - or nearest visible location
1815 public void scrollToColumn(int column)
1818 column = column < 0 ? ap.av.startRes : column;
1819 ap.scrollTo(column, column, ap.av.startSeq, true, true);