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;
23 import jalview.commands.EditCommand;
24 import jalview.commands.EditCommand.Action;
25 import jalview.datamodel.ColumnSelection;
26 import jalview.datamodel.SearchResults;
27 import jalview.datamodel.Sequence;
28 import jalview.datamodel.SequenceFeature;
29 import jalview.datamodel.SequenceGroup;
30 import jalview.datamodel.SequenceI;
31 import jalview.schemes.ResidueProperties;
32 import jalview.structure.SelectionSource;
33 import jalview.structure.SequenceListener;
34 import jalview.structure.StructureSelectionManager;
35 import jalview.structure.VamsasSource;
36 import jalview.util.MessageManager;
38 import java.awt.BorderLayout;
40 import java.awt.FontMetrics;
41 import java.awt.Panel;
42 import java.awt.Point;
43 import java.awt.event.InputEvent;
44 import java.awt.event.MouseEvent;
45 import java.awt.event.MouseListener;
46 import java.awt.event.MouseMotionListener;
47 import java.util.Vector;
49 public class SeqPanel extends Panel implements MouseMotionListener,
50 MouseListener, SequenceListener
53 public SeqCanvas seqCanvas;
55 public AlignmentPanel ap;
57 protected int lastres;
59 protected int startseq;
61 protected AlignViewport av;
63 // if character is inserted or deleted, we will need to recalculate the
65 boolean seqEditOccurred = false;
67 ScrollThread scrollThread = null;
69 boolean mouseDragging = false;
71 boolean editingSeqs = false;
73 boolean groupEditing = false;
77 boolean changeEndSeq = false;
79 boolean changeStartSeq = false;
81 boolean changeEndRes = false;
83 boolean changeStartRes = false;
85 SequenceGroup stretchGroup = null;
87 StringBuffer keyboardNo1;
89 StringBuffer keyboardNo2;
91 boolean mouseWheelPressed = false;
95 EditCommand editCommand;
97 StructureSelectionManager ssm;
99 public SeqPanel(AlignViewport avp, AlignmentPanel p)
103 seqCanvas = new SeqCanvas(avp);
104 setLayout(new BorderLayout());
109 seqCanvas.addMouseMotionListener(this);
110 seqCanvas.addMouseListener(this);
111 ssm = StructureSelectionManager.getStructureSelectionManager(av.applet);
112 ssm.addStructureViewerListener(this);
119 if (editCommand != null && editCommand.getSize() > 0)
121 ap.alignFrame.addHistoryItem(editCommand);
122 av.firePropertyChange("alignment", null, av.getAlignment()
129 groupEditing = false;
137 seqCanvas.cursorY = getKeyboardNo1() - 1;
141 void setCursorColumn()
143 seqCanvas.cursorX = getKeyboardNo1() - 1;
147 void setCursorRowAndColumn()
149 if (keyboardNo2 == null)
151 keyboardNo2 = new StringBuffer();
155 seqCanvas.cursorX = getKeyboardNo1() - 1;
156 seqCanvas.cursorY = getKeyboardNo2() - 1;
161 void setCursorPosition()
163 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
165 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
169 void moveCursor(int dx, int dy)
171 seqCanvas.cursorX += dx;
172 seqCanvas.cursorY += dy;
173 if (av.hasHiddenColumns()
174 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
176 int original = seqCanvas.cursorX - dx;
177 int maxWidth = av.getAlignment().getWidth();
179 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
180 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
182 seqCanvas.cursorX += dx;
185 if (seqCanvas.cursorX >= maxWidth
186 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
188 seqCanvas.cursorX = original;
194 void scrollToVisible()
196 if (seqCanvas.cursorX < 0)
198 seqCanvas.cursorX = 0;
200 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
202 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
205 if (seqCanvas.cursorY < 0)
207 seqCanvas.cursorY = 0;
209 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
211 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
215 if (av.wrapAlignment)
217 ap.scrollToWrappedVisible(seqCanvas.cursorX);
221 while (seqCanvas.cursorY < av.startSeq)
225 while (seqCanvas.cursorY + 1 > av.endSeq)
229 while (seqCanvas.cursorX < av.getColumnSelection()
230 .adjustForHiddenColumns(av.startRes))
233 if (!ap.scrollRight(false))
238 while (seqCanvas.cursorX > av.getColumnSelection()
239 .adjustForHiddenColumns(av.endRes))
241 if (!ap.scrollRight(true))
247 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
248 seqCanvas.cursorX, seqCanvas.cursorY);
253 void setSelectionAreaAtCursor(boolean topLeft)
255 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
257 if (av.getSelectionGroup() != null)
259 SequenceGroup sg = av.getSelectionGroup();
260 // Find the top and bottom of this group
261 int min = av.getAlignment().getHeight(), max = 0;
262 for (int i = 0; i < sg.getSize(); i++)
264 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
279 sg.setStartRes(seqCanvas.cursorX);
280 if (sg.getEndRes() < seqCanvas.cursorX)
282 sg.setEndRes(seqCanvas.cursorX);
285 min = seqCanvas.cursorY;
289 sg.setEndRes(seqCanvas.cursorX);
290 if (sg.getStartRes() > seqCanvas.cursorX)
292 sg.setStartRes(seqCanvas.cursorX);
295 max = seqCanvas.cursorY + 1;
300 // Only the user can do this
301 av.setSelectionGroup(null);
305 // Now add any sequences between min and max
307 for (int i = min; i < max; i++)
309 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
314 if (av.getSelectionGroup() == null)
316 SequenceGroup sg = new SequenceGroup();
317 sg.setStartRes(seqCanvas.cursorX);
318 sg.setEndRes(seqCanvas.cursorX);
319 sg.addSequence(sequence, false);
320 av.setSelectionGroup(sg);
322 ap.paintAlignment(false);
326 void insertGapAtCursor(boolean group)
328 groupEditing = group;
329 startseq = seqCanvas.cursorY;
330 lastres = seqCanvas.cursorX;
331 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
335 void deleteGapAtCursor(boolean group)
337 groupEditing = group;
338 startseq = seqCanvas.cursorY;
339 lastres = seqCanvas.cursorX + getKeyboardNo1();
340 editSequence(false, seqCanvas.cursorX);
344 void numberPressed(char value)
346 if (keyboardNo1 == null)
348 keyboardNo1 = new StringBuffer();
351 if (keyboardNo2 != null)
353 keyboardNo2.append(value);
357 keyboardNo1.append(value);
365 if (keyboardNo1 != null)
367 int value = Integer.parseInt(keyboardNo1.toString());
371 } catch (Exception x)
382 if (keyboardNo2 != null)
384 int value = Integer.parseInt(keyboardNo2.toString());
388 } catch (Exception x)
395 void setStatusMessage(SequenceI sequence, int res, int seq)
397 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
398 + sequence.getName());
401 if (av.getAlignment().isNucleotide())
403 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
407 text.append(" Nucleotide: ");
412 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
415 text.append(" Residue: ");
424 text.append(obj + " (" + sequence.findPosition(res) + ")");
428 ap.alignFrame.statusBar.setText(text.toString());
432 public void mousePressed(MouseEvent evt)
434 lastMousePress = evt.getPoint();
436 // For now, ignore the mouseWheel font resizing on Macs
437 // As the Button2_mask always seems to be true
438 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
441 mouseWheelPressed = true;
445 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
447 if (evt.isControlDown() || evt.isAltDown())
455 doMousePressedDefineMode(evt);
459 int seq = findSeq(evt);
460 int res = findRes(evt);
462 if (seq < 0 || res < 0)
467 if ((seq < av.getAlignment().getHeight())
468 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
482 public void mouseClicked(MouseEvent evt)
484 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
485 if (evt.getClickCount() > 1)
487 if (av.getSelectionGroup() != null
488 && av.getSelectionGroup().getSize() == 1
489 && av.getSelectionGroup().getEndRes()
490 - av.getSelectionGroup().getStartRes() < 2)
492 av.setSelectionGroup(null);
495 SequenceFeature[] features = findFeaturesAtRes(sequence,
496 sequence.findPosition(findRes(evt)));
498 if (features != null && features.length > 0)
500 SearchResults highlight = new SearchResults();
501 highlight.addResult(sequence, features[0].getBegin(),
502 features[0].getEnd());
503 seqCanvas.highlightSearchResults(highlight);
505 if (features != null && features.length > 0)
507 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
508 { sequence }, features, false, ap);
510 seqCanvas.highlightSearchResults(null);
515 public void mouseReleased(MouseEvent evt)
517 mouseDragging = false;
518 mouseWheelPressed = false;
519 ap.paintAlignment(true);
523 doMouseReleasedDefineMode(evt);
531 int startWrapBlock = -1;
533 int wrappedBlock = -1;
535 int findRes(MouseEvent evt)
540 if (av.wrapAlignment)
543 int hgap = av.charHeight;
544 if (av.scaleAboveWrapped)
546 hgap += av.charHeight;
549 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
550 + seqCanvas.getAnnotationHeight();
554 x -= seqCanvas.LABEL_WEST;
556 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
562 wrappedBlock = y / cHeight;
563 wrappedBlock += av.getStartRes() / cwidth;
565 res = wrappedBlock * cwidth + x / av.getCharWidth();
570 res = (x / av.getCharWidth()) + av.getStartRes();
573 if (av.hasHiddenColumns())
575 res = av.getColumnSelection().adjustForHiddenColumns(res);
582 int findSeq(MouseEvent evt)
584 final int sqnum = findAlRow(evt);
585 return (sqnum < 0) ? 0 : sqnum;
591 * @return row in alignment that was selected (or -1 for column selection)
593 private int findAlRow(MouseEvent evt)
598 if (av.wrapAlignment)
600 int hgap = av.charHeight;
601 if (av.scaleAboveWrapped)
603 hgap += av.charHeight;
606 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
607 + seqCanvas.getAnnotationHeight();
611 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
620 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
621 .getAlignment().getHeight() - 1);
631 public void doMousePressed(MouseEvent evt)
634 int seq = findSeq(evt);
635 int res = findRes(evt);
637 if (seq < av.getAlignment().getHeight()
638 && res < av.getAlignment().getSequenceAt(seq).getLength())
640 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
641 // Find the residue's position in the sequence (res is the position
658 public void mouseOverSequence(SequenceI sequence, int index, int pos)
660 String tmp = sequence.hashCode() + index + "";
661 if (lastMessage == null || !lastMessage.equals(tmp))
663 ssm.mouseOverSequence(sequence, index, pos, av);
669 public void highlightSequence(SearchResults results)
671 if (av.followHighlight)
673 if (ap.scrollToPosition(results, true))
675 ap.alignFrame.repaint();
678 seqCanvas.highlightSearchResults(results);
683 public VamsasSource getVamsasSource()
685 return this.ap == null ? null : this.ap.av;
688 public void updateColours(SequenceI seq, int index)
690 System.out.println("update the seqPanel colours");
694 public void mouseMoved(MouseEvent evt)
696 int res = findRes(evt);
697 int seq = findSeq(evt);
699 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
708 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
709 if (res > sequence.getLength())
718 int respos = sequence.findPosition(res);
721 mouseOverSequence(sequence, res, respos);
724 StringBuilder text = new StringBuilder();
725 text.append("Sequence ").append(Integer.toString(seq + 1))
726 .append(" ID: ").append(sequence.getName());
729 final String ch = String.valueOf(sequence.getCharAt(res));
730 if (av.getAlignment().isNucleotide())
732 obj = ResidueProperties.nucleotideName.get(ch);
735 text.append(" Nucleotide: ").append(obj);
740 obj = "X".equalsIgnoreCase(ch) ? "STOP"
741 : ResidueProperties.aa2Triplet.get(ch);
744 text.append(" Residue: ").append(obj);
750 text.append(" (").append(Integer.toString(respos)).append(")");
753 ap.alignFrame.statusBar.setText(text.toString());
755 StringBuilder tooltipText = new StringBuilder();
756 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
759 for (int g = 0; g < groups.length; g++)
761 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
763 if (!groups[g].getName().startsWith("JTreeGroup")
764 && !groups[g].getName().startsWith("JGroup"))
766 tooltipText.append(groups[g].getName()).append(" ");
768 if (groups[g].getDescription() != null)
770 tooltipText.append(groups[g].getDescription());
772 tooltipText.append("\n");
777 // use aa to see if the mouse pointer is on a
778 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
779 sequence.findPosition(res));
782 while (index < allFeatures.length)
784 SequenceFeature sf = allFeatures[index];
786 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
788 if (sf.getDescription() != null)
790 tooltipText.append(" " + sf.getDescription());
793 if (sf.getValue("status") != null)
795 String status = sf.getValue("status").toString();
796 if (status.length() > 0)
798 tooltipText.append(" (" + sf.getValue("status") + ")");
801 tooltipText.append("\n");
808 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
812 tooltip.setTip(tooltipText.toString());
816 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
818 Vector tmp = new Vector();
819 SequenceFeature[] features = sequence.getSequenceFeatures();
820 if (features != null)
822 for (int i = 0; i < features.length; i++)
824 if (av.featuresDisplayed == null
825 || !av.featuresDisplayed.containsKey(features[i].getType()))
830 if (features[i].featureGroup != null
831 && seqCanvas.fr.featureGroups != null
832 && seqCanvas.fr.featureGroups
833 .containsKey(features[i].featureGroup)
834 && !((Boolean) seqCanvas.fr.featureGroups
835 .get(features[i].featureGroup)).booleanValue())
840 if ((features[i].getBegin() <= res)
841 && (features[i].getEnd() >= res))
843 tmp.addElement(features[i]);
848 features = new SequenceFeature[tmp.size()];
849 tmp.copyInto(features);
856 public void mouseDragged(MouseEvent evt)
858 if (mouseWheelPressed)
860 int oldWidth = av.charWidth;
862 // Which is bigger, left-right or up-down?
863 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
866 int fontSize = av.font.getSize();
868 if (evt.getY() < lastMousePress.y && av.charHeight > 1)
872 else if (evt.getY() > lastMousePress.y)
882 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
883 av.charWidth = oldWidth;
887 if (evt.getX() < lastMousePress.x && av.charWidth > 1)
891 else if (evt.getX() > lastMousePress.x)
896 if (av.charWidth < 1)
904 FontMetrics fm = getFontMetrics(av.getFont());
905 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
907 lastMousePress = evt.getPoint();
909 ap.paintAlignment(false);
910 ap.annotationPanel.image = null;
916 doMouseDraggedDefineMode(evt);
920 int res = findRes(evt);
927 if ((lastres == -1) || (lastres == res))
932 if ((res < av.getAlignment().getWidth()) && (res < lastres))
934 // dragLeft, delete gap
935 editSequence(false, res);
939 editSequence(true, res);
942 mouseDragging = true;
943 if (scrollThread != null)
945 scrollThread.setEvent(evt);
950 synchronized void editSequence(boolean insertGap, int startres)
954 boolean fixedColumns = false;
955 SequenceGroup sg = av.getSelectionGroup();
957 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
959 if (!groupEditing && av.hasHiddenRows())
961 if (av.isHiddenRepSequence(seq))
963 sg = av.getRepresentedSequences(seq);
968 StringBuffer message = new StringBuffer();
971 message.append(MessageManager.getString("action.edit_group")).append(
973 if (editCommand == null)
975 editCommand = new EditCommand(
976 MessageManager.getString("action.edit_group"));
981 message.append(MessageManager.getString("label.edit_sequence"))
982 .append(" " + seq.getName());
983 String label = seq.getName();
984 if (label.length() > 10)
986 label = label.substring(0, 10);
988 if (editCommand == null)
990 editCommand = new EditCommand(MessageManager.formatMessage(
991 "label.edit_params", new String[]
998 message.append(" insert ");
1002 message.append(" delete ");
1005 message.append(Math.abs(startres - lastres) + " gaps.");
1006 ap.alignFrame.statusBar.setText(message.toString());
1008 // Are we editing within a selection group?
1010 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1013 fixedColumns = true;
1015 // sg might be null as the user may only see 1 sequence,
1016 // but the sequence represents a group
1019 if (!av.isHiddenRepSequence(seq))
1025 sg = av.getRepresentedSequences(seq);
1028 fixedLeft = sg.getStartRes();
1029 fixedRight = sg.getEndRes();
1031 if ((startres < fixedLeft && lastres >= fixedLeft)
1032 || (startres >= fixedLeft && lastres < fixedLeft)
1033 || (startres > fixedRight && lastres <= fixedRight)
1034 || (startres <= fixedRight && lastres > fixedRight))
1040 if (fixedLeft > startres)
1042 fixedRight = fixedLeft - 1;
1045 else if (fixedRight < startres)
1047 fixedLeft = fixedRight;
1052 if (av.hasHiddenColumns())
1054 fixedColumns = true;
1055 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1056 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1058 if ((insertGap && startres > y1 && lastres < y1)
1059 || (!insertGap && startres < y2 && lastres > y2))
1065 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1066 // Selection spans a hidden region
1067 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1075 fixedRight = y2 - 1;
1082 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1083 .toArray(new SequenceI[0]);
1088 // If the user has selected the whole sequence, and is dragging to
1089 // the right, we can still extend the alignment and selectionGroup
1090 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1091 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1093 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1094 fixedRight = sg.getEndRes();
1097 // Is it valid with fixed columns??
1098 // Find the next gap before the end
1099 // of the visible region boundary
1100 boolean blank = false;
1101 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1105 for (SequenceI gs : groupSeqs)
1107 for (int j = 0; j < startres - lastres; j++)
1109 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1125 if (sg.getSize() == av.getAlignment().getHeight())
1127 if ((av.hasHiddenColumns() && startres < av
1128 .getColumnSelection().getHiddenBoundaryRight(startres)))
1134 int alWidth = av.getAlignment().getWidth();
1135 if (av.hasHiddenRows())
1137 int hwidth = av.getAlignment().getHiddenSequences()
1139 if (hwidth > alWidth)
1144 // We can still insert gaps if the selectionGroup
1145 // contains all the sequences
1146 sg.setEndRes(sg.getEndRes() + startres - lastres);
1147 fixedRight = alWidth + startres - lastres;
1158 else if (!insertGap)
1160 // / Are we able to delete?
1161 // ie are all columns blank?
1163 for (SequenceI gs : groupSeqs)
1165 for (int j = startres; j < lastres; j++)
1167 if (gs.getLength() <= j)
1172 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1174 // Not a gap, block edit not valid
1184 // dragging to the right
1185 if (fixedColumns && fixedRight != -1)
1187 for (int j = lastres; j < startres; j++)
1189 insertChar(j, groupSeqs, fixedRight);
1194 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1195 startres - lastres, av.getAlignment(), true);
1200 // dragging to the left
1201 if (fixedColumns && fixedRight != -1)
1203 for (int j = lastres; j > startres; j--)
1205 deleteChar(startres, groupSeqs, fixedRight);
1210 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1211 lastres - startres, av.getAlignment(), true);
1217 // ///Editing a single sequence///////////
1221 // dragging to the right
1222 if (fixedColumns && fixedRight != -1)
1224 for (int j = lastres; j < startres; j++)
1226 insertChar(j, new SequenceI[]
1227 { seq }, fixedRight);
1232 editCommand.appendEdit(Action.INSERT_GAP, new SequenceI[]
1233 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1238 // dragging to the left
1239 if (fixedColumns && fixedRight != -1)
1241 for (int j = lastres; j > startres; j--)
1243 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1248 deleteChar(startres, new SequenceI[]
1249 { seq }, fixedRight);
1254 // could be a keyboard edit trying to delete none gaps
1256 for (int m = startres; m < lastres; m++)
1258 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1267 editCommand.appendEdit(Action.DELETE_GAP, new SequenceI[]
1268 { seq }, startres, max, av.getAlignment(), true);
1275 seqCanvas.repaint();
1278 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1280 int blankColumn = fixedColumn;
1281 for (int s = 0; s < seq.length; s++)
1283 // Find the next gap before the end of the visible region boundary
1284 // If lastCol > j, theres a boundary after the gap insertion
1286 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1288 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1290 // Theres a space, so break and insert the gap
1295 if (blankColumn <= j)
1297 blankColumn = fixedColumn;
1303 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1304 av.getAlignment(), true);
1306 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1311 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1314 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1317 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1318 av.getAlignment(), true);
1321 // ////////////////////////////////////////
1322 // ///Everything below this is for defining the boundary of the rubberband
1323 // ////////////////////////////////////////
1324 public void doMousePressedDefineMode(MouseEvent evt)
1326 if (scrollThread != null)
1328 scrollThread.running = false;
1329 scrollThread = null;
1332 int res = findRes(evt);
1333 int seq = findSeq(evt);
1335 startWrapBlock = wrappedBlock;
1342 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1344 if (sequence == null || res > sequence.getLength())
1349 stretchGroup = av.getSelectionGroup();
1351 if (stretchGroup == null)
1353 stretchGroup = av.getAlignment().findGroup(sequence);
1354 if (stretchGroup != null && res > stretchGroup.getStartRes()
1355 && res < stretchGroup.getEndRes())
1357 av.setSelectionGroup(stretchGroup);
1361 stretchGroup = null;
1365 else if (!stretchGroup.getSequences(null).contains(sequence)
1366 || stretchGroup.getStartRes() > res
1367 || stretchGroup.getEndRes() < res)
1369 stretchGroup = null;
1371 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1373 if (allGroups != null)
1375 for (int i = 0; i < allGroups.length; i++)
1377 if (allGroups[i].getStartRes() <= res
1378 && allGroups[i].getEndRes() >= res)
1380 stretchGroup = allGroups[i];
1385 av.setSelectionGroup(stretchGroup);
1388 // DETECT RIGHT MOUSE BUTTON IN AWT
1389 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1391 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1392 sequence.findPosition(res));
1394 Vector links = null;
1395 if (allFeatures != null)
1397 for (int i = 0; i < allFeatures.length; i++)
1399 if (allFeatures[i].links != null)
1403 links = new Vector();
1405 for (int j = 0; j < allFeatures[i].links.size(); j++)
1407 links.addElement(allFeatures[i].links.elementAt(j));
1412 APopupMenu popup = new APopupMenu(ap, null, links);
1414 popup.show(this, evt.getX(), evt.getY());
1420 seqCanvas.cursorX = findRes(evt);
1421 seqCanvas.cursorY = findSeq(evt);
1422 seqCanvas.repaint();
1426 // Only if left mouse button do we want to change group sizes
1428 if (stretchGroup == null)
1430 // define a new group here
1431 SequenceGroup sg = new SequenceGroup();
1432 sg.setStartRes(res);
1434 sg.addSequence(sequence, false);
1435 av.setSelectionGroup(sg);
1438 if (av.getConservationSelected())
1440 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1443 if (av.getAbovePIDThreshold())
1445 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1452 public void doMouseReleasedDefineMode(MouseEvent evt)
1454 if (stretchGroup == null)
1459 stretchGroup.recalcConservation(); // always do this - annotation has own
1461 if (stretchGroup.cs != null)
1463 stretchGroup.cs.alignmentChanged(stretchGroup,
1464 av.getHiddenRepSequences());
1466 if (stretchGroup.cs.conservationApplied())
1468 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1469 stretchGroup.getName());
1473 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1474 stretchGroup.getName());
1477 changeEndRes = false;
1478 changeStartRes = false;
1479 stretchGroup = null;
1480 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1481 ap.paintAlignment(true);
1485 public void doMouseDraggedDefineMode(MouseEvent evt)
1487 int res = findRes(evt);
1488 int y = findSeq(evt);
1490 if (wrappedBlock != startWrapBlock)
1495 if (stretchGroup == null)
1500 mouseDragging = true;
1502 if (y > av.getAlignment().getHeight())
1504 y = av.getAlignment().getHeight() - 1;
1507 if (res >= av.getAlignment().getWidth())
1509 res = av.getAlignment().getWidth() - 1;
1512 if (stretchGroup.getEndRes() == res)
1514 // Edit end res position of selected group
1515 changeEndRes = true;
1517 else if (stretchGroup.getStartRes() == res)
1519 // Edit start res position of selected group
1520 changeStartRes = true;
1530 if (res > (stretchGroup.getStartRes() - 1))
1532 stretchGroup.setEndRes(res);
1535 else if (changeStartRes)
1537 if (res < (stretchGroup.getEndRes() + 1))
1539 stretchGroup.setStartRes(res);
1543 int dragDirection = 0;
1549 else if (y < oldSeq)
1554 while ((y != oldSeq) && (oldSeq > -1)
1555 && (y < av.getAlignment().getHeight()))
1557 // This routine ensures we don't skip any sequences, as the
1558 // selection is quite slow.
1559 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1561 oldSeq += dragDirection;
1568 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1570 if (stretchGroup.getSequences(null).contains(nextSeq))
1572 stretchGroup.deleteSequence(seq, false);
1578 stretchGroup.addSequence(seq, false);
1581 stretchGroup.addSequence(nextSeq, false);
1590 if (res > av.endRes || res < av.startRes || y < av.startSeq
1596 if (scrollThread != null)
1598 scrollThread.setEvent(evt);
1601 seqCanvas.repaint();
1604 public void mouseEntered(MouseEvent e)
1611 if (scrollThread != null)
1613 scrollThread.running = false;
1614 scrollThread = null;
1618 public void mouseExited(MouseEvent e)
1620 if (av.getWrapAlignment())
1625 if (mouseDragging && scrollThread == null)
1627 scrollThread = new ScrollThread();
1631 void scrollCanvas(MouseEvent evt)
1635 if (scrollThread != null)
1637 scrollThread.running = false;
1638 scrollThread = null;
1640 mouseDragging = false;
1644 if (scrollThread == null)
1646 scrollThread = new ScrollThread();
1649 mouseDragging = true;
1650 scrollThread.setEvent(evt);
1655 // this class allows scrolling off the bottom of the visible alignment
1656 class ScrollThread extends Thread
1660 boolean running = false;
1662 public ScrollThread()
1667 public void setEvent(MouseEvent e)
1672 public void stopScrolling()
1686 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1688 running = ap.scrollUp(true);
1691 if (mouseDragging && evt.getY() >= getSize().height
1692 && av.getAlignment().getHeight() > av.getEndSeq())
1694 running = ap.scrollUp(false);
1697 if (mouseDragging && evt.getX() < 0)
1699 running = ap.scrollRight(false);
1702 else if (mouseDragging && evt.getX() >= getSize().width)
1704 running = ap.scrollRight(true);
1711 } catch (Exception ex)
1719 * modify current selection according to a received message.
1721 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1722 SelectionSource source)
1724 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1725 // handles selection messages...
1726 // TODO: extend config options to allow user to control if selections may be
1727 // shared between viewports.
1729 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignViewport) source)
1730 .getSequenceSetId().equals(av.getSequenceSetId()))))
1734 // do we want to thread this ? (contention with seqsel and colsel locks, I
1736 // rules are: colsel is copied if there is a real intersection between
1737 // sequence selection
1738 boolean repaint = false, copycolsel = true;
1739 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1741 SequenceGroup sgroup = null;
1742 if (seqsel != null && seqsel.getSize() > 0)
1744 if (av.getAlignment() == null)
1747 .println("Selection message: alignviewport av SeqSetId="
1748 + av.getSequenceSetId() + " ViewId="
1750 + " 's alignment is NULL! returning immediatly.");
1753 sgroup = seqsel.intersect(av.getAlignment(),
1754 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1755 if ((sgroup == null || sgroup.getSize() == 0)
1756 && (colsel == null || colsel.size() == 0))
1758 // don't copy columns if the region didn't intersect.
1762 if (sgroup != null && sgroup.getSize() > 0)
1764 av.setSelectionGroup(sgroup);
1768 av.setSelectionGroup(null);
1770 repaint = av.isSelectionGroupChanged(true);
1773 && (av.getColumnSelection() == null || !av
1774 .isColSelChanged(true)))
1776 // the current selection is unset or from a previous message
1777 // so import the new colsel.
1778 if (colsel == null || colsel.size() == 0)
1780 if (av.getColumnSelection() != null)
1782 av.getColumnSelection().clear();
1787 // TODO: shift colSel according to the intersecting sequences
1788 if (av.getColumnSelection() == null)
1790 av.setColumnSelection(new ColumnSelection(colsel));
1794 av.getColumnSelection().setElementsFrom(colsel);
1797 repaint |= av.isColSelChanged(true);
1800 && av.hasHiddenColumns()
1801 && (av.getColumnSelection() == null || av.getColumnSelection()
1802 .getHiddenColumns() == null))
1804 System.err.println("Bad things");
1808 ap.scalePanelHolder.repaint();
1814 * scroll to the given row/column - or nearest visible location
1819 public void scrollTo(int row, int column)
1822 row = row < 0 ? ap.av.startSeq : row;
1823 column = column < 0 ? ap.av.startRes : column;
1824 ap.scrollTo(column, column, row, true, true);
1828 * scroll to the given row - or nearest visible location
1832 public void scrollToRow(int row)
1835 row = row < 0 ? ap.av.startSeq : row;
1836 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1840 * scroll to the given column - or nearest visible location
1844 public void scrollToColumn(int column)
1847 column = column < 0 ? ap.av.startRes : column;
1848 ap.scrollTo(column, column, ap.av.startSeq, true, true);