From 4059c82f431d3f98931b290d9a72da2a54a0fc95 Mon Sep 17 00:00:00 2001 From: amwaterhouse Date: Tue, 24 Oct 2006 11:24:20 +0000 Subject: [PATCH] EditCommand implemented --- src/jalview/gui/SeqPanel.java | 516 ++++++++++++++++++++++------------------- 1 file changed, 271 insertions(+), 245 deletions(-) diff --git a/src/jalview/gui/SeqPanel.java b/src/jalview/gui/SeqPanel.java index 5c96e79..b57305a 100755 --- a/src/jalview/gui/SeqPanel.java +++ b/src/jalview/gui/SeqPanel.java @@ -19,14 +19,12 @@ package jalview.gui; import jalview.datamodel.*; - +import jalview.commands.EditCommand; import jalview.schemes.*; import java.awt.*; import java.awt.event.*; - import javax.swing.*; - import java.util.Vector; @@ -40,78 +38,78 @@ public class SeqPanel extends JPanel implements MouseListener, MouseMotionListener, MouseWheelListener { - /** DOCUMENT ME!! */ - public SeqCanvas seqCanvas; - - /** DOCUMENT ME!! */ - public AlignmentPanel ap; - protected int lastres; - protected int startseq; - protected AlignViewport av; - - // if character is inserted or deleted, we will need to recalculate the conservation - boolean seqEditOccurred = false; - ScrollThread scrollThread = null; - boolean mouseDragging = false; - boolean editingSeqs = false; - boolean groupEditing = false; - - ////////////////////////////////////////// - /////Everything below this is for defining the boundary of the rubberband - ////////////////////////////////////////// - int oldSeq = -1; - boolean changeEndSeq = false; - boolean changeStartSeq = false; - boolean changeEndRes = false; - boolean changeStartRes = false; - SequenceGroup stretchGroup = null; - boolean remove = false; - - Point lastMousePress; - boolean mouseWheelPressed = false; - StringBuffer keyboardNo1; - StringBuffer keyboardNo2; - - java.net.URL linkImageURL; - - StringBuffer tooltipText = new StringBuffer(""); - String tmpString; - - /** - * Creates a new SeqPanel object. - * - * @param avp DOCUMENT ME! - * @param p DOCUMENT ME! - */ - public SeqPanel(AlignViewport avp, AlignmentPanel p) + /** DOCUMENT ME!! */ + public SeqCanvas seqCanvas; + + /** DOCUMENT ME!! */ + public AlignmentPanel ap; + protected int lastres; + protected int startseq; + protected AlignViewport av; + + ScrollThread scrollThread = null; + boolean mouseDragging = false; + boolean editingSeqs = false; + boolean groupEditing = false; + + ////////////////////////////////////////// + /////Everything below this is for defining the boundary of the rubberband + ////////////////////////////////////////// + int oldSeq = -1; + boolean changeEndSeq = false; + boolean changeStartSeq = false; + boolean changeEndRes = false; + boolean changeStartRes = false; + SequenceGroup stretchGroup = null; + boolean remove = false; + + Point lastMousePress; + boolean mouseWheelPressed = false; + StringBuffer keyboardNo1; + StringBuffer keyboardNo2; + + java.net.URL linkImageURL; + + StringBuffer tooltipText = new StringBuffer(""); + String tmpString; + + EditCommand editCommand; + + /** + * Creates a new SeqPanel object. + * + * @param avp DOCUMENT ME! + * @param p DOCUMENT ME! + */ + public SeqPanel(AlignViewport avp, AlignmentPanel p) + { + linkImageURL = getClass().getResource("/images/link.gif"); + ToolTipManager.sharedInstance().registerComponent(this); + ToolTipManager.sharedInstance().setInitialDelay(0); + ToolTipManager.sharedInstance().setDismissDelay(10000); + this.av = avp; + setBackground(Color.white); + + seqCanvas = new SeqCanvas(avp); + setLayout(new BorderLayout()); + add(seqCanvas, BorderLayout.CENTER); + + ap = p; + + if (!av.isDataset()) { - linkImageURL = getClass().getResource("/images/link.gif"); - ToolTipManager.sharedInstance().registerComponent(this); - ToolTipManager.sharedInstance().setInitialDelay(0); - ToolTipManager.sharedInstance().setDismissDelay(10000); - this.av = avp; - setBackground(Color.white); - - seqCanvas = new SeqCanvas(avp); - setLayout(new BorderLayout()); - add(seqCanvas, BorderLayout.CENTER); - - ap = p; - - if(!av.isDataset()) - { - addMouseMotionListener(this); - addMouseListener(this); - addMouseWheelListener(this); - } + addMouseMotionListener(this); + addMouseListener(this); + addMouseWheelListener(this); } + } - int startWrapBlock=-1; - int wrappedBlock=-1; - int findRes(MouseEvent evt) - { - int res = 0; - int x = evt.getX(); + int startWrapBlock = -1; + int wrappedBlock = -1; + int findRes(MouseEvent evt) + { + int res = 0; + int x = evt.getX(); if (av.wrapAlignment) { @@ -123,60 +121,89 @@ public class SeqPanel extends JPanel implements MouseListener, int cHeight = av.getAlignment().getHeight() * av.charHeight + hgap + seqCanvas.getAnnotationHeight(); - int y = evt.getY(); - y -= hgap; - x -= seqCanvas.LABEL_WEST; - + int y = evt.getY(); + y -= hgap; + x -= seqCanvas.LABEL_WEST; - int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth()); - if(cwidth<1) - return 0; + int cwidth = seqCanvas.getWrappedCanvasWidth(this.getWidth()); + if (cwidth < 1) + return 0; - wrappedBlock = y / cHeight; - wrappedBlock += av.getStartRes() / cwidth; + wrappedBlock = y / cHeight; + wrappedBlock += av.getStartRes() / cwidth; - res = wrappedBlock * cwidth + x / av.getCharWidth(); + res = wrappedBlock * cwidth + x / av.getCharWidth(); } else { - res = (x / av.getCharWidth()) + av.getStartRes(); + res = (x / av.getCharWidth()) + av.getStartRes(); } - if(av.hasHiddenColumns) - res = av.getColumnSelection().adjustForHiddenColumns(res); + if (av.hasHiddenColumns) + res = av.getColumnSelection().adjustForHiddenColumns(res); return res; - } + } - int findSeq(MouseEvent evt) - { - int seq = 0; - int y = evt.getY(); + int findSeq(MouseEvent evt) + { + int seq = 0; + int y = evt.getY(); - if (av.wrapAlignment) - { - int hgap = av.charHeight; - if (av.scaleAboveWrapped) - hgap += av.charHeight; + if (av.wrapAlignment) + { + int hgap = av.charHeight; + if (av.scaleAboveWrapped) + hgap += av.charHeight; - int cHeight = av.getAlignment().getHeight() * av.charHeight - + hgap + seqCanvas.getAnnotationHeight(); + int cHeight = av.getAlignment().getHeight() * av.charHeight + + hgap + seqCanvas.getAnnotationHeight(); - y -= hgap; + y -= hgap; - seq = Math.min( (y % cHeight) / av.getCharHeight(), - av.alignment.getHeight() -1); - } - else - { - seq = Math.min( (y / av.getCharHeight()) + av.getStartSeq(), - av.alignment.getHeight() -1); - } + seq = Math.min( (y % cHeight) / av.getCharHeight(), + av.alignment.getHeight() - 1); + } + else + { + seq = Math.min( (y / av.getCharHeight()) + av.getStartSeq(), + av.alignment.getHeight() - 1); + } - return seq; - } + return seq; + } + + SequenceFeature[] findFeaturesAtRes(SequenceI sequence, int res) + { + Vector tmp = new Vector(); + SequenceFeature[] features = sequence.getSequenceFeatures(); + + if (features != null) + { + for (int i = 0; i < features.length; i++) + { + if (av.featuresDisplayed == null + || !av.featuresDisplayed.containsKey(features[i].getType())) + continue; + + if ( (features[i].getBegin() <= sequence.findPosition(res)) && + (features[i].getEnd() >= sequence.findPosition(res))) + { + tmp.addElement(features[i]); + } + } + } + + features = new SequenceFeature[tmp.size()]; + for(int i=0; i 0) + { + ap.alignFrame.addHistoryItem(editCommand); + av.firePropertyChange("alignment", null, + av.getAlignment().getSequences()); + } + startseq = -1; lastres = -1; - seqEditOccurred = false; editingSeqs = false; groupEditing = false; keyboardNo1 = null; keyboardNo2 = null; + editCommand = null; } void setCursorRow() @@ -373,24 +407,20 @@ public class SeqPanel extends JPanel implements MouseListener, void insertGapAtCursor(boolean group) { - ap.alignFrame.addHistoryItem(new HistoryItem("Edit Sequence", - av.alignment, HistoryItem.EDIT)); groupEditing = group; startseq = seqCanvas.cursorY; lastres = seqCanvas.cursorX; editSequence(true, seqCanvas.cursorX+getKeyboardNo(keyboardNo1)); - editOccurred(); + endEditing(); } void deleteGapAtCursor(boolean group) { - ap.alignFrame.addHistoryItem(new HistoryItem("Edit Sequence", - av.alignment, HistoryItem.EDIT)); groupEditing = group; startseq = seqCanvas.cursorY; lastres = seqCanvas.cursorX+getKeyboardNo(keyboardNo1); editSequence(false, seqCanvas.cursorX); - editOccurred(); + endEditing(); } void numberPressed(char value) @@ -429,7 +459,7 @@ public class SeqPanel extends JPanel implements MouseListener, return; } - editOccurred(); + endEditing(); ap.repaint(); } @@ -466,17 +496,12 @@ public class SeqPanel extends JPanel implements MouseListener, return; } - - int seq = findSeq(evt); int res = findRes(evt); if(seq<0 || res<0) return; - ap.alignFrame.addHistoryItem(new HistoryItem("Edit Sequence", - av.alignment, HistoryItem.EDIT)); - if ((seq < av.getAlignment().getHeight()) && (res < av.getAlignment().getSequenceAt(seq).getLength())) { @@ -532,20 +557,13 @@ public class SeqPanel extends JPanel implements MouseListener, { tooltipText.setLength(6); // Cuts the buffer back to - SequenceFeature [] features = sequence.getDatasetSequence().getSequenceFeatures(); + SequenceFeature[] features = findFeaturesAtRes( + sequence.getDatasetSequence(), res); + if(features!=null) { for (int i = 0; i < features.length; i++) { - - if ( (features[i].getBegin() <= sequence.findPosition(res)) && - (features[i].getEnd() >= sequence.findPosition(res))) - { - if(av.featuresDisplayed==null - || !av.featuresDisplayed.containsKey(features[i].getType())) - continue; - - if (features[i].getType().equals("disulfide bond")) { if (features[i].getBegin() == sequence.findPosition(res) @@ -621,8 +639,8 @@ public class SeqPanel extends JPanel implements MouseListener, tooltipText.append(" "); } - } - else if(features[i].begin==0 && features[i].end==0) + + // else if(features[i].begin==0 && features[i].end==0) { // seqSpecific.append(features[i].featureGroup+": " // + features[i].getType()+" " @@ -632,7 +650,6 @@ public class SeqPanel extends JPanel implements MouseListener, } - if(tooltipText.length()==6) // { @@ -796,9 +813,20 @@ public class SeqPanel extends JPanel implements MouseListener, SequenceI seq = av.alignment.getSequenceAt(startseq); StringBuffer message = new StringBuffer(); if (groupEditing) - message.append("Edit group:"); + { + message.append("Edit group:"); + if (editCommand == null) + editCommand = new EditCommand("Edit Group"); + } else - message.append("Edit sequence: "+seq.getName()); + { + message.append("Edit sequence: " + seq.getName()); + String label = seq.getName(); + if(label.length()>10) + label = label.substring(0,10); + if(editCommand==null) + editCommand = new EditCommand("Edit "+label); + } if(insertGap) message.append(" insert "); @@ -984,66 +1012,100 @@ public class SeqPanel extends JPanel implements MouseListener, } } - - for (g = 0; g < groupSize; g++) - { if (insertGap) { // dragging to the right - for (int j = lastres; j < startres; j++) + if (fixedColumns && fixedRight != -1) { - if (fixedColumns && fixedRight != -1) + for (int j = lastres; j < startres; j++) { - insertChar(j, groupSeqs[g], fixedRight); + insertChar(j, groupSeqs, fixedRight); } - else - insertChar(j, groupSeqs[g]); + } + else + { + editCommand.appendEdit(EditCommand.INSERT_GAP, + groupSeqs, + startres, startres-lastres, + av.getGapCharacter(), + true); } } else { // dragging to the left - for (int j = lastres; j > startres; j--) + if (fixedColumns && fixedRight != -1) { - if (fixedColumns && fixedRight != -1) + for (int j = lastres; j > startres; j--) { - deleteChar(startres, groupSeqs[g], fixedRight); - } - else - { - deleteChar(startres, groupSeqs[g]); + deleteChar(startres, groupSeqs, fixedRight); } } + else + editCommand.appendEdit(EditCommand.DELETE_GAP, + groupSeqs, + startres, lastres - startres, + av.getGapCharacter(), + true); + } - } } else /////Editing a single sequence/////////// { if (insertGap) { // dragging to the right - for (int j = lastres; j < startres; j++) + if (fixedColumns && fixedRight != -1) { - if (fixedColumns && fixedRight != -1) + for (int j = lastres; j < startres; j++) { - insertChar(j, seq, fixedRight); + insertChar(j, new SequenceI[]{seq}, fixedRight); } - else - insertChar(j, seq); + } + else + { + editCommand.appendEdit(EditCommand.INSERT_GAP, + new SequenceI[] + {seq}, + lastres, startres-lastres, + av.getGapCharacter(), + true); } } else { // dragging to the left - for (int j = lastres; j > startres; j--) + if (fixedColumns && fixedRight != -1) { - if (fixedColumns && fixedRight != -1) + for (int j = lastres; j > startres; j--) { - deleteChar(startres, seq, fixedRight); + if (!jalview.util.Comparison.isGap(seq.getCharAt(startres))) + { + endEditing(); + break; + } + deleteChar(startres, new SequenceI[]{seq}, fixedRight); } - else + } + else + { + //could be a keyboard edit trying to delete none gaps + int max=0; + for(int m = startres; m0) + { + editCommand.appendEdit(EditCommand.DELETE_GAP, + new SequenceI[] + {seq}, + startres, max, + av.getGapCharacter(), + true); } } } @@ -1054,84 +1116,55 @@ public class SeqPanel extends JPanel implements MouseListener, } - /** - * DOCUMENT ME! - * - * @param j DOCUMENT ME! - * @param seq DOCUMENT ME! - */ - void insertChar(int j, SequenceI seq) - { - seq.insertCharAt(j, av.getGapCharacter()); - seqEditOccurred = true; - } - void insertChar(int j, SequenceI seq, int fixedColumn) + void insertChar(int j, SequenceI [] seq, int fixedColumn) { - //Find the next gap before the end of the visible region boundary - //If lastCol > j, theres a boundary after the gap insertion int blankColumn = fixedColumn; - for (blankColumn = fixedColumn; blankColumn > j; blankColumn--) + for(int s=0; s j, theres a boundary after the gap insertion + + for (blankColumn = fixedColumn; blankColumn > j; blankColumn--) { - //Theres a space, so break and insert the gap - break; + if (jalview.util.Comparison.isGap(seq[s].getCharAt(blankColumn))) + { + //Theres a space, so break and insert the gap + break; + } } - } - if (blankColumn <= j) - { - blankColumn = fixedColumn; - endEditing(); - return; - } - - if (!jalview.util.Comparison.isGap(seq.getCharAt(blankColumn))) - { - //Just Checking - System.out.println("Tried removing residue (INSERT)"+seq.getCharAt(fixedColumn)); - return; + if (blankColumn <= j) + { + blankColumn = fixedColumn; + endEditing(); + return; + } } - seq.deleteCharAt(blankColumn); - seq.insertCharAt(j, av.getGapCharacter()); - seqEditOccurred = true; - } + editCommand.appendEdit(EditCommand.DELETE_GAP, + seq, + blankColumn, 1, av.getGapCharacter(), true); - void deleteChar(int j, SequenceI seq, int fixedColumn) - { - if (!jalview.util.Comparison.isGap(seq.getCharAt(j))) - { - ap.alignFrame.statusBar.setText( - "End editing: Tried removing residue " + seq.getCharAt(j)); - return; - } + editCommand.appendEdit(EditCommand.INSERT_GAP, + seq, + j, 1, av.getGapCharacter(), + true); - seq.deleteCharAt(j); - seq.insertCharAt(fixedColumn, av.getGapCharacter()); - seqEditOccurred = true; } - /** - * DOCUMENT ME! - * - * @param j DOCUMENT ME! - * @param seq DOCUMENT ME! - */ - void deleteChar(int j, SequenceI seq) + void deleteChar(int j, SequenceI [] seq, int fixedColumn) { - if (!jalview.util.Comparison.isGap(seq.getCharAt(j))) - { - ap.alignFrame.statusBar.setText( - "End editing: Tried removing residue " + seq.getCharAt(j)); - return; - } - seq.deleteCharAt(j); - seqEditOccurred = true; - seqCanvas.repaint(); + editCommand.appendEdit(EditCommand.DELETE_GAP, + seq, + j, 1, av.getGapCharacter(), true); + + editCommand.appendEdit(EditCommand.INSERT_GAP, + seq, + fixedColumn, 1, av.getGapCharacter(), true); } + /** * DOCUMENT ME! * @@ -1168,7 +1201,20 @@ public class SeqPanel extends JPanel implements MouseListener, } public void mouseClicked(MouseEvent evt) - {} + { + if(evt.getClickCount()>1) + { + + SequenceFeature[] features = findFeaturesAtRes( + av.alignment.getSequenceAt( findSeq(evt) ).getDatasetSequence(), + findRes(evt) + ); + for(int f=0; f