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.
24 import java.util.List;
27 import java.awt.event.*;
31 import jalview.commands.*;
32 import jalview.datamodel.*;
33 import jalview.io.SequenceAnnotationReport;
34 import jalview.schemes.*;
35 import jalview.structure.*;
36 import jalview.util.MessageManager;
42 * @version $Revision: 1.130 $
44 public class SeqPanel extends JPanel implements MouseListener,
45 MouseMotionListener, MouseWheelListener, SequenceListener,
50 public SeqCanvas seqCanvas;
53 public AlignmentPanel ap;
55 protected int lastres;
57 protected int startseq;
59 protected AlignViewport av;
61 ScrollThread scrollThread = null;
63 boolean mouseDragging = false;
65 boolean editingSeqs = false;
67 boolean groupEditing = false;
69 // ////////////////////////////////////////
70 // ///Everything below this is for defining the boundary of the rubberband
71 // ////////////////////////////////////////
74 boolean changeEndSeq = false;
76 boolean changeStartSeq = false;
78 boolean changeEndRes = false;
80 boolean changeStartRes = false;
82 SequenceGroup stretchGroup = null;
84 boolean remove = false;
88 boolean mouseWheelPressed = false;
90 StringBuffer keyboardNo1;
92 StringBuffer keyboardNo2;
94 java.net.URL linkImageURL;
96 private final SequenceAnnotationReport seqARep;
98 StringBuffer tooltipText = new StringBuffer("<html>");
102 EditCommand editCommand;
104 StructureSelectionManager ssm;
107 * Creates a new SeqPanel object.
114 public SeqPanel(AlignViewport av, AlignmentPanel ap)
116 linkImageURL = getClass().getResource("/images/link.gif");
117 seqARep = new SequenceAnnotationReport(linkImageURL.toString());
118 ToolTipManager.sharedInstance().registerComponent(this);
119 ToolTipManager.sharedInstance().setInitialDelay(0);
120 ToolTipManager.sharedInstance().setDismissDelay(10000);
122 setBackground(Color.white);
124 seqCanvas = new SeqCanvas(ap);
125 setLayout(new BorderLayout());
126 add(seqCanvas, BorderLayout.CENTER);
132 addMouseMotionListener(this);
133 addMouseListener(this);
134 addMouseWheelListener(this);
135 ssm = av.getStructureSelectionManager();
136 ssm.addStructureViewerListener(this);
137 ssm.addSelectionListener(this);
141 int startWrapBlock = -1;
143 int wrappedBlock = -1;
145 int findRes(MouseEvent evt)
150 if (av.wrapAlignment)
153 int hgap = av.charHeight;
154 if (av.scaleAboveWrapped)
156 hgap += av.charHeight;
159 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
160 + seqCanvas.getAnnotationHeight();
164 x -= seqCanvas.LABEL_WEST;
166 int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth());
172 wrappedBlock = y / cHeight;
173 wrappedBlock += av.getStartRes() / cwidth;
175 res = wrappedBlock * cwidth + x / av.getCharWidth();
180 if (x > seqCanvas.getWidth() + seqCanvas.getWidth())
182 // make sure we calculate relative to visible alignment, rather than
184 x = seqCanvas.getX() + seqCanvas.getWidth();
186 res = (x / av.getCharWidth()) + av.getStartRes();
189 if (av.hasHiddenColumns())
191 res = av.getColumnSelection().adjustForHiddenColumns(res);
198 int findSeq(MouseEvent evt)
203 if (av.wrapAlignment)
205 int hgap = av.charHeight;
206 if (av.scaleAboveWrapped)
208 hgap += av.charHeight;
211 int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap
212 + seqCanvas.getAnnotationHeight();
216 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
221 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
222 .getAlignment().getHeight() - 1);
228 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
230 Vector tmp = new Vector();
231 SequenceFeature[] features = sequence.getSequenceFeatures();
232 if (features != null)
234 for (int i = 0; i < features.length; i++)
236 if (av.featuresDisplayed == null
237 || !av.featuresDisplayed.containsKey(features[i].getType()))
242 if (features[i].featureGroup != null
243 && seqCanvas.fr.featureGroups != null
244 && seqCanvas.fr.featureGroups
245 .containsKey(features[i].featureGroup)
246 && !((Boolean) seqCanvas.fr.featureGroups
247 .get(features[i].featureGroup)).booleanValue())
250 if ((features[i].getBegin() <= res)
251 && (features[i].getEnd() >= res))
253 tmp.addElement(features[i]);
258 features = new SequenceFeature[tmp.size()];
259 tmp.copyInto(features);
266 if (editCommand != null && editCommand.getSize() > 0)
268 ap.alignFrame.addHistoryItem(editCommand);
269 av.firePropertyChange("alignment", null, av.getAlignment()
276 groupEditing = false;
284 seqCanvas.cursorY = getKeyboardNo1() - 1;
288 void setCursorColumn()
290 seqCanvas.cursorX = getKeyboardNo1() - 1;
294 void setCursorRowAndColumn()
296 if (keyboardNo2 == null)
298 keyboardNo2 = new StringBuffer();
302 seqCanvas.cursorX = getKeyboardNo1() - 1;
303 seqCanvas.cursorY = getKeyboardNo2() - 1;
308 void setCursorPosition()
310 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
312 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
316 void moveCursor(int dx, int dy)
318 seqCanvas.cursorX += dx;
319 seqCanvas.cursorY += dy;
320 if (av.hasHiddenColumns()
321 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
323 int original = seqCanvas.cursorX - dx;
324 int maxWidth = av.getAlignment().getWidth();
326 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
327 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
329 seqCanvas.cursorX += dx;
332 if (seqCanvas.cursorX >= maxWidth
333 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
335 seqCanvas.cursorX = original;
342 void scrollToVisible()
344 if (seqCanvas.cursorX < 0)
346 seqCanvas.cursorX = 0;
348 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
350 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
353 if (seqCanvas.cursorY < 0)
355 seqCanvas.cursorY = 0;
357 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
359 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
363 if (av.wrapAlignment)
365 ap.scrollToWrappedVisible(seqCanvas.cursorX);
369 while (seqCanvas.cursorY < av.startSeq)
373 while (seqCanvas.cursorY + 1 > av.endSeq)
377 if (!av.wrapAlignment)
379 while (seqCanvas.cursorX < av.getColumnSelection()
380 .adjustForHiddenColumns(av.startRes))
382 if (!ap.scrollRight(false))
387 while (seqCanvas.cursorX > av.getColumnSelection()
388 .adjustForHiddenColumns(av.endRes))
390 if (!ap.scrollRight(true))
397 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
398 seqCanvas.cursorX, seqCanvas.cursorY);
403 void setSelectionAreaAtCursor(boolean topLeft)
405 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
407 if (av.getSelectionGroup() != null)
409 SequenceGroup sg = av.getSelectionGroup();
410 // Find the top and bottom of this group
411 int min = av.getAlignment().getHeight(), max = 0;
412 for (int i = 0; i < sg.getSize(); i++)
414 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
429 sg.setStartRes(seqCanvas.cursorX);
430 if (sg.getEndRes() < seqCanvas.cursorX)
432 sg.setEndRes(seqCanvas.cursorX);
435 min = seqCanvas.cursorY;
439 sg.setEndRes(seqCanvas.cursorX);
440 if (sg.getStartRes() > seqCanvas.cursorX)
442 sg.setStartRes(seqCanvas.cursorX);
445 max = seqCanvas.cursorY + 1;
450 // Only the user can do this
451 av.setSelectionGroup(null);
455 // Now add any sequences between min and max
456 sg.getSequences(null).clear();
457 for (int i = min; i < max; i++)
459 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
464 if (av.getSelectionGroup() == null)
466 SequenceGroup sg = new SequenceGroup();
467 sg.setStartRes(seqCanvas.cursorX);
468 sg.setEndRes(seqCanvas.cursorX);
469 sg.addSequence(sequence, false);
470 av.setSelectionGroup(sg);
473 ap.paintAlignment(false);
477 void insertGapAtCursor(boolean group)
479 groupEditing = group;
480 startseq = seqCanvas.cursorY;
481 lastres = seqCanvas.cursorX;
482 editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1());
486 void deleteGapAtCursor(boolean group)
488 groupEditing = group;
489 startseq = seqCanvas.cursorY;
490 lastres = seqCanvas.cursorX + getKeyboardNo1();
491 editSequence(false, false, seqCanvas.cursorX);
495 void insertNucAtCursor(boolean group, String nuc)
497 groupEditing = group;
498 startseq = seqCanvas.cursorY;
499 lastres = seqCanvas.cursorX;
500 editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1());
504 void numberPressed(char value)
506 if (keyboardNo1 == null)
508 keyboardNo1 = new StringBuffer();
511 if (keyboardNo2 != null)
513 keyboardNo2.append(value);
517 keyboardNo1.append(value);
525 if (keyboardNo1 != null)
527 int value = Integer.parseInt(keyboardNo1.toString());
531 } catch (Exception x)
542 if (keyboardNo2 != null)
544 int value = Integer.parseInt(keyboardNo2.toString());
548 } catch (Exception x)
562 public void mouseReleased(MouseEvent evt)
564 mouseDragging = false;
565 mouseWheelPressed = false;
569 doMouseReleasedDefineMode(evt);
583 public void mousePressed(MouseEvent evt)
585 lastMousePress = evt.getPoint();
587 if (javax.swing.SwingUtilities.isMiddleMouseButton(evt))
589 mouseWheelPressed = true;
593 if (evt.isShiftDown() || evt.isAltDown() || evt.isControlDown())
595 if (evt.isAltDown() || evt.isControlDown())
603 doMousePressedDefineMode(evt);
607 int seq = findSeq(evt);
608 int res = findRes(evt);
610 if (seq < 0 || res < 0)
615 if ((seq < av.getAlignment().getHeight())
616 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
633 public void mouseOverSequence(SequenceI sequence, int index, int pos)
635 String tmp = sequence.hashCode() + " " + index + " " + pos;
637 if (lastMessage == null || !lastMessage.equals(tmp))
639 // System.err.println("mouseOver Sequence: "+tmp);
640 ssm.mouseOverSequence(sequence, index, pos, av);
646 public void highlightSequence(SearchResults results)
648 if (av.followHighlight)
650 if (ap.scrollToPosition(results, false))
652 seqCanvas.revalidate();
655 seqCanvas.highlightSearchResults(results);
659 public void updateColours(SequenceI seq, int index)
661 System.out.println("update the seqPanel colours");
672 public void mouseMoved(MouseEvent evt)
676 // This is because MacOSX creates a mouseMoved
677 // If control is down, other platforms will not.
681 int res = findRes(evt);
682 int seq = findSeq(evt);
684 if (res < 0 || seq < 0 || seq >= av.getAlignment().getHeight())
689 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
691 if (res >= sequence.getLength())
696 pos = setStatusMessage(sequence, res, seq);
697 if (ssm != null && pos > -1)
698 mouseOverSequence(sequence, res, pos);
700 tooltipText.setLength(6); // Cuts the buffer back to <html>
702 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
705 for (int g = 0; g < groups.length; g++)
707 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
709 if (tooltipText.length() > 6)
711 tooltipText.append("<br>");
714 if (!groups[g].getName().startsWith("JTreeGroup")
715 && !groups[g].getName().startsWith("JGroup"))
717 tooltipText.append(groups[g].getName());
720 if (groups[g].getDescription() != null)
722 tooltipText.append(": " + groups[g].getDescription());
728 // use aa to see if the mouse pointer is on a
729 if (av.showSequenceFeatures)
732 SequenceFeature[] features = findFeaturesAtRes(
733 sequence.getDatasetSequence(),
734 rpos = sequence.findPosition(res));
735 seqARep.appendFeatures(tooltipText, rpos, features,
736 this.ap.seqPanel.seqCanvas.fr.minmax);
738 if (tooltipText.length() == 6) // <html></html>
740 setToolTipText(null);
745 tooltipText.append("</html>");
746 if (lastTooltip == null
747 || !lastTooltip.equals(tooltipText.toString()))
749 setToolTipText(tooltipText.toString());
750 lastTooltip = tooltipText.toString();
757 private Point lastp = null;
762 * @see javax.swing.JComponent#getToolTipLocation(java.awt.event.MouseEvent)
764 public Point getToolTipLocation(MouseEvent event)
766 int x = event.getX(), w = getWidth();
767 int wdth = (w - x < 200) ? -(w / 2) : 5; // switch sides when tooltip is too
770 if (!event.isShiftDown() || p == null)
772 p = (tooltipText != null && tooltipText.length() > 6) ? new Point(
773 event.getX() + wdth, event.getY() - 20) : null;
776 * TODO: try to modify position region is not obcured by tooltip
784 * Set status message in alignment panel
787 * aligned sequence object
791 * index of sequence in alignment
792 * @return position of res in sequence
794 int setStatusMessage(SequenceI sequence, int res, int seq)
797 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
798 + sequence.getName());
801 if (av.getAlignment().isNucleotide())
803 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
807 text.append(" Nucleotide: ");
812 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
815 text.append(" Residue: ");
821 pos = sequence.findPosition(res);
824 text.append(obj + " (" + pos + ")");
827 ap.alignFrame.statusBar.setText(text.toString());
838 public void mouseDragged(MouseEvent evt)
840 if (mouseWheelPressed)
842 int oldWidth = av.charWidth;
844 // Which is bigger, left-right or up-down?
845 if (Math.abs(evt.getY() - lastMousePress.getY()) > Math.abs(evt
846 .getX() - lastMousePress.getX()))
848 int fontSize = av.font.getSize();
850 if (evt.getY() < lastMousePress.getY())
854 else if (evt.getY() > lastMousePress.getY())
864 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
865 av.charWidth = oldWidth;
870 if (evt.getX() < lastMousePress.getX() && av.charWidth > 1)
874 else if (evt.getX() > lastMousePress.getX())
879 ap.paintAlignment(false);
882 FontMetrics fm = getFontMetrics(av.getFont());
883 av.validCharWidth = fm.charWidth('M') <= av.charWidth;
885 lastMousePress = evt.getPoint();
892 doMouseDraggedDefineMode(evt);
896 int res = findRes(evt);
903 if ((lastres == -1) || (lastres == res))
908 if ((res < av.getAlignment().getWidth()) && (res < lastres))
910 // dragLeft, delete gap
911 editSequence(false, false, res);
915 editSequence(true, false, res);
918 mouseDragging = true;
919 if (scrollThread != null)
921 scrollThread.setEvent(evt);
925 // TODO: Make it more clever than many booleans
926 synchronized void editSequence(boolean insertGap, boolean editSeq,
931 boolean fixedColumns = false;
932 SequenceGroup sg = av.getSelectionGroup();
934 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
936 // No group, but the sequence may represent a group
937 if (!groupEditing && av.hasHiddenRows())
939 if (av.isHiddenRepSequence(seq))
941 sg = av.getRepresentedSequences(seq);
946 StringBuffer message = new StringBuffer();
949 message.append("Edit group:");
950 if (editCommand == null)
952 editCommand = new EditCommand(MessageManager.getString("action.edit_group"));
957 message.append("Edit sequence: " + seq.getName());
958 String label = seq.getName();
959 if (label.length() > 10)
961 label = label.substring(0, 10);
963 if (editCommand == null)
965 editCommand = new EditCommand(MessageManager.formatMessage("label.edit_params", new String[]{label}));
971 message.append(" insert ");
975 message.append(" delete ");
978 message.append(Math.abs(startres - lastres) + " gaps.");
979 ap.alignFrame.statusBar.setText(message.toString());
981 // Are we editing within a selection group?
983 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
988 // sg might be null as the user may only see 1 sequence,
989 // but the sequence represents a group
992 if (!av.isHiddenRepSequence(seq))
997 sg = av.getRepresentedSequences(seq);
1000 fixedLeft = sg.getStartRes();
1001 fixedRight = sg.getEndRes();
1003 if ((startres < fixedLeft && lastres >= fixedLeft)
1004 || (startres >= fixedLeft && lastres < fixedLeft)
1005 || (startres > fixedRight && lastres <= fixedRight)
1006 || (startres <= fixedRight && lastres > fixedRight))
1012 if (fixedLeft > startres)
1014 fixedRight = fixedLeft - 1;
1017 else if (fixedRight < startres)
1019 fixedLeft = fixedRight;
1024 if (av.hasHiddenColumns())
1026 fixedColumns = true;
1027 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1028 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1030 if ((insertGap && startres > y1 && lastres < y1)
1031 || (!insertGap && startres < y2 && lastres > y2))
1037 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1038 // Selection spans a hidden region
1039 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1047 fixedRight = y2 - 1;
1054 List<SequenceI> vseqs = sg.getSequences(av.getHiddenRepSequences());
1055 int g, groupSize = vseqs.size();
1056 SequenceI[] groupSeqs = new SequenceI[groupSize];
1057 for (g = 0; g < groupSeqs.length; g++)
1059 groupSeqs[g] = vseqs.get(g);
1065 // If the user has selected the whole sequence, and is dragging to
1066 // the right, we can still extend the alignment and selectionGroup
1067 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1068 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1070 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1071 fixedRight = sg.getEndRes();
1074 // Is it valid with fixed columns??
1075 // Find the next gap before the end
1076 // of the visible region boundary
1077 boolean blank = false;
1078 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1082 for (g = 0; g < groupSize; g++)
1084 for (int j = 0; j < startres - lastres; j++)
1086 if (!jalview.util.Comparison.isGap(groupSeqs[g]
1087 .getCharAt(fixedRight - j)))
1102 if (sg.getSize() == av.getAlignment().getHeight())
1104 if ((av.hasHiddenColumns() && startres < av
1105 .getColumnSelection().getHiddenBoundaryRight(startres)))
1111 int alWidth = av.getAlignment().getWidth();
1112 if (av.hasHiddenRows())
1114 int hwidth = av.getAlignment().getHiddenSequences()
1116 if (hwidth > alWidth)
1121 // We can still insert gaps if the selectionGroup
1122 // contains all the sequences
1123 sg.setEndRes(sg.getEndRes() + startres - lastres);
1124 fixedRight = alWidth + startres - lastres;
1135 else if (!insertGap)
1137 // / Are we able to delete?
1138 // ie are all columns blank?
1140 for (g = 0; g < groupSize; g++)
1142 for (int j = startres; j < lastres; j++)
1144 if (groupSeqs[g].getLength() <= j)
1149 if (!jalview.util.Comparison.isGap(groupSeqs[g].getCharAt(j)))
1151 // Not a gap, block edit not valid
1161 // dragging to the right
1162 if (fixedColumns && fixedRight != -1)
1164 for (int j = lastres; j < startres; j++)
1166 insertChar(j, groupSeqs, fixedRight);
1171 editCommand.appendEdit(EditCommand.INSERT_GAP, groupSeqs,
1172 startres, startres - lastres, av.getAlignment(), true);
1177 // dragging to the left
1178 if (fixedColumns && fixedRight != -1)
1180 for (int j = lastres; j > startres; j--)
1182 deleteChar(startres, groupSeqs, fixedRight);
1187 editCommand.appendEdit(EditCommand.DELETE_GAP, groupSeqs,
1188 startres, lastres - startres, av.getAlignment(), true);
1194 // ///Editing a single sequence///////////
1198 // dragging to the right
1199 if (fixedColumns && fixedRight != -1)
1201 for (int j = lastres; j < startres; j++)
1203 insertChar(j, new SequenceI[]
1204 { seq }, fixedRight);
1209 editCommand.appendEdit(EditCommand.INSERT_GAP, new SequenceI[]
1210 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1217 // dragging to the left
1218 if (fixedColumns && fixedRight != -1)
1220 for (int j = lastres; j > startres; j--)
1222 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1227 deleteChar(startres, new SequenceI[]
1228 { seq }, fixedRight);
1233 // could be a keyboard edit trying to delete none gaps
1235 for (int m = startres; m < lastres; m++)
1237 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1246 editCommand.appendEdit(EditCommand.DELETE_GAP,
1248 { seq }, startres, max, av.getAlignment(), true);
1253 {// insertGap==false AND editSeq==TRUE;
1254 if (fixedColumns && fixedRight != -1)
1256 for (int j = lastres; j < startres; j++)
1258 insertChar(j, new SequenceI[]
1259 { seq }, fixedRight);
1264 editCommand.appendEdit(EditCommand.INSERT_NUC, new SequenceI[]
1265 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1272 seqCanvas.repaint();
1275 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1277 int blankColumn = fixedColumn;
1278 for (int s = 0; s < seq.length; s++)
1280 // Find the next gap before the end of the visible region boundary
1281 // If lastCol > j, theres a boundary after the gap insertion
1283 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1285 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1287 // Theres a space, so break and insert the gap
1292 if (blankColumn <= j)
1294 blankColumn = fixedColumn;
1300 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, blankColumn, 1,
1301 av.getAlignment(), true);
1303 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, j, 1,
1304 av.getAlignment(), true);
1308 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1311 editCommand.appendEdit(EditCommand.DELETE_GAP, seq, j, 1,
1312 av.getAlignment(), true);
1314 editCommand.appendEdit(EditCommand.INSERT_GAP, seq, fixedColumn, 1,
1315 av.getAlignment(), true);
1325 public void mouseEntered(MouseEvent e)
1332 if (scrollThread != null)
1334 scrollThread.running = false;
1335 scrollThread = null;
1346 public void mouseExited(MouseEvent e)
1348 if (av.getWrapAlignment())
1355 scrollThread = new ScrollThread();
1360 public void mouseClicked(MouseEvent evt)
1362 SequenceGroup sg = null;
1363 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
1364 if (evt.getClickCount() > 1)
1366 sg = av.getSelectionGroup();
1367 if (sg != null && sg.getSize() == 1
1368 && sg.getEndRes() - sg.getStartRes() < 2)
1370 av.setSelectionGroup(null);
1373 SequenceFeature[] features = findFeaturesAtRes(
1374 sequence.getDatasetSequence(),
1375 sequence.findPosition(findRes(evt)));
1377 if (features != null && features.length > 0)
1379 SearchResults highlight = new SearchResults();
1380 highlight.addResult(sequence, features[0].getBegin(),
1381 features[0].getEnd());
1382 seqCanvas.highlightSearchResults(highlight);
1384 if (features != null && features.length > 0)
1386 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
1387 { sequence }, features, false, ap);
1389 seqCanvas.highlightSearchResults(null);
1395 public void mouseWheelMoved(MouseWheelEvent e)
1398 if (e.getWheelRotation() > 0)
1400 if (e.isShiftDown())
1402 ap.scrollRight(true);
1412 if (e.isShiftDown())
1414 ap.scrollRight(false);
1421 // TODO Update tooltip for new position.
1430 public void doMousePressedDefineMode(MouseEvent evt)
1432 int res = findRes(evt);
1433 int seq = findSeq(evt);
1436 startWrapBlock = wrappedBlock;
1438 if (av.wrapAlignment && seq > av.getAlignment().getHeight())
1440 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
1441 .getString("label.cannot_edit_annotations_in_wrapped_view"),
1442 MessageManager.getString("label.wrapped_view_no_edit"),
1443 JOptionPane.WARNING_MESSAGE);
1447 if (seq < 0 || res < 0)
1452 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1454 if ((sequence == null) || (res > sequence.getLength()))
1459 stretchGroup = av.getSelectionGroup();
1461 if (stretchGroup == null)
1463 stretchGroup = av.getAlignment().findGroup(sequence);
1465 if ((stretchGroup != null) && (res > stretchGroup.getStartRes())
1466 && (res < stretchGroup.getEndRes()))
1468 av.setSelectionGroup(stretchGroup);
1472 stretchGroup = null;
1475 else if (!stretchGroup.getSequences(null).contains(sequence)
1476 || (stretchGroup.getStartRes() > res)
1477 || (stretchGroup.getEndRes() < res))
1479 stretchGroup = null;
1481 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1483 if (allGroups != null)
1485 for (int i = 0; i < allGroups.length; i++)
1487 if ((allGroups[i].getStartRes() <= res)
1488 && (allGroups[i].getEndRes() >= res))
1490 stretchGroup = allGroups[i];
1496 av.setSelectionGroup(stretchGroup);
1500 if (javax.swing.SwingUtilities.isRightMouseButton(evt))
1502 SequenceFeature[] allFeatures = findFeaturesAtRes(
1503 sequence.getDatasetSequence(), sequence.findPosition(res));
1504 Vector links = new Vector();
1505 for (int i = 0; i < allFeatures.length; i++)
1507 if (allFeatures[i].links != null)
1509 for (int j = 0; j < allFeatures[i].links.size(); j++)
1511 links.addElement(allFeatures[i].links.elementAt(j));
1516 jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(ap, null, links);
1517 pop.show(this, evt.getX(), evt.getY());
1523 seqCanvas.cursorX = findRes(evt);
1524 seqCanvas.cursorY = findSeq(evt);
1525 seqCanvas.repaint();
1529 if (stretchGroup == null)
1531 // Only if left mouse button do we want to change group sizes
1533 // define a new group here
1534 SequenceGroup sg = new SequenceGroup();
1535 sg.setStartRes(res);
1537 sg.addSequence(sequence, false);
1538 av.setSelectionGroup(sg);
1542 if (av.getConservationSelected())
1544 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1548 if (av.getAbovePIDThreshold())
1550 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1553 if ((stretchGroup != null) && (stretchGroup.getEndRes() == res))
1555 // Edit end res position of selected group
1556 changeEndRes = true;
1558 else if ((stretchGroup != null)
1559 && (stretchGroup.getStartRes() == res))
1561 // Edit end res position of selected group
1562 changeStartRes = true;
1564 stretchGroup.getWidth();
1567 seqCanvas.repaint();
1576 public void doMouseReleasedDefineMode(MouseEvent evt)
1578 if (stretchGroup == null)
1583 stretchGroup.recalcConservation(); // always do this - annotation has own
1585 if (stretchGroup.cs != null)
1587 stretchGroup.cs.alignmentChanged(stretchGroup,
1588 av.getHiddenRepSequences());
1590 if (stretchGroup.cs.conservationApplied())
1592 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1593 stretchGroup.getName());
1597 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1598 stretchGroup.getName());
1601 PaintRefresher.Refresh(this, av.getSequenceSetId());
1602 ap.paintAlignment(true);
1604 changeEndRes = false;
1605 changeStartRes = false;
1606 stretchGroup = null;
1616 public void doMouseDraggedDefineMode(MouseEvent evt)
1618 int res = findRes(evt);
1619 int y = findSeq(evt);
1621 if (wrappedBlock != startWrapBlock)
1626 if (stretchGroup == null)
1631 if (res >= av.getAlignment().getWidth())
1633 res = av.getAlignment().getWidth() - 1;
1636 if (stretchGroup.getEndRes() == res)
1638 // Edit end res position of selected group
1639 changeEndRes = true;
1641 else if (stretchGroup.getStartRes() == res)
1643 // Edit start res position of selected group
1644 changeStartRes = true;
1647 if (res < av.getStartRes())
1649 res = av.getStartRes();
1654 if (res > (stretchGroup.getStartRes() - 1))
1656 stretchGroup.setEndRes(res);
1659 else if (changeStartRes)
1661 if (res < (stretchGroup.getEndRes() + 1))
1663 stretchGroup.setStartRes(res);
1667 int dragDirection = 0;
1673 else if (y < oldSeq)
1678 while ((y != oldSeq) && (oldSeq > -1)
1679 && (y < av.getAlignment().getHeight()))
1681 // This routine ensures we don't skip any sequences, as the
1682 // selection is quite slow.
1683 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1685 oldSeq += dragDirection;
1692 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1694 if (stretchGroup.getSequences(null).contains(nextSeq))
1696 stretchGroup.deleteSequence(seq, false);
1702 stretchGroup.addSequence(seq, false);
1705 stretchGroup.addSequence(nextSeq, false);
1714 mouseDragging = true;
1716 if (scrollThread != null)
1718 scrollThread.setEvent(evt);
1721 seqCanvas.repaint();
1724 void scrollCanvas(MouseEvent evt)
1728 if (scrollThread != null)
1730 scrollThread.running = false;
1731 scrollThread = null;
1733 mouseDragging = false;
1737 if (scrollThread == null)
1739 scrollThread = new ScrollThread();
1742 mouseDragging = true;
1743 scrollThread.setEvent(evt);
1748 // this class allows scrolling off the bottom of the visible alignment
1749 class ScrollThread extends Thread
1753 boolean running = false;
1755 public ScrollThread()
1760 public void setEvent(MouseEvent e)
1765 public void stopScrolling()
1779 if (mouseDragging && (evt.getY() < 0) && (av.getStartSeq() > 0))
1781 running = ap.scrollUp(true);
1784 if (mouseDragging && (evt.getY() >= getHeight())
1785 && (av.getAlignment().getHeight() > av.getEndSeq()))
1787 running = ap.scrollUp(false);
1790 if (mouseDragging && (evt.getX() < 0))
1792 running = ap.scrollRight(false);
1794 else if (mouseDragging && (evt.getX() >= getWidth()))
1796 running = ap.scrollRight(true);
1803 } catch (Exception ex)
1811 * modify current selection according to a received message.
1814 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1815 SelectionSource source)
1817 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1818 // handles selection messages...
1819 // TODO: extend config options to allow user to control if selections may be
1820 // shared between viewports.
1822 || !av.followSelection
1823 || (av.isSelectionGroupChanged(false) || av
1824 .isColSelChanged(false))
1825 || (source instanceof AlignViewport && ((AlignViewport) source)
1826 .getSequenceSetId().equals(av.getSequenceSetId())))
1830 // do we want to thread this ? (contention with seqsel and colsel locks, I
1832 // rules are: colsel is copied if there is a real intersection between
1833 // sequence selection
1834 boolean repaint = false, copycolsel = true;
1835 // if (!av.isSelectionGroupChanged(false))
1837 SequenceGroup sgroup = null;
1838 if (seqsel != null && seqsel.getSize() > 0)
1840 if (av.getAlignment() == null)
1842 jalview.bin.Cache.log.warn("alignviewport av SeqSetId="
1843 + av.getSequenceSetId() + " ViewId=" + av.getViewId()
1844 + " 's alignment is NULL! returning immediatly.");
1847 sgroup = seqsel.intersect(av.getAlignment(),
1848 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1849 if ((sgroup == null || sgroup.getSize() == 0)
1850 || (colsel == null || colsel.size() == 0))
1852 // don't copy columns if the region didn't intersect.
1856 if (sgroup != null && sgroup.getSize() > 0)
1858 av.setSelectionGroup(sgroup);
1862 av.setSelectionGroup(null);
1864 av.isSelectionGroupChanged(true);
1869 // the current selection is unset or from a previous message
1870 // so import the new colsel.
1871 if (colsel == null || colsel.size() == 0)
1873 if (av.getColumnSelection() != null)
1875 av.getColumnSelection().clear();
1881 // TODO: shift colSel according to the intersecting sequences
1882 if (av.getColumnSelection() == null)
1884 av.setColumnSelection(new ColumnSelection(colsel));
1888 av.getColumnSelection().setElementsFrom(colsel);
1891 av.isColSelChanged(true);
1895 && av.hasHiddenColumns()
1896 && (av.getColumnSelection() == null || av.getColumnSelection()
1897 .getHiddenColumns() == null))
1899 System.err.println("Bad things");
1903 // probably finessing with multiple redraws here
1904 PaintRefresher.Refresh(this, av.getSequenceSetId());
1905 // ap.paintAlignment(false);