From 45a65f09ecbfeb1d24a463385af1356fa55f2210 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Mon, 8 Oct 2018 09:09:27 +0100 Subject: [PATCH] JAL-1244 editSequence extract method refactoring --- src/jalview/gui/SeqPanel.java | 407 +++++++++++++++++++++-------------------- 1 file changed, 211 insertions(+), 196 deletions(-) diff --git a/src/jalview/gui/SeqPanel.java b/src/jalview/gui/SeqPanel.java index 92a5030..d0aa93f 100644 --- a/src/jalview/gui/SeqPanel.java +++ b/src/jalview/gui/SeqPanel.java @@ -92,9 +92,9 @@ public class SeqPanel extends JPanel */ private int lastMouseSeq; - protected int lastres; + protected int editLastRes; - protected int startseq; + protected int editStartSeq; protected AlignViewport av; @@ -303,8 +303,8 @@ public class SeqPanel extends JPanel /* * Tidy up come what may... */ - startseq = -1; - lastres = -1; + editStartSeq = -1; + editLastRes = -1; editingSeqs = false; groupEditing = false; keyboardNo1 = null; @@ -532,8 +532,8 @@ public class SeqPanel extends JPanel void insertGapAtCursor(boolean group) { groupEditing = group; - startseq = seqCanvas.cursorY; - lastres = seqCanvas.cursorX; + editStartSeq = seqCanvas.cursorY; + editLastRes = seqCanvas.cursorX; editSequence(true, false, seqCanvas.cursorX + getKeyboardNo1()); endEditing(); } @@ -541,8 +541,8 @@ public class SeqPanel extends JPanel void deleteGapAtCursor(boolean group) { groupEditing = group; - startseq = seqCanvas.cursorY; - lastres = seqCanvas.cursorX + getKeyboardNo1(); + editStartSeq = seqCanvas.cursorY; + editLastRes = seqCanvas.cursorX + getKeyboardNo1(); editSequence(false, false, seqCanvas.cursorX); endEditing(); } @@ -551,8 +551,8 @@ public class SeqPanel extends JPanel { // TODO not called - delete? groupEditing = group; - startseq = seqCanvas.cursorY; - lastres = seqCanvas.cursorX; + editStartSeq = seqCanvas.cursorY; + editLastRes = seqCanvas.cursorX; editSequence(false, true, seqCanvas.cursorX + getKeyboardNo1()); endEditing(); } @@ -680,13 +680,13 @@ public class SeqPanel extends JPanel if ((seq < av.getAlignment().getHeight()) && (res < av.getAlignment().getSequenceAt(seq).getLength())) { - startseq = seq; - lastres = res; + editStartSeq = seq; + editLastRes = res; } else { - startseq = -1; - lastres = -1; + editStartSeq = -1; + editLastRes = -1; } return; @@ -1145,12 +1145,12 @@ public class SeqPanel extends JPanel res = 0; } - if ((lastres == -1) || (lastres == res)) + if ((editLastRes == -1) || (editLastRes == res)) { return; } - if ((res < av.getAlignment().getWidth()) && (res < lastres)) + if ((res < av.getAlignment().getWidth()) && (res < editLastRes)) { // dragLeft, delete gap editSequence(false, false, res); @@ -1199,7 +1199,7 @@ public class SeqPanel extends JPanel boolean fixedColumns = false; SequenceGroup sg = av.getSelectionGroup(); - SequenceI seq = av.getAlignment().getSequenceAt(startseq); + final SequenceI seq = av.getAlignment().getSequenceAt(editStartSeq); // No group, but the sequence may represent a group if (!groupEditing && av.hasHiddenRows()) @@ -1263,10 +1263,10 @@ public class SeqPanel extends JPanel fixedLeft = sg.getStartRes(); fixedRight = sg.getEndRes(); - if ((startres < fixedLeft && lastres >= fixedLeft) - || (startres >= fixedLeft && lastres < fixedLeft) - || (startres > fixedRight && lastres <= fixedRight) - || (startres <= fixedRight && lastres > fixedRight)) + if ((startres < fixedLeft && editLastRes >= fixedLeft) + || (startres >= fixedLeft && editLastRes < fixedLeft) + || (startres > fixedRight && editLastRes <= fixedRight) + || (startres <= fixedRight && editLastRes > fixedRight)) { endEditing(); return; @@ -1292,8 +1292,8 @@ public class SeqPanel extends JPanel int y2 = av.getAlignment().getHiddenColumns() .getNextHiddenBoundary(false, startres); - if ((insertGap && startres > y1 && lastres < y1) - || (!insertGap && startres < y2 && lastres > y2)) + if ((insertGap && startres > y1 && editLastRes < y1) + || (!insertGap && startres < y2 && editLastRes > y2)) { endEditing(); return; @@ -1314,251 +1314,266 @@ public class SeqPanel extends JPanel } } - SequenceI[] seqs = new SequenceI[] { seq }; + boolean endEditing = doEditSequence(insertGap, editSeq, startres, + fixedRight, fixedColumns, sg); - boolean endEditing = false; + /* + * report what actually happened (might be less than + * what was requested), by inspecting the edit command added + */ + String msg = getEditStatusMessage(editCommand); + ap.alignFrame.statusBar.setText(msg == null ? " " : msg); + if (endEditing) + { + endEditing(); + } - try + editLastRes = startres; + seqCanvas.repaint(); + } + + /** + * A helper method that performs the requested editing to insert or delete + * gaps (if possible). Answers true if the edit was successful, false if could + * only be performed in part or not at all. Failure may occur in 'locked edit' + * mode, when an insertion requires a matching gapped position (or column) to + * delete, and deletion requires a gapped position (or column) to remove. + * + * @param insertGap + * @param editSeq + * @param startres + * @param fixedRight + * @param fixedColumns + * @param sg + * @return + */ + protected boolean doEditSequence(boolean insertGap, boolean editSeq, + final int startres, int fixedRight, boolean fixedColumns, + SequenceGroup sg) + { + final SequenceI seq = av.getAlignment().getSequenceAt(editStartSeq); + SequenceI[] seqs = new SequenceI[] { seq }; + + if (groupEditing) { - if (groupEditing) + List vseqs = sg.getSequences(av.getHiddenRepSequences()); + int g, groupSize = vseqs.size(); + SequenceI[] groupSeqs = new SequenceI[groupSize]; + for (g = 0; g < groupSeqs.length; g++) { - List vseqs = sg.getSequences(av.getHiddenRepSequences()); - int g, groupSize = vseqs.size(); - SequenceI[] groupSeqs = new SequenceI[groupSize]; - for (g = 0; g < groupSeqs.length; g++) + groupSeqs[g] = vseqs.get(g); + } + + // drag to right + if (insertGap) + { + // If the user has selected the whole sequence, and is dragging to + // the right, we can still extend the alignment and selectionGroup + if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight + && sg.getEndRes() == av.getAlignment().getWidth() - 1) { - groupSeqs[g] = vseqs.get(g); + sg.setEndRes( + av.getAlignment().getWidth() + startres - editLastRes); + fixedRight = sg.getEndRes(); } - // drag to right - if (insertGap) + // Is it valid with fixed columns?? + // Find the next gap before the end + // of the visible region boundary + boolean blank = false; + for (; fixedRight > editLastRes; fixedRight--) { - // If the user has selected the whole sequence, and is dragging to - // the right, we can still extend the alignment and selectionGroup - if (sg.getStartRes() == 0 && sg.getEndRes() == fixedRight - && sg.getEndRes() == av.getAlignment().getWidth() - 1) - { - sg.setEndRes(av.getAlignment().getWidth() + startres - lastres); - fixedRight = sg.getEndRes(); - } + blank = true; - // Is it valid with fixed columns?? - // Find the next gap before the end - // of the visible region boundary - boolean blank = false; - for (; fixedRight > lastres; fixedRight--) + for (g = 0; g < groupSize; g++) { - blank = true; - - for (g = 0; g < groupSize; g++) + for (int j = 0; j < startres - editLastRes; j++) { - for (int j = 0; j < startres - lastres; j++) + if (!Comparison + .isGap(groupSeqs[g].getCharAt(fixedRight - j))) { - if (!Comparison - .isGap(groupSeqs[g].getCharAt(fixedRight - j))) - { - blank = false; - break; - } + blank = false; + break; } } - if (blank) - { - break; - } } + if (blank) + { + break; + } + } - if (!blank) + if (!blank) + { + if (sg.getSize() == av.getAlignment().getHeight()) { - if (sg.getSize() == av.getAlignment().getHeight()) + if ((av.hasHiddenColumns() + && startres < av.getAlignment().getHiddenColumns() + .getNextHiddenBoundary(false, startres))) { - if ((av.hasHiddenColumns() - && startres < av.getAlignment().getHiddenColumns() - .getNextHiddenBoundary(false, startres))) - { - endEditing = true; - return; - } + return true; + } - int alWidth = av.getAlignment().getWidth(); - if (av.hasHiddenRows()) + int alWidth = av.getAlignment().getWidth(); + if (av.hasHiddenRows()) + { + int hwidth = av.getAlignment().getHiddenSequences() + .getWidth(); + if (hwidth > alWidth) { - int hwidth = av.getAlignment().getHiddenSequences() - .getWidth(); - if (hwidth > alWidth) - { - alWidth = hwidth; - } + alWidth = hwidth; } - // We can still insert gaps if the selectionGroup - // contains all the sequences - sg.setEndRes(sg.getEndRes() + startres - lastres); - fixedRight = alWidth + startres - lastres; - } - else - { - endEditing = true; - return; } + // We can still insert gaps if the selectionGroup + // contains all the sequences + sg.setEndRes(sg.getEndRes() + startres - editLastRes); + fixedRight = alWidth + startres - editLastRes; + } + else + { + return true; } } + } - // drag to left - else if (!insertGap) - { - // / Are we able to delete? - // ie are all columns blank? + // drag to left + else if (!insertGap) + { + // / Are we able to delete? + // ie are all columns blank? - for (g = 0; g < groupSize; g++) + for (g = 0; g < groupSize; g++) + { + for (int j = startres; j < editLastRes; j++) { - for (int j = startres; j < lastres; j++) + if (groupSeqs[g].getLength() <= j) { - if (groupSeqs[g].getLength() <= j) - { - continue; - } + continue; + } - if (!Comparison.isGap(groupSeqs[g].getCharAt(j))) - { - // Not a gap, block edit not valid - endEditing = true; - return; - } + if (!Comparison.isGap(groupSeqs[g].getCharAt(j))) + { + // Not a gap, block edit not valid + return true; } } } + } - if (insertGap) + if (insertGap) + { + // dragging to the right + if (fixedColumns && fixedRight != -1) { - // dragging to the right - if (fixedColumns && fixedRight != -1) + for (int j = editLastRes; j < startres; j++) { - for (int j = lastres; j < startres; j++) - { - insertGap(j, groupSeqs, fixedRight); - } + insertGap(j, groupSeqs, fixedRight); } - else + } + else + { + appendEdit(Action.INSERT_GAP, groupSeqs, startres, + startres - editLastRes, false); + } + } + else + { + // dragging to the left + if (fixedColumns && fixedRight != -1) + { + for (int j = editLastRes; j > startres; j--) { - appendEdit(Action.INSERT_GAP, groupSeqs, startres, - startres - lastres, false); + deleteChar(startres, groupSeqs, fixedRight); } } else { - // dragging to the left - if (fixedColumns && fixedRight != -1) + appendEdit(Action.DELETE_GAP, groupSeqs, startres, + editLastRes - startres, false); + } + } + } + else + { + /* + * editing a single sequence + */ + if (insertGap) + { + // dragging to the right + if (fixedColumns && fixedRight != -1) + { + for (int j = editLastRes; j < startres; j++) { - for (int j = lastres; j > startres; j--) + if (!insertGap(j, seqs, fixedRight)) { - deleteChar(startres, groupSeqs, fixedRight); + /* + * e.g. cursor mode command asked for + * more inserts than are possible + */ + return true; } } - else - { - appendEdit(Action.DELETE_GAP, groupSeqs, startres, - lastres - startres, false); - } + } + else + { + appendEdit(Action.INSERT_GAP, seqs, editLastRes, + startres - editLastRes, false); } } else { - /* - * editing a single sequence - */ - if (insertGap) + if (!editSeq) { - // dragging to the right + // dragging to the left if (fixedColumns && fixedRight != -1) { - for (int j = lastres; j < startres; j++) + for (int j = editLastRes; j > startres; j--) { - if (!insertGap(j, seqs, fixedRight)) + if (!Comparison.isGap(seq.getCharAt(startres))) { - /* - * e.g. cursor mode command asked for - * more inserts than are possible - */ - endEditing = true; - break; + return true; } + deleteChar(startres, seqs, fixedRight); } } else { - appendEdit(Action.INSERT_GAP, seqs, lastres, startres - lastres, - false); - } - } - else - { - if (!editSeq) - { - // dragging to the left - if (fixedColumns && fixedRight != -1) + // could be a keyboard edit trying to delete none gaps + int max = 0; + for (int m = startres; m < editLastRes; m++) { - for (int j = lastres; j > startres; j--) + if (!Comparison.isGap(seq.getCharAt(m))) { - if (!Comparison.isGap(seq.getCharAt(startres))) - { - endEditing = true; - break; - } - deleteChar(startres, seqs, fixedRight); + break; } + max++; } - else + if (max > 0) { - // could be a keyboard edit trying to delete none gaps - int max = 0; - for (int m = startres; m < lastres; m++) - { - if (!Comparison.isGap(seq.getCharAt(m))) - { - break; - } - max++; - } - if (max > 0) - { - appendEdit(Action.DELETE_GAP, seqs, startres, max, false); - } + appendEdit(Action.DELETE_GAP, seqs, startres, max, false); } } - else - {// insertGap==false AND editSeq==TRUE; - if (fixedColumns && fixedRight != -1) - { - for (int j = lastres; j < startres; j++) - { - insertGap(j, seqs, fixedRight); - } - } - else + } + else + {// insertGap==false AND editSeq==TRUE; + if (fixedColumns && fixedRight != -1) + { + for (int j = editLastRes; j < startres; j++) { - appendEdit(Action.INSERT_NUC, seqs, lastres, - startres - lastres, false); + insertGap(j, seqs, fixedRight); } } + else + { + appendEdit(Action.INSERT_NUC, seqs, editLastRes, + startres - editLastRes, false); + } } } - } finally - { - /* - * report what actually happened (might be less than - * what was requested) - */ - String msg = getEditStatusMessage(editCommand); - ap.alignFrame.statusBar.setText(msg == null ? " " : msg); - - if (endEditing) - { - endEditing(); - } - - lastres = startres; - seqCanvas.repaint(); } + + return false; } /** -- 1.7.10.2