Skip to content

Commit

Permalink
correct undo redo
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Sep 5, 2024
1 parent 0be5488 commit b618bae
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/main/java/ai/nets/samj/ij/ui/IJ1PromptsProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,15 @@ public class IJ1PromptsProvider implements PromptsResultsDisplay, MouseListener,
/**
* Save lists of polygons deleted at the same time to undo their deleting
*/
private Stack<List<PolygonRoi>> redoStack = new Stack<>();
private Stack<List<PolygonRoi>> redoStack = new Stack<>();
/**
* Tracks if Ctrl+Z has already been handled
*/
private boolean undoPressed = false;
/**
* Tracks if Ctrl+Y has already been handled
*/
private boolean redoPressed = false;
/**
* The number of words per line in the error message dialogs
*/
Expand Down Expand Up @@ -350,6 +358,7 @@ private void registerListeners() {
activeCanvas.addMouseListener(this);
activeCanvas.addKeyListener(this);
activeWindow.addWindowListener(this);
activeWindow.addKeyListener(this);
IJ.addEventListener(this);
}

Expand All @@ -360,6 +369,7 @@ private void deRegisterListeners() {
activeCanvas.removeMouseListener(this);
activeCanvas.removeKeyListener(this);
activeWindow.removeWindowListener(this);
activeWindow.removeKeyListener(this);
}

@Override
Expand Down Expand Up @@ -493,6 +503,7 @@ void addToRoiManager(final List<Polygon> polys, final String promptShape) {
final PolygonRoi pRoi = new PolygonRoi(p, PolygonRoi.POLYGON);
pRoi.setName(promptsCreatedCnt + "." + (resNo ++) + "_"+promptShape + "_" + promptsToNet.getName());
this.addToRoiManager(pRoi);
undoRois.add(pRoi);
}
this.undoStack.push(undoRois);
}
Expand Down Expand Up @@ -558,6 +569,12 @@ public void keyReleased(KeyEvent e) {
|| (e.getKeyCode() == KeyEvent.VK_META && PlatformDetection.isMacOS())) {
submitAndClearPoints();
}
if (e.getKeyCode() == KeyEvent.VK_Z) {
redoPressed = false;
}
if (e.getKeyCode() == KeyEvent.VK_Y) {
undoPressed = false;
}
}

@Override
Expand Down Expand Up @@ -685,7 +702,8 @@ public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_Z && this.undoStack.size() != 0) {
if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_Z && this.undoStack.size() != 0 && !redoPressed) {
redoPressed = true;
try {
List<PolygonRoi> redoList = undoStack.peek();
int n = this.roiManager.getCount() - 1;
Expand All @@ -694,7 +712,8 @@ public void keyPressed(KeyEvent e) {
redoStack.push(redoList);
} catch (Exception ex) {
}
} else if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_Y && this.redoStack.size() != 0) {
} else if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_Y && this.redoStack.size() != 0 && !undoPressed) {
undoPressed = true;
List<PolygonRoi> redoList = redoStack.peek();
for (PolygonRoi pol : redoList) this.addToRoiManager(pol);
redoStack.pop();
Expand Down

0 comments on commit b618bae

Please sign in to comment.