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 StringBuilder text = new StringBuilder(32);
834 text.append("Sequence " + (seq + 1) + " ID: " + sequence.getName());
836 String residue = null;
838 * Try to translate the display character to residue name (null for gap).
840 final String displayChar = String.valueOf(sequence.getCharAt(res));
841 if (av.getAlignment().isNucleotide())
843 residue = ResidueProperties.nucleotideName.get(displayChar);
846 text.append(" Nucleotide: ").append(residue);
851 residue = "X".equalsIgnoreCase(displayChar) ? "STOP"
852 : ResidueProperties.aa2Triplet.get(displayChar);
855 text.append(" Residue: ").append(residue);
861 pos = sequence.findPosition(res);
862 text.append(" (").append(Integer.toString(pos)).append(")");
864 ap.alignFrame.statusBar.setText(text.toString());
875 public void mouseDragged(MouseEvent evt)
877 if (mouseWheelPressed)
879 int oldWidth = av.charWidth;
881 // Which is bigger, left-right or up-down?
882 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
883 .getX() - lastMousePress.getX()))
885 int fontSize = av.font.getSize();
887 if (evt.getY() < lastMousePress.getY())
891 else if (evt.getY() > lastMousePress.getY())
901 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
902 av.charWidth = oldWidth;
907 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
911 else if (evt.getX() > lastMousePress.getX())
916 ap.paintAlignment(false);
919 FontMetrics fm = getFontMetrics(av.getFont());
920 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
922 lastMousePress = evt.getPoint();
929 doMouseDraggedDefineMode(evt);
933 int res = findRes(evt);
940 if ((lastres == -1) || (lastres == res))
945 if ((res < av.getAlignment().getWidth()) && (res < lastres))
947 // dragLeft, delete gap
948 editSequence(false, false, res);
952 editSequence(true, false, res);
955 mouseDragging = true;
956 if (scrollThread != null)
958 scrollThread.setEvent(evt);
962 // TODO: Make it more clever than many booleans
963 synchronized void editSequence(boolean insertGap, boolean editSeq,
968 boolean fixedColumns = false;
969 SequenceGroup sg = av.getSelectionGroup();
971 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
973 // No group, but the sequence may represent a group
974 if (!groupEditing && av.hasHiddenRows())
976 if (av.isHiddenRepSequence(seq))
978 sg = av.getRepresentedSequences(seq);
983 StringBuilder message = new StringBuilder(64);
986 message.append("Edit group:");
987 if (editCommand == null)
989 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
994 message.append("Edit sequence: " + seq.getName());
995 String label = seq.getName();
996 if (label.length() > 10)
998 label = label.substring(0, 10);
1000 if (editCommand == null)
1002 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
1008 message.append(" insert ");
1012 message.append(" delete ");
1015 message.append(Math.abs(startres - lastres) + " gaps.");
1016 ap.alignFrame.statusBar.setText(message.toString());
1018 // Are we editing within a selection group?
1020 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1023 fixedColumns = true;
1025 // sg might be null as the user may only see 1 sequence,
1026 // but the sequence represents a group
1029 if (!av.isHiddenRepSequence(seq))
1034 sg = av.getRepresentedSequences(seq);
1037 fixedLeft = sg.getStartRes();
1038 fixedRight = sg.getEndRes();
1040 if ((startres < fixedLeft && lastres >= fixedLeft)
1041 || (startres >= fixedLeft && lastres < fixedLeft)
1042 || (startres > fixedRight && lastres <= fixedRight)
1043 || (startres <= fixedRight && lastres > fixedRight))
1049 if (fixedLeft > startres)
1051 fixedRight = fixedLeft - 1;
1054 else if (fixedRight < startres)
1056 fixedLeft = fixedRight;
1061 if (av.hasHiddenColumns())
1063 fixedColumns = true;
1064 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1065 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1067 if ((insertGap && startres > y1 && lastres < y1)
1068 || (!insertGap && startres < y2 && lastres > y2))
1074 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1075 // Selection spans a hidden region
1076 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1084 fixedRight = y2 - 1;
1091 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1092 int g, groupSize = vseqs.size();
1093 SequenceI[] groupSeqs = new SequenceI[groupSize];
1094 for (g = 0; g < groupSeqs.length; g++)
1096 groupSeqs[g] = vseqs.get(g);
1102 // If the user has selected the whole sequence, and is dragging to
1103 // the right, we can still extend the alignment and selectionGroup
1104 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1105 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1107 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1108 fixedRight = sg.getEndRes();
1111 // Is it valid with fixed columns??
1112 // Find the next gap before the end
1113 // of the visible region boundary
1114 boolean blank = false;
1115 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1119 for (g = 0; g < groupSize; g++)
1121 for (int j = 0; j < startres - lastres; j++)
1123 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1124 .getCharAt(fixedRight - j)))
1139 if (sg.getSize() == av.getAlignment().getHeight())
1141 if ((av.hasHiddenColumns() && startres < av
1142 .getColumnSelection().getHiddenBoundaryRight(startres)))
1148 int alWidth = av.getAlignment().getWidth();
1149 if (av.hasHiddenRows())
1151 int hwidth = av.getAlignment().getHiddenSequences()
1153 if (hwidth > alWidth)
1158 // We can still insert gaps if the selectionGroup
1159 // contains all the sequences
1160 sg.setEndRes(sg.getEndRes() + startres - lastres);
1161 fixedRight = alWidth + startres - lastres;
1172 else if (!insertGap)
1174 // / Are we able to delete?
1175 // ie are all columns blank?
1177 for (g = 0; g < groupSize; g++)
1179 for (int j = startres; j < lastres; j++)
1181 if (groupSeqs[g].getLength() <= j)
1186 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1188 // Not a gap, block edit not valid
1198 // dragging to the right
1199 if (fixedColumns && fixedRight != -1)
1201 for (int j = lastres; j < startres; j++)
1203 insertChar(j, groupSeqs, fixedRight);
1208 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1214 // dragging to the left
1215 if (fixedColumns && fixedRight != -1)
1217 for (int j = lastres; j > startres; j--)
1219 deleteChar(startres, groupSeqs, fixedRight);
1224 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1231 // ///Editing a single sequence///////////
1235 // dragging to the right
1236 if (fixedColumns && fixedRight != -1)
1238 for (int j = lastres; j < startres; j++)
1240 insertChar(j, new SequenceI[]
1241 { seq }, fixedRight);
1246 appendEdit(Action.INSERT_GAP, new SequenceI[]
1247 { seq }, lastres, startres - lastres);
1254 // dragging to the left
1255 if (fixedColumns && fixedRight != -1)
1257 for (int j = lastres; j > startres; j--)
1259 if (!Comparison.isGap(seq.getCharAt(startres)))
1264 deleteChar(startres, new SequenceI[]
1265 { seq }, fixedRight);
1270 // could be a keyboard edit trying to delete none gaps
1272 for (int m = startres; m < lastres; m++)
1274 if (!Comparison.isGap(seq.getCharAt(m)))
1283 appendEdit(Action.DELETE_GAP, new SequenceI[]
1284 { seq }, startres, max);
1289 {// insertGap==false AND editSeq==TRUE;
1290 if (fixedColumns && fixedRight != -1)
1292 for (int j = lastres; j < startres; j++)
1294 insertChar(j, new SequenceI[]
1295 { seq }, fixedRight);
1300 appendEdit(Action.INSERT_NUC, new SequenceI[]
1301 { seq }, lastres, startres - lastres);
1308 seqCanvas.repaint();
1311 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1313 int blankColumn = fixedColumn;
1314 for (int s = 0; s < seq.length; s++)
1316 // Find the next gap before the end of the visible region boundary
1317 // If lastCol > j, theres a boundary after the gap insertion
1319 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1321 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1323 // Theres a space, so break and insert the gap
1328 if (blankColumn <= j)
1330 blankColumn = fixedColumn;
1336 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1338 appendEdit(Action.INSERT_GAP, seq, j, 1);
1343 * Helper method to add and perform one edit action.
1350 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1354 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1355 av.getAlignment().getGapCharacter());
1357 editCommand.appendEdit(edit, av.getAlignment(),
1361 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1364 appendEdit(Action.DELETE_GAP, seq, j, 1);
1366 appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1);
1376 public void mouseEntered(MouseEvent e)
1383 if (scrollThread != null)
1385 scrollThread.running = false;
1386 scrollThread = null;
1397 public void mouseExited(MouseEvent e)
1399 if (av.getWrapAlignment())
1406 scrollThread = new ScrollThread();
1411 public void mouseClicked(MouseEvent evt)
1413 SequenceGroup sg = null;
1414 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1415 if (evt.getClickCount() > 1)
1417 sg = av.getSelectionGroup();
1418 if (sg != null && sg.getSize() == 1
1419 && sg.getEndRes() - sg.getStartRes() < 2)
1421 av.setSelectionGroup(null);
1424 SequenceFeature[] features = findFeaturesAtRes(
1425 sequence.getDatasetSequence(),
1426 sequence.findPosition(findRes(evt)));
1428 if (features != null && features.length > 0)
1430 SearchResults highlight = new SearchResults();
1431 highlight.addResult(sequence, features[0].getBegin(),
1432 features[0].getEnd());
1433 seqCanvas.highlightSearchResults(highlight);
1435 if (features != null && features.length > 0)
1437 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1438 { sequence }, features, false, ap);
1440 seqCanvas.highlightSearchResults(null);
1446 public void mouseWheelMoved(MouseWheelEvent e)
1449 if (e.getWheelRotation() > 0)
1451 if (e.isShiftDown())
1453 ap.scrollRight(true);
1463 if (e.isShiftDown())
1465 ap.scrollRight(false);
1472 // TODO Update tooltip for new position.
1481 public void doMousePressedDefineMode(MouseEvent evt)
1483 int res = findRes(evt);
1484 int seq = findSeq(evt);
1487 startWrapBlock = wrappedBlock;
1489 if (av.wrapAlignment && seq > av.getAlignment().getHeight())
1491 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1492 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1493 MessageManager.getString("label.wrapped_view_no_edit"),
1494 JOptionPane.WARNING_MESSAGE);
1498 if (seq < 0 || res < 0)
1503 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1505 if ((sequence == null) || (res > sequence.getLength()))
1510 stretchGroup = av.getSelectionGroup();
1512 if (stretchGroup == null)
1514 stretchGroup = av.getAlignment().findGroup(sequence);
1516 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1517 && (res < stretchGroup.getEndRes()))
1519 av.setSelectionGroup(stretchGroup);
1523 stretchGroup = null;
1526 else if (!stretchGroup.getSequences(null).contains(sequence)
1527 || (stretchGroup.getStartRes() > res)
1528 || (stretchGroup.getEndRes() < res))
1530 stretchGroup = null;
1532 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1534 if (allGroups != null)
1536 for (int i = 0; i < allGroups.length; i++)
1538 if ((allGroups[i].getStartRes() <= res)
1539 && (allGroups[i].getEndRes() >= res))
1541 stretchGroup = allGroups[i];
1547 av.setSelectionGroup(stretchGroup);
1551 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1553 SequenceFeature[] allFeatures = findFeaturesAtRes(
1554 sequence.getDatasetSequence(), sequence.findPosition(res));
1555 Vector links = new Vector();
1556 for (int i = 0; i < allFeatures.length; i++)
1558 if (allFeatures[i].links != null)
1560 for (int j = 0; j < allFeatures[i].links.size(); j++)
1562 links.addElement(allFeatures[i].links.elementAt(j));
1567 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1568 pop.show(this, evt.getX(), evt.getY());
1574 seqCanvas.cursorX = findRes(evt);
1575 seqCanvas.cursorY = findSeq(evt);
1576 seqCanvas.repaint();
1580 if (stretchGroup == null)
1582 // Only if left mouse button do we want to change group sizes
1584 // define a new group here
1585 SequenceGroup sg = new SequenceGroup();
1586 sg.setStartRes(res);
1588 sg.addSequence(sequence, false);
1589 av.setSelectionGroup(sg);
1593 if (av.getConservationSelected())
1595 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1599 if (av.getAbovePIDThreshold())
1601 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1604 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1606 // Edit end res position of selected group
1607 changeEndRes = true;
1609 else if ((stretchGroup != null)
1610 && (stretchGroup.getStartRes() == res))
1612 // Edit end res position of selected group
1613 changeStartRes = true;
1615 stretchGroup.getWidth();
1618 seqCanvas.repaint();
1627 public void doMouseReleasedDefineMode(MouseEvent evt)
1629 if (stretchGroup == null)
1634 stretchGroup.recalcConservation(); // always do this - annotation has own
1636 if (stretchGroup.cs != null)
1638 stretchGroup.cs.alignmentChanged(stretchGroup,
1639 av.getHiddenRepSequences());
1641 if (stretchGroup.cs.conservationApplied())
1643 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1644 stretchGroup.getName());
1648 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1649 stretchGroup.getName());
1652 PaintRefresher.Refresh(this, av.getSequenceSetId());
1653 ap.paintAlignment(true);
1655 changeEndRes = false;
1656 changeStartRes = false;
1657 stretchGroup = null;
1667 public void doMouseDraggedDefineMode(MouseEvent evt)
1669 int res = findRes(evt);
1670 int y = findSeq(evt);
1672 if (wrappedBlock != startWrapBlock)
1677 if (stretchGroup == null)
1682 if (res >= av.getAlignment().getWidth())
1684 res = av.getAlignment().getWidth() - 1;
1687 if (stretchGroup.getEndRes() == res)
1689 // Edit end res position of selected group
1690 changeEndRes = true;
1692 else if (stretchGroup.getStartRes() == res)
1694 // Edit start res position of selected group
1695 changeStartRes = true;
1698 if (res < av.getStartRes())
1700 res = av.getStartRes();
1705 if (res > (stretchGroup.getStartRes() - 1))
1707 stretchGroup.setEndRes(res);
1710 else if (changeStartRes)
1712 if (res < (stretchGroup.getEndRes() + 1))
1714 stretchGroup.setStartRes(res);
1718 int dragDirection = 0;
1724 else if (y < oldSeq)
1729 while ((y != oldSeq) && (oldSeq > -1)
1730 && (y < av.getAlignment().getHeight()))
1732 // This routine ensures we don't skip any sequences, as the
1733 // selection is quite slow.
1734 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1736 oldSeq += dragDirection;
1743 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1745 if (stretchGroup.getSequences(null).contains(nextSeq))
1747 stretchGroup.deleteSequence(seq, false);
1753 stretchGroup.addSequence(seq, false);
1756 stretchGroup.addSequence(nextSeq, false);
1765 mouseDragging = true;
1767 if (scrollThread != null)
1769 scrollThread.setEvent(evt);
1772 seqCanvas.repaint();
1775 void scrollCanvas(MouseEvent evt)
1779 if (scrollThread != null)
1781 scrollThread.running = false;
1782 scrollThread = null;
1784 mouseDragging = false;
1788 if (scrollThread == null)
1790 scrollThread = new ScrollThread();
1793 mouseDragging = true;
1794 scrollThread.setEvent(evt);
1799 // this class allows scrolling off the bottom of the visible alignment
1800 class ScrollThread extends Thread
1804 boolean running = false;
1806 public ScrollThread()
1811 public void setEvent(MouseEvent e)
1816 public void stopScrolling()
1830 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1832 running = ap.scrollUp(true);
1835 if (mouseDragging && (evt.getY() >= getHeight())
1836 && (av.getAlignment().getHeight() > av.getEndSeq()))
1838 running = ap.scrollUp(false);
1841 if (mouseDragging && (evt.getX() < 0))
1843 running = ap.scrollRight(false);
1845 else if (mouseDragging && (evt.getX() >= getWidth()))
1847 running = ap.scrollRight(true);
1854 } catch (Exception ex)
1862 * modify current selection according to a received message.
1865 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1866 SelectionSource source)
1868 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1869 // handles selection messages...
1870 // TODO: extend config options to allow user to control if selections may be
1871 // shared between viewports.
1873 || !av.followSelection
1874 || (av.isSelectionGroupChanged(false) || av
1875 .isColSelChanged(false))
1876 || (source instanceof AlignViewport && ((AlignViewport) source)
1877 .getSequenceSetId().equals(av.getSequenceSetId())))
1881 // do we want to thread this ? (contention with seqsel and colsel locks, I
1883 // rules are: colsel is copied if there is a real intersection between
1884 // sequence selection
1885 boolean repaint = false, copycolsel = true;
1886 // if (!av.isSelectionGroupChanged(false))
1888 SequenceGroup sgroup = null;
1889 if (seqsel != null && seqsel.getSize() > 0)
1891 if (av.getAlignment() == null)
1893 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1894 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1895 + " 's alignment is NULL! returning immediatly.");
1898 sgroup = seqsel.intersect(av.getAlignment(),
1899 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1900 if ((sgroup == null || sgroup.getSize() == 0)
1901 || (colsel == null || colsel.size() == 0))
1903 // don't copy columns if the region didn't intersect.
1907 if (sgroup != null && sgroup.getSize() > 0)
1909 av.setSelectionGroup(sgroup);
1913 av.setSelectionGroup(null);
1915 av.isSelectionGroupChanged(true);
1920 // the current selection is unset or from a previous message
1921 // so import the new colsel.
1922 if (colsel == null || colsel.size() == 0)
1924 if (av.getColumnSelection() != null)
1926 av.getColumnSelection().clear();
1932 // TODO: shift colSel according to the intersecting sequences
1933 if (av.getColumnSelection() == null)
1935 av.setColumnSelection(new ColumnSelection(colsel));
1939 av.getColumnSelection().setElementsFrom(colsel);
1942 av.isColSelChanged(true);
1946 && av.hasHiddenColumns()
1947 && (av.getColumnSelection() == null || av.getColumnSelection()
1948 .getHiddenColumns() == null))
1950 System.err.println("Bad things");
1954 // probably finessing with multiple redraws here
1955 PaintRefresher.Refresh(this, av.getSequenceSetId());
1956 // ap.paintAlignment(false);