-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stupid implementation of ctrl-z - just snapshoting whole project on e…
…very action. should probably do with diff and child field change listeners...
- Loading branch information
klesun
authored and
klesun
committed
Jan 10, 2016
1 parent
499ff4e
commit 844cb77
Showing
6 changed files
with
107 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.klesun_model; | ||
|
||
// this class stores history to undo/redo | ||
// for now, i plan woodenly just store SheetMusic snapshot | ||
// after each handled event (if it differs from previous snapshot) | ||
|
||
import com.google.common.collect.EvictingQueue; | ||
import org.json.JSONObject; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.Deque; | ||
import java.util.Queue; | ||
import java.util.stream.StreamSupport; | ||
|
||
public class SnapshotStorage | ||
{ | ||
/** @TODO: storing snapshots is obviously not efficient (especially with the json string generation) | ||
* hang change listeners to fields of Models and store diff instead of snapshots */ | ||
final private Deque<JSONObject> snapshots = new ArrayDeque<>(); | ||
final private Deque<JSONObject> redoSnapshots = new ArrayDeque<>(); | ||
|
||
public void add(JSONObject snapshot) | ||
{ | ||
if (snapshots.isEmpty() || !areEqual(snapshot, snapshots.peekLast())) { | ||
|
||
snapshots.add(snapshot); | ||
redoSnapshots.clear(); | ||
|
||
if (snapshots.size() > 20) { | ||
snapshots.removeFirst(); | ||
} | ||
} | ||
} | ||
|
||
public Explain<JSONObject> undo() | ||
{ | ||
if (snapshots.size() >= 2) { | ||
JSONObject current = snapshots.pollLast(); | ||
redoSnapshots.add(current); | ||
|
||
return new Explain<>(snapshots.peekLast()); | ||
} else { | ||
return new Explain<>(false, "Here History Starts"); | ||
} | ||
} | ||
|
||
public Explain<JSONObject> redo() | ||
{ | ||
if (redoSnapshots.size() >= 2) { | ||
JSONObject current = redoSnapshots.pollLast(); | ||
snapshots.add(current); | ||
|
||
return new Explain<>(redoSnapshots.peekLast()); | ||
} else { | ||
return new Explain<>(false, "Here History Ends"); | ||
} | ||
} | ||
|
||
// probably will be faster if we assert without string generation... | ||
private static boolean areEqual(JSONObject a, JSONObject b) { | ||
return a.toString().equals(b.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters