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.
23 import jalview.commands.EditCommand;
24 import jalview.commands.EditCommand.Action;
25 import jalview.commands.EditCommand.Edit;
26 import jalview.datamodel.ColumnSelection;
27 import jalview.datamodel.SearchResults;
28 import jalview.datamodel.Sequence;
29 import jalview.datamodel.SequenceFeature;
30 import jalview.datamodel.SequenceGroup;
31 import jalview.datamodel.SequenceI;
32 import jalview.io.SequenceAnnotationReport;
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.Comparison;
40 import jalview.util.MessageManager;
42 import java.awt.BorderLayout;
43 import java.awt.Color;
45 import java.awt.FontMetrics;
46 import java.awt.Point;
47 import java.awt.event.MouseEvent;
48 import java.awt.event.MouseListener;
49 import java.awt.event.MouseMotionListener;
50 import java.awt.event.MouseWheelEvent;
51 import java.awt.event.MouseWheelListener;
52 import java.util.List;
53 import java.util.Vector;
55 import javax.swing.JOptionPane;
56 import javax.swing.JPanel;
57 import javax.swing.ToolTipManager;
63 * @version $Revision: 1.130 $
65 public class SeqPanel extends JPanel implements MouseListener,
66 MouseMotionListener, MouseWheelListener, SequenceListener,
71 public SeqCanvas seqCanvas;
74 public AlignmentPanel ap;
76 protected int lastres;
78 protected int startseq;
80 protected AlignViewport av;
82 ScrollThread scrollThread = null;
84 boolean mouseDragging = false;
86 boolean editingSeqs = false;
88 boolean groupEditing = false;
90 // ////////////////////////////////////////
91 // ///Everything below this is for defining the boundary of the rubberband
92 // ////////////////////////////////////////
95 boolean changeEndSeq = false;
97 boolean changeStartSeq = false;
99 boolean changeEndRes = false;
101 boolean changeStartRes = false;
103 SequenceGroup stretchGroup = null;
105 boolean remove = false;
107 Point lastMousePress;
109 boolean mouseWheelPressed = false;
111 StringBuffer keyboardNo1;
113 StringBuffer keyboardNo2;
115 java.net.URL linkImageURL;
117 private final SequenceAnnotationReport seqARep;
119 StringBuffer tooltipText = new StringBuffer();
123 EditCommand editCommand;
125 StructureSelectionManager ssm;
128 * Creates a new SeqPanel object.
135 public SeqPanel(AlignViewport av, AlignmentPanel ap)
137 linkImageURL = getClass().getResource("/images/link.gif");
138 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
139 ToolTipManager.sharedInstance().registerComponent(this);
140 ToolTipManager.sharedInstance().setInitialDelay(0);
141 ToolTipManager.sharedInstance().setDismissDelay(10000);
143 setBackground(Color.white);
145 seqCanvas = new SeqCanvas(ap);
146 setLayout(new BorderLayout());
147 add(seqCanvas, BorderLayout.CENTER);
153 addMouseMotionListener(this);
154 addMouseListener(this);
155 addMouseWheelListener(this);
156 ssm = av.getStructureSelectionManager();
157 ssm.addStructureViewerListener(this);
158 ssm.addSelectionListener(this);
162 int startWrapBlock = -1;
164 int wrappedBlock = -1;
166 int findRes(MouseEvent evt)
171 if (av.wrapAlignment)
174 int hgap = av.charHeight;
175 if (av.scaleAboveWrapped)
177 hgap += av.charHeight;
180 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
181 + seqCanvas.getAnnotationHeight();
185 x -= seqCanvas.LABEL_WEST;
187 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
193 wrappedBlock = y / cHeight;
194 wrappedBlock += av.getStartRes() / cwidth;
196 res = wrappedBlock * cwidth + x / av.getCharWidth();
201 if (x > seqCanvas.getWidth() + seqCanvas.getWidth())
203 // make sure we calculate relative to visible alignment, rather than
205 x = seqCanvas.getX() + seqCanvas.getWidth();
207 res = (x / av.getCharWidth()) + av.getStartRes();
210 if (av.hasHiddenColumns())
212 res = av.getColumnSelection().adjustForHiddenColumns(res);
219 int findSeq(MouseEvent evt)
224 if (av.wrapAlignment)
226 int hgap = av.charHeight;
227 if (av.scaleAboveWrapped)
229 hgap += av.charHeight;
232 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
233 + seqCanvas.getAnnotationHeight();
237 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
242 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
243 .getAlignment().getHeight() - 1);
249 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
251 Vector tmp = new Vector();
252 SequenceFeature[] features = sequence.getSequenceFeatures();
253 if (features != null)
255 for (int i = 0; i < features.length; i++)
257 if (av.featuresDisplayed == null
258 || !av.featuresDisplayed.containsKey(features[i].getType()))
263 if (features[i].featureGroup != null
264 && seqCanvas.fr.featureGroups != null
265 && seqCanvas.fr.featureGroups
266 .containsKey(features[i].featureGroup)
267 && !((Boolean) seqCanvas.fr.featureGroups
268 .get(features[i].featureGroup)).booleanValue())
273 if ((features[i].getBegin() <= res)
274 && (features[i].getEnd() >= res))
276 tmp.addElement(features[i]);
281 features = new SequenceFeature[tmp.size()];
282 tmp.copyInto(features);
288 * When all of a sequence of edits are complete, put the resulting edit list
289 * on the history stack (undo list), and reset flags for editing in progress.
295 if (editCommand != null && editCommand.getSize() > 0)
297 ap.alignFrame.addHistoryItem(editCommand);
298 av.firePropertyChange("alignment", null, av.getAlignment()
304 * Tidy up come what may...
309 groupEditing = false;
318 seqCanvas.cursorY = getKeyboardNo1() - 1;
322 void setCursorColumn()
324 seqCanvas.cursorX = getKeyboardNo1() - 1;
328 void setCursorRowAndColumn()
330 if (keyboardNo2 == null)
332 keyboardNo2 = new StringBuffer();
336 seqCanvas.cursorX = getKeyboardNo1() - 1;
337 seqCanvas.cursorY = getKeyboardNo2() - 1;
342 void setCursorPosition()
344 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
346 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
350 void moveCursor(int dx, int dy)
352 seqCanvas.cursorX += dx;
353 seqCanvas.cursorY += dy;
354 if (av.hasHiddenColumns()
355 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
357 int original = seqCanvas.cursorX - dx;
358 int maxWidth = av.getAlignment().getWidth();
360 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
361 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
363 seqCanvas.cursorX += dx;
366 if (seqCanvas.cursorX >= maxWidth
367 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
369 seqCanvas.cursorX = original;
376 void scrollToVisible()
378 if (seqCanvas.cursorX < 0)
380 seqCanvas.cursorX = 0;
382 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
384 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
387 if (seqCanvas.cursorY < 0)
389 seqCanvas.cursorY = 0;
391 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
393 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
397 if (av.wrapAlignment)
399 ap.scrollToWrappedVisible(seqCanvas.cursorX);
403 while (seqCanvas.cursorY < av.startSeq)
407 while (seqCanvas.cursorY + 1 > av.endSeq)
411 if (!av.wrapAlignment)
413 while (seqCanvas.cursorX < av.getColumnSelection()
414 .adjustForHiddenColumns(av.startRes))
416 if (!ap.scrollRight(false))
421 while (seqCanvas.cursorX > av.getColumnSelection()
422 .adjustForHiddenColumns(av.endRes))
424 if (!ap.scrollRight(true))
431 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
432 seqCanvas.cursorX, seqCanvas.cursorY);
437 void setSelectionAreaAtCursor(boolean topLeft)
439 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
441 if (av.getSelectionGroup() != null)
443 SequenceGroup sg = av.getSelectionGroup();
444 // Find the top and bottom of this group
445 int min = av.getAlignment().getHeight(), max = 0;
446 for (int i = 0; i < sg.getSize(); i++)
448 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
463 sg.setStartRes(seqCanvas.cursorX);
464 if (sg.getEndRes() < seqCanvas.cursorX)
466 sg.setEndRes(seqCanvas.cursorX);
469 min = seqCanvas.cursorY;
473 sg.setEndRes(seqCanvas.cursorX);
474 if (sg.getStartRes() > seqCanvas.cursorX)
476 sg.setStartRes(seqCanvas.cursorX);
479 max = seqCanvas.cursorY + 1;
484 // Only the user can do this
485 av.setSelectionGroup(null);
489 // Now add any sequences between min and max
490 sg.getSequences(null).clear();
491 for (int i = min; i < max; i++)
493 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
498 if (av.getSelectionGroup() == null)
500 SequenceGroup sg = new SequenceGroup();
501 sg.setStartRes(seqCanvas.cursorX);
502 sg.setEndRes(seqCanvas.cursorX);
503 sg.addSequence(sequence, false);
504 av.setSelectionGroup(sg);
507 ap.paintAlignment(false);
511 void insertGapAtCursor(boolean group)
513 groupEditing = group;
514 startseq = seqCanvas.cursorY;
515 lastres = seqCanvas.cursorX;
516 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
520 void deleteGapAtCursor(boolean group)
522 groupEditing = group;
523 startseq = seqCanvas.cursorY;
524 lastres = seqCanvas.cursorX + getKeyboardNo1();
525 editSequence(false, false, seqCanvas.cursorX);
529 void insertNucAtCursor(boolean group, String nuc)
531 groupEditing = group;
532 startseq = seqCanvas.cursorY;
533 lastres = seqCanvas.cursorX;
534 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
538 void numberPressed(char value)
540 if (keyboardNo1 == null)
542 keyboardNo1 = new StringBuffer();
545 if (keyboardNo2 != null)
547 keyboardNo2.append(value);
551 keyboardNo1.append(value);
559 if (keyboardNo1 != null)
561 int value = Integer.parseInt(keyboardNo1.toString());
565 } catch (Exception x)
576 if (keyboardNo2 != null)
578 int value = Integer.parseInt(keyboardNo2.toString());
582 } catch (Exception x)
596 public void mouseReleased(MouseEvent evt)
598 mouseDragging = false;
599 mouseWheelPressed = false;
603 doMouseReleasedDefineMode(evt);
617 public void mousePressed(MouseEvent evt)
619 lastMousePress = evt.getPoint();
621 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
623 mouseWheelPressed = true;
627 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
629 if (evt.isAltDown() || evt.isControlDown())
637 doMousePressedDefineMode(evt);
641 int seq = findSeq(evt);
642 int res = findRes(evt);
644 if (seq < 0 || res < 0)
649 if ((seq < av.getAlignment().getHeight())
650 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
666 public void mouseOverSequence(SequenceI sequence, int index, int pos)
668 String tmp = sequence.hashCode() + " " + index + " " + pos;
670 if (lastMessage == null || !lastMessage.equals(tmp))
672 // System.err.println("mouseOver Sequence: "+tmp);
673 ssm.mouseOverSequence(sequence, index, pos, av);
679 public void highlightSequence(SearchResults results)
681 if (av.followHighlight)
683 if (ap.scrollToPosition(results, false))
685 seqCanvas.revalidate();
688 seqCanvas.highlightSearchResults(results);
692 public VamsasSource getVamsasSource()
694 return this.ap == null ? null : this.ap.av;
696 public void updateColours(SequenceI seq, int index)
698 System.out.println("update the seqPanel colours");
709 public void mouseMoved(MouseEvent evt)
713 // This is because MacOSX creates a mouseMoved
714 // If control is down, other platforms will not.
718 int res = findRes(evt);
719 int seq = findSeq(evt);
721 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
726 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
728 if (res >= sequence.getLength())
733 pos = setStatusMessage(sequence, res, seq);
734 if (ssm != null && pos > -1)
736 mouseOverSequence(sequence, res, pos);
739 tooltipText.setLength(6); // Cuts the buffer back to <html>
741 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
744 for (int g = 0; g < groups.length; g++)
746 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
748 if (!groups[g].getName().startsWith("JTreeGroup")
749 && !groups[g].getName().startsWith("JGroup"))
751 tooltipText.append(groups[g].getName());
754 if (groups[g].getDescription() != null)
756 tooltipText.append(": " + groups[g].getDescription());
762 // use aa to see if the mouse pointer is on a
763 if (av.showSequenceFeatures)
766 SequenceFeature[] features = findFeaturesAtRes(
767 sequence.getDatasetSequence(),
768 rpos = sequence.findPosition(res));
769 seqARep.appendFeatures(tooltipText, rpos, features,
770 this.ap.seqPanel.seqCanvas.fr.minmax);
772 if (tooltipText.length() == 6) // <html></html>
774 setToolTipText(null);
779 if (lastTooltip == null
780 || !lastTooltip.equals(tooltipText.toString()))
782 String formatedTooltipText = JvSwingUtils.wrapTooltip(true,
783 tooltipText.toString());
784 // String formatedTooltipText = tooltipText.toString();
785 setToolTipText(formatedTooltipText);
786 lastTooltip = tooltipText.toString();
793 private Point lastp = null;
798 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
800 public Point getToolTipLocation(MouseEvent event)
802 int x = event.getX(), w = getWidth();
803 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
806 if (!event.isShiftDown() || p == null)
808 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
809 event.getX() + wdth, event.getY() - 20) : null;
812 * TODO: try to modify position region is not obcured by tooltip
820 * Set status message in alignment panel
823 * aligned sequence object
827 * index of sequence in alignment
828 * @return position of res in sequence
830 int setStatusMessage(SequenceI sequence, int res, int seq)
833 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
834 + sequence.getName());
837 if (av.getAlignment().isNucleotide())
839 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
843 text.append(" Nucleotide: ");
848 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
851 text.append(" Residue: ");
857 pos = sequence.findPosition(res);
860 text.append(obj + " (" + pos + ")");
863 ap.alignFrame.statusBar.setText(text.toString());
874 public void mouseDragged(MouseEvent evt)
876 if (mouseWheelPressed)
878 int oldWidth = av.charWidth;
880 // Which is bigger, left-right or up-down?
881 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
882 .getX() - lastMousePress.getX()))
884 int fontSize = av.font.getSize();
886 if (evt.getY() < lastMousePress.getY())
890 else if (evt.getY() > lastMousePress.getY())
900 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
901 av.charWidth = oldWidth;
906 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
910 else if (evt.getX() > lastMousePress.getX())
915 ap.paintAlignment(false);
918 FontMetrics fm = getFontMetrics(av.getFont());
919 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
921 lastMousePress = evt.getPoint();
928 doMouseDraggedDefineMode(evt);
932 int res = findRes(evt);
939 if ((lastres == -1) || (lastres == res))
944 if ((res < av.getAlignment().getWidth()) && (res < lastres))
946 // dragLeft, delete gap
947 editSequence(false, false, res);
951 editSequence(true, false, res);
954 mouseDragging = true;
955 if (scrollThread != null)
957 scrollThread.setEvent(evt);
961 // TODO: Make it more clever than many booleans
962 synchronized void editSequence(boolean insertGap, boolean editSeq,
967 boolean fixedColumns = false;
968 SequenceGroup sg = av.getSelectionGroup();
970 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
972 // No group, but the sequence may represent a group
973 if (!groupEditing && av.hasHiddenRows())
975 if (av.isHiddenRepSequence(seq))
977 sg = av.getRepresentedSequences(seq);
982 StringBuilder message = new StringBuilder(64);
985 message.append("Edit group:");
986 if (editCommand == null)
988 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
993 message.append("Edit sequence: " + seq.getName());
994 String label = seq.getName();
995 if (label.length() > 10)
997 label = label.substring(0, 10);
999 if (editCommand == null)
1001 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
1007 message.append(" insert ");
1011 message.append(" delete ");
1014 message.append(Math.abs(startres - lastres) + " gaps.");
1015 ap.alignFrame.statusBar.setText(message.toString());
1017 // Are we editing within a selection group?
1019 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1022 fixedColumns = true;
1024 // sg might be null as the user may only see 1 sequence,
1025 // but the sequence represents a group
1028 if (!av.isHiddenRepSequence(seq))
1033 sg = av.getRepresentedSequences(seq);
1036 fixedLeft = sg.getStartRes();
1037 fixedRight = sg.getEndRes();
1039 if ((startres < fixedLeft && lastres >= fixedLeft)
1040 || (startres >= fixedLeft && lastres < fixedLeft)
1041 || (startres > fixedRight && lastres <= fixedRight)
1042 || (startres <= fixedRight && lastres > fixedRight))
1048 if (fixedLeft > startres)
1050 fixedRight = fixedLeft - 1;
1053 else if (fixedRight < startres)
1055 fixedLeft = fixedRight;
1060 if (av.hasHiddenColumns())
1062 fixedColumns = true;
1063 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1064 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1066 if ((insertGap && startres > y1 && lastres < y1)
1067 || (!insertGap && startres < y2 && lastres > y2))
1073 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1074 // Selection spans a hidden region
1075 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1083 fixedRight = y2 - 1;
1090 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1091 int g, groupSize = vseqs.size();
1092 SequenceI[] groupSeqs = new SequenceI[groupSize];
1093 for (g = 0; g < groupSeqs.length; g++)
1095 groupSeqs[g] = vseqs.get(g);
1101 // If the user has selected the whole sequence, and is dragging to
1102 // the right, we can still extend the alignment and selectionGroup
1103 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1104 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1106 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1107 fixedRight = sg.getEndRes();
1110 // Is it valid with fixed columns??
1111 // Find the next gap before the end
1112 // of the visible region boundary
1113 boolean blank = false;
1114 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1118 for (g = 0; g < groupSize; g++)
1120 for (int j = 0; j < startres - lastres; j++)
1122 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1123 .getCharAt(fixedRight - j)))
1138 if (sg.getSize() == av.getAlignment().getHeight())
1140 if ((av.hasHiddenColumns() && startres < av
1141 .getColumnSelection().getHiddenBoundaryRight(startres)))
1147 int alWidth = av.getAlignment().getWidth();
1148 if (av.hasHiddenRows())
1150 int hwidth = av.getAlignment().getHiddenSequences()
1152 if (hwidth > alWidth)
1157 // We can still insert gaps if the selectionGroup
1158 // contains all the sequences
1159 sg.setEndRes(sg.getEndRes() + startres - lastres);
1160 fixedRight = alWidth + startres - lastres;
1171 else if (!insertGap)
1173 // / Are we able to delete?
1174 // ie are all columns blank?
1176 for (g = 0; g < groupSize; g++)
1178 for (int j = startres; j < lastres; j++)
1180 if (groupSeqs[g].getLength() <= j)
1185 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1187 // Not a gap, block edit not valid
1197 // dragging to the right
1198 if (fixedColumns && fixedRight != -1)
1200 for (int j = lastres; j < startres; j++)
1202 insertChar(j, groupSeqs, fixedRight);
1207 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1213 // dragging to the left
1214 if (fixedColumns && fixedRight != -1)
1216 for (int j = lastres; j > startres; j--)
1218 deleteChar(startres, groupSeqs, fixedRight);
1223 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1230 // ///Editing a single sequence///////////
1234 // dragging to the right
1235 if (fixedColumns && fixedRight != -1)
1237 for (int j = lastres; j < startres; j++)
1239 insertChar(j, new SequenceI[]
1240 { seq }, fixedRight);
1245 appendEdit(Action.INSERT_GAP, new SequenceI[]
1246 { seq }, lastres, startres - lastres);
1253 // dragging to the left
1254 if (fixedColumns && fixedRight != -1)
1256 for (int j = lastres; j > startres; j--)
1258 if (!Comparison.isGap(seq.getCharAt(startres)))
1263 deleteChar(startres, new SequenceI[]
1264 { seq }, fixedRight);
1269 // could be a keyboard edit trying to delete none gaps
1271 for (int m = startres; m < lastres; m++)
1273 if (!Comparison.isGap(seq.getCharAt(m)))
1282 appendEdit(Action.DELETE_GAP, new SequenceI[]
1283 { seq }, startres, max);
1288 {// insertGap==false AND editSeq==TRUE;
1289 if (fixedColumns && fixedRight != -1)
1291 for (int j = lastres; j < startres; j++)
1293 insertChar(j, new SequenceI[]
1294 { seq }, fixedRight);
1299 appendEdit(Action.INSERT_NUC, new SequenceI[]
1300 { seq }, lastres, startres - lastres);
1307 seqCanvas.repaint();
1310 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1312 int blankColumn = fixedColumn;
1313 for (int s = 0; s < seq.length; s++)
1315 // Find the next gap before the end of the visible region boundary
1316 // If lastCol > j, theres a boundary after the gap insertion
1318 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1320 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1322 // Theres a space, so break and insert the gap
1327 if (blankColumn <= j)
1329 blankColumn = fixedColumn;
1335 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1337 appendEdit(Action.INSERT_GAP, seq, j, 1);
1342 * Helper method to add and perform one edit action.
1349 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1353 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1354 av.getAlignment().getGapCharacter());
1356 editCommand.appendEdit(edit, av.getAlignment(),
1360 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1363 appendEdit(Action.DELETE_GAP, seq, j, 1);
1365 appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1);
1375 public void mouseEntered(MouseEvent e)
1382 if (scrollThread != null)
1384 scrollThread.running = false;
1385 scrollThread = null;
1396 public void mouseExited(MouseEvent e)
1398 if (av.getWrapAlignment())
1405 scrollThread = new ScrollThread();
1410 public void mouseClicked(MouseEvent evt)
1412 SequenceGroup sg = null;
1413 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1414 if (evt.getClickCount() > 1)
1416 sg = av.getSelectionGroup();
1417 if (sg != null && sg.getSize() == 1
1418 && sg.getEndRes() - sg.getStartRes() < 2)
1420 av.setSelectionGroup(null);
1423 SequenceFeature[] features = findFeaturesAtRes(
1424 sequence.getDatasetSequence(),
1425 sequence.findPosition(findRes(evt)));
1427 if (features != null && features.length > 0)
1429 SearchResults highlight = new SearchResults();
1430 highlight.addResult(sequence, features[0].getBegin(),
1431 features[0].getEnd());
1432 seqCanvas.highlightSearchResults(highlight);
1434 if (features != null && features.length > 0)
1436 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1437 { sequence }, features, false, ap);
1439 seqCanvas.highlightSearchResults(null);
1445 public void mouseWheelMoved(MouseWheelEvent e)
1448 if (e.getWheelRotation() > 0)
1450 if (e.isShiftDown())
1452 ap.scrollRight(true);
1462 if (e.isShiftDown())
1464 ap.scrollRight(false);
1471 // TODO Update tooltip for new position.
1480 public void doMousePressedDefineMode(MouseEvent evt)
1482 int res = findRes(evt);
1483 int seq = findSeq(evt);
1486 startWrapBlock = wrappedBlock;
1488 if (av.wrapAlignment && seq > av.getAlignment().getHeight())
1490 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1491 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1492 MessageManager.getString("label.wrapped_view_no_edit"),
1493 JOptionPane.WARNING_MESSAGE);
1497 if (seq < 0 || res < 0)
1502 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1504 if ((sequence == null) || (res > sequence.getLength()))
1509 stretchGroup = av.getSelectionGroup();
1511 if (stretchGroup == null)
1513 stretchGroup = av.getAlignment().findGroup(sequence);
1515 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1516 && (res < stretchGroup.getEndRes()))
1518 av.setSelectionGroup(stretchGroup);
1522 stretchGroup = null;
1525 else if (!stretchGroup.getSequences(null).contains(sequence)
1526 || (stretchGroup.getStartRes() > res)
1527 || (stretchGroup.getEndRes() < res))
1529 stretchGroup = null;
1531 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1533 if (allGroups != null)
1535 for (int i = 0; i < allGroups.length; i++)
1537 if ((allGroups[i].getStartRes() <= res)
1538 && (allGroups[i].getEndRes() >= res))
1540 stretchGroup = allGroups[i];
1546 av.setSelectionGroup(stretchGroup);
1550 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1552 SequenceFeature[] allFeatures = findFeaturesAtRes(
1553 sequence.getDatasetSequence(), sequence.findPosition(res));
1554 Vector links = new Vector();
1555 for (int i = 0; i < allFeatures.length; i++)
1557 if (allFeatures[i].links != null)
1559 for (int j = 0; j < allFeatures[i].links.size(); j++)
1561 links.addElement(allFeatures[i].links.elementAt(j));
1566 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1567 pop.show(this, evt.getX(), evt.getY());
1573 seqCanvas.cursorX = findRes(evt);
1574 seqCanvas.cursorY = findSeq(evt);
1575 seqCanvas.repaint();
1579 if (stretchGroup == null)
1581 // Only if left mouse button do we want to change group sizes
1583 // define a new group here
1584 SequenceGroup sg = new SequenceGroup();
1585 sg.setStartRes(res);
1587 sg.addSequence(sequence, false);
1588 av.setSelectionGroup(sg);
1592 if (av.getConservationSelected())
1594 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1598 if (av.getAbovePIDThreshold())
1600 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1603 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1605 // Edit end res position of selected group
1606 changeEndRes = true;
1608 else if ((stretchGroup != null)
1609 && (stretchGroup.getStartRes() == res))
1611 // Edit end res position of selected group
1612 changeStartRes = true;
1614 stretchGroup.getWidth();
1617 seqCanvas.repaint();
1626 public void doMouseReleasedDefineMode(MouseEvent evt)
1628 if (stretchGroup == null)
1633 stretchGroup.recalcConservation(); // always do this - annotation has own
1635 if (stretchGroup.cs != null)
1637 stretchGroup.cs.alignmentChanged(stretchGroup,
1638 av.getHiddenRepSequences());
1640 if (stretchGroup.cs.conservationApplied())
1642 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1643 stretchGroup.getName());
1647 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1648 stretchGroup.getName());
1651 PaintRefresher.Refresh(this, av.getSequenceSetId());
1652 ap.paintAlignment(true);
1654 changeEndRes = false;
1655 changeStartRes = false;
1656 stretchGroup = null;
1666 public void doMouseDraggedDefineMode(MouseEvent evt)
1668 int res = findRes(evt);
1669 int y = findSeq(evt);
1671 if (wrappedBlock != startWrapBlock)
1676 if (stretchGroup == null)
1681 if (res >= av.getAlignment().getWidth())
1683 res = av.getAlignment().getWidth() - 1;
1686 if (stretchGroup.getEndRes() == res)
1688 // Edit end res position of selected group
1689 changeEndRes = true;
1691 else if (stretchGroup.getStartRes() == res)
1693 // Edit start res position of selected group
1694 changeStartRes = true;
1697 if (res < av.getStartRes())
1699 res = av.getStartRes();
1704 if (res > (stretchGroup.getStartRes() - 1))
1706 stretchGroup.setEndRes(res);
1709 else if (changeStartRes)
1711 if (res < (stretchGroup.getEndRes() + 1))
1713 stretchGroup.setStartRes(res);
1717 int dragDirection = 0;
1723 else if (y < oldSeq)
1728 while ((y != oldSeq) && (oldSeq > -1)
1729 && (y < av.getAlignment().getHeight()))
1731 // This routine ensures we don't skip any sequences, as the
1732 // selection is quite slow.
1733 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1735 oldSeq += dragDirection;
1742 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1744 if (stretchGroup.getSequences(null).contains(nextSeq))
1746 stretchGroup.deleteSequence(seq, false);
1752 stretchGroup.addSequence(seq, false);
1755 stretchGroup.addSequence(nextSeq, false);
1764 mouseDragging = true;
1766 if (scrollThread != null)
1768 scrollThread.setEvent(evt);
1771 seqCanvas.repaint();
1774 void scrollCanvas(MouseEvent evt)
1778 if (scrollThread != null)
1780 scrollThread.running = false;
1781 scrollThread = null;
1783 mouseDragging = false;
1787 if (scrollThread == null)
1789 scrollThread = new ScrollThread();
1792 mouseDragging = true;
1793 scrollThread.setEvent(evt);
1798 // this class allows scrolling off the bottom of the visible alignment
1799 class ScrollThread extends Thread
1803 boolean running = false;
1805 public ScrollThread()
1810 public void setEvent(MouseEvent e)
1815 public void stopScrolling()
1829 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1831 running = ap.scrollUp(true);
1834 if (mouseDragging && (evt.getY() >= getHeight())
1835 && (av.getAlignment().getHeight() > av.getEndSeq()))
1837 running = ap.scrollUp(false);
1840 if (mouseDragging && (evt.getX() < 0))
1842 running = ap.scrollRight(false);
1844 else if (mouseDragging && (evt.getX() >= getWidth()))
1846 running = ap.scrollRight(true);
1853 } catch (Exception ex)
1861 * modify current selection according to a received message.
1864 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1865 SelectionSource source)
1867 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1868 // handles selection messages...
1869 // TODO: extend config options to allow user to control if selections may be
1870 // shared between viewports.
1872 || !av.followSelection
1873 || (av.isSelectionGroupChanged(false) || av
1874 .isColSelChanged(false))
1875 || (source instanceof AlignViewport && ((AlignViewport) source)
1876 .getSequenceSetId().equals(av.getSequenceSetId())))
1880 // do we want to thread this ? (contention with seqsel and colsel locks, I
1882 // rules are: colsel is copied if there is a real intersection between
1883 // sequence selection
1884 boolean repaint = false, copycolsel = true;
1885 // if (!av.isSelectionGroupChanged(false))
1887 SequenceGroup sgroup = null;
1888 if (seqsel != null && seqsel.getSize() > 0)
1890 if (av.getAlignment() == null)
1892 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1893 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1894 + " 's alignment is NULL! returning immediatly.");
1897 sgroup = seqsel.intersect(av.getAlignment(),
1898 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1899 if ((sgroup == null || sgroup.getSize() == 0)
1900 || (colsel == null || colsel.size() == 0))
1902 // don't copy columns if the region didn't intersect.
1906 if (sgroup != null && sgroup.getSize() > 0)
1908 av.setSelectionGroup(sgroup);
1912 av.setSelectionGroup(null);
1914 av.isSelectionGroupChanged(true);
1919 // the current selection is unset or from a previous message
1920 // so import the new colsel.
1921 if (colsel == null || colsel.size() == 0)
1923 if (av.getColumnSelection() != null)
1925 av.getColumnSelection().clear();
1931 // TODO: shift colSel according to the intersecting sequences
1932 if (av.getColumnSelection() == null)
1934 av.setColumnSelection(new ColumnSelection(colsel));
1938 av.getColumnSelection().setElementsFrom(colsel);
1941 av.isColSelChanged(true);
1945 && av.hasHiddenColumns()
1946 && (av.getColumnSelection() == null || av.getColumnSelection()
1947 .getHiddenColumns() == null))
1949 System.err.println("Bad things");
1953 // probably finessing with multiple redraws here
1954 PaintRefresher.Refresh(this, av.getSequenceSetId());
1955 // ap.paintAlignment(false);