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.
21 package jalview.appletgui;
23 import jalview.api.AlignViewportI;
24 import jalview.commands.EditCommand;
25 import jalview.commands.EditCommand.Action;
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.schemes.ResidueProperties;
33 import jalview.structure.SelectionListener;
34 import jalview.structure.SelectionSource;
35 import jalview.structure.SequenceListener;
36 import jalview.structure.StructureSelectionManager;
37 import jalview.structure.VamsasSource;
38 import jalview.util.MappingUtils;
39 import jalview.util.MessageManager;
41 import java.awt.BorderLayout;
43 import java.awt.FontMetrics;
44 import java.awt.Panel;
45 import java.awt.Point;
46 import java.awt.event.InputEvent;
47 import java.awt.event.MouseEvent;
48 import java.awt.event.MouseListener;
49 import java.awt.event.MouseMotionListener;
50 import java.util.Vector;
52 public class SeqPanel extends Panel implements MouseMotionListener,
53 MouseListener, SequenceListener, SelectionListener
56 public SeqCanvas seqCanvas;
58 public AlignmentPanel ap;
60 protected int lastres;
62 protected int startseq;
64 protected AlignViewport av;
66 // if character is inserted or deleted, we will need to recalculate the
68 boolean seqEditOccurred = false;
70 ScrollThread scrollThread = null;
72 boolean mouseDragging = false;
74 boolean editingSeqs = false;
76 boolean groupEditing = false;
80 boolean changeEndSeq = false;
82 boolean changeStartSeq = false;
84 boolean changeEndRes = false;
86 boolean changeStartRes = false;
88 SequenceGroup stretchGroup = null;
90 StringBuffer keyboardNo1;
92 StringBuffer keyboardNo2;
94 boolean mouseWheelPressed = false;
98 EditCommand editCommand;
100 StructureSelectionManager ssm;
102 public SeqPanel(AlignViewport avp, AlignmentPanel p)
106 seqCanvas = new SeqCanvas(avp);
107 setLayout(new BorderLayout());
112 seqCanvas.addMouseMotionListener(this);
113 seqCanvas.addMouseListener(this);
114 ssm = StructureSelectionManager.getStructureSelectionManager(av.applet);
115 ssm.addStructureViewerListener(this);
116 ssm.addSelectionListener(this);
123 if (editCommand != null && editCommand.getSize() > 0)
125 ap.alignFrame.addHistoryItem(editCommand);
126 av.firePropertyChange("alignment", null, av.getAlignment()
133 groupEditing = false;
141 seqCanvas.cursorY = getKeyboardNo1() - 1;
145 void setCursorColumn()
147 seqCanvas.cursorX = getKeyboardNo1() - 1;
151 void setCursorRowAndColumn()
153 if (keyboardNo2 == null)
155 keyboardNo2 = new StringBuffer();
159 seqCanvas.cursorX = getKeyboardNo1() - 1;
160 seqCanvas.cursorY = getKeyboardNo2() - 1;
165 void setCursorPosition()
167 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
169 seqCanvas.cursorX = sequence.findIndex(getKeyboardNo1()) - 1;
173 void moveCursor(int dx, int dy)
175 seqCanvas.cursorX += dx;
176 seqCanvas.cursorY += dy;
177 if (av.hasHiddenColumns()
178 && !av.getColumnSelection().isVisible(seqCanvas.cursorX))
180 int original = seqCanvas.cursorX - dx;
181 int maxWidth = av.getAlignment().getWidth();
183 while (!av.getColumnSelection().isVisible(seqCanvas.cursorX)
184 && seqCanvas.cursorX < maxWidth && seqCanvas.cursorX > 0)
186 seqCanvas.cursorX += dx;
189 if (seqCanvas.cursorX >= maxWidth
190 || !av.getColumnSelection().isVisible(seqCanvas.cursorX))
192 seqCanvas.cursorX = original;
198 void scrollToVisible()
200 if (seqCanvas.cursorX < 0)
202 seqCanvas.cursorX = 0;
204 else if (seqCanvas.cursorX > av.getAlignment().getWidth() - 1)
206 seqCanvas.cursorX = av.getAlignment().getWidth() - 1;
209 if (seqCanvas.cursorY < 0)
211 seqCanvas.cursorY = 0;
213 else if (seqCanvas.cursorY > av.getAlignment().getHeight() - 1)
215 seqCanvas.cursorY = av.getAlignment().getHeight() - 1;
219 if (av.getWrapAlignment())
221 ap.scrollToWrappedVisible(seqCanvas.cursorX);
225 while (seqCanvas.cursorY < av.startSeq)
229 while (seqCanvas.cursorY + 1 > av.endSeq)
233 while (seqCanvas.cursorX < av.getColumnSelection()
234 .adjustForHiddenColumns(av.startRes))
237 if (!ap.scrollRight(false))
242 while (seqCanvas.cursorX > av.getColumnSelection()
243 .adjustForHiddenColumns(av.endRes))
245 if (!ap.scrollRight(true))
251 setStatusMessage(av.getAlignment().getSequenceAt(seqCanvas.cursorY),
252 seqCanvas.cursorX, seqCanvas.cursorY);
257 void setSelectionAreaAtCursor(boolean topLeft)
259 SequenceI sequence = av.getAlignment().getSequenceAt(seqCanvas.cursorY);
261 if (av.getSelectionGroup() != null)
263 SequenceGroup sg = av.getSelectionGroup();
264 // Find the top and bottom of this group
265 int min = av.getAlignment().getHeight(), max = 0;
266 for (int i = 0; i < sg.getSize(); i++)
268 int index = av.getAlignment().findIndex(sg.getSequenceAt(i));
283 sg.setStartRes(seqCanvas.cursorX);
284 if (sg.getEndRes() < seqCanvas.cursorX)
286 sg.setEndRes(seqCanvas.cursorX);
289 min = seqCanvas.cursorY;
293 sg.setEndRes(seqCanvas.cursorX);
294 if (sg.getStartRes() > seqCanvas.cursorX)
296 sg.setStartRes(seqCanvas.cursorX);
299 max = seqCanvas.cursorY + 1;
304 // Only the user can do this
305 av.setSelectionGroup(null);
309 // Now add any sequences between min and max
311 for (int i = min; i < max; i++)
313 sg.addSequence(av.getAlignment().getSequenceAt(i), false);
318 if (av.getSelectionGroup() == null)
320 SequenceGroup sg = new SequenceGroup();
321 sg.setStartRes(seqCanvas.cursorX);
322 sg.setEndRes(seqCanvas.cursorX);
323 sg.addSequence(sequence, false);
324 av.setSelectionGroup(sg);
326 ap.paintAlignment(false);
330 void insertGapAtCursor(boolean group)
332 groupEditing = group;
333 startseq = seqCanvas.cursorY;
334 lastres = seqCanvas.cursorX;
335 editSequence(true, seqCanvas.cursorX + getKeyboardNo1());
339 void deleteGapAtCursor(boolean group)
341 groupEditing = group;
342 startseq = seqCanvas.cursorY;
343 lastres = seqCanvas.cursorX + getKeyboardNo1();
344 editSequence(false, seqCanvas.cursorX);
348 void numberPressed(char value)
350 if (keyboardNo1 == null)
352 keyboardNo1 = new StringBuffer();
355 if (keyboardNo2 != null)
357 keyboardNo2.append(value);
361 keyboardNo1.append(value);
369 if (keyboardNo1 != null)
371 int value = Integer.parseInt(keyboardNo1.toString());
375 } catch (Exception x)
386 if (keyboardNo2 != null)
388 int value = Integer.parseInt(keyboardNo2.toString());
392 } catch (Exception x)
399 void setStatusMessage(SequenceI sequence, int res, int seq)
401 StringBuffer text = new StringBuffer("Sequence " + (seq + 1) + " ID: "
402 + sequence.getName());
405 if (av.getAlignment().isNucleotide())
407 obj = ResidueProperties.nucleotideName.get(sequence.getCharAt(res)
411 text.append(" Nucleotide: ");
416 obj = ResidueProperties.aa2Triplet.get(sequence.getCharAt(res) + "");
419 text.append(" Residue: ");
428 text.append(obj + " (" + sequence.findPosition(res) + ")");
432 ap.alignFrame.statusBar.setText(text.toString());
436 public void mousePressed(MouseEvent evt)
438 lastMousePress = evt.getPoint();
440 // For now, ignore the mouseWheel font resizing on Macs
441 // As the Button2_mask always seems to be true
442 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK
445 mouseWheelPressed = true;
449 if (evt.isShiftDown() || evt.isControlDown() || evt.isAltDown())
451 if (evt.isControlDown() || evt.isAltDown())
459 doMousePressedDefineMode(evt);
463 int seq = findSeq(evt);
464 int res = findRes(evt);
466 if (seq < 0 || res < 0)
471 if ((seq < av.getAlignment().getHeight())
472 && (res < av.getAlignment().getSequenceAt(seq).getLength()))
486 public void mouseClicked(MouseEvent evt)
488 SequenceI sequence = av.getAlignment().getSequenceAt(findSeq(evt));
489 if (evt.getClickCount() > 1)
491 if (av.getSelectionGroup() != null
492 && av.getSelectionGroup().getSize() == 1
493 && av.getSelectionGroup().getEndRes()
494 - av.getSelectionGroup().getStartRes() < 2)
496 av.setSelectionGroup(null);
499 SequenceFeature[] features = findFeaturesAtRes(sequence,
500 sequence.findPosition(findRes(evt)));
502 if (features != null && features.length > 0)
504 SearchResults highlight = new SearchResults();
505 highlight.addResult(sequence, features[0].getBegin(),
506 features[0].getEnd());
507 seqCanvas.highlightSearchResults(highlight);
509 if (features != null && features.length > 0)
511 seqCanvas.getFeatureRenderer().amendFeatures(new SequenceI[]
512 { sequence }, features, false, ap);
514 seqCanvas.highlightSearchResults(null);
519 public void mouseReleased(MouseEvent evt)
521 mouseDragging = false;
522 mouseWheelPressed = false;
523 ap.paintAlignment(true);
527 doMouseReleasedDefineMode(evt);
535 int startWrapBlock = -1;
537 int wrappedBlock = -1;
539 int findRes(MouseEvent evt)
544 if (av.getWrapAlignment())
547 int hgap = av.getCharHeight();
548 if (av.getScaleAboveWrapped())
550 hgap += av.getCharHeight();
553 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
555 + seqCanvas.getAnnotationHeight();
559 x -= seqCanvas.LABEL_WEST;
561 int cwidth = seqCanvas.getWrappedCanvasWidth(getSize().width);
567 wrappedBlock = y / cHeight;
568 wrappedBlock += av.getStartRes() / cwidth;
570 res = wrappedBlock * cwidth + x / av.getCharWidth();
575 res = (x / av.getCharWidth()) + av.getStartRes();
578 if (av.hasHiddenColumns())
580 res = av.getColumnSelection().adjustForHiddenColumns(res);
587 int findSeq(MouseEvent evt)
589 final int sqnum = findAlRow(evt);
590 return (sqnum < 0) ? 0 : sqnum;
596 * @return row in alignment that was selected (or -1 for column selection)
598 private int findAlRow(MouseEvent evt)
603 if (av.getWrapAlignment())
605 int hgap = av.getCharHeight();
606 if (av.getScaleAboveWrapped())
608 hgap += av.getCharHeight();
611 int cHeight = av.getAlignment().getHeight() * av.getCharHeight()
613 + seqCanvas.getAnnotationHeight();
617 seq = Math.min((y % cHeight) / av.getCharHeight(), av.getAlignment()
626 seq = Math.min((y / av.getCharHeight()) + av.getStartSeq(), av
627 .getAlignment().getHeight() - 1);
637 public void doMousePressed(MouseEvent evt)
640 int seq = findSeq(evt);
641 int res = findRes(evt);
643 if (seq < av.getAlignment().getHeight()
644 && res < av.getAlignment().getSequenceAt(seq).getLength())
646 // char resstr = align.getSequenceAt(seq).getSequence().charAt(res);
647 // Find the residue's position in the sequence (res is the position
664 public void mouseOverSequence(SequenceI sequence, int index, int pos)
666 String tmp = sequence.hashCode() + index + "";
667 if (lastMessage == null || !lastMessage.equals(tmp))
669 ssm.mouseOverSequence(sequence, index, pos, av);
675 public void highlightSequence(SearchResults results)
677 if (av.followHighlight)
679 if (ap.scrollToPosition(results, true))
681 ap.alignFrame.repaint();
684 seqCanvas.highlightSearchResults(results);
689 public VamsasSource getVamsasSource()
691 return this.ap == null ? null : this.ap.av;
694 public void updateColours(SequenceI seq, int index)
696 System.out.println("update the seqPanel colours");
700 public void mouseMoved(MouseEvent evt)
702 int res = findRes(evt);
703 int seq = findSeq(evt);
705 if (seq >= av.getAlignment().getHeight() || seq < 0 || res < 0)
714 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
715 if (res > sequence.getLength())
724 int respos = sequence.findPosition(res);
727 mouseOverSequence(sequence, res, respos);
730 StringBuilder text = new StringBuilder();
731 text.append("Sequence ").append(Integer.toString(seq + 1))
732 .append(" ID: ").append(sequence.getName());
735 final String ch = String.valueOf(sequence.getCharAt(res));
736 if (av.getAlignment().isNucleotide())
738 obj = ResidueProperties.nucleotideName.get(ch);
741 text.append(" Nucleotide: ").append(obj);
746 obj = "X".equalsIgnoreCase(ch) ? "STOP"
747 : ResidueProperties.aa2Triplet.get(ch);
750 text.append(" Residue: ").append(obj);
756 text.append(" (").append(Integer.toString(respos)).append(")");
759 ap.alignFrame.statusBar.setText(text.toString());
761 StringBuilder tooltipText = new StringBuilder();
762 SequenceGroup[] groups = av.getAlignment().findAllGroups(sequence);
765 for (int g = 0; g < groups.length; g++)
767 if (groups[g].getStartRes() <= res && groups[g].getEndRes() >= res)
769 if (!groups[g].getName().startsWith("JTreeGroup")
770 && !groups[g].getName().startsWith("JGroup"))
772 tooltipText.append(groups[g].getName()).append(" ");
774 if (groups[g].getDescription() != null)
776 tooltipText.append(groups[g].getDescription());
778 tooltipText.append("\n");
783 // use aa to see if the mouse pointer is on a
784 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
785 sequence.findPosition(res));
788 while (index < allFeatures.length)
790 SequenceFeature sf = allFeatures[index];
792 tooltipText.append(sf.getType() + " " + sf.begin + ":" + sf.end);
794 if (sf.getDescription() != null)
796 tooltipText.append(" " + sf.getDescription());
799 if (sf.getValue("status") != null)
801 String status = sf.getValue("status").toString();
802 if (status.length() > 0)
804 tooltipText.append(" (" + sf.getValue("status") + ")");
807 tooltipText.append("\n");
814 tooltip = new Tooltip(tooltipText.toString(), seqCanvas);
818 tooltip.setTip(tooltipText.toString());
822 SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res)
824 Vector tmp = new Vector();
825 SequenceFeature[] features = sequence.getSequenceFeatures();
826 if (features != null)
828 for (int i = 0; i < features.length; i++)
830 if (av.getFeaturesDisplayed() == null
831 || !av.getFeaturesDisplayed().isVisible(features[i].getType()))
836 if (features[i].featureGroup != null
837 && !seqCanvas.fr.checkGroupVisibility(features[i].featureGroup,false))
842 if ((features[i].getBegin() <= res)
843 && (features[i].getEnd() >= res))
845 tmp.addElement(features[i]);
850 features = new SequenceFeature[tmp.size()];
851 tmp.copyInto(features);
858 public void mouseDragged(MouseEvent evt)
860 if (mouseWheelPressed)
862 int oldWidth = av.getCharWidth();
864 // Which is bigger, left-right or up-down?
865 if (Math.abs(evt.getY() - lastMousePress.y) > Math.abs(evt.getX()
868 int fontSize = av.font.getSize();
870 if (evt.getY() < lastMousePress.y && av.getCharHeight() > 1)
874 else if (evt.getY() > lastMousePress.y)
884 av.setFont(new Font(av.font.getName(), av.font.getStyle(), fontSize));
885 av.setCharWidth(oldWidth);
889 if (evt.getX() < lastMousePress.x && av.getCharWidth() > 1)
891 av.setCharWidth(av.getCharWidth() - 1);
893 else if (evt.getX() > lastMousePress.x)
895 av.setCharWidth(av.getCharWidth() + 1);
898 if (av.getCharWidth() < 1)
906 FontMetrics fm = getFontMetrics(av.getFont());
907 av.validCharWidth = fm.charWidth('M') <= av.getCharWidth();
909 lastMousePress = evt.getPoint();
911 ap.paintAlignment(false);
912 ap.annotationPanel.image = null;
918 doMouseDraggedDefineMode(evt);
922 int res = findRes(evt);
929 if ((lastres == -1) || (lastres == res))
934 if ((res < av.getAlignment().getWidth()) && (res < lastres))
936 // dragLeft, delete gap
937 editSequence(false, res);
941 editSequence(true, res);
944 mouseDragging = true;
945 if (scrollThread != null)
947 scrollThread.setEvent(evt);
952 synchronized void editSequence(boolean insertGap, int startres)
956 boolean fixedColumns = false;
957 SequenceGroup sg = av.getSelectionGroup();
959 SequenceI seq = av.getAlignment().getSequenceAt(startseq);
961 if (!groupEditing && av.hasHiddenRows())
963 if (av.isHiddenRepSequence(seq))
965 sg = av.getRepresentedSequences(seq);
970 StringBuffer message = new StringBuffer();
973 message.append(MessageManager.getString("action.edit_group")).append(
975 if (editCommand == null)
977 editCommand = new EditCommand(
978 MessageManager.getString("action.edit_group"));
983 message.append(MessageManager.getString("label.edit_sequence"))
984 .append(" " + seq.getName());
985 String label = seq.getName();
986 if (label.length() > 10)
988 label = label.substring(0, 10);
990 if (editCommand == null)
992 editCommand = new EditCommand(MessageManager.formatMessage(
993 "label.edit_params", new String[]
1000 message.append(" insert ");
1004 message.append(" delete ");
1007 message.append(Math.abs(startres - lastres) + " gaps.");
1008 ap.alignFrame.statusBar.setText(message.toString());
1010 // Are we editing within a selection group?
1012 || (sg != null && sg.getSequences(av.getHiddenRepSequences())
1015 fixedColumns = true;
1017 // sg might be null as the user may only see 1 sequence,
1018 // but the sequence represents a group
1021 if (!av.isHiddenRepSequence(seq))
1027 sg = av.getRepresentedSequences(seq);
1030 fixedLeft = sg.getStartRes();
1031 fixedRight = sg.getEndRes();
1033 if ((startres < fixedLeft && lastres >= fixedLeft)
1034 || (startres >= fixedLeft && lastres < fixedLeft)
1035 || (startres > fixedRight && lastres <= fixedRight)
1036 || (startres <= fixedRight && lastres > fixedRight))
1042 if (fixedLeft > startres)
1044 fixedRight = fixedLeft - 1;
1047 else if (fixedRight < startres)
1049 fixedLeft = fixedRight;
1054 if (av.hasHiddenColumns())
1056 fixedColumns = true;
1057 int y1 = av.getColumnSelection().getHiddenBoundaryLeft(startres);
1058 int y2 = av.getColumnSelection().getHiddenBoundaryRight(startres);
1060 if ((insertGap && startres > y1 && lastres < y1)
1061 || (!insertGap && startres < y2 && lastres > y2))
1067 // System.out.print(y1+" "+y2+" "+fixedLeft+" "+fixedRight+"~~");
1068 // Selection spans a hidden region
1069 if (fixedLeft < y1 && (fixedRight > y2 || fixedRight == -1))
1077 fixedRight = y2 - 1;
1084 SequenceI[] groupSeqs = sg.getSequences(av.getHiddenRepSequences())
1085 .toArray(new SequenceI[0]);
1090 // If the user has selected the whole sequence, and is dragging to
1091 // the right, we can still extend the alignment and selectionGroup
1092 if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight
1093 && sg.getEndRes() == av.getAlignment().getWidth() - 1)
1095 sg.setEndRes(av.getAlignment().getWidth() + startres - lastres);
1096 fixedRight = sg.getEndRes();
1099 // Is it valid with fixed columns??
1100 // Find the next gap before the end
1101 // of the visible region boundary
1102 boolean blank = false;
1103 for (fixedRight = fixedRight; fixedRight > lastres; fixedRight--)
1107 for (SequenceI gs : groupSeqs)
1109 for (int j = 0; j < startres - lastres; j++)
1111 if (!jalview.util.Comparison.isGap(gs.getCharAt(fixedRight
1127 if (sg.getSize() == av.getAlignment().getHeight())
1129 if ((av.hasHiddenColumns() && startres < av
1130 .getColumnSelection().getHiddenBoundaryRight(startres)))
1136 int alWidth = av.getAlignment().getWidth();
1137 if (av.hasHiddenRows())
1139 int hwidth = av.getAlignment().getHiddenSequences()
1141 if (hwidth > alWidth)
1146 // We can still insert gaps if the selectionGroup
1147 // contains all the sequences
1148 sg.setEndRes(sg.getEndRes() + startres - lastres);
1149 fixedRight = alWidth + startres - lastres;
1160 else if (!insertGap)
1162 // / Are we able to delete?
1163 // ie are all columns blank?
1165 for (SequenceI gs : groupSeqs)
1167 for (int j = startres; j < lastres; j++)
1169 if (gs.getLength() <= j)
1174 if (!jalview.util.Comparison.isGap(gs.getCharAt(j)))
1176 // Not a gap, block edit not valid
1186 // dragging to the right
1187 if (fixedColumns && fixedRight != -1)
1189 for (int j = lastres; j < startres; j++)
1191 insertChar(j, groupSeqs, fixedRight);
1196 editCommand.appendEdit(Action.INSERT_GAP, groupSeqs, startres,
1197 startres - lastres, av.getAlignment(), true);
1202 // dragging to the left
1203 if (fixedColumns && fixedRight != -1)
1205 for (int j = lastres; j > startres; j--)
1207 deleteChar(startres, groupSeqs, fixedRight);
1212 editCommand.appendEdit(Action.DELETE_GAP, groupSeqs, startres,
1213 lastres - startres, av.getAlignment(), true);
1219 // ///Editing a single sequence///////////
1223 // dragging to the right
1224 if (fixedColumns && fixedRight != -1)
1226 for (int j = lastres; j < startres; j++)
1228 insertChar(j, new SequenceI[]
1229 { seq }, fixedRight);
1234 editCommand.appendEdit(Action.INSERT_GAP, new SequenceI[]
1235 { seq }, lastres, startres - lastres, av.getAlignment(), true);
1240 // dragging to the left
1241 if (fixedColumns && fixedRight != -1)
1243 for (int j = lastres; j > startres; j--)
1245 if (!jalview.util.Comparison.isGap(seq.getCharAt(startres)))
1250 deleteChar(startres, new SequenceI[]
1251 { seq }, fixedRight);
1256 // could be a keyboard edit trying to delete none gaps
1258 for (int m = startres; m < lastres; m++)
1260 if (!jalview.util.Comparison.isGap(seq.getCharAt(m)))
1269 editCommand.appendEdit(Action.DELETE_GAP, new SequenceI[]
1270 { seq }, startres, max, av.getAlignment(), true);
1277 seqCanvas.repaint();
1280 void insertChar(int j, SequenceI[] seq, int fixedColumn)
1282 int blankColumn = fixedColumn;
1283 for (int s = 0; s < seq.length; s++)
1285 // Find the next gap before the end of the visible region boundary
1286 // If lastCol > j, theres a boundary after the gap insertion
1288 for (blankColumn = fixedColumn; blankColumn > j; blankColumn--)
1290 if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn)))
1292 // Theres a space, so break and insert the gap
1297 if (blankColumn <= j)
1299 blankColumn = fixedColumn;
1305 editCommand.appendEdit(Action.DELETE_GAP, seq, blankColumn, 1,
1306 av.getAlignment(), true);
1308 editCommand.appendEdit(Action.INSERT_GAP, seq, j, 1, av.getAlignment(),
1313 void deleteChar(int j, SequenceI[] seq, int fixedColumn)
1316 editCommand.appendEdit(Action.DELETE_GAP, seq, j, 1, av.getAlignment(),
1319 editCommand.appendEdit(Action.INSERT_GAP, seq, fixedColumn, 1,
1320 av.getAlignment(), true);
1323 // ////////////////////////////////////////
1324 // ///Everything below this is for defining the boundary of the rubberband
1325 // ////////////////////////////////////////
1326 public void doMousePressedDefineMode(MouseEvent evt)
1328 if (scrollThread != null)
1330 scrollThread.running = false;
1331 scrollThread = null;
1334 int res = findRes(evt);
1335 int seq = findSeq(evt);
1337 startWrapBlock = wrappedBlock;
1344 SequenceI sequence = av.getAlignment().getSequenceAt(seq);
1346 if (sequence == null || res > sequence.getLength())
1351 stretchGroup = av.getSelectionGroup();
1353 if (stretchGroup == null)
1355 stretchGroup = av.getAlignment().findGroup(sequence);
1356 if (stretchGroup != null && res > stretchGroup.getStartRes()
1357 && res < stretchGroup.getEndRes())
1359 av.setSelectionGroup(stretchGroup);
1363 stretchGroup = null;
1367 else if (!stretchGroup.getSequences(null).contains(sequence)
1368 || stretchGroup.getStartRes() > res
1369 || stretchGroup.getEndRes() < res)
1371 stretchGroup = null;
1373 SequenceGroup[] allGroups = av.getAlignment().findAllGroups(sequence);
1375 if (allGroups != null)
1377 for (int i = 0; i < allGroups.length; i++)
1379 if (allGroups[i].getStartRes() <= res
1380 && allGroups[i].getEndRes() >= res)
1382 stretchGroup = allGroups[i];
1387 av.setSelectionGroup(stretchGroup);
1390 // DETECT RIGHT MOUSE BUTTON IN AWT
1391 if ((evt.getModifiers() & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK)
1393 SequenceFeature[] allFeatures = findFeaturesAtRes(sequence,
1394 sequence.findPosition(res));
1396 Vector links = null;
1397 if (allFeatures != null)
1399 for (int i = 0; i < allFeatures.length; i++)
1401 if (allFeatures[i].links != null)
1405 links = new Vector();
1407 for (int j = 0; j < allFeatures[i].links.size(); j++)
1409 links.addElement(allFeatures[i].links.elementAt(j));
1414 APopupMenu popup = new APopupMenu(ap, null, links);
1416 popup.show(this, evt.getX(), evt.getY());
1422 seqCanvas.cursorX = findRes(evt);
1423 seqCanvas.cursorY = findSeq(evt);
1424 seqCanvas.repaint();
1428 // Only if left mouse button do we want to change group sizes
1430 if (stretchGroup == null)
1432 // define a new group here
1433 SequenceGroup sg = new SequenceGroup();
1434 sg.setStartRes(res);
1436 sg.addSequence(sequence, false);
1437 av.setSelectionGroup(sg);
1440 if (av.getConservationSelected())
1442 SliderPanel.setConservationSlider(ap, av.getGlobalColourScheme(),
1445 if (av.getAbovePIDThreshold())
1447 SliderPanel.setPIDSliderSource(ap, av.getGlobalColourScheme(),
1454 public void doMouseReleasedDefineMode(MouseEvent evt)
1456 if (stretchGroup == null)
1461 stretchGroup.recalcConservation(); // always do this - annotation has own
1463 if (stretchGroup.cs != null)
1465 stretchGroup.cs.alignmentChanged(stretchGroup,
1466 av.getHiddenRepSequences());
1468 if (stretchGroup.cs.conservationApplied())
1470 SliderPanel.setConservationSlider(ap, stretchGroup.cs,
1471 stretchGroup.getName());
1475 SliderPanel.setPIDSliderSource(ap, stretchGroup.cs,
1476 stretchGroup.getName());
1479 changeEndRes = false;
1480 changeStartRes = false;
1481 stretchGroup = null;
1482 PaintRefresher.Refresh(ap, av.getSequenceSetId());
1483 ap.paintAlignment(true);
1487 public void doMouseDraggedDefineMode(MouseEvent evt)
1489 int res = findRes(evt);
1490 int y = findSeq(evt);
1492 if (wrappedBlock != startWrapBlock)
1497 if (stretchGroup == null)
1502 mouseDragging = true;
1504 if (y > av.getAlignment().getHeight())
1506 y = av.getAlignment().getHeight() - 1;
1509 if (res >= av.getAlignment().getWidth())
1511 res = av.getAlignment().getWidth() - 1;
1514 if (stretchGroup.getEndRes() == res)
1516 // Edit end res position of selected group
1517 changeEndRes = true;
1519 else if (stretchGroup.getStartRes() == res)
1521 // Edit start res position of selected group
1522 changeStartRes = true;
1532 if (res > (stretchGroup.getStartRes() - 1))
1534 stretchGroup.setEndRes(res);
1537 else if (changeStartRes)
1539 if (res < (stretchGroup.getEndRes() + 1))
1541 stretchGroup.setStartRes(res);
1545 int dragDirection = 0;
1551 else if (y < oldSeq)
1556 while ((y != oldSeq) && (oldSeq > -1)
1557 && (y < av.getAlignment().getHeight()))
1559 // This routine ensures we don't skip any sequences, as the
1560 // selection is quite slow.
1561 Sequence seq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1563 oldSeq += dragDirection;
1570 Sequence nextSeq = (Sequence) av.getAlignment().getSequenceAt(oldSeq);
1572 if (stretchGroup.getSequences(null).contains(nextSeq))
1574 stretchGroup.deleteSequence(seq, false);
1580 stretchGroup.addSequence(seq, false);
1583 stretchGroup.addSequence(nextSeq, false);
1592 if (res > av.endRes || res < av.startRes || y < av.startSeq
1598 if (scrollThread != null)
1600 scrollThread.setEvent(evt);
1603 seqCanvas.repaint();
1606 public void mouseEntered(MouseEvent e)
1613 if (scrollThread != null)
1615 scrollThread.running = false;
1616 scrollThread = null;
1620 public void mouseExited(MouseEvent e)
1622 if (av.getWrapAlignment())
1627 if (mouseDragging && scrollThread == null)
1629 scrollThread = new ScrollThread();
1633 void scrollCanvas(MouseEvent evt)
1637 if (scrollThread != null)
1639 scrollThread.running = false;
1640 scrollThread = null;
1642 mouseDragging = false;
1646 if (scrollThread == null)
1648 scrollThread = new ScrollThread();
1651 mouseDragging = true;
1652 scrollThread.setEvent(evt);
1657 // this class allows scrolling off the bottom of the visible alignment
1658 class ScrollThread extends Thread
1662 boolean running = false;
1664 public ScrollThread()
1669 public void setEvent(MouseEvent e)
1674 public void stopScrolling()
1688 if (mouseDragging && evt.getY() < 0 && av.getStartSeq() > 0)
1690 running = ap.scrollUp(true);
1693 if (mouseDragging && evt.getY() >= getSize().height
1694 && av.getAlignment().getHeight() > av.getEndSeq())
1696 running = ap.scrollUp(false);
1699 if (mouseDragging && evt.getX() < 0)
1701 running = ap.scrollRight(false);
1704 else if (mouseDragging && evt.getX() >= getSize().width)
1706 running = ap.scrollRight(true);
1713 } catch (Exception ex)
1721 * modify current selection according to a received message.
1723 public void selection(SequenceGroup seqsel, ColumnSelection colsel,
1724 SelectionSource source)
1726 // TODO: fix this hack - source of messages is align viewport, but SeqPanel
1727 // handles selection messages...
1728 // TODO: extend config options to allow user to control if selections may be
1729 // shared between viewports.
1731 && (av == source || !av.followSelection || (source instanceof AlignViewport && ((AlignViewport) source)
1732 .getSequenceSetId().equals(av.getSequenceSetId()))))
1738 * Check for selection in a view of which this one is a dna/protein
1741 if (selectionFromTranslation(seqsel, colsel, source))
1746 // do we want to thread this ? (contention with seqsel and colsel locks, I
1748 // rules are: colsel is copied if there is a real intersection between
1749 // sequence selection
1750 boolean repaint = false, copycolsel = true;
1751 if (av.getSelectionGroup() == null || !av.isSelectionGroupChanged(true))
1753 SequenceGroup sgroup = null;
1754 if (seqsel != null && seqsel.getSize() > 0)
1756 if (av.getAlignment() == null)
1759 .println("Selection message: alignviewport av SeqSetId="
1760 + av.getSequenceSetId() + " ViewId="
1762 + " 's alignment is NULL! returning immediatly.");
1765 sgroup = seqsel.intersect(av.getAlignment(),
1766 (av.hasHiddenRows()) ? av.getHiddenRepSequences() : null);
1767 if ((sgroup == null || sgroup.getSize() == 0)
1768 && (colsel == null || colsel.size() == 0))
1770 // don't copy columns if the region didn't intersect.
1774 if (sgroup != null && sgroup.getSize() > 0)
1776 av.setSelectionGroup(sgroup);
1780 av.setSelectionGroup(null);
1782 repaint = av.isSelectionGroupChanged(true);
1785 && (av.getColumnSelection() == null || !av
1786 .isColSelChanged(true)))
1788 // the current selection is unset or from a previous message
1789 // so import the new colsel.
1790 if (colsel == null || colsel.size() == 0)
1792 if (av.getColumnSelection() != null)
1794 av.getColumnSelection().clear();
1799 // TODO: shift colSel according to the intersecting sequences
1800 if (av.getColumnSelection() == null)
1802 av.setColumnSelection(new ColumnSelection(colsel));
1806 av.getColumnSelection().setElementsFrom(colsel);
1809 repaint |= av.isColSelChanged(true);
1812 && av.hasHiddenColumns()
1813 && (av.getColumnSelection() == null || av.getColumnSelection()
1814 .getHiddenColumns() == null))
1816 System.err.println("Bad things");
1820 ap.scalePanelHolder.repaint();
1826 * scroll to the given row/column - or nearest visible location
1831 public void scrollTo(int row, int column)
1834 row = row < 0 ? ap.av.startSeq : row;
1835 column = column < 0 ? ap.av.startRes : column;
1836 ap.scrollTo(column, column, row, true, true);
1840 * scroll to the given row - or nearest visible location
1844 public void scrollToRow(int row)
1847 row = row < 0 ? ap.av.startSeq : row;
1848 ap.scrollTo(ap.av.startRes, ap.av.startRes, row, true, true);
1852 * scroll to the given column - or nearest visible location
1856 public void scrollToColumn(int column)
1859 column = column < 0 ? ap.av.startRes : column;
1860 ap.scrollTo(column, column, ap.av.startSeq, true, true);
1864 * If this panel is a cdna/protein translation view of the selection source,
1865 * tries to map the source selection to a local one, and returns true. Else
1872 protected boolean selectionFromTranslation(SequenceGroup seqsel,
1873 ColumnSelection colsel, SelectionSource source)
1875 if (!(source instanceof AlignViewportI)) {
1878 final AlignViewportI sourceAv = (AlignViewportI) source;
1879 if (sourceAv.getCodingComplement() != av && av.getCodingComplement() != sourceAv)
1885 * Map sequence selection
1887 SequenceGroup sg = MappingUtils.mapSequenceGroup(seqsel, sourceAv, av);
1888 av.setSelectionGroup(sg);
1889 av.isSelectionGroupChanged(true);
1892 * Map column selection
1894 ColumnSelection cs = MappingUtils.mapColumnSelection(colsel, sourceAv,
1896 av.setColumnSelection(cs);
1897 av.isColSelChanged(true);
1899 firePropertyChange("alignment", null, av.getAlignment().getSequences());
1900 // PaintRefresher.Refresh(this, av.getSequenceSetId());