1 package org.herac.tuxguitar.gui.undo.undoables.measure; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 import java.util.List; 6 7 import org.herac.tuxguitar.gui.TuxGuitar; 8 import org.herac.tuxguitar.gui.undo.CannotRedoException; 9 import org.herac.tuxguitar.gui.undo.CannotUndoException; 10 import org.herac.tuxguitar.gui.undo.UndoableEdit; 11 import org.herac.tuxguitar.gui.undo.undoables.UndoableCaretHelper; 12 import org.herac.tuxguitar.song.helpers.TGSongSegment; 13 import org.herac.tuxguitar.song.helpers.TGSongSegmentHelper; 14 import org.herac.tuxguitar.song.models.TGMarker; UndoableAddMeasure()15 16 public class UndoableRemoveMeasure implements UndoableEdit{ 17 private int doAction; 18 private UndoableCaretHelper undoCaret; 19 private UndoableCaretHelper redoCaret; 20 private TGSongSegment tracksMeasures; 21 private UndoMarkers undoMarkers; 22 private int n1; 23 private int n2; 24 25 public UndoableRemoveMeasure(int n1,int n2){ 26 this.doAction = UNDO_ACTION; 27 this.undoCaret = new UndoableCaretHelper(); 28 this.n1 = n1; 29 this.n2 = n2; 30 this.tracksMeasures = new TGSongSegmentHelper(TuxGuitar.instance().getSongManager()).copyMeasures(n1,n2); 31 this.undoMarkers = new UndoMarkers(); 32 } 33 34 public void redo() throws CannotRedoException { 35 if(!canRedo()){ 36 throw new CannotRedoException(); 37 } 38 TuxGuitar.instance().getSongManager().removeMeasureHeaders(this.n1,this.n2); 39 TuxGuitar.instance().fireUpdate(); 40 41 this.redoCaret.update(); 42 this.doAction = UNDO_ACTION; 43 } 44 45 public void undo() throws CannotUndoException { 46 if(!canUndo()){ 47 throw new CannotUndoException(); 48 } 49 new TGSongSegmentHelper(TuxGuitar.instance().getSongManager()).insertMeasures(this.tracksMeasures.clone(TuxGuitar.instance().getSongManager().getFactory()),this.n1,0,0); 50 51 TuxGuitar.instance().fireUpdate(); 52 this.undoMarkers.undo(); 53 this.undoCaret.update(); 54 55 this.doAction = REDO_ACTION; 56 } 57 58 public boolean canRedo() { 59 return (this.doAction == REDO_ACTION); 60 } 61 62 public boolean canUndo() { 63 return (this.doAction == UNDO_ACTION); 64 } 65 66 public UndoableRemoveMeasure endUndo(){ 67 this.redoCaret = new UndoableCaretHelper(); 68 return this; 69 } 70 71 private class UndoMarkers{ 72 private List markers; 73 74 public UndoMarkers(){ 75 this.markers = new ArrayList(); 76 Iterator it = TuxGuitar.instance().getSongManager().getMarkers().iterator(); 77 while(it.hasNext()){ 78 this.markers.add(((TGMarker)it.next()).clone(TuxGuitar.instance().getSongManager().getFactory())); 79 } 80 } 81 82 public void undo(){ 83 TuxGuitar.instance().getSongManager().removeAllMarkers(); 84 Iterator it = this.markers.iterator(); 85 while(it.hasNext()){ 86 TGMarker marker = (TGMarker)it.next(); 87 TuxGuitar.instance().getSongManager().updateMarker(marker.clone(TuxGuitar.instance().getSongManager().getFactory())); 88 } 89 } 90 } 91 } 92