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);
351 if (keyboardNo1 != null)
353 int value = Integer.parseInt(keyboardNo1.toString());
357 } catch (Exception x)
366 if (keyboardNo2!=null){
367 int value = Integer.parseInt(keyboardNo2.toString());
371 } catch (Exception x)
377 void setStatusMessage(SequenceI sequence, int res, int seq)
379 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
380 + sequence.getName());
383 if (av.getAlignment().isNucleotide())
385 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
389 text.append(" Nucleotide: ");
394 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
397 text.append(" Residue: ");
406 text.append(obj + " (" + sequence.findPosition(res) + ")");
410 ap.alignFrame.statusBar.setText(text.toString());
414 public void mousePressed(MouseEvent evt)
416 lastMousePress = evt.getPoint();
418 // For now, ignore the mouseWheel font resizing on Macs
419 // As the Button2_mask always seems to be true
420 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
423 mouseWheelPressed = true;
427 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
429 if (evt.isControlDown() || evt.isAltDown())
437 doMousePressedDefineMode(evt);
441 int seq = findSeq(evt);
442 int res = findRes(evt);
444 if (seq < 0 || res < 0)
449 if ((seq < av.getAlignment().getHeight())
450 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
464 public void mouseClicked(MouseEvent evt)
466 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
467 if (evt.getClickCount() > 1)
469 if (av.getSelectionGroup() != null
470 && av.getSelectionGroup().getSize() == 1
471 && av.getSelectionGroup().getEndRes()
472 - av.getSelectionGroup().getStartRes() < 2)
474 av.setSelectionGroup(null);
477 SequenceFeature[] features = findFeaturesAtRes(sequence,
478 sequence.findPosition(findRes(evt)));
480 if (features != null && features.length > 0)
482 SearchResults highlight = new SearchResults();
483 highlight.addResult(sequence, features[0].getBegin(),
484 features[0].getEnd());
485 seqCanvas.highlightSearchResults(highlight);
487 if (features != null && features.length > 0)
489 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
490 { sequence }, features, false, ap);
492 seqCanvas.highlightSearchResults(null);
497 public void mouseReleased(MouseEvent evt)
499 mouseDragging = false;
500 mouseWheelPressed = false;
501 ap.paintAlignment(true);
505 doMouseReleasedDefineMode(evt);
513 int startWrapBlock = -1;
515 int wrappedBlock = -1;
517 int findRes(MouseEvent evt)
522 if (av.wrapAlignment)
525 int hgap = av.charHeight;
526 if (av.scaleAboveWrapped)
528 hgap += av.charHeight;
531 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
532 + seqCanvas.getAnnotationHeight();
536 x -= seqCanvas.LABEL_WEST;
538 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
544 wrappedBlock = y / cHeight;
545 wrappedBlock += av.getStartRes() / cwidth;
547 res = wrappedBlock * cwidth + x / av.getCharWidth();
552 res = (x / av.getCharWidth()) + av.getStartRes();
555 if (av.hasHiddenColumns())
557 res = av.getColumnSelection().adjustForHiddenColumns(res);
564 int findSeq(MouseEvent evt)
566 final int sqnum = findAlRow(evt);
567 return (sqnum < 0) ? 0 : sqnum;
573 * @return row in alignment that was selected (or -1 for column selection)
575 private int findAlRow(MouseEvent evt)
580 if (av.wrapAlignment)
582 int hgap = av.charHeight;
583 if (av.scaleAboveWrapped)
585 hgap += av.charHeight;
588 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
589 + seqCanvas.getAnnotationHeight();
593 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
602 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
603 .getAlignment().getHeight() - 1);
613 public void doMousePressed(MouseEvent evt)
616 int seq = findSeq(evt);
617 int res = findRes(evt);
619 if (seq < av.getAlignment().getHeight()
620 && res < av.getAlignment().getSequenceAt(seq).getLength())
622 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
623 // Find the residue's position in the sequence (res is the position
640 public void mouseOverSequence(SequenceI sequence, int index, int pos)
642 String tmp = sequence.hashCode() + index + "";
643 if (lastMessage == null || !lastMessage.equals(tmp))
644 ssm.mouseOverSequence(sequence, index, pos, av);
649 public void highlightSequence(SearchResults results)
651 if (av.followHighlight)
653 if (ap.scrollToPosition(results, true))
655 ap.alignFrame.repaint();
658 seqCanvas.highlightSearchResults(results);
662 public void updateColours(SequenceI seq, int index)
664 System.out.println("update the seqPanel colours");
668 public void mouseMoved(MouseEvent evt)
670 int res = findRes(evt);
671 int seq = findSeq(evt);
673 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
682 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
683 if (res > sequence.getLength())
692 int respos = sequence.findPosition(res);
694 mouseOverSequence(sequence, res, respos);
696 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
697 + sequence.getName());
700 if (av.getAlignment().isNucleotide())
702 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
706 text.append(" Nucleotide: ");
711 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
714 text.append(" Residue: ");
722 text.append(obj + " (" + respos + ")");
726 ap.alignFrame.statusBar.setText(text.toString());
728 StringBuffer tooltipText = new StringBuffer();
729 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
732 for (int g = 0; g < groups.length; g++)
734 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
736 if (!groups[g].getName().startsWith("JTreeGroup")
737 && !groups[g].getName().startsWith("JGroup"))
739 tooltipText.append(groups[g].getName() + " ");
741 if (groups[g].getDescription() != null)
743 tooltipText.append(groups[g].getDescription());
745 tooltipText.append("\n");
750 // use aa to see if the mouse pointer is on a
751 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
752 sequence.findPosition(res));
755 while (index < allFeatures.length)
757 SequenceFeature sf = allFeatures[index];
759 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
761 if (sf.getDescription() != null)
763 tooltipText.append(" " + sf.getDescription());
766 if (sf.getValue("status") != null)
768 String status = sf.getValue("status").toString();
769 if (status.length() > 0)
771 tooltipText.append(" (" + sf.getValue("status") + ")");
774 tooltipText.append("\n");
781 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
785 tooltip.setTip(tooltipText.toString());
789 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
791 Vector tmp = new Vector();
792 SequenceFeature[] features = sequence.getSequenceFeatures();
793 if (features != null)
795 for (int i = 0; i < features.length; i++)
797 if (av.featuresDisplayed == null
798 || !av.featuresDisplayed.containsKey(features[i].getType()))
803 if (features[i].featureGroup != null
804 && seqCanvas.fr.featureGroups != null
805 && seqCanvas.fr.featureGroups
806 .containsKey(features[i].featureGroup)
807 && !((Boolean) seqCanvas.fr.featureGroups
808 .get(features[i].featureGroup)).booleanValue())
811 if ((features[i].getBegin() <= res)
812 && (features[i].getEnd() >= res))
814 tmp.addElement(features[i]);
819 features = new SequenceFeature[tmp.size()];
820 tmp.copyInto(features);
827 public void mouseDragged(MouseEvent evt)
829 if (mouseWheelPressed)
831 int oldWidth = av.charWidth;
833 // Which is bigger, left-right or up-down?
834 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
837 int fontSize = av.font.getSize();
839 if (evt.getY() < lastMousePress.y && av.charHeight > 1)
843 else if (evt.getY() > lastMousePress.y)
853 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
854 av.charWidth = oldWidth;
858 if (evt.getX() < lastMousePress.x && av.charWidth > 1)
862 else if (evt.getX() > lastMousePress.x)
867 if (av.charWidth < 1)
875 FontMetrics fm = getFontMetrics(av.getFont());
876 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
878 lastMousePress = evt.getPoint();
880 ap.paintAlignment(false);
881 ap.annotationPanel.image = null;
887 doMouseDraggedDefineMode(evt);
891 int res = findRes(evt);
898 if ((lastres == -1) || (lastres == res))
903 if ((res < av.getAlignment().getWidth()) && (res < lastres))
905 // dragLeft, delete gap
906 editSequence(false, res);
910 editSequence(true, res);
913 mouseDragging = true;
914 if (scrollThread != null)
916 scrollThread.setEvent(evt);
921 synchronized void editSequence(boolean insertGap, int startres)
925 boolean fixedColumns = false;
926 SequenceGroup sg = av.getSelectionGroup();
928 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
930 if (!groupEditing && av.hasHiddenRows())
932 if (av.isHiddenRepSequence(seq))
934 sg = (SequenceGroup) av.getRepresentedSequences(seq);
939 StringBuffer message = new StringBuffer();
942 message.append("Edit group:");
943 if (editCommand == null)
945 editCommand = new EditCommand("Edit Group");
950 message.append("Edit sequence: " + seq.getName());
951 String label = seq.getName();
952 if (label.length() > 10)
954 label = label.substring(0, 10);
956 if (editCommand == null)
958 editCommand = new EditCommand("Edit " + label);
964 message.append(" insert ");
968 message.append(" delete ");
971 message.append(Math.abs(startres - lastres) + " gaps.");
972 ap.alignFrame.statusBar.setText(message.toString());
974 // Are we editing within a selection group?
976 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
981 // sg might be null as the user may only see 1 sequence,
982 // but the sequence represents a group
985 if (!av.isHiddenRepSequence(seq))
991 sg = av.getRepresentedSequences(seq);
994 fixedLeft = sg.getStartRes();
995 fixedRight = sg.getEndRes();
997 if ((startres < fixedLeft && lastres >= fixedLeft)
998 || (startres >= fixedLeft && lastres < fixedLeft)
999 || (startres > fixedRight && lastres <= fixedRight)
1000 || (startres <= fixedRight && lastres > fixedRight))
1006 if (fixedLeft > startres)
1008 fixedRight = fixedLeft - 1;
1011 else if (fixedRight < startres)
1013 fixedLeft = fixedRight;
1018 if (av.hasHiddenColumns())
1020 fixedColumns = true;
1021 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1022 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1024 if ((insertGap && startres > y1 && lastres < y1)
1025 || (!insertGap && startres < y2 && lastres > y2))
1031 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1032 // Selection spans a hidden region
1033 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1041 fixedRight = y2 - 1;
1048 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1049 .toArray(new SequenceI[0]);
1054 // If the user has selected the whole sequence, and is dragging to
1055 // the right, we can still extend the alignment and selectionGroup
1056 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1057 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1059 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1060 fixedRight = sg.getEndRes();
1063 // Is it valid with fixed columns??
1064 // Find the next gap before the end
1065 // of the visible region boundary
1066 boolean blank = false;
1067 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1071 for (SequenceI gs : groupSeqs)
1073 for (int j = 0; j < startres - lastres; j++)
1075 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1091 if (sg.getSize() == av.getAlignment().getHeight())
1093 if ((av.hasHiddenColumns() && startres < av
1094 .getColumnSelection().getHiddenBoundaryRight(startres)))
1100 int alWidth = av.getAlignment().getWidth();
1101 if (av.hasHiddenRows())
1103 int hwidth = av.getAlignment().getHiddenSequences()
1105 if (hwidth > alWidth)
1110 // We can still insert gaps if the selectionGroup
1111 // contains all the sequences
1112 sg.setEndRes(sg.getEndRes() + startres - lastres);
1113 fixedRight = alWidth + startres - lastres;
1124 else if (!insertGap)
1126 // / Are we able to delete?
1127 // ie are all columns blank?
1129 for (SequenceI gs : groupSeqs)
1131 for (int j = startres; j < lastres; j++)
1133 if (gs.getLength() <= j)
1138 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1140 // Not a gap, block edit not valid
1150 // dragging to the right
1151 if (fixedColumns && fixedRight != -1)
1153 for (int j = lastres; j < startres; j++)
1155 insertChar(j, groupSeqs, fixedRight);
1160 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1161 startres, startres - lastres, av.getAlignment(), true);
1166 // dragging to the left
1167 if (fixedColumns && fixedRight != -1)
1169 for (int j = lastres; j > startres; j--)
1171 deleteChar(startres, groupSeqs, fixedRight);
1176 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1177 startres, lastres - startres, av.getAlignment(), true);
1183 // ///Editing a single sequence///////////
1187 // dragging to the right
1188 if (fixedColumns && fixedRight != -1)
1190 for (int j = lastres; j < startres; j++)
1192 insertChar(j, new SequenceI[]
1193 { seq }, fixedRight);
1198 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1199 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1204 // dragging to the left
1205 if (fixedColumns && fixedRight != -1)
1207 for (int j = lastres; j > startres; j--)
1209 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1214 deleteChar(startres, new SequenceI[]
1215 { seq }, fixedRight);
1220 // could be a keyboard edit trying to delete none gaps
1222 for (int m = startres; m < lastres; m++)
1224 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1233 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1234 { seq }, startres, max, av.getAlignment(), true);
1241 seqCanvas.repaint();
1244 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1246 int blankColumn = fixedColumn;
1247 for (int s = 0; s < seq.length; s++)
1249 // Find the next gap before the end of the visible region boundary
1250 // If lastCol > j, theres a boundary after the gap insertion
1252 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1254 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1256 // Theres a space, so break and insert the gap
1261 if (blankColumn <= j)
1263 blankColumn = fixedColumn;
1269 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1270 av.getAlignment(), true);
1272 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1,
1273 av.getAlignment(), true);
1277 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1280 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1,
1281 av.getAlignment(), true);
1283 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1284 av.getAlignment(), true);
1287 // ////////////////////////////////////////
1288 // ///Everything below this is for defining the boundary of the rubberband
1289 // ////////////////////////////////////////
1290 public void doMousePressedDefineMode(MouseEvent evt)
1292 if (scrollThread != null)
1294 scrollThread.running = false;
1295 scrollThread = null;
1298 int res = findRes(evt);
1299 int seq = findSeq(evt);
1301 startWrapBlock = wrappedBlock;
1308 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1310 if (sequence == null || res > sequence.getLength())
1315 stretchGroup = av.getSelectionGroup();
1317 if (stretchGroup == null)
1319 stretchGroup = av.getAlignment().findGroup(sequence);
1320 if (stretchGroup != null && res > stretchGroup.getStartRes()
1321 && res < stretchGroup.getEndRes())
1323 av.setSelectionGroup(stretchGroup);
1327 stretchGroup = null;
1331 else if (!stretchGroup.getSequences(null).contains(sequence)
1332 || stretchGroup.getStartRes() > res
1333 || stretchGroup.getEndRes() < res)
1335 stretchGroup = null;
1337 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1339 if (allGroups != null)
1341 for (int i = 0; i < allGroups.length; i++)
1343 if (allGroups[i].getStartRes() <= res
1344 && allGroups[i].getEndRes() >= res)
1346 stretchGroup = allGroups[i];
1351 av.setSelectionGroup(stretchGroup);
1354 // DETECT RIGHT MOUSE BUTTON IN AWT
1355 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1357 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1358 sequence.findPosition(res));
1360 Vector links = null;
1361 if (allFeatures != null)
1363 for (int i = 0; i < allFeatures.length; i++)
1365 if (allFeatures[i].links != null)
1369 links = new Vector();
1371 for (int j = 0; j < allFeatures[i].links.size(); j++)
1373 links.addElement(allFeatures[i].links.elementAt(j));
1378 APopupMenu popup = new APopupMenu(ap, null, links);
1380 popup.show(this, evt.getX(), evt.getY());
1386 seqCanvas.cursorX = findRes(evt);
1387 seqCanvas.cursorY = findSeq(evt);
1388 seqCanvas.repaint();
1392 // Only if left mouse button do we want to change group sizes
1394 if (stretchGroup == null)
1396 // define a new group here
1397 SequenceGroup sg = new SequenceGroup();
1398 sg.setStartRes(res);
1400 sg.addSequence(sequence, false);
1401 av.setSelectionGroup(sg);
1404 if (av.getConservationSelected())
1406 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1409 if (av.getAbovePIDThreshold())
1411 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1418 public void doMouseReleasedDefineMode(MouseEvent evt)
1420 if (stretchGroup == null)
1425 stretchGroup.recalcConservation(); // always do this - annotation has own
1427 if (stretchGroup.cs != null)
1429 stretchGroup.cs.alignmentChanged(stretchGroup,
1430 av.getHiddenRepSequences());
1432 if (stretchGroup.cs.conservationApplied())
1434 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1435 stretchGroup.getName());
1439 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1440 stretchGroup.getName());
1443 changeEndRes = false;
1444 changeStartRes = false;
1445 stretchGroup = null;
1446 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1447 ap.paintAlignment(true);
1451 public void doMouseDraggedDefineMode(MouseEvent evt)
1453 int res = findRes(evt);
1454 int y = findSeq(evt);
1456 if (wrappedBlock != startWrapBlock)
1461 if (stretchGroup == null)
1466 mouseDragging = true;
1468 if (y > av.getAlignment().getHeight())
1470 y = av.getAlignment().getHeight() - 1;
1473 if (res >= av.getAlignment().getWidth())
1475 res = av.getAlignment().getWidth() - 1;
1478 if (stretchGroup.getEndRes() == res)
1480 // Edit end res position of selected group
1481 changeEndRes = true;
1483 else if (stretchGroup.getStartRes() == res)
1485 // Edit start res position of selected group
1486 changeStartRes = true;
1496 if (res > (stretchGroup.getStartRes() - 1))
1498 stretchGroup.setEndRes(res);
1501 else if (changeStartRes)
1503 if (res < (stretchGroup.getEndRes() + 1))
1505 stretchGroup.setStartRes(res);
1509 int dragDirection = 0;
1515 else if (y < oldSeq)
1520 while ((y != oldSeq) && (oldSeq > -1)
1521 && (y < av.getAlignment().getHeight()))
1523 // This routine ensures we don't skip any sequences, as the
1524 // selection is quite slow.
1525 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1527 oldSeq += dragDirection;
1534 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1536 if (stretchGroup.getSequences(null).contains(nextSeq))
1538 stretchGroup.deleteSequence(seq, false);
1544 stretchGroup.addSequence(seq, false);
1547 stretchGroup.addSequence(nextSeq, false);
1556 if (res > av.endRes || res < av.startRes || y < av.startSeq
1562 if (scrollThread != null)
1564 scrollThread.setEvent(evt);
1567 seqCanvas.repaint();
1570 public void mouseEntered(MouseEvent e)
1577 if (scrollThread != null)
1579 scrollThread.running = false;
1580 scrollThread = null;
1584 public void mouseExited(MouseEvent e)
1586 if (av.getWrapAlignment())
1591 if (mouseDragging && scrollThread == null)
1593 scrollThread = new ScrollThread();
1597 void scrollCanvas(MouseEvent evt)
1601 if (scrollThread != null)
1603 scrollThread.running = false;
1604 scrollThread = null;
1606 mouseDragging = false;
1610 if (scrollThread == null)
1612 scrollThread = new ScrollThread();
1615 mouseDragging = true;
1616 scrollThread.setEvent(evt);
1621 // this class allows scrolling off the bottom of the visible alignment
1622 class ScrollThread extends Thread
1626 boolean running = false;
1628 public ScrollThread()
1633 public void setEvent(MouseEvent e)
1638 public void stopScrolling()
1652 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1654 running = ap.scrollUp(true);
1657 if (mouseDragging && evt.getY() >= getSize().height
1658 && av.getAlignment().getHeight() > av.getEndSeq())
1660 running = ap.scrollUp(false);
1663 if (mouseDragging && evt.getX() < 0)
1665 running = ap.scrollRight(false);
1668 else if (mouseDragging && evt.getX() >= getSize().width)
1670 running = ap.scrollRight(true);
1677 } catch (Exception ex)
1685 * modify current selection according to a received message.
1687 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1688 SelectionSource source)
1690 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1691 // handles selection messages...
1692 // TODO: extend config options to allow user to control if selections may be
1693 // shared between viewports.
1695 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignViewport) source)
1696 .getSequenceSetId().equals(av.getSequenceSetId()))))
1700 // do we want to thread this ? (contention with seqsel and colsel locks, I
1702 // rules are: colsel is copied if there is a real intersection between
1703 // sequence selection
1704 boolean repaint = false, copycolsel = true;
1705 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1707 SequenceGroup sgroup = null;
1708 if (seqsel != null && seqsel.getSize() > 0)
1710 if (av.getAlignment() == null)
1713 .println("Selection message: alignviewport av SeqSetId="
1714 + av.getSequenceSetId() + " ViewId="
1716 + " 's alignment is NULL! returning immediatly.");
1719 sgroup = seqsel.intersect(av.getAlignment(),
1720 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1721 if ((sgroup == null || sgroup.getSize() == 0)
1722 && (colsel == null || colsel.size() == 0))
1724 // don't copy columns if the region didn't intersect.
1728 if (sgroup != null && sgroup.getSize() > 0)
1730 av.setSelectionGroup(sgroup);
1734 av.setSelectionGroup(null);
1736 repaint = av.isSelectionGroupChanged(true);
1739 && (av.getColumnSelection() == null || !av
1740 .isColSelChanged(true)))
1742 // the current selection is unset or from a previous message
1743 // so import the new colsel.
1744 if (colsel == null || colsel.size() == 0)
1746 if (av.getColumnSelection() != null)
1748 av.getColumnSelection().clear();
1753 // TODO: shift colSel according to the intersecting sequences
1754 if (av.getColumnSelection() == null)
1756 av.setColumnSelection(new ColumnSelection(colsel));
1760 av.getColumnSelection().setElementsFrom(colsel);
1763 repaint |= av.isColSelChanged(true);
1766 && av.hasHiddenColumns()
1767 && (av.getColumnSelection() == null || av.getColumnSelection()
1768 .getHiddenColumns() == null))
1770 System.err.println("Bad things");
1774 ap.scalePanelHolder.repaint();
1780 * scroll to the given row/column - or nearest visible location
1785 public void scrollTo(int row, int column)
1788 row = row < 0 ? ap.av.startSeq : row;
1789 column = column < 0 ? ap.av.startRes : column;
1790 ap.scrollTo(column, column, row, true, true);
1794 * scroll to the given row - or nearest visible location
1798 public void scrollToRow(int row)
1801 row = row < 0 ? ap.av.startSeq : row;
1802 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1806 * scroll to the given column - or nearest visible location
1810 public void scrollToColumn(int column)
1813 column = column < 0 ? ap.av.startRes : column;
1814 ap.scrollTo(column, column, ap.av.startSeq, true, true);