Undo per SWF

This commit is contained in:
Jindra Petřík
2024-10-07 20:30:07 +02:00
parent d4078d3a0c
commit dda394ef52
4 changed files with 89 additions and 52 deletions

View File

@@ -16,16 +16,19 @@
*/
package com.jpexs.decompiler.flash.easygui;
import com.jpexs.decompiler.flash.SWF;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
/**
*
* @author JPEXS
*/
public class UndoManager {
private int historyPos = 0;
private final List<DoableOperation> history = new ArrayList<>();
private final Map<SWF, Integer> historyPosMap = new WeakHashMap<>();
private final Map<SWF, List<DoableOperation>> historyMap = new WeakHashMap<>();
private final List<Runnable> changeListeners = new ArrayList<>();
@@ -43,61 +46,86 @@ public class UndoManager {
}
}
public void doOperation(DoableOperation doableOperation) {
public void doOperation(DoableOperation doableOperation, SWF swf) {
if (!historyMap.containsKey(swf)) {
historyMap.put(swf, new ArrayList<>());
}
if (!historyPosMap.containsKey(swf)) {
historyPosMap.put(swf, 0);
}
int historyPos = historyPosMap.get(swf);
List<DoableOperation> history = historyMap.get(swf);
//drop redos
while(history.size() > historyPos) {
history.remove(historyPos);
}
history.add(doableOperation);
historyPos++;
historyMap.get(swf).add(doableOperation);
historyPosMap.put(swf, historyPosMap.get(swf) + 1);
doableOperation.doOperation();
fireChange();
}
public boolean canUndo() {
return historyPos > 0;
public boolean canUndo(SWF swf) {
if (!historyPosMap.containsKey(swf)) {
return false;
}
return historyPosMap.get(swf) > 0;
}
public String getUndoName() {
if (!canUndo()) {
public String getUndoName(SWF swf) {
if (!canUndo(swf)) {
return null;
}
return history.get(historyPos - 1).getDescription();
return historyMap.get(swf).get(historyPosMap.get(swf) - 1).getDescription();
}
public void undo() {
if (historyPos == 0) {
public void undo(SWF swf) {
if (!canUndo(swf)) {
return;
}
historyPos--;
history.get(historyPos).undoOperation();
historyPosMap.put(swf, historyPosMap.get(swf) - 1);
historyMap.get(swf).get(historyPosMap.get(swf)).undoOperation();
fireChange();
}
public void redo() {
if (!canRedo()) {
public void redo(SWF swf) {
if (!canRedo(swf)) {
return;
}
history.get(historyPos).doOperation();
historyPos++;
historyMap.get(swf).get(historyPosMap.get(swf)).doOperation();
historyPosMap.put(swf, historyPosMap.get(swf) + 1);
fireChange();
}
public String getRedoName() {
if (!canRedo()) {
public String getRedoName(SWF swf) {
if (!canRedo(swf)) {
return null;
}
return history.get(historyPos).getDescription();
return historyMap.get(swf).get(historyPosMap.get(swf)).getDescription();
}
public boolean canRedo() {
return history.size() > historyPos;
public boolean canRedo(SWF swf) {
if (!historyMap.containsKey(swf)) {
return false;
}
return historyMap.get(swf).size() > historyPosMap.get(swf);
}
public void clear() {
history.clear();
historyPos = 0;
historyMap.clear();
historyPosMap.clear();
fireChange();
}
public void clear(SWF swf) {
if (!historyMap.containsKey(swf)) {
return;
}
historyMap.get(swf).clear();
historyPosMap.put(swf, 0);
fireChange();
}
}