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.featuresDisplayed == null
804 || !av.featuresDisplayed.containsKey(features[i].getType()))
809 if (features[i].featureGroup != null
810 && seqCanvas.fr.featureGroups != null
811 && seqCanvas.fr.featureGroups
812 .containsKey(features[i].featureGroup)
813 && !((Boolean) seqCanvas.fr.featureGroups
814 .get(features[i].featureGroup)).booleanValue())
817 if ((features[i].getBegin() <= res)
818 && (features[i].getEnd() >= res))
820 tmp.addElement(features[i]);
825 features = new SequenceFeature[tmp.size()];
826 tmp.copyInto(features);
833 public void mouseDragged(MouseEvent evt)
835 if (mouseWheelPressed)
837 int oldWidth = av.charWidth;
839 // Which is bigger, left-right or up-down?
840 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
843 int fontSize = av.font.getSize();
845 if (evt.getY() < lastMousePress.y && av.charHeight > 1)
849 else if (evt.getY() > lastMousePress.y)
859 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
860 av.charWidth = oldWidth;
864 if (evt.getX() < lastMousePress.x && av.charWidth > 1)
868 else if (evt.getX() > lastMousePress.x)
873 if (av.charWidth < 1)
881 FontMetrics fm = getFontMetrics(av.getFont());
882 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
884 lastMousePress = evt.getPoint();
886 ap.paintAlignment(false);
887 ap.annotationPanel.image = null;
893 doMouseDraggedDefineMode(evt);
897 int res = findRes(evt);
904 if ((lastres == -1) || (lastres == res))
909 if ((res < av.getAlignment().getWidth()) && (res < lastres))
911 // dragLeft, delete gap
912 editSequence(false, res);
916 editSequence(true, res);
919 mouseDragging = true;
920 if (scrollThread != null)
922 scrollThread.setEvent(evt);
927 synchronized void editSequence(boolean insertGap, int startres)
931 boolean fixedColumns = false;
932 SequenceGroup sg = av.getSelectionGroup();
934 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
936 if (!groupEditing && av.hasHiddenRows())
938 if (av.isHiddenRepSequence(seq))
940 sg = (SequenceGroup) av.getRepresentedSequences(seq);
945 StringBuffer message = new StringBuffer();
948 message.append("Edit group:");
949 if (editCommand == null)
951 editCommand = new EditCommand("Edit Group");
956 message.append("Edit sequence: " + seq.getName());
957 String label = seq.getName();
958 if (label.length() > 10)
960 label = label.substring(0, 10);
962 if (editCommand == null)
964 editCommand = new EditCommand("Edit " + label);
970 message.append(" insert ");
974 message.append(" delete ");
977 message.append(Math.abs(startres - lastres) + " gaps.");
978 ap.alignFrame.statusBar.setText(message.toString());
980 // Are we editing within a selection group?
982 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
987 // sg might be null as the user may only see 1 sequence,
988 // but the sequence represents a group
991 if (!av.isHiddenRepSequence(seq))
997 sg = av.getRepresentedSequences(seq);
1000 fixedLeft = sg.getStartRes();
1001 fixedRight = sg.getEndRes();
1003 if ((startres < fixedLeft && lastres >= fixedLeft)
1004 || (startres >= fixedLeft && lastres < fixedLeft)
1005 || (startres > fixedRight && lastres <= fixedRight)
1006 || (startres <= fixedRight && lastres > fixedRight))
1012 if (fixedLeft > startres)
1014 fixedRight = fixedLeft - 1;
1017 else if (fixedRight < startres)
1019 fixedLeft = fixedRight;
1024 if (av.hasHiddenColumns())
1026 fixedColumns = true;
1027 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1028 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1030 if ((insertGap && startres > y1 && lastres < y1)
1031 || (!insertGap && startres < y2 && lastres > y2))
1037 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1038 // Selection spans a hidden region
1039 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1047 fixedRight = y2 - 1;
1054 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1055 .toArray(new SequenceI[0]);
1060 // If the user has selected the whole sequence, and is dragging to
1061 // the right, we can still extend the alignment and selectionGroup
1062 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1063 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1065 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1066 fixedRight = sg.getEndRes();
1069 // Is it valid with fixed columns??
1070 // Find the next gap before the end
1071 // of the visible region boundary
1072 boolean blank = false;
1073 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1077 for (SequenceI gs : groupSeqs)
1079 for (int j = 0; j < startres - lastres; j++)
1081 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1097 if (sg.getSize() == av.getAlignment().getHeight())
1099 if ((av.hasHiddenColumns() && startres < av
1100 .getColumnSelection().getHiddenBoundaryRight(startres)))
1106 int alWidth = av.getAlignment().getWidth();
1107 if (av.hasHiddenRows())
1109 int hwidth = av.getAlignment().getHiddenSequences()
1111 if (hwidth > alWidth)
1116 // We can still insert gaps if the selectionGroup
1117 // contains all the sequences
1118 sg.setEndRes(sg.getEndRes() + startres - lastres);
1119 fixedRight = alWidth + startres - lastres;
1130 else if (!insertGap)
1132 // / Are we able to delete?
1133 // ie are all columns blank?
1135 for (SequenceI gs : groupSeqs)
1137 for (int j = startres; j < lastres; j++)
1139 if (gs.getLength() <= j)
1144 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1146 // Not a gap, block edit not valid
1156 // dragging to the right
1157 if (fixedColumns && fixedRight != -1)
1159 for (int j = lastres; j < startres; j++)
1161 insertChar(j, groupSeqs, fixedRight);
1166 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1167 startres, startres - lastres, av.getAlignment(), true);
1172 // dragging to the left
1173 if (fixedColumns && fixedRight != -1)
1175 for (int j = lastres; j > startres; j--)
1177 deleteChar(startres, groupSeqs, fixedRight);
1182 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1183 startres, lastres - startres, av.getAlignment(), true);
1189 // ///Editing a single sequence///////////
1193 // dragging to the right
1194 if (fixedColumns && fixedRight != -1)
1196 for (int j = lastres; j < startres; j++)
1198 insertChar(j, new SequenceI[]
1199 { seq }, fixedRight);
1204 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1205 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1210 // dragging to the left
1211 if (fixedColumns && fixedRight != -1)
1213 for (int j = lastres; j > startres; j--)
1215 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1220 deleteChar(startres, new SequenceI[]
1221 { seq }, fixedRight);
1226 // could be a keyboard edit trying to delete none gaps
1228 for (int m = startres; m < lastres; m++)
1230 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1239 editCommand.appendEdit(EditCommand.DELETE_GAP, new SequenceI[]
1240 { seq }, startres, max, av.getAlignment(), true);
1247 seqCanvas.repaint();
1250 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1252 int blankColumn = fixedColumn;
1253 for (int s = 0; s < seq.length; s++)
1255 // Find the next gap before the end of the visible region boundary
1256 // If lastCol > j, theres a boundary after the gap insertion
1258 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1260 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1262 // Theres a space, so break and insert the gap
1267 if (blankColumn <= j)
1269 blankColumn = fixedColumn;
1275 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1276 av.getAlignment(), true);
1278 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1,
1279 av.getAlignment(), true);
1283 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1286 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1,
1287 av.getAlignment(), true);
1289 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1290 av.getAlignment(), true);
1293 // ////////////////////////////////////////
1294 // ///Everything below this is for defining the boundary of the rubberband
1295 // ////////////////////////////////////////
1296 public void doMousePressedDefineMode(MouseEvent evt)
1298 if (scrollThread != null)
1300 scrollThread.running = false;
1301 scrollThread = null;
1304 int res = findRes(evt);
1305 int seq = findSeq(evt);
1307 startWrapBlock = wrappedBlock;
1314 SequenceI sequence = (Sequence) av.getAlignment().getSequenceAt(seq);
1316 if (sequence == null || res > sequence.getLength())
1321 stretchGroup = av.getSelectionGroup();
1323 if (stretchGroup == null)
1325 stretchGroup = av.getAlignment().findGroup(sequence);
1326 if (stretchGroup != null && res > stretchGroup.getStartRes()
1327 && res < stretchGroup.getEndRes())
1329 av.setSelectionGroup(stretchGroup);
1333 stretchGroup = null;
1337 else if (!stretchGroup.getSequences(null).contains(sequence)
1338 || stretchGroup.getStartRes() > res
1339 || stretchGroup.getEndRes() < res)
1341 stretchGroup = null;
1343 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1345 if (allGroups != null)
1347 for (int i = 0; i < allGroups.length; i++)
1349 if (allGroups[i].getStartRes() <= res
1350 && allGroups[i].getEndRes() >= res)
1352 stretchGroup = allGroups[i];
1357 av.setSelectionGroup(stretchGroup);
1360 // DETECT RIGHT MOUSE BUTTON IN AWT
1361 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1363 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1364 sequence.findPosition(res));
1366 Vector links = null;
1367 if (allFeatures != null)
1369 for (int i = 0; i < allFeatures.length; i++)
1371 if (allFeatures[i].links != null)
1375 links = new Vector();
1377 for (int j = 0; j < allFeatures[i].links.size(); j++)
1379 links.addElement(allFeatures[i].links.elementAt(j));
1384 APopupMenu popup = new APopupMenu(ap, null, links);
1386 popup.show(this, evt.getX(), evt.getY());
1392 seqCanvas.cursorX = findRes(evt);
1393 seqCanvas.cursorY = findSeq(evt);
1394 seqCanvas.repaint();
1398 // Only if left mouse button do we want to change group sizes
1400 if (stretchGroup == null)
1402 // define a new group here
1403 SequenceGroup sg = new SequenceGroup();
1404 sg.setStartRes(res);
1406 sg.addSequence(sequence, false);
1407 av.setSelectionGroup(sg);
1410 if (av.getConservationSelected())
1412 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1415 if (av.getAbovePIDThreshold())
1417 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1424 public void doMouseReleasedDefineMode(MouseEvent evt)
1426 if (stretchGroup == null)
1431 stretchGroup.recalcConservation(); // always do this - annotation has own
1433 if (stretchGroup.cs != null)
1435 stretchGroup.cs.alignmentChanged(stretchGroup,
1436 av.getHiddenRepSequences());
1438 if (stretchGroup.cs.conservationApplied())
1440 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1441 stretchGroup.getName());
1445 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1446 stretchGroup.getName());
1449 changeEndRes = false;
1450 changeStartRes = false;
1451 stretchGroup = null;
1452 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1453 ap.paintAlignment(true);
1457 public void doMouseDraggedDefineMode(MouseEvent evt)
1459 int res = findRes(evt);
1460 int y = findSeq(evt);
1462 if (wrappedBlock != startWrapBlock)
1467 if (stretchGroup == null)
1472 mouseDragging = true;
1474 if (y > av.getAlignment().getHeight())
1476 y = av.getAlignment().getHeight() - 1;
1479 if (res >= av.getAlignment().getWidth())
1481 res = av.getAlignment().getWidth() - 1;
1484 if (stretchGroup.getEndRes() == res)
1486 // Edit end res position of selected group
1487 changeEndRes = true;
1489 else if (stretchGroup.getStartRes() == res)
1491 // Edit start res position of selected group
1492 changeStartRes = true;
1502 if (res > (stretchGroup.getStartRes() - 1))
1504 stretchGroup.setEndRes(res);
1507 else if (changeStartRes)
1509 if (res < (stretchGroup.getEndRes() + 1))
1511 stretchGroup.setStartRes(res);
1515 int dragDirection = 0;
1521 else if (y < oldSeq)
1526 while ((y != oldSeq) && (oldSeq > -1)
1527 && (y < av.getAlignment().getHeight()))
1529 // This routine ensures we don't skip any sequences, as the
1530 // selection is quite slow.
1531 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1533 oldSeq += dragDirection;
1540 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1542 if (stretchGroup.getSequences(null).contains(nextSeq))
1544 stretchGroup.deleteSequence(seq, false);
1550 stretchGroup.addSequence(seq, false);
1553 stretchGroup.addSequence(nextSeq, false);
1562 if (res > av.endRes || res < av.startRes || y < av.startSeq
1568 if (scrollThread != null)
1570 scrollThread.setEvent(evt);
1573 seqCanvas.repaint();
1576 public void mouseEntered(MouseEvent e)
1583 if (scrollThread != null)
1585 scrollThread.running = false;
1586 scrollThread = null;
1590 public void mouseExited(MouseEvent e)
1592 if (av.getWrapAlignment())
1597 if (mouseDragging && scrollThread == null)
1599 scrollThread = new ScrollThread();
1603 void scrollCanvas(MouseEvent evt)
1607 if (scrollThread != null)
1609 scrollThread.running = false;
1610 scrollThread = null;
1612 mouseDragging = false;
1616 if (scrollThread == null)
1618 scrollThread = new ScrollThread();
1621 mouseDragging = true;
1622 scrollThread.setEvent(evt);
1627 // this class allows scrolling off the bottom of the visible alignment
1628 class ScrollThread extends Thread
1632 boolean running = false;
1634 public ScrollThread()
1639 public void setEvent(MouseEvent e)
1644 public void stopScrolling()
1658 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1660 running = ap.scrollUp(true);
1663 if (mouseDragging && evt.getY() >= getSize().height
1664 && av.getAlignment().getHeight() > av.getEndSeq())
1666 running = ap.scrollUp(false);
1669 if (mouseDragging && evt.getX() < 0)
1671 running = ap.scrollRight(false);
1674 else if (mouseDragging && evt.getX() >= getSize().width)
1676 running = ap.scrollRight(true);
1683 } catch (Exception ex)
1691 * modify current selection according to a received message.
1693 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1694 SelectionSource source)
1696 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1697 // handles selection messages...
1698 // TODO: extend config options to allow user to control if selections may be
1699 // shared between viewports.
1701 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignViewport) source)
1702 .getSequenceSetId().equals(av.getSequenceSetId()))))
1706 // do we want to thread this ? (contention with seqsel and colsel locks, I
1708 // rules are: colsel is copied if there is a real intersection between
1709 // sequence selection
1710 boolean repaint = false, copycolsel = true;
1711 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1713 SequenceGroup sgroup = null;
1714 if (seqsel != null && seqsel.getSize() > 0)
1716 if (av.getAlignment() == null)
1719 .println("Selection message: alignviewport av SeqSetId="
1720 + av.getSequenceSetId() + " ViewId="
1722 + " 's alignment is NULL! returning immediatly.");
1725 sgroup = seqsel.intersect(av.getAlignment(),
1726 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1727 if ((sgroup == null || sgroup.getSize() == 0)
1728 && (colsel == null || colsel.size() == 0))
1730 // don't copy columns if the region didn't intersect.
1734 if (sgroup != null && sgroup.getSize() > 0)
1736 av.setSelectionGroup(sgroup);
1740 av.setSelectionGroup(null);
1742 repaint = av.isSelectionGroupChanged(true);
1745 && (av.getColumnSelection() == null || !av
1746 .isColSelChanged(true)))
1748 // the current selection is unset or from a previous message
1749 // so import the new colsel.
1750 if (colsel == null || colsel.size() == 0)
1752 if (av.getColumnSelection() != null)
1754 av.getColumnSelection().clear();
1759 // TODO: shift colSel according to the intersecting sequences
1760 if (av.getColumnSelection() == null)
1762 av.setColumnSelection(new ColumnSelection(colsel));
1766 av.getColumnSelection().setElementsFrom(colsel);
1769 repaint |= av.isColSelChanged(true);
1772 && av.hasHiddenColumns()
1773 && (av.getColumnSelection() == null || av.getColumnSelection()
1774 .getHiddenColumns() == null))
1776 System.err.println("Bad things");
1780 ap.scalePanelHolder.repaint();
1786 * scroll to the given row/column - or nearest visible location
1791 public void scrollTo(int row, int column)
1794 row = row < 0 ? ap.av.startSeq : row;
1795 column = column < 0 ? ap.av.startRes : column;
1796 ap.scrollTo(column, column, row, true, true);
1800 * scroll to the given row - or nearest visible location
1804 public void scrollToRow(int row)
1807 row = row < 0 ? ap.av.startSeq : row;
1808 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1812 * scroll to the given column - or nearest visible location
1816 public void scrollToColumn(int column)
1819 column = column < 0 ? ap.av.startRes : column;
1820 ap.scrollTo(column, column, ap.av.startSeq, true, true);