fixed a bug in the new undo management

This commit is contained in:
hneemann 2019-05-28 19:58:01 +02:00
parent 27301f2da6
commit 9cd7f3c2c8
3 changed files with 33 additions and 2 deletions

View File

@ -157,7 +157,6 @@ public class Circuit implements Copyable<Circuit> {
*/ */
public void save(File filename) throws IOException { public void save(File filename) throws IOException {
save(new FileOutputStream(filename)); save(new FileOutputStream(filename));
origin = filename;
} }
/** /**
@ -200,6 +199,8 @@ public class Circuit implements Copyable<Circuit> {
if (original.measurementOrdering != null) if (original.measurementOrdering != null)
measurementOrdering.addAll(original.measurementOrdering); measurementOrdering.addAll(original.measurementOrdering);
origin = original.origin;
version = 1; version = 1;
} }
@ -219,6 +220,7 @@ public class Circuit implements Copyable<Circuit> {
circuit.attributes = new ElementAttributes(attributes); circuit.attributes = new ElementAttributes(attributes);
circuit.wires.addAll(wires); circuit.wires.addAll(wires);
circuit.visualElements.addAll(visualElements); circuit.visualElements.addAll(visualElements);
circuit.origin = origin;
return circuit; return circuit;
} }
@ -714,6 +716,15 @@ public class Circuit implements Copyable<Circuit> {
return origin; return origin;
} }
/**
* Sets the file name
*
* @param filename the file name
*/
public void setOrigin(File filename) {
this.origin = filename;
}
/** /**
* Visual element filter * Visual element filter
*/ */

View File

@ -490,6 +490,11 @@ public class CircuitComponent extends JComponent implements ChangedListener, Lib
*/ */
public void save(File filename) throws IOException { public void save(File filename) throws IOException {
getCircuit().save(filename); getCircuit().save(filename);
try {
undoManager.applyWithoutHistory(circuit -> circuit.setOrigin(filename));
} catch (ModifyException e) {
throw new RuntimeException("internal error in save", e);
}
undoManager.saved(); undoManager.saved();
} }

View File

@ -10,7 +10,8 @@ import java.util.ArrayList;
/** /**
* Class which implements Undo/Redo logic. * Class which implements Undo/Redo logic.
* Uses an event sourcing approach. * Uses an event sourcing approach.
* Make sure that no modifications are made beside the {@link UndoManager#apply(Modification)} method! * Make sure that no modifications are made beside the {@link UndoManager#apply(Modification)}
* or {@link UndoManager#applyWithoutHistory(Modification)} method!
* *
* @param <A> the structure to modify * @param <A> the structure to modify
*/ */
@ -198,4 +199,18 @@ public class UndoManager<A extends Copyable<A>> {
listeners.remove(listener); listeners.remove(listener);
} }
/**
* Applies a modification to the object without a history record.
* Needs to be used to ensure all existing copies are modified.
*
* @param modification the modification
* @throws ModifyException ModifyException
*/
public void applyWithoutHistory(Modification<A> modification) throws ModifyException {
if (actual != null)
modification.modify(actual);
modification.modify(initial);
}
} }