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() - m.getSequence().getStart();
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(
568 new SequenceI[] { 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()
610 + hgap + seqCanvas.getAnnotationHeight();
614 x -= seqCanvas.LABEL_WEST;
616 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
622 wrappedBlock = y / cHeight;
623 wrappedBlock += av.getStartRes() / cwidth;
625 res = wrappedBlock * cwidth + x / av.getCharWidth();
630 res = (x / av.getCharWidth()) + av.getStartRes();
633 if (av.hasHiddenColumns())
635 res = av.getColumnSelection().adjustForHiddenColumns(res);
642 int findSeq(MouseEvent evt)
644 final int sqnum = findAlRow(evt);
645 return (sqnum < 0) ? 0 : sqnum;
651 * @return row in alignment that was selected (or -1 for column selection)
653 private int findAlRow(MouseEvent evt)
658 if (av.getWrapAlignment())
660 int hgap = av.getCharHeight();
661 if (av.getScaleAboveWrapped())
663 hgap += av.getCharHeight();
666 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
667 + hgap + seqCanvas.getAnnotationHeight();
671 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
680 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
681 .getAlignment().getHeight() - 1);
691 public void doMousePressed(MouseEvent evt)
694 int seq = findSeq(evt);
695 int res = findRes(evt);
697 if (seq < av.getAlignment().getHeight()
698 && res < av.getAlignment().getSequenceAt(seq).getLength())
700 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
701 // Find the residue's position in the sequence (res is the position
718 public void mouseOverSequence(SequenceI sequence, int index, int pos)
720 String tmp = sequence.hashCode() + index + "";
721 if (lastMessage == null || !lastMessage.equals(tmp))
723 ssm.mouseOverSequence(sequence, index, pos, av);
729 public void highlightSequence(SearchResults results)
731 if (av.isFollowHighlight())
733 if (ap.scrollToPosition(results, true))
735 ap.alignFrame.repaint();
738 setStatusMessage(results);
739 seqCanvas.highlightSearchResults(results);
744 public VamsasSource getVamsasSource()
746 return this.ap == null ? null : this.ap.av;
749 public void updateColours(SequenceI seq, int index)
751 System.out.println("update the seqPanel colours");
755 public void mouseMoved(MouseEvent evt)
757 int res = findRes(evt);
758 int seq = findSeq(evt);
760 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
769 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
770 if (res > sequence.getLength())
779 int respos = sequence.findPosition(res);
782 mouseOverSequence(sequence, res, respos);
785 StringBuilder text = new StringBuilder();
786 text.append("Sequence ").append(Integer.toString(seq + 1))
787 .append(" ID: ").append(sequence.getName());
790 final String ch = String.valueOf(sequence.getCharAt(res));
791 if (av.getAlignment().isNucleotide())
793 obj = ResidueProperties.nucleotideName.get(ch);
796 text.append(" Nucleotide: ").append(obj);
801 obj = "X".equalsIgnoreCase(ch) ? "X" : ResidueProperties.aa2Triplet
805 text.append(" Residue: ").append(obj);
811 text.append(" (").append(Integer.toString(respos)).append(")");
814 ap.alignFrame.statusBar.setText(text.toString());
816 StringBuilder tooltipText = new StringBuilder();
817 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
820 for (int g = 0; g < groups.length; g++)
822 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
824 if (!groups[g].getName().startsWith("JTreeGroup")
825 && !groups[g].getName().startsWith("JGroup"))
827 tooltipText.append(groups[g].getName()).append(" ");
829 if (groups[g].getDescription() != null)
831 tooltipText.append(groups[g].getDescription());
833 tooltipText.append("\n");
838 // use aa to see if the mouse pointer is on a
839 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
840 sequence.findPosition(res));
843 while (index < allFeatures.length)
845 SequenceFeature sf = allFeatures[index];
847 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
849 if (sf.getDescription() != null)
851 tooltipText.append(" " + sf.getDescription());
854 if (sf.getValue("status") != null)
856 String status = sf.getValue("status").toString();
857 if (status.length() > 0)
859 tooltipText.append(" (" + sf.getValue("status") + ")");
862 tooltipText.append("\n");
869 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
873 tooltip.setTip(tooltipText.toString());
877 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
879 Vector tmp = new Vector();
880 SequenceFeature[] features = sequence.getSequenceFeatures();
881 if (features != null)
883 for (int i = 0; i < features.length; i++)
885 if (av.getFeaturesDisplayed() == null
886 || !av.getFeaturesDisplayed().isVisible(
887 features[i].getType()))
892 if (features[i].featureGroup != null
893 && !seqCanvas.fr.checkGroupVisibility(
894 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[] { label }));
1056 message.append(" insert ");
1060 message.append(" delete ");
1063 message.append(Math.abs(startres - lastres) + " gaps.");
1064 ap.alignFrame.statusBar.setText(message.toString());
1066 // Are we editing within a selection group?
1068 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1071 fixedColumns = true;
1073 // sg might be null as the user may only see 1 sequence,
1074 // but the sequence represents a group
1077 if (!av.isHiddenRepSequence(seq))
1083 sg = av.getRepresentedSequences(seq);
1086 fixedLeft = sg.getStartRes();
1087 fixedRight = sg.getEndRes();
1089 if ((startres < fixedLeft && lastres >= fixedLeft)
1090 || (startres >= fixedLeft && lastres < fixedLeft)
1091 || (startres > fixedRight && lastres <= fixedRight)
1092 || (startres <= fixedRight && lastres > fixedRight))
1098 if (fixedLeft > startres)
1100 fixedRight = fixedLeft - 1;
1103 else if (fixedRight < startres)
1105 fixedLeft = fixedRight;
1110 if (av.hasHiddenColumns())
1112 fixedColumns = true;
1113 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1114 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1116 if ((insertGap && startres > y1 && lastres < y1)
1117 || (!insertGap && startres < y2 && lastres > y2))
1123 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1124 // Selection spans a hidden region
1125 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1133 fixedRight = y2 - 1;
1140 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1141 .toArray(new SequenceI[0]);
1146 // If the user has selected the whole sequence, and is dragging to
1147 // the right, we can still extend the alignment and selectionGroup
1148 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1149 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1151 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1152 fixedRight = sg.getEndRes();
1155 // Is it valid with fixed columns??
1156 // Find the next gap before the end
1157 // of the visible region boundary
1158 boolean blank = false;
1159 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1163 for (SequenceI gs : groupSeqs)
1165 for (int j = 0; j < startres - lastres; j++)
1167 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1183 if (sg.getSize() == av.getAlignment().getHeight())
1185 if ((av.hasHiddenColumns() && startres < av
1186 .getColumnSelection().getHiddenBoundaryRight(startres)))
1192 int alWidth = av.getAlignment().getWidth();
1193 if (av.hasHiddenRows())
1195 int hwidth = av.getAlignment().getHiddenSequences()
1197 if (hwidth > alWidth)
1202 // We can still insert gaps if the selectionGroup
1203 // contains all the sequences
1204 sg.setEndRes(sg.getEndRes() + startres - lastres);
1205 fixedRight = alWidth + startres - lastres;
1216 else if (!insertGap)
1218 // / Are we able to delete?
1219 // ie are all columns blank?
1221 for (SequenceI gs : groupSeqs)
1223 for (int j = startres; j < lastres; j++)
1225 if (gs.getLength() <= j)
1230 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1232 // Not a gap, block edit not valid
1242 // dragging to the right
1243 if (fixedColumns && fixedRight != -1)
1245 for (int j = lastres; j < startres; j++)
1247 insertChar(j, groupSeqs, fixedRight);
1252 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1253 startres - lastres, av.getAlignment(), true);
1258 // dragging to the left
1259 if (fixedColumns && fixedRight != -1)
1261 for (int j = lastres; j > startres; j--)
1263 deleteChar(startres, groupSeqs, fixedRight);
1268 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1269 lastres - startres, av.getAlignment(), true);
1275 // ///Editing a single sequence///////////
1279 // dragging to the right
1280 if (fixedColumns && fixedRight != -1)
1282 for (int j = lastres; j < startres; j++)
1284 insertChar(j, new SequenceI[] { seq }, fixedRight);
1289 editCommand.appendEdit(Action.INSERT_GAP,
1290 new SequenceI[] { seq }, lastres, startres - lastres,
1291 av.getAlignment(), true);
1296 // dragging to the left
1297 if (fixedColumns && fixedRight != -1)
1299 for (int j = lastres; j > startres; j--)
1301 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1306 deleteChar(startres, new SequenceI[] { seq }, fixedRight);
1311 // could be a keyboard edit trying to delete none gaps
1313 for (int m = startres; m < lastres; m++)
1315 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1324 editCommand.appendEdit(Action.DELETE_GAP,
1325 new SequenceI[] { seq }, startres, max,
1326 av.getAlignment(), true);
1333 seqCanvas.repaint();
1336 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1338 int blankColumn = fixedColumn;
1339 for (int s = 0; s < seq.length; s++)
1341 // Find the next gap before the end of the visible region boundary
1342 // If lastCol > j, theres a boundary after the gap insertion
1344 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1346 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1348 // Theres a space, so break and insert the gap
1353 if (blankColumn <= j)
1355 blankColumn = fixedColumn;
1361 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1362 av.getAlignment(), true);
1364 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1369 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1372 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1375 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1376 av.getAlignment(), true);
1379 // ////////////////////////////////////////
1380 // ///Everything below this is for defining the boundary of the rubberband
1381 // ////////////////////////////////////////
1382 public void doMousePressedDefineMode(MouseEvent evt)
1384 if (scrollThread != null)
1386 scrollThread.running = false;
1387 scrollThread = null;
1390 int res = findRes(evt);
1391 int seq = findSeq(evt);
1393 startWrapBlock = wrappedBlock;
1400 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1402 if (sequence == null || res > sequence.getLength())
1407 stretchGroup = av.getSelectionGroup();
1409 if (stretchGroup == null)
1411 stretchGroup = av.getAlignment().findGroup(sequence);
1412 if (stretchGroup != null && res > stretchGroup.getStartRes()
1413 && res < stretchGroup.getEndRes())
1415 av.setSelectionGroup(stretchGroup);
1419 stretchGroup = null;
1423 else if (!stretchGroup.getSequences(null).contains(sequence)
1424 || stretchGroup.getStartRes() > res
1425 || stretchGroup.getEndRes() < res)
1427 stretchGroup = null;
1429 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1431 if (allGroups != null)
1433 for (int i = 0; i < allGroups.length; i++)
1435 if (allGroups[i].getStartRes() <= res
1436 && allGroups[i].getEndRes() >= res)
1438 stretchGroup = allGroups[i];
1443 av.setSelectionGroup(stretchGroup);
1446 // DETECT RIGHT MOUSE BUTTON IN AWT
1447 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1449 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1450 sequence.findPosition(res));
1452 Vector<String> links = null;
1453 if (allFeatures != null)
1455 for (int i = 0; i < allFeatures.length; i++)
1457 if (allFeatures[i].links != null)
1461 links = new Vector<String>();
1463 for (int j = 0; j < allFeatures[i].links.size(); j++)
1465 links.addElement(allFeatures[i].links.elementAt(j));
1470 APopupMenu popup = new APopupMenu(ap, null, links);
1472 popup.show(this, evt.getX(), evt.getY());
1478 seqCanvas.cursorX = findRes(evt);
1479 seqCanvas.cursorY = findSeq(evt);
1480 seqCanvas.repaint();
1484 // Only if left mouse button do we want to change group sizes
1486 if (stretchGroup == null)
1488 // define a new group here
1489 SequenceGroup sg = new SequenceGroup();
1490 sg.setStartRes(res);
1492 sg.addSequence(sequence, false);
1493 av.setSelectionGroup(sg);
1496 if (av.getConservationSelected())
1498 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1501 if (av.getAbovePIDThreshold())
1503 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1510 public void doMouseReleasedDefineMode(MouseEvent evt)
1512 if (stretchGroup == null)
1517 stretchGroup.recalcConservation(); // always do this - annotation has own
1519 if (stretchGroup.cs != null)
1521 stretchGroup.cs.alignmentChanged(stretchGroup,
1522 av.getHiddenRepSequences());
1524 if (stretchGroup.cs.conservationApplied())
1526 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1527 stretchGroup.getName());
1531 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1532 stretchGroup.getName());
1535 changeEndRes = false;
1536 changeStartRes = false;
1537 stretchGroup = null;
1538 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1539 ap.paintAlignment(true);
1543 public void doMouseDraggedDefineMode(MouseEvent evt)
1545 int res = findRes(evt);
1546 int y = findSeq(evt);
1548 if (wrappedBlock != startWrapBlock)
1553 if (stretchGroup == null)
1558 mouseDragging = true;
1560 if (y > av.getAlignment().getHeight())
1562 y = av.getAlignment().getHeight() - 1;
1565 if (res >= av.getAlignment().getWidth())
1567 res = av.getAlignment().getWidth() - 1;
1570 if (stretchGroup.getEndRes() == res)
1572 // Edit end res position of selected group
1573 changeEndRes = true;
1575 else if (stretchGroup.getStartRes() == res)
1577 // Edit start res position of selected group
1578 changeStartRes = true;
1588 if (res > (stretchGroup.getStartRes() - 1))
1590 stretchGroup.setEndRes(res);
1593 else if (changeStartRes)
1595 if (res < (stretchGroup.getEndRes() + 1))
1597 stretchGroup.setStartRes(res);
1601 int dragDirection = 0;
1607 else if (y < oldSeq)
1612 while ((y != oldSeq) && (oldSeq > -1)
1613 && (y < av.getAlignment().getHeight()))
1615 // This routine ensures we don't skip any sequences, as the
1616 // selection is quite slow.
1617 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1619 oldSeq += dragDirection;
1626 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1628 if (stretchGroup.getSequences(null).contains(nextSeq))
1630 stretchGroup.deleteSequence(seq, false);
1636 stretchGroup.addSequence(seq, false);
1639 stretchGroup.addSequence(nextSeq, false);
1648 if (res > av.endRes || res < av.startRes || y < av.startSeq
1654 if (scrollThread != null)
1656 scrollThread.setEvent(evt);
1659 seqCanvas.repaint();
1662 public void mouseEntered(MouseEvent e)
1669 if (scrollThread != null)
1671 scrollThread.running = false;
1672 scrollThread = null;
1676 public void mouseExited(MouseEvent e)
1678 if (av.getWrapAlignment())
1683 if (mouseDragging && scrollThread == null)
1685 scrollThread = new ScrollThread();
1689 void scrollCanvas(MouseEvent evt)
1693 if (scrollThread != null)
1695 scrollThread.running = false;
1696 scrollThread = null;
1698 mouseDragging = false;
1702 if (scrollThread == null)
1704 scrollThread = new ScrollThread();
1707 mouseDragging = true;
1708 scrollThread.setEvent(evt);
1713 // this class allows scrolling off the bottom of the visible alignment
1714 class ScrollThread extends Thread
1718 boolean running = false;
1720 public ScrollThread()
1725 public void setEvent(MouseEvent e)
1730 public void stopScrolling()
1744 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1746 running = ap.scrollUp(true);
1749 if (mouseDragging && evt.getY() >= getSize().height
1750 && av.getAlignment().getHeight() > av.getEndSeq())
1752 running = ap.scrollUp(false);
1755 if (mouseDragging && evt.getX() < 0)
1757 running = ap.scrollRight(false);
1760 else if (mouseDragging && evt.getX() >= getSize().width)
1762 running = ap.scrollRight(true);
1769 } catch (Exception ex)
1777 * modify current selection according to a received message.
1779 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1780 SelectionSource source)
1782 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1783 // handles selection messages...
1784 // TODO: extend config options to allow user to control if selections may be
1785 // shared between viewports.
1787 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignmentViewport) source)
1788 .getSequenceSetId().equals(av.getSequenceSetId()))))
1794 * Check for selection in a view of which this one is a dna/protein
1797 if (selectionFromTranslation(seqsel, colsel, source))
1802 // do we want to thread this ? (contention with seqsel and colsel locks, I
1804 // rules are: colsel is copied if there is a real intersection between
1805 // sequence selection
1806 boolean repaint = false, copycolsel = true;
1807 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1809 SequenceGroup sgroup = null;
1810 if (seqsel != null && seqsel.getSize() > 0)
1812 if (av.getAlignment() == null)
1815 .println("Selection message: alignviewport av SeqSetId="
1816 + av.getSequenceSetId() + " ViewId="
1818 + " 's alignment is NULL! returning immediatly.");
1821 sgroup = seqsel.intersect(av.getAlignment(),
1822 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1823 if ((sgroup == null || sgroup.getSize() == 0)
1824 && (colsel == null || colsel.size() == 0))
1826 // don't copy columns if the region didn't intersect.
1830 if (sgroup != null && sgroup.getSize() > 0)
1832 av.setSelectionGroup(sgroup);
1836 av.setSelectionGroup(null);
1838 repaint = av.isSelectionGroupChanged(true);
1841 && (av.getColumnSelection() == null || !av
1842 .isColSelChanged(true)))
1844 // the current selection is unset or from a previous message
1845 // so import the new colsel.
1846 if (colsel == null || colsel.size() == 0)
1848 if (av.getColumnSelection() != null)
1850 av.getColumnSelection().clear();
1855 // TODO: shift colSel according to the intersecting sequences
1856 if (av.getColumnSelection() == null)
1858 av.setColumnSelection(new ColumnSelection(colsel));
1862 av.getColumnSelection().setElementsFrom(colsel);
1865 repaint |= av.isColSelChanged(true);
1868 && av.hasHiddenColumns()
1869 && (av.getColumnSelection() == null || av.getColumnSelection()
1870 .getHiddenColumns() == null))
1872 System.err.println("Bad things");
1876 ap.scalePanelHolder.repaint();
1882 * scroll to the given row/column - or nearest visible location
1887 public void scrollTo(int row, int column)
1890 row = row < 0 ? ap.av.startSeq : row;
1891 column = column < 0 ? ap.av.startRes : column;
1892 ap.scrollTo(column, column, row, true, true);
1896 * scroll to the given row - or nearest visible location
1900 public void scrollToRow(int row)
1903 row = row < 0 ? ap.av.startSeq : row;
1904 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1908 * scroll to the given column - or nearest visible location
1912 public void scrollToColumn(int column)
1915 column = column < 0 ? ap.av.startRes : column;
1916 ap.scrollTo(column, column, ap.av.startSeq, true, true);
1920 * If this panel is a cdna/protein translation view of the selection source,
1921 * tries to map the source selection to a local one, and returns true. Else
1928 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1929 ColumnSelection colsel, SelectionSource source)
1931 if (!(source instanceof AlignViewportI))
1935 final AlignViewportI sourceAv = (AlignViewportI) source;
1936 if (sourceAv.getCodingComplement() != av
1937 && av.getCodingComplement() != sourceAv)
1943 * Map sequence selection
1945 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1946 av.setSelectionGroup(sg);
1947 av.isSelectionGroupChanged(true);
1950 * Map column selection
1952 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1954 av.setColumnSelection(cs);
1955 av.isColSelChanged(true);
1957 ap.scalePanelHolder.repaint();