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.api.AlignViewportI;
24 import jalview.commands.EditCommand;
25 import jalview.commands.EditCommand.Action;
26 import jalview.datamodel.ColumnSelection;
27 import jalview.datamodel.SearchResults;
28 import jalview.datamodel.SearchResults.Match;
29 import jalview.datamodel.Sequence;
30 import jalview.datamodel.SequenceFeature;
31 import jalview.datamodel.SequenceGroup;
32 import jalview.datamodel.SequenceI;
33 import jalview.schemes.ResidueProperties;
34 import jalview.structure.SelectionListener;
35 import jalview.structure.SelectionSource;
36 import jalview.structure.SequenceListener;
37 import jalview.structure.StructureSelectionManager;
38 import jalview.structure.VamsasSource;
39 import jalview.util.MappingUtils;
40 import jalview.util.MessageManager;
42 import java.awt.BorderLayout;
44 import java.awt.FontMetrics;
45 import java.awt.Panel;
46 import java.awt.Point;
47 import java.awt.event.InputEvent;
48 import java.awt.event.MouseEvent;
49 import java.awt.event.MouseListener;
50 import java.awt.event.MouseMotionListener;
51 import java.util.List;
52 import java.util.Vector;
54 public class SeqPanel extends Panel implements MouseMotionListener,
55 MouseListener, SequenceListener, SelectionListener
58 public SeqCanvas seqCanvas;
60 public AlignmentPanel ap;
62 protected int lastres;
64 protected int startseq;
66 protected AlignViewport av;
68 // if character is inserted or deleted, we will need to recalculate the
70 boolean seqEditOccurred = false;
72 ScrollThread scrollThread = null;
74 boolean mouseDragging = false;
76 boolean editingSeqs = false;
78 boolean groupEditing = false;
82 boolean changeEndSeq = false;
84 boolean changeStartSeq = false;
86 boolean changeEndRes = false;
88 boolean changeStartRes = false;
90 SequenceGroup stretchGroup = null;
92 StringBuffer keyboardNo1;
94 StringBuffer keyboardNo2;
96 boolean mouseWheelPressed = false;
100 EditCommand editCommand;
102 StructureSelectionManager ssm;
104 public SeqPanel(AlignViewport avp, AlignmentPanel p)
108 seqCanvas = new SeqCanvas(avp);
109 setLayout(new BorderLayout());
114 seqCanvas.addMouseMotionListener(this);
115 seqCanvas.addMouseListener(this);
116 ssm = StructureSelectionManager.getStructureSelectionManager(av.applet);
117 ssm.addStructureViewerListener(this);
118 ssm.addSelectionListener(this);
125 if (editCommand != null && editCommand.getSize() > 0)
127 ap.alignFrame.addHistoryItem(editCommand);
128 av.firePropertyChange("alignment", null, av.getAlignment()
135 groupEditing = false;
143 seqCanvas.cursorY = getKeyboardNo1() - 1;
147 void setCursorColumn()
149 seqCanvas.cursorX = getKeyboardNo1() - 1;
153 void setCursorRowAndColumn()
155 if (keyboardNo2 == null)
157 keyboardNo2 = new StringBuffer();
161 seqCanvas.cursorX = getKeyboardNo1() - 1;
162 seqCanvas.cursorY = getKeyboardNo2() - 1;
167 void setCursorPosition()
169 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
171 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
175 void moveCursor(int dx, int dy)
177 seqCanvas.cursorX += dx;
178 seqCanvas.cursorY += dy;
179 if (av.hasHiddenColumns()
180 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
182 int original = seqCanvas.cursorX - dx;
183 int maxWidth = av.getAlignment().getWidth();
185 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
186 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
188 seqCanvas.cursorX += dx;
191 if (seqCanvas.cursorX >= maxWidth
192 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
194 seqCanvas.cursorX = original;
200 void scrollToVisible()
202 if (seqCanvas.cursorX < 0)
204 seqCanvas.cursorX = 0;
206 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
208 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
211 if (seqCanvas.cursorY < 0)
213 seqCanvas.cursorY = 0;
215 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
217 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
221 if (av.getWrapAlignment())
223 ap.scrollToWrappedVisible(seqCanvas.cursorX);
227 while (seqCanvas.cursorY < av.startSeq)
231 while (seqCanvas.cursorY + 1 > av.endSeq)
235 while (seqCanvas.cursorX < av.getColumnSelection()
236 .adjustForHiddenColumns(av.startRes))
239 if (!ap.scrollRight(false))
244 while (seqCanvas.cursorX > av.getColumnSelection()
245 .adjustForHiddenColumns(av.endRes))
247 if (!ap.scrollRight(true))
253 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
254 seqCanvas.cursorX, seqCanvas.cursorY);
259 void setSelectionAreaAtCursor(boolean topLeft)
261 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
263 if (av.getSelectionGroup() != null)
265 SequenceGroup sg = av.getSelectionGroup();
266 // Find the top and bottom of this group
267 int min = av.getAlignment().getHeight(), max = 0;
268 for (int i = 0; i < sg.getSize(); i++)
270 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
285 sg.setStartRes(seqCanvas.cursorX);
286 if (sg.getEndRes() < seqCanvas.cursorX)
288 sg.setEndRes(seqCanvas.cursorX);
291 min = seqCanvas.cursorY;
295 sg.setEndRes(seqCanvas.cursorX);
296 if (sg.getStartRes() > seqCanvas.cursorX)
298 sg.setStartRes(seqCanvas.cursorX);
301 max = seqCanvas.cursorY + 1;
306 // Only the user can do this
307 av.setSelectionGroup(null);
311 // Now add any sequences between min and max
313 for (int i = min; i < max; i++)
315 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
320 if (av.getSelectionGroup() == null)
322 SequenceGroup sg = new SequenceGroup();
323 sg.setStartRes(seqCanvas.cursorX);
324 sg.setEndRes(seqCanvas.cursorX);
325 sg.addSequence(sequence, false);
326 av.setSelectionGroup(sg);
328 ap.paintAlignment(false);
332 void insertGapAtCursor(boolean group)
334 groupEditing = group;
335 startseq = seqCanvas.cursorY;
336 lastres = seqCanvas.cursorX;
337 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
341 void deleteGapAtCursor(boolean group)
343 groupEditing = group;
344 startseq = seqCanvas.cursorY;
345 lastres = seqCanvas.cursorX + getKeyboardNo1();
346 editSequence(false, seqCanvas.cursorX);
350 void numberPressed(char value)
352 if (keyboardNo1 == null)
354 keyboardNo1 = new StringBuffer();
357 if (keyboardNo2 != null)
359 keyboardNo2.append(value);
363 keyboardNo1.append(value);
371 if (keyboardNo1 != null)
373 int value = Integer.parseInt(keyboardNo1.toString());
377 } catch (Exception x)
388 if (keyboardNo2 != null)
390 int value = Integer.parseInt(keyboardNo2.toString());
394 } catch (Exception x)
402 * Set status message in alignment panel
405 * aligned sequence object
409 * index of sequence in alignment
410 * @return position of res in sequence
412 void setStatusMessage(SequenceI sequence, int res, int seq)
414 // TODO remove duplication of identical gui method
415 StringBuilder text = new StringBuilder(32);
416 String seqno = seq == -1 ? "" : " " + (seq + 1);
417 text.append("Sequence" + seqno + " ID: " + sequence.getName());
419 String residue = null;
421 * Try to translate the display character to residue name (null for gap).
423 final String displayChar = String.valueOf(sequence.getCharAt(res));
424 if (av.getAlignment().isNucleotide())
426 residue = ResidueProperties.nucleotideName.get(displayChar);
429 text.append(" Nucleotide: ").append(residue);
434 residue = "X".equalsIgnoreCase(displayChar) ? "X"
435 : ResidueProperties.aa2Triplet.get(displayChar);
438 text.append(" Residue: ").append(residue);
445 pos = sequence.findPosition(res);
446 text.append(" (").append(Integer.toString(pos)).append(")");
448 // Object obj = null;
449 // if (av.getAlignment().isNucleotide())
451 // obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
455 // text.append(" Nucleotide: ");
460 // obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
463 // text.append(" Residue: ");
472 // text.append(obj + " (" + sequence.findPosition(res) + ")");
476 ap.alignFrame.statusBar.setText(text.toString());
481 * Set the status bar message to highlight the first matched position in
486 private void setStatusMessage(SearchResults results)
488 List<Match> matches = results.getResults();
489 if (!matches.isEmpty())
491 Match m = matches.get(0);
492 SequenceI seq = m.getSequence();
493 int sequenceIndex = this.av.getAlignment().findIndex(seq);
496 * Convert position in sequence (base 1) to sequence character array index
499 int start = m.getStart() - 1;
500 setStatusMessage(seq, start, sequenceIndex);
504 public void mousePressed(MouseEvent evt)
506 lastMousePress = evt.getPoint();
508 // For now, ignore the mouseWheel font resizing on Macs
509 // As the Button2_mask always seems to be true
510 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
513 mouseWheelPressed = true;
517 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
519 if (evt.isControlDown() || evt.isAltDown())
527 doMousePressedDefineMode(evt);
531 int seq = findSeq(evt);
532 int res = findRes(evt);
534 if (seq < 0 || res < 0)
539 if ((seq < av.getAlignment().getHeight())
540 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
554 public void mouseClicked(MouseEvent evt)
556 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
557 if (evt.getClickCount() > 1)
559 if (av.getSelectionGroup() != null
560 && av.getSelectionGroup().getSize() == 1
561 && av.getSelectionGroup().getEndRes()
562 - av.getSelectionGroup().getStartRes() < 2)
564 av.setSelectionGroup(null);
567 SequenceFeature[] features = findFeaturesAtRes(sequence,
568 sequence.findPosition(findRes(evt)));
570 if (features != null && features.length > 0)
572 SearchResults highlight = new SearchResults();
573 highlight.addResult(sequence, features[0].getBegin(),
574 features[0].getEnd());
575 seqCanvas.highlightSearchResults(highlight);
577 if (features != null && features.length > 0)
579 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
580 { sequence }, features, false, ap);
582 seqCanvas.highlightSearchResults(null);
587 public void mouseReleased(MouseEvent evt)
589 mouseDragging = false;
590 mouseWheelPressed = false;
591 ap.paintAlignment(true);
595 doMouseReleasedDefineMode(evt);
603 int startWrapBlock = -1;
605 int wrappedBlock = -1;
607 int findRes(MouseEvent evt)
612 if (av.getWrapAlignment())
615 int hgap = av.getCharHeight();
616 if (av.getScaleAboveWrapped())
618 hgap += av.getCharHeight();
621 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
623 + seqCanvas.getAnnotationHeight();
627 x -= seqCanvas.LABEL_WEST;
629 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
635 wrappedBlock = y / cHeight;
636 wrappedBlock += av.getStartRes() / cwidth;
638 res = wrappedBlock * cwidth + x / av.getCharWidth();
643 res = (x / av.getCharWidth()) + av.getStartRes();
646 if (av.hasHiddenColumns())
648 res = av.getColumnSelection().adjustForHiddenColumns(res);
655 int findSeq(MouseEvent evt)
657 final int sqnum = findAlRow(evt);
658 return (sqnum < 0) ? 0 : sqnum;
664 * @return row in alignment that was selected (or -1 for column selection)
666 private int findAlRow(MouseEvent evt)
671 if (av.getWrapAlignment())
673 int hgap = av.getCharHeight();
674 if (av.getScaleAboveWrapped())
676 hgap += av.getCharHeight();
679 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
681 + seqCanvas.getAnnotationHeight();
685 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
694 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
695 .getAlignment().getHeight() - 1);
705 public void doMousePressed(MouseEvent evt)
708 int seq = findSeq(evt);
709 int res = findRes(evt);
711 if (seq < av.getAlignment().getHeight()
712 && res < av.getAlignment().getSequenceAt(seq).getLength())
714 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
715 // Find the residue's position in the sequence (res is the position
732 public void mouseOverSequence(SequenceI sequence, int index, int pos)
734 String tmp = sequence.hashCode() + index + "";
735 if (lastMessage == null || !lastMessage.equals(tmp))
737 ssm.mouseOverSequence(sequence, index, pos, av);
743 public void highlightSequence(SearchResults results)
745 if (av.followHighlight)
747 if (ap.scrollToPosition(results, true))
749 ap.alignFrame.repaint();
752 setStatusMessage(results);
753 seqCanvas.highlightSearchResults(results);
758 public VamsasSource getVamsasSource()
760 return this.ap == null ? null : this.ap.av;
763 public void updateColours(SequenceI seq, int index)
765 System.out.println("update the seqPanel colours");
769 public void mouseMoved(MouseEvent evt)
771 int res = findRes(evt);
772 int seq = findSeq(evt);
774 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
783 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
784 if (res > sequence.getLength())
793 int respos = sequence.findPosition(res);
796 mouseOverSequence(sequence, res, respos);
799 StringBuilder text = new StringBuilder();
800 text.append("Sequence ").append(Integer.toString(seq + 1))
801 .append(" ID: ").append(sequence.getName());
804 final String ch = String.valueOf(sequence.getCharAt(res));
805 if (av.getAlignment().isNucleotide())
807 obj = ResidueProperties.nucleotideName.get(ch);
810 text.append(" Nucleotide: ").append(obj);
815 obj = "X".equalsIgnoreCase(ch) ? "X"
816 : ResidueProperties.aa2Triplet.get(ch);
819 text.append(" Residue: ").append(obj);
825 text.append(" (").append(Integer.toString(respos)).append(")");
828 ap.alignFrame.statusBar.setText(text.toString());
830 StringBuilder tooltipText = new StringBuilder();
831 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
834 for (int g = 0; g < groups.length; g++)
836 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
838 if (!groups[g].getName().startsWith("JTreeGroup")
839 && !groups[g].getName().startsWith("JGroup"))
841 tooltipText.append(groups[g].getName()).append(" ");
843 if (groups[g].getDescription() != null)
845 tooltipText.append(groups[g].getDescription());
847 tooltipText.append("\n");
852 // use aa to see if the mouse pointer is on a
853 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
854 sequence.findPosition(res));
857 while (index < allFeatures.length)
859 SequenceFeature sf = allFeatures[index];
861 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
863 if (sf.getDescription() != null)
865 tooltipText.append(" " + sf.getDescription());
868 if (sf.getValue("status") != null)
870 String status = sf.getValue("status").toString();
871 if (status.length() > 0)
873 tooltipText.append(" (" + sf.getValue("status") + ")");
876 tooltipText.append("\n");
883 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
887 tooltip.setTip(tooltipText.toString());
891 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
893 Vector tmp = new Vector();
894 SequenceFeature[] features = sequence.getSequenceFeatures();
895 if (features != null)
897 for (int i = 0; i < features.length; i++)
899 if (av.getFeaturesDisplayed() == null
900 || !av.getFeaturesDisplayed().isVisible(features[i].getType()))
905 if (features[i].featureGroup != null
906 && !seqCanvas.fr.checkGroupVisibility(features[i].featureGroup,false))
911 if ((features[i].getBegin() <= res)
912 && (features[i].getEnd() >= res))
914 tmp.addElement(features[i]);
919 features = new SequenceFeature[tmp.size()];
920 tmp.copyInto(features);
927 public void mouseDragged(MouseEvent evt)
929 if (mouseWheelPressed)
931 int oldWidth = av.getCharWidth();
933 // Which is bigger, left-right or up-down?
934 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
937 int fontSize = av.font.getSize();
939 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
943 else if (evt.getY() > lastMousePress.y)
953 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
954 av.setCharWidth(oldWidth);
958 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
960 av.setCharWidth(av.getCharWidth() - 1);
962 else if (evt.getX() > lastMousePress.x)
964 av.setCharWidth(av.getCharWidth() + 1);
967 if (av.getCharWidth() < 1)
975 FontMetrics fm = getFontMetrics(av.getFont());
976 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
978 lastMousePress = evt.getPoint();
980 ap.paintAlignment(false);
981 ap.annotationPanel.image = null;
987 doMouseDraggedDefineMode(evt);
991 int res = findRes(evt);
998 if ((lastres == -1) || (lastres == res))
1003 if ((res < av.getAlignment().getWidth()) && (res < lastres))
1005 // dragLeft, delete gap
1006 editSequence(false, res);
1010 editSequence(true, res);
1013 mouseDragging = true;
1014 if (scrollThread != null)
1016 scrollThread.setEvent(evt);
1021 synchronized void editSequence(boolean insertGap, int startres)
1024 int fixedRight = -1;
1025 boolean fixedColumns = false;
1026 SequenceGroup sg = av.getSelectionGroup();
1028 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1030 if (!groupEditing && av.hasHiddenRows())
1032 if (av.isHiddenRepSequence(seq))
1034 sg = av.getRepresentedSequences(seq);
1035 groupEditing = true;
1039 StringBuffer message = new StringBuffer();
1042 message.append(MessageManager.getString("action.edit_group")).append(
1044 if (editCommand == null)
1046 editCommand = new EditCommand(
1047 MessageManager.getString("action.edit_group"));
1052 message.append(MessageManager.getString("label.edit_sequence"))
1053 .append(" " + seq.getName());
1054 String label = seq.getName();
1055 if (label.length() > 10)
1057 label = label.substring(0, 10);
1059 if (editCommand == null)
1061 editCommand = new EditCommand(MessageManager.formatMessage(
1062 "label.edit_params", new String[]
1069 message.append(" insert ");
1073 message.append(" delete ");
1076 message.append(Math.abs(startres - lastres) + " gaps.");
1077 ap.alignFrame.statusBar.setText(message.toString());
1079 // Are we editing within a selection group?
1081 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1084 fixedColumns = true;
1086 // sg might be null as the user may only see 1 sequence,
1087 // but the sequence represents a group
1090 if (!av.isHiddenRepSequence(seq))
1096 sg = av.getRepresentedSequences(seq);
1099 fixedLeft = sg.getStartRes();
1100 fixedRight = sg.getEndRes();
1102 if ((startres < fixedLeft && lastres >= fixedLeft)
1103 || (startres >= fixedLeft && lastres < fixedLeft)
1104 || (startres > fixedRight && lastres <= fixedRight)
1105 || (startres <= fixedRight && lastres > fixedRight))
1111 if (fixedLeft > startres)
1113 fixedRight = fixedLeft - 1;
1116 else if (fixedRight < startres)
1118 fixedLeft = fixedRight;
1123 if (av.hasHiddenColumns())
1125 fixedColumns = true;
1126 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1127 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1129 if ((insertGap && startres > y1 && lastres < y1)
1130 || (!insertGap && startres < y2 && lastres > y2))
1136 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1137 // Selection spans a hidden region
1138 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1146 fixedRight = y2 - 1;
1153 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1154 .toArray(new SequenceI[0]);
1159 // If the user has selected the whole sequence, and is dragging to
1160 // the right, we can still extend the alignment and selectionGroup
1161 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1162 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1164 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1165 fixedRight = sg.getEndRes();
1168 // Is it valid with fixed columns??
1169 // Find the next gap before the end
1170 // of the visible region boundary
1171 boolean blank = false;
1172 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1176 for (SequenceI gs : groupSeqs)
1178 for (int j = 0; j < startres - lastres; j++)
1180 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1196 if (sg.getSize() == av.getAlignment().getHeight())
1198 if ((av.hasHiddenColumns() && startres < av
1199 .getColumnSelection().getHiddenBoundaryRight(startres)))
1205 int alWidth = av.getAlignment().getWidth();
1206 if (av.hasHiddenRows())
1208 int hwidth = av.getAlignment().getHiddenSequences()
1210 if (hwidth > alWidth)
1215 // We can still insert gaps if the selectionGroup
1216 // contains all the sequences
1217 sg.setEndRes(sg.getEndRes() + startres - lastres);
1218 fixedRight = alWidth + startres - lastres;
1229 else if (!insertGap)
1231 // / Are we able to delete?
1232 // ie are all columns blank?
1234 for (SequenceI gs : groupSeqs)
1236 for (int j = startres; j < lastres; j++)
1238 if (gs.getLength() <= j)
1243 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1245 // Not a gap, block edit not valid
1255 // dragging to the right
1256 if (fixedColumns && fixedRight != -1)
1258 for (int j = lastres; j < startres; j++)
1260 insertChar(j, groupSeqs, fixedRight);
1265 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1266 startres - lastres, av.getAlignment(), true);
1271 // dragging to the left
1272 if (fixedColumns && fixedRight != -1)
1274 for (int j = lastres; j > startres; j--)
1276 deleteChar(startres, groupSeqs, fixedRight);
1281 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1282 lastres - startres, av.getAlignment(), true);
1288 // ///Editing a single sequence///////////
1292 // dragging to the right
1293 if (fixedColumns && fixedRight != -1)
1295 for (int j = lastres; j < startres; j++)
1297 insertChar(j, new SequenceI[]
1298 { seq }, fixedRight);
1303 editCommand.appendEdit(Action.INSERT_GAP, new SequenceI[]
1304 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1309 // dragging to the left
1310 if (fixedColumns && fixedRight != -1)
1312 for (int j = lastres; j > startres; j--)
1314 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1319 deleteChar(startres, new SequenceI[]
1320 { seq }, fixedRight);
1325 // could be a keyboard edit trying to delete none gaps
1327 for (int m = startres; m < lastres; m++)
1329 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1338 editCommand.appendEdit(Action.DELETE_GAP, new SequenceI[]
1339 { seq }, startres, max, av.getAlignment(), true);
1346 seqCanvas.repaint();
1349 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1351 int blankColumn = fixedColumn;
1352 for (int s = 0; s < seq.length; s++)
1354 // Find the next gap before the end of the visible region boundary
1355 // If lastCol > j, theres a boundary after the gap insertion
1357 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1359 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1361 // Theres a space, so break and insert the gap
1366 if (blankColumn <= j)
1368 blankColumn = fixedColumn;
1374 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1375 av.getAlignment(), true);
1377 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1382 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1385 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1388 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1389 av.getAlignment(), true);
1392 // ////////////////////////////////////////
1393 // ///Everything below this is for defining the boundary of the rubberband
1394 // ////////////////////////////////////////
1395 public void doMousePressedDefineMode(MouseEvent evt)
1397 if (scrollThread != null)
1399 scrollThread.running = false;
1400 scrollThread = null;
1403 int res = findRes(evt);
1404 int seq = findSeq(evt);
1406 startWrapBlock = wrappedBlock;
1413 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1415 if (sequence == null || res > sequence.getLength())
1420 stretchGroup = av.getSelectionGroup();
1422 if (stretchGroup == null)
1424 stretchGroup = av.getAlignment().findGroup(sequence);
1425 if (stretchGroup != null && res > stretchGroup.getStartRes()
1426 && res < stretchGroup.getEndRes())
1428 av.setSelectionGroup(stretchGroup);
1432 stretchGroup = null;
1436 else if (!stretchGroup.getSequences(null).contains(sequence)
1437 || stretchGroup.getStartRes() > res
1438 || stretchGroup.getEndRes() < res)
1440 stretchGroup = null;
1442 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1444 if (allGroups != null)
1446 for (int i = 0; i < allGroups.length; i++)
1448 if (allGroups[i].getStartRes() <= res
1449 && allGroups[i].getEndRes() >= res)
1451 stretchGroup = allGroups[i];
1456 av.setSelectionGroup(stretchGroup);
1459 // DETECT RIGHT MOUSE BUTTON IN AWT
1460 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1462 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1463 sequence.findPosition(res));
1465 Vector links = null;
1466 if (allFeatures != null)
1468 for (int i = 0; i < allFeatures.length; i++)
1470 if (allFeatures[i].links != null)
1474 links = new Vector();
1476 for (int j = 0; j < allFeatures[i].links.size(); j++)
1478 links.addElement(allFeatures[i].links.elementAt(j));
1483 APopupMenu popup = new APopupMenu(ap, null, links);
1485 popup.show(this, evt.getX(), evt.getY());
1491 seqCanvas.cursorX = findRes(evt);
1492 seqCanvas.cursorY = findSeq(evt);
1493 seqCanvas.repaint();
1497 // Only if left mouse button do we want to change group sizes
1499 if (stretchGroup == null)
1501 // define a new group here
1502 SequenceGroup sg = new SequenceGroup();
1503 sg.setStartRes(res);
1505 sg.addSequence(sequence, false);
1506 av.setSelectionGroup(sg);
1509 if (av.getConservationSelected())
1511 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1514 if (av.getAbovePIDThreshold())
1516 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1523 public void doMouseReleasedDefineMode(MouseEvent evt)
1525 if (stretchGroup == null)
1530 stretchGroup.recalcConservation(); // always do this - annotation has own
1532 if (stretchGroup.cs != null)
1534 stretchGroup.cs.alignmentChanged(stretchGroup,
1535 av.getHiddenRepSequences());
1537 if (stretchGroup.cs.conservationApplied())
1539 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1540 stretchGroup.getName());
1544 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1545 stretchGroup.getName());
1548 changeEndRes = false;
1549 changeStartRes = false;
1550 stretchGroup = null;
1551 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1552 ap.paintAlignment(true);
1556 public void doMouseDraggedDefineMode(MouseEvent evt)
1558 int res = findRes(evt);
1559 int y = findSeq(evt);
1561 if (wrappedBlock != startWrapBlock)
1566 if (stretchGroup == null)
1571 mouseDragging = true;
1573 if (y > av.getAlignment().getHeight())
1575 y = av.getAlignment().getHeight() - 1;
1578 if (res >= av.getAlignment().getWidth())
1580 res = av.getAlignment().getWidth() - 1;
1583 if (stretchGroup.getEndRes() == res)
1585 // Edit end res position of selected group
1586 changeEndRes = true;
1588 else if (stretchGroup.getStartRes() == res)
1590 // Edit start res position of selected group
1591 changeStartRes = true;
1601 if (res > (stretchGroup.getStartRes() - 1))
1603 stretchGroup.setEndRes(res);
1606 else if (changeStartRes)
1608 if (res < (stretchGroup.getEndRes() + 1))
1610 stretchGroup.setStartRes(res);
1614 int dragDirection = 0;
1620 else if (y < oldSeq)
1625 while ((y != oldSeq) && (oldSeq > -1)
1626 && (y < av.getAlignment().getHeight()))
1628 // This routine ensures we don't skip any sequences, as the
1629 // selection is quite slow.
1630 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1632 oldSeq += dragDirection;
1639 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1641 if (stretchGroup.getSequences(null).contains(nextSeq))
1643 stretchGroup.deleteSequence(seq, false);
1649 stretchGroup.addSequence(seq, false);
1652 stretchGroup.addSequence(nextSeq, false);
1661 if (res > av.endRes || res < av.startRes || y < av.startSeq
1667 if (scrollThread != null)
1669 scrollThread.setEvent(evt);
1672 seqCanvas.repaint();
1675 public void mouseEntered(MouseEvent e)
1682 if (scrollThread != null)
1684 scrollThread.running = false;
1685 scrollThread = null;
1689 public void mouseExited(MouseEvent e)
1691 if (av.getWrapAlignment())
1696 if (mouseDragging && scrollThread == null)
1698 scrollThread = new ScrollThread();
1702 void scrollCanvas(MouseEvent evt)
1706 if (scrollThread != null)
1708 scrollThread.running = false;
1709 scrollThread = null;
1711 mouseDragging = false;
1715 if (scrollThread == null)
1717 scrollThread = new ScrollThread();
1720 mouseDragging = true;
1721 scrollThread.setEvent(evt);
1726 // this class allows scrolling off the bottom of the visible alignment
1727 class ScrollThread extends Thread
1731 boolean running = false;
1733 public ScrollThread()
1738 public void setEvent(MouseEvent e)
1743 public void stopScrolling()
1757 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1759 running = ap.scrollUp(true);
1762 if (mouseDragging && evt.getY() >= getSize().height
1763 && av.getAlignment().getHeight() > av.getEndSeq())
1765 running = ap.scrollUp(false);
1768 if (mouseDragging && evt.getX() < 0)
1770 running = ap.scrollRight(false);
1773 else if (mouseDragging && evt.getX() >= getSize().width)
1775 running = ap.scrollRight(true);
1782 } catch (Exception ex)
1790 * modify current selection according to a received message.
1792 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1793 SelectionSource source)
1795 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1796 // handles selection messages...
1797 // TODO: extend config options to allow user to control if selections may be
1798 // shared between viewports.
1800 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignViewport) source)
1801 .getSequenceSetId().equals(av.getSequenceSetId()))))
1807 * Check for selection in a view of which this one is a dna/protein
1810 if (selectionFromTranslation(seqsel, colsel, source))
1815 // do we want to thread this ? (contention with seqsel and colsel locks, I
1817 // rules are: colsel is copied if there is a real intersection between
1818 // sequence selection
1819 boolean repaint = false, copycolsel = true;
1820 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1822 SequenceGroup sgroup = null;
1823 if (seqsel != null && seqsel.getSize() > 0)
1825 if (av.getAlignment() == null)
1828 .println("Selection message: alignviewport av SeqSetId="
1829 + av.getSequenceSetId() + " ViewId="
1831 + " 's alignment is NULL! returning immediatly.");
1834 sgroup = seqsel.intersect(av.getAlignment(),
1835 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1836 if ((sgroup == null || sgroup.getSize() == 0)
1837 && (colsel == null || colsel.size() == 0))
1839 // don't copy columns if the region didn't intersect.
1843 if (sgroup != null && sgroup.getSize() > 0)
1845 av.setSelectionGroup(sgroup);
1849 av.setSelectionGroup(null);
1851 repaint = av.isSelectionGroupChanged(true);
1854 && (av.getColumnSelection() == null || !av
1855 .isColSelChanged(true)))
1857 // the current selection is unset or from a previous message
1858 // so import the new colsel.
1859 if (colsel == null || colsel.size() == 0)
1861 if (av.getColumnSelection() != null)
1863 av.getColumnSelection().clear();
1868 // TODO: shift colSel according to the intersecting sequences
1869 if (av.getColumnSelection() == null)
1871 av.setColumnSelection(new ColumnSelection(colsel));
1875 av.getColumnSelection().setElementsFrom(colsel);
1878 repaint |= av.isColSelChanged(true);
1881 && av.hasHiddenColumns()
1882 && (av.getColumnSelection() == null || av.getColumnSelection()
1883 .getHiddenColumns() == null))
1885 System.err.println("Bad things");
1889 ap.scalePanelHolder.repaint();
1895 * scroll to the given row/column - or nearest visible location
1900 public void scrollTo(int row, int column)
1903 row = row < 0 ? ap.av.startSeq : row;
1904 column = column < 0 ? ap.av.startRes : column;
1905 ap.scrollTo(column, column, row, true, true);
1909 * scroll to the given row - or nearest visible location
1913 public void scrollToRow(int row)
1916 row = row < 0 ? ap.av.startSeq : row;
1917 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1921 * scroll to the given column - or nearest visible location
1925 public void scrollToColumn(int column)
1928 column = column < 0 ? ap.av.startRes : column;
1929 ap.scrollTo(column, column, ap.av.startSeq, true, true);
1933 * If this panel is a cdna/protein translation view of the selection source,
1934 * tries to map the source selection to a local one, and returns true. Else
1941 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1942 ColumnSelection colsel, SelectionSource source)
1944 if (!(source instanceof AlignViewportI)) {
1947 final AlignViewportI sourceAv = (AlignViewportI) source;
1948 if (sourceAv.getCodingComplement() != av && av.getCodingComplement() != sourceAv)
1954 * Map sequence selection
1956 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1957 av.setSelectionGroup(sg);
1958 av.isSelectionGroupChanged(true);
1961 * Map column selection
1963 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1965 av.setColumnSelection(cs);
1966 av.isColSelChanged(true);
1968 ap.scalePanelHolder.repaint();