2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.AlignmentI;
27 import jalview.datamodel.ColumnSelection;
28 import jalview.datamodel.SearchResults;
29 import jalview.datamodel.SearchResults.Match;
30 import jalview.datamodel.Sequence;
31 import jalview.datamodel.SequenceFeature;
32 import jalview.datamodel.SequenceGroup;
33 import jalview.datamodel.SequenceI;
34 import jalview.schemes.ResidueProperties;
35 import jalview.structure.SelectionListener;
36 import jalview.structure.SelectionSource;
37 import jalview.structure.SequenceListener;
38 import jalview.structure.StructureSelectionManager;
39 import jalview.structure.VamsasSource;
40 import jalview.util.MappingUtils;
41 import jalview.util.MessageManager;
42 import jalview.viewmodel.AlignmentViewport;
44 import java.awt.BorderLayout;
46 import java.awt.FontMetrics;
47 import java.awt.Panel;
48 import java.awt.Point;
49 import java.awt.event.InputEvent;
50 import java.awt.event.MouseEvent;
51 import java.awt.event.MouseListener;
52 import java.awt.event.MouseMotionListener;
53 import java.util.Vector;
55 public class SeqPanel extends Panel implements MouseMotionListener,
56 MouseListener, SequenceListener, SelectionListener
59 public SeqCanvas seqCanvas;
61 public AlignmentPanel ap;
63 protected int lastres;
65 protected int startseq;
67 protected AlignViewport av;
69 // if character is inserted or deleted, we will need to recalculate the
71 boolean seqEditOccurred = false;
73 ScrollThread scrollThread = null;
75 boolean mouseDragging = false;
77 boolean editingSeqs = false;
79 boolean groupEditing = false;
83 boolean changeEndSeq = false;
85 boolean changeStartSeq = false;
87 boolean changeEndRes = false;
89 boolean changeStartRes = false;
91 SequenceGroup stretchGroup = null;
93 StringBuffer keyboardNo1;
95 StringBuffer keyboardNo2;
97 boolean mouseWheelPressed = false;
101 EditCommand editCommand;
103 StructureSelectionManager ssm;
105 public SeqPanel(AlignViewport avp, AlignmentPanel p)
109 seqCanvas = new SeqCanvas(avp);
110 setLayout(new BorderLayout());
115 seqCanvas.addMouseMotionListener(this);
116 seqCanvas.addMouseListener(this);
117 ssm = StructureSelectionManager.getStructureSelectionManager(av.applet);
118 ssm.addStructureViewerListener(this);
119 ssm.addSelectionListener(this);
126 if (editCommand != null && editCommand.getSize() > 0)
128 ap.alignFrame.addHistoryItem(editCommand);
129 av.firePropertyChange("alignment", null, av.getAlignment()
136 groupEditing = false;
144 seqCanvas.cursorY = getKeyboardNo1() - 1;
148 void setCursorColumn()
150 seqCanvas.cursorX = getKeyboardNo1() - 1;
154 void setCursorRowAndColumn()
156 if (keyboardNo2 == null)
158 keyboardNo2 = new StringBuffer();
162 seqCanvas.cursorX = getKeyboardNo1() - 1;
163 seqCanvas.cursorY = getKeyboardNo2() - 1;
168 void setCursorPosition()
170 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
172 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
176 void moveCursor(int dx, int dy)
178 seqCanvas.cursorX += dx;
179 seqCanvas.cursorY += dy;
180 if (av.hasHiddenColumns()
181 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
183 int original = seqCanvas.cursorX - dx;
184 int maxWidth = av.getAlignment().getWidth();
186 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
187 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
189 seqCanvas.cursorX += dx;
192 if (seqCanvas.cursorX >= maxWidth
193 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
195 seqCanvas.cursorX = original;
201 void scrollToVisible()
203 if (seqCanvas.cursorX < 0)
205 seqCanvas.cursorX = 0;
207 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
209 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
212 if (seqCanvas.cursorY < 0)
214 seqCanvas.cursorY = 0;
216 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
218 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
222 if (av.getWrapAlignment())
224 ap.scrollToWrappedVisible(seqCanvas.cursorX);
228 while (seqCanvas.cursorY < av.startSeq)
232 while (seqCanvas.cursorY + 1 > av.endSeq)
236 while (seqCanvas.cursorX < av.getColumnSelection()
237 .adjustForHiddenColumns(av.startRes))
240 if (!ap.scrollRight(false))
245 while (seqCanvas.cursorX > av.getColumnSelection()
246 .adjustForHiddenColumns(av.endRes))
248 if (!ap.scrollRight(true))
254 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
255 seqCanvas.cursorX, seqCanvas.cursorY);
260 void setSelectionAreaAtCursor(boolean topLeft)
262 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
264 if (av.getSelectionGroup() != null)
266 SequenceGroup sg = av.getSelectionGroup();
267 // Find the top and bottom of this group
268 int min = av.getAlignment().getHeight(), max = 0;
269 for (int i = 0; i < sg.getSize(); i++)
271 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
286 sg.setStartRes(seqCanvas.cursorX);
287 if (sg.getEndRes() < seqCanvas.cursorX)
289 sg.setEndRes(seqCanvas.cursorX);
292 min = seqCanvas.cursorY;
296 sg.setEndRes(seqCanvas.cursorX);
297 if (sg.getStartRes() > seqCanvas.cursorX)
299 sg.setStartRes(seqCanvas.cursorX);
302 max = seqCanvas.cursorY + 1;
307 // Only the user can do this
308 av.setSelectionGroup(null);
312 // Now add any sequences between min and max
314 for (int i = min; i < max; i++)
316 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
321 if (av.getSelectionGroup() == null)
323 SequenceGroup sg = new SequenceGroup();
324 sg.setStartRes(seqCanvas.cursorX);
325 sg.setEndRes(seqCanvas.cursorX);
326 sg.addSequence(sequence, false);
327 av.setSelectionGroup(sg);
329 ap.paintAlignment(false);
333 void insertGapAtCursor(boolean group)
335 groupEditing = group;
336 startseq = seqCanvas.cursorY;
337 lastres = seqCanvas.cursorX;
338 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
342 void deleteGapAtCursor(boolean group)
344 groupEditing = group;
345 startseq = seqCanvas.cursorY;
346 lastres = seqCanvas.cursorX + getKeyboardNo1();
347 editSequence(false, seqCanvas.cursorX);
351 void numberPressed(char value)
353 if (keyboardNo1 == null)
355 keyboardNo1 = new StringBuffer();
358 if (keyboardNo2 != null)
360 keyboardNo2.append(value);
364 keyboardNo1.append(value);
372 if (keyboardNo1 != null)
374 int value = Integer.parseInt(keyboardNo1.toString());
378 } catch (Exception x)
389 if (keyboardNo2 != null)
391 int value = Integer.parseInt(keyboardNo2.toString());
395 } catch (Exception x)
403 * Set status message in alignment panel
406 * aligned sequence object
410 * index of sequence in alignment
411 * @return position of res in sequence
413 void setStatusMessage(SequenceI sequence, int res, int seq)
415 // TODO remove duplication of identical gui method
416 StringBuilder text = new StringBuilder(32);
417 String seqno = seq == -1 ? "" : " " + (seq + 1);
418 text.append("Sequence" + seqno + " ID: " + sequence.getName());
420 String residue = null;
422 * Try to translate the display character to residue name (null for gap).
424 final String displayChar = String.valueOf(sequence.getCharAt(res));
425 if (av.getAlignment().isNucleotide())
427 residue = ResidueProperties.nucleotideName.get(displayChar);
430 text.append(" Nucleotide: ").append(residue);
435 residue = "X".equalsIgnoreCase(displayChar) ? "X" : ("*"
436 .equals(displayChar) ? "STOP" : ResidueProperties.aa2Triplet
440 text.append(" Residue: ").append(residue);
447 pos = sequence.findPosition(res);
448 text.append(" (").append(Integer.toString(pos)).append(")");
451 ap.alignFrame.statusBar.setText(text.toString());
455 * Set the status bar message to highlight the first matched position in
459 * @return true if results were matched, false if not
461 private boolean setStatusMessage(SearchResults results)
463 AlignmentI al = this.av.getAlignment();
464 int sequenceIndex = al.findIndex(results);
465 if (sequenceIndex == -1)
469 SequenceI ds = al.getSequenceAt(sequenceIndex).getDatasetSequence();
470 for (Match m : results.getResults())
472 SequenceI seq = m.getSequence();
473 if (seq.getDatasetSequence() != null)
475 seq = seq.getDatasetSequence();
481 * Convert position in sequence (base 1) to sequence character array
484 int start = m.getStart() - 1;
485 setStatusMessage(seq, start, sequenceIndex);
492 public void mousePressed(MouseEvent evt)
494 lastMousePress = evt.getPoint();
496 // For now, ignore the mouseWheel font resizing on Macs
497 // As the Button2_mask always seems to be true
498 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
501 mouseWheelPressed = true;
505 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
507 if (evt.isControlDown() || evt.isAltDown())
515 doMousePressedDefineMode(evt);
519 int seq = findSeq(evt);
520 int res = findRes(evt);
522 if (seq < 0 || res < 0)
527 if ((seq < av.getAlignment().getHeight())
528 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
542 public void mouseClicked(MouseEvent evt)
544 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
545 if (evt.getClickCount() > 1)
547 if (av.getSelectionGroup() != null
548 && av.getSelectionGroup().getSize() == 1
549 && av.getSelectionGroup().getEndRes()
550 - av.getSelectionGroup().getStartRes() < 2)
552 av.setSelectionGroup(null);
555 SequenceFeature[] features = findFeaturesAtRes(sequence,
556 sequence.findPosition(findRes(evt)));
558 if (features != null && features.length > 0)
560 SearchResults highlight = new SearchResults();
561 highlight.addResult(sequence, features[0].getBegin(),
562 features[0].getEnd());
563 seqCanvas.highlightSearchResults(highlight);
565 if (features != null && features.length > 0)
567 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
568 { sequence }, features, false, ap);
570 seqCanvas.highlightSearchResults(null);
575 public void mouseReleased(MouseEvent evt)
577 mouseDragging = false;
578 mouseWheelPressed = false;
579 ap.paintAlignment(true);
583 doMouseReleasedDefineMode(evt);
591 int startWrapBlock = -1;
593 int wrappedBlock = -1;
595 int findRes(MouseEvent evt)
600 if (av.getWrapAlignment())
603 int hgap = av.getCharHeight();
604 if (av.getScaleAboveWrapped())
606 hgap += av.getCharHeight();
609 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
611 + seqCanvas.getAnnotationHeight();
615 x -= seqCanvas.LABEL_WEST;
617 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
623 wrappedBlock = y / cHeight;
624 wrappedBlock += av.getStartRes() / cwidth;
626 res = wrappedBlock * cwidth + x / av.getCharWidth();
631 res = (x / av.getCharWidth()) + av.getStartRes();
634 if (av.hasHiddenColumns())
636 res = av.getColumnSelection().adjustForHiddenColumns(res);
643 int findSeq(MouseEvent evt)
645 final int sqnum = findAlRow(evt);
646 return (sqnum < 0) ? 0 : sqnum;
652 * @return row in alignment that was selected (or -1 for column selection)
654 private int findAlRow(MouseEvent evt)
659 if (av.getWrapAlignment())
661 int hgap = av.getCharHeight();
662 if (av.getScaleAboveWrapped())
664 hgap += av.getCharHeight();
667 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
669 + seqCanvas.getAnnotationHeight();
673 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
682 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
683 .getAlignment().getHeight() - 1);
693 public void doMousePressed(MouseEvent evt)
696 int seq = findSeq(evt);
697 int res = findRes(evt);
699 if (seq < av.getAlignment().getHeight()
700 && res < av.getAlignment().getSequenceAt(seq).getLength())
702 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
703 // Find the residue's position in the sequence (res is the position
720 public void mouseOverSequence(SequenceI sequence, int index, int pos)
722 String tmp = sequence.hashCode() + index + "";
723 if (lastMessage == null || !lastMessage.equals(tmp))
725 ssm.mouseOverSequence(sequence, index, pos, av);
731 public void highlightSequence(SearchResults results)
733 if (av.isFollowHighlight())
735 if (ap.scrollToPosition(results, true))
737 ap.alignFrame.repaint();
740 setStatusMessage(results);
741 seqCanvas.highlightSearchResults(results);
746 public VamsasSource getVamsasSource()
748 return this.ap == null ? null : this.ap.av;
751 public void updateColours(SequenceI seq, int index)
753 System.out.println("update the seqPanel colours");
757 public void mouseMoved(MouseEvent evt)
759 int res = findRes(evt);
760 int seq = findSeq(evt);
762 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
771 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
772 if (res > sequence.getLength())
781 int respos = sequence.findPosition(res);
784 mouseOverSequence(sequence, res, respos);
787 StringBuilder text = new StringBuilder();
788 text.append("Sequence ").append(Integer.toString(seq + 1))
789 .append(" ID: ").append(sequence.getName());
792 final String ch = String.valueOf(sequence.getCharAt(res));
793 if (av.getAlignment().isNucleotide())
795 obj = ResidueProperties.nucleotideName.get(ch);
798 text.append(" Nucleotide: ").append(obj);
803 obj = "X".equalsIgnoreCase(ch) ? "X"
804 : ResidueProperties.aa2Triplet.get(ch);
807 text.append(" Residue: ").append(obj);
813 text.append(" (").append(Integer.toString(respos)).append(")");
816 ap.alignFrame.statusBar.setText(text.toString());
818 StringBuilder tooltipText = new StringBuilder();
819 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
822 for (int g = 0; g < groups.length; g++)
824 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
826 if (!groups[g].getName().startsWith("JTreeGroup")
827 && !groups[g].getName().startsWith("JGroup"))
829 tooltipText.append(groups[g].getName()).append(" ");
831 if (groups[g].getDescription() != null)
833 tooltipText.append(groups[g].getDescription());
835 tooltipText.append("\n");
840 // use aa to see if the mouse pointer is on a
841 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
842 sequence.findPosition(res));
845 while (index < allFeatures.length)
847 SequenceFeature sf = allFeatures[index];
849 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
851 if (sf.getDescription() != null)
853 tooltipText.append(" " + sf.getDescription());
856 if (sf.getValue("status") != null)
858 String status = sf.getValue("status").toString();
859 if (status.length() > 0)
861 tooltipText.append(" (" + sf.getValue("status") + ")");
864 tooltipText.append("\n");
871 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
875 tooltip.setTip(tooltipText.toString());
879 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
881 Vector tmp = new Vector();
882 SequenceFeature[] features = sequence.getSequenceFeatures();
883 if (features != null)
885 for (int i = 0; i < features.length; i++)
887 if (av.getFeaturesDisplayed() == null
888 || !av.getFeaturesDisplayed().isVisible(features[i].getType()))
893 if (features[i].featureGroup != null
894 && !seqCanvas.fr.checkGroupVisibility(features[i].featureGroup,false))
899 if ((features[i].getBegin() <= res)
900 && (features[i].getEnd() >= res))
902 tmp.addElement(features[i]);
907 features = new SequenceFeature[tmp.size()];
908 tmp.copyInto(features);
915 public void mouseDragged(MouseEvent evt)
917 if (mouseWheelPressed)
919 int oldWidth = av.getCharWidth();
921 // Which is bigger, left-right or up-down?
922 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
925 int fontSize = av.font.getSize();
927 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
931 else if (evt.getY() > lastMousePress.y)
941 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
942 av.setCharWidth(oldWidth);
946 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
948 av.setCharWidth(av.getCharWidth() - 1);
950 else if (evt.getX() > lastMousePress.x)
952 av.setCharWidth(av.getCharWidth() + 1);
955 if (av.getCharWidth() < 1)
963 FontMetrics fm = getFontMetrics(av.getFont());
964 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
966 lastMousePress = evt.getPoint();
968 ap.paintAlignment(false);
969 ap.annotationPanel.image = null;
975 doMouseDraggedDefineMode(evt);
979 int res = findRes(evt);
986 if ((lastres == -1) || (lastres == res))
991 if ((res < av.getAlignment().getWidth()) && (res < lastres))
993 // dragLeft, delete gap
994 editSequence(false, res);
998 editSequence(true, res);
1001 mouseDragging = true;
1002 if (scrollThread != null)
1004 scrollThread.setEvent(evt);
1009 synchronized void editSequence(boolean insertGap, int startres)
1012 int fixedRight = -1;
1013 boolean fixedColumns = false;
1014 SequenceGroup sg = av.getSelectionGroup();
1016 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
1018 if (!groupEditing && av.hasHiddenRows())
1020 if (av.isHiddenRepSequence(seq))
1022 sg = av.getRepresentedSequences(seq);
1023 groupEditing = true;
1027 StringBuffer message = new StringBuffer();
1030 message.append(MessageManager.getString("action.edit_group")).append(
1032 if (editCommand == null)
1034 editCommand = new EditCommand(
1035 MessageManager.getString("action.edit_group"));
1040 message.append(MessageManager.getString("label.edit_sequence"))
1041 .append(" " + seq.getName());
1042 String label = seq.getName();
1043 if (label.length() > 10)
1045 label = label.substring(0, 10);
1047 if (editCommand == null)
1049 editCommand = new EditCommand(MessageManager.formatMessage(
1050 "label.edit_params", new String[]
1057 message.append(" insert ");
1061 message.append(" delete ");
1064 message.append(Math.abs(startres - lastres) + " gaps.");
1065 ap.alignFrame.statusBar.setText(message.toString());
1067 // Are we editing within a selection group?
1069 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1072 fixedColumns = true;
1074 // sg might be null as the user may only see 1 sequence,
1075 // but the sequence represents a group
1078 if (!av.isHiddenRepSequence(seq))
1084 sg = av.getRepresentedSequences(seq);
1087 fixedLeft = sg.getStartRes();
1088 fixedRight = sg.getEndRes();
1090 if ((startres < fixedLeft && lastres >= fixedLeft)
1091 || (startres >= fixedLeft && lastres < fixedLeft)
1092 || (startres > fixedRight && lastres <= fixedRight)
1093 || (startres <= fixedRight && lastres > fixedRight))
1099 if (fixedLeft > startres)
1101 fixedRight = fixedLeft - 1;
1104 else if (fixedRight < startres)
1106 fixedLeft = fixedRight;
1111 if (av.hasHiddenColumns())
1113 fixedColumns = true;
1114 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1115 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1117 if ((insertGap && startres > y1 && lastres < y1)
1118 || (!insertGap && startres < y2 && lastres > y2))
1124 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1125 // Selection spans a hidden region
1126 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1134 fixedRight = y2 - 1;
1141 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1142 .toArray(new SequenceI[0]);
1147 // If the user has selected the whole sequence, and is dragging to
1148 // the right, we can still extend the alignment and selectionGroup
1149 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1150 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1152 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1153 fixedRight = sg.getEndRes();
1156 // Is it valid with fixed columns??
1157 // Find the next gap before the end
1158 // of the visible region boundary
1159 boolean blank = false;
1160 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1164 for (SequenceI gs : groupSeqs)
1166 for (int j = 0; j < startres - lastres; j++)
1168 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1184 if (sg.getSize() == av.getAlignment().getHeight())
1186 if ((av.hasHiddenColumns() && startres < av
1187 .getColumnSelection().getHiddenBoundaryRight(startres)))
1193 int alWidth = av.getAlignment().getWidth();
1194 if (av.hasHiddenRows())
1196 int hwidth = av.getAlignment().getHiddenSequences()
1198 if (hwidth > alWidth)
1203 // We can still insert gaps if the selectionGroup
1204 // contains all the sequences
1205 sg.setEndRes(sg.getEndRes() + startres - lastres);
1206 fixedRight = alWidth + startres - lastres;
1217 else if (!insertGap)
1219 // / Are we able to delete?
1220 // ie are all columns blank?
1222 for (SequenceI gs : groupSeqs)
1224 for (int j = startres; j < lastres; j++)
1226 if (gs.getLength() <= j)
1231 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1233 // Not a gap, block edit not valid
1243 // dragging to the right
1244 if (fixedColumns && fixedRight != -1)
1246 for (int j = lastres; j < startres; j++)
1248 insertChar(j, groupSeqs, fixedRight);
1253 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1254 startres - lastres, av.getAlignment(), true);
1259 // dragging to the left
1260 if (fixedColumns && fixedRight != -1)
1262 for (int j = lastres; j > startres; j--)
1264 deleteChar(startres, groupSeqs, fixedRight);
1269 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1270 lastres - startres, av.getAlignment(), true);
1276 // ///Editing a single sequence///////////
1280 // dragging to the right
1281 if (fixedColumns && fixedRight != -1)
1283 for (int j = lastres; j < startres; j++)
1285 insertChar(j, new SequenceI[]
1286 { seq }, fixedRight);
1291 editCommand.appendEdit(Action.INSERT_GAP, new SequenceI[]
1292 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1297 // dragging to the left
1298 if (fixedColumns && fixedRight != -1)
1300 for (int j = lastres; j > startres; j--)
1302 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1307 deleteChar(startres, new SequenceI[]
1308 { seq }, fixedRight);
1313 // could be a keyboard edit trying to delete none gaps
1315 for (int m = startres; m < lastres; m++)
1317 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1326 editCommand.appendEdit(Action.DELETE_GAP, new SequenceI[]
1327 { seq }, startres, max, av.getAlignment(), true);
1334 seqCanvas.repaint();
1337 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1339 int blankColumn = fixedColumn;
1340 for (int s = 0; s < seq.length; s++)
1342 // Find the next gap before the end of the visible region boundary
1343 // If lastCol > j, theres a boundary after the gap insertion
1345 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1347 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1349 // Theres a space, so break and insert the gap
1354 if (blankColumn <= j)
1356 blankColumn = fixedColumn;
1362 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1363 av.getAlignment(), true);
1365 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1370 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1373 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1376 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1377 av.getAlignment(), true);
1380 // ////////////////////////////////////////
1381 // ///Everything below this is for defining the boundary of the rubberband
1382 // ////////////////////////////////////////
1383 public void doMousePressedDefineMode(MouseEvent evt)
1385 if (scrollThread != null)
1387 scrollThread.running = false;
1388 scrollThread = null;
1391 int res = findRes(evt);
1392 int seq = findSeq(evt);
1394 startWrapBlock = wrappedBlock;
1401 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1403 if (sequence == null || res > sequence.getLength())
1408 stretchGroup = av.getSelectionGroup();
1410 if (stretchGroup == null)
1412 stretchGroup = av.getAlignment().findGroup(sequence);
1413 if (stretchGroup != null && res > stretchGroup.getStartRes()
1414 && res < stretchGroup.getEndRes())
1416 av.setSelectionGroup(stretchGroup);
1420 stretchGroup = null;
1424 else if (!stretchGroup.getSequences(null).contains(sequence)
1425 || stretchGroup.getStartRes() > res
1426 || stretchGroup.getEndRes() < res)
1428 stretchGroup = null;
1430 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1432 if (allGroups != null)
1434 for (int i = 0; i < allGroups.length; i++)
1436 if (allGroups[i].getStartRes() <= res
1437 && allGroups[i].getEndRes() >= res)
1439 stretchGroup = allGroups[i];
1444 av.setSelectionGroup(stretchGroup);
1447 // DETECT RIGHT MOUSE BUTTON IN AWT
1448 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1450 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1451 sequence.findPosition(res));
1453 Vector<String> links = null;
1454 if (allFeatures != null)
1456 for (int i = 0; i < allFeatures.length; i++)
1458 if (allFeatures[i].links != null)
1462 links = new Vector<String>();
1464 for (int j = 0; j < allFeatures[i].links.size(); j++)
1466 links.addElement(allFeatures[i].links.elementAt(j));
1471 APopupMenu popup = new APopupMenu(ap, null, links);
1473 popup.show(this, evt.getX(), evt.getY());
1479 seqCanvas.cursorX = findRes(evt);
1480 seqCanvas.cursorY = findSeq(evt);
1481 seqCanvas.repaint();
1485 // Only if left mouse button do we want to change group sizes
1487 if (stretchGroup == null)
1489 // define a new group here
1490 SequenceGroup sg = new SequenceGroup();
1491 sg.setStartRes(res);
1493 sg.addSequence(sequence, false);
1494 av.setSelectionGroup(sg);
1497 if (av.getConservationSelected())
1499 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1502 if (av.getAbovePIDThreshold())
1504 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1511 public void doMouseReleasedDefineMode(MouseEvent evt)
1513 if (stretchGroup == null)
1518 stretchGroup.recalcConservation(); // always do this - annotation has own
1520 if (stretchGroup.cs != null)
1522 stretchGroup.cs.alignmentChanged(stretchGroup,
1523 av.getHiddenRepSequences());
1525 if (stretchGroup.cs.conservationApplied())
1527 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1528 stretchGroup.getName());
1532 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1533 stretchGroup.getName());
1536 changeEndRes = false;
1537 changeStartRes = false;
1538 stretchGroup = null;
1539 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1540 ap.paintAlignment(true);
1544 public void doMouseDraggedDefineMode(MouseEvent evt)
1546 int res = findRes(evt);
1547 int y = findSeq(evt);
1549 if (wrappedBlock != startWrapBlock)
1554 if (stretchGroup == null)
1559 mouseDragging = true;
1561 if (y > av.getAlignment().getHeight())
1563 y = av.getAlignment().getHeight() - 1;
1566 if (res >= av.getAlignment().getWidth())
1568 res = av.getAlignment().getWidth() - 1;
1571 if (stretchGroup.getEndRes() == res)
1573 // Edit end res position of selected group
1574 changeEndRes = true;
1576 else if (stretchGroup.getStartRes() == res)
1578 // Edit start res position of selected group
1579 changeStartRes = true;
1589 if (res > (stretchGroup.getStartRes() - 1))
1591 stretchGroup.setEndRes(res);
1594 else if (changeStartRes)
1596 if (res < (stretchGroup.getEndRes() + 1))
1598 stretchGroup.setStartRes(res);
1602 int dragDirection = 0;
1608 else if (y < oldSeq)
1613 while ((y != oldSeq) && (oldSeq > -1)
1614 && (y < av.getAlignment().getHeight()))
1616 // This routine ensures we don't skip any sequences, as the
1617 // selection is quite slow.
1618 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1620 oldSeq += dragDirection;
1627 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1629 if (stretchGroup.getSequences(null).contains(nextSeq))
1631 stretchGroup.deleteSequence(seq, false);
1637 stretchGroup.addSequence(seq, false);
1640 stretchGroup.addSequence(nextSeq, false);
1649 if (res > av.endRes || res < av.startRes || y < av.startSeq
1655 if (scrollThread != null)
1657 scrollThread.setEvent(evt);
1660 seqCanvas.repaint();
1663 public void mouseEntered(MouseEvent e)
1670 if (scrollThread != null)
1672 scrollThread.running = false;
1673 scrollThread = null;
1677 public void mouseExited(MouseEvent e)
1679 if (av.getWrapAlignment())
1684 if (mouseDragging && scrollThread == null)
1686 scrollThread = new ScrollThread();
1690 void scrollCanvas(MouseEvent evt)
1694 if (scrollThread != null)
1696 scrollThread.running = false;
1697 scrollThread = null;
1699 mouseDragging = false;
1703 if (scrollThread == null)
1705 scrollThread = new ScrollThread();
1708 mouseDragging = true;
1709 scrollThread.setEvent(evt);
1714 // this class allows scrolling off the bottom of the visible alignment
1715 class ScrollThread extends Thread
1719 boolean running = false;
1721 public ScrollThread()
1726 public void setEvent(MouseEvent e)
1731 public void stopScrolling()
1745 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1747 running = ap.scrollUp(true);
1750 if (mouseDragging && evt.getY() >= getSize().height
1751 && av.getAlignment().getHeight() > av.getEndSeq())
1753 running = ap.scrollUp(false);
1756 if (mouseDragging && evt.getX() < 0)
1758 running = ap.scrollRight(false);
1761 else if (mouseDragging && evt.getX() >= getSize().width)
1763 running = ap.scrollRight(true);
1770 } catch (Exception ex)
1778 * modify current selection according to a received message.
1780 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1781 SelectionSource source)
1783 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1784 // handles selection messages...
1785 // TODO: extend config options to allow user to control if selections may be
1786 // shared between viewports.
1788 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignmentViewport) source)
1789 .getSequenceSetId().equals(av.getSequenceSetId()))))
1795 * Check for selection in a view of which this one is a dna/protein
1798 if (selectionFromTranslation(seqsel, colsel, source))
1803 // do we want to thread this ? (contention with seqsel and colsel locks, I
1805 // rules are: colsel is copied if there is a real intersection between
1806 // sequence selection
1807 boolean repaint = false, copycolsel = true;
1808 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1810 SequenceGroup sgroup = null;
1811 if (seqsel != null && seqsel.getSize() > 0)
1813 if (av.getAlignment() == null)
1816 .println("Selection message: alignviewport av SeqSetId="
1817 + av.getSequenceSetId() + " ViewId="
1819 + " 's alignment is NULL! returning immediatly.");
1822 sgroup = seqsel.intersect(av.getAlignment(),
1823 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1824 if ((sgroup == null || sgroup.getSize() == 0)
1825 && (colsel == null || colsel.size() == 0))
1827 // don't copy columns if the region didn't intersect.
1831 if (sgroup != null && sgroup.getSize() > 0)
1833 av.setSelectionGroup(sgroup);
1837 av.setSelectionGroup(null);
1839 repaint = av.isSelectionGroupChanged(true);
1842 && (av.getColumnSelection() == null || !av
1843 .isColSelChanged(true)))
1845 // the current selection is unset or from a previous message
1846 // so import the new colsel.
1847 if (colsel == null || colsel.size() == 0)
1849 if (av.getColumnSelection() != null)
1851 av.getColumnSelection().clear();
1856 // TODO: shift colSel according to the intersecting sequences
1857 if (av.getColumnSelection() == null)
1859 av.setColumnSelection(new ColumnSelection(colsel));
1863 av.getColumnSelection().setElementsFrom(colsel);
1866 repaint |= av.isColSelChanged(true);
1869 && av.hasHiddenColumns()
1870 && (av.getColumnSelection() == null || av.getColumnSelection()
1871 .getHiddenColumns() == null))
1873 System.err.println("Bad things");
1877 ap.scalePanelHolder.repaint();
1883 * scroll to the given row/column - or nearest visible location
1888 public void scrollTo(int row, int column)
1891 row = row < 0 ? ap.av.startSeq : row;
1892 column = column < 0 ? ap.av.startRes : column;
1893 ap.scrollTo(column, column, row, true, true);
1897 * scroll to the given row - or nearest visible location
1901 public void scrollToRow(int row)
1904 row = row < 0 ? ap.av.startSeq : row;
1905 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1909 * scroll to the given column - or nearest visible location
1913 public void scrollToColumn(int column)
1916 column = column < 0 ? ap.av.startRes : column;
1917 ap.scrollTo(column, column, ap.av.startSeq, true, true);
1921 * If this panel is a cdna/protein translation view of the selection source,
1922 * tries to map the source selection to a local one, and returns true. Else
1929 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1930 ColumnSelection colsel, SelectionSource source)
1932 if (!(source instanceof AlignViewportI)) {
1935 final AlignViewportI sourceAv = (AlignViewportI) source;
1936 if (sourceAv.getCodingComplement() != av && av.getCodingComplement() != sourceAv)
1942 * Map sequence selection
1944 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1945 av.setSelectionGroup(sg);
1946 av.isSelectionGroupChanged(true);
1949 * Map column selection
1951 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1953 av.setColumnSelection(cs);
1954 av.isColSelChanged(true);
1956 ap.scalePanelHolder.repaint();