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.api.AlignViewportI;
24 import jalview.commands.EditCommand;
25 import jalview.commands.EditCommand.Action;
26 import jalview.commands.EditCommand.Edit;
27 import jalview.datamodel.ColumnSelection;
28 import jalview.datamodel.SearchResults;
29 import jalview.datamodel.Sequence;
30 import jalview.datamodel.SequenceFeature;
31 import jalview.datamodel.SequenceGroup;
32 import jalview.datamodel.SequenceI;
33 import jalview.io.SequenceAnnotationReport;
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.Comparison;
41 import jalview.util.MappingUtils;
42 import jalview.util.MessageManager;
43 import jalview.viewmodel.AlignmentViewport;
45 import java.awt.BorderLayout;
46 import java.awt.Color;
48 import java.awt.FontMetrics;
49 import java.awt.Point;
50 import java.awt.event.MouseEvent;
51 import java.awt.event.MouseListener;
52 import java.awt.event.MouseMotionListener;
53 import java.awt.event.MouseWheelEvent;
54 import java.awt.event.MouseWheelListener;
55 import java.util.List;
56 import java.util.Vector;
58 import javax.swing.JOptionPane;
59 import javax.swing.JPanel;
60 import javax.swing.ToolTipManager;
66 * @version $Revision: 1.130 $
68 public class SeqPanel extends JPanel implements MouseListener,
69 MouseMotionListener, MouseWheelListener, SequenceListener,
74 public SeqCanvas seqCanvas;
77 public AlignmentPanel ap;
79 protected int lastres;
81 protected int startseq;
83 protected AlignViewport av;
85 ScrollThread scrollThread = null;
87 boolean mouseDragging = false;
89 boolean editingSeqs = false;
91 boolean groupEditing = false;
93 // ////////////////////////////////////////
94 // ///Everything below this is for defining the boundary of the rubberband
95 // ////////////////////////////////////////
98 boolean changeEndSeq = false;
100 boolean changeStartSeq = false;
102 boolean changeEndRes = false;
104 boolean changeStartRes = false;
106 SequenceGroup stretchGroup = null;
108 boolean remove = false;
110 Point lastMousePress;
112 boolean mouseWheelPressed = false;
114 StringBuffer keyboardNo1;
116 StringBuffer keyboardNo2;
118 java.net.URL linkImageURL;
120 private final SequenceAnnotationReport seqARep;
122 StringBuffer tooltipText = new StringBuffer();
126 EditCommand editCommand;
128 StructureSelectionManager ssm;
131 * Creates a new SeqPanel object.
138 public SeqPanel(AlignViewport av, AlignmentPanel ap)
140 linkImageURL = getClass().getResource("/images/link.gif");
141 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
142 ToolTipManager.sharedInstance().registerComponent(this);
143 ToolTipManager.sharedInstance().setInitialDelay(0);
144 ToolTipManager.sharedInstance().setDismissDelay(10000);
146 setBackground(Color.white);
148 seqCanvas = new SeqCanvas(ap);
149 setLayout(new BorderLayout());
150 add(seqCanvas, BorderLayout.CENTER);
156 addMouseMotionListener(this);
157 addMouseListener(this);
158 addMouseWheelListener(this);
159 ssm = av.getStructureSelectionManager();
160 ssm.addStructureViewerListener(this);
161 ssm.addSelectionListener(this);
165 int startWrapBlock = -1;
167 int wrappedBlock = -1;
169 int findRes(MouseEvent evt)
174 if (av.wrapAlignment)
177 int hgap = av.charHeight;
178 if (av.scaleAboveWrapped)
180 hgap += av.charHeight;
183 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
184 + seqCanvas.getAnnotationHeight();
188 x -= seqCanvas.LABEL_WEST;
190 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
196 wrappedBlock = y / cHeight;
197 wrappedBlock += av.getStartRes() / cwidth;
199 res = wrappedBlock * cwidth + x / av.getCharWidth();
204 if (x > seqCanvas.getWidth() + seqCanvas.getWidth())
206 // make sure we calculate relative to visible alignment, rather than
208 x = seqCanvas.getX() + seqCanvas.getWidth();
210 res = (x / av.getCharWidth()) + av.getStartRes();
213 if (av.hasHiddenColumns())
215 res = av.getColumnSelection().adjustForHiddenColumns(res);
222 int findSeq(MouseEvent evt)
227 if (av.wrapAlignment)
229 int hgap = av.charHeight;
230 if (av.scaleAboveWrapped)
232 hgap += av.charHeight;
235 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
236 + seqCanvas.getAnnotationHeight();
240 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
245 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
246 .getAlignment().getHeight() - 1);
252 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
254 Vector tmp = new Vector();
255 SequenceFeature[] features = sequence.getSequenceFeatures();
256 if (features != null)
258 for (int i = 0; i < features.length; i++)
260 if (av.featuresDisplayed == null
261 || !av.featuresDisplayed.containsKey(features[i].getType()))
266 if (features[i].featureGroup != null
267 && seqCanvas.fr.featureGroups != null
268 && seqCanvas.fr.featureGroups
269 .containsKey(features[i].featureGroup)
270 && !((Boolean) seqCanvas.fr.featureGroups
271 .get(features[i].featureGroup)).booleanValue())
276 if ((features[i].getBegin() <= res)
277 && (features[i].getEnd() >= res))
279 tmp.addElement(features[i]);
284 features = new SequenceFeature[tmp.size()];
285 tmp.copyInto(features);
291 * When all of a sequence of edits are complete, put the resulting edit list
292 * on the history stack (undo list), and reset flags for editing in progress.
298 if (editCommand != null && editCommand.getSize() > 0)
300 ap.alignFrame.addHistoryItem(editCommand);
301 av.firePropertyChange("alignment", null, av.getAlignment()
307 * Tidy up come what may...
312 groupEditing = false;
321 seqCanvas.cursorY = getKeyboardNo1() - 1;
325 void setCursorColumn()
327 seqCanvas.cursorX = getKeyboardNo1() - 1;
331 void setCursorRowAndColumn()
333 if (keyboardNo2 == null)
335 keyboardNo2 = new StringBuffer();
339 seqCanvas.cursorX = getKeyboardNo1() - 1;
340 seqCanvas.cursorY = getKeyboardNo2() - 1;
345 void setCursorPosition()
347 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
349 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
353 void moveCursor(int dx, int dy)
355 seqCanvas.cursorX += dx;
356 seqCanvas.cursorY += dy;
357 if (av.hasHiddenColumns()
358 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
360 int original = seqCanvas.cursorX - dx;
361 int maxWidth = av.getAlignment().getWidth();
363 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
364 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
366 seqCanvas.cursorX += dx;
369 if (seqCanvas.cursorX >= maxWidth
370 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
372 seqCanvas.cursorX = original;
379 void scrollToVisible()
381 if (seqCanvas.cursorX < 0)
383 seqCanvas.cursorX = 0;
385 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
387 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
390 if (seqCanvas.cursorY < 0)
392 seqCanvas.cursorY = 0;
394 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
396 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
400 if (av.wrapAlignment)
402 ap.scrollToWrappedVisible(seqCanvas.cursorX);
406 while (seqCanvas.cursorY < av.startSeq)
410 while (seqCanvas.cursorY + 1 > av.endSeq)
414 if (!av.wrapAlignment)
416 while (seqCanvas.cursorX < av.getColumnSelection()
417 .adjustForHiddenColumns(av.startRes))
419 if (!ap.scrollRight(false))
424 while (seqCanvas.cursorX > av.getColumnSelection()
425 .adjustForHiddenColumns(av.endRes))
427 if (!ap.scrollRight(true))
434 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
435 seqCanvas.cursorX, seqCanvas.cursorY);
440 void setSelectionAreaAtCursor(boolean topLeft)
442 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
444 if (av.getSelectionGroup() != null)
446 SequenceGroup sg = av.getSelectionGroup();
447 // Find the top and bottom of this group
448 int min = av.getAlignment().getHeight(), max = 0;
449 for (int i = 0; i < sg.getSize(); i++)
451 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
466 sg.setStartRes(seqCanvas.cursorX);
467 if (sg.getEndRes() < seqCanvas.cursorX)
469 sg.setEndRes(seqCanvas.cursorX);
472 min = seqCanvas.cursorY;
476 sg.setEndRes(seqCanvas.cursorX);
477 if (sg.getStartRes() > seqCanvas.cursorX)
479 sg.setStartRes(seqCanvas.cursorX);
482 max = seqCanvas.cursorY + 1;
487 // Only the user can do this
488 av.setSelectionGroup(null);
492 // Now add any sequences between min and max
493 sg.getSequences(null).clear();
494 for (int i = min; i < max; i++)
496 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
501 if (av.getSelectionGroup() == null)
503 SequenceGroup sg = new SequenceGroup();
504 sg.setStartRes(seqCanvas.cursorX);
505 sg.setEndRes(seqCanvas.cursorX);
506 sg.addSequence(sequence, false);
507 av.setSelectionGroup(sg);
510 ap.paintAlignment(false);
514 void insertGapAtCursor(boolean group)
516 groupEditing = group;
517 startseq = seqCanvas.cursorY;
518 lastres = seqCanvas.cursorX;
519 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
523 void deleteGapAtCursor(boolean group)
525 groupEditing = group;
526 startseq = seqCanvas.cursorY;
527 lastres = seqCanvas.cursorX + getKeyboardNo1();
528 editSequence(false, false, seqCanvas.cursorX);
532 void insertNucAtCursor(boolean group, String nuc)
534 groupEditing = group;
535 startseq = seqCanvas.cursorY;
536 lastres = seqCanvas.cursorX;
537 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
541 void numberPressed(char value)
543 if (keyboardNo1 == null)
545 keyboardNo1 = new StringBuffer();
548 if (keyboardNo2 != null)
550 keyboardNo2.append(value);
554 keyboardNo1.append(value);
562 if (keyboardNo1 != null)
564 int value = Integer.parseInt(keyboardNo1.toString());
568 } catch (Exception x)
579 if (keyboardNo2 != null)
581 int value = Integer.parseInt(keyboardNo2.toString());
585 } catch (Exception x)
599 public void mouseReleased(MouseEvent evt)
601 mouseDragging = false;
602 mouseWheelPressed = false;
606 doMouseReleasedDefineMode(evt);
620 public void mousePressed(MouseEvent evt)
622 lastMousePress = evt.getPoint();
624 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
626 mouseWheelPressed = true;
630 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
632 if (evt.isAltDown() || evt.isControlDown())
640 doMousePressedDefineMode(evt);
644 int seq = findSeq(evt);
645 int res = findRes(evt);
647 if (seq < 0 || res < 0)
652 if ((seq < av.getAlignment().getHeight())
653 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
669 public void mouseOverSequence(SequenceI sequence, int index, int pos)
671 String tmp = sequence.hashCode() + " " + index + " " + pos;
673 if (lastMessage == null || !lastMessage.equals(tmp))
675 // System.err.println("mouseOver Sequence: "+tmp);
676 ssm.mouseOverSequence(sequence, index, pos, av);
682 public void highlightSequence(SearchResults results)
684 if (av.followHighlight)
686 if (ap.scrollToPosition(results, false))
688 seqCanvas.revalidate();
691 seqCanvas.highlightSearchResults(results);
695 public VamsasSource getVamsasSource()
697 return this.ap == null ? null : this.ap.av;
699 public void updateColours(SequenceI seq, int index)
701 System.out.println("update the seqPanel colours");
712 public void mouseMoved(MouseEvent evt)
716 // This is because MacOSX creates a mouseMoved
717 // If control is down, other platforms will not.
721 int res = findRes(evt);
722 int seq = findSeq(evt);
724 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
729 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
731 if (res >= sequence.getLength())
736 pos = setStatusMessage(sequence, res, seq);
737 if (ssm != null && pos > -1)
739 mouseOverSequence(sequence, res, pos);
742 tooltipText.setLength(6); // Cuts the buffer back to <html>
744 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
747 for (int g = 0; g < groups.length; g++)
749 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
751 if (!groups[g].getName().startsWith("JTreeGroup")
752 && !groups[g].getName().startsWith("JGroup"))
754 tooltipText.append(groups[g].getName());
757 if (groups[g].getDescription() != null)
759 tooltipText.append(": " + groups[g].getDescription());
765 // use aa to see if the mouse pointer is on a
766 if (av.showSequenceFeatures)
769 SequenceFeature[] features = findFeaturesAtRes(
770 sequence.getDatasetSequence(),
771 rpos = sequence.findPosition(res));
772 seqARep.appendFeatures(tooltipText, rpos, features,
773 this.ap.seqPanel.seqCanvas.fr.minmax);
775 if (tooltipText.length() == 6) // <html></html>
777 setToolTipText(null);
782 if (lastTooltip == null
783 || !lastTooltip.equals(tooltipText.toString()))
785 String formatedTooltipText = JvSwingUtils.wrapTooltip(true,
786 tooltipText.toString());
787 // String formatedTooltipText = tooltipText.toString();
788 setToolTipText(formatedTooltipText);
789 lastTooltip = tooltipText.toString();
796 private Point lastp = null;
801 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
803 public Point getToolTipLocation(MouseEvent event)
805 int x = event.getX(), w = getWidth();
806 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
809 if (!event.isShiftDown() || p == null)
811 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
812 event.getX() + wdth, event.getY() - 20) : null;
815 * TODO: try to modify position region is not obcured by tooltip
823 * Set status message in alignment panel
826 * aligned sequence object
830 * index of sequence in alignment
831 * @return position of res in sequence
833 int setStatusMessage(SequenceI sequence, int res, int seq)
836 StringBuilder text = new StringBuilder(32);
837 text.append("Sequence " + (seq + 1) + " ID: " + sequence.getName());
839 String residue = null;
841 * Try to translate the display character to residue name (null for gap).
843 final String displayChar = String.valueOf(sequence.getCharAt(res));
844 if (av.getAlignment().isNucleotide())
846 residue = ResidueProperties.nucleotideName.get(displayChar);
849 text.append(" Nucleotide: ").append(residue);
854 residue = "X".equalsIgnoreCase(displayChar) ? "STOP"
855 : ResidueProperties.aa2Triplet.get(displayChar);
858 text.append(" Residue: ").append(residue);
864 pos = sequence.findPosition(res);
865 text.append(" (").append(Integer.toString(pos)).append(")");
867 ap.alignFrame.statusBar.setText(text.toString());
878 public void mouseDragged(MouseEvent evt)
880 if (mouseWheelPressed)
882 int oldWidth = av.charWidth;
884 // Which is bigger, left-right or up-down?
885 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
886 .getX() - lastMousePress.getX()))
888 int fontSize = av.font.getSize();
890 if (evt.getY() < lastMousePress.getY())
894 else if (evt.getY() > lastMousePress.getY())
904 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
905 av.charWidth = oldWidth;
910 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
914 else if (evt.getX() > lastMousePress.getX())
919 ap.paintAlignment(false);
922 FontMetrics fm = getFontMetrics(av.getFont());
923 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
925 lastMousePress = evt.getPoint();
932 doMouseDraggedDefineMode(evt);
936 int res = findRes(evt);
943 if ((lastres == -1) || (lastres == res))
948 if ((res < av.getAlignment().getWidth()) && (res < lastres))
950 // dragLeft, delete gap
951 editSequence(false, false, res);
955 editSequence(true, false, res);
958 mouseDragging = true;
959 if (scrollThread != null)
961 scrollThread.setEvent(evt);
965 // TODO: Make it more clever than many booleans
966 synchronized void editSequence(boolean insertGap, boolean editSeq,
971 boolean fixedColumns = false;
972 SequenceGroup sg = av.getSelectionGroup();
974 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
976 // No group, but the sequence may represent a group
977 if (!groupEditing && av.hasHiddenRows())
979 if (av.isHiddenRepSequence(seq))
981 sg = av.getRepresentedSequences(seq);
986 StringBuilder message = new StringBuilder(64);
989 message.append("Edit group:");
990 if (editCommand == null)
992 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
997 message.append("Edit sequence: " + seq.getName());
998 String label = seq.getName();
999 if (label.length() > 10)
1001 label = label.substring(0, 10);
1003 if (editCommand == null)
1005 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
1011 message.append(" insert ");
1015 message.append(" delete ");
1018 message.append(Math.abs(startres - lastres) + " gaps.");
1019 ap.alignFrame.statusBar.setText(message.toString());
1021 // Are we editing within a selection group?
1023 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1026 fixedColumns = true;
1028 // sg might be null as the user may only see 1 sequence,
1029 // but the sequence represents a group
1032 if (!av.isHiddenRepSequence(seq))
1037 sg = av.getRepresentedSequences(seq);
1040 fixedLeft = sg.getStartRes();
1041 fixedRight = sg.getEndRes();
1043 if ((startres < fixedLeft && lastres >= fixedLeft)
1044 || (startres >= fixedLeft && lastres < fixedLeft)
1045 || (startres > fixedRight && lastres <= fixedRight)
1046 || (startres <= fixedRight && lastres > fixedRight))
1052 if (fixedLeft > startres)
1054 fixedRight = fixedLeft - 1;
1057 else if (fixedRight < startres)
1059 fixedLeft = fixedRight;
1064 if (av.hasHiddenColumns())
1066 fixedColumns = true;
1067 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1068 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1070 if ((insertGap && startres > y1 && lastres < y1)
1071 || (!insertGap && startres < y2 && lastres > y2))
1077 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1078 // Selection spans a hidden region
1079 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1087 fixedRight = y2 - 1;
1094 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1095 int g, groupSize = vseqs.size();
1096 SequenceI[] groupSeqs = new SequenceI[groupSize];
1097 for (g = 0; g < groupSeqs.length; g++)
1099 groupSeqs[g] = vseqs.get(g);
1105 // If the user has selected the whole sequence, and is dragging to
1106 // the right, we can still extend the alignment and selectionGroup
1107 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1108 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1110 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1111 fixedRight = sg.getEndRes();
1114 // Is it valid with fixed columns??
1115 // Find the next gap before the end
1116 // of the visible region boundary
1117 boolean blank = false;
1118 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1122 for (g = 0; g < groupSize; g++)
1124 for (int j = 0; j < startres - lastres; j++)
1126 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1127 .getCharAt(fixedRight - j)))
1142 if (sg.getSize() == av.getAlignment().getHeight())
1144 if ((av.hasHiddenColumns() && startres < av
1145 .getColumnSelection().getHiddenBoundaryRight(startres)))
1151 int alWidth = av.getAlignment().getWidth();
1152 if (av.hasHiddenRows())
1154 int hwidth = av.getAlignment().getHiddenSequences()
1156 if (hwidth > alWidth)
1161 // We can still insert gaps if the selectionGroup
1162 // contains all the sequences
1163 sg.setEndRes(sg.getEndRes() + startres - lastres);
1164 fixedRight = alWidth + startres - lastres;
1175 else if (!insertGap)
1177 // / Are we able to delete?
1178 // ie are all columns blank?
1180 for (g = 0; g < groupSize; g++)
1182 for (int j = startres; j < lastres; j++)
1184 if (groupSeqs[g].getLength() <= j)
1189 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1191 // Not a gap, block edit not valid
1201 // dragging to the right
1202 if (fixedColumns && fixedRight != -1)
1204 for (int j = lastres; j < startres; j++)
1206 insertChar(j, groupSeqs, fixedRight);
1211 appendEdit(Action.INSERT_GAP, groupSeqs, startres, startres
1217 // dragging to the left
1218 if (fixedColumns && fixedRight != -1)
1220 for (int j = lastres; j > startres; j--)
1222 deleteChar(startres, groupSeqs, fixedRight);
1227 appendEdit(Action.DELETE_GAP, groupSeqs, startres, lastres
1234 // ///Editing a single sequence///////////
1238 // dragging to the right
1239 if (fixedColumns && fixedRight != -1)
1241 for (int j = lastres; j < startres; j++)
1243 insertChar(j, new SequenceI[]
1244 { seq }, fixedRight);
1249 appendEdit(Action.INSERT_GAP, new SequenceI[]
1250 { seq }, lastres, startres - lastres);
1257 // dragging to the left
1258 if (fixedColumns && fixedRight != -1)
1260 for (int j = lastres; j > startres; j--)
1262 if (!Comparison.isGap(seq.getCharAt(startres)))
1267 deleteChar(startres, new SequenceI[]
1268 { seq }, fixedRight);
1273 // could be a keyboard edit trying to delete none gaps
1275 for (int m = startres; m < lastres; m++)
1277 if (!Comparison.isGap(seq.getCharAt(m)))
1286 appendEdit(Action.DELETE_GAP, new SequenceI[]
1287 { seq }, startres, max);
1292 {// insertGap==false AND editSeq==TRUE;
1293 if (fixedColumns && fixedRight != -1)
1295 for (int j = lastres; j < startres; j++)
1297 insertChar(j, new SequenceI[]
1298 { seq }, fixedRight);
1303 appendEdit(Action.INSERT_NUC, new SequenceI[]
1304 { seq }, lastres, startres - lastres);
1311 seqCanvas.repaint();
1314 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1316 int blankColumn = fixedColumn;
1317 for (int s = 0; s < seq.length; s++)
1319 // Find the next gap before the end of the visible region boundary
1320 // If lastCol > j, theres a boundary after the gap insertion
1322 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1324 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1326 // Theres a space, so break and insert the gap
1331 if (blankColumn <= j)
1333 blankColumn = fixedColumn;
1339 appendEdit(Action.DELETE_GAP, seq, blankColumn, 1);
1341 appendEdit(Action.INSERT_GAP, seq, j, 1);
1346 * Helper method to add and perform one edit action.
1353 protected void appendEdit(Action action, SequenceI[] seq, int pos,
1357 final Edit edit = new EditCommand().new Edit(action, seq, pos, count,
1358 av.getAlignment().getGapCharacter());
1360 editCommand.appendEdit(edit, av.getAlignment(),
1364 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1367 appendEdit(Action.DELETE_GAP, seq, j, 1);
1369 appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1);
1379 public void mouseEntered(MouseEvent e)
1386 if (scrollThread != null)
1388 scrollThread.running = false;
1389 scrollThread = null;
1400 public void mouseExited(MouseEvent e)
1402 if (av.getWrapAlignment())
1409 scrollThread = new ScrollThread();
1414 public void mouseClicked(MouseEvent evt)
1416 SequenceGroup sg = null;
1417 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1418 if (evt.getClickCount() > 1)
1420 sg = av.getSelectionGroup();
1421 if (sg != null && sg.getSize() == 1
1422 && sg.getEndRes() - sg.getStartRes() < 2)
1424 av.setSelectionGroup(null);
1427 SequenceFeature[] features = findFeaturesAtRes(
1428 sequence.getDatasetSequence(),
1429 sequence.findPosition(findRes(evt)));
1431 if (features != null && features.length > 0)
1433 SearchResults highlight = new SearchResults();
1434 highlight.addResult(sequence, features[0].getBegin(),
1435 features[0].getEnd());
1436 seqCanvas.highlightSearchResults(highlight);
1438 if (features != null && features.length > 0)
1440 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1441 { sequence }, features, false, ap);
1443 seqCanvas.highlightSearchResults(null);
1449 public void mouseWheelMoved(MouseWheelEvent e)
1452 if (e.getWheelRotation() > 0)
1454 if (e.isShiftDown())
1456 ap.scrollRight(true);
1466 if (e.isShiftDown())
1468 ap.scrollRight(false);
1475 // TODO Update tooltip for new position.
1484 public void doMousePressedDefineMode(MouseEvent evt)
1486 int res = findRes(evt);
1487 int seq = findSeq(evt);
1490 startWrapBlock = wrappedBlock;
1492 if (av.wrapAlignment && seq > av.getAlignment().getHeight())
1494 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1495 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1496 MessageManager.getString("label.wrapped_view_no_edit"),
1497 JOptionPane.WARNING_MESSAGE);
1501 if (seq < 0 || res < 0)
1506 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1508 if ((sequence == null) || (res > sequence.getLength()))
1513 stretchGroup = av.getSelectionGroup();
1515 if (stretchGroup == null)
1517 stretchGroup = av.getAlignment().findGroup(sequence);
1519 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1520 && (res < stretchGroup.getEndRes()))
1522 av.setSelectionGroup(stretchGroup);
1526 stretchGroup = null;
1529 else if (!stretchGroup.getSequences(null).contains(sequence)
1530 || (stretchGroup.getStartRes() > res)
1531 || (stretchGroup.getEndRes() < res))
1533 stretchGroup = null;
1535 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1537 if (allGroups != null)
1539 for (int i = 0; i < allGroups.length; i++)
1541 if ((allGroups[i].getStartRes() <= res)
1542 && (allGroups[i].getEndRes() >= res))
1544 stretchGroup = allGroups[i];
1550 av.setSelectionGroup(stretchGroup);
1554 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1556 SequenceFeature[] allFeatures = findFeaturesAtRes(
1557 sequence.getDatasetSequence(), sequence.findPosition(res));
1558 Vector links = new Vector();
1559 for (int i = 0; i < allFeatures.length; i++)
1561 if (allFeatures[i].links != null)
1563 for (int j = 0; j < allFeatures[i].links.size(); j++)
1565 links.addElement(allFeatures[i].links.elementAt(j));
1570 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1571 pop.show(this, evt.getX(), evt.getY());
1577 seqCanvas.cursorX = findRes(evt);
1578 seqCanvas.cursorY = findSeq(evt);
1579 seqCanvas.repaint();
1583 if (stretchGroup == null)
1585 // Only if left mouse button do we want to change group sizes
1587 // define a new group here
1588 SequenceGroup sg = new SequenceGroup();
1589 sg.setStartRes(res);
1591 sg.addSequence(sequence, false);
1592 av.setSelectionGroup(sg);
1596 if (av.getConservationSelected())
1598 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1602 if (av.getAbovePIDThreshold())
1604 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1607 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1609 // Edit end res position of selected group
1610 changeEndRes = true;
1612 else if ((stretchGroup != null)
1613 && (stretchGroup.getStartRes() == res))
1615 // Edit end res position of selected group
1616 changeStartRes = true;
1618 stretchGroup.getWidth();
1621 seqCanvas.repaint();
1630 public void doMouseReleasedDefineMode(MouseEvent evt)
1632 if (stretchGroup == null)
1637 stretchGroup.recalcConservation(); // always do this - annotation has own
1639 if (stretchGroup.cs != null)
1641 stretchGroup.cs.alignmentChanged(stretchGroup,
1642 av.getHiddenRepSequences());
1644 if (stretchGroup.cs.conservationApplied())
1646 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1647 stretchGroup.getName());
1651 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1652 stretchGroup.getName());
1655 PaintRefresher.Refresh(this, av.getSequenceSetId());
1656 ap.paintAlignment(true);
1658 changeEndRes = false;
1659 changeStartRes = false;
1660 stretchGroup = null;
1670 public void doMouseDraggedDefineMode(MouseEvent evt)
1672 int res = findRes(evt);
1673 int y = findSeq(evt);
1675 if (wrappedBlock != startWrapBlock)
1680 if (stretchGroup == null)
1685 if (res >= av.getAlignment().getWidth())
1687 res = av.getAlignment().getWidth() - 1;
1690 if (stretchGroup.getEndRes() == res)
1692 // Edit end res position of selected group
1693 changeEndRes = true;
1695 else if (stretchGroup.getStartRes() == res)
1697 // Edit start res position of selected group
1698 changeStartRes = true;
1701 if (res < av.getStartRes())
1703 res = av.getStartRes();
1708 if (res > (stretchGroup.getStartRes() - 1))
1710 stretchGroup.setEndRes(res);
1713 else if (changeStartRes)
1715 if (res < (stretchGroup.getEndRes() + 1))
1717 stretchGroup.setStartRes(res);
1721 int dragDirection = 0;
1727 else if (y < oldSeq)
1732 while ((y != oldSeq) && (oldSeq > -1)
1733 && (y < av.getAlignment().getHeight()))
1735 // This routine ensures we don't skip any sequences, as the
1736 // selection is quite slow.
1737 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1739 oldSeq += dragDirection;
1746 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1748 if (stretchGroup.getSequences(null).contains(nextSeq))
1750 stretchGroup.deleteSequence(seq, false);
1756 stretchGroup.addSequence(seq, false);
1759 stretchGroup.addSequence(nextSeq, false);
1768 mouseDragging = true;
1770 if (scrollThread != null)
1772 scrollThread.setEvent(evt);
1775 seqCanvas.repaint();
1778 void scrollCanvas(MouseEvent evt)
1782 if (scrollThread != null)
1784 scrollThread.running = false;
1785 scrollThread = null;
1787 mouseDragging = false;
1791 if (scrollThread == null)
1793 scrollThread = new ScrollThread();
1796 mouseDragging = true;
1797 scrollThread.setEvent(evt);
1802 // this class allows scrolling off the bottom of the visible alignment
1803 class ScrollThread extends Thread
1807 boolean running = false;
1809 public ScrollThread()
1814 public void setEvent(MouseEvent e)
1819 public void stopScrolling()
1833 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1835 running = ap.scrollUp(true);
1838 if (mouseDragging && (evt.getY() >= getHeight())
1839 && (av.getAlignment().getHeight() > av.getEndSeq()))
1841 running = ap.scrollUp(false);
1844 if (mouseDragging && (evt.getX() < 0))
1846 running = ap.scrollRight(false);
1848 else if (mouseDragging && (evt.getX() >= getWidth()))
1850 running = ap.scrollRight(true);
1857 } catch (Exception ex)
1865 * modify current selection according to a received message.
1868 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1869 SelectionSource source)
1871 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1872 // handles selection messages...
1873 // TODO: extend config options to allow user to control if selections may be
1874 // shared between viewports.
1875 boolean iSentTheSelection = (av == source
1876 || (source instanceof AlignViewport && ((AlignmentViewport) source)
1877 .getSequenceSetId().equals(av.getSequenceSetId())));
1878 if (iSentTheSelection || !av.followSelection)
1884 * Ignore the selection if there is one of our own pending.
1886 if (av.isSelectionGroupChanged(false) || av.isColSelChanged(false))
1892 * Check for selection in a view of which this one is a dna/protein
1895 if (selectionFromTranslation(seqsel, colsel, source))
1900 // do we want to thread this ? (contention with seqsel and colsel locks, I
1902 // rules are: colsel is copied if there is a real intersection between
1903 // sequence selection
1904 boolean repaint = false;
1905 boolean copycolsel = true;
1907 SequenceGroup sgroup = null;
1908 if (seqsel != null && seqsel.getSize() > 0)
1910 if (av.getAlignment() == null)
1912 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1913 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1914 + " 's alignment is NULL! returning immediately.");
1917 sgroup = seqsel.intersect(av.getAlignment(),
1918 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1919 if ((sgroup == null || sgroup.getSize() == 0)
1920 || (colsel == null || colsel.size() == 0))
1922 // don't copy columns if the region didn't intersect.
1926 if (sgroup != null && sgroup.getSize() > 0)
1928 av.setSelectionGroup(sgroup);
1932 av.setSelectionGroup(null);
1934 av.isSelectionGroupChanged(true);
1939 // the current selection is unset or from a previous message
1940 // so import the new colsel.
1941 if (colsel == null || colsel.size() == 0)
1943 if (av.getColumnSelection() != null)
1945 av.getColumnSelection().clear();
1951 // TODO: shift colSel according to the intersecting sequences
1952 if (av.getColumnSelection() == null)
1954 av.setColumnSelection(new ColumnSelection(colsel));
1958 av.getColumnSelection().setElementsFrom(colsel);
1961 av.isColSelChanged(true);
1966 && av.hasHiddenColumns()
1967 && (av.getColumnSelection() == null || av.getColumnSelection()
1968 .getHiddenColumns() == null))
1970 System.err.println("Bad things");
1972 if (repaint) // always true!
1974 // probably finessing with multiple redraws here
1975 PaintRefresher.Refresh(this, av.getSequenceSetId());
1976 // ap.paintAlignment(false);
1981 * If this panel is a cdna/protein translation view of the selection source,
1982 * tries to map the source selection to a local one, and returns true. Else
1989 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1990 ColumnSelection colsel, SelectionSource source)
1992 if (!(source instanceof AlignViewportI)) {
1995 final AlignViewportI sourceAv = (AlignViewportI) source;
1996 if (sourceAv.getCodingComplement() != av && av.getCodingComplement() != sourceAv)
2002 * Map sequence selection
2004 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
2005 av.setSelectionGroup(sg);
2006 av.isSelectionGroupChanged(true);
2009 * Map column selection
2011 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
2013 av.setColumnSelection(cs);
2014 av.isColSelChanged(true);
2016 PaintRefresher.Refresh(this, av.getSequenceSetId());